Skip to content

Commit

Permalink
Add tests for the filter
Browse files Browse the repository at this point in the history
  • Loading branch information
justusdieckmann committed Mar 19, 2024
1 parent a80464e commit e09b654
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 5 deletions.
10 changes: 5 additions & 5 deletions filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function filter($text, array $options = []) {
if (self::str_starts_with($match, "</$currenttag")) {
$replacement = null;
if ($episode) {
$replacement = self::render_player($episode[0], $episode[1], $i++, $width, $height);
$replacement = $this->render_player($episode[0], $episode[1], $i++, $width, $height);
}
if ($replacement) {
$newtext .= $replacement;
Expand All @@ -149,22 +149,22 @@ public function filter($text, array $options = []) {
$height = null;
$texttoreplace = null;
$currenttag = null;
} else if (!$episode && $currenttag === 'video' && self::str_starts_with($match, '<source ')) {
} else if (!$episode && $currenttag === 'video' && preg_match('/^<source\s/', $match)) {
$src = self::get_attribute($match, 'src');
if ($src) {
$episode = self::test_url($src, $occurrences);
}
}
} else {
if (self::str_starts_with($match, '<video ')) {
if (preg_match('/^<video[>\s]/', $match)) {
$currenttag = 'video';
$width = self::get_attribute($match, 'width', '[0-9]+');
$height = self::get_attribute($match, 'height', '[0-9]+');
$src = self::get_attribute($match, 'src');
if ($src) {
$episode = self::test_url($src, $occurrences);
}
} else if (self::str_starts_with($match, '<a ')) {
} else if (preg_match('/^<a\s/', $match)) {
$src = self::get_attribute($match, 'href');
if ($src) {
$episode = self::test_url($src, $occurrences);
Expand Down Expand Up @@ -195,7 +195,7 @@ public function filter($text, array $options = []) {
* @param int|null $height Optionally height for player.
* @return string|null
*/
private static function render_player(int $ocinstanceid, string $episodeid, int $playerid,
protected function render_player(int $ocinstanceid, string $episodeid, int $playerid,
$width = null, $height = null): string|null {
global $OUTPUT, $PAGE;

Expand Down
106 changes: 106 additions & 0 deletions tests/replacement_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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 [
[
' <p> hello </p> <video src="http://localhost:8080/play/f78ac136-8252-4b8e-bfea-4786c6993f03"> hello </video>',
' <p> hello </p> <oc-video episode="f78ac136-8252-4b8e-bfea-4786c6993f03"/>'
],
[
'<video src="https://somethingother.com"></video><video>
<source
src="https://stable.opencast.de/play/370e5bef-1d59-4440-858a-4df62e767dfc">
</video>',
'<video src="https://somethingother.com"></video><oc-video episode="370e5bef-1d59-4440-858a-4df62e767dfc"/>'
],
[
'<video
autoplay loopdiloop
src="http://localhost:8080/play/f9e7b289-c8be-462f-80bf-d1f493c6ed55"></video>',
'<oc-video episode="f9e7b289-c8be-462f-80bf-d1f493c6ed55"/>'
],
[
'begin <video>
<source src="https://somethingother.de/play/4380f73a-47a6-41c6-b854-ec0fa9d0261b">
<source
src="https://stable.opencast.de/play/2e0ca3bb-df8e-4913-9380-c925efaf5ac2">
</video> end',
'begin <oc-video episode="2e0ca3bb-df8e-4913-9380-c925efaf5ac2"/> end'
],
[
'and a link <a href="https://www.google.com">link</a>
<a href="http://localhost:8080/play/09b9d154-c849-429d-adea-3df4f76429b6">look, a video!</a>',
'and a link <a href="https://www.google.com">link</a>
<oc-video episode="09b9d154-c849-429d-adea-3df4f76429b6"/>'
],
[
'and now two <a
href="http://localhost:8080/play/64b085e9-0142-4a10-a08e-3dbce055e740">look, a video!</a>
<video src="http://localhost:8080/play/329885fe-d18e-4c6b-a896-dbc66463a6b2"></video>.',
'and now two <oc-video episode="64b085e9-0142-4a10-a08e-3dbce055e740"/>
<oc-video episode="329885fe-d18e-4c6b-a896-dbc66463a6b2"/>.'
],
];
}

}
55 changes: 55 additions & 0 deletions tests/testable_filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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 '<oc-video episode="'. $episodeid . '"/>';
}

}

0 comments on commit e09b654

Please sign in to comment.