Update docs/hooks.txt for ShowSearchHitTitle
[mediawiki.git] / tests / phpunit / languages / LanguageConverterTest.php
blob81184aaadb9da0d826b52631b912db7bac42bb84
1 <?php
3 class LanguageConverterTest extends MediaWikiLangTestCase {
4 /** @var LanguageToTest */
5 protected $lang = null;
6 /** @var TestConverter */
7 protected $lc = null;
9 protected function setUp() {
10 parent::setUp();
12 $this->setMwGlobals( [
13 'wgContLang' => Language::factory( 'tg' ),
14 'wgLanguageCode' => 'tg',
15 'wgDefaultLanguageVariant' => false,
16 'wgRequest' => new FauxRequest( [] ),
17 'wgUser' => new User,
18 ] );
20 $this->lang = new LanguageToTest();
21 $this->lc = new TestConverter(
22 $this->lang, 'tg',
23 [ 'tg', 'tg-latn' ]
27 protected function tearDown() {
28 unset( $this->lc );
29 unset( $this->lang );
31 parent::tearDown();
34 /**
35 * @covers LanguageConverter::getPreferredVariant
37 public function testGetPreferredVariantDefaults() {
38 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
41 /**
42 * @covers LanguageConverter::getPreferredVariant
43 * @covers LanguageConverter::getHeaderVariant
45 public function testGetPreferredVariantHeaders() {
46 global $wgRequest;
47 $wgRequest->setHeader( 'Accept-Language', 'tg-latn' );
49 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
52 /**
53 * @covers LanguageConverter::getPreferredVariant
54 * @covers LanguageConverter::getHeaderVariant
56 public function testGetPreferredVariantHeaderWeight() {
57 global $wgRequest;
58 $wgRequest->setHeader( 'Accept-Language', 'tg;q=1' );
60 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
63 /**
64 * @covers LanguageConverter::getPreferredVariant
65 * @covers LanguageConverter::getHeaderVariant
67 public function testGetPreferredVariantHeaderWeight2() {
68 global $wgRequest;
69 $wgRequest->setHeader( 'Accept-Language', 'tg-latn;q=1' );
71 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
74 /**
75 * @covers LanguageConverter::getPreferredVariant
76 * @covers LanguageConverter::getHeaderVariant
78 public function testGetPreferredVariantHeaderMulti() {
79 global $wgRequest;
80 $wgRequest->setHeader( 'Accept-Language', 'en, tg-latn;q=1' );
82 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
85 /**
86 * @covers LanguageConverter::getPreferredVariant
88 public function testGetPreferredVariantUserOption() {
89 global $wgUser;
91 $wgUser = new User;
92 $wgUser->load(); // from 'defaults'
93 $wgUser->mId = 1;
94 $wgUser->mDataLoaded = true;
95 $wgUser->mOptionsLoaded = true;
96 $wgUser->setOption( 'variant', 'tg-latn' );
98 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
102 * @covers LanguageConverter::getPreferredVariant
103 * @covers LanguageConverter::getUserVariant
105 public function testGetPreferredVariantUserOptionForForeignLanguage() {
106 global $wgContLang, $wgUser;
108 $wgContLang = Language::factory( 'en' );
109 $wgUser = new User;
110 $wgUser->load(); // from 'defaults'
111 $wgUser->mId = 1;
112 $wgUser->mDataLoaded = true;
113 $wgUser->mOptionsLoaded = true;
114 $wgUser->setOption( 'variant-tg', 'tg-latn' );
116 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
120 * @covers LanguageConverter::getPreferredVariant
121 * @covers LanguageConverter::getUserVariant
122 * @covers LanguageConverter::getURLVariant
124 public function testGetPreferredVariantHeaderUserVsUrl() {
125 global $wgContLang, $wgRequest, $wgUser;
127 $wgContLang = Language::factory( 'tg-latn' );
128 $wgRequest->setVal( 'variant', 'tg' );
129 $wgUser = User::newFromId( "admin" );
130 $wgUser->setId( 1 );
131 $wgUser->mFrom = 'defaults';
132 $wgUser->mOptionsLoaded = true;
133 // The user's data is ignored because the variant is set in the URL.
134 $wgUser->setOption( 'variant', 'tg-latn' );
135 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
139 * @covers LanguageConverter::getPreferredVariant
141 public function testGetPreferredVariantDefaultLanguageVariant() {
142 global $wgDefaultLanguageVariant;
144 $wgDefaultLanguageVariant = 'tg-latn';
145 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
149 * @covers LanguageConverter::getPreferredVariant
150 * @covers LanguageConverter::getURLVariant
152 public function testGetPreferredVariantDefaultLanguageVsUrlVariant() {
153 global $wgDefaultLanguageVariant, $wgRequest, $wgContLang;
155 $wgContLang = Language::factory( 'tg-latn' );
156 $wgDefaultLanguageVariant = 'tg';
157 $wgRequest->setVal( 'variant', null );
158 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
163 * Test converter (from Tajiki to latin orthography)
165 class TestConverter extends LanguageConverter {
166 private $table = [
167 'б' => 'b',
168 'в' => 'v',
169 'г' => 'g',
172 function loadDefaultTables() {
173 $this->mTables = [
174 'tg-latn' => new ReplacementArray( $this->table ),
175 'tg' => new ReplacementArray()
180 class LanguageToTest extends Language {
181 function __construct() {
182 parent::__construct();
183 $variants = [ 'tg', 'tg-latn' ];
184 $this->mConverter = new TestConverter( $this, 'tg', $variants );