. 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/util/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); static::assertSame($expected, $rendered); } /** * @dataProvider linkifyProvider * * @param $content * @param $expected * @param $config * @throws \ServerException */ 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)) { static::assertSame($expected, $rendered); } else { $content = common_remove_unicode_formatting(nl2br(htmlspecialchars($content))); static::assertSame($content, $rendered); } } public static function provider() { return [ ['not a link :: no way', 'not a link :: no way',], ['link http://www.somesite.com/xyz/35637563@N00/52803365/ link', 'link http://www.somesite.com/xyz/35637563@N00/52803365/ link',], ['http://127.0.0.1', 'http://127.0.0.1',], ['http://[::1]:99/test.php', 'http://[::1]:99/test.php',], ['http://::1/test.php', 'http://::1/test.php',], ['http://::1', 'http://::1',], ['http://127.0.0.1', 'http://127.0.0.1',], ['http://example.com', 'http://example.com',], ['http://example.com.', 'http://example.com.',], ['/var/lib/example.so', '/var/lib/example.so',], ['example', 'example',], ['mailto:user@example.com', 'mailto:user@example.com',], ['mailto:user@example.com?subject=test', 'mailto:user@example.com?subject=test',], ['xmpp:user@example.com', 'xmpp:user@example.com',], ['#example', '#',], ['#example.com', '#',], ['#.net', '#',], ['http://example', 'http://example',], ['http://3xampl3', 'http://3xampl3',], ['http://example/', 'http://example/',], ['http://example/path', 'http://example/path',], ['http://example.com', 'http://example.com',], ['https://example.com', 'https://example.com',], ['ftp://example.com', 'ftp://example.com',], ['ftps://example.com', 'ftps://example.com',], ['http://user@example.com', 'http://user@example.com',], ['http://user:pass@example.com', 'http://user:pass@example.com',], ['http://example.com:8080', 'http://example.com:8080',], ['http://example.com:8080/test.php', 'http://example.com:8080/test.php',], ['http://www.example.com', 'http://www.example.com',], ['http://example.com/', 'http://example.com/',], ['http://example.com/path', 'http://example.com/path',], ['http://example.com/path.html', 'http://example.com/path.html',], ['http://example.com/path.html#fragment', 'http://example.com/path.html#fragment',], ['http://example.com/path.php?foo=bar&bar=foo', 'http://example.com/path.php?foo=bar&bar=foo',], ['http://example.com.', 'http://example.com.',], ['http://müllärör.de', 'http://müllärör.de',], ['http://ﺱﺲﺷ.com', 'http://ﺱﺲﺷ.com',], ['http://сделаткартинки.com', 'http://сделаткартинки.com',], ['http://tūdaliņ.lv', 'http://tūdaliņ.lv',], ['http://brændendekærlighed.com', 'http://brændendekærlighed.com',], ['http://あーるいん.com', 'http://あーるいん.com',], ['http://예비교사.com', 'http://예비교사.com',], ['http://example.com.', 'http://example.com.',], ['http://example.com?', 'http://example.com?',], ['http://example.com!', 'http://example.com!',], ['http://example.com,', 'http://example.com,',], ['http://example.com;', 'http://example.com;',], ['http://example.com:', 'http://example.com:',], ['\'http://example.com\'', '\'http://example.com\'',], ['"http://example.com"', '"http://example.com"',], ['"http://example.com/"', '"http://example.com/"',], ['http://example.com', 'http://example.com',], ['(http://example.com)', '(http://example.com)',], ['[http://example.com]', '[http://example.com]',], ['', '<http://example.com>',], ['http://example.com/path/(foo)/bar', 'http://example.com/path/(foo)/bar',], ['http://example.com/path/[foo]/bar', 'http://example.com/path/[foo]/bar',], ['http://example.com/path/foo/(bar)', 'http://example.com/path/foo/(bar)',], //Not a valid url - urls cannot contain unencoded square brackets ['http://example.com/path/foo/[bar]', 'http://example.com/path/foo/[bar]',], ['Hey, check out my cool site http://example.com okay?', 'Hey, check out my cool site http://example.com okay?',], ['What about parens (e.g. http://example.com/path/foo/(bar))?', 'What about parens (e.g. http://example.com/path/foo/(bar))?',], ['What about parens (e.g. http://example.com/path/foo/(bar)?', 'What about parens (e.g. http://example.com/path/foo/(bar)?',], ['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 ['What about parens (e.g. http://example.com/path/(foo,bar)?', 'What about parens (e.g. http://example.com/path/(foo,bar)?',], ['Unbalanced too (e.g. http://example.com/path/((((foo)/bar)?', 'Unbalanced too (e.g. http://example.com/path/((((foo)/bar)?',], ['Unbalanced too (e.g. http://example.com/path/(foo))))/bar)?', 'Unbalanced too (e.g. http://example.com/path/(foo))))/bar)?',], ['Unbalanced too (e.g. http://example.com/path/foo/((((bar)?', 'Unbalanced too (e.g. http://example.com/path/foo/((((bar)?',], ['Unbalanced too (e.g. http://example.com/path/foo/(bar))))?', 'Unbalanced too (e.g. http://example.com/path/foo/(bar))))?',], ['file.ext', 'file.ext',], ['file.html', 'file.html',], ['file.php', 'file.php',], // scheme-less HTTP URLs with @ in the path: http://status.net/open-source/issues/2248 ['http://flickr.com/photos/34807140@N05/3838905434', 'http://flickr.com/photos/34807140@N05/3838905434',], ]; } public static function linkifyProvider() { return [ //bare ip addresses are no longer supported ['127.0.0.1', '127.0.0.1', 'bare_ipv4',], ['127.0.0.1:99', '127.0.0.1:99', 'bare_ipv4',], ['127.0.0.1/Name:test.php', '127.0.0.1/Name:test.php', 'bare_ipv4',], ['127.0.0.1/~test', '127.0.0.1/~test', 'bare_ipv4',], ['127.0.0.1/+test', '127.0.0.1/+test', 'bare_ipv4',], ['127.0.0.1/$test', '127.0.0.1/$test', 'bare_ipv4',], ['127.0.0.1/\'test', '127.0.0.1/\'test', 'bare_ipv4',], ['127.0.0.1/"test', '127.0.0.1/"test', 'bare_ipv4',], ['127.0.0.1/test"test', '127.0.0.1/test"test', 'bare_ipv4',], ['127.0.0.1/-test', '127.0.0.1/-test', 'bare_ipv4',], ['127.0.0.1/_test', '127.0.0.1/_test', 'bare_ipv4',], ['127.0.0.1/!test', '127.0.0.1/!test', 'bare_ipv4',], ['127.0.0.1/*test', '127.0.0.1/*test', 'bare_ipv4',], ['127.0.0.1/test%20stuff', '127.0.0.1/test%20stuff', 'bare_ipv4',], ['2001:4978:1b5:0:21d:e0ff:fe66:59ab/test.php', '2001:4978:1b5:0:21d:e0ff:fe66:59ab/test.php', 'bare_ipv6',], ['[2001:4978:1b5:0:21d:e0ff:fe66:59ab]:99/test.php', '[2001:4978:1b5:0:21d:e0ff:fe66:59ab]:99/test.php', 'bare_ipv6',], ['2001:4978:1b5:0:21d:e0ff:fe66:59ab', '2001:4978:1b5:0:21d:e0ff:fe66:59ab', 'bare_ipv6',], ['example.com', 'example.com', 'bare_domains',], ['flickr.com/photos/34807140@N05/3838905434', 'flickr.com/photos/34807140@N05/3838905434', 'bare_domains',], ['What about parens (e.g. example.com/path/foo/(bar))?', 'What about parens (e.g. example.com/path/foo/(bar))?', 'bare_domains',], ['What about parens (e.g. example.com/path/foo/(bar)?', 'What about parens (e.g. example.com/path/foo/(bar)?', 'bare_domains',], ['What about parens (e.g. example.com/path/foo/(bar).)?', 'What about parens (e.g. example.com/path/foo/(bar).?', 'bare_domains',], ['What about parens (e.g. example.com/path/(foo,bar)?', 'What about parens (e.g. example.com/path/(foo,bar)?', 'bare_domains',], ['example.com', 'example.com', 'bare_domains',], ['example.org', 'example.org', 'bare_domains',], ['example.co.uk', 'example.co.uk', 'bare_domains',], ['www.example.co.uk', 'www.example.co.uk', 'bare_domains',], ['farm1.images.example.co.uk', 'farm1.images.example.co.uk', 'bare_domains',], ['example.museum', 'example.museum', 'bare_domains',], ['example.travel', 'example.travel', 'bare_domains',], ['example.com.', 'example.com.', 'bare_domains',], ['example.com?', 'example.com?', 'bare_domains',], ['example.com!', 'example.com!', 'bare_domains',], ['example.com,', 'example.com,', 'bare_domains',], ['example.com;', 'example.com;', 'bare_domains',], ['example.com:', 'example.com:', 'bare_domains',], ['\'example.com\'', '\'example.com\'', 'bare_domains',], ['"example.com"', '"example.com"', 'bare_domains',], ['example.com', 'example.com', 'bare_domains',], ['(example.com)', '(example.com)', 'bare_domains',], ['[example.com]', '[example.com]', 'bare_domains',], ['', '<example.com>', 'bare_domains',], ['Hey, check out my cool site example.com okay?', 'Hey, check out my cool site example.com okay?', 'bare_domains',], ['Hey, check out my cool site example.com.I made it.', 'Hey, check out my cool site example.com.I made it.', 'bare_domains',], ['Hey, check out my cool site example.com.Funny thing...', 'Hey, check out my cool site example.com.Funny thing...', 'bare_domains',], ['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',], ['example.com:8080/test.php', 'example.com:8080/test.php', 'bare_domains',], ['user_name+other@example.com', 'user_name+other@example.com', 'bare_domains',], ['user@example.com', 'user@example.com', 'bare_domains',], ]; } }