Clean and repair many phpunit tests (+ fix implied configuration)
[lhc/web/wiklou.git] / tests / phpunit / includes / LanguageConverterTest.php
1 <?php
2
3 class LanguageConverterTest extends MediaWikiLangTestCase {
4 protected $lang = null;
5 protected $lc = null;
6
7 protected function setUp() {
8 parent::setUp();
9
10 $this->setMwGlobals( array(
11 'wgContLang' => Language::factory( 'tg' ),
12 'wgDefaultLanguageVariant' => false,
13 'wgMemc' => new EmptyBagOStuff,
14 'wgRequest' => new FauxRequest( array() ),
15 'wgUser' => new User,
16 ) );
17
18 $this->lang = new LanguageToTest();
19 $this->lc = new TestConverter(
20 $this->lang, 'tg',
21 array( 'tg', 'tg-latn' )
22 );
23 }
24
25 protected function tearDown() {
26 unset( $this->lc );
27 unset( $this->lang );
28
29 parent::tearDown();
30 }
31
32 function testGetPreferredVariantDefaults() {
33 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
34 }
35
36 function testGetPreferredVariantHeaders() {
37 global $wgRequest;
38 $wgRequest->setHeader( 'Accept-Language', 'tg-latn' );
39
40 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
41 }
42
43 function testGetPreferredVariantHeaderWeight() {
44 global $wgRequest;
45 $wgRequest->setHeader( 'Accept-Language', 'tg;q=1' );
46
47 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
48 }
49
50 function testGetPreferredVariantHeaderWeight2() {
51 global $wgRequest;
52 $wgRequest->setHeader( 'Accept-Language', 'tg-latn;q=1' );
53
54 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
55 }
56
57 function testGetPreferredVariantHeaderMulti() {
58 global $wgRequest;
59 $wgRequest->setHeader( 'Accept-Language', 'en, tg-latn;q=1' );
60
61 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
62 }
63
64 function testGetPreferredVariantUserOption() {
65 global $wgUser;
66
67 $wgUser = new User;
68 $wgUser->load(); // from 'defaults'
69 $wgUser->mId = 1;
70 $wgUser->mDataLoaded = true;
71 $wgUser->mOptionsLoaded = true;
72 $wgUser->setOption( 'variant', 'tg-latn' );
73
74 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
75 }
76
77 function testGetPreferredVariantHeaderUserVsUrl() {
78 global $wgContLang, $wgRequest, $wgUser;
79
80 $wgContLang = Language::factory( 'tg-latn' );
81 $wgRequest->setVal( 'variant', 'tg' );
82 $wgUser = User::newFromId( "admin" );
83 $wgUser->setId( 1 );
84 $wgUser->mFrom = 'defaults';
85 $wgUser->mOptionsLoaded = true;
86 $wgUser->setOption( 'variant', 'tg-latn' ); // The user's data is ignored
87 // because the variant is set in the URL.
88 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
89 }
90
91
92 function testGetPreferredVariantDefaultLanguageVariant() {
93 global $wgDefaultLanguageVariant;
94
95 $wgDefaultLanguageVariant = 'tg-latn';
96 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
97 }
98
99 function testGetPreferredVariantDefaultLanguageVsUrlVariant() {
100 global $wgDefaultLanguageVariant, $wgRequest, $wgContLang;
101
102 $wgContLang = Language::factory( 'tg-latn' );
103 $wgDefaultLanguageVariant = 'tg';
104 $wgRequest->setVal( 'variant', null );
105 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
106 }
107 }
108
109 /**
110 * Test converter (from Tajiki to latin orthography)
111 */
112 class TestConverter extends LanguageConverter {
113 private $table = array(
114 'б' => 'b',
115 'в' => 'v',
116 'г' => 'g',
117 );
118
119 function loadDefaultTables() {
120 $this->mTables = array(
121 'tg-latn' => new ReplacementArray( $this->table ),
122 'tg' => new ReplacementArray()
123 );
124 }
125
126 }
127
128 class LanguageToTest extends Language {
129 function __construct() {
130 parent::__construct();
131 $variants = array( 'tg', 'tg-latn' );
132 $this->mConverter = new TestConverter( $this, 'tg', $variants );
133 }
134 }