weekly release 2.3dev
[moodle.git] / lib / tests / weblib_test.php
CommitLineData
a3d5830a
PS
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 * These tests rely on the rsstest.xml file on download.moodle.org,
19 * from eloys listing:
20 * rsstest.xml: One valid rss feed.
21 * md5: 8fd047914863bf9b3a4b1514ec51c32c
22 * size: 32188
23 *
24 * If networking/proxy configuration is wrong these tests will fail..
25 *
26 * @package core
27 * @category phpunit
28 * @copyright &copy; 2006 The Open University
29 * @author T.J.Hunt@open.ac.uk
30 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
31 */
32
33defined('MOODLE_INTERNAL') || die();
34
35
f0202ae9 36class web_testcase extends advanced_testcase {
a3d5830a
PS
37
38 function test_format_string() {
39 global $CFG;
40
41 // Ampersands
42 $this->assertEquals(format_string("& &&&&& &&"), "&amp; &amp;&amp;&amp;&amp;&amp; &amp;&amp;");
43 $this->assertEquals(format_string("ANother & &&&&& Category"), "ANother &amp; &amp;&amp;&amp;&amp;&amp; Category");
44 $this->assertEquals(format_string("ANother & &&&&& Category", true), "ANother &amp; &amp;&amp;&amp;&amp;&amp; Category");
45 $this->assertEquals(format_string("Nick's Test Site & Other things", true), "Nick's Test Site &amp; Other things");
46
47 // String entities
48 $this->assertEquals(format_string("&quot;"), "&quot;");
49
50 // Digital entities
51 $this->assertEquals(format_string("&11234;"), "&11234;");
52
53 // Unicode entities
54 $this->assertEquals(format_string("&#4475;"), "&#4475;");
55
56 // < and > signs
57 $originalformatstringstriptags = $CFG->formatstringstriptags;
58
59 $CFG->formatstringstriptags = false;
60 $this->assertEquals(format_string('x < 1'), 'x &lt; 1');
61 $this->assertEquals(format_string('x > 1'), 'x &gt; 1');
62 $this->assertEquals(format_string('x < 1 and x > 0'), 'x &lt; 1 and x &gt; 0');
63
64 $CFG->formatstringstriptags = true;
65 $this->assertEquals(format_string('x < 1'), 'x &lt; 1');
66 $this->assertEquals(format_string('x > 1'), 'x &gt; 1');
67 $this->assertEquals(format_string('x < 1 and x > 0'), 'x &lt; 1 and x &gt; 0');
68
69 $CFG->formatstringstriptags = $originalformatstringstriptags;
70 }
71
72 function test_s() {
73 $this->assertEquals(s("This Breaks \" Strict"), "This Breaks &quot; Strict");
74 $this->assertEquals(s("This Breaks <a>\" Strict</a>"), "This Breaks &lt;a&gt;&quot; Strict&lt;/a&gt;");
75 }
76
77 function test_format_text_email() {
78 $this->assertEquals("This is a TEST",
79 format_text_email('<p>This is a <strong>test</strong></p>',FORMAT_HTML));
80 $this->assertEquals("This is a TEST",
81 format_text_email('<p class="frogs">This is a <strong class=\'fishes\'>test</strong></p>',FORMAT_HTML));
82 $this->assertEquals('& so is this',
83 format_text_email('&amp; so is this',FORMAT_HTML));
84 $this->assertEquals('Two bullets: '.textlib::code2utf8(8226).' *',
85 format_text_email('Two bullets: &#x2022; &#8226;',FORMAT_HTML));
86 $this->assertEquals(textlib::code2utf8(0x7fd2).textlib::code2utf8(0x7fd2),
87 format_text_email('&#x7fd2;&#x7FD2;',FORMAT_HTML));
88 }
89
90 function test_highlight() {
91 $this->assertEquals(highlight('good', 'This is good'), 'This is <span class="highlight">good</span>');
92 $this->assertEquals(highlight('SpaN', 'span'), '<span class="highlight">span</span>');
93 $this->assertEquals(highlight('span', 'SpaN'), '<span class="highlight">SpaN</span>');
94 $this->assertEquals(highlight('span', '<span>span</span>'), '<span><span class="highlight">span</span></span>');
95 $this->assertEquals(highlight('good is', 'He is good'), 'He <span class="highlight">is</span> <span class="highlight">good</span>');
96 $this->assertEquals(highlight('+good', 'This is good'), 'This is <span class="highlight">good</span>');
97 $this->assertEquals(highlight('-good', 'This is good'), 'This is good');
98 $this->assertEquals(highlight('+good', 'This is goodness'), 'This is goodness');
99 $this->assertEquals(highlight('good', 'This is goodness'), 'This is <span class="highlight">good</span>ness');
100 }
101
102 function test_replace_ampersands() {
103 $this->assertEquals(replace_ampersands_not_followed_by_entity("This & that &nbsp;"), "This &amp; that &nbsp;");
104 $this->assertEquals(replace_ampersands_not_followed_by_entity("This &nbsp that &nbsp;"), "This &amp;nbsp that &nbsp;");
105 }
106
107 function test_strip_links() {
108 $this->assertEquals(strip_links('this is a <a href="http://someaddress.com/query">link</a>'), 'this is a link');
109 }
110
111 function test_wikify_links() {
112 $this->assertEquals(wikify_links('this is a <a href="http://someaddress.com/query">link</a>'), 'this is a link [ http://someaddress.com/query ]');
113 }
114
115 function test_moodle_url_round_trip() {
116 $strurl = 'http://moodle.org/course/view.php?id=5';
117 $url = new moodle_url($strurl);
118 $this->assertEquals($strurl, $url->out(false));
119
120 $strurl = 'http://moodle.org/user/index.php?contextid=53&sifirst=M&silast=D';
121 $url = new moodle_url($strurl);
122 $this->assertEquals($strurl, $url->out(false));
123 }
124
125 function test_moodle_url_round_trip_array_params() {
126 $strurl = 'http://example.com/?a%5B1%5D=1&a%5B2%5D=2';
127 $url = new moodle_url($strurl);
128 $this->assertEquals($strurl, $url->out(false));
129
130 $url = new moodle_url('http://example.com/?a[1]=1&a[2]=2');
131 $this->assertEquals($strurl, $url->out(false));
132
133 // For un-keyed array params, we expect 0..n keys to be returned
134 $strurl = 'http://example.com/?a%5B0%5D=0&a%5B1%5D=1';
135 $url = new moodle_url('http://example.com/?a[]=0&a[]=1');
136 $this->assertEquals($strurl, $url->out(false));
137 }
138
139 function test_compare_url() {
140 $url1 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2));
141 $url2 = new moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
142
143 $this->assertFalse($url1->compare($url2, URL_MATCH_BASE));
144 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
145 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
146
147 $url2 = new moodle_url('index.php', array('var1' => 1, 'var3' => 3));
148
149 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
150 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
151 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
152
153 $url2 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
154
155 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
156 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
157 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
158
159 $url2 = new moodle_url('index.php', array('var2' => 2, 'var1' => 1));
160
161 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
162 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
163 $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT));
164 }
165
166 function test_out_as_local_url() {
167 $url1 = new moodle_url('/lib/tests/weblib_test.php');
168 $this->assertEquals('/lib/tests/weblib_test.php', $url1->out_as_local_url());
169 }
170
171 /**
172 * @expectedException coding_exception
173 * @return void
174 */
175 function test_out_as_local_url_error() {
4ca04fb5 176 $url2 = new moodle_url('http://www.google.com/lib/tests/weblib_test.php');
a3d5830a
PS
177 $url2->out_as_local_url();
178 }
179
180 public function test_clean_text() {
181 $text = "lala <applet>xx</applet>";
182 $this->assertEquals($text, clean_text($text, FORMAT_PLAIN));
183 $this->assertEquals('lala xx', clean_text($text, FORMAT_MARKDOWN));
184 $this->assertEquals('lala xx', clean_text($text, FORMAT_MOODLE));
185 $this->assertEquals('lala xx', clean_text($text, FORMAT_HTML));
186 }
f0202ae9 187
bd319d12 188 public function test_qualified_me() {
f0202ae9
PS
189 global $PAGE, $FULLME, $CFG;
190 $this->resetAfterTest();
191
192 $PAGE = new moodle_page();
193
194 $FULLME = $CFG->wwwroot.'/course/view.php?id=1&xx=yy';
195 $this->assertEquals($FULLME, qualified_me());
196
197 $PAGE->set_url('/course/view.php', array('id'=>1));
198 $this->assertEquals($CFG->wwwroot.'/course/view.php?id=1', qualified_me());
199 }
a3d5830a 200}