From e09b654c91384acb98aa2246862aac43d3be8b71 Mon Sep 17 00:00:00 2001 From: Justus Dieckmann Date: Tue, 19 Mar 2024 15:36:40 +0100 Subject: [PATCH] Add tests for the filter --- filter.php | 10 ++-- tests/replacement_test.php | 106 +++++++++++++++++++++++++++++++++++++ tests/testable_filter.php | 55 +++++++++++++++++++ 3 files changed, 166 insertions(+), 5 deletions(-) create mode 100644 tests/replacement_test.php create mode 100644 tests/testable_filter.php diff --git a/filter.php b/filter.php index edfbfad..713b1f9 100644 --- a/filter.php +++ b/filter.php @@ -137,7 +137,7 @@ public function filter($text, array $options = []) { if (self::str_starts_with($match, "render_player($episode[0], $episode[1], $i++, $width, $height); } if ($replacement) { $newtext .= $replacement; @@ -149,14 +149,14 @@ public function filter($text, array $options = []) { $height = null; $texttoreplace = null; $currenttag = null; - } else if (!$episode && $currenttag === 'video' && self::str_starts_with($match, '\s]/', $match)) { $currenttag = 'video'; $width = self::get_attribute($match, 'width', '[0-9]+'); $height = self::get_attribute($match, 'height', '[0-9]+'); @@ -164,7 +164,7 @@ public function filter($text, array $options = []) { if ($src) { $episode = self::test_url($src, $occurrences); } - } else if (self::str_starts_with($match, '. + +/** + * Testcases for the opencast filter. + * + * @package filter_opencast + * @copyright 2024 Justus Dieckmann, University of Münster. + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace filter_opencast; +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/filter/opencast/tests/testable_filter.php'); + +/** + * Testcases for the opencast filter. + * + * @package filter_opencast + * @copyright 2024 Justus Dieckmann, University of Münster. + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @group filter_opencast + */ +class replacement_test extends \advanced_testcase { + + public function setUp(): void { + $this->resetAfterTest(); + set_config('episodeurl_1', "http://localhost:8080/play/[EPISODEID]\nhttps://stable.opencast.de/play/[EPISODEID]", + 'filter_opencast'); + } + + /** + * Actual test function. + * + * @dataProvider replacement_provider + * @covers \filter_opencast + * @param string $input input for filter + * @param string $output expected filter output. + */ + public function test_replacement($input, $output) { + $filter = new testable_filter(\context_system::instance(), []); + $this->assertEquals($output, $filter->filter($input)); + } + + /** + * Provides test cases. + */ + public function replacement_provider() { + return [ + [ + '

hello

', + '

hello

' + ], + [ + '', + '' + ], + [ + '', + '' + ], + [ + 'begin end', + 'begin end' + ], + [ + 'and a link
link +look, a video!', + 'and a link link +' + ], + [ + 'and now two look, a video! +.', + 'and now two +.' + ], + ]; + } + +} diff --git a/tests/testable_filter.php b/tests/testable_filter.php new file mode 100644 index 0000000..2ddaed1 --- /dev/null +++ b/tests/testable_filter.php @@ -0,0 +1,55 @@ +. + +/** + * Testable opencast filter. + * + * @package filter_opencast + * @copyright 2024 Justus Dieckmann, University of Münster. + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace filter_opencast; +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/filter/opencast/filter.php'); + +/** + * Testable opencast filter. + * + * @package filter_opencast + * @copyright 2024 Justus Dieckmann, University of Münster. + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class testable_filter extends \filter_opencast { + + /** + * Render a simple + * @param int $ocinstanceid Id of ocinstance. + * @param string $episodeid Id opencast episode. + * @param int $playerid Unique id to assign to player element. + * @param int|null $width Optionally width for player. + * @param int|null $height Optionally height for player. + * @return string|null + */ + protected function render_player(int $ocinstanceid, string $episodeid, int $playerid, $width = null, + $height = null): string|null { + return ''; + } + +}