From c7d02e4c90c11fbc664021b478d2d10f6e212961 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 27 Mar 2018 08:46:55 +0800 Subject: [PATCH 1/1] MDL-61732 filter_emoticon: Extend unit tests --- filter/emoticon/tests/filter_test.php | 94 +++++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 27 deletions(-) diff --git a/filter/emoticon/tests/filter_test.php b/filter/emoticon/tests/filter_test.php index 63c71ce324..aa519210e4 100644 --- a/filter/emoticon/tests/filter_test.php +++ b/filter/emoticon/tests/filter_test.php @@ -34,42 +34,82 @@ require_once($CFG->dirroot . '/filter/emoticon/filter.php'); // Include the code class filter_emoticon_testcase extends advanced_testcase { /** - * Verify configured target formats are observed. Just that. + * Tests the filter doesn't affect nolink classes. + * + * @dataProvider filter_emoticon_provider */ - public function test_filter_emoticon_formats() { - - $this->resetAfterTest(true); // We are modifying the config. + public function test_filter_emoticon($input, $format, $expected) { + $this->resetAfterTest(); $filter = new testable_filter_emoticon(); - - // Verify texts not matching target formats aren't filtered. - $expected = '(grr)'; - $options = array('originalformat' => FORMAT_MOODLE); // Only FORMAT_HTML is filtered, see {@link testable_filter_emoticon}. - $this->assertEquals($expected, $filter->filter('(grr)', $options)); - - $options = array('originalformat' => FORMAT_MARKDOWN); // Only FORMAT_HTML is filtered, see {@link testable_filter_emoticon}. - $this->assertEquals($expected, $filter->filter('(grr)', $options)); - - $options = array('originalformat' => FORMAT_PLAIN); // Only FORMAT_HTML is filtered, see {@link testable_filter_emoticon}. - $this->assertEquals($expected, $filter->filter('(grr)', $options)); - - // And texts matching target formats are filtered. - $expected = 'angry'; - $options = array('originalformat' => FORMAT_HTML); // Only FORMAT_HTML is filtered, see {@link testable_filter_emoticon}. - $this->assertEquals($expected, $filter->filter('(grr)', $options)); + $this->assertEquals($expected, $filter->filter($input, [ + 'originalformat' => $format, + ])); } /** - * Tests the filter doesn't affect nolink classes. + * The data provider for filter emoticon tests. + * + * @return array */ - public function test_filter_emoticon_leave_nolink_be() { - $this->resetAfterTest(); + 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)', + ], + ]; + } - $filter = new testable_filter_emoticon(); + /** + * Translate the text for a sigle 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)); + } + } - $expected = '(n)'; - $options = array('originalformat' => FORMAT_HTML); // Only FORMAT_HTML is filtered, see {@link testable_filter_emoticon}. - $this->assertEquals($expected, $filter->filter('(n)', $options)); + return $text; } } -- 2.15.1