芝麻web文件管理V1.00
编辑当前文件:/home2/sdektunc/www/cepali/filter/emoticon/tests/filter_test.php
. /** * Skype icons filter phpunit tests * * @package filter_emoticon * @category test * @copyright 2013 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/filter/emoticon/filter.php'); // Include the code to test. /** * Skype icons filter testcase. */ class filter_emoticon_testcase extends advanced_testcase { /** * Tests the filter doesn't affect nolink classes. * * @dataProvider filter_emoticon_provider */ public function test_filter_emoticon($input, $format, $expected) { $this->resetAfterTest(); $filter = new testable_filter_emoticon(); $this->assertEquals($expected, $filter->filter($input, [ 'originalformat' => $format, ])); } /** * The data provider for filter emoticon tests. * * @return array */ public function filter_emoticon_provider() { $grr = '(grr)'; return [ 'FORMAT_MOODLE is not filtered' => [ 'input' => $grr, 'format' => FORMAT_MOODLE, 'expected' => $grr, ], 'FORMAT_MARKDOWN is not filtered' => [ 'input' => $grr, 'format' => FORMAT_MARKDOWN, 'expected' => $grr, ], 'FORMAT_PLAIN is not filtered' => [ 'input' => $grr, 'format' => FORMAT_PLAIN, 'expected' => $grr, ], 'FORMAT_HTML is filtered' => [ 'input' => $grr, 'format' => FORMAT_HTML, 'expected' => $this->get_converted_content_for_emoticon($grr), ], 'Script tag should not be processed' => [ 'input' => "", 'format' => FORMAT_HTML, 'expected' => "", ], 'Basic nolink should not be processed' => [ 'input' => '
(n)
', 'format' => FORMAT_HTML, 'expected' => '
(n)
', ], 'Nested nolink should not be processed' => [ 'input' => '
(n)
(n)
', 'format' => FORMAT_HTML, 'expected' => '
(n)
(n)
', ], 'Nested nolink should not be processed but following emoticon' => [ 'input' => '
(n)
(n)
(n)', 'format' => FORMAT_HTML, 'expected' => '
(n)
(n)
' . $this->get_converted_content_for_emoticon('(n)'), ], 'Basic pre should not be processed' => [ 'input' => '
(n)
', 'format' => FORMAT_HTML, 'expected' => '
(n)
', ], 'Nested pre should not be processed' => [ 'input' => '
(n)
(n)
', 'format' => FORMAT_HTML, 'expected' => '
(n)
(n)
', ], 'Nested pre should not be processed but following emoticon' => [ 'input' => '
(n)
(n)
(n)', 'format' => FORMAT_HTML, 'expected' => '
(n)
(n)
' . $this->get_converted_content_for_emoticon('(n)'), ], ]; } /** * Translate the text for a single emoticon into the rendered value. * * @param string $text The text to translate. * @return string */ public function get_converted_content_for_emoticon($text) { global $OUTPUT; $manager = get_emoticon_manager(); $emoticons = $manager->get_emoticons(); foreach ($emoticons as $emoticon) { if ($emoticon->text == $text) { return $OUTPUT->render($manager->prepare_renderable_emoticon($emoticon)); } } return $text; } /** * Tests the filter doesn't break anything if activated but invalid format passed. * */ public function test_filter_invalidformat() { global $PAGE; $this->resetAfterTest(); $filter = new testable_filter_emoticon(); $input = '(grr)'; $expected = '(grr)'; $this->assertEquals($expected, $filter->filter($input, [ 'originalformat' => 'ILLEGALFORMAT', ])); } /** * Tests the filter doesn't break anything if activated but no emoticons available. * */ public function test_filter_emptyemoticons() { global $CFG; $this->resetAfterTest(); // Empty the emoticons array. $CFG->emoticons = null; $filter = new filter_emoticon(context_system::instance(), array('originalformat' => FORMAT_HTML)); $input = '(grr)'; $expected = '(grr)'; $this->assertEquals($expected, $filter->filter($input, [ 'originalformat' => FORMAT_HTML, ])); } } /** * Subclass for easier testing. */ class testable_filter_emoticon extends filter_emoticon { public function __construct() { // Reset static emoticon caches. parent::$emoticontexts = array(); parent::$emoticonimgs = array(); // Use this context for filtering. $this->context = context_system::instance(); // Define FORMAT_HTML as only one filtering in DB. set_config('formats', implode(',', array(FORMAT_HTML)), 'filter_emoticon'); } }