2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Unit tests for format_text defined in weblib.php.
22 * @copyright 2015 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26 defined('MOODLE_INTERNAL') || die();
30 * Unit tests for format_text defined in weblib.php.
32 * @copyright 2015 The Open University
33 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
35 class core_weblib_format_text_testcase extends advanced_testcase {
37 public function test_format_text_format_html() {
38 $this->resetAfterTest();
39 filter_set_global_state('emoticon', TEXTFILTER_ON);
40 $this->assertRegExp('~^<p><img class="emoticon" alt="smile" ([^>]+)></p>$~',
41 format_text('<p>:-)</p>', FORMAT_HTML));
44 public function test_format_text_format_html_no_filters() {
45 $this->resetAfterTest();
46 filter_set_global_state('emoticon', TEXTFILTER_ON);
47 $this->assertEquals('<p>:-)</p>',
48 format_text('<p>:-)</p>', FORMAT_HTML, array('filter' => false)));
51 public function test_format_text_format_plain() {
52 // Note FORMAT_PLAIN does not filter ever, no matter we ask for filtering.
53 $this->resetAfterTest();
54 filter_set_global_state('emoticon', TEXTFILTER_ON);
55 $this->assertEquals(':-)',
56 format_text(':-)', FORMAT_PLAIN));
59 public function test_format_text_format_plain_no_filters() {
60 $this->resetAfterTest();
61 filter_set_global_state('emoticon', TEXTFILTER_ON);
62 $this->assertEquals(':-)',
63 format_text(':-)', FORMAT_PLAIN, array('filter' => false)));
66 public function test_format_text_format_markdown() {
67 $this->resetAfterTest();
68 filter_set_global_state('emoticon', TEXTFILTER_ON);
69 $this->assertRegExp('~^<p><em><img class="emoticon" alt="smile" ([^>]+)></em></p>\n$~',
70 format_text('*:-)*', FORMAT_MARKDOWN));
73 public function test_format_text_format_markdown_nofilter() {
74 $this->resetAfterTest();
75 filter_set_global_state('emoticon', TEXTFILTER_ON);
76 $this->assertEquals("<p><em>:-)</em></p>\n",
77 format_text('*:-)*', FORMAT_MARKDOWN, array('filter' => false)));
80 public function test_format_text_format_moodle() {
81 $this->resetAfterTest();
82 filter_set_global_state('emoticon', TEXTFILTER_ON);
83 $this->assertRegExp('~^<div class="text_to_html"><p><img class="emoticon" alt="smile" ([^>]+)></p></div>$~',
84 format_text('<p>:-)</p>', FORMAT_MOODLE));
87 public function test_format_text_format_moodle_no_filters() {
88 $this->resetAfterTest();
89 filter_set_global_state('emoticon', TEXTFILTER_ON);
90 $this->assertEquals('<div class="text_to_html"><p>:-)</p></div>',
91 format_text('<p>:-)</p>', FORMAT_MOODLE, array('filter' => false)));
94 public function test_format_text_overflowdiv() {
95 $this->assertEquals('<div class="no-overflow"><p>:-)</p></div>',
96 format_text('<p>:-)</p>', FORMAT_HTML, array('overflowdiv' => true)));
100 * Test adding blank target attribute to links
102 * @dataProvider format_text_blanktarget_testcases
103 * @param string $link The link to add target="_blank" to
104 * @param string $expected The expected filter value
106 public function test_format_text_blanktarget($link, $expected) {
107 $actual = format_text($link, FORMAT_MOODLE, array('blanktarget' => true, 'filter' => false, 'noclean' => true));
108 $this->assertEquals($expected, $actual);
112 * Data provider for the test_format_text_blanktarget testcase
114 * @return array of testcases
116 public function format_text_blanktarget_testcases() {
120 '<a href="https://www.youtube.com/watch?v=JeimE8Wz6e4">Hey, that\'s pretty good!</a>',
121 '<div class="text_to_html"><a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" target="_blank"' .
122 ' rel="noreferrer">Hey, that\'s pretty good!</a></div>'
126 '<a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" rel="nofollow">Hey, that\'s pretty good!</a>',
127 '<div class="text_to_html"><a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" rel="nofollow noreferrer"' .
128 ' target="_blank">Hey, that\'s pretty good!</a></div>'
130 'Link with rel noreferrer' =>
132 '<a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" rel="noreferrer">Hey, that\'s pretty good!</a>',
133 '<div class="text_to_html"><a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" rel="noreferrer"' .
134 ' target="_blank">Hey, that\'s pretty good!</a></div>'
136 'Link with target' =>
138 '<a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" target="_self">Hey, that\'s pretty good!</a>',
139 '<div class="text_to_html"><a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" target="_self">' .
140 'Hey, that\'s pretty good!</a></div>'
142 'Link with target blank' =>
144 '<a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" target="_blank">Hey, that\'s pretty good!</a>',
145 '<div class="text_to_html"><a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" target="_blank"' .
146 ' rel="noreferrer">Hey, that\'s pretty good!</a></div>'