3 * Unit tests for (some of) ../weblib.php.
5 * @copyright © 2006 The Open University
6 * @author T.J.Hunt@open.ac.uk
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
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');
25 function test_format_string() {
27 $this->assertEqual(format_string("& &&&&& &&"), "& &&&&& &&");
28 $this->assertEqual(format_string("ANother & &&&&& Category"), "ANother & &&&&& Category");
29 $this->assertEqual(format_string("ANother & &&&&& Category", true), "ANother & &&&&& Category");
30 $this->assertEqual(format_string("Nick's Test Site & Other things", true), "Nick's Test Site & Other things");
33 $this->assertEqual(format_string("""), """);
36 $this->assertEqual(format_string("&11234;"), "&11234;");
39 $this->assertEqual(format_string("ᅻ"), "ᅻ");
43 $this->assertEqual(s("This Breaks \" Strict"), "This Breaks " Strict");
44 $this->assertEqual(s("This Breaks <a>\" Strict</a>"), "This Breaks <a>" Strict</a>");
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('& so is this',FORMAT_HTML
));
54 $tl = textlib_get_instance();
55 $this->assertEqual('Two bullets: '.$tl->code2utf8(8226).' *',
56 format_text_email('Two bullets: • •',FORMAT_HTML
));
57 $this->assertEqual($tl->code2utf8(0x7fd2).$tl->code2utf8(0x7fd2),
58 format_text_email('習習',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 "), "This & that ");
75 $this->assertEqual(replace_ampersands_not_followed_by_entity("This   that "), "This &nbsp that ");
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('£ä'), '£ä');
88 $this->assertEqual(fix_non_standard_entities('£ä'), '£ä');
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
));