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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
<?php
class LanguageConverterTest extends MediaWikiLangTestCase {
/** @var LanguageToTest */
protected $lang = null;
/** @var TestConverter */
protected $lc = null;
protected function setUp() {
parent::setUp();
$this->setMwGlobals( array(
'wgContLang' => Language::factory( 'tg' ),
'wgLanguageCode' => 'tg',
'wgDefaultLanguageVariant' => false,
'wgMemc' => new EmptyBagOStuff,
'wgRequest' => new FauxRequest( array() ),
'wgUser' => new User,
) );
$this->lang = new LanguageToTest();
$this->lc = new TestConverter(
$this->lang, 'tg',
array( 'tg', 'tg-latn' )
);
}
protected function tearDown() {
unset( $this->lc );
unset( $this->lang );
parent::tearDown();
}
/**
* @covers LanguageConverter::getPreferredVariant
*/
public function testGetPreferredVariantDefaults() {
$this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
}
/**
* @covers LanguageConverter::getPreferredVariant
* @covers LanguageConverter::getHeaderVariant
*/
public function testGetPreferredVariantHeaders() {
global $wgRequest;
$wgRequest->setHeader( 'Accept-Language', 'tg-latn' );
$this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
}
/**
* @covers LanguageConverter::getPreferredVariant
* @covers LanguageConverter::getHeaderVariant
*/
public function testGetPreferredVariantHeaderWeight() {
global $wgRequest;
$wgRequest->setHeader( 'Accept-Language', 'tg;q=1' );
$this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
}
/**
* @covers LanguageConverter::getPreferredVariant
* @covers LanguageConverter::getHeaderVariant
*/
public function testGetPreferredVariantHeaderWeight2() {
global $wgRequest;
$wgRequest->setHeader( 'Accept-Language', 'tg-latn;q=1' );
$this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
}
/**
* @covers LanguageConverter::getPreferredVariant
* @covers LanguageConverter::getHeaderVariant
*/
public function testGetPreferredVariantHeaderMulti() {
global $wgRequest;
$wgRequest->setHeader( 'Accept-Language', 'en, tg-latn;q=1' );
$this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
}
/**
* @covers LanguageConverter::getPreferredVariant
*/
public function testGetPreferredVariantUserOption() {
global $wgUser;
$wgUser = new User;
$wgUser->load(); // from 'defaults'
$wgUser->mId = 1;
$wgUser->mDataLoaded = true;
$wgUser->mOptionsLoaded = true;
$wgUser->setOption( 'variant', 'tg-latn' );
$this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
}
/**
* @covers LanguageConverter::getPreferredVariant
* @covers LanguageConverter::getUserVariant
*/
public function testGetPreferredVariantUserOptionForForeignLanguage() {
global $wgContLang, $wgUser;
$wgContLang = Language::factory( 'en' );
$wgUser = new User;
$wgUser->load(); // from 'defaults'
$wgUser->mId = 1;
$wgUser->mDataLoaded = true;
$wgUser->mOptionsLoaded = true;
$wgUser->setOption( 'variant-tg', 'tg-latn' );
$this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
}
/**
* @covers LanguageConverter::getPreferredVariant
* @covers LanguageConverter::getUserVariant
* @covers LanguageConverter::getURLVariant
*/
public function testGetPreferredVariantHeaderUserVsUrl() {
global $wgContLang, $wgRequest, $wgUser;
$wgContLang = Language::factory( 'tg-latn' );
$wgRequest->setVal( 'variant', 'tg' );
$wgUser = User::newFromId( "admin" );
$wgUser->setId( 1 );
$wgUser->mFrom = 'defaults';
$wgUser->mOptionsLoaded = true;
// The user's data is ignored because the variant is set in the URL.
$wgUser->setOption( 'variant', 'tg-latn' );
$this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
}
/**
* @covers LanguageConverter::getPreferredVariant
*/
public function testGetPreferredVariantDefaultLanguageVariant() {
global $wgDefaultLanguageVariant;
$wgDefaultLanguageVariant = 'tg-latn';
$this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
}
/**
* @covers LanguageConverter::getPreferredVariant
* @covers LanguageConverter::getURLVariant
*/
public function testGetPreferredVariantDefaultLanguageVsUrlVariant() {
global $wgDefaultLanguageVariant, $wgRequest, $wgContLang;
$wgContLang = Language::factory( 'tg-latn' );
$wgDefaultLanguageVariant = 'tg';
$wgRequest->setVal( 'variant', null );
$this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
}
}
/**
* Test converter (from Tajiki to latin orthography)
*/
class TestConverter extends LanguageConverter {
private $table = array(
'б' => 'b',
'в' => 'v',
'г' => 'g',
);
function loadDefaultTables() {
$this->mTables = array(
'tg-latn' => new ReplacementArray( $this->table ),
'tg' => new ReplacementArray()
);
}
}
class LanguageToTest extends Language {
function __construct() {
parent::__construct();
$variants = array( 'tg', 'tg-latn' );
$this->mConverter = new TestConverter( $this, 'tg', $variants );
}
}
|