assertEqual(format_string("& &&&&& &&"), "& &&&&& &&"); $this->assertEqual(format_string("ANother & &&&&& Category"), "ANother & &&&&& Category"); $this->assertEqual(format_string("ANother & &&&&& Category", true), "ANother & &&&&& Category"); $this->assertEqual(format_string("Nick's Test Site & Other things", true), "Nick's Test Site & Other things"); // String entities $this->assertEqual(format_string("""), """); // Digital entities $this->assertEqual(format_string("&11234;"), "&11234;"); // Unicode entities $this->assertEqual(format_string("ᅻ"), "ᅻ"); } function test_s() { $this->assertEqual(s("This Breaks \" Strict"), "This Breaks " Strict"); } function test_format_text_email() { $this->assertEqual("\n\nThis is a TEST", format_text_email('

This is a test

',FORMAT_HTML)); $this->assertEqual("\n\nThis is a TEST", format_text_email('

This is a test

',FORMAT_HTML)); $this->assertEqual("& so is this", format_text_email('& so is this',FORMAT_HTML)); $tl = textlib_get_instance(); $this->assertEqual('Two bullets: '.$tl->code2utf8(8226).' *', format_text_email('Two bullets: • •',FORMAT_HTML)); $this->assertEqual($tl->code2utf8(0x7fd2).$tl->code2utf8(0x7fd2), format_text_email('習習',FORMAT_HTML)); } function test_highlight() { $this->assertEqual(highlight('good', 'This is good'), 'This is good'); $this->assertEqual(highlight('SpaN', 'span'), 'span'); $this->assertEqual(highlight('span', 'SpaN'), 'SpaN'); $this->assertEqual(highlight('span', 'span'), 'span'); $this->assertEqual(highlight('good is', 'He is good'), 'He is good'); $this->assertEqual(highlight('+good', 'This is good'), 'This is good'); $this->assertEqual(highlight('-good', 'This is good'), 'This is good'); $this->assertEqual(highlight('+good', 'This is goodness'), 'This is goodness'); $this->assertEqual(highlight('good', 'This is goodness'), 'This is goodness'); } function old_convert_urls_into_links(&$text) { /// Make lone URLs into links. eg http://moodle.com/ $text = eregi_replace("([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", "\\1\\2://\\3\\4", $text); /// eg www.moodle.com $text = eregi_replace("([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])", "\\1www.\\2\\3", $text); } function get_test_text(){ return <<dummy It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. Where can I get some? There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc. Wikipedia http://www.lorem-ipsum.info/ END; } function test_convert_urls_into_links() { $texts = array ( //just a url 'http://moodle.org - URL' => 'http://moodle.org - URL', 'www.moodle.org - URL' => 'www.moodle.org - URL', //url with params 'URL: http://moodle.org/s/i=1&j=2' => 'URL: http://moodle.org/s/i=1&j=2', //url with escaped params 'URL: www.moodle.org/s/i=1&j=2' => 'URL: www.moodle.org/s/i=1&j=2', //https url with params 'URL: https://moodle.org/s/i=1&j=2' => 'URL: https://moodle.org/s/i=1&j=2', //url with port and params 'URL: http://moodle.org:8080/s/i=1' => 'URL: http://moodle.org:8080/s/i=1', //url in brackets '(http://moodle.org) - URL' => '(http://moodle.org) - URL', '(www.moodle.org) - URL' => '(www.moodle.org) - URL', //url in square brackets '[http://moodle.org] - URL' => '[http://moodle.org] - URL', '[www.moodle.org] - URL' => '[www.moodle.org] - URL', //url in brackets with anchor '[http://moodle.org/main#anchor] - URL' => '[http://moodle.org/main#anchor] - URL', '[www.moodle.org/main#anchor] - URL' => '[www.moodle.org/main#anchor] - URL', //brackets within the url 'URL: http://cc.org/url_(withpar)_go/?i=2' => 'URL: http://cc.org/url_(withpar)_go/?i=2', 'URL: www.cc.org/url_(withpar)_go/?i=2' => 'URL: www.cc.org/url_(withpar)_go/?i=2', 'URL: http://cc.org/url_(with)_(par)_go/?i=2' => 'URL: http://cc.org/url_(with)_(par)_go/?i=2', 'URL: www.cc.org/url_(with)_(par)_go/?i=2' => 'URL: www.cc.org/url_(with)_(par)_go/?i=2', 'http://en.wikipedia.org/wiki/Slash_(punctuation)'=>'http://en.wikipedia.org/wiki/Slash_(punctuation)', 'http://en.wikipedia.org/wiki/%28#Parentheses_.28_.29 - URL' => 'http://en.wikipedia.org/wiki/%28#Parentheses_.28_.29 - URL', 'http://en.wikipedia.org/wiki/(#Parentheses_.28_.29 - URL' => 'http://en.wikipedia.org/wiki/(#Parentheses_.28_.29 - URL', //escaped brackets in url 'http://en.wikipedia.org/wiki/Slash_%28punctuation%29'=>'http://en.wikipedia.org/wiki/Slash_%28punctuation%29', //anchor tag 'URL: http://moodle.org' => 'URL: http://moodle.org', 'URL: www.moodle.org' => 'URL: www.moodle.org', 'URL: http://moodle.org' => 'URL: http://moodle.org', 'URL: www.moodle.org' => 'URL: www.moodle.org', //escaped anchor tag. Commented out as part of MDL-21183 //htmlspecialchars('escaped anchor tag www.moodle.org') => 'escaped anchor tag <a href="http://moodle.org"> www.moodle.org</a>', //trailing fullstop 'URL: http://moodle.org/s/i=1&j=2.' => 'URL: http://moodle.org/s/i=1&j=2.', 'URL: www.moodle.org/s/i=1&j=2.' => 'URL: www.moodle.org/s/i=1&j=2.', //trailing unmatched bracket 'URL: http://moodle.org)
' => 'URL: http://moodle.org)
', //partially escaped html 'URL:

text www.moodle.org</p> text' => 'URL:

text www.moodle.org</p> text', //decimal url parameter 'URL: www.moodle.org?u=1.23' => 'URL: www.moodle.org?u=1.23', //escaped space in url 'URL: www.moodle.org?u=test+param&' => 'URL: www.moodle.org?u=test+param&', //odd characters in url param 'URL: www.moodle.org?param=:)' => 'URL: www.moodle.org?param=:)', //multiple urls 'URL: http://moodle.org www.moodle.org' => 'URL: http://moodle.org www.moodle.org', //containing anchor tags including a class parameter and a url to convert 'URL: http://moodle.org www.moodle.org http://moodle.org' => 'URL: http://moodle.org www.moodle.org http://moodle.org', //subdomain 'http://subdomain.moodle.org - URL' => 'http://subdomain.moodle.org - URL', //multiple subdomains 'http://subdomain.subdomain.moodle.org - URL' => 'http://subdomain.subdomain.moodle.org - URL', //looks almost like a link but isnt 'This contains http, http:// and www but no actual links.'=>'This contains http, http:// and www but no actual links.', //no link at all 'This is a story about moodle.coming to a cinema near you.'=>'This is a story about moodle.coming to a cinema near you.', //URLs containing utf 8 characters 'http://Iñtërnâtiônàlizætiøn.com?ô=nëø'=>'http://Iñtërnâtiônàlizætiøn.com?ô=nëø', 'www.Iñtërnâtiônàlizætiøn.com?ô=nëø'=>'www.Iñtërnâtiônàlizætiøn.com?ô=nëø', //text containing utf 8 characters outside of a url 'Iñtërnâtiônàlizætiøn is important to http://moodle.org'=>'Iñtërnâtiônàlizætiøn is important to http://moodle.org', //too hard to identify without additional regexs 'moodle.org' => 'moodle.org', //some text with no link between related html tags 'no link here' => 'no link here', //some text with a link between related html tags 'a link here www.moodle.org' => 'a link here www.moodle.org', //some text containing a link within unrelated tags '
This is some text. www.moodle.com then some more text
' => '
This is some text. www.moodle.com then some more text
', //check we aren't modifying img tags 'image' => 'image', 'image' => 'image', //and another url within one tag ' ' => ' ', ' ' => ' ', '

'=>'', //partially escaped img tag 'partially escaped img tag <img src="http://moodle.org/logo/logo-240x60.gif" />' => 'partially escaped img tag <img src="http://moodle.org/logo/logo-240x60.gif" />', //fully escaped img tag. Commented out as part of MDL-21183 //htmlspecialchars('fully escaped img tag ') => 'fully escaped img tag <img src="http://moodle.org/logo/logo-240x60.gif" />', //Double http with www 'One more link like http://www.moodle.org to test' => 'One more link like http://www.moodle.org to test', //Encoded URLs in the path 'URL: http://127.0.0.1/one%28parenthesis%29/path?param=value' => 'URL: http://127.0.0.1/one%28parenthesis%29/path?param=value', 'URL: www.localhost.com/one%28parenthesis%29/path?param=value' => 'URL: www.localhost.com/one%28parenthesis%29/path?param=value', //Encoded URLs in the query 'URL: http://127.0.0.1/path/to?param=value_with%28parenthesis%29¶m2=1' => 'URL: http://127.0.0.1/path/to?param=value_with%28parenthesis%29¶m2=1', 'URL: www.localhost.com/path/to?param=value_with%28parenthesis%29¶m2=1' => 'URL: www.localhost.com/path/to?param=value_with%28parenthesis%29¶m2=1', //URLs in Javascript. Commented out as part of MDL-21183 //'var url="http://moodle.org";'=>'var url="http://moodle.org";', //'var url = "http://moodle.org";'=>'var url = "http://moodle.org";', //'var url="www.moodle.org";'=>'var url="www.moodle.org";', //'var url = "www.moodle.org";'=>'var url = "www.moodle.org";', //doctype. do we care about this failing? //''=>'' ); foreach ($texts as $text => $correctresult) { $msg = "Testing text: ". str_replace('%', '%%', $text) . ": %s"; // Escape original '%' so sprintf() wont get confused convert_urls_into_links($text); $this->assertEqual($text, $correctresult, $msg); } //performance testing $reps = 1000; $time_start = microtime(true); for($i=0;$i<$reps;$i++) { $text = $this->get_test_text(); convert_urls_into_links($text); } $time_end = microtime(true); $new_time = $time_end - $time_start; $time_start = microtime(true); for($i=0;$i<$reps;$i++) { $text = $this->get_test_text(); $this->old_convert_urls_into_links($text); } $time_end = microtime(true); $old_time = $time_end - $time_start; $fast_enough = false; if( $new_time < $old_time ) { $fast_enough = true; } $this->assertEqual($fast_enough, true, 'Timing test: ' . $new_time . 'secs (new) < ' . $old_time . 'secs (old)'); } } ?>