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 'wgRequest' => new FauxRequest( array() ),
20 $this->lang
= new LanguageToTest();
21 $this->lc
= new TestConverter(
23 array( 'tg', 'tg-latn' )
27 protected function tearDown() {
35 * @covers LanguageConverter::getPreferredVariant
37 public function testGetPreferredVariantDefaults() {
38 $this->assertEquals( 'tg', $this->lc
->getPreferredVariant() );
42 * @covers LanguageConverter::getPreferredVariant
43 * @covers LanguageConverter::getHeaderVariant
45 public function testGetPreferredVariantHeaders() {
47 $wgRequest->setHeader( 'Accept-Language', 'tg-latn' );
49 $this->assertEquals( 'tg-latn', $this->lc
->getPreferredVariant() );
53 * @covers LanguageConverter::getPreferredVariant
54 * @covers LanguageConverter::getHeaderVariant
56 public function testGetPreferredVariantHeaderWeight() {
58 $wgRequest->setHeader( 'Accept-Language', 'tg;q=1' );
60 $this->assertEquals( 'tg', $this->lc
->getPreferredVariant() );
64 * @covers LanguageConverter::getPreferredVariant
65 * @covers LanguageConverter::getHeaderVariant
67 public function testGetPreferredVariantHeaderWeight2() {
69 $wgRequest->setHeader( 'Accept-Language', 'tg-latn;q=1' );
71 $this->assertEquals( 'tg-latn', $this->lc
->getPreferredVariant() );
75 * @covers LanguageConverter::getPreferredVariant
76 * @covers LanguageConverter::getHeaderVariant
78 public function testGetPreferredVariantHeaderMulti() {
80 $wgRequest->setHeader( 'Accept-Language', 'en, tg-latn;q=1' );
82 $this->assertEquals( 'tg-latn', $this->lc
->getPreferredVariant() );
86 * @covers LanguageConverter::getPreferredVariant
88 public function testGetPreferredVariantUserOption() {
92 $wgUser->load(); // from 'defaults'
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' );
110 $wgUser->load(); // from 'defaults'
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" );
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 = array(
172 function loadDefaultTables() {
173 $this->mTables
= array(
174 'tg-latn' => new ReplacementArray( $this->table
),
175 'tg' => new ReplacementArray()
180 class LanguageToTest
extends Language
{
181 function __construct() {
182 parent
::__construct();
183 $variants = array( 'tg', 'tg-latn' );
184 $this->mConverter
= new TestConverter( $this, 'tg', $variants );