. /** * Unit test for the filter_urltolink * * @package filter_urltolink * @category phpunit * @copyright 2010 David Mudrak * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/filter/urltolink/filter.php'); // Include the code to test class filter_urltolink_filter_testcase extends basic_testcase { function get_convert_urls_into_links_test_cases() { // Create a 4095 and 4096 long URLs. $superlong4095 = str_pad('http://www.superlong4095.com?this=something', 4095, 'a'); $superlong4096 = str_pad('http://www.superlong4096.com?this=something', 4096, 'a'); $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 with complex fragment. 'Most voted issues: https://tracker.moodle.org/browse/MDL#selectedTab=com.atlassian.jira.plugin.system.project%3Apopularissues-panel' => 'Most voted issues: https://tracker.moodle.org/browse/MDL#selectedTab=com.atlassian.jira.plugin.system.project%3Apopularissues-panel', // Domain with more parts 'URL: www.bbc.co.uk.' => 'URL: www.bbc.co.uk.', // URL in brackets. '(http://moodle.org) - URL' => '(http://moodle.org) - URL', '(www.moodle.org) - URL' => '(www.moodle.org) - URL', // URL in brackets with a path. '(http://example.com/index.html) - URL' => '(http://example.com/index.html) - URL', '(www.example.com/index.html) - URL' => '(www.example.com/index.html) - 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', // URL in square brackets. '[http://moodle.org] - URL' => '[http://moodle.org] - URL', '[www.moodle.org] - URL' => '[www.moodle.org] - URL', // URL in square brackets with a path. '[http://example.com/index.html] - URL' => '[http://example.com/index.html] - URL', '[www.example.com/index.html] - URL' => '[www.example.com/index.html] - URL', // URL in square 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', // URL legitimately ending in a bracket. Commented out as part of MDL-22390. See next tests for work-arounds. // '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&', //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', 'image' => 'image', //and another url within one tag ' ' => ' ', ' ' => ' ', '

'=>'', '' => '', ' ' => ' ', // CSS URLs. '' => '
', '
' => '
', '
' => '
', '
' => '
', //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', // Test URL less than 4096 characters in size is converted to link. 'URL: ' . $superlong4095 => 'URL: ' . $superlong4095 . '', // Test URL equal to or greater than 4096 characters in size is not converted to link. 'URL: ' . $superlong4096 => 'URL: ' . $superlong4096, // Testing URL within a span tag. 'URL: my link to http://google.com ' => 'URL: my link to http://google.com ', // Nested tags test. 'www.google.com' => 'www.google.com', '' => '', // Test realistic content. '

Lorem ipsum amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut http://google.com aliquip ex ea commodo consequat. Duis aute irure in reprehenderit in excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia https://docs.google.com/document/d/BrokenLinkPleaseAyacDHc_Ov8aoskoSVQsfmLHP_jYAkRMk/edit?usp=sharing https://docs.google.com/document/d/BrokenLinkPleaseAyacDHc_Ov8aoskoSVQsfmLHP_jYAkRMk/edit?usp=sharing mollit anim id est laborum.

' => '

Lorem ipsum amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut http://google.com aliquip ex ea commodo consequat. Duis aute irure in reprehenderit in excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia https://docs.google.com/document/d/BrokenLinkPleaseAyacDHc_Ov8aoskoSVQsfmLHP_jYAkRMk/edit?usp=sharing https://docs.google.com/document/d/BrokenLinkPleaseAyacDHc_Ov8aoskoSVQsfmLHP_jYAkRMk/edit?usp=sharing mollit anim id est laborum.

', // Test some broken html. '5 < 10 www.google.com im a link' => '5 < 10 www.google.comim a link', 'h3 (www.styles.com/h3) < h1 (www.styles.com/h1)' => 'h3 (www.styles.com/h3) < h1 (www.styles.com/h1)', '

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

text www.moodle.org</p> text', // Some more urls. '' => '', 'www.google.com' => 'www.google.com', 'http://nolandforzombies.com Zombies FTW http://aliens.org' => 'http://nolandforzombies.com Zombies FTW http://aliens.org', //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? //''=>'' ); $data = array(); foreach ($texts as $text => $correctresult) { $data[] = array($text, $correctresult); } return $data; } /** * @dataProvider get_convert_urls_into_links_test_cases */ function test_convert_urls_into_links($text, $correctresult) { $testablefilter = new testable_filter_urltolink(); $testablefilter->convert_urls_into_links($text); $this->assertEquals($correctresult, $text); } } /** * Test subclass that makes all the protected methods we want to test public. */ class testable_filter_urltolink extends filter_urltolink { public function __construct() { } public function convert_urls_into_links(&$text) { parent::convert_urls_into_links($text); } }