* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/filter/mediaplugin/filter.php'); // Include the code to test
class filter_mediaplugin_testcase extends advanced_testcase {
function test_filter_mediaplugin_link() {
$this->resetAfterTest(true);
// we need to enable the plugins somehow and the flash fallback.
\core\plugininfo\media::set_enabled_plugins('vimeo,youtube,videojs,html5video,swf,html5audio');
set_config('useflash', true, 'media_videojs');
$filterplugin = new filter_mediaplugin(null, array());
$longurl = 'my test file';
$longhref = '';
do {
$longhref .= 'a';
} while(strlen($longhref) + strlen($longurl) < 4095);
$longurl = 'my test file';
$validtexts = array (
'test mp3',
'test ogg',
'test mp4',
'test',
'test file',
'test file',
'test file',
'test file',
'test file',
'test flv',
'test file',
'test ogg',
'test mp3',
'test mp3',
'test mp3',
'youtube\'s',
'
test mp3',
'test wav
',
'youtube\'s',
// Test a long URL under 4096 characters.
$longurl
);
//test for valid link
foreach ($validtexts as $text) {
$msg = "Testing text: ". $text;
$filter = $filterplugin->filter($text);
$this->assertNotEquals($text, $filter, $msg);
}
$insertpoint = strrpos($longurl, 'http://');
$longurl = substr_replace($longurl, 'http://pushover4096chars', $insertpoint, 0);
$originalurl = 'Some text.
' .
'Valid link
';
$paddedurl = str_pad($originalurl, 6000, 'z');
$validpaddedurl = 'Some text.
';
$validpaddedurl = str_pad($validpaddedurl, 6000 + (strlen($validpaddedurl) - strlen($originalurl)), 'z');
$invalidtexts = array(
'href="http://moodle.org/testfile/test.mp3"',
'test test',
'test test',
'test test',
'test test',
'sample',
'',
'test',
'test mp3',
'',
'test',
'test',
'test mp3',
'test mp3',
'test mp3',
// Test a long URL over 4096 characters.
$longurl
);
//test for invalid link
foreach ($invalidtexts as $text) {
$msg = "Testing text: ". $text;
$filter = $filterplugin->filter($text);
$this->assertEquals($text, $filter, $msg);
}
// Valid mediaurl followed by a longurl.
$precededlongurl = 'test.mp3'. $longurl;
$filter = $filterplugin->filter($precededlongurl);
$this->assertEquals(1, substr_count($filter, ''));
$this->assertContains($longurl, $filter);
// Testing for cases where: to be filtered content has 6+ text afterwards.
$filter = $filterplugin->filter($paddedurl);
$this->assertEquals($validpaddedurl, $filter, $msg);
}
}