MDL-25826 add more HTMLPurifier integration tests
[moodle.git] / lib / simpletest / testweblib.php
CommitLineData
6ba65fa0 1<?php
2/**
3 * Unit tests for (some of) ../weblib.php.
4 *
5 * @copyright &copy; 2006 The Open University
6 * @author T.J.Hunt@open.ac.uk
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package moodlecore
9 */
10
cdbbd26f 11if (!defined('MOODLE_INTERNAL')) {
12 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
13}
6ba65fa0 14
cc4245a3 15class web_test extends UnitTestCase {
6ba65fa0 16
081a63a9 17 public static $includecoverage = array('lib/weblib.php');
18
6ba65fa0 19 function setUp() {
20 }
21
22 function tearDown() {
23 }
24
25 function test_format_string() {
a205dcdc 26 // Ampersands
b9c639d6 27 $this->assertEqual(format_string("& &&&&& &&"), "&amp; &amp;&amp;&amp;&amp;&amp; &amp;&amp;");
6ba65fa0 28 $this->assertEqual(format_string("ANother & &&&&& Category"), "ANother &amp; &amp;&amp;&amp;&amp;&amp; Category");
29 $this->assertEqual(format_string("ANother & &&&&& Category", true), "ANother &amp; &amp;&amp;&amp;&amp;&amp; Category");
30 $this->assertEqual(format_string("Nick's Test Site & Other things", true), "Nick's Test Site &amp; Other things");
b9c639d6 31
a205dcdc 32 // String entities
33 $this->assertEqual(format_string("&quot;"), "&quot;");
b9c639d6 34
a205dcdc 35 // Digital entities
36 $this->assertEqual(format_string("&11234;"), "&11234;");
b9c639d6 37
a205dcdc 38 // Unicode entities
39 $this->assertEqual(format_string("&#4475;"), "&#4475;");
b9c639d6 40 }
41
dacb47c0 42 function test_s() {
b9c639d6 43 $this->assertEqual(s("This Breaks \" Strict"), "This Breaks &quot; Strict");
6dbcacee 44 $this->assertEqual(s("This Breaks <a>\" Strict</a>"), "This Breaks &lt;a&gt;&quot; Strict&lt;/a&gt;");
dacb47c0 45 }
b9c639d6 46
977b3d31 47 function test_format_text_email() {
3e83641f 48 $this->assertEqual("\n\nThis is a TEST",
977b3d31 49 format_text_email('<p>This is a <strong>test</strong></p>',FORMAT_HTML));
3e83641f 50 $this->assertEqual("\n\nThis is a TEST",
977b3d31 51 format_text_email('<p class="frogs">This is a <strong class=\'fishes\'>test</strong></p>',FORMAT_HTML));
52 $this->assertEqual('& so is this',
3e83641f 53 format_text_email('&amp; so is this',FORMAT_HTML));
977b3d31 54 $tl = textlib_get_instance();
3e83641f 55 $this->assertEqual('Two bullets: '.$tl->code2utf8(8226).' *',
56 format_text_email('Two bullets: &#x2022; &#8226;',FORMAT_HTML));
977b3d31 57 $this->assertEqual($tl->code2utf8(0x7fd2).$tl->code2utf8(0x7fd2),
3e83641f 58 format_text_email('&#x7fd2;&#x7FD2;',FORMAT_HTML));
977b3d31 59 }
9289e4c9 60
61 function test_highlight() {
62 $this->assertEqual(highlight('good', 'This is good'), 'This is <span class="highlight">good</span>');
63 $this->assertEqual(highlight('SpaN', 'span'), '<span class="highlight">span</span>');
64 $this->assertEqual(highlight('span', 'SpaN'), '<span class="highlight">SpaN</span>');
65 $this->assertEqual(highlight('span', '<span>span</span>'), '<span><span class="highlight">span</span></span>');
66 $this->assertEqual(highlight('good is', 'He is good'), 'He <span class="highlight">is</span> <span class="highlight">good</span>');
67 $this->assertEqual(highlight('+good', 'This is good'), 'This is <span class="highlight">good</span>');
68 $this->assertEqual(highlight('-good', 'This is good'), 'This is good');
69 $this->assertEqual(highlight('+good', 'This is goodness'), 'This is goodness');
70 $this->assertEqual(highlight('good', 'This is goodness'), 'This is <span class="highlight">good</span>ness');
71 }
6dbcacee 72
f8065dd2 73 function test_replace_ampersands() {
6dbcacee 74 $this->assertEqual(replace_ampersands_not_followed_by_entity("This & that &nbsp;"), "This &amp; that &nbsp;");
75 $this->assertEqual(replace_ampersands_not_followed_by_entity("This &nbsp that &nbsp;"), "This &amp;nbsp that &nbsp;");
76 }
31c2087d 77
78 function test_strip_links() {
79 $this->assertEqual(strip_links('this is a <a href="http://someaddress.com/query">link</a>'), 'this is a link');
80 }
81
82 function test_wikify_links() {
83 $this->assertEqual(wikify_links('this is a <a href="http://someaddress.com/query">link</a>'), 'this is a link [ http://someaddress.com/query ]');
84 }
85
86 function test_fix_non_standard_entities() {
f66af887
PS
87 $this->assertEqual(fix_non_standard_entities('&#x00A3&#0228'), '&#xA3;&#228;');
88 $this->assertEqual(fix_non_standard_entities('&#x00A3;&#0228;'), '&#xA3;&#228;');
31c2087d 89 }
90
827b2f7a 91 function test_compare_url() {
92 $url1 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2));
93 $url2 = new moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
94
95 $this->assertFalse($url1->compare($url2, URL_MATCH_BASE));
96 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
97 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
98
99 $url2 = new moodle_url('index.php', array('var1' => 1, 'var3' => 3));
100
101 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
102 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
103 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
104
105 $url2 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
106
107 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
108 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
109 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
110
111 $url2 = new moodle_url('index.php', array('var2' => 2, 'var1' => 1));
112
113 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
114 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
115 $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT));
116 }
db4f638a 117
a194c218
TH
118 public function test_html_to_text_simple() {
119 $this->assertEqual("\n\n_Hello_ WORLD!", html_to_text('<p><i>Hello</i> <b>world</b>!</p>'));
120 }
121
122 public function test_html_to_text_image() {
123 $this->assertEqual('[edit]', html_to_text('<img src="edit.png" alt="edit" />'));
124 }
125
6cc09234
TH
126 public function test_html_to_text_image_with_backslash() {
127 $this->assertEqual('[\edit]', html_to_text('<img src="edit.png" alt="\edit" />'));
128 }
129
a194c218
TH
130 public function test_html_to_text_nowrap() {
131 $long = "Here is a long string, more than 75 characters long, since by default html_to_text wraps text at 75 chars.";
132 $this->assertEqual($long, html_to_text($long, 0));
133 }
e6906df2 134
6cb034c9
TH
135 public function test_html_to_text_dont_screw_up_utf8() {
136 $this->assertEqual("\n\nAll the WORLD’S a stage.", html_to_text('<p>All the <strong>world’s</strong> a stage.</p>'));
137 }
138
e6906df2
PS
139 public function test_clean_text() {
140 $text = "lala <applet>xx</applet>";
141 $this->assertEqual($text, clean_text($text, FORMAT_PLAIN));
142 $this->assertEqual('lala xx', clean_text($text, FORMAT_MARKDOWN));
143 $this->assertEqual('lala xx', clean_text($text, FORMAT_MOODLE));
144 $this->assertEqual('lala xx', clean_text($text, FORMAT_HTML));
145 }
b031caf8 146
6ba65fa0 147}
827b2f7a 148
8926f844 149