a7aa3bfdfe5aeef99211bd4ba62a5b3c730be815
[moodle.git] / lib / tests / weblib_format_text_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
13 //
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/>.
17 /**
18  * Unit tests for format_text defined in weblib.php.
19  *
20  * @package   core
21  * @category  test
22  * @copyright 2015 The Open University
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU Public License
24  */
26 defined('MOODLE_INTERNAL') || die();
29 /**
30  * Unit tests for format_text defined in weblib.php.
31  *
32  * @copyright 2015 The Open University
33  * @license   http://www.gnu.org/copyleft/gpl.html GNU Public License
34  */
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));
42     }
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)));
49     }
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));
57     }
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)));
64     }
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));
71     }
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)));
78     }
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));
85     }
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)));
92     }
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)));
97     }
99     /**
100      * Test adding blank target attribute to links
101      *
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
105      */
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);
109     }
111     /**
112      * Data provider for the test_format_text_blanktarget testcase
113      *
114      * @return array of testcases
115      */
116     public function format_text_blanktarget_testcases() {
117         return [
118             'Simple link' =>
119                 [
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>'
123                 ],
124             'Link with rel' =>
125                 [
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>'
129                 ],
130             'Link with rel noreferrer' =>
131                 [
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>'
135                 ],
136             'Link with target' =>
137                 [
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>'
141                 ],
142             'Link with target blank' =>
143                 [
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>'
147                 ]
148         ];
149     }