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 | |
---|
33 | class 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&inga=42&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/#&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://-.~_!$&'()*+,;=:%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=â&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&utf8=*">github.com/search?q=gif&utf8=*</a> ', |
---|
116 | ' https://github.com/search?q=gif&utf8=â ' => ' <a href="https://github.com/search?q=gif&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&inga=42&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/#&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://-.~_!$&'()*+,;=:%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=â&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&utf8=*">github.com/search?q=gif&utf8=*</a> ', |
---|
211 | ' https://github.com/search?q=gif&utf8=â ' => ' <a href="https://github.com/search?q=gif&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&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">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 | |
---|
280 | function test_URLSlug() |
---|
281 | { |
---|
282 | $strings = [ |
---|
283 | 'This becomes a slug' => 'this-becomes-a-slug', |
---|
284 | 'http://äŸå.å·ç纞/?x=y&1=2#asdf' => 'http-%e4%be%8b%e5%ad%90-%e5%8d%b7%e7%ad%92%e7%ba%b8-x-yand1-2-asdf', |
---|
285 | 'à À à ö à Ì à a à Ê à Þ Ã
Ã¥' => 'a-a-o-o-u-u-sz-a-ae-ae-o-o-a-a', |
---|
286 | ' - ' => '', |
---|
287 | '' => '', |
---|
288 | ' ' => '', |
---|
289 | '?' => '', |
---|
290 | 'a file.JPG' => 'a-file-jpg', |
---|
291 | '/a/path/file.JPG' => 'a-path-file-jpg', |
---|
292 | '/a/directory/' => 'a-directory', |
---|
293 | '/' => '', |
---|
294 | 'this-is-already-a-slug' => 'this-is-already-a-slug', |
---|
295 | 'HTML entities ok—or not!' => 'html-entities-ok-or-not', |
---|
296 | '<p>tagsâŠ</p>' => 'ltpgttagshelliplt-pgt', |
---|
297 | 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.' => 'lorem-ipsum-dolor-sit-amet-consetetur-sadipscing-elitr-sed-diam-nonumy-eirmod-tempor-invidunt-ut-labore-et-dolore-magna-aliquyam-erat-sed-diam-voluptua-at-vero-eos-et-accusam-et-justo-duo-dolores-et-ea-rebum-stet-clita-kasd-gubergren-no-sea-takimata-sanctus-est-lorem-ipsum-dolor-sit-amet-lorem-ipsum-dolor-sit-amet-consetetur-sadipscing-elitr-sed-diam-nonumy-eirmod-tempor-invidunt-ut-labore-et-dolore-magna-aliquyam-erat-sed-diam-voluptua-at-vero-eos-et-accusam-et-justo-duo-dolores-et-ea-rebum-stet-clita-kasd-gubergren-no-sea-takimata-sanctus-est-lorem-ipsum-dolor-sit-amet-lorem-ipsum-dolor-sit-amet-consetetur-sadipscing-elitr-sed-diam-nonumy-eirmod-tempor-invidunt-ut-labore-et-dolore-magna-aliquyam-erat-sed-diam-voluptua-at-vero-eos-et-accusam-et-justo-duo-dolores-et-ea-rebum-stet-clita-kasd-gubergren-no-sea-takimata-sanctus-est-lorem-ipsum-dolor-sit-amet', |
---|
298 | // Binary data. |
---|
299 | 'xÃâ¡-]ïpıoË;ï)0ñ]à fhH¡Ãg+£KËH |
---|
300 | Ën*âÃâ¡ÃËÃŒQÃõ.âulÃâ ËyÃËïuâ«âºÂ¢>kÆâÊì+¯âË âE±ÃhâU÷%>âï¬ï¬â€1' => 'xodagger-emacrp%c4%b1-otildecmacr0nafhhiexcleg-poundk%cb%87h-nedaggeritildeuqoo-lozulineyatildeiuintcentgtkfnofsumaei-macrinfin-enotplusmnahudividegtasymp%ef%ac%81%ef%ac%81le1', |
---|
301 | '-- repeated - characters -- so---extra - - - look-out!' => 'repeated-characters-so-extra-look-out', |
---|
302 | 'NesthÀkchen\'s Teenage Years' => 'nesthakchens-teenage-years', |
---|
303 | 'áŒÏαÏΜεá¿Ï Akharneîs' => '%e1%bc%88chialpharhonuepsilon%e1%bf%96sigmaf-akharneis', |
---|
304 | 'ÐÐœÑПМ ÐÐ°Ð²Ð»ÐŸÐ²ÐžÑ Ð§ÐµÑ
Пв' => '%d0%90%d0%bd%d1%82%d0%be%d0%bd-%d0%9f%d0%b0%d0%b2%d0%bb%d0%be%d0%b2%d0%b8%d1%87-%d0%a7%d0%b5%d1%85%d0%be%d0%b2', |
---|
305 | 'Demain dÚs l\'aube' => 'demain-des-laube', |
---|
306 | 'Demain dÚs lâaube' => 'demain-des-laube', |
---|
307 | ]; |
---|
308 | foreach ($strings as $input => $expected) { |
---|
309 | // printf("%s\n", URLSlug($input)); |
---|
310 | $result = URLSlug($input); |
---|
311 | $this->assertEquals($expected, $result, sprintf('Failed with input: %s', $input)); |
---|
312 | } |
---|
313 | } |
---|
314 | } |
---|