source: trunk/tests/UtilitiesTest.php @ 653

Last change on this file since 653 was 653, checked in by anonymous, 5 years ago

Update fancyTxt()

File size: 25.6 KB
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2012 Strangecode, LLC
6 *
7 * This file is part of The Strangecode Codebase.
8 *
9 * The Strangecode Codebase is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * The Strangecode Codebase is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/**
24 * PHPUnit test case for codebase/lib/Utilities.inc.php
25 *
26 * The method skeletons below need to be filled in with
27 * real data so that the tests will run correctly. Replace
28 * all EXPECTED_VAL and PARAM strings with real data.
29 *
30 * Created with PHPUnit_Skeleton on 2005-08-09
31 */
32
33class UtilitiesTest extends PHPUnit_Framework_TestCase {
34
35    function setUp()
36    {
37        // define('_CLI', true);
38        require dirname(__FILE__) . '/_config.inc.php';
39        require_once '../lib/Utilities.inc.php';
40        // $this->Utilities =& new Utilities(PARAM);
41    }
42
43    function tearDown()
44    {
45        $app =& App::getInstance();
46        $app->stop();
47    }
48
49    function test_hyperlinkTxt()
50    {
51        $urls = [
52            ' http://www.asdf.com/ ' => ' <a href="http://www.asdf.com/">www.asdf.com</a> ',
53            ' ws://www.asdf.com/ ' => ' <a href="ws://www.asdf.com/">ws://www.asdf.com/</a> ',
54            ' sftp://user@host.tld:22/path/x ' => ' <a href="sftp://user@host.tld:22/path/x">sftp://user@host.tld:22/path/x</a> ',
55            ' www.asdf.com/ ' => ' <a href="http://www.asdf.com/">www.asdf.com</a> ',
56            ' http://asdf/ ' => ' <a href="http://asdf/">asdf</a> ',
57            ' www.asdf.com/? ' => ' <a href="http://www.asdf.com/">www.asdf.com</a>? ',
58            ' www.asdf.com/?x=y+z ' => ' <a href="http://www.asdf.com/?x=y+z">www.asdf.com/?x=y+z</a> ',
59            ' www.asdf.com/?x=y+ ' => ' <a href="http://www.asdf.com/?x=y+">www.asdf.com/?x=y+</a> ',
60            ' www.asdf.com/?x= ' => ' <a href="http://www.asdf.com/?x=">www.asdf.com/?x=</a> ',
61            ' www.asdf.com/?x ' => ' <a href="http://www.asdf.com/?x">www.asdf.com/?x</a> ',
62            ' www.asdf.com/?❀=🔥 ' => ' <a href="http://www.asdf.com/?❀=🔥">www.asdf.com/?❀=🔥</a> ',
63            ' www.asdf.com? ' => ' <a href="http://www.asdf.com">www.asdf.com</a>? ',
64            ' www.asdf.com! ' => ' <a href="http://www.asdf.com">www.asdf.com</a>! ',
65            ' www.asdf.com. ' => ' <a href="http://www.asdf.com">www.asdf.com</a>. ',
66            ' `www.asdf.com` ' => ' `www.asdf.com` ',
67            ' "www.asdf.com" ' => ' "www.asdf.com" ',
68            ' <www.asdf.com> ' => ' <<a href="http://www.asdf.com">www.asdf.com</a>> ',
69            ' <http://www.asdf.com> ' => ' <<a href="http://www.asdf.com">www.asdf.com</a>> ',
70            ' (http://www.asdf.com) ' => ' (<a href="http://www.asdf.com">www.asdf.com</a>) ',
71            ' (URL: http://www.asdf.com#1) ' => ' (URL: <a href="http://www.asdf.com#1">www.asdf.com#1</a>) ',
72            ' <a href="http://www.example.com/">Click Here</a> ' => ' <a href="http://www.example.com/">Click Here</a> ',
73            ' <a href="http://www.example.com/">http://www.example.com/</a> ' => ' <a href="http://www.example.com/">http://www.example.com/</a> ',
74            ' <a href=http://www.example.com/>http://www.example.com/</a> ' => ' <a href=http://www.example.com/>http://www.example.com/</a> ',
75            ' <a href=\'http://www.example.com/\' >http://www.example.com/</a> ' => ' <a href=\'http://www.example.com/\' >http://www.example.com/</a> ',
76            ' http://foo.com/blah_blah ' => ' <a href="http://foo.com/blah_blah">foo.com/blah_blah</a> ',
77            ' http://foo.com/blah_blah/ ' => ' <a href="http://foo.com/blah_blah/">foo.com/blah_blah/</a> ',
78            ' http://foo.com/blah_blah_(wikipedia) ' => ' <a href="http://foo.com/blah_blah_(wikipedia">foo.com/blah_blah_(wikipedia</a>) ',
79            ' http://foo.com/blah_blah_(wikipedia)_(again) ' => ' <a href="http://foo.com/blah_blah_(wikipedia)_(again">foo.com/blah_blah_(wikipedia)_(again</a>) ',
80            ' http://www.example.com/wpstyle/?p=364 ' => ' <a href="http://www.example.com/wpstyle/?p=364">www.example.com/wpstyle/?p=364</a> ',
81            ' https://www.example.com/foo/?bar=baz&inga=42&quux ' => ' <a href="https://www.example.com/foo/?bar=baz&amp;inga=42&amp;quux">www.example.com/foo/?bar=baz&inga=42&quux</a> ',
82            ' http://✪df.ws/123 ' => ' <a href="http://✪df.ws/123">✪df.ws/123</a> ',
83            ' http://userid:password@example.com:8080 ' => ' <a href="http://userid:password@example.com:8080">userid:password@example.com:8080</a> ',
84            ' http://userid:password@example.com:8080/ ' => ' <a href="http://userid:password@example.com:8080/">userid:password@example.com:8080</a> ',
85            ' http://userid@example.com ' => ' <a href="http://userid@example.com">userid@example.com</a> ',
86            ' http://userid@example.com/ ' => ' <a href="http://userid@example.com/">userid@example.com</a> ',
87            ' http://userid@example.com:8080 ' => ' <a href="http://userid@example.com:8080">userid@example.com:8080</a> ',
88            ' http://userid@example.com:8080/ ' => ' <a href="http://userid@example.com:8080/">userid@example.com:8080</a> ',
89            ' http://userid:password@example.com ' => ' <a href="http://userid:password@example.com">userid:password@example.com</a> ',
90            ' http://userid:password@example.com/ ' => ' <a href="http://userid:password@example.com/">userid:password@example.com</a> ',
91            ' http://142.42.1.1/ ' => ' <a href="http://142.42.1.1/">142.42.1.1</a> ',
92            ' http://142.42.1.1:8080/ ' => ' <a href="http://142.42.1.1:8080/">142.42.1.1:8080</a> ',
93            ' http://➡.ws/äš¹ ' => ' <a href="http://➡.ws/äš¹">➡.ws/äš¹</a> ',
94            ' http://⌘.ws ' => ' <a href="http://⌘.ws">⌘.ws</a> ',
95            ' http://⌘.ws/ ' => ' <a href="http://⌘.ws/">⌘.ws</a> ',
96            ' http://foo.com/blah_(wikipedia)#cite-1 ' => ' <a href="http://foo.com/blah_(wikipedia)#cite-1">foo.com/blah_(wikipedia)#cite-1</a> ',
97            ' http://foo.com/blah_(wikipedia)_blah#cite-1 ' => ' <a href="http://foo.com/blah_(wikipedia)_blah#cite-1">foo.com/blah_(wikipedia)_blah#cite-1</a> ',
98            ' http://foo.com/unicode_(✪)_in_parens ' => ' <a href="http://foo.com/unicode_(✪)_in_parens">foo.com/unicode_(✪)_in_parens</a> ',
99            ' http://foo.com/(something)?after=parens ' => ' <a href="http://foo.com/(something)?after=parens">foo.com/(something)?after=parens</a> ',
100            ' http://☺.damowmow.com/ ' => ' <a href="http://☺.damowmow.com/">☺.damowmow.com</a> ',
101            ' http://code.google.com/events/#&product=browser ' => ' <a href="http://code.google.com/events/#&amp;product=browser">code.google.com/events/#&product=browser</a> ',
102            ' http://j.mp ' => ' <a href="http://j.mp">j.mp</a> ',
103            ' ftp://foo.bar/baz ' => ' <a href="ftp://foo.bar/baz">ftp://foo.bar/baz</a> ',
104            ' http://foo.bar/?q=Test%20URL-encoded%20stuff ' => ' <a href="http://foo.bar/?q=Test%20URL-encoded%20stuff">foo.bar/?q=Test%20URL-encoded%20stuff</a> ',
105            ' http://مثال.إختؚار ' => ' <a href="http://مثال.إختؚار">مثال.إختؚار</a> ',
106            ' http://䟋子.测试 ' => ' <a href="http://䟋子.测试">䟋子.测试</a> ',
107            ' http://à€‰à€Šà€Ÿà€¹à€°à€£.à€ªà€°à¥€à€•à¥à€·à€Ÿ ' => ' <a href="http://à€‰à€Šà€Ÿà€¹à€°à€£.à€ªà€°à¥€à€•à¥à€·à€Ÿ">à€‰à€Šà€Ÿà€¹à€°à€£.à€ªà€°à¥€à€•à¥à€·à€Ÿ</a> ',
108            ' http://-.~_!$&\'()*+,;=:%40:80%2f::::::@example.com ' => ' <a href="http://-.~_!$&amp;&#039;()*+,;=:%40:80%2f::::::@example.com">-.~_!$&\'()*+,;=:%40:80%2f::::::@example.com</a> ',
109            ' http://1337.net ' => ' <a href="http://1337.net">1337.net</a> ',
110            ' http://a.b-c.de ' => ' <a href="http://a.b-c.de">a.b-c.de</a> ',
111            ' http://223.255.255.254 ' => ' <a href="http://223.255.255.254">223.255.255.254</a> ',
112            ' http://x and.co OK?' => ' http://x and.co OK?',
113            ' http://foo.bar?q=Spaces should be encoded ' => ' <a href="http://foo.bar?q=Spaces">foo.bar?q=Spaces</a> should be encoded ',
114            ' https://github.com/search?utf8=✓&q=gif ' => ' <a href="https://github.com/search?utf8=✓&amp;q=gif">github.com/search?utf8=✓&q=gif</a> ',
115            ' https://github.com/search?q=gif&utf8=* ' => ' <a href="https://github.com/search?q=gif&amp;utf8=*">github.com/search?q=gif&utf8=*</a> ',
116            ' https://github.com/search?q=gif&utf8=✓ ' => ' <a href="https://github.com/search?q=gif&amp;utf8=✓">github.com/search?q=gif&utf8=✓</a> ',
117
118            // These should fail.
119            ' http://x and.co OK?' => ' http://x and.co OK?',
120            ' www. ' => ' www. ',
121            ' www.x ' => ' www.x ',
122            ' http: ' => ' http: ',
123            ' http:// ' => ' http:// ',
124            ' http:x ' => ' http:x ',
125            ' http://x ' => ' http://x ',
126            ' http://x/ ' => ' http://x/ ',
127            ' http:// ?.x.com/ ' => ' http:// ?.x.com/ ',
128            ' http://?.com/ ' => ' http://?.com/ ',
129            ' http://.?.com/ ' => ' http://.?.com/ ',
130
131            // These are allowed to fuckup due to limitations in our ability to be omnipotent.
132            ' http:// www.x.com/ ' => ' http:// <a href="http://www.x.com/">www.x.com</a> ',
133            ' <a href=http://www.example.com/ > http://www.example.com/</a> ' => ' <a href=http://www.example.com/ > <a href="http://www.example.com/">www.example.com</a></a> ',
134
135            '<a href="http://www.example.com">Lorem ipsum</a> <a href="sftp://user@host.tld/path">dolor sit amet</a> sftp://user@host.tld/path, [http://www.example.com/ consetetur] sadipscing elitr, (sed diam) www.example.com! Nonumy http://x.com! Invidunt mailto:asdf@asdf.com ut <a href="mailto:asdf@asdf.com">asdf@asdf.com</a> \'\'\'et dolore\'\'\' magna. <!-- comment -->  Aliquyam:erat, sed://diam //voluptua//. At vero.eos.et {{{ for (var i = Things.length - 1; i >= 0; i--) { Things[i]; } }}} `http://x.com` justo duo doloreshttp://www.not.com!' => '<a href="http://www.example.com">Lorem ipsum</a> <a href="sftp://user@host.tld/path">dolor sit amet</a> <a href="sftp://user@host.tld/path">sftp://user@host.tld/path</a>, [<a href="http://www.example.com/">www.example.com</a> consetetur] sadipscing elitr, (sed diam) <a href="http://www.example.com">www.example.com</a>! Nonumy <a href="http://x.com">x.com</a>! Invidunt <a href="mailto:asdf@asdf.com">mailto:asdf@asdf.com</a> ut <a href="mailto:asdf@asdf.com">asdf@asdf.com</a> \'\'\'et dolore\'\'\' magna. <!-- comment -->  Aliquyam:erat, <a href="sed://diam">sed://diam</a> //voluptua//. At vero.eos.et {{{ for (var i = Things.length - 1; i >= 0; i--) { Things[i]; } }}} `http://x.com` justo duo <a href="doloreshttp://www.not.com">doloreshttp://www.not.com</a>!',
136        ];
137
138        $urls_strict = [
139            ' http://www.asdf.com/ ' => ' <a href="http://www.asdf.com/">www.asdf.com</a> ',
140            ' ws://www.asdf.com/ ' => ' <a href="ws://www.asdf.com/">ws://www.asdf.com/</a> ',
141            ' sftp://user@host.tld:22/path/x ' => ' <a href="sftp://user@host.tld:22/path/x">sftp://user@host.tld:22/path/x</a> ',
142            ' sip:beer ' => ' <a href="sip:beer">sip:beer</a> ',
143            ' http://asdf/ ' => ' <a href="http://asdf/">asdf</a> ',
144            ' http://www.asdf.com/? ' => ' <a href="http://www.asdf.com/?">www.asdf.com/?</a> ',
145            ' http://www.asdf.com/?x=y+z ' => ' <a href="http://www.asdf.com/?x=y+z">www.asdf.com/?x=y+z</a> ',
146            ' http://www.asdf.com/?x=y+ ' => ' <a href="http://www.asdf.com/?x=y+">www.asdf.com/?x=y+</a> ',
147            ' http://www.asdf.com/?x= ' => ' <a href="http://www.asdf.com/?x=">www.asdf.com/?x=</a> ',
148            ' http://www.asdf.com/?x ' => ' <a href="http://www.asdf.com/?x">www.asdf.com/?x</a> ',
149            ' https://www.asdf.com/?❀=🔥 ' => ' <a href="https://www.asdf.com/?❀=🔥">www.asdf.com/?❀=🔥</a> ',
150            ' www.asdf.com? ' => ' www.asdf.com? ',
151            ' www.asdf.com/ ' => ' www.asdf.com/ ',
152            ' www.asdf.com/? ' => ' www.asdf.com/? ',
153            ' www.asdf.com/?x=y+z ' => ' www.asdf.com/?x=y+z ',
154            ' www.asdf.com/?x=y+ ' => ' www.asdf.com/?x=y+ ',
155            ' www.asdf.com/?x= ' => ' www.asdf.com/?x= ',
156            ' www.asdf.com/?x ' => ' www.asdf.com/?x ',
157            ' www.asdf.com/?❀=🔥 ' => ' www.asdf.com/?❀=🔥 ',
158            ' www.asdf.com! ' => ' www.asdf.com! ',
159            ' www.asdf.com. ' => ' www.asdf.com. ',
160            ' `www.asdf.com` ' => ' `www.asdf.com` ',
161            ' "www.asdf.com" ' => ' "www.asdf.com" ',
162            ' <www.asdf.com> ' => ' <www.asdf.com> ',
163            ' `http://www.asdf.com` ' => ' `http://www.asdf.com` ',
164            ' "http://www.asdf.com" ' => ' "http://www.asdf.com" ',
165            ' <http://www.asdf.com> ' => ' <<a href="http://www.asdf.com">www.asdf.com</a>> ',
166            ' (http://www.asdf.com) ' => ' (<a href="http://www.asdf.com)">www.asdf.com)</a> ',
167            ' (URL: http://www.asdf.com#1) ' => ' (URL: <a href="http://www.asdf.com#1)">www.asdf.com#1)</a> ',
168            ' <a href="http://www.example.com/">Click Here</a> ' => ' <a href="http://www.example.com/">Click Here</a> ',
169            ' <a href="http://www.example.com/">http://www.example.com/</a> ' => ' <a href="http://www.example.com/">http://www.example.com/</a> ',
170            ' <a href=http://www.example.com/>http://www.example.com/</a> ' => ' <a href=http://www.example.com/>http://www.example.com/</a> ',
171            ' <a href=\'http://www.example.com/\' >http://www.example.com/</a> ' => ' <a href=\'http://www.example.com/\' >http://www.example.com/</a> ',
172            ' http://foo.com/blah_blah ' => ' <a href="http://foo.com/blah_blah">foo.com/blah_blah</a> ',
173            ' http://foo.com/blah_blah/ ' => ' <a href="http://foo.com/blah_blah/">foo.com/blah_blah/</a> ',
174            ' http://foo.com/blah_blah_(wikipedia) ' => ' <a href="http://foo.com/blah_blah_(wikipedia)">foo.com/blah_blah_(wikipedia)</a> ',
175            ' http://foo.com/blah_blah_(wikipedia)_(again) ' => ' <a href="http://foo.com/blah_blah_(wikipedia)_(again)">foo.com/blah_blah_(wikipedia)_(again)</a> ',
176            ' http://www.example.com/wpstyle/?p=364 ' => ' <a href="http://www.example.com/wpstyle/?p=364">www.example.com/wpstyle/?p=364</a> ',
177            ' https://www.example.com/foo/?bar=baz&inga=42&quux ' => ' <a href="https://www.example.com/foo/?bar=baz&amp;inga=42&amp;quux">www.example.com/foo/?bar=baz&inga=42&quux</a> ',
178            ' http://✪df.ws/123 ' => ' <a href="http://✪df.ws/123">✪df.ws/123</a> ',
179            ' http://userid:password@example.com:8080 ' => ' <a href="http://userid:password@example.com:8080">userid:password@example.com:8080</a> ',
180            ' http://userid:password@example.com:8080/ ' => ' <a href="http://userid:password@example.com:8080/">userid:password@example.com:8080</a> ',
181            ' http://userid@example.com ' => ' <a href="http://userid@example.com">userid@example.com</a> ',
182            ' http://userid@example.com/ ' => ' <a href="http://userid@example.com/">userid@example.com</a> ',
183            ' http://userid@example.com:8080 ' => ' <a href="http://userid@example.com:8080">userid@example.com:8080</a> ',
184            ' http://userid@example.com:8080/ ' => ' <a href="http://userid@example.com:8080/">userid@example.com:8080</a> ',
185            ' http://userid:password@example.com ' => ' <a href="http://userid:password@example.com">userid:password@example.com</a> ',
186            ' http://userid:password@example.com/ ' => ' <a href="http://userid:password@example.com/">userid:password@example.com</a> ',
187            ' http://142.42.1.1/ ' => ' <a href="http://142.42.1.1/">142.42.1.1</a> ',
188            ' http://142.42.1.1:8080/ ' => ' <a href="http://142.42.1.1:8080/">142.42.1.1:8080</a> ',
189            ' http://➡.ws/äš¹ ' => ' <a href="http://➡.ws/äš¹">➡.ws/äš¹</a> ',
190            ' http://⌘.ws ' => ' <a href="http://⌘.ws">⌘.ws</a> ',
191            ' http://⌘.ws/ ' => ' <a href="http://⌘.ws/">⌘.ws</a> ',
192            ' http://foo.com/blah_(wikipedia)#cite-1 ' => ' <a href="http://foo.com/blah_(wikipedia)#cite-1">foo.com/blah_(wikipedia)#cite-1</a> ',
193            ' http://foo.com/blah_(wikipedia)_blah#cite-1 ' => ' <a href="http://foo.com/blah_(wikipedia)_blah#cite-1">foo.com/blah_(wikipedia)_blah#cite-1</a> ',
194            ' http://foo.com/unicode_(✪)_in_parens ' => ' <a href="http://foo.com/unicode_(✪)_in_parens">foo.com/unicode_(✪)_in_parens</a> ',
195            ' http://foo.com/(something)?after=parens ' => ' <a href="http://foo.com/(something)?after=parens">foo.com/(something)?after=parens</a> ',
196            ' http://☺.damowmow.com/ ' => ' <a href="http://☺.damowmow.com/">☺.damowmow.com</a> ',
197            ' http://code.google.com/events/#&product=browser ' => ' <a href="http://code.google.com/events/#&amp;product=browser">code.google.com/events/#&product=browser</a> ',
198            ' http://j.mp ' => ' <a href="http://j.mp">j.mp</a> ',
199            ' ftp://foo.bar/baz ' => ' <a href="ftp://foo.bar/baz">ftp://foo.bar/baz</a> ',
200            ' http://foo.bar/?q=Test%20URL-encoded%20stuff ' => ' <a href="http://foo.bar/?q=Test%20URL-encoded%20stuff">foo.bar/?q=Test%20URL-encoded%20stuff</a> ',
201            ' http://مثال.إختؚار ' => ' <a href="http://مثال.إختؚار">مثال.إختؚار</a> ',
202            ' http://䟋子.测试 ' => ' <a href="http://䟋子.测试">䟋子.测试</a> ',
203            ' http://à€‰à€Šà€Ÿà€¹à€°à€£.à€ªà€°à¥€à€•à¥à€·à€Ÿ ' => ' <a href="http://à€‰à€Šà€Ÿà€¹à€°à€£.à€ªà€°à¥€à€•à¥à€·à€Ÿ">à€‰à€Šà€Ÿà€¹à€°à€£.à€ªà€°à¥€à€•à¥à€·à€Ÿ</a> ',
204            ' http://-.~_!$&\'()*+,;=:%40:80%2f::::::@example.com ' => ' <a href="http://-.~_!$&amp;&#039;()*+,;=:%40:80%2f::::::@example.com">-.~_!$&\'()*+,;=:%40:80%2f::::::@example.com</a> ',
205            ' http://1337.net ' => ' <a href="http://1337.net">1337.net</a> ',
206            ' http://a.b-c.de ' => ' <a href="http://a.b-c.de">a.b-c.de</a> ',
207            ' http://223.255.255.254 ' => ' <a href="http://223.255.255.254">223.255.255.254</a> ',
208            ' http://foo.bar?q=Spaces should be encoded ' => ' <a href="http://foo.bar?q=Spaces">foo.bar?q=Spaces</a> should be encoded ',
209            ' https://github.com/search?utf8=✓&q=gif ' => ' <a href="https://github.com/search?utf8=✓&amp;q=gif">github.com/search?utf8=✓&q=gif</a> ',
210            ' https://github.com/search?q=gif&utf8=* ' => ' <a href="https://github.com/search?q=gif&amp;utf8=*">github.com/search?q=gif&utf8=*</a> ',
211            ' https://github.com/search?q=gif&utf8=✓ ' => ' <a href="https://github.com/search?q=gif&amp;utf8=✓">github.com/search?q=gif&utf8=✓</a> ',
212
213            // Unusual schemes.
214            ' view-source:https://trac.strangecode.com/x/ticket/2#comment:4 ' => ' <a href="view-source:https://trac.strangecode.com/x/ticket/2#comment:4">view-source:https://trac.strangecode.com/x/ticket/2#comment:4</a> ',
215            ' mailto:asdf@asdf.com ' => ' <a href="mailto:asdf@asdf.com">mailto:asdf@asdf.com</a> ',
216            ' facetime://+19995551234 ' => ' <a href="facetime://+19995551234">facetime://+19995551234</a> ',
217            ' tel:+15552001010 ' => ' <a href="tel:+15552001010">tel:+15552001010</a> ',
218            ' magnet:?xt=urn:ed2k:354B15E68FB8F36D7CD88FF94116CDC1&xt=urn:tree:tiger:7N5OAMRNGMSSEUE3ORHOKWN4WWIQ5X4EBOOTLJY&xt=urn:btih:QHQXPYWMACKDWKP47RRVIV7VOURXFE5Q&xl=10826029&dn=mediawiki-1.15.1.tar.gz&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&as=http%3A%2F%2Fdownload.wikimedia.org%2Fmediawiki%2F1.15%2Fmediawiki-1.15.1.tar.gz&xs=http%3A%2F%2Fcache.example.org%2FXRX2PEFXOOEJFRVUCX6HMZMKS5TWG4K5&xs=dchub://example.org ' => ' <a href="magnet:?xt=urn:ed2k:354B15E68FB8F36D7CD88FF94116CDC1&amp;xt=urn:tree:tiger:7N5OAMRNGMSSEUE3ORHOKWN4WWIQ5X4EBOOTLJY&amp;xt=urn:btih:QHQXPYWMACKDWKP47RRVIV7VOURXFE5Q&amp;xl=10826029&amp;dn=mediawiki-1.15.1.tar.gz&amp;tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&amp;as=http%3A%2F%2Fdownload.wikimedia.org%2Fmediawiki%2F1.15%2Fmediawiki-1.15.1.tar.gz&amp;xs=http%3A%2F%2Fcache.example.org%2FXRX2PEFXOOEJFRVUCX6HMZMKS5TWG4K5&amp;xs=dchub://example.org">magnet:?xt=urn:ed2k:354B15E68FB8F36D7CD88FF94116CDC1&xt=urn:tree:tiger:7N5OAMRNGMSSEUE3ORHOKWN4WWIQ5X4EBOOTLJY&xt=urn:btih:QHQXPYWMACKDWKP47RRVIV7VOURXFE5Q&xl=10826029&dn=mediawiki-1.15.1.tar.gz&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&as=http%3A%2F%2Fdownload.wikimedia.org%2Fmediawiki%2F1.15%2Fmediawiki-1.15.1.tar.gz&xs=http%3A%2F%2Fcache.example.org%2FXRX2PEFXOOEJFRVUCX6HMZMKS5TWG4K5&xs=dchub://example.org</a> ',
219            ' chrome-extension://71f05c488a96b441ef679b09267ea46c/index.html ' => ' <a href="chrome-extension://71f05c488a96b441ef679b09267ea46c/index.html">chrome-extension://71f05c488a96b441ef679b09267ea46c/index.html</a> ',
220
221            // These should fail.
222            ' http://x and.co OK?' => ' http://x and.co OK?',
223            ' www. ' => ' www. ',
224            ' www.x ' => ' www.x ',
225            ' http: ' => ' http: ',
226            ' http:// ' => ' http:// ',
227            ' http:x ' => ' http:x ',
228            ' http://x ' => ' http://x ',
229            ' http://x/ ' => ' http://x/ ',
230            ' http:// www.x.com/ ' => ' http:// www.x.com/ ',
231            ' http://?.com/ ' => ' http://?.com/ ',
232            ' http://.?.com/ ' => ' http://.?.com/ ',
233
234            // These are allowed to fuckup due to limitations in our ability to be omnipotent.
235            ' <a href=http://www.example.com/ > http://www.example.com/</a> ' => ' <a href=http://www.example.com/ > <a href="http://www.example.com/">www.example.com</a></a> ',
236            ' dummyhttp://www.asdf.com/ ' => ' <a href="dummyhttp://www.asdf.com/">dummyhttp://www.asdf.com/</a> ',
237
238            '<a href="http://www.example.com">Lorem ipsum</a> <a href="sftp://user@host.tld/path">dolor sit amet</a> sftp://user@host.tld/path, [http://www.example.com/ consetetur] sadipscing elitr, (sed diam) www.example.com! Nonumy http://x.com! Invidunt mailto:asdf@asdf.com ut <a href="mailto:asdf@asdf.com">asdf@asdf.com</a> \'\'\'et dolore\'\'\' magna. <!-- comment -->  Aliquyam:erat, sed://diam //voluptua//. At vero.eos.et {{{ for (var i = Things.length - 1; i >= 0; i--) { Things[i]; } }}} `http://x.com` justo duo doloreshttp://www.not.com!' => '<a href="http://www.example.com">Lorem ipsum</a> <a href="sftp://user@host.tld/path">dolor sit amet</a> <a href="sftp://user@host.tld/path,">sftp://user@host.tld/path,</a> [<a href="http://www.example.com/">www.example.com</a> consetetur] sadipscing elitr, (sed diam) www.example.com! Nonumy <a href="http://x.com!">x.com!</a> Invidunt <a href="mailto:asdf@asdf.com">mailto:asdf@asdf.com</a> ut <a href="mailto:asdf@asdf.com">asdf@asdf.com</a> \'\'\'et dolore\'\'\' magna. <!-- comment -->  Aliquyam:erat, <a href="sed://diam">sed://diam</a> //voluptua//. At vero.eos.et {{{ for (var i = Things.length - 1; i >= 0; i--) { Things[i]; } }}} `http://x.com` justo duo <a href="doloreshttp://www.not.com!">doloreshttp://www.not.com!</a>',
239        ];
240        foreach ($urls as $input => $expected) {
241            $result = hyperlinkTxt($input, false);
242            $this->assertEquals($expected, $result, sprintf('Failed with input: %s', $input));
243        }
244        foreach ($urls_strict as $input => $expected) {
245            // printf("'%s' => '%s',\n", $input, hyperlinkTxt($input, true));
246            $result = hyperlinkTxt($input, true);
247            $this->assertEquals($expected, $result, sprintf('Failed with input: %s', $input));
248        }
249    }
250
251    function test_fancyTxt()
252    {
253        $strings = [
254            'Due to the <a href="https://en.wikipedia.org/wiki/Camp_Fire_(2018)">fires in "Paradise," CA</a> it\'s a bad day.' => '• Due to the <a href="https://en.wikipedia.org/wiki/Camp_Fire_(2018)">fires in “Paradise,” CA</a> it’s a bad day.',
255            'Simply--"\'check back\' later"--in 6\'3"...' => '• Simply – “‘check back’ later” – in 6\'3"
',
256            '"Double "double \'single\'""--?' => '• "Double “double ‘single’”" – ?',
257            '<a href="javascript:openPopup(\'/foo/bar.php\')">Click here</a>....' => '• <a href="javascript:openPopup(\'/foo/bar.php\')">Click here</a>....',
258            'If you have any questions please <a href="/contact/">contact the ants\' mailboxes</a>.' => '• If you have any questions please <a href="/contact/">contact the ants’ mailboxes</a>.',
259            'This URL: "http://䟋子.卷筒纞/?x=y&1=2#asdf"' => '• This URL: “http://䟋子.卷筒纞/?x=y&1=2#asdf”',
260            // '\'...\' and "..."' => '• ‘
’ and “
”', // FIXME: this one doesn't work.
261            '\'1...\' and "2..."' => '• ‘1
’ and “2
”',
262            '\'One\'s self...\' and "Twosome"' => '• ‘One’s self
’ and “Twosome”',
263            '"\'Well,\' said I, \'the gentleman at No. 4.\'"' => '• “‘Well,’ said I, ‘the gentleman at No. 4.’”',
264            '"...an office in Leadenhall Street--and--"
265"O\'Reilly\'s office?"
266"What!" he roared.
267"Yes, to-day."* She stood smiling.'
268=>
269'• “
an office in Leadenhall Street – and – ”
270“O’Reilly’s office?”
271“What!” he roared.
272“Yes, to-day.”* She stood smiling.',
273        ];
274        foreach ($strings as $input => $expected) {
275            $result = fancyTxt($input, ['/^/', '/^·/'], ['· ', '•']);
276            $this->assertEquals($expected, $result, sprintf('Failed with input: %s', $input));
277        }
278    }
279}
Note: See TracBrowser for help on using the repository browser.