MediaWiki  REL1_19
LanguageConverterTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class LanguageConverterTest extends MediaWikiLangTestCase {
00004         protected $lang = null;
00005         protected $lc = null;
00006 
00007         function setUp() {
00008                 parent::setUp();
00009                 global $wgMemc, $wgRequest, $wgUser, $wgContLang;
00010 
00011                 $wgUser = new User;
00012                 $wgRequest = new FauxRequest( array() );
00013                 $wgMemc = new EmptyBagOStuff;
00014                 $wgContLang = Language::factory( 'tg' );
00015                 $this->lang = new LanguageToTest();
00016                 $this->lc = new TestConverter( $this->lang, 'tg',
00017                                                                            array( 'tg', 'tg-latn' ) );
00018         }
00019 
00020         function tearDown() {
00021                 global $wgMemc;
00022                 unset( $wgMemc );
00023                 unset( $this->lc );
00024                 unset( $this->lang );
00025                 parent::tearDown();
00026         }
00027 
00028         function testGetPreferredVariantDefaults() {
00029                 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
00030         }
00031 
00032         function testGetPreferredVariantHeaders() {
00033                 global $wgRequest;
00034                 $wgRequest->setHeader( 'Accept-Language', 'tg-latn' );
00035 
00036                 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
00037         }
00038 
00039         function testGetPreferredVariantHeaderWeight() {
00040                 global $wgRequest;
00041                 $wgRequest->setHeader( 'Accept-Language', 'tg;q=1' );
00042 
00043                 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
00044         }
00045 
00046         function testGetPreferredVariantHeaderWeight2() {
00047                 global $wgRequest;
00048                 $wgRequest->setHeader( 'Accept-Language', 'tg-latn;q=1' );
00049 
00050                 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
00051         }
00052 
00053         function testGetPreferredVariantHeaderMulti() {
00054                 global $wgRequest;
00055                 $wgRequest->setHeader( 'Accept-Language', 'en, tg-latn;q=1' );
00056 
00057                 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
00058         }
00059 
00060         function testGetPreferredVariantUserOption() {
00061                 global $wgUser;
00062 
00063                 $wgUser = new User;
00064                 $wgUser->load(); // from 'defaults'
00065                 $wgUser->mId = 1;
00066                 $wgUser->mDataLoaded = true;
00067                 $wgUser->mOptionsLoaded = true;
00068                 $wgUser->setOption( 'variant', 'tg-latn' );
00069 
00070                 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
00071         }
00072 
00073         function testGetPreferredVariantHeaderUserVsUrl() {
00074                 global $wgRequest, $wgUser, $wgContLang;
00075 
00076                 $wgContLang = Language::factory( 'tg-latn' );
00077                 $wgRequest->setVal( 'variant', 'tg' );
00078                 $wgUser = User::newFromId( "admin" );
00079                 $wgUser->setId( 1 );
00080                 $wgUser->mFrom = 'defaults';
00081                 $wgUser->mOptionsLoaded = true;
00082                 $wgUser->setOption( 'variant', 'tg-latn' ); // The user's data is ignored
00083                                                                                                   // because the variant is set in the URL.
00084                 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
00085         }
00086 
00087 
00088         function testGetPreferredVariantDefaultLanguageVariant() {
00089                 global $wgDefaultLanguageVariant;
00090 
00091                 $wgDefaultLanguageVariant = 'tg-latn';
00092                 $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
00093         }
00094 
00095         function testGetPreferredVariantDefaultLanguageVsUrlVariant() {
00096                 global $wgDefaultLanguageVariant, $wgRequest, $wgContLang;
00097 
00098                 $wgContLang = Language::factory( 'tg-latn' );
00099                 $wgDefaultLanguageVariant = 'tg';
00100                 $wgRequest->setVal( 'variant', null );
00101                 $this->assertEquals( 'tg', $this->lc->getPreferredVariant() );
00102         }
00103 }
00104 
00108 class TestConverter extends LanguageConverter {
00109         private $table = array(
00110                 'б' => 'b',
00111                 'в' => 'v',
00112                 'г' => 'g',
00113         );
00114 
00115         function loadDefaultTables() {
00116                 $this->mTables = array(
00117                         'tg-latn' => new ReplacementArray( $this->table ),
00118                         'tg'      => new ReplacementArray()
00119                 );
00120         }
00121 
00122 }
00123 
00124 class LanguageToTest extends Language {
00125         function __construct() {
00126                 parent::__construct();
00127                 $variants = array( 'tg', 'tg-latn' );
00128                 $this->mConverter = new TestConverter( $this, 'tg', $variants );
00129         }
00130 }