. namespace Tests\Unit; if (!defined('INSTALLDIR')) { define('INSTALLDIR', dirname(dirname(__DIR__))); } if (!defined('PUBLICDIR')) { define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public'); } if (!defined('GNUSOCIAL')) { define('GNUSOCIAL', true); } if (!defined('STATUSNET')) { // Compatibility define('STATUSNET', true); } use PHPUnit\Framework\TestCase; require_once INSTALLDIR . '/lib/common.php'; final class URLDetectionTest extends TestCase { /** * @dataProvider provider * @param $content * @param $expected */ public function testProduction($content, $expected) { $rendered = common_render_text($content); // hack! $rendered = preg_replace('/id="attachment-\d+"/', 'id="attachment-XXX"', $rendered); $this->assertEquals($expected, $rendered); } /** * @dataProvider linkifyProvider * @param $content * @param $expected * @param $config */ public function testLinkifyProduction($content, $expected, $config) { $rendered = common_render_text($content); // hack! $rendered = preg_replace('/id="attachment-\d+"/', 'id="attachment-XXX"', $rendered); if (common_config('linkify', $config)) { $this->assertEquals($expected, $rendered); } else { $content = common_remove_unicode_formatting(nl2br(htmlspecialchars($content))); $this->assertEquals($content, $rendered); } } static public function provider() { return array( array('not a link :: no way', 'not a link :: no way'), array('link http://www.somesite.com/xyz/35637563@N00/52803365/ link', 'link http://www.somesite.com/xyz/35637563@N00/52803365/ link'), array('http://127.0.0.1', 'http://127.0.0.1'), array('http://[::1]:99/test.php', 'http://[::1]:99/test.php'), array('http://::1/test.php', 'http://::1/test.php'), array('http://::1', 'http://::1'), array('http://127.0.0.1', 'http://127.0.0.1'), array('http://example.com', 'http://example.com'), array('http://example.com.', 'http://example.com.'), array('/var/lib/example.so', '/var/lib/example.so'), array('example', 'example'), array('mailto:user@example.com', 'mailto:user@example.com'), array('mailto:user@example.com?subject=test', 'mailto:user@example.com?subject=test'), array('xmpp:user@example.com', 'xmpp:user@example.com'), array('#example', '#'), array('#example.com', '#'), array('#.net', '#'), array('http://example', 'http://example'), array('http://3xampl3', 'http://3xampl3'), array('http://example/', 'http://example/'), array('http://example/path', 'http://example/path'), array('http://example.com', 'http://example.com'), array('https://example.com', 'https://example.com'), array('ftp://example.com', 'ftp://example.com'), array('ftps://example.com', 'ftps://example.com'), array('http://user@example.com', 'http://user@example.com'), array('http://user:pass@example.com', 'http://user:pass@example.com'), array('http://example.com:8080', 'http://example.com:8080'), array('http://example.com:8080/test.php', 'http://example.com:8080/test.php'), array('http://www.example.com', 'http://www.example.com'), array('http://example.com/', 'http://example.com/'), array('http://example.com/path', 'http://example.com/path'), array('http://example.com/path.html', 'http://example.com/path.html'), array('http://example.com/path.html#fragment', 'http://example.com/path.html#fragment'), array('http://example.com/path.php?foo=bar&bar=foo', 'http://example.com/path.php?foo=bar&bar=foo'), array('http://example.com.', 'http://example.com.'), array('http://müllärör.de', 'http://müllärör.de'), array('http://ﺱﺲﺷ.com', 'http://ﺱﺲﺷ.com'), array('http://сделаткартинки.com', 'http://сделаткартинки.com'), array('http://tūdaliņ.lv', 'http://tūdaliņ.lv'), array('http://brændendekærlighed.com', 'http://brændendekærlighed.com'), array('http://あーるいん.com', 'http://あーるいん.com'), array('http://예비교사.com', 'http://예비교사.com'), array('http://example.com.', 'http://example.com.'), array('http://example.com?', 'http://example.com?'), array('http://example.com!', 'http://example.com!'), array('http://example.com,', 'http://example.com,'), array('http://example.com;', 'http://example.com;'), array('http://example.com:', 'http://example.com:'), array('\'http://example.com\'', '\'http://example.com\''), array('"http://example.com"', '"http://example.com"'), array('"http://example.com/"', '"http://example.com/"'), array('http://example.com', 'http://example.com'), array('(http://example.com)', '(http://example.com)'), array('[http://example.com]', '[http://example.com]'), array('', '<http://example.com>'), array('http://example.com/path/(foo)/bar', 'http://example.com/path/(foo)/bar'), array('http://example.com/path/[foo]/bar', 'http://example.com/path/[foo]/bar'), array('http://example.com/path/foo/(bar)', 'http://example.com/path/foo/(bar)'), //Not a valid url - urls cannot contain unencoded square brackets array('http://example.com/path/foo/[bar]', 'http://example.com/path/foo/[bar]'), array('Hey, check out my cool site http://example.com okay?', 'Hey, check out my cool site http://example.com okay?'), array('What about parens (e.g. http://example.com/path/foo/(bar))?', 'What about parens (e.g. http://example.com/path/foo/(bar))?'), array('What about parens (e.g. http://example.com/path/foo/(bar)?', 'What about parens (e.g. http://example.com/path/foo/(bar)?'), array('What about parens (e.g. http://example.com/path/foo/(bar).)?', 'What about parens (e.g. http://example.com/path/foo/(bar).)?'), //Not a valid url - urls cannot contain unencoded commas array('What about parens (e.g. http://example.com/path/(foo,bar)?', 'What about parens (e.g. http://example.com/path/(foo,bar)?'), array('Unbalanced too (e.g. http://example.com/path/((((foo)/bar)?', 'Unbalanced too (e.g. http://example.com/path/((((foo)/bar)?'), array('Unbalanced too (e.g. http://example.com/path/(foo))))/bar)?', 'Unbalanced too (e.g. http://example.com/path/(foo))))/bar)?'), array('Unbalanced too (e.g. http://example.com/path/foo/((((bar)?', 'Unbalanced too (e.g. http://example.com/path/foo/((((bar)?'), array('Unbalanced too (e.g. http://example.com/path/foo/(bar))))?', 'Unbalanced too (e.g. http://example.com/path/foo/(bar))))?'), array('file.ext', 'file.ext'), array('file.html', 'file.html'), array('file.php', 'file.php'), // scheme-less HTTP URLs with @ in the path: http://status.net/open-source/issues/2248 array('http://flickr.com/photos/34807140@N05/3838905434', 'http://flickr.com/photos/34807140@N05/3838905434'), ); } static public function linkifyProvider() { return array( //bare ip addresses are no longer supported array('127.0.0.1', '127.0.0.1', 'bare_ipv4'), array('127.0.0.1:99', '127.0.0.1:99', 'bare_ipv4'), array('127.0.0.1/Name:test.php', '127.0.0.1/Name:test.php', 'bare_ipv4'), array('127.0.0.1/~test', '127.0.0.1/~test', 'bare_ipv4'), array('127.0.0.1/+test', '127.0.0.1/+test', 'bare_ipv4'), array('127.0.0.1/$test', '127.0.0.1/$test', 'bare_ipv4'), array('127.0.0.1/\'test', '127.0.0.1/\'test', 'bare_ipv4'), array('127.0.0.1/"test', '127.0.0.1/"test', 'bare_ipv4'), array('127.0.0.1/test"test', '127.0.0.1/test"test', 'bare_ipv4'), array('127.0.0.1/-test', '127.0.0.1/-test', 'bare_ipv4'), array('127.0.0.1/_test', '127.0.0.1/_test', 'bare_ipv4'), array('127.0.0.1/!test', '127.0.0.1/!test', 'bare_ipv4'), array('127.0.0.1/*test', '127.0.0.1/*test', 'bare_ipv4'), array('127.0.0.1/test%20stuff', '127.0.0.1/test%20stuff', 'bare_ipv4'), array('2001:4978:1b5:0:21d:e0ff:fe66:59ab/test.php', '2001:4978:1b5:0:21d:e0ff:fe66:59ab/test.php', 'bare_ipv6'), array('[2001:4978:1b5:0:21d:e0ff:fe66:59ab]:99/test.php', '[2001:4978:1b5:0:21d:e0ff:fe66:59ab]:99/test.php', 'bare_ipv6'), array('2001:4978:1b5:0:21d:e0ff:fe66:59ab', '2001:4978:1b5:0:21d:e0ff:fe66:59ab', 'bare_ipv6'), array('example.com', 'example.com', 'bare_domains'), array('flickr.com/photos/34807140@N05/3838905434', 'flickr.com/photos/34807140@N05/3838905434', 'bare_domains'), array('What about parens (e.g. example.com/path/foo/(bar))?', 'What about parens (e.g. example.com/path/foo/(bar))?', 'bare_domains'), array('What about parens (e.g. example.com/path/foo/(bar)?', 'What about parens (e.g. example.com/path/foo/(bar)?', 'bare_domains'), array('What about parens (e.g. example.com/path/foo/(bar).)?', 'What about parens (e.g. example.com/path/foo/(bar).?', 'bare_domains'), array('What about parens (e.g. example.com/path/(foo,bar)?', 'What about parens (e.g. example.com/path/(foo,bar)?', 'bare_domains'), array('example.com', 'example.com', 'bare_domains'), array('example.org', 'example.org', 'bare_domains'), array('example.co.uk', 'example.co.uk', 'bare_domains'), array('www.example.co.uk', 'www.example.co.uk', 'bare_domains'), array('farm1.images.example.co.uk', 'farm1.images.example.co.uk', 'bare_domains'), array('example.museum', 'example.museum', 'bare_domains'), array('example.travel', 'example.travel', 'bare_domains'), array('example.com.', 'example.com.', 'bare_domains'), array('example.com?', 'example.com?', 'bare_domains'), array('example.com!', 'example.com!', 'bare_domains'), array('example.com,', 'example.com,', 'bare_domains'), array('example.com;', 'example.com;', 'bare_domains'), array('example.com:', 'example.com:', 'bare_domains'), array('\'example.com\'', '\'example.com\'', 'bare_domains'), array('"example.com"', '"example.com"', 'bare_domains'), array('example.com', 'example.com', 'bare_domains'), array('(example.com)', '(example.com)', 'bare_domains'), array('[example.com]', '[example.com]', 'bare_domains'), array('', '<example.com>', 'bare_domains'), array('Hey, check out my cool site example.com okay?', 'Hey, check out my cool site example.com okay?', 'bare_domains'), array('Hey, check out my cool site example.com.I made it.', 'Hey, check out my cool site example.com.I made it.', 'bare_domains'), array('Hey, check out my cool site example.com.Funny thing...', 'Hey, check out my cool site example.com.Funny thing...', 'bare_domains'), array('Hey, check out my cool site example.com.You will love it.', 'Hey, check out my cool site example.com.You will love it.', 'bare_domains'), array('example.com:8080/test.php', 'example.com:8080/test.php', 'bare_domains'), array('user_name+other@example.com', 'user_name+other@example.com', 'bare_domains'), array('user@example.com', 'user@example.com', 'bare_domains'), ); } }