Compress images
[mediawiki.git] / tests / phpunit / includes / LanguageConverterTest.php
blobbaf28b07fa9274c51851e9de606962890a6b8fa0
1 <?php
3 class LanguageConverterTest extends MediaWikiLangTestCase {
4 protected $lang = null;
5 protected $lc = null;
7 function setUp() {
8 parent::setUp();
9 global $wgMemc, $wgRequest, $wgUser, $wgContLang;
11 $wgUser = new User;
12 $wgRequest = new FauxRequest( array() );
13 $wgMemc = new EmptyBagOStuff;
14 $wgContLang = Language::factory( 'tg' );
15 $this->lang = new LanguageToTest();
16 $this->lc = new TestConverter( $this->lang, 'tg',
17 array( 'tg', 'tg-latn' ) );
20 function tearDown() {
21 global $wgMemc;
22 unset( $wgMemc );
23 unset( $this->lc );
24 unset( $this->lang );
25 parent::tearDown();
28 function testGetPreferredVariantDefaults() {
29 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
32 function testGetPreferredVariantHeaders() {
33 global $wgRequest;
34 $wgRequest->setHeader( 'Accept-Language', 'tg-latn' );
36 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
39 function testGetPreferredVariantHeaderWeight() {
40 global $wgRequest;
41 $wgRequest->setHeader( 'Accept-Language', 'tg;q=1' );
43 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
46 function testGetPreferredVariantHeaderWeight2() {
47 global $wgRequest;
48 $wgRequest->setHeader( 'Accept-Language', 'tg-latn;q=1' );
50 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
53 function testGetPreferredVariantHeaderMulti() {
54 global $wgRequest;
55 $wgRequest->setHeader( 'Accept-Language', 'en, tg-latn;q=1' );
57 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
60 function testGetPreferredVariantUserOption() {
61 global $wgUser;
63 $wgUser = new User;
64 $wgUser->load(); // from 'defaults'
65 $wgUser->mId = 1;
66 $wgUser->mDataLoaded = true;
67 $wgUser->mOptionsLoaded = true;
68 $wgUser->setOption( 'variant', 'tg-latn' );
70 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
73 function testGetPreferredVariantHeaderUserVsUrl() {
74 global $wgRequest, $wgUser, $wgContLang;
76 $wgContLang = Language::factory( 'tg-latn' );
77 $wgRequest->setVal( 'variant', 'tg' );
78 $wgUser = User::newFromId( "admin" );
79 $wgUser->setId( 1 );
80 $wgUser->mFrom = 'defaults';
81 $wgUser->mOptionsLoaded = true;
82 $wgUser->setOption( 'variant', 'tg-latn' ); // The user's data is ignored
83 // because the variant is set in the URL.
84 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
88 function testGetPreferredVariantDefaultLanguageVariant() {
89 global $wgDefaultLanguageVariant;
91 $wgDefaultLanguageVariant = 'tg-latn';
92 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
95 function testGetPreferredVariantDefaultLanguageVsUrlVariant() {
96 global $wgDefaultLanguageVariant, $wgRequest, $wgContLang;
98 $wgContLang = Language::factory( 'tg-latn' );
99 $wgDefaultLanguageVariant = 'tg';
100 $wgRequest->setVal( 'variant', null );
101 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
106 * Test converter (from Tajiki to latin orthography)
108 class TestConverter extends LanguageConverter {
109 private $table = array(
110 'б' => 'b',
111 'в' => 'v',
112 'г' => 'g',
115 function loadDefaultTables() {
116 $this->mTables = array(
117 'tg-latn' => new ReplacementArray( $this->table ),
118 'tg' => new ReplacementArray()
124 class LanguageToTest extends Language {
125 function __construct() {
126 parent::__construct();
127 $variants = array( 'tg', 'tg-latn' );
128 $this->mConverter = new TestConverter( $this, 'tg', $variants );