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