| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Context\RequestContext; |
| 4 |
use MediaWiki\Language\Language; |
| 5 |
use MediaWiki\Linker\LinkTarget; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
use MediaWiki\Page\PageReference; |
| 8 |
use MediaWiki\Page\PageReferenceValue; |
| 9 |
use MediaWiki\Title\Title; |
| 10 |
use MediaWiki\Title\TitleValue; |
| 11 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 12 |
use MediaWiki\User\User; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class LanguageConverterTest extends MediaWikiLangTestCase { |
| 19 |
|
| 20 |
|
| 21 |
protected $lang; |
| 22 |
|
| 23 |
|
| 24 |
protected $lc; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
private function setContextUser( User $user ) { |
| 30 |
|
| 31 |
|
| 32 |
RequestContext::getMain()->setUser( $user ); |
| 33 |
} |
| 34 |
|
| 35 |
protected function setUp(): void { |
| 36 |
parent::setUp(); |
| 37 |
$this->overrideConfigValues( [ |
| 38 |
MainConfigNames::LanguageCode => 'en', |
| 39 |
MainConfigNames::DefaultLanguageVariant => false, |
| 40 |
] ); |
| 41 |
$this->setContentLang( 'tg' ); |
| 42 |
$this->setContextUser( new User ); |
| 43 |
|
| 44 |
$this->lang = $this->createNoOpMock( Language::class, [ 'factory', 'getNsText', 'ucfirst' ] ); |
| 45 |
$this->lang->method( 'getNsText' )->with( NS_MEDIAWIKI )->willReturn( 'MediaWiki' ); |
| 46 |
$this->lang->method( 'ucfirst' )->willReturnCallback( 'ucfirst' ); |
| 47 |
$this->lc = new DummyConverter( $this->lang ); |
| 48 |
} |
| 49 |
|
| 50 |
protected function tearDown(): void { |
| 51 |
unset( $this->lc ); |
| 52 |
unset( $this->lang ); |
| 53 |
|
| 54 |
parent::tearDown(); |
| 55 |
} |
| 56 |
|
| 57 |
public function testGetPreferredVariantDefaults() { |
| 58 |
$this->assertEquals( 'tg', $this->lc->getPreferredVariant() ); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function testGetPreferredVariant( $requestVal, $expected ) { |
| 65 |
$request = RequestContext::getMain()->getRequest(); |
| 66 |
$request->setVal( 'variant', $requestVal ); |
| 67 |
|
| 68 |
$this->assertEquals( $expected, $this->lc->getPreferredVariant() ); |
| 69 |
} |
| 70 |
|
| 71 |
public static function provideGetPreferredVariant() { |
| 72 |
yield 'normal (tg-latn)' => [ 'tg-latn', 'tg-latn' ]; |
| 73 |
yield 'deprecated (bat-smg)' => [ 'bat-smg', 'sgs' ]; |
| 74 |
yield 'BCP47 (en-simple)' => [ 'en-simple', 'simple' ]; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function testGetPreferredVariantHeaders( $headerVal, $expected ) { |
| 81 |
$request = RequestContext::getMain()->getRequest(); |
| 82 |
$request->setHeader( 'Accept-Language', $headerVal ); |
| 83 |
|
| 84 |
$this->assertEquals( $expected, $this->lc->getPreferredVariant() ); |
| 85 |
} |
| 86 |
|
| 87 |
public static function provideGetPreferredVariantHeaders() { |
| 88 |
yield 'normal (tg-latn)' => [ 'tg-latn', 'tg-latn' ]; |
| 89 |
yield 'BCP47 (en-simple)' => [ 'en-simple', 'simple' ]; |
| 90 |
yield 'with weight #1' => [ 'tg;q=1', 'tg' ]; |
| 91 |
yield 'with weight #2' => [ 'tg-latn;q=1', 'tg-latn' ]; |
| 92 |
yield 'with multi' => [ 'en, tg-latn;q=1', 'tg-latn' ]; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public function testGetPreferredVariantUserOption( $optionVal, $expected, $foreignLang ) { |
| 99 |
$optionName = 'variant'; |
| 100 |
if ( $foreignLang ) { |
| 101 |
$this->setContentLang( 'en' ); |
| 102 |
$optionName = 'variant-tg'; |
| 103 |
} |
| 104 |
|
| 105 |
$user = new User; |
| 106 |
$user->load(); |
| 107 |
$user->mId = 1; |
| 108 |
$user->mDataLoaded = true; |
| 109 |
|
| 110 |
$userOptionsLookup = $this->createMock( UserOptionsLookup::class ); |
| 111 |
$userOptionsLookup->method( 'getOption' ) |
| 112 |
->with( $user, $optionName ) |
| 113 |
->willReturn( $optionVal ); |
| 114 |
$this->setService( 'UserOptionsLookup', $userOptionsLookup ); |
| 115 |
|
| 116 |
$this->setContextUser( $user ); |
| 117 |
|
| 118 |
$this->assertEquals( $expected, $this->lc->getPreferredVariant() ); |
| 119 |
} |
| 120 |
|
| 121 |
public static function provideGetPreferredVariantUserOption() { |
| 122 |
yield 'normal (tg-latn)' => [ 'tg-latn', 'tg-latn', false ]; |
| 123 |
yield 'deprecated (bat-smg)' => [ 'bat-smg', 'sgs', false ]; |
| 124 |
yield 'BCP47 (en-simple)' => [ 'en-simple', 'simple', false ]; |
| 125 |
yield 'for foreign language, normal (tg-latn)' => [ 'tg-latn', 'tg-latn', true ]; |
| 126 |
yield 'for foreign language, deprecated (bat-smg)' => [ 'bat-smg', 'sgs', true ]; |
| 127 |
yield 'for foreign language, BCP47 (en-simple)' => [ 'en-simple', 'simple', true ]; |
| 128 |
} |
| 129 |
|
| 130 |
public function testGetPreferredVariantHeaderUserVsUrl() { |
| 131 |
$request = RequestContext::getMain()->getRequest(); |
| 132 |
|
| 133 |
$this->setContentLang( 'tg-latn' ); |
| 134 |
$request->setVal( 'variant', 'tg' ); |
| 135 |
|
| 136 |
$user = User::newFromId( "admin" ); |
| 137 |
$user->setId( 1 ); |
| 138 |
$user->mFrom = 'defaults'; |
| 139 |
|
| 140 |
$userOptionsLookup = $this->createMock( UserOptionsLookup::class ); |
| 141 |
$userOptionsLookup->method( 'getOption' ) |
| 142 |
->with( $user, 'variant' ) |
| 143 |
->willReturn( 'tg-latn' ); |
| 144 |
$this->setService( 'UserOptionsLookup', $userOptionsLookup ); |
| 145 |
|
| 146 |
$this->setContextUser( $user ); |
| 147 |
|
| 148 |
$this->assertEquals( 'tg', $this->lc->getPreferredVariant() ); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public function testGetPreferredVariantDefaultLanguageVariant( $globalVal, $expected ) { |
| 155 |
$this->overrideConfigValue( MainConfigNames::DefaultLanguageVariant, $globalVal ); |
| 156 |
$this->assertEquals( $expected, $this->lc->getPreferredVariant() ); |
| 157 |
} |
| 158 |
|
| 159 |
public function testGetPreferredVariantDefaultLanguageVsUrlVariant() { |
| 160 |
$request = RequestContext::getMain()->getRequest(); |
| 161 |
|
| 162 |
$this->setContentLang( 'tg-latn' ); |
| 163 |
$this->overrideConfigValue( MainConfigNames::DefaultLanguageVariant, 'tg' ); |
| 164 |
$request->setVal( 'variant', null ); |
| 165 |
$this->assertEquals( 'tg', $this->lc->getPreferredVariant() ); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public function testAutoConvertT124404() { |
| 172 |
$testString = str_repeat( 'xxx xxx xxx', 1000 ); |
| 173 |
$testString .= "\n<big id='в'></big>"; |
| 174 |
$this->setIniSetting( 'pcre.backtrack_limit', 200 ); |
| 175 |
$result = $this->lc->autoConvert( $testString, 'tg-latn' ); |
| 176 |
v |
| 177 |
$this->assertStringNotContainsString( |
| 178 |
'v', |
| 179 |
$result, |
| 180 |
"в converted to v despite being in attribue" |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function testConvertTitle( $title, string $expected ): void { |
| 190 |
if ( is_callable( $title ) ) { |
| 191 |
$title = $title(); |
| 192 |
} |
| 193 |
$actual = $this->lc->convertTitle( $title ); |
| 194 |
$this->assertSame( $expected, $actual ); |
| 195 |
} |
| 196 |
|
| 197 |
public static function provideTitlesToConvert(): array { |
| 198 |
return [ |
| 199 |
'Title FromText default' => [ |
| 200 |
Title::makeTitle( NS_MAIN, 'Dummy_title' ), |
| 201 |
'Dummy title', |
| 202 |
], |
| 203 |
'Title FromText with NS' => [ |
| 204 |
Title::makeTitle( NS_FILE, 'Dummy_title' ), |
| 205 |
'Акс:Dummy title', |
| 206 |
], |
| 207 |
'Title MainPage default' => [ |
| 208 |
static function () { |
| 209 |
|
| 210 |
return Title::newMainPage(); |
| 211 |
}, |
| 212 |
'Саҳифаи аслӣ', |
| 213 |
], |
| 214 |
'Title MainPage with MessageLocalizer' => [ |
| 215 |
static function () { |
| 216 |
|
| 217 |
return Title::newMainPage( new MockMessageLocalizer() ); |
| 218 |
}, |
| 219 |
'Саҳифаи аслӣ', |
| 220 |
], |
| 221 |
'TitleValue' => [ |
| 222 |
new TitleValue( NS_FILE, 'Dummy page' ), |
| 223 |
'Акс:Dummy page', |
| 224 |
], |
| 225 |
'PageReference' => [ |
| 226 |
new PageReferenceValue( NS_FILE, 'Dummy page', PageReference::LOCAL ), |
| 227 |
'Акс:Dummy page', |
| 228 |
], |
| 229 |
]; |
| 230 |
} |
| 231 |
} |