| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
use MediaWiki\Language\Language; |
| 23 |
use MediaWiki\Title\ForeignTitle; |
| 24 |
use MediaWiki\Title\NaiveForeignTitleFactory; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class NaiveForeignTitleFactoryTest extends MediaWikiUnitTestCase { |
| 32 |
|
| 33 |
public static function basicProvider() { |
| 34 |
return [ |
| 35 |
[ |
| 36 |
'MainNamespaceArticle', 0, |
| 37 |
new ForeignTitle( 0, '', 'MainNamespaceArticle' ), |
| 38 |
], |
| 39 |
[ |
| 40 |
'MainNamespaceArticle', null, |
| 41 |
new ForeignTitle( null, '', 'MainNamespaceArticle' ), |
| 42 |
], |
| 43 |
[ |
| 44 |
'Talk:Nice_talk', 1, |
| 45 |
new ForeignTitle( 1, 'Talk', 'Nice_talk' ), |
| 46 |
], |
| 47 |
[ |
| 48 |
'Bogus:Nice_talk', 0, |
| 49 |
new ForeignTitle( 0, '', 'Bogus:Nice_talk' ), |
| 50 |
], |
| 51 |
[ |
| 52 |
'Bogus:Nice_talk', 9000, |
| 53 |
new ForeignTitle( 9000, 'Bogus', 'Nice_talk' ), |
| 54 |
], |
| 55 |
[ |
| 56 |
'Bogus:Nice_talk', 4, |
| 57 |
new ForeignTitle( 4, 'Bogus', 'Nice_talk' ), |
| 58 |
], |
| 59 |
[ |
| 60 |
'Talk:Extra:Nice_talk', 1, |
| 61 |
new ForeignTitle( 1, 'Talk', 'Extra:Nice_talk' ), |
| 62 |
], |
| 63 |
[ |
| 64 |
'Talk:Extra:Nice_talk', null, |
| 65 |
new ForeignTitle( null, 'Talk', 'Extra:Nice_talk' ), |
| 66 |
], |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function testBasic( $title, $ns, ForeignTitle $foreignTitle ) { |
| 74 |
$contentLanguage = $this->createMock( Language::class ); |
| 75 |
$contentLanguage->method( 'getNsIndex' ) |
| 76 |
->willReturnCallback( |
| 77 |
static function ( $text ) { |
| 78 |
$text = strtolower( $text ); |
| 79 |
if ( $text === '' ) { |
| 80 |
return NS_MAIN; |
| 81 |
} |
| 82 |
$namespaces = [ |
| 83 |
'talk' => NS_TALK, |
| 84 |
'user' => NS_USER, |
| 85 |
]; |
| 86 |
return $namespaces[ $text ] ?? false; |
| 87 |
} |
| 88 |
); |
| 89 |
$factory = new NaiveForeignTitleFactory( $contentLanguage ); |
| 90 |
|
| 91 |
$testTitle = $factory->createForeignTitle( $title, $ns ); |
| 92 |
|
| 93 |
$this->assertEquals( $testTitle->isNamespaceIdKnown(), |
| 94 |
$foreignTitle->isNamespaceIdKnown() ); |
| 95 |
|
| 96 |
if ( |
| 97 |
$testTitle->isNamespaceIdKnown() && |
| 98 |
$foreignTitle->isNamespaceIdKnown() |
| 99 |
) { |
| 100 |
$this->assertEquals( $testTitle->getNamespaceId(), |
| 101 |
$foreignTitle->getNamespaceId() ); |
| 102 |
} |
| 103 |
|
| 104 |
$this->assertEquals( $testTitle->getNamespaceName(), |
| 105 |
$foreignTitle->getNamespaceName() ); |
| 106 |
$this->assertEquals( $testTitle->getText(), $foreignTitle->getText() ); |
| 107 |
|
| 108 |
$this->assertEquals( str_replace( ' ', '_', $title ), |
| 109 |
$foreignTitle->getFullText() ); |
| 110 |
} |
| 111 |
} |