MDL-54853 weblib: Properly specify encoding for blanktarget
[moodle.git] / lib / tests / weblib_format_text_test.php
CommitLineData
7d94679f
TH
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/>.
16
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 */
25
26defined('MOODLE_INTERNAL') || die();
27
28
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 */
35class core_weblib_format_text_testcase extends advanced_testcase {
36
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 }
43
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 }
50
51 public function test_format_text_format_plain() {
0d6f53e8
EL
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 }
58
59 public function test_format_text_format_plain_no_filters() {
7d94679f
TH
60 $this->resetAfterTest();
61 filter_set_global_state('emoticon', TEXTFILTER_ON);
62 $this->assertEquals(':-)',
63 format_text(':-)', FORMAT_PLAIN, array('filter' => false)));
64 }
65
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 }
72
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 }
79
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 }
86
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 }
93
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 }
bf997487
CB
98
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 }
110
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 [
2549f46f
CB
118 'Simple link' => [
119 '<a href="https://www.youtube.com/watch?v=JeimE8Wz6e4">Hey, that\'s pretty good!</a>',
120 '<div class="text_to_html"><a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" target="_blank"' .
121 ' rel="noreferrer">Hey, that\'s pretty good!</a></div>'
122 ],
123 'Link with rel' => [
124 '<a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" rel="nofollow">Hey, that\'s pretty good!</a>',
125 '<div class="text_to_html"><a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" rel="nofollow noreferrer"' .
126 ' target="_blank">Hey, that\'s pretty good!</a></div>'
127 ],
128 'Link with rel noreferrer' => [
129 '<a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" rel="noreferrer">Hey, that\'s pretty good!</a>',
130 '<div class="text_to_html"><a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" rel="noreferrer"' .
131 ' target="_blank">Hey, that\'s pretty good!</a></div>'
132 ],
133 'Link with target' => [
134 '<a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" target="_self">Hey, that\'s pretty good!</a>',
135 '<div class="text_to_html"><a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" target="_self">' .
136 'Hey, that\'s pretty good!</a></div>'
137 ],
138 'Link with target blank' => [
139 '<a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" target="_blank">Hey, that\'s pretty good!</a>',
140 '<div class="text_to_html"><a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" target="_blank"' .
141 ' rel="noreferrer">Hey, that\'s pretty good!</a></div>'
142 ],
143 'Link with Frank\'s casket inscription' => [
144 '<a href="https://en.wikipedia.org/wiki/Franks_Casket">ᚠᛁᛋᚳ᛫ᚠᛚᚩᛞᚢ᛫ᚪᚻᚩᚠᚩᚾᚠᛖᚱᚷ ᛖᚾᛒᛖᚱᛁᚷ ᚹᚪᚱᚦᚷᚪ᛬ᛋᚱᛁᚳᚷᚱᚩᚱᚾᚦᚫᚱᚻᛖᚩᚾᚷᚱᛖᚢᛏᚷᛁᛋᚹᚩᛗ ᚻ' .
145 'ᚱᚩᚾᚫᛋᛒᚪᚾ ᛗᚫᚷᛁᚠᛁᛋᚳ᛫ᚠᛚᚩᛞᚢ᛫ᚪᚻᚩᚠᚩᚾᚠᛖᚱᚷ ᛖᚾᛒᛖᚱᛁᚷ ᚹᚪᚱᚦᚷᚪ᛬ᛋᚱᛁᚳᚷᚱᚩᚱᚾᚦᚫᚱᚻᛖᚩᚾᚷᚱᛖᚢᛏᚷᛁᛋᚹᚩᛗ ᚻᚱᚩᚾᚫᛋᛒᚪᚾ ᛗᚫᚷᛁ</a>',
146 '<div class="text_to_html"><a href="https://en.wikipedia.org/wiki/Franks_Casket" target="_blank" ' .
147 'rel="noreferrer">ᚠᛁᛋᚳ᛫ᚠᛚᚩᛞᚢ᛫ᚪᚻᚩᚠᚩᚾᚠᛖᚱᚷ ᛖᚾᛒᛖᚱᛁᚷ ᚹᚪᚱᚦᚷᚪ᛬ᛋᚱᛁᚳᚷᚱᚩᚱᚾᚦᚫᚱᚻᛖᚩᚾᚷᚱᛖᚢᛏᚷᛁᛋᚹᚩᛗ ᚻᚱᚩᚾᚫᛋᛒᚪᚾ ᛗᚫᚷᛁᚠᛁᛋᚳ᛫ᚠᛚᚩᛞᚢ᛫ᚪᚻᚩᚠᚩᚾᚠᛖᚱᚷ ᛖᚾ' .
148 'ᛒᛖᚱᛁᚷ ᚹᚪᚱᚦᚷᚪ᛬ᛋᚱᛁᚳᚷᚱᚩᚱᚾᚦᚫᚱᚻᛖᚩᚾᚷᚱᛖᚢᛏᚷᛁᛋᚹᚩᛗ ᚻᚱᚩᚾᚫᛋᛒᚪᚾ ᛗᚫᚷᛁ</a></div>'
149 ],
150 'No link' => [
151 'Some very boring text written with the Latin script',
152 '<div class="text_to_html">Some very boring text written with the Latin script</div>'
153 ],
154 'No link with Thror\'s map runes' => [
155 'ᛋᛏᚫᚾᛞ ᛒᚣ ᚦᛖ ᚷᚱᛖᚣ ᛋᛏᚩᚾᛖ ᚻᚹᛁᛚᛖ ᚦᛖ ᚦᚱᚢᛋᚻ ᚾᚩᚳᛋ ᚫᚾᛞ ᚦᛖ ᛋᛖᛏᛏᛁᚾᚷ ᛋᚢᚾ ᚹᛁᚦ ᚦᛖ ᛚᚫᛋᛏ ᛚᛁᚷᚻᛏ ᚩᚠ ᛞᚢᚱᛁᚾᛋ ᛞᚫᚣ ᚹᛁᛚᛚ ᛋᚻᛁᚾᛖ ᚢᛈᚩᚾ ᚦᛖ ᚳᛖᚣᚻᚩᛚᛖ',
156 '<div class="text_to_html">ᛋᛏᚫᚾᛞ ᛒᚣ ᚦᛖ ᚷᚱᛖᚣ ᛋᛏᚩᚾᛖ ᚻᚹᛁᛚᛖ ᚦᛖ ᚦᚱᚢᛋᚻ ᚾᚩᚳᛋ ᚫᚾᛞ ᚦᛖ ᛋᛖᛏᛏᛁᚾᚷ ᛋᚢᚾ ᚹᛁᚦ ᚦᛖ ᛚᚫᛋᛏ ᛚᛁᚷᚻᛏ ᚩᚠ ᛞᚢᚱᛁᚾᛋ ᛞᚫᚣ ᚹ' .
157 'ᛁᛚᛚ ᛋᚻᛁᚾᛖ ᚢᛈᚩᚾ ᚦᛖ ᚳᛖᚣᚻᚩᛚᛖ</div>'
158 ]
bf997487
CB
159 ];
160 }
7d94679f 161}