| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Content\WikitextContent; |
| 4 |
use MediaWiki\Context\RequestContext; |
| 5 |
use MediaWiki\MainConfigNames; |
| 6 |
use MediaWiki\Specials\SpecialMyLanguage; |
| 7 |
use MediaWiki\Specials\SpecialPageLanguage; |
| 8 |
use MediaWiki\Title\Title; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class SpecialMyLanguageTest extends MediaWikiIntegrationTestCase { |
| 15 |
public function addDBDataOnce() { |
| 16 |
$titles = [ |
| 17 |
'Page/Another', |
| 18 |
'Page/Another/ar', |
| 19 |
'Page/Another/en', |
| 20 |
'Page/Another/ru', |
| 21 |
'Page/Another/zh', |
| 22 |
'Page/Foreign', |
| 23 |
'Page/Foreign/en', |
| 24 |
'Page/Foreign/zh', |
| 25 |
'Page/Redirect', |
| 26 |
]; |
| 27 |
|
| 28 |
|
| 29 |
$pageLang = [ |
| 30 |
'Page/Foreign' => 'zh', |
| 31 |
]; |
| 32 |
$pageContent = [ |
| 33 |
'Page/Redirect' => '#REDIRECT [[Page/Another#Section]]', |
| 34 |
]; |
| 35 |
$user = $this->getTestSysop()->getUser(); |
| 36 |
$context = RequestContext::getMain(); |
| 37 |
$context->setUser( $user ); |
| 38 |
foreach ( $titles as $title ) { |
| 39 |
$this->editPage( |
| 40 |
$title, |
| 41 |
new WikitextContent( $pageContent[$title] ?? 'SpecialMyLanguageTest content' ), |
| 42 |
'SpecialMyLanguageTest Summary', |
| 43 |
NS_MAIN, |
| 44 |
$user |
| 45 |
); |
| 46 |
if ( isset( $pageLang[$title] ) ) { |
| 47 |
SpecialPageLanguage::changePageLanguage( |
| 48 |
$context, Title::newFromText( $title ), $pageLang[$title], 'Test' ); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function testFindTitle( $expected, $subpage, $contLang, $userLang ) { |
| 62 |
$this->setContentLang( $contLang ); |
| 63 |
$services = $this->getServiceContainer(); |
| 64 |
$special = new SpecialMyLanguage( |
| 65 |
$services->getLanguageNameUtils(), |
| 66 |
$services->getRedirectLookup() |
| 67 |
); |
| 68 |
$special->getContext()->setLanguage( $userLang ); |
| 69 |
$this->overrideConfigValue( MainConfigNames::PageLanguageUseDB, true ); |
| 70 |
|
| 71 |
$this->mergeMwGlobalArrayValue( 'wgNamespacesWithSubpages', [ NS_MAIN => true ] ); |
| 72 |
$this->assertTitle( $expected, $special->findTitle( $subpage ) ); |
| 73 |
$this->mergeMwGlobalArrayValue( 'wgNamespacesWithSubpages', [ NS_MAIN => false ] ); |
| 74 |
$this->assertTitle( $expected, $special->findTitle( $subpage ) ); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private function assertTitle( $expected, $title ) { |
| 82 |
if ( $expected === null ) { |
| 83 |
$this->assertNull( $title ); |
| 84 |
} else { |
| 85 |
$expected = Title::newFromText( $expected ); |
| 86 |
$this->assertTrue( $expected->isSameLinkAs( $title ) ); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
public static function provideFindTitle() { |
| 91 |
|
| 92 |
return [ |
| 93 |
|
| 94 |
[ null, '::Fail', 'en', 'en' ], |
| 95 |
[ 'Page/Another', 'Page/Another/en', 'en', 'en' ], |
| 96 |
[ 'Page/Another', 'Page/Another', 'en', 'en' ], |
| 97 |
[ 'Page/Another/ru', 'Page/Another', 'en', 'ru' ], |
| 98 |
[ 'Page/Another', 'Page/Another', 'en', 'es' ], |
| 99 |
[ 'Page/Another/zh', 'Page/Another', 'en', 'zh' ], |
| 100 |
[ 'Page/Another/zh', 'Page/Another', 'en', 'zh-hans' ], |
| 101 |
[ 'Page/Another/zh', 'Page/Another', 'en', 'zh-mo' ], |
| 102 |
[ 'Page/Another/zh', 'Page/Another', 'en', 'gan' ], |
| 103 |
[ 'Page/Another/zh', 'Page/Another', 'en', 'gan-hant' ], |
| 104 |
[ 'Page/Another/en', 'Page/Another', 'de', 'es' ], |
| 105 |
[ 'Page/Another/ar', 'Page/Another', 'en', 'ar' ], |
| 106 |
[ 'Page/Another/ar', 'Page/Another', 'en', 'arz' ], |
| 107 |
[ 'Page/Another/ar', 'Page/Another/de', 'en', 'arz' ], |
| 108 |
[ 'Page/Another/ru', 'Page/Another/ru', 'en', 'arz' ], |
| 109 |
[ 'Page/Another/ar', 'Page/Another/ru', 'en', 'ar' ], |
| 110 |
[ null, 'Special:Blankpage', 'en', 'ar' ], |
| 111 |
[ null, 'Media:Fail', 'en', 'ar' ], |
| 112 |
[ 'Page/Foreign/en', 'Page/Foreign', 'en', 'en' ], |
| 113 |
[ 'Page/Foreign', 'Page/Foreign', 'en', 'zh-hk' ], |
| 114 |
[ 'Page/Another/ar#Section', 'Page/Redirect', 'en', 'ar' ], |
| 115 |
]; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public function testFindTitleForTransclusion( $expected, $subpage, $langCode, $userLang ) { |
| 127 |
$this->setContentLang( $langCode ); |
| 128 |
$services = $this->getServiceContainer(); |
| 129 |
$special = new SpecialMyLanguage( |
| 130 |
$services->getLanguageNameUtils(), |
| 131 |
$services->getRedirectLookup() |
| 132 |
); |
| 133 |
$special->getContext()->setLanguage( $userLang ); |
| 134 |
|
| 135 |
$this->mergeMwGlobalArrayValue( 'wgNamespacesWithSubpages', [ NS_MAIN => true ] ); |
| 136 |
$this->assertTitle( $expected, $special->findTitleForTransclusion( $subpage ) ); |
| 137 |
$this->mergeMwGlobalArrayValue( 'wgNamespacesWithSubpages', [ NS_MAIN => false ] ); |
| 138 |
$this->assertTitle( $expected, $special->findTitleForTransclusion( $subpage ) ); |
| 139 |
} |
| 140 |
|
| 141 |
public static function provideFindTitleForTransclusion() { |
| 142 |
|
| 143 |
return [ |
| 144 |
|
| 145 |
[ 'Page/Another/en', 'Page/Another/en', 'en', 'en' ], |
| 146 |
[ 'Page/Another/en', 'Page/Another', 'en', 'en' ], |
| 147 |
[ 'Page/Another/en', 'Page/Another', 'en', 'frc' ], |
| 148 |
]; |
| 149 |
} |
| 150 |
} |