Merge branch 'MDL-29276' of git://github.com/mouneyrac/moodle
[moodle.git] / lib / simpletest / testweblib.php
blob20f6ebf73e12006bd55b0dd7d6abfd634805ebec
1 <?php
2 /**
3 * Unit tests for (some of) ../weblib.php.
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 */
11 if (!defined('MOODLE_INTERNAL')) {
12 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
15 class web_test extends UnitTestCase {
17 public static $includecoverage = array('lib/weblib.php');
19 function setUp() {
22 function tearDown() {
25 function test_format_string() {
26 // Ampersands
27 $this->assertEqual(format_string("& &&&&& &&"), "&amp; &amp;&amp;&amp;&amp;&amp; &amp;&amp;");
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");
32 // String entities
33 $this->assertEqual(format_string("&quot;"), "&quot;");
35 // Digital entities
36 $this->assertEqual(format_string("&11234;"), "&11234;");
38 // Unicode entities
39 $this->assertEqual(format_string("&#4475;"), "&#4475;");
42 function test_s() {
43 $this->assertEqual(s("This Breaks \" Strict"), "This Breaks &quot; Strict");
44 $this->assertEqual(s("This Breaks <a>\" Strict</a>"), "This Breaks &lt;a&gt;&quot; Strict&lt;/a&gt;");
47 function test_format_text_email() {
48 $this->assertEqual("\n\nThis is a TEST",
49 format_text_email('<p>This is a <strong>test</strong></p>',FORMAT_HTML));
50 $this->assertEqual("\n\nThis is a TEST",
51 format_text_email('<p class="frogs">This is a <strong class=\'fishes\'>test</strong></p>',FORMAT_HTML));
52 $this->assertEqual('& so is this',
53 format_text_email('&amp; so is this',FORMAT_HTML));
54 $tl = textlib_get_instance();
55 $this->assertEqual('Two bullets: '.$tl->code2utf8(8226).' *',
56 format_text_email('Two bullets: &#x2022; &#8226;',FORMAT_HTML));
57 $this->assertEqual($tl->code2utf8(0x7fd2).$tl->code2utf8(0x7fd2),
58 format_text_email('&#x7fd2;&#x7FD2;',FORMAT_HTML));
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');
73 function test_replace_ampersands() {
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;");
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');
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 ]');
86 function test_fix_non_standard_entities() {
87 $this->assertEqual(fix_non_standard_entities('&#x00A3&#0228'), '&#xA3;&#228;');
88 $this->assertEqual(fix_non_standard_entities('&#x00A3;&#0228;'), '&#xA3;&#228;');
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));
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));
99 $url2 = new moodle_url('index.php', array('var1' => 1, 'var3' => 3));
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));
105 $url2 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
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));
111 $url2 = new moodle_url('index.php', array('var2' => 2, 'var1' => 1));
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));
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>'));
122 public function test_html_to_text_image() {
123 $this->assertEqual('[edit]', html_to_text('<img src="edit.png" alt="edit" />'));
126 public function test_html_to_text_image_with_backslash() {
127 $this->assertEqual('[\edit]', html_to_text('<img src="edit.png" alt="\edit" />'));
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));
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>'));
139 public function test_html_to_text_trailing_whitespace() {
140 $this->assertEqual('With trailing whitespace and some more text', html_to_text("With trailing whitespace \nand some more text", 0));
143 public function test_clean_text() {
144 $text = "lala <applet>xx</applet>";
145 $this->assertEqual($text, clean_text($text, FORMAT_PLAIN));
146 $this->assertEqual('lala xx', clean_text($text, FORMAT_MARKDOWN));
147 $this->assertEqual('lala xx', clean_text($text, FORMAT_MOODLE));
148 $this->assertEqual('lala xx', clean_text($text, FORMAT_HTML));