Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

243 lines (215 sloc) 20.54 kB
* Unit tests for (some of) ../weblib.php.
* @copyright &copy; 2006 The Open University
* @author
* @license GNU Public License
* @package moodlecore
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
class web_test extends UnitTestCase {
function setUp() {
function tearDown() {
function test_format_string() {
// Ampersands
$this->assertEqual(format_string("& &&&&& &&"), "&amp; &amp;&amp;&amp;&amp;&amp; &amp;&amp;");
$this->assertEqual(format_string("ANother & &&&&& Category"), "ANother &amp; &amp;&amp;&amp;&amp;&amp; Category");
$this->assertEqual(format_string("ANother & &&&&& Category", true), "ANother &amp; &amp;&amp;&amp;&amp;&amp; Category");
$this->assertEqual(format_string("Nick's Test Site & Other things", true), "Nick's Test Site &amp; Other things");
// String entities
$this->assertEqual(format_string("&quot;"), "&quot;");
// Digital entities
$this->assertEqual(format_string("&11234;"), "&11234;");
// Unicode entities
$this->assertEqual(format_string("&#4475;"), "&#4475;");
function test_s() {
$this->assertEqual(s("This Breaks \" Strict"), "This Breaks &quot; Strict");
function test_format_text_email() {
$this->assertEqual("\n\nThis is a TEST",
format_text_email('<p>This is a <strong>test</strong></p>',FORMAT_HTML));
$this->assertEqual("\n\nThis is a TEST",
format_text_email('<p class="frogs">This is a <strong class=\'fishes\'>test</strong></p>',FORMAT_HTML));
$this->assertEqual("& so is this",
format_text_email('&amp; so is this',FORMAT_HTML));
$tl = textlib_get_instance();
$this->assertEqual('Two bullets: '.$tl->code2utf8(8226).' *',
format_text_email('Two bullets: &#x2022; &#8226;',FORMAT_HTML));
function test_highlight() {
$this->assertEqual(highlight('good', 'This is good'), 'This is <span class="highlight">good</span>');
$this->assertEqual(highlight('SpaN', 'span'), '<span class="highlight">span</span>');
$this->assertEqual(highlight('span', 'SpaN'), '<span class="highlight">SpaN</span>');
$this->assertEqual(highlight('span', '<span>span</span>'), '<span><span class="highlight">span</span></span>');
$this->assertEqual(highlight('good is', 'He is good'), 'He <span class="highlight">is</span> <span class="highlight">good</span>');
$this->assertEqual(highlight('+good', 'This is good'), 'This is <span class="highlight">good</span>');
$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 <span class="highlight">good</span>ness');
function old_convert_urls_into_links(&$text) {
/// Make lone URLs into links. eg
$text = eregi_replace("([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
"\\1<a href=\"\\2://\\3\\4\" target=\"_blank\">\\2://\\3\\4</a>", $text);
/// eg
$text = eregi_replace("([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])",
"\\1<a href=\"http://www.\\2\\3\" target=\"_blank\">www.\\2\\3</a>", $text);
function get_test_text(){
return <<<END
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Why do we use it?<a href="dummylink.htm">dummy</a>
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.
<a href="">Wikipedia</a>
function test_convert_urls_into_links() {
$texts = array (
//just a url
' - URL' => '<a href="" target="_blank"></a> - URL',
' - URL' => '<a href="" target="_blank"></a> - URL',
//url with params
'URL:' => 'URL: <a href="" target="_blank"></a>',
//url with escaped params
'URL:;j=2' => 'URL: <a href=";j=2" target="_blank">;j=2</a>',
//https url with params
'URL:' => 'URL: <a href="" target="_blank"></a>',
//url with port and params
'URL:' => 'URL: <a href="" target="_blank"></a>',
//url in brackets
'( - URL' => '(<a href="" target="_blank"></a>) - URL',
'( - URL' => '(<a href="" target="_blank"></a>) - URL',
//url in square brackets
'[] - URL' => '[<a href="" target="_blank"></a>] - URL',
'[] - URL' => '[<a href="" target="_blank"></a>] - URL',
//url in brackets with anchor
'[] - URL' => '[<a href="" target="_blank"></a>] - URL',
'[] - URL' => '[<a href="" target="_blank"></a>] - URL',
//brackets within the url
'URL:' => 'URL: <a href="" target="_blank"></a>',
'URL:' => 'URL: <a href="" target="_blank"></a>',
'URL:' => 'URL: <a href="" target="_blank"></a>',
'URL:' => 'URL: <a href="" target="_blank"></a>',
''=>'<a href="" target="_blank"></a>',
' - URL' => '<a href="" target="_blank"></a> - URL',
' - URL' => '<a href="" target="_blank"></a> - URL',
//escaped brackets in url
''=>'<a href="" target="_blank"></a>',
//anchor tag
'URL: <a href=""></a>' => 'URL: <a href=""></a>',
'URL: <a href=""></a>' => 'URL: <a href=""></a>',
'URL: <a href=""></a>' => 'URL: <a href=""></a>',
'URL: <a href=""></a>' => 'URL: <a href=""></a>',
//escaped anchor tag. todo decide whether we want to make this work. MDL-21183
htmlspecialchars('escaped anchor tag <a href=""></a>') => 'escaped anchor tag &lt;a href=""&gt;;/a&gt;',
//trailing fullstop
'URL:' => 'URL: <a href="" target="_blank"></a>.',
'URL:;j=2.' => 'URL: <a href=";j=2" target="_blank">;j=2</a>.',
//trailing unmatched bracket
'URL:<br />' => 'URL: <a href="" target="_blank"></a>)<br />',
//partially escaped html
'URL: <p>text;/p> text' => 'URL: <p>text <a href="" target="_blank"></a>&lt;/p> text',
//decimal url parameter
'URL:' => 'URL: <a href="" target="_blank"></a>',
//escaped space in url
'URL:' => 'URL: <a href="" target="_blank"></a>',
//odd characters in url param
'URL:' => 'URL: <a href="" target="_blank"></a>',
//multiple urls
=> 'URL: <a href="" target="_blank"></a> <a href="" target="_blank"></a>',
//containing anchor tags including a class parameter and a url to convert
'URL: <a href=""></a> <a class="customclass" href=""></a>'
=> 'URL: <a href=""></a> <a href="" target="_blank"></a> <a class="customclass" href=""></a>',
' - URL' => '<a href="" target="_blank"></a> - URL',
//multiple subdomains
' - URL' => '<a href="" target="_blank"></a> - 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ëø'=>'<a href="http://Iñtërnâtiônàlizætiøô=nëø" target="_blank">http://Iñtërnâtiônàlizætiøô=nëø</a>',
'www.Iñtërnâtiônàlizætiøô=nëø'=>'<a href="http://www.Iñtërnâtiônàlizætiøô=nëø" target="_blank">www.Iñtërnâtiônàlizætiøô=nëø</a>',
//text containing utf 8 characters outside of a url
'Iñtërnâtiônàlizætiøn is important to'=>'Iñtërnâtiônàlizætiøn is important to <a href="" target="_blank"></a>',
//too hard to identify without additional regexs
'' => '',
//some text with no link between related html tags
'<b>no link here</b>' => '<b>no link here</b>',
//some text with a link between related html tags
'<b>a link here</b>' => '<b>a link here <a href="" target="_blank"></a></b>',
//some text containing a link within unrelated tags
'<br />This is some text. then some more text<br />' => '<br />This is some text. <a href="" target="_blank"></a> then some more text<br />',
//check we aren't modifying img tags
'image<img src="" />' => 'image<img src="" />',
'image<img src="" />' => 'image<img src="" />',
//and another url within one tag
'<td background="">&nbsp;</td>' => '<td background="">&nbsp;</td>',
'<td background="">&nbsp;</td>' => '<td background="">&nbsp;</td>',
'<form name="input" action="" method="get">'=>'<form name="input" action="" method="get">',
//partially escaped img tag
'partially escaped img tag &lt;img src="" />' => 'partially escaped img tag &lt;img src="" />',
//fully escaped img tag. Do we want this to work on escaped text? MDL-21183
htmlspecialchars('fully escaped img tag <img src="" />') => 'fully escaped img tag &lt;img src="" /&gt;',
//Double http with www
'One more link like to test' => 'One more link like <a href="" target="_blank"></a> to test',
//Encoded URLs in the path
'URL:' => 'URL: <a href="" target="_blank"></a>',
'URL:' => 'URL: <a href="" target="_blank"></a>',
//Encoded URLs in the query
'URL:' => 'URL: <a href="" target="_blank"></a>',
'URL:' => 'URL: <a href="" target="_blank"></a>',
//URLs in Javascript
'var url="";'=>'var url="";',
'var url = "";'=>'var url = "";',
'var url="";'=>'var url="";',
'var url = "";'=>'var url = "";',
//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
$this->assertEqual($text, $correctresult, $msg);
//performance testing
$reps = 1000;
$time_start = microtime(true);
for($i=0;$i<$reps;$i++) {
$text = $this->get_test_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();
$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:');
Jump to Line
Something went wrong with that request. Please try again.