. /** * Unit test for the filter_mediaplugin * * @package filter_mediaplugin * @category phpunit * @copyright 2011 Rossiani Wijaya * @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() { global $CFG; $this->resetAfterTest(true); // we need to enable the plugins somehow $CFG->core_media_enable_youtube = 1; $CFG->core_media_enable_vimeo = 1; $CFG->core_media_enable_mp3 = 1; $CFG->core_media_enable_flv = 1; $CFG->core_media_enable_swf = 1; $CFG->core_media_enable_html5audio = 1; $CFG->core_media_enable_html5video = 1; $CFG->core_media_enable_qt = 1; $CFG->core_media_enable_wmp = 1; $CFG->core_media_enable_rm = 1; $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 mpg', 'test', 'test file', 'test file', 'test file', 'test file', 'test file', 'test flv', 'test file', 'test mp3', 'test mp3', 'test mp3', 'test mp3', 'youtube\'s', ' test mp3', 'test mp3 ', '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, 'M.util.add_audio_player'));
        $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);
    }
}