Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / specials / SpecialMyLanguageTest.php
blobf89a408b7fe9958e461645ca735f9258176d7494
1 <?php
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;
10 /**
11 * @group Database
12 * @covers \MediaWiki\Specials\SpecialMyLanguage
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',
27 // In the real-world, they are in respective languages,
28 // but we don't need to set all of them for tests.
29 $pageLang = [
30 'Page/Foreign' => 'zh',
32 $pageContent = [
33 'Page/Redirect' => '#REDIRECT [[Page/Another#Section]]',
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
46 if ( isset( $pageLang[$title] ) ) {
47 SpecialPageLanguage::changePageLanguage(
48 $context, Title::newFromText( $title ), $pageLang[$title], 'Test' );
53 /**
54 * @covers \MediaWiki\Specials\SpecialMyLanguage::findTitle
55 * @dataProvider provideFindTitle
56 * @param string $expected
57 * @param string $subpage
58 * @param string $contLang
59 * @param string $userLang
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()
68 $special->getContext()->setLanguage( $userLang );
69 $this->overrideConfigValue( MainConfigNames::PageLanguageUseDB, true );
70 // Test with subpages both enabled and disabled
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 ) );
77 /**
78 * @param string $expected
79 * @param Title|null $title
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 ) );
90 public static function provideFindTitle() {
91 // See addDBDataOnce() for page declarations
92 return [
93 // [ $expected, $subpage, $contLang, $userLang ]
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' ],
119 * @covers \MediaWiki\Specials\SpecialMyLanguage::findTitleForTransclusion
120 * @dataProvider provideFindTitleForTransclusion
121 * @param string $expected
122 * @param string $subpage
123 * @param string $langCode
124 * @param string $userLang
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()
133 $special->getContext()->setLanguage( $userLang );
134 // Test with subpages both enabled and disabled
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 ) );
141 public static function provideFindTitleForTransclusion() {
142 // See addDBDataOnce() for page declarations
143 return [
144 // [ $expected, $subpage, $langCode, $userLang ]
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' ],