3 class LanguageConverterTest
extends MediaWikiLangTestCase
{
4 /** @var LanguageToTest */
5 protected $lang = null;
6 /** @var TestConverter */
9 protected function setUp() {
12 $this->setMwGlobals( array(
13 'wgContLang' => Language
::factory( 'tg' ),
14 'wgLanguageCode' => 'tg',
15 'wgDefaultLanguageVariant' => false,
16 'wgMemc' => new EmptyBagOStuff
,
17 'wgRequest' => new FauxRequest( array() ),
21 $this->lang
= new LanguageToTest();
22 $this->lc
= new TestConverter(
24 array( 'tg', 'tg-latn' )
28 protected function tearDown() {
36 * @covers LanguageConverter::getPreferredVariant
38 public function testGetPreferredVariantDefaults() {
39 $this->assertEquals( 'tg', $this->lc
->getPreferredVariant() );
43 * @covers LanguageConverter::getPreferredVariant
44 * @covers LanguageConverter::getHeaderVariant
46 public function testGetPreferredVariantHeaders() {
48 $wgRequest->setHeader( 'Accept-Language', 'tg-latn' );
50 $this->assertEquals( 'tg-latn', $this->lc
->getPreferredVariant() );
54 * @covers LanguageConverter::getPreferredVariant
55 * @covers LanguageConverter::getHeaderVariant
57 public function testGetPreferredVariantHeaderWeight() {
59 $wgRequest->setHeader( 'Accept-Language', 'tg;q=1' );
61 $this->assertEquals( 'tg', $this->lc
->getPreferredVariant() );
65 * @covers LanguageConverter::getPreferredVariant
66 * @covers LanguageConverter::getHeaderVariant
68 public function testGetPreferredVariantHeaderWeight2() {
70 $wgRequest->setHeader( 'Accept-Language', 'tg-latn;q=1' );
72 $this->assertEquals( 'tg-latn', $this->lc
->getPreferredVariant() );
76 * @covers LanguageConverter::getPreferredVariant
77 * @covers LanguageConverter::getHeaderVariant
79 public function testGetPreferredVariantHeaderMulti() {
81 $wgRequest->setHeader( 'Accept-Language', 'en, tg-latn;q=1' );
83 $this->assertEquals( 'tg-latn', $this->lc
->getPreferredVariant() );
87 * @covers LanguageConverter::getPreferredVariant
89 public function testGetPreferredVariantUserOption() {
93 $wgUser->load(); // from 'defaults'
95 $wgUser->mDataLoaded
= true;
96 $wgUser->mOptionsLoaded
= true;
97 $wgUser->setOption( 'variant', 'tg-latn' );
99 $this->assertEquals( 'tg-latn', $this->lc
->getPreferredVariant() );
103 * @covers LanguageConverter::getPreferredVariant
104 * @covers LanguageConverter::getUserVariant
106 public function testGetPreferredVariantUserOptionForForeignLanguage() {
107 global $wgContLang, $wgUser;
109 $wgContLang = Language
::factory( 'en' );
111 $wgUser->load(); // from 'defaults'
113 $wgUser->mDataLoaded
= true;
114 $wgUser->mOptionsLoaded
= true;
115 $wgUser->setOption( 'variant-tg', 'tg-latn' );
117 $this->assertEquals( 'tg-latn', $this->lc
->getPreferredVariant() );
121 * @covers LanguageConverter::getPreferredVariant
122 * @covers LanguageConverter::getUserVariant
123 * @covers LanguageConverter::getURLVariant
125 public function testGetPreferredVariantHeaderUserVsUrl() {
126 global $wgContLang, $wgRequest, $wgUser;
128 $wgContLang = Language
::factory( 'tg-latn' );
129 $wgRequest->setVal( 'variant', 'tg' );
130 $wgUser = User
::newFromId( "admin" );
132 $wgUser->mFrom
= 'defaults';
133 $wgUser->mOptionsLoaded
= true;
134 // The user's data is ignored because the variant is set in the URL.
135 $wgUser->setOption( 'variant', 'tg-latn' );
136 $this->assertEquals( 'tg', $this->lc
->getPreferredVariant() );
140 * @covers LanguageConverter::getPreferredVariant
142 public function testGetPreferredVariantDefaultLanguageVariant() {
143 global $wgDefaultLanguageVariant;
145 $wgDefaultLanguageVariant = 'tg-latn';
146 $this->assertEquals( 'tg-latn', $this->lc
->getPreferredVariant() );
150 * @covers LanguageConverter::getPreferredVariant
151 * @covers LanguageConverter::getURLVariant
153 public function testGetPreferredVariantDefaultLanguageVsUrlVariant() {
154 global $wgDefaultLanguageVariant, $wgRequest, $wgContLang;
156 $wgContLang = Language
::factory( 'tg-latn' );
157 $wgDefaultLanguageVariant = 'tg';
158 $wgRequest->setVal( 'variant', null );
159 $this->assertEquals( 'tg', $this->lc
->getPreferredVariant() );
164 * Test converter (from Tajiki to latin orthography)
166 class TestConverter
extends LanguageConverter
{
167 private $table = array(
173 function loadDefaultTables() {
174 $this->mTables
= array(
175 'tg-latn' => new ReplacementArray( $this->table
),
176 'tg' => new ReplacementArray()
181 class LanguageToTest
extends Language
{
182 function __construct() {
183 parent
::__construct();
184 $variants = array( 'tg', 'tg-latn' );
185 $this->mConverter
= new TestConverter( $this, 'tg', $variants );