http://example.com/path?foo[1]=a&foo[2]=b
75 | 76 | TEXT; 77 | 78 | $html = <<http://www.google.com.tw 81 | 82 | This is SSL URL: 83 | https://www.google.com.tw 84 | 85 | This is URL with path: 86 | http://www.google.com.tw/images 87 | 88 | This is URL with query: 89 | http://www.google.com.tw/search?q=foo&num=100 90 | 91 | This is URL with multi-level query: 92 | http://example.com/?foo[1]=a&foo[2]=b 93 | 94 | This is URL with fragment: 95 | http://example.com/path#top 96 | 97 | This is URL inline: http://example.com/path#top with test. 98 | 99 | This is an IDN URL: http://dømi.fo 100 | 101 | This is an IDN URL in Devanagari: http://सार्वभौमिक-स्वीकृति-परीक्षण.संगठन 102 | 103 | This is URL in HTML: 104 | LINK 105 | http://example.com/path?foo[1]=a&foo[2]=b 106 |http://example.com/path?foo[1]=a&foo[2]=b
109 | 110 | HTML; 111 | 112 | self::assertStringSafeEquals($html, $this->instance->convert($text)); 113 | } 114 | 115 | /** 116 | * testConvert 117 | * 118 | * @return void 119 | */ 120 | public function testLink() 121 | { 122 | $url = 'http://www.google.com'; 123 | 124 | self::assertEquals( 125 | 'http://www.google.com', 126 | $this->instance->link($url, ['foo' => 'bar']) 127 | ); 128 | 129 | $this->instance->stripScheme(true); 130 | 131 | self::assertEquals( 132 | 'www.google.com', 133 | $this->instance->link($url, ['foo' => 'bar']) 134 | ); 135 | 136 | $this->instance->autoTitle(true); 137 | 138 | self::assertEquals( 139 | 'www.google.com', 140 | $this->instance->link($url, ['foo' => 'bar']) 141 | ); 142 | } 143 | 144 | /** 145 | * testTextLimit 146 | * 147 | * @return void 148 | */ 149 | public function testTextLimit() 150 | { 151 | $url = 'http://campus.asukademy.com/learning/job/84-find-internship-opportunity-through-platform.html'; 152 | 153 | $this->instance->textLimit(50); 154 | 155 | self::assertEquals( 156 | 'http://campus.asukademy.com/learning/job/84-fin...', 157 | $this->instance->link($url) 158 | ); 159 | 160 | $this->instance->textLimit(function ($url) { 161 | return Autolink::shortenUrl($url); 162 | }); 163 | 164 | self::assertEquals( 165 | 'http://campus.asukademy.com/....../84-find-interns......', 166 | $this->instance->link($url) 167 | ); 168 | } 169 | 170 | /** 171 | * testAutoTitle 172 | * 173 | * @return void 174 | */ 175 | public function testAutoTitle() 176 | { 177 | $url = 'http://example.com/path?foo["1"]=a&foo[\'2\']=b'; 178 | 179 | $this->instance->autoTitle(true); 180 | 181 | self::assertEquals( 182 | 'http://example.com/path?foo["1"]=a&foo['2']=b', 183 | $this->instance->link($url, ['foo' => 'bar']) 184 | ); 185 | } 186 | 187 | /** 188 | * testStripScheme 189 | * 190 | * @return void 191 | */ 192 | public function testStripScheme() 193 | { 194 | $this->instance->stripScheme(true); 195 | 196 | $url = 'http://campus.asukademy.com/learning/job/84-find-internship-opportunity-through-platform.html'; 197 | 198 | self::assertEquals( 199 | 'campus.asukademy.com/learning/job/84-find-internship-opportunity-through-platform.html', 200 | $this->instance->link($url) 201 | ); 202 | } 203 | 204 | public function testAddScheme() 205 | { 206 | $url = 'ftp://example.com'; 207 | 208 | self::assertEquals('' . $url . '', $this->instance->convert($url)); 209 | 210 | $url = 'ftps://example.com'; 211 | 212 | self::assertEquals('' . $url . '', $this->instance->convert($url)); 213 | 214 | $url = 'https://example.com'; 215 | 216 | self::assertEquals('' . $url . '', $this->instance->convert($url)); 217 | 218 | $url = 'skype://example.com'; 219 | 220 | self::assertEquals($url, $this->instance->convert($url)); 221 | 222 | $this->instance->addScheme('skype'); 223 | 224 | self::assertEquals('' . $url . '', $this->instance->convert($url)); 225 | } 226 | 227 | public function testLinkNoScheme() 228 | { 229 | $this->instance->linkNoScheme('http'); 230 | 231 | $url = 'ftp://example.com'; 232 | 233 | self::assertEquals('' . $url . '', $this->instance->convert($url)); 234 | 235 | $url = 'example.com'; 236 | 237 | self::assertEquals('' . $url . '', $this->instance->convert($url)); 238 | 239 | $url = 'https://example.com'; 240 | 241 | self::assertEquals('' . $url . '', $this->instance->convert($url)); 242 | 243 | $url = 'skype://example.com'; 244 | 245 | self::assertEquals($url, $this->instance->convert($url)); 246 | 247 | $this->instance->addScheme('skype'); 248 | 249 | self::assertEquals('' . $url . '', $this->instance->convert($url)); 250 | 251 | $url = 'dømi.fo'; 252 | 253 | self::assertEquals('' . $url . '', $this->instance->convert($url)); 254 | 255 | $url = 'dømi.fo/dømi'; 256 | 257 | self::assertEquals('' . $url . '', $this->instance->convert($url)); 258 | } 259 | 260 | public function testLinkNoSchemeShouldIgnoreEmail(): void 261 | { 262 | $this->instance->linkNoScheme('http'); 263 | 264 | $url = 'ABC hello@email.com CBA'; 265 | 266 | self::assertEquals('ABC hello@email.com CBA', $this->instance->convert($url)); 267 | } 268 | 269 | /** 270 | * testGetAndSetScheme 271 | * 272 | * @return void 273 | */ 274 | public function testGetAndSetScheme() 275 | { 276 | $autolink = new Autolink([], ['a', 'b', 'http']); 277 | 278 | self::assertEquals(['http', 'https', 'ftp', 'ftps', 'a', 'b'], $autolink->getSchemes()); 279 | 280 | self::assertEquals('http|https|ftp|ftps|a|b', $autolink->getSchemes(true)); 281 | 282 | $autolink->setSchemes('skype'); 283 | 284 | self::assertEquals(['skype'], $autolink->getSchemes()); 285 | 286 | $autolink->setSchemes('mailto'); 287 | 288 | self::assertEquals(['mailto'], $autolink->getSchemes()); 289 | 290 | $autolink->setSchemes('mailto', 'mailto'); 291 | 292 | self::assertEquals(['mailto'], $autolink->getSchemes()); 293 | 294 | $autolink->removeScheme('mailto'); 295 | 296 | self::assertEquals([], $autolink->getSchemes()); 297 | } 298 | 299 | public function testAutoEscape() 300 | { 301 | $autolink = new Autolink(); 302 | 303 | $url = 'https://example.com/?foo=bar&yoo=baz'; 304 | 305 | self::assertEquals( 306 | '' . htmlspecialchars($url) . '', 307 | $autolink->convert($url) 308 | ); 309 | 310 | $autolink->autoEscape(false); 311 | 312 | self::assertEquals('' . htmlspecialchars($url) . '', $autolink->convert($url)); 313 | 314 | $url = 'hello+admin&test@example.org'; 315 | 316 | $autolink->autoEscape(true); 317 | 318 | self::assertEquals( 319 | '' . htmlspecialchars($url) . '', 320 | $autolink->convertEmail($url) 321 | ); 322 | 323 | $autolink->autoEscape(false); 324 | 325 | self::assertEquals( 326 | '' . htmlspecialchars($url) . '', 327 | $autolink->convertEmail($url) 328 | ); 329 | } 330 | 331 | public function testConvertEmail() 332 | { 333 | $text = <<