Debian Codegraph @

tests/phpunit/includes/title/TitleUrlTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\MainConfigNames;
4 use MediaWiki\Parser\Sanitizer;
5 use MediaWiki\Tests\Unit\DummyServicesTrait;
6 use MediaWiki\Title\Title;
7
8 /**
9 * @group Database
10 *
11 * @covers \MediaWiki\Title\Title::getLocalURL
12 * @covers \MediaWiki\Title\Title::getLinkURL
13 * @covers \MediaWiki\Title\Title::getFullURL
14 * @covers \MediaWiki\Title\Title::getFullUrlForRedirect
15 * @covers \MediaWiki\Title\Title::getCanonicalURL
16 * @covers \MediaWiki\Title\Title::getInternalURL
17 */
18 class TitleUrlTest extends MediaWikiLangTestCase {
19 use DummyServicesTrait;
20
21 protected function setUp(): void {
22 parent::setUp();
23 $this->overrideConfigValues( [
24 MainConfigNames::Server => '//m.xx.wiki.test',
25 MainConfigNames::CanonicalServer => 'https://xx.wiki.test',
26 MainConfigNames::InternalServer => 'http://app23.internal',
27 MainConfigNames::ArticlePath => '/wiki/$1',
28 MainConfigNames::ExternalInterwikiFragmentMode => 'html5',
29 MainConfigNames::FragmentMode => [ 'legacy', 'html5' ],
30 MainConfigNames::MainPageIsDomainRoot => true,
31 MainConfigNames::ActionPaths => [ 'edit' => '/m/edit/$1' ],
32 MainConfigNames::VariantArticlePath => '/$2/$1',
33 MainConfigNames::ScriptPath => '/m',
34 MainConfigNames::Script => '/m/index.php',
35 MainConfigNames::UsePigLatinVariant => true,
36 ] );
37
38 // Some tests use interwikis - define valid prefixes and their configuration
39 $interwikiLookup = $this->getDummyInterwikiLookup( [
40 [ 'iw_prefix' => 'acme', 'iw_url' => 'https://acme.test/$1' ],
41 [ 'iw_prefix' => 'yy', 'iw_url' => '//yy.wiki.test/wiki/$1', 'iw_local' => true ]
42 ] );
43 $this->setService( 'InterwikiLookup', $interwikiLookup );
44
45 $this->clearHooks( [
46 'GetFullURL',
47 'GetLocalURL__Article',
48 'GetLocalURL__Internal',
49 'GetLocalURL',
50 'GetInternalURL',
51 'GetCanonicalURL',
52 ] );
53 }
54
55 public function testUrlsForSimpleTitle() {
56 $title = Title::makeTitle( NS_USER, 'Göatee' );
57 $name = $title->getPrefixedURL();
58
59 $query = [ 'x' => 'one two', 'y' => '#' ];
60 $queryString = 'x=one+two&y=%23';
61
62 // Test getLocalURL()
63 $this->assertSame(
64 '/wiki/' . $name,
65 $title->getLocalURL(),
66 'getLocalURL()'
67 );
68 $this->assertSame(
69 '/m/index.php?title=' . $name . '&' . $queryString,
70 $title->getLocalURL( $query ),
71 'getLocalURL( array )'
72 );
73 $this->assertSame(
74 '/m/index.php?title=' . $name . '&' . $queryString,
75 $title->getLocalURL( $queryString ),
76 'getLocalURL( string )'
77 );
78
79 // Test getFullURL()
80 $this->assertSame(
81 '//m.xx.wiki.test/wiki/' . $name,
82 $title->getFullURL(),
83 'getFullURL()'
84 );
85 $this->assertSame(
86 '//m.xx.wiki.test/m/index.php?title=' . $name . '&' . $queryString,
87 $title->getFullURL( $query ),
88 'getFullURL( array )'
89 );
90 $this->assertSame(
91 'https://xx.wiki.test/wiki/' . $name,
92 $title->getFullURL( [], false, PROTO_CANONICAL ),
93 'getFullURL() with PROTO_CANONICAL'
94 );
95 $this->assertSame(
96 'http://m.xx.wiki.test/wiki/' . $name,
97 $title->getFullURL( [], false, PROTO_HTTP ),
98 'getFullURL() with PROTO_HTTP'
99 );
100 $this->assertSame(
101 'http://app23.internal/wiki/' . $name,
102 $title->getFullURL( [], false, PROTO_INTERNAL ),
103 'getFullURL() with PROTO_INTERNAL'
104 );
105
106 // Test getFullUrlForRedirect()
107 // Note that it uses PROTO_CURRENT by default, which is 'http' for tests
108 $this->assertSame(
109 'http://m.xx.wiki.test/wiki/' . $name,
110 $title->getFullUrlForRedirect(),
111 'getFullUrlForRedirect()'
112 );
113 $this->assertSame(
114 'http://m.xx.wiki.test/m/index.php?title=' . $name . '&' . $queryString,
115 $title->getFullUrlForRedirect( $query ),
116 'getFullUrlForRedirect( array )'
117 );
118 $this->assertSame(
119 '//m.xx.wiki.test/wiki/' . $name,
120 $title->getFullUrlForRedirect( [], PROTO_RELATIVE ),
121 'getFullUrlForRedirect() with PROTO_RELATIVE'
122 );
123 $this->assertSame(
124 'https://xx.wiki.test/wiki/' . $name,
125 $title->getFullUrlForRedirect( [], PROTO_CANONICAL ),
126 'getFullUrlForRedirect() with PROTO_CANONICAL'
127 );
128 $this->assertSame(
129 'http://app23.internal/wiki/' . $name,
130 $title->getFullUrlForRedirect( [], PROTO_INTERNAL ),
131 'getFullUrlForRedirect() with PROTO_INTERNAL'
132 );
133
134 // Test getLinkURL()
135 $this->assertSame(
136 '/wiki/' . $name,
137 $title->getLinkURL(),
138 'getLinkURL()'
139 );
140 $this->assertSame(
141 '/m/index.php?title=' . $name . '&' . $queryString,
142 $title->getLinkURL( $query ),
143 'getLinkURL( array )'
144 );
145 $this->assertSame(
146 '/m/index.php?title=' . $name . '&' . $queryString,
147 $title->getLinkURL( $queryString ),
148 'getLinkURL( string )'
149 );
150 $this->assertSame(
151 '//m.xx.wiki.test/wiki/' . $name,
152 $title->getLinkURL( [], false, PROTO_RELATIVE ),
153 'getLinkURL() with PROTO_RELATIVE'
154 );
155 $this->assertSame(
156 'http://m.xx.wiki.test/wiki/' . $name,
157 $title->getLinkURL( [], false, PROTO_CURRENT ),
158 'getLinkURL() with PROTO_CURRENT'
159 );
160 $this->assertSame(
161 'https://m.xx.wiki.test/wiki/' . $name,
162 $title->getLinkURL( [], false, PROTO_HTTPS ),
163 'getLinkURL() with PROTO_HTTPS'
164 );
165 $this->assertSame(
166 'https://xx.wiki.test/wiki/' . $name,
167 $title->getLinkURL( [], false, PROTO_CANONICAL ),
168 'getLinkURL() with PROTO_CANONICAL'
169 );
170 $this->assertSame(
171 'http://app23.internal/wiki/' . $name,
172 $title->getLinkURL( [], false, PROTO_INTERNAL ),
173 'getLinkURL() with PROTO_INTERNAL'
174 );
175 $this->assertSame(
176 'http://app23.internal/m/index.php?title=' . $name . '&' . $queryString,
177 $title->getLinkURL( $query, false, PROTO_INTERNAL ),
178 'getLinkURL( array ) with PROTO_INTERNAL'
179 );
180
181 // Test getInternalURL()
182 $this->assertSame(
183 'http://app23.internal/wiki/' . $name,
184 $title->getInternalURL(),
185 'getInternalURL()'
186 );
187 $this->assertSame(
188 'http://app23.internal/m/index.php?title=' . $name . '&' . $queryString,
189 $title->getInternalURL( $query ),
190 'getInternalURL( array )'
191 );
192 $this->assertSame(
193 'http://app23.internal/m/index.php?title=' . $name . '&' . $queryString,
194 $title->getInternalURL( $queryString ),
195 'getInternalURL( string )'
196 );
197
198 // Test getCanonicalURL()
199 $this->assertSame(
200 'https://xx.wiki.test/wiki/' . $name,
201 $title->getCanonicalURL(),
202 'getCanonicalURL()'
203 );
204 $this->assertSame(
205 'https://xx.wiki.test/m/index.php?title=' . $name . '&' . $queryString,
206 $title->getCanonicalURL( $query ),
207 'getCanonicalURL( array )'
208 );
209 $this->assertSame(
210 'https://xx.wiki.test/m/index.php?title=' . $name . '&' . $queryString,
211 $title->getCanonicalURL( $queryString ),
212 'getCanonicalURL( string )'
213 );
214 }
215
216 public function testUrlsWithActionPath() {
217 $title = Title::makeTitle( NS_USER, 'Göatee' );
218 $name = $title->getPrefixedURL();
219
220 $query1 = [ 'action' => 'edit' ];
221 $query2 = [ 'x' => 'one two', 'y' => '#' ];
222 $queryString = 'x=one+two&y=%23';
223
224 // Test getLocalURL()
225 $this->assertSame(
226 '/m/edit/' . $name,
227 $title->getLocalURL( $query1 ),
228 'getLocalURL( array )'
229 );
230 $this->assertSame(
231 '/m/edit/' . $name . '?' . $queryString,
232 $title->getLocalURL( $query1 + $query2 ),
233 'getLocalURL( array + array )'
234 );
235
236 // Test getFullURL()
237 $this->assertSame(
238 '//m.xx.wiki.test/m/edit/' . $name . '?' . $queryString,
239 $title->getFullURL( $query1 + $query2 ),
240 'getFullURL( array )'
241 );
242
243 // Test getFullUrlForRedirect()
244 // Note that it uses PROTO_CURRENT by default, which is 'http' for tests
245 $this->assertSame(
246 'http://m.xx.wiki.test/m/edit/' . $name . '?' . $queryString,
247 $title->getFullUrlForRedirect( $query1 + $query2 ),
248 'getFullUrlForRedirect( array )'
249 );
250
251 // Test getLinkURL()
252 $this->assertSame(
253 '/m/edit/' . $name . '?' . $queryString,
254 $title->getLinkURL( $query1 + $query2 ),
255 'getLinkURL( array + array )'
256 );
257
258 // Test getInternalURL()
259 $this->assertSame(
260 'http://app23.internal/m/edit/' . $name . '?' . $queryString,
261 $title->getInternalURL( $query1 + $query2 ),
262 'getInternalURL( array )'
263 );
264
265 // Test getCanonicalURL()
266 $this->assertSame(
267 'https://xx.wiki.test/m/edit/' . $name . '?' . $queryString,
268 $title->getCanonicalURL( $query1 + $query2 ),
269 'getCanonicalURL( array )'
270 );
271 }
272
273 public function testUrlsWithVariantPath() {
274 $title = Title::makeTitle( NS_USER, 'Göatee' );
275 $name = $title->getPrefixedURL();
276
277 $query1 = [ 'variant' => 'en-x-piglatin' ];
278 $query2 = [ 'x' => 'one two', 'y' => '#' ];
279 $queryString = 'x=one+two&y=%23';
280
281 // Test getLocalURL()
282 $this->assertSame(
283 '/en-x-piglatin/' . $name,
284 $title->getLocalURL( $query1 ),
285 'getLocalURL( array )'
286 );
287 $this->assertSame( // NOTE: this could as well apply the variant path
288 '/m/index.php?title=' . $name . '&variant=en-x-piglatin&' . $queryString,
289 $title->getLocalURL( $query1 + $query2 ),
290 'getLocalURL( array + array )'
291 );
292
293 // Test getFullURL()
294 $this->assertSame(
295 '//m.xx.wiki.test/en-x-piglatin/' . $name,
296 $title->getFullURL( $query1 ),
297 'getFullURL( array )'
298 );
299
300 // Test getFullUrlForRedirect()
301 // Note that it uses PROTO_CURRENT by default, which is 'http' for tests
302 $this->assertSame(
303 'http://m.xx.wiki.test/en-x-piglatin/' . $name,
304 $title->getFullUrlForRedirect( $query1 ),
305 'getFullUrlForRedirect( array )'
306 );
307
308 // Test getLinkURL()
309 $this->assertSame(
310 '/en-x-piglatin/' . $name,
311 $title->getLinkURL( $query1 ),
312 'getLinkURL( array + array )'
313 );
314
315 // Test getInternalURL()
316 $this->assertSame(
317 'http://app23.internal/en-x-piglatin/' . $name,
318 $title->getInternalURL( $query1 ),
319 'getInternalURL( array )'
320 );
321
322 // Test getCanonicalURL()
323 $this->assertSame(
324 'https://xx.wiki.test/en-x-piglatin/' . $name,
325 $title->getCanonicalURL( $query1 ),
326 'getCanonicalURL( array )'
327 );
328 }
329
330 public function testUrlsForMainPage() {
331 $title = Title::newMainPage();
332 $name = $title->getPrefixedURL();
333
334 $query = [ 'x' => 'one two', 'y' => '#' ];
335 $queryString = 'x=one+two&y=%23';
336
337 // Test getLocalURL()
338 $this->assertSame(
339 '/',
340 $title->getLocalURL(),
341 'getLocalURL()'
342 );
343 $this->assertSame(
344 '/m/index.php?title=' . $name . '&' . $queryString,
345 $title->getLocalURL( $query ),
346 'getLocalURL( array )'
347 );
348
349 // Test getFullURL()
350 $this->assertSame(
351 '//m.xx.wiki.test/',
352 $title->getFullURL(),
353 'getFullURL()'
354 );
355 $this->assertSame(
356 '//m.xx.wiki.test/m/index.php?title=' . $name . '&' . $queryString,
357 $title->getFullURL( $query ),
358 'getFullURL( array )'
359 );
360
361 // Test getFullUrlForRedirect()
362 // Note that it uses PROTO_CURRENT by default, which is 'http' for tests
363 $this->assertSame(
364 'http://m.xx.wiki.test/',
365 $title->getFullUrlForRedirect(),
366 'getFullUrlForRedirect()'
367 );
368 $this->assertSame(
369 'http://m.xx.wiki.test/m/index.php?title=' . $name . '&' . $queryString,
370 $title->getFullUrlForRedirect( $query ),
371 'getFullUrlForRedirect( array )'
372 );
373
374 // Test getLinkURL()
375 $this->assertSame(
376 '/',
377 $title->getLinkURL(),
378 'getLinkURL()'
379 );
380 $this->assertSame(
381 '/m/index.php?title=' . $name . '&' . $queryString,
382 $title->getLinkURL( $query ),
383 'getLinkURL( array )'
384 );
385
386 // Test getInternalURL()
387 $this->assertSame(
388 'http://app23.internal/',
389 $title->getInternalURL(),
390 'getInternalURL()'
391 );
392 $this->assertSame(
393 'http://app23.internal/m/index.php?title=' . $name . '&' . $queryString,
394 $title->getInternalURL( $query ),
395 'getInternalURL( array )'
396 );
397
398 // Test getCanonicalURL()
399 $this->assertSame(
400 'https://xx.wiki.test/',
401 $title->getCanonicalURL(),
402 'getCanonicalURL()'
403 );
404 $this->assertSame(
405 'https://xx.wiki.test/m/index.php?title=' . $name . '&' . $queryString,
406 $title->getCanonicalURL( $query ),
407 'getCanonicalURL( array )'
408 );
409 }
410
411 public function testUrlsForTitleWithFragment() {
412 $title = Title::makeTitle( NS_USER, 'Göatee', 'Sectiön' );
413 $name = $title->getPrefixedURL();
414 $fragment = 'Secti.C3.B6n';
415
416 $query = [ 'x' => 'one two', 'y' => '#' ];
417 $queryString = 'x=one+two&y=%23';
418
419 // Test getLocalURL() ignores fragment
420 $this->assertSame(
421 '/wiki/' . $name,
422 $title->getLocalURL(),
423 'getLocalURL()'
424 );
425 $this->assertSame(
426 '/m/index.php?title=' . $name . '&' . $queryString,
427 $title->getLocalURL( $query ),
428 'getLocalURL( array )'
429 );
430
431 // Test getFullURL() includes fragment
432 $this->assertSame(
433 '//m.xx.wiki.test/wiki/' . $name . '#' . $fragment,
434 $title->getFullURL(),
435 'getFullURL()'
436 );
437 $this->assertSame(
438 '//m.xx.wiki.test/m/index.php?title=' . $name . '&' . $queryString . '#' . $fragment,
439 $title->getFullURL( $query ),
440 'getFullURL( array )'
441 );
442 $this->assertSame(
443 'https://xx.wiki.test/wiki/' . $name . '#' . $fragment,
444 $title->getFullURL( [], false, PROTO_CANONICAL ),
445 'getFullURL() with PROTO_CANONICAL'
446 );
447
448 // Test getFullUrlForRedirect() includes fragment
449 $this->assertSame(
450 'http://m.xx.wiki.test/wiki/' . $name . '#' . $fragment,
451 $title->getFullUrlForRedirect(),
452 'getFullUrlForRedirect()'
453 );
454 $this->assertSame(
455 'http://m.xx.wiki.test/m/index.php?title=' . $name . '&' . $queryString . '#' . $fragment,
456 $title->getFullUrlForRedirect( $query ),
457 'getFullUrlForRedirect( array )'
458 );
459 $this->assertSame(
460 '//m.xx.wiki.test/wiki/' . $name . '#' . $fragment,
461 $title->getFullUrlForRedirect( [], PROTO_RELATIVE ),
462 'getFullUrlForRedirect() with PROTO_RELATIVE'
463 );
464
465 // Test getLinkURL() includes fragment
466 $this->assertSame(
467 '/wiki/' . $name . '#' . $fragment,
468 $title->getLinkURL(),
469 'getLinkURL()'
470 );
471 $this->assertSame(
472 '/m/index.php?title=' . $name . '&' . $queryString . '#' . $fragment,
473 $title->getLinkURL( $query ),
474 'getLinkURL( array )'
475 );
476 $this->assertSame(
477 '//m.xx.wiki.test/wiki/' . $name . '#' . $fragment,
478 $title->getLinkURL( [], false, PROTO_RELATIVE ),
479 'getLinkURL() with PROTO_RELATIVE'
480 );
481
482 // Test getInternalURL() ignores fragment
483 $this->assertSame(
484 'http://app23.internal/wiki/' . $name,
485 $title->getInternalURL(),
486 'getInternalURL()'
487 );
488 $this->assertSame(
489 'http://app23.internal/m/index.php?title=' . $name . '&' . $queryString,
490 $title->getInternalURL( $query ),
491 'getInternalURL( array )'
492 );
493
494 // Test getCanonicalURL() includes fragment
495 $this->assertSame(
496 'https://xx.wiki.test/wiki/' . $name . '#' . $fragment,
497 $title->getCanonicalURL(),
498 'getCanonicalURL()'
499 );
500 $this->assertSame(
501 'https://xx.wiki.test/m/index.php?title=' . $name . '&' . $queryString . '#' . $fragment,
502 $title->getCanonicalURL( $query ),
503 'getCanonicalURL( array )'
504 );
505
506 // Test $wgFragmentMode
507 $this->overrideConfigValue( MainConfigNames::FragmentMode, [ 'html5', 'legacy' ] );
508 $fragment = 'Sectiön';
509
510 $this->assertSame(
511 '//m.xx.wiki.test/wiki/' . $name . '#' . $fragment,
512 $title->getFullURL(),
513 'getFullURL()'
514 );
515 $this->assertSame(
516 '/wiki/' . $name . '#' . $fragment,
517 $title->getLinkURL(),
518 'getLinkURL()'
519 );
520 }
521
522 public function testUrlsWithFragmentOnly() {
523 $title = Title::makeTitle( NS_MAIN, '', 'Jümp' );
524 $fragment = Sanitizer::escapeIdForLink( 'Jümp' );
525
526 $query = [ 'x' => 'one two', 'y' => '#' ];
527 $queryString = 'x=one+two&y=%23';
528
529 // Test getLocalURL() ignores fragment
530 $this->assertSame( // NOTE: not useful, may change!
531 '/wiki/',
532 $title->getLocalURL(),
533 'getLocalURL()'
534 );
535 $this->assertSame( // NOTE: not useful, may change!
536 '/m/index.php?title=&' . $queryString,
537 $title->getLocalURL( $query ),
538 'getLocalURL( array )'
539 );
540
541 // Test getFullURL() includes fragment
542 $this->assertSame( // NOTE: not useful, may change!
543 '//m.xx.wiki.test/wiki/#' . $fragment,
544 $title->getFullURL(),
545 'getFullURL()'
546 );
547 $this->assertSame( // NOTE: not useful, may change!
548 '//m.xx.wiki.test/m/index.php?title=&' . $queryString . '#' . $fragment,
549 $title->getFullURL( $query ),
550 'getFullURL( array )'
551 );
552 $this->assertSame( // NOTE: not useful, may change!
553 'https://xx.wiki.test/wiki/#' . $fragment,
554 $title->getFullURL( [], false, PROTO_CANONICAL ),
555 'getFullURL() with PROTO_CANONICAL'
556 );
557
558 // Test getFullUrlForRedirect() includes fragment
559 $this->assertSame( // NOTE: not useful, may change!
560 'http://m.xx.wiki.test/wiki/#' . $fragment,
561 $title->getFullUrlForRedirect(),
562 'getFullUrlForRedirect()'
563 );
564 $this->assertSame( // NOTE: not useful, may change!
565 'http://m.xx.wiki.test/m/index.php?title=&' . $queryString . '#' . $fragment,
566 $title->getFullUrlForRedirect( $query ),
567 'getFullUrlForRedirect( array )'
568 );
569 $this->assertSame( // NOTE: not useful, may change!
570 '//m.xx.wiki.test/wiki/#' . $fragment,
571 $title->getFullUrlForRedirect( [], PROTO_RELATIVE ),
572 'getFullUrlForRedirect() with PROTO_RELATIVE'
573 );
574
575 // Test getLinkURL()
576 $this->assertSame( // NOTE: this is the one useful way to handle a fragment jump
577 '#' . $fragment,
578 $title->getLinkURL(),
579 'getLinkURL()'
580 );
581 $this->assertSame(
582 '#' . $fragment, // NOTE: not useful, may change!
583 $title->getLinkURL( $query ),
584 'getLinkURL( array )'
585 );
586 $this->assertSame( // NOTE: not useful, may change!
587 '//m.xx.wiki.test/wiki/#' . $fragment,
588 $title->getLinkURL( [], false, PROTO_RELATIVE ),
589 'getLinkURL() with PROTO_RELATIVE'
590 );
591 $this->assertSame( // NOTE: not useful, may change!
592 'https://xx.wiki.test/m/index.php?title=&' . $queryString . '#' . $fragment,
593 $title->getLinkURL( $query, false, PROTO_CANONICAL ),
594 'getLinkURL() with PROTO_RELATIVE'
595 );
596
597 // Test getInternalURL() ignores fragment
598 $this->assertSame( // NOTE: not useful, may change!
599 'http://app23.internal/wiki/',
600 $title->getInternalURL(),
601 'getInternalURL()'
602 );
603 $this->assertSame( // NOTE: not useful, may change!
604 'http://app23.internal/m/index.php?title=&' . $queryString,
605 $title->getInternalURL( $query ),
606 'getInternalURL( array )'
607 );
608
609 // Test getCanonicalURL() includes fragment
610 $this->assertSame( // NOTE: not useful, may change!
611 'https://xx.wiki.test/wiki/#' . $fragment,
612 $title->getCanonicalURL(),
613 'getCanonicalURL()'
614 );
615 $this->assertSame( // NOTE: not useful, may change!
616 'https://xx.wiki.test/m/index.php?title=&' . $queryString . '#' . $fragment,
617 $title->getCanonicalURL( $query ),
618 'getCanonicalURL( array )'
619 );
620 }
621
622 public function testUrlsForUnknownInterwiki() {
623 // the "xyzzy" prefix is not known
624 $title = Title::makeTitle( NS_MAIN, 'Foobär', '', 'xyzzy' );
625 $name = $title->getPrefixedURL();
626
627 $query = [ 'x' => 'one two', 'y' => '#' ];
628 $queryString = 'x=one+two&y=%23';
629
630 // Test getLocalURL()
631 $this->assertSame(
632 '/wiki/' . $name,
633 $title->getLocalURL(),
634 'getLocalURL()'
635 );
636 $this->assertSame(
637 '/m/index.php?title=' . $name . '&' . $queryString,
638 $title->getLocalURL( $query ),
639 'getLocalURL( array )'
640 );
641
642 // Test getFullURL()
643 $this->assertSame(
644 '//m.xx.wiki.test/wiki/' . $name,
645 $title->getFullURL(),
646 'getFullURL()'
647 );
648 $this->assertSame(
649 '//m.xx.wiki.test/m/index.php?title=' . $name . '&' . $queryString,
650 $title->getFullURL( $query ),
651 'getFullURL( array )'
652 );
653 $this->assertSame(
654 'https://m.xx.wiki.test/wiki/' . $name,
655 $title->getFullURL( [], false, PROTO_HTTPS ),
656 'getFullURL( array ) with PROTO_HTTPS'
657 );
658
659 // Test getFullUrlForRedirect()
660 // Note that it uses PROTO_CURRENT by default, which is 'http' for tests
661 $this->assertSame(
662 'http://m.xx.wiki.test/wiki/Special:GoToInterwiki/' . $name,
663 $title->getFullUrlForRedirect(),
664 'getFullUrlForRedirect()'
665 );
666 $this->assertSame(
667 'http://m.xx.wiki.test/m/index.php?title=Special:GoToInterwiki/'
668 . $name . '&' . $queryString,
669 $title->getFullUrlForRedirect( $query ),
670 'getFullUrlForRedirect( array )'
671 );
672
673 // Test getLinkURL()
674 $this->assertSame( // NOTE: could also just be '/wiki/...'
675 '//m.xx.wiki.test/wiki/' . $name,
676 $title->getLinkURL(),
677 'getLinkURL()'
678 );
679 $this->assertSame( // NOTE: could also just be '/m/...'
680 '//m.xx.wiki.test/m/index.php?title=' . $name . '&' . $queryString,
681 $title->getLinkURL( $query ),
682 'getLinkURL( array )'
683 );
684
685 // Test getInternalURL()
686 $this->assertSame(
687 'http://app23.internal/wiki/' . $name,
688 $title->getInternalURL(),
689 'getInternalURL()'
690 );
691 $this->assertSame(
692 'http://app23.internal/m/index.php?title=' . $name . '&' . $queryString,
693 $title->getInternalURL( $query ),
694 'getInternalURL( array )'
695 );
696
697 // Test getCanonicalURL()
698 $this->assertSame(
699 'https://xx.wiki.test/wiki/' . $name,
700 $title->getCanonicalURL(),
701 'getCanonicalURL()'
702 );
703 $this->assertSame(
704 'https://xx.wiki.test/m/index.php?title=' . $name . '&' . $queryString,
705 $title->getCanonicalURL( $query ),
706 'getCanonicalURL( array )'
707 );
708 }
709
710 public function testUrlsForExternalInterwikiWithFragment() {
711 // the "acme" prefix is a known external interwiki prefix
712 $title = Title::makeTitle( NS_MAIN, 'fröbnitz/foo+bar', 'Sectiön', 'acme' );
713 $section = 'Sectiön';
714
715 // NOTE: The "/" should remain unencoded even in interwiki links
716 $name = wfUrlencode( $title->getDBkey() );
717
718 $query = [ 'x' => 'one two+three', 'y' => '#' ];
719 $queryString = 'x=one+two%2Bthree&y=%23';
720
721 // Test getLocalURL()
722 $this->assertSame(
723 'https://acme.test/' . $name,
724 $title->getLocalURL(),
725 'getLocalURL()'
726 );
727 $this->assertSame(
728 'https://acme.test/' . $name . '?' . $queryString,
729 $title->getLocalURL( $query ),
730 'getLocalURL( array )'
731 );
732
733 // Test getFullURL()
734 $this->assertSame(
735 'https://acme.test/' . $name . '#' . $section,
736 $title->getFullURL(),
737 'getFullURL()'
738 );
739 $this->assertSame(
740 'https://acme.test/' . $name . '?' . $queryString . '#' . $section,
741 $title->getFullURL( $query ),
742 'getFullURL( array )'
743 );
744 $this->assertSame( // should not trigger action path
745 'https://acme.test/' . $name . '?action=edit#' . $section,
746 $title->getFullURL( [ 'action' => 'edit' ] ),
747 'getFullURL( array )'
748 );
749 $this->assertSame( // should not trigger variant path
750 'https://acme.test/' . $name . '?variant=en-x-piglatin#' . $section,
751 $title->getFullURL( [ 'variant' => 'en-x-piglatin' ] ),
752 'getFullURL( array )'
753 );
754
755 // Test getFullUrlForRedirect()
756 // Note that it uses PROTO_CURRENT by default, which is 'http' for tests
757 $this->assertSame(
758 'http://m.xx.wiki.test/wiki/Special:GoToInterwiki/acme:' . $name,
759 $title->getFullUrlForRedirect(),
760 'getFullUrlForRedirect()'
761 );
762 $this->assertSame(
763 'http://m.xx.wiki.test/m/index.php?title=Special:GoToInterwiki/acme:'
764 . $name . '&' . $queryString,
765 $title->getFullUrlForRedirect( $query ),
766 'getFullUrlForRedirect( array )'
767 );
768
769 // Test getLinkURL() includes fragment
770 $this->assertSame(
771 'https://acme.test/' . $name . '#' . $section,
772 $title->getLinkURL(),
773 'getLinkURL()'
774 );
775 $this->assertSame(
776 'https://acme.test/' . $name . '?' . $queryString . '#' . $section,
777 $title->getLinkURL( $query ),
778 'getLinkURL( array )'
779 );
780
781 // Test getInternalURL() ignores fragment
782 $this->assertSame( // NOTE: the current behavior is just wrong.
783 'http://app23.internalhttps//acme.test/' . $name,
784 $title->getInternalURL(),
785 'getInternalURL()'
786 );
787 $this->assertSame( // NOTE: the current behavior is just wrong.
788 'http://app23.internalhttps//acme.test/' . $name . '?' . $queryString,
789 $title->getInternalURL( $query ),
790 'getInternalURL( array )'
791 );
792
793 // Test getCanonicalURL() includes fragment
794 $this->assertSame(
795 'https://acme.test/' . $name . '#' . $section,
796 $title->getCanonicalURL(),
797 'getCanonicalURL()'
798 );
799 $this->assertSame(
800 'https://acme.test/' . $name . '?' . $queryString . '#' . $section,
801 $title->getCanonicalURL( $query ),
802 'getCanonicalURL( array )'
803 );
804
805 // Test $wgFragmentMode
806 $this->overrideConfigValues( [
807 MainConfigNames::FragmentMode => [ 'html5', 'legacy' ],
808 MainConfigNames::ExternalInterwikiFragmentMode => 'legacy',
809 ] );
810 $section = 'Secti.C3.B6n';
811
812 $this->assertSame(
813 'https://acme.test/' . $name . '#' . $section,
814 $title->getFullURL(),
815 'getFullURL()'
816 );
817 $this->assertSame(
818 'https://acme.test/' . $name . '#' . $section,
819 $title->getLinkURL(),
820 'getLinkURL()'
821 );
822 }
823
824 public function testUrlsForLocalInterwikiWithFragment() {
825 // the "yy" prefix is a known local interwiki prefix
826 $title = Title::makeTitle( NS_MAIN, 'fröbnitz', 'Sectiön', 'yy' );
827 $name = wfUrlencode( $title->getDBkey() );
828
829 // local interwikis use $wgFragmentMode, not $wgExternalInterwikiFragmentMode
830 $section = 'Secti.C3.B6n';
831
832 $query = [ 'x' => 'one two', 'y' => '#' ];
833 $queryString = 'x=one+two&y=%23';
834
835 // Test getLocalURL()
836 $this->assertSame(
837 '//yy.wiki.test/wiki/' . $name,
838 $title->getLocalURL(),
839 'getLocalURL()'
840 );
841 $this->assertSame(
842 '//yy.wiki.test/wiki/' . $name . '?' . $queryString,
843 $title->getLocalURL( $query ),
844 'getLocalURL( array )'
845 );
846
847 // Test getFullURL()
848 $this->assertSame(
849 '//yy.wiki.test/wiki/' . $name . '#' . $section,
850 $title->getFullURL(),
851 'getFullURL()'
852 );
853 $this->assertSame(
854 '//yy.wiki.test/wiki/' . $name . '?' . $queryString . '#' . $section,
855 $title->getFullURL( $query ),
856 'getFullURL( array )'
857 );
858 $this->assertSame(
859 'https://yy.wiki.test/wiki/' . $name . '#' . $section,
860 $title->getFullURL( [], false, PROTO_HTTPS ),
861 'getFullURL( array ) with PROTO_HTTPS'
862 );
863 $this->assertSame(
864 'https://yy.wiki.test/wiki/' . $name . '#' . $section,
865 $title->getFullURL( [], false, PROTO_CANONICAL ),
866 'getFullURL( array ) with PROTO_CANONICAL'
867 );
868 $this->assertSame(
869 'http://yy.wiki.test/wiki/' . $name . '#' . $section,
870 $title->getFullURL( [], false, PROTO_INTERNAL ),
871 'getFullURL( array ) with PROTO_INTERNAL'
872 );
873 $this->assertSame( // NOTE: could as well use action path
874 '//yy.wiki.test/wiki/' . $name . '?action=edit#' . $section,
875 $title->getFullURL( [ 'action' => 'edit' ] ),
876 'getFullURL( array )'
877 );
878 $this->assertSame( // NOTE: could as well use variant path
879 '//yy.wiki.test/wiki/' . $name . '?variant=en-x-piglatin#' . $section,
880 $title->getFullURL( [ 'variant' => 'en-x-piglatin' ] ),
881 'getFullURL( array )'
882 );
883
884 // Test getFullUrlForRedirect()
885 // Note that it uses PROTO_CURRENT by default, which is 'http' for tests
886 $this->assertSame(
887 'http://m.xx.wiki.test/wiki/Special:GoToInterwiki/yy:' . $name,
888 $title->getFullUrlForRedirect(),
889 'getFullUrlForRedirect()'
890 );
891 $this->assertSame(
892 'http://m.xx.wiki.test/m/index.php?title=Special:GoToInterwiki/yy:'
893 . $name . '&' . $queryString,
894 $title->getFullUrlForRedirect( $query ),
895 'getFullUrlForRedirect( array )'
896 );
897
898 // Test getLinkURL() includes fragment
899 $this->assertSame(
900 '//yy.wiki.test/wiki/' . $name . '#' . $section,
901 $title->getLinkURL(),
902 'getLinkURL()'
903 );
904 $this->assertSame(
905 '//yy.wiki.test/wiki/' . $name . '?' . $queryString . '#' . $section,
906 $title->getLinkURL( $query ),
907 'getLinkURL( array )'
908 );
909 $this->assertSame(
910 'https://yy.wiki.test/wiki/' . $name . '?' . $queryString . '#' . $section,
911 $title->getLinkURL( $query, false, PROTO_CANONICAL ),
912 'getLinkURL( array ) with PROTO_CANONICAL'
913 );
914 $this->assertSame(
915 'http://yy.wiki.test/wiki/' . $name . '?' . $queryString . '#' . $section,
916 $title->getLinkURL( $query, false, PROTO_INTERNAL ),
917 'getLinkURL( array ) with PROTO_INTERNAL'
918 );
919 $this->assertSame(
920 'https://yy.wiki.test/wiki/' . $name . '?' . $queryString . '#' . $section,
921 $title->getLinkURL( $query, false, PROTO_HTTPS ),
922 'getLinkURL( array ) with PROTO_HTTPS'
923 );
924
925 // Test getInternalURL() ignores fragment
926 $this->assertSame( // NOTE: the current behavior is just wrong.
927 'http://app23.internal//yy.wiki.test/wiki/' . $name,
928 $title->getInternalURL(),
929 'getInternalURL()'
930 );
931 $this->assertSame( // NOTE: the current behavior is just wrong.
932 'http://app23.internal//yy.wiki.test/wiki/' . $name . '?' . $queryString,
933 $title->getInternalURL( $query ),
934 'getInternalURL( array )'
935 );
936
937 // Test getCanonicalURL() includes fragment
938 $this->assertSame(
939 'https://yy.wiki.test/wiki/' . $name . '#' . $section,
940 $title->getCanonicalURL(),
941 'getCanonicalURL()'
942 );
943 $this->assertSame(
944 'https://yy.wiki.test/wiki/' . $name . '?' . $queryString . '#' . $section,
945 $title->getCanonicalURL( $query ),
946 'getCanonicalURL( array )'
947 );
948
949 // Test $wgFragmentMode
950 $this->overrideConfigValues( [
951 MainConfigNames::FragmentMode => [ 'html5', 'legacy' ],
952 MainConfigNames::ExternalInterwikiFragmentMode => 'legacy',
953 ] );
954 $section = 'Sectiön';
955
956 $this->assertSame(
957 '//yy.wiki.test/wiki/' . $name . '#' . $section,
958 $title->getFullURL(),
959 'getFullURL()'
960 );
961 $this->assertSame(
962 '//yy.wiki.test/wiki/' . $name . '#' . $section,
963 $title->getLinkURL(),
964 'getLinkURL()'
965 );
966 }
967
968 public function testUrlsWithHttpsPort() {
969 $title = Title::makeTitle( NS_USER, 'Göatee' );
970 $name = $title->getPrefixedURL();
971
972 // NOTE: $wgHttpsPort is only supported if $wgCanonicalServer does not use HTTPS
973 $this->overrideConfigValues( [
974 MainConfigNames::CanonicalServer => 'http://xx.wiki.test',
975 MainConfigNames::HttpsPort => '4444',
976 ] );
977
978 // Test getFullURL()
979 $this->assertSame(
980 '//m.xx.wiki.test/wiki/' . $name,
981 $title->getFullURL( [], false, PROTO_RELATIVE ),
982 'getFullURL() with PROTO_RELATIVE'
983 );
984 $this->assertSame(
985 'http://xx.wiki.test/wiki/' . $name,
986 $title->getFullURL( [], false, PROTO_CANONICAL ),
987 'getFullURL() with PROTO_CANONICAL'
988 );
989 $this->assertSame(
990 'http://m.xx.wiki.test/wiki/' . $name,
991 $title->getFullURL( [], false, PROTO_HTTP ),
992 'getFullURL() with PROTO_HTTP'
993 );
994 $this->assertSame(
995 'https://m.xx.wiki.test:4444/wiki/' . $name,
996 $title->getFullURL( [], false, PROTO_HTTPS ),
997 'getFullURL() with PROTO_HTTP'
998 );
999 $this->assertSame(
1000 'http://app23.internal/wiki/' . $name,
1001 $title->getFullURL( [], false, PROTO_INTERNAL ),
1002 'getFullURL() with PROTO_INTERNAL'
1003 );
1004
1005 // Test getFullUrlForRedirect()
1006 $this->assertSame(
1007 '//m.xx.wiki.test/wiki/' . $name,
1008 $title->getFullUrlForRedirect( [], PROTO_RELATIVE ),
1009 'getFullUrlForRedirect() with PROTO_RELATIVE'
1010 );
1011 $this->assertSame(
1012 'https://m.xx.wiki.test:4444/wiki/' . $name,
1013 $title->getFullUrlForRedirect( [], PROTO_HTTPS ),
1014 'getFullUrlForRedirect() with PROTO_HTTPS'
1015 );
1016
1017 // Test getLinkURL()
1018 $this->assertSame(
1019 '//m.xx.wiki.test/wiki/' . $name,
1020 $title->getLinkURL( [], false, PROTO_RELATIVE ),
1021 'getLinkURL() with PROTO_RELATIVE'
1022 );
1023 $this->assertSame(
1024 'https://m.xx.wiki.test:4444/wiki/' . $name,
1025 $title->getLinkURL( [], false, PROTO_HTTPS ),
1026 'getLinkURL() with PROTO_HTTPS'
1027 );
1028 $this->assertSame(
1029 'http://xx.wiki.test/wiki/' . $name,
1030 $title->getLinkURL( [], false, PROTO_CANONICAL ),
1031 'getLinkURL() with PROTO_CANONICAL'
1032 );
1033 $this->assertSame(
1034 'http://app23.internal/wiki/' . $name,
1035 $title->getLinkURL( [], false, PROTO_INTERNAL ),
1036 'getLinkURL() with PROTO_INTERNAL'
1037 );
1038
1039 // Test getInternalURL()
1040 $this->assertSame(
1041 'http://app23.internal/wiki/' . $name,
1042 $title->getInternalURL(),
1043 'getInternalURL()'
1044 );
1045
1046 // Test getCanonicalURL()
1047 $this->assertSame(
1048 'http://xx.wiki.test/wiki/' . $name,
1049 $title->getCanonicalURL(),
1050 'getCanonicalURL()'
1051 );
1052 }
1053
1054 }