Debian Codegraph @

tests/phpunit/unit/includes/utils/UrlUtilsProviders.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Utils\UrlUtils;
4
5 /**
6 * Shared data providers for UrlUtilsTest and some GlobalFunctions tests
7 */
8 class UrlUtilsProviders {
9 private const DEFAULT_PROTOS = [
10 'http' => PROTO_HTTP,
11 'https' => PROTO_HTTPS,
12 'protocol-relative' => PROTO_RELATIVE,
13 'current' => PROTO_CURRENT,
14 'fallback' => PROTO_FALLBACK,
15 'canonical' => PROTO_CANONICAL,
16 'internal' => PROTO_INTERNAL,
17 ];
18
19 public static function provideExpandException(): Generator {
20 foreach ( self::DEFAULT_PROTOS as $protoDesc => $defaultProto ) {
21 $options = [
22 UrlUtils::SERVER => null,
23 UrlUtils::CANONICAL_SERVER => 'http://example.com',
24 UrlUtils::INTERNAL_SERVER => 'http://example.com',
25 ];
26
27 if ( $defaultProto === PROTO_CANONICAL ) {
28 $options[UrlUtils::CANONICAL_SERVER] = null;
29 } elseif ( $defaultProto === PROTO_INTERNAL ) {
30 $options[UrlUtils::INTERNAL_SERVER] = null;
31 }
32
33 yield "defaultProto $protoDesc with appropriate options unset" => [
34 $options, $defaultProto, BadMethodCallException::class,
35 'Cannot call expand() if the appropriate SERVER/CANONICAL_SERVER/INTERNAL_SERVER' .
36 ' option was not passed to the constructor',
37 ];
38 }
39
40 yield 'protocol-relative server with port and custom HTTPS port' => [
41 [ UrlUtils::SERVER => '//example.com:123', UrlUtils::HTTPS_PORT => 456 ],
42 PROTO_HTTPS,
43 Exception::class,
44 'A protocol-relative server may not contain a port number',
45 ];
46 }
47
48 public static function provideExpand() {
49 $modes = [ 'http', 'https' ];
50 $servers = [
51 'http://example.com',
52 'https://example.com',
53 '//example.com',
54 ];
55 $defaultProtos = [
56 'http' => PROTO_HTTP,
57 'https' => PROTO_HTTPS,
58 'protocol-relative' => PROTO_RELATIVE,
59 'fallback' => PROTO_FALLBACK,
60 'canonical' => PROTO_CANONICAL,
61 ];
62
63 foreach ( ArrayUtils::cartesianProduct( $modes, $servers, $modes, array_keys( $defaultProtos ) )
64 as [ $fallbackProto, $server, $canServerMode, $protoDesc ]
65 ) {
66 $defaultProto = $defaultProtos[$protoDesc];
67 $canServer = "$canServerMode://example2.com";
68 $options = [
69 UrlUtils::SERVER => $server,
70 UrlUtils::CANONICAL_SERVER => $canServer,
71 UrlUtils::HTTPS_PORT => 443,
72 UrlUtils::FALLBACK_PROTOCOL => $fallbackProto,
73 ];
74 $case = "fallback: $fallbackProto, default: $protoDesc, server: $server, canonical: $canServer";
75 yield "No-op fully-qualified http URL ($case)" => [
76 'http://example.com',
77 $options, $defaultProto,
78 'http://example.com',
79 ];
80 yield "No-op fully-qualified https URL ($case)" => [
81 'https://example.com',
82 $options, $defaultProto,
83 'https://example.com',
84 ];
85 yield "No-op rootless path-only URL ($case" => [
86 "wiki/FooBar",
87 $options, $defaultProto,
88 'wiki/FooBar',
89 ];
90
91 // Determine expected protocol
92 if ( $protoDesc === 'protocol-relative' ) {
93 $p = '';
94 } elseif ( $protoDesc === 'fallback' ) {
95 $p = "$fallbackProto:";
96 } elseif ( $protoDesc === 'canonical' ) {
97 $p = "$canServerMode:";
98 } else {
99 $p = $protoDesc . ':';
100 }
101 yield "Expand protocol-relative URL ($case)" => [
102 '//wikipedia.org',
103 $options, $defaultProto,
104 "$p//wikipedia.org",
105 ];
106
107 // Determine expected server name
108 if ( $protoDesc === 'canonical' ) {
109 $srv = $canServer;
110 } elseif ( $server === '//example.com' ) {
111 $srv = $p . $server;
112 } else {
113 $srv = $server;
114 }
115 yield "Expand path that starts with slash ($case)" => [
116 '/wiki/FooBar',
117 $options, $defaultProto,
118 "$srv/wiki/FooBar",
119 ];
120 }
121
122 // Non-standard https port
123 $optionsRel111 = [
124 UrlUtils::SERVER => '//wiki.example.com',
125 UrlUtils::CANONICAL_SERVER => 'http://wiki.example.com',
126 UrlUtils::HTTPS_PORT => 111,
127 UrlUtils::FALLBACK_PROTOCOL => 'https',
128 ];
129 yield "No-op foreign URL, ignore custom port config" => [
130 'https://foreign.example.com/foo',
131 $optionsRel111, PROTO_HTTPS,
132 'https://foreign.example.com/foo',
133 ];
134 yield "No-op foreign URL, preserve existing port" => [
135 'https://foreign.example.com:222/foo',
136 $optionsRel111, PROTO_HTTPS,
137 'https://foreign.example.com:222/foo',
138 ];
139 yield "Expand path with custom HTTPS port" => [
140 '/foo',
141 $optionsRel111, PROTO_HTTPS,
142 'https://wiki.example.com:111/foo',
143 ];
144
145 // Silly corner cases
146 yield 'CanonicalServer with no protocol' => [
147 '/',
148 [
149 UrlUtils::CANONICAL_SERVER => '//dont.do.this',
150 UrlUtils::FALLBACK_PROTOCOL => 'https',
151 ],
152 PROTO_CANONICAL,
153 'http://dont.do.this/',
154 ];
155 yield 'InternalServer with no protocol' => [
156 '/',
157 [
158 UrlUtils::INTERNAL_SERVER => '//dont.do.this',
159 UrlUtils::FALLBACK_PROTOCOL => 'https',
160 ],
161 PROTO_INTERNAL,
162 'http://dont.do.this/',
163 ];
164
165 // XXX Find something that this will actually return null for
166
167 // XXX Add dot-removing tests
168 }
169
170 public static function provideGetServer(): Generator {
171 foreach ( self::provideExpand() as $desc => [ $input, $options, $defaultProto, $expected ] ) {
172 if ( $input !== '/wiki/FooBar' ) {
173 continue;
174 }
175 $desc = str_replace( 'Expand path that starts with slash (', '', $desc );
176 $desc = str_replace( ')', '', $desc );
177 $expected = str_replace( '/wiki/FooBar', '', $expected );
178 yield $desc => [ $options, $defaultProto, $expected ];
179 }
180 }
181
182 public static function provideAssemble(): Generator {
183 $schemes = [
184 '' => [],
185 '//' => [
186 'delimiter' => '//',
187 ],
188 'http://' => [
189 'scheme' => 'http',
190 'delimiter' => '://',
191 ],
192 ];
193
194 $hosts = [
195 '' => [],
196 'example.com' => [
197 'host' => 'example.com',
198 ],
199 'example.com:123' => [
200 'host' => 'example.com',
201 'port' => 123,
202 ],
203 '[email protected]' => [
204 'user' => 'id',
205 'host' => 'example.com',
206 ],
207 '[email protected]:123' => [
208 'user' => 'id',
209 'host' => 'example.com',
210 'port' => 123,
211 ],
212 'id:[email protected]' => [
213 'user' => 'id',
214 'pass' => 'key',
215 'host' => 'example.com',
216 ],
217 'id:[email protected]:123' => [
218 'user' => 'id',
219 'pass' => 'key',
220 'host' => 'example.com',
221 'port' => 123,
222 ],
223 ];
224
225 foreach ( $schemes as $scheme => $schemeParts ) {
226 foreach ( $hosts as $host => $hostParts ) {
227 foreach ( [ '', '/', '/0', '/path' ] as $path ) {
228 foreach ( [ '', '0', 'query' ] as $query ) {
229 foreach ( [ '', '0', 'fragment' ] as $fragment ) {
230 $parts = array_merge(
231 $schemeParts,
232 $hostParts
233 );
234 $url = $scheme .
235 $host .
236 $path;
237
238 if ( $path !== '' ) {
239 $parts['path'] = $path;
240 }
241 if ( $query !== '' ) {
242 $parts['query'] = $query;
243 $url .= '?' . $query;
244 }
245 if ( $fragment !== '' ) {
246 $parts['fragment'] = $fragment;
247 $url .= '#' . $fragment;
248 }
249
250 yield [ $parts, $url ];
251 }
252 }
253 }
254 }
255 }
256
257 yield [
258 [
259 'scheme' => 'http',
260 'delimiter' => '://',
261 'user' => 'id',
262 'pass' => 'key',
263 'host' => 'example.org',
264 'port' => 321,
265 'path' => '/over/there',
266 'query' => 'name=ferret&foo=bar',
267 'fragment' => 'nose',
268 ],
269 'http://id:[email protected]:321/over/there?name=ferret&foo=bar#nose',
270 ];
271
272 // Account for parse_url() on PHP >= 8 returning an empty query field for URLs ending with
273 // '?' such as "http://url.with.empty.query/foo?" (T268852)
274 yield [
275 [
276 'scheme' => 'http',
277 'delimiter' => '://',
278 'host' => 'url.with.empty.query',
279 'path' => '/foo',
280 'query' => '',
281 ],
282 'http://url.with.empty.query/foo',
283 ];
284 }
285
286 public static function provideRemoveDotSegments(): Generator {
287 yield [ '/a/b/c/./../../g', '/a/g' ];
288 yield [ 'mid/content=5/../6', 'mid/6' ];
289 yield [ '/a//../b', '/a/b' ];
290 yield [ '/.../a', '/.../a' ];
291 yield [ '.../a', '.../a' ];
292 yield [ '', '' ];
293 yield [ '/', '/' ];
294 yield [ '//', '//' ];
295 yield [ '.', '' ];
296 yield [ '..', '' ];
297 yield [ '...', '...' ];
298 yield [ '/.', '/' ];
299 yield [ '/..', '/' ];
300 yield [ './', '' ];
301 yield [ '../', '' ];
302 yield [ './a', 'a' ];
303 yield [ '../a', 'a' ];
304 yield [ '../../a', 'a' ];
305 yield [ '.././a', 'a' ];
306 yield [ './../a', 'a' ];
307 yield [ '././a', 'a' ];
308 yield [ '../../', '' ];
309 yield [ '.././', '' ];
310 yield [ './../', '' ];
311 yield [ '././', '' ];
312 yield [ '../..', '' ];
313 yield [ '../.', '' ];
314 yield [ './..', '' ];
315 yield [ './.', '' ];
316 yield [ '/../../a', '/a' ];
317 yield [ '/.././a', '/a' ];
318 yield [ '/./../a', '/a' ];
319 yield [ '/././a', '/a' ];
320 yield [ '/../../', '/' ];
321 yield [ '/.././', '/' ];
322 yield [ '/./../', '/' ];
323 yield [ '/././', '/' ];
324 yield [ '/../..', '/' ];
325 yield [ '/../.', '/' ];
326 yield [ '/./..', '/' ];
327 yield [ '/./.', '/' ];
328 yield [ 'b/../../a', '/a' ];
329 yield [ 'b/.././a', '/a' ];
330 yield [ 'b/./../a', '/a' ];
331 yield [ 'b/././a', 'b/a' ];
332 yield [ 'b/../../', '/' ];
333 yield [ 'b/.././', '/' ];
334 yield [ 'b/./../', '/' ];
335 yield [ 'b/././', 'b/' ];
336 yield [ 'b/../..', '/' ];
337 yield [ 'b/../.', '/' ];
338 yield [ 'b/./..', '/' ];
339 yield [ 'b/./.', 'b/' ];
340 yield [ '/b/../../a', '/a' ];
341 yield [ '/b/.././a', '/a' ];
342 yield [ '/b/./../a', '/a' ];
343 yield [ '/b/././a', '/b/a' ];
344 yield [ '/b/../../', '/' ];
345 yield [ '/b/.././', '/' ];
346 yield [ '/b/./../', '/' ];
347 yield [ '/b/././', '/b/' ];
348 yield [ '/b/../..', '/' ];
349 yield [ '/b/../.', '/' ];
350 yield [ '/b/./..', '/' ];
351 yield [ '/b/./.', '/b/' ];
352 }
353
354 public static function provideValidProtocols(): Generator {
355 foreach ( [ 'validProtocols', 'validAbsoluteProtocols' ] as $method ) {
356 yield "$method with string for UrlProtocols" =>
357 [ $method, 'some|string', 'some|string' ];
358 yield "$method simple case" => [ $method, [ 'foo', 'bar' ], 'foo|bar' ];
359 yield "$method reserved characters" => [ $method,
360 [ '^si|lly', 'in/valid', 'p[ro*t?o.c+o(ls$' ],
361 '\^si\|lly|in\/valid|p\[ro\*t\?o\.c\+o\(ls\$' ];
362 }
363 yield 'validProtocols with relative' =>
364 [ 'validProtocols', [ 'a', '//', 'b' ], 'a|\/\/|b' ];
365 yield 'validAbsoluteProtocols with relative' =>
366 [ 'validAbsoluteProtocols', [ 'a', '//', 'b' ], 'a|b' ];
367 }
368
369 public static function provideParse(): Generator {
370 yield [
371 '//example.org',
372 [
373 'scheme' => '',
374 'delimiter' => '//',
375 'host' => 'example.org',
376 ]
377 ];
378 yield [
379 'http://example.org',
380 [
381 'scheme' => 'http',
382 'delimiter' => '://',
383 'host' => 'example.org',
384 ]
385 ];
386 yield [
387 'https://example.org',
388 [
389 'scheme' => 'https',
390 'delimiter' => '://',
391 'host' => 'example.org',
392 ]
393 ];
394 yield [
395 'http://id:[email protected]:123/path?foo=bar#baz',
396 [
397 'scheme' => 'http',
398 'delimiter' => '://',
399 'user' => 'id',
400 'pass' => 'key',
401 'host' => 'example.org',
402 'port' => 123,
403 'path' => '/path',
404 'query' => 'foo=bar',
405 'fragment' => 'baz',
406 ]
407 ];
408 yield [
409 'file:///',
410 [
411 'scheme' => 'file',
412 'delimiter' => '://',
413 'host' => '',
414 'path' => '/',
415 ]
416 ];
417 yield [
418 'file://example.org/etc/php.ini',
419 [
420 'scheme' => 'file',
421 'delimiter' => '://',
422 'host' => 'example.org',
423 'path' => '/etc/php.ini',
424 ]
425 ];
426 yield [
427 'file:///etc/php.ini',
428 [
429 'scheme' => 'file',
430 'delimiter' => '://',
431 'host' => '',
432 'path' => '/etc/php.ini',
433 ]
434 ];
435 yield [
436 'file:///c:/',
437 [
438 'scheme' => 'file',
439 'delimiter' => '://',
440 'host' => '',
441 'path' => '/c:/',
442 ]
443 ];
444 yield [
445 'file://example.org',
446 [
447 'scheme' => 'file',
448 'delimiter' => '://',
449 'host' => 'example.org',
450 ]
451 ];
452 yield [
453 'mailto:[email protected]',
454 [
455 'scheme' => 'mailto',
456 'delimiter' => ':',
457 'host' => '',
458 'path' => '[email protected]',
459 ]
460 ];
461 yield [
462 'mailto:[email protected]?subject=Foo',
463 [
464 'scheme' => 'mailto',
465 'delimiter' => ':',
466 'host' => '',
467 'path' => '[email protected]',
468 'query' => 'subject=Foo',
469 ]
470 ];
471 yield [
472 'mailto:?subject=Foo',
473 [
474 'scheme' => 'mailto',
475 'delimiter' => ':',
476 'host' => '',
477 'query' => 'subject=Foo',
478 ]
479 ];
480 yield [
481 'invalid://test/',
482 null
483 ];
484 // T212067
485 yield [
486 '//evil.com?example.org/foo/bar',
487 [
488 'scheme' => '',
489 'delimiter' => '//',
490 'host' => 'evil.com',
491 'query' => 'example.org/foo/bar',
492 ]
493 ];
494 yield [
495 '//evil.com?example.org/foo/bar?baz#quux',
496 [
497 'scheme' => '',
498 'delimiter' => '//',
499 'host' => 'evil.com',
500 'query' => 'example.org/foo/bar?baz',
501 'fragment' => 'quux',
502 ]
503 ];
504 yield [
505 '//evil.com?example.org?baz#quux',
506 [
507 'scheme' => '',
508 'delimiter' => '//',
509 'host' => 'evil.com',
510 'query' => 'example.org?baz',
511 'fragment' => 'quux',
512 ]
513 ];
514 yield [
515 '//evil.com?example.org#quux',
516 [
517 'scheme' => '',
518 'delimiter' => '//',
519 'host' => 'evil.com',
520 'query' => 'example.org',
521 'fragment' => 'quux',
522 ]
523 ];
524 yield [
525 '%0Ahttp://example.com',
526 null,
527 ];
528 yield [
529 'http:///test.com',
530 null,
531 ];
532 yield [
533 'news:[email protected]',
534 [
535 'scheme' => 'news',
536 'delimiter' => ':',
537 'host' => '',
538 'path' => '[email protected]'
539 ]
540 ];
541 // T294559
542 yield [
543 '//xy.wikimedia.org/wiki/Foo:1234',
544 [
545 'scheme' => '',
546 'delimiter' => '//',
547 'host' => 'xy.wikimedia.org',
548 'path' => '/wiki/Foo:1234'
549 ]
550 ];
551 yield [
552 '//xy.wikimedia.org:8888/wiki/Foo:1234',
553 [
554 'scheme' => '',
555 'delimiter' => '//',
556 'host' => 'xy.wikimedia.org',
557 'path' => '/wiki/Foo:1234',
558 'port' => 8888,
559 ]
560 ];
561 }
562
563 public static function provideMatchesDomainList(): Generator {
564 $protocols = [ 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' ];
565 foreach ( $protocols as $pDesc => $p ) {
566 yield "No matches for empty domains array, $pDesc URL" => [
567 "$p//www.example.com",
568 [],
569 false,
570 ];
571 yield "Exact match in domains array, $pDesc URL" => [
572 "$p//www.example.com",
573 [ 'www.example.com' ],
574 true,
575 ];
576 yield "Match without subdomain in domains array, $pDesc URL" => [
577 "$p//www.example.com",
578 [ 'example.com' ],
579 true,
580 ];
581 yield "Exact match with other domains in array, $pDesc URL" => [
582 "$p//www.example2.com",
583 [ 'www.example.com', 'www.example2.com', 'www.example3.com' ],
584 true,
585 ];
586 yield "Match without subdomain with other domains in array, $pDesc URL" => [
587 "$p//www.example2.com",
588 [ 'example.com', 'example2.com', 'example3,com' ],
589 true,
590 ];
591 yield "Domain not in array, $pDesc URL" => [
592 "$p//www.example4.com",
593 [ 'example.com', 'example2.com', 'example3,com' ],
594 false,
595 ];
596 yield "Non-matching substring of domain, $pDesc URL" => [
597 "$p//nds-nl.wikipedia.org",
598 [ 'nl.wikipedia.org' ],
599 false,
600 ];
601 }
602 }
603
604 }