test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / LanguageConverterTest.php
blob93ce1195a1c7101ac09e8a1be3f54a950cea60cd
1 <?php
3 class LanguageConverterTest extends MediaWikiLangTestCase {
4 protected $lang = null;
5 protected $lc = null;
7 protected function setUp() {
8 parent::setUp();
10 $this->setMwGlobals( array(
11 'wgContLang' => Language::factory( 'tg' ),
12 'wgLanguageCode' => 'tg',
13 'wgDefaultLanguageVariant' => false,
14 'wgMemc' => new EmptyBagOStuff,
15 'wgRequest' => new FauxRequest( array() ),
16 'wgUser' => new User,
17 ) );
19 $this->lang = new LanguageToTest();
20 $this->lc = new TestConverter(
21 $this->lang, 'tg',
22 array( 'tg', 'tg-latn' )
26 protected function tearDown() {
27 unset( $this->lc );
28 unset( $this->lang );
30 parent::tearDown();
33 function testGetPreferredVariantDefaults() {
34 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
37 function testGetPreferredVariantHeaders() {
38 global $wgRequest;
39 $wgRequest->setHeader( 'Accept-Language', 'tg-latn' );
41 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
44 function testGetPreferredVariantHeaderWeight() {
45 global $wgRequest;
46 $wgRequest->setHeader( 'Accept-Language', 'tg;q=1' );
48 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
51 function testGetPreferredVariantHeaderWeight2() {
52 global $wgRequest;
53 $wgRequest->setHeader( 'Accept-Language', 'tg-latn;q=1' );
55 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
58 function testGetPreferredVariantHeaderMulti() {
59 global $wgRequest;
60 $wgRequest->setHeader( 'Accept-Language', 'en, tg-latn;q=1' );
62 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
65 function testGetPreferredVariantUserOption() {
66 global $wgUser;
68 $wgUser = new User;
69 $wgUser->load(); // from 'defaults'
70 $wgUser->mId = 1;
71 $wgUser->mDataLoaded = true;
72 $wgUser->mOptionsLoaded = true;
73 $wgUser->setOption( 'variant', 'tg-latn' );
75 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
78 function testGetPreferredVariantHeaderUserVsUrl() {
79 global $wgContLang, $wgRequest, $wgUser;
81 $wgContLang = Language::factory( 'tg-latn' );
82 $wgRequest->setVal( 'variant', 'tg' );
83 $wgUser = User::newFromId( "admin" );
84 $wgUser->setId( 1 );
85 $wgUser->mFrom = 'defaults';
86 $wgUser->mOptionsLoaded = true;
87 // The user's data is ignored because the variant is set in the URL.
88 $wgUser->setOption( 'variant', 'tg-latn' );
89 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
93 function testGetPreferredVariantDefaultLanguageVariant() {
94 global $wgDefaultLanguageVariant;
96 $wgDefaultLanguageVariant = 'tg-latn';
97 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
100 function testGetPreferredVariantDefaultLanguageVsUrlVariant() {
101 global $wgDefaultLanguageVariant, $wgRequest, $wgContLang;
103 $wgContLang = Language::factory( 'tg-latn' );
104 $wgDefaultLanguageVariant = 'tg';
105 $wgRequest->setVal( 'variant', null );
106 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
111 * Test converter (from Tajiki to latin orthography)
113 class TestConverter extends LanguageConverter {
114 private $table = array(
115 'б' => 'b',
116 'в' => 'v',
117 'г' => 'g',
120 function loadDefaultTables() {
121 $this->mTables = array(
122 'tg-latn' => new ReplacementArray( $this->table ),
123 'tg' => new ReplacementArray()
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 );