|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00011 if (!defined('MOODLE_INTERNAL')) { 00012 die('Direct access to this script is forbidden.'); 00013 } 00014 00015 class web_test extends UnitTestCase { 00016 00017 public static $includecoverage = array('lib/weblib.php'); 00018 00019 function setUp() { 00020 } 00021 00022 function tearDown() { 00023 } 00024 00025 function test_format_string() { 00026 global $CFG; 00027 00028 // Ampersands 00029 $this->assertEqual(format_string("& &&&&& &&"), "& &&&&& &&"); 00030 $this->assertEqual(format_string("ANother & &&&&& Category"), "ANother & &&&&& Category"); 00031 $this->assertEqual(format_string("ANother & &&&&& Category", true), "ANother & &&&&& Category"); 00032 $this->assertEqual(format_string("Nick's Test Site & Other things", true), "Nick's Test Site & Other things"); 00033 00034 // String entities 00035 $this->assertEqual(format_string("""), """); 00036 00037 // Digital entities 00038 $this->assertEqual(format_string("&11234;"), "&11234;"); 00039 00040 // Unicode entities 00041 $this->assertEqual(format_string("ᅻ"), "ᅻ"); 00042 00043 // < and > signs 00044 $originalformatstringstriptags = $CFG->formatstringstriptags; 00045 00046 $CFG->formatstringstriptags = false; 00047 $this->assertEqual(format_string('x < 1'), 'x < 1'); 00048 $this->assertEqual(format_string('x > 1'), 'x > 1'); 00049 $this->assertEqual(format_string('x < 1 and x > 0'), 'x < 1 and x > 0'); 00050 00051 $CFG->formatstringstriptags = true; 00052 $this->assertEqual(format_string('x < 1'), 'x < 1'); 00053 $this->assertEqual(format_string('x > 1'), 'x > 1'); 00054 $this->assertEqual(format_string('x < 1 and x > 0'), 'x < 1 and x > 0'); 00055 00056 $CFG->formatstringstriptags = $originalformatstringstriptags; 00057 } 00058 00059 function test_s() { 00060 $this->assertEqual(s("This Breaks \" Strict"), "This Breaks " Strict"); 00061 $this->assertEqual(s("This Breaks <a>\" Strict</a>"), "This Breaks <a>" Strict</a>"); 00062 } 00063 00064 function test_format_text_email() { 00065 $this->assertEqual("\n\nThis is a TEST", 00066 format_text_email('<p>This is a <strong>test</strong></p>',FORMAT_HTML)); 00067 $this->assertEqual("\n\nThis is a TEST", 00068 format_text_email('<p class="frogs">This is a <strong class=\'fishes\'>test</strong></p>',FORMAT_HTML)); 00069 $this->assertEqual('& so is this', 00070 format_text_email('& so is this',FORMAT_HTML)); 00071 $tl = textlib_get_instance(); 00072 $this->assertEqual('Two bullets: '.$tl->code2utf8(8226).' *', 00073 format_text_email('Two bullets: • •',FORMAT_HTML)); 00074 $this->assertEqual($tl->code2utf8(0x7fd2).$tl->code2utf8(0x7fd2), 00075 format_text_email('習習',FORMAT_HTML)); 00076 } 00077 00078 function test_highlight() { 00079 $this->assertEqual(highlight('good', 'This is good'), 'This is <span class="highlight">good</span>'); 00080 $this->assertEqual(highlight('SpaN', 'span'), '<span class="highlight">span</span>'); 00081 $this->assertEqual(highlight('span', 'SpaN'), '<span class="highlight">SpaN</span>'); 00082 $this->assertEqual(highlight('span', '<span>span</span>'), '<span><span class="highlight">span</span></span>'); 00083 $this->assertEqual(highlight('good is', 'He is good'), 'He <span class="highlight">is</span> <span class="highlight">good</span>'); 00084 $this->assertEqual(highlight('+good', 'This is good'), 'This is <span class="highlight">good</span>'); 00085 $this->assertEqual(highlight('-good', 'This is good'), 'This is good'); 00086 $this->assertEqual(highlight('+good', 'This is goodness'), 'This is goodness'); 00087 $this->assertEqual(highlight('good', 'This is goodness'), 'This is <span class="highlight">good</span>ness'); 00088 } 00089 00090 function test_replace_ampersands() { 00091 $this->assertEqual(replace_ampersands_not_followed_by_entity("This & that "), "This & that "); 00092 $this->assertEqual(replace_ampersands_not_followed_by_entity("This   that "), "This &nbsp that "); 00093 } 00094 00095 function test_strip_links() { 00096 $this->assertEqual(strip_links('this is a <a href="http://someaddress.com/query">link</a>'), 'this is a link'); 00097 } 00098 00099 function test_wikify_links() { 00100 $this->assertEqual(wikify_links('this is a <a href="http://someaddress.com/query">link</a>'), 'this is a link [ http://someaddress.com/query ]'); 00101 } 00102 00103 function test_compare_url() { 00104 $url1 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2)); 00105 $url2 = new moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3)); 00106 00107 $this->assertFalse($url1->compare($url2, URL_MATCH_BASE)); 00108 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS)); 00109 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT)); 00110 00111 $url2 = new moodle_url('index.php', array('var1' => 1, 'var3' => 3)); 00112 00113 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); 00114 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS)); 00115 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT)); 00116 00117 $url2 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3)); 00118 00119 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); 00120 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS)); 00121 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT)); 00122 00123 $url2 = new moodle_url('index.php', array('var2' => 2, 'var1' => 1)); 00124 00125 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); 00126 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS)); 00127 $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT)); 00128 } 00129 00130 public function test_html_to_text_simple() { 00131 $this->assertEqual("\n\n_Hello_ WORLD!", html_to_text('<p><i>Hello</i> <b>world</b>!</p>')); 00132 } 00133 00134 public function test_html_to_text_image() { 00135 $this->assertEqual('[edit]', html_to_text('<img src="edit.png" alt="edit" />')); 00136 } 00137 00138 public function test_html_to_text_image_with_backslash() { 00139 $this->assertEqual('[\edit]', html_to_text('<img src="edit.png" alt="\edit" />')); 00140 } 00141 00142 public function test_html_to_text_nowrap() { 00143 $long = "Here is a long string, more than 75 characters long, since by default html_to_text wraps text at 75 chars."; 00144 $this->assertEqual($long, html_to_text($long, 0)); 00145 } 00146 00147 public function test_html_to_text_dont_screw_up_utf8() { 00148 $this->assertEqual("\n\nAll the WORLD’S a stage.", html_to_text('<p>All the <strong>world’s</strong> a stage.</p>')); 00149 } 00150 00151 public function test_html_to_text_trailing_whitespace() { 00152 $this->assertEqual('With trailing whitespace and some more text', html_to_text("With trailing whitespace \nand some more text", 0)); 00153 } 00154 00155 public function test_html_to_text_0() { 00156 $this->assertIdentical('0', html_to_text('0')); 00157 } 00158 00159 public function test_html_to_text_pre_parsing_problem() { 00160 $strorig = 'Consider the following function:<br /><pre><span style="color: rgb(153, 51, 102);">void FillMeUp(char* in_string) {'. 00161 '<br /> int i = 0;<br /> while (in_string[i] != \'\0\') {<br /> in_string[i] = \'X\';<br /> i++;<br /> }<br />'. 00162 '}</span></pre>What would happen if a non-terminated string were input to this function?<br /><br />'; 00163 00164 $strconv = 'Consider the following function: 00165 00166 void FillMeUp(char* in_string) { 00167 int i = 0; 00168 while (in_string[i] != \'\0\') { 00169 in_string[i] = \'X\'; 00170 i++; 00171 } 00172 } 00173 What would happen if a non-terminated string were input to this function? 00174 00175 '; 00176 00177 $this->assertIdentical($strconv, html_to_text($strorig)); 00178 } 00179 00180 public function test_clean_text() { 00181 $text = "lala <applet>xx</applet>"; 00182 $this->assertEqual($text, clean_text($text, FORMAT_PLAIN)); 00183 $this->assertEqual('lala xx', clean_text($text, FORMAT_MARKDOWN)); 00184 $this->assertEqual('lala xx', clean_text($text, FORMAT_MOODLE)); 00185 $this->assertEqual('lala xx', clean_text($text, FORMAT_HTML)); 00186 } 00187 }