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