Debian Codegraph @

tests/phpunit/unit/includes/title/TitleTest.php

raw symbols SCIP
1 <?php
2
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 */
21 namespace MediaWiki\Tests\Unit;
22
23 use MediaWiki\Linker\LinkTarget;
24 use MediaWiki\Page\PageIdentity;
25 use MediaWiki\Page\PageIdentityValue;
26 use MediaWiki\Page\PageReference;
27 use MediaWiki\Page\PageReferenceValue;
28 use MediaWiki\Title\Title;
29 use MediaWiki\Title\TitleValue;
30 use MediaWiki\User\UserIdentityValue;
31 use MediaWikiUnitTestCase;
32
33 /**
34 * @covers \MediaWiki\Title\Title
35 *
36 * @author DannyS712
37 */
38 class TitleTest extends MediaWikiUnitTestCase {
39
40 public function testGetters() {
41 // The only way to create a title without needing any services is ::makeTitle
42 // link to `w:Project:About Wikipedia#Introduction'
43 $title = Title::makeTitle(
44 NS_PROJECT,
45 'About Wikipedia',
46 'Introduction',
47 'w'
48 );
49 $this->assertTrue( $title->isExternal() );
50 $this->assertSame( 'w', $title->getInterwiki() );
51 $this->assertSame( 'About Wikipedia', $title->getText() );
52 $this->assertSame( wfUrlencode( 'About_Wikipedia' ), $title->getPartialURL() );
53 $this->assertSame( 'About_Wikipedia', $title->getDBkey() );
54 $this->assertSame( NS_PROJECT, $title->getNamespace() );
55 $this->assertFalse( $title->isSpecialPage() );
56 $this->assertFalse( $title->isConversionTable() );
57 $this->assertSame( 'Introduction', $title->getFragment() );
58 $this->assertTrue( $title->hasFragment() );
59 }
60
61 public function testIsSpecial() {
62 // Already checked false above, try with true now
63 $title = Title::makeTitle( NS_SPECIAL, 'SpecialPages' );
64 $this->assertTrue( $title->isSpecialPage() );
65 }
66
67 public function testIsConversionTable() {
68 // Already checked false above, try with true now
69 $title = Title::makeTitle( NS_MEDIAWIKI, 'Conversiontable/foo' );
70 $this->assertTrue( $title->isConversionTable() );
71 }
72
73 /**
74 * @covers \MediaWiki\Title\Title::legalChars
75 */
76 public function testLegalChars() {
77 $titlechars = Title::legalChars();
78
79 foreach ( range( 1, 255 ) as $num ) {
80 $chr = chr( $num );
81 if ( $num <= 0x1f || str_contains( "#[]{}<>|\x7f", $chr ) ) {
82 $this->assertFalse(
83 (bool)preg_match( "/[$titlechars]/", $chr ),
84 "chr($num) = $chr is not a valid titlechar"
85 );
86 } else {
87 $this->assertTrue(
88 (bool)preg_match( "/[$titlechars]/", $chr ),
89 "chr($num) = $chr is a valid titlechar"
90 );
91 }
92 }
93 }
94
95 public static function provideConvertByteClassToUnicodeClass() {
96 return [
97 [
98 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
99 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
100 ],
101 [
102 'QWERTYf-\\xFF+',
103 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
104 ],
105 [
106 'QWERTY\\x66-\\xFD+',
107 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
108 ],
109 [
110 'QWERTYf-y+',
111 'QWERTYf-y+',
112 ],
113 [
114 'QWERTYf-\\x80+',
115 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
116 ],
117 [
118 'QWERTY\\x66-\\x80+\\x23',
119 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
120 ],
121 [
122 'QWERTY\\x66-\\x80+\\xD3',
123 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
124 ],
125 [
126 '\\\\\\x99',
127 '\\\\\\u0080-\\uFFFF',
128 ],
129 [
130 '-\\x99',
131 '\\-\\u0080-\\uFFFF',
132 ],
133 [
134 'QWERTY\\-\\x99',
135 'QWERTY\\-\\u0080-\\uFFFF',
136 ],
137 [
138 '\\\\x99',
139 '\\\\x99',
140 ],
141 [
142 'A-\\x9F',
143 'A-\\x7F\\u0080-\\uFFFF',
144 ],
145 [
146 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
147 'f-wQWERTYFXZ\\u0080-\\uFFFF',
148 ],
149 [
150 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
151 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
152 ],
153 ];
154 }
155
156 /**
157 * @dataProvider provideConvertByteClassToUnicodeClass
158 * @covers \MediaWiki\Title\Title::convertByteClassToUnicodeClass
159 */
160 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
161 $this->assertEquals( $unicodeClass, Title::convertByteClassToUnicodeClass( $byteClass ) );
162 }
163
164 public static function provideNewFromTitleValue() {
165 return [
166 [ new TitleValue( NS_MAIN, 'Foo' ) ],
167 [ new TitleValue( NS_MAIN, 'Foo', 'bar' ) ],
168 [ new TitleValue( NS_USER, 'Hansi_Maier' ) ],
169 ];
170 }
171
172 /**
173 * @covers \MediaWiki\Title\Title::newFromLinkTarget
174 * @dataProvider provideNewFromTitleValue
175 */
176 public function testNewFromLinkTarget( LinkTarget $value ) {
177 $title = Title::newFromLinkTarget( $value );
178
179 $dbkey = str_replace( ' ', '_', $value->getText() );
180 $this->assertEquals( $dbkey, $title->getDBkey() );
181 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
182 $this->assertEquals( $value->getFragment(), $title->getFragment() );
183 }
184
185 /**
186 * @covers \MediaWiki\Title\Title::newFromLinkTarget
187 */
188 public function testNewFromLinkTarget_clone() {
189 $title = Title::makeTitle( NS_MAIN, 'Example' );
190 $this->assertSame( $title, Title::newFromLinkTarget( $title ) );
191
192 // The Title::NEW_CLONE flag should ensure that a fresh instance is returned.
193 $clone = Title::newFromLinkTarget( $title, Title::NEW_CLONE );
194 $this->assertNotSame( $title, $clone );
195 $this->assertTrue( $clone->equals( $title ) );
196 }
197
198 public function provideCastFromLinkTarget() {
199 return array_merge( [ [ null ] ], $this->provideNewFromTitleValue() );
200 }
201
202 /**
203 * @covers \MediaWiki\Title\Title::castFromLinkTarget
204 * @dataProvider provideCastFromLinkTarget
205 */
206 public function testCastFromLinkTarget( $value ) {
207 $title = Title::castFromLinkTarget( $value );
208
209 if ( $value === null ) {
210 $this->assertNull( $title );
211 } else {
212 $dbkey = str_replace( ' ', '_', $value->getText() );
213 $this->assertSame( $dbkey, $title->getDBkey() );
214 $this->assertSame( $value->getNamespace(), $title->getNamespace() );
215 $this->assertSame( $value->getFragment(), $title->getFragment() );
216 }
217 }
218
219 public static function provideDataForTestSetAndGetFragment() {
220 return [
221 [ '#fragment', 'fragment' ],
222 [ '#fragment_frag', 'fragment frag' ],
223 [ 'fragment', 'fragment' ],
224 [ 'fragment_frag', 'fragment frag' ],
225 ];
226 }
227
228 /**
229 * @covers \MediaWiki\Title\Title::getFragment
230 * @covers \MediaWiki\Title\Title::setFragment
231 * @covers \MediaWiki\Title\Title::normalizeFragment
232 * @dataProvider provideDataForTestSetAndGetFragment
233 */
234 public function testSetAndGetFragment( string $fragment, $expected ) {
235 $title = Title::makeTitle( NS_MAIN, 'Title' );
236 $title->setFragment( $fragment );
237 $this->assertSame( $expected, $title->getFragment() );
238 }
239
240 public static function provideTitleWithOrWithoutFragments() {
241 return [
242 [ Title::makeTitle( NS_MAIN, 'Title', 'fragment' ), true ],
243 [ Title::makeTitle( NS_MAIN, 'Title' ), false ],
244 [ Title::makeTitle( NS_HELP, '' ), false ],
245 ];
246 }
247
248 /**
249 * @covers \MediaWiki\Title\Title::hasFragment
250 * @dataProvider provideTitleWithOrWithoutFragments
251 */
252 public function testHasFragment( Title $title, $expected ) {
253 $this->assertSame( $expected, $title->hasFragment() );
254 }
255
256 public static function provideCompare() {
257 yield 'Title == Title' => [
258 Title::makeTitle( NS_MAIN, 'Aa' ),
259 Title::makeTitle( NS_MAIN, 'Aa' ),
260 0
261 ];
262 yield 'Title > Title, name' => [
263 Title::makeTitle( NS_MAIN, 'Ax' ),
264 Title::makeTitle( NS_MAIN, 'Aa' ),
265 1
266 ];
267 yield 'Title < Title, name' => [
268 Title::makeTitle( NS_MAIN, 'Aa' ),
269 Title::makeTitle( NS_MAIN, 'Ax' ),
270 -1
271 ];
272 yield 'Title > Title, ns' => [
273 Title::makeTitle( NS_TALK, 'Aa' ),
274 Title::makeTitle( NS_MAIN, 'Aa' ),
275 1
276 ];
277 yield 'Title < Title, ns' => [
278 Title::makeTitle( NS_SPECIAL, 'Aa' ),
279 Title::makeTitle( NS_MAIN, 'Aa' ),
280 -1
281 ];
282 yield 'LinkTarget == PageReference' => [
283 new TitleValue( NS_MAIN, 'Aa' ),
284 new PageReferenceValue( NS_MAIN, 'Aa', PageReference::LOCAL ),
285 0
286 ];
287 yield 'Title > PageReference, name' => [
288 Title::makeTitle( NS_TALK, 'Aa' ),
289 new PageReferenceValue( NS_MAIN, 'Aa', PageReference::LOCAL ),
290 1
291 ];
292 yield 'LinkTarget < Title, ns' => [
293 new TitleValue( NS_SPECIAL, 'Aa' ),
294 Title::makeTitle( NS_MAIN, 'Aa' ),
295 -2
296 ];
297 }
298
299 /**
300 * @dataProvider provideCompare
301 * @covers \MediaWiki\Title\Title::compare
302 */
303 public function testCompare( $a, $b, $expected ) {
304 if ( $expected > 0 ) {
305 $this->assertGreaterThan( 0, Title::compare( $a, $b ) );
306 } elseif ( $expected < 0 ) {
307 $this->assertLessThan( 0, Title::compare( $a, $b ) );
308 } else {
309 $this->assertSame( 0, Title::compare( $a, $b ) );
310 }
311 }
312
313 public function provideCastFromPageIdentity() {
314 yield [ null ];
315
316 $fake = $this->createMock( PageIdentity::class );
317 $fake->method( 'getId' )->willReturn( 7 );
318 $fake->method( 'getNamespace' )->willReturn( NS_MAIN );
319 $fake->method( 'getDBkey' )->willReturn( 'Test' );
320
321 yield [ $fake ];
322
323 $fake = $this->createMock( Title::class );
324 $fake->method( 'getId' )->willReturn( 7 );
325 $fake->method( 'getNamespace' )->willReturn( NS_MAIN );
326 $fake->method( 'getDBkey' )->willReturn( 'Test' );
327
328 yield [ $fake ];
329 }
330
331 /**
332 * @covers \MediaWiki\Title\Title::castFromPageIdentity
333 * @dataProvider provideCastFromPageIdentity
334 */
335 public function testCastFromPageIdentity( ?PageIdentity $value ) {
336 $title = Title::castFromPageIdentity( $value );
337
338 if ( $value === null ) {
339 $this->assertNull( $title );
340 } elseif ( $value instanceof Title ) {
341 $this->assertSame( $value, $title );
342 } else {
343 $this->assertSame( $value->getId(), $title->getArticleID() );
344 $this->assertSame( $value->getNamespace(), $title->getNamespace() );
345 $this->assertSame( $value->getDBkey(), $title->getDBkey() );
346 }
347 }
348
349 public static function provideCastFromPageReference() {
350 yield [ new PageReferenceValue( NS_MAIN, 'Test', PageReference::LOCAL ) ];
351 }
352
353 /**
354 * @covers \MediaWiki\Title\Title::castFromPageReference
355 * @dataProvider provideCastFromPageIdentity
356 * @dataProvider provideCastFromPageReference
357 */
358 public function testCastFromPageReference( ?PageReference $value ) {
359 $title = Title::castFromPageReference( $value );
360
361 if ( $value === null ) {
362 $this->assertNull( $title );
363 } elseif ( $value instanceof Title ) {
364 $this->assertSame( $value, $title );
365 } else {
366 $this->assertSame( $value->getNamespace(), $title->getNamespace() );
367 $this->assertSame( $value->getDBkey(), $title->getDBkey() );
368 }
369 }
370
371 public static function provideCreateFragmentTitle() {
372 return [
373 [ NS_MAIN, 'Test', 'foo' ],
374 [ NS_TALK, 'Test', 'foo', '' ],
375 [ NS_CATEGORY, 'Test', 'foo', 'bar' ],
376 [ NS_MAIN, 'Test1', '', 'interwiki', 'baz' ]
377 ];
378 }
379
380 /**
381 * @covers \MediaWiki\Title\Title::createFragmentTarget
382 * @dataProvider provideCreateFragmentTitle
383 */
384 public function testCreateFragmentTitle( $namespace, $title, $fragment ) {
385 $title = Title::makeTitle( $namespace, $title );
386 $fragmentTitle = $title->createFragmentTarget( $fragment );
387
388 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
389 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
390 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
391 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
392 }
393
394 public static function provideEquals() {
395 yield '(makeTitle) same text' => [
396 Title::makeTitle( NS_MAIN, 'Main Page' ),
397 Title::makeTitle( NS_MAIN, 'Main Page' ),
398 true
399 ];
400 yield '(makeTitle) different text' => [
401 Title::makeTitle( NS_MAIN, 'Main Page' ),
402 Title::makeTitle( NS_MAIN, 'Not The Main Page' ),
403 false
404 ];
405 yield '(makeTitle) different namespace, same text' => [
406 Title::makeTitle( NS_MAIN, 'Main Page' ),
407 Title::makeTitle( NS_PROJECT, 'Main Page' ),
408 false
409 ];
410 yield '(makeTitle) namespace alias' => [
411 Title::makeTitle( NS_FILE, 'Example.png' ),
412 Title::makeTitle( NS_FILE, 'Example.png' ),
413 true
414 ];
415 yield '(makeTitle) same special page' => [
416 Title::makeTitle( NS_SPECIAL, 'Version' ),
417 Title::makeTitle( NS_SPECIAL, 'Version' ),
418 true
419 ];
420 yield '(makeTitle) different special page' => [
421 Title::makeTitle( NS_SPECIAL, 'Version' ),
422 Title::makeTitle( NS_SPECIAL, 'Recentchanges' ),
423 false
424 ];
425 yield '(makeTitle) compare special and normal page' => [
426 Title::makeTitle( NS_SPECIAL, 'Version' ),
427 Title::makeTitle( NS_MAIN, 'Main Page' ),
428 false
429 ];
430 yield '(makeTitle) same fragment' => [
431 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
432 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
433 true
434 ];
435 yield '(makeTitle) different fragment (ignored)' => [
436 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
437 Title::makeTitle( NS_MAIN, 'Foo', 'Baz', '' ),
438 true
439 ];
440 yield '(makeTitle) fragment vs no fragment (ignored)' => [
441 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
442 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
443 true
444 ];
445 yield '(makeTitle) same interwiki' => [
446 Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
447 Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
448 true
449 ];
450 yield '(makeTitle) different interwiki' => [
451 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
452 Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
453 false
454 ];
455
456 // Wrong type
457 yield '(makeTitle vs PageIdentityValue) name text' => [
458 Title::makeTitle( NS_MAIN, 'Foo' ),
459 new PageIdentityValue( 0, NS_MAIN, 'Foo', PageIdentity::LOCAL ),
460 false
461 ];
462 yield '(makeTitle vs TitleValue) name text' => [
463 Title::makeTitle( NS_MAIN, 'Foo' ),
464 new TitleValue( NS_MAIN, 'Foo' ),
465 false
466 ];
467 yield '(makeTitle vs UserIdentityValue) name text' => [
468 Title::makeTitle( NS_MAIN, 'Foo' ),
469 new UserIdentityValue( 7, 'Foo' ),
470 false
471 ];
472 }
473
474 /**
475 * @covers \MediaWiki\Title\Title::equals
476 * @dataProvider provideEquals
477 */
478 public function testEquals( Title $firstValue, $secondValue, $expectedSame ) {
479 $this->assertSame(
480 $expectedSame,
481 $firstValue->equals( $secondValue )
482 );
483 }
484
485 public static function provideIsSameLinkAs() {
486 yield 'same text' => [
487 Title::makeTitle( 0, 'Foo' ),
488 new TitleValue( 0, 'Foo' ),
489 true
490 ];
491 yield 'same namespace' => [
492 Title::makeTitle( 1, 'Bar_Baz' ),
493 new TitleValue( 1, 'Bar_Baz' ),
494 true
495 ];
496 yield 'same text, different namespace' => [
497 Title::makeTitle( 0, 'Foo' ),
498 new TitleValue( 1, 'Foo' ),
499 false
500 ];
501 yield 'different text' => [
502 Title::makeTitle( 0, 'Foo' ),
503 new TitleValue( 0, 'Foozz' ),
504 false
505 ];
506 yield 'different fragment' => [
507 Title::makeTitle( 0, 'Foo', '' ),
508 new TitleValue( 0, 'Foo', 'Bar' ),
509 false
510 ];
511 yield 'different interwiki' => [
512 Title::makeTitle( 0, 'Foo', '', 'bar' ),
513 new TitleValue( 0, 'Foo', '', '' ),
514 false
515 ];
516 }
517
518 /**
519 * @covers \MediaWiki\Title\Title::isSameLinkAs
520 * @dataProvider provideIsSameLinkAs
521 */
522 public function testIsSameLinkAs( Title $firstValue, $secondValue, $expectedSame ) {
523 $this->assertSame(
524 $expectedSame,
525 $firstValue->isSameLinkAs( $secondValue )
526 );
527 }
528
529 }