You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
5.3 KiB

13 years ago
  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-31
  8. *
  9. * Unit tests for I18N class
  10. */
  11. require_once dirname(__FILE__) . '/../../Registry.php';
  12. require_once dirname(__FILE__) . '/../../Config.php';
  13. require_once dirname(__FILE__) . '/../../classes/Env.class.php';
  14. require_once dirname(__FILE__) . '/../../i18n/I18N.php';
  15. /**
  16. * @runTestsInSeparateProcesses
  17. */
  18. class I18NTest extends PHPUnit_Framework_TestCase
  19. {
  20. public function run(PHPUnit_Framework_TestResult $result = NULL)
  21. {
  22. $this->setPreserveGlobalState(false);
  23. return parent::run($result);
  24. }
  25. /**
  26. * @runInSeparateProcess
  27. */
  28. public function testInit()
  29. {
  30. $this->setConstants();
  31. Config::set('I18N', array('locales' => array('ru' => 'ru-ru', 'us' => 'en-us')));
  32. I18N::init();
  33. $this->assertAttributeEquals(array('ru' => 'ru-ru', 'us' => 'en-us'), 'locales', 'I18N');
  34. $this->assertAttributeEquals('ru-ru', 'locale', 'I18N');
  35. }
  36. /**
  37. * @expectedException Exception
  38. */
  39. public function testInitNoConfig()
  40. {
  41. I18N::init();
  42. }
  43. /**
  44. * @runInSeparateProcess
  45. */
  46. public function testInitFullConfig()
  47. {
  48. $this->setConstants();
  49. Config::set('I18N', array('locales' => array('ru' => 'ru-ru', 'en' => 'en_US'), 'bidi' => 'bidivalue', 'default' => 'ru'));
  50. I18N::init();
  51. $this->assertAttributeEquals('ru-ru', 'locale', 'I18N');
  52. $this->assertAttributeEquals('bidivalue', 'bidi', 'I18N');
  53. $this->assertAttributeEquals('ru', 'default', 'I18N');
  54. }
  55. /**
  56. * @runInSeparateProcess
  57. */
  58. public function testInitPostLangSet()
  59. {
  60. $this->setConstants();
  61. $_POST['lang'] = 'en';
  62. $_COOKIE['lang'] = 'en';
  63. Config::set('I18N', array('locales' => array('ru' => 'ru-ru', 'en' => 'en_US'), 'bidi' => 'bidivalue', 'default' => 'ru'));
  64. set_exit_overload(function() { return FALSE; });
  65. I18N::init();
  66. $this->assertAttributeEquals('en_US', 'locale', 'I18N');
  67. unset_exit_overload();
  68. }
  69. /**
  70. * @runInSeparateProcess
  71. * @expectedException PHPUnit_Framework_Error
  72. */
  73. public function testInitSetDefaultLangNotInLocales()
  74. {
  75. $this->setConstants();
  76. Config::set('I18N', array('locales' => array('rus' => 'ru_RU'), 'default' => 'ru'));
  77. I18N::init();
  78. $this->assertAttributeEquals('ru_RU', 'locale', 'I18N');
  79. }
  80. /**
  81. * @runInSeparateProcess
  82. */
  83. public function testInitServerLangConfigured()
  84. {
  85. $this->setConstants();
  86. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'ru';
  87. Config::set('I18N', array('locales' => array('ru_ru' => 'ru_RU', 'ru' => 'ru-ru', 'us' => 'en-us')));
  88. I18N::init();
  89. $this->assertAttributeEquals('ru-ru', 'locale', 'I18N');
  90. }
  91. /**
  92. * @runInSeparateProcess
  93. */
  94. public function testInitServerLangConfiguredWithWeights()
  95. {
  96. $this->setConstants();
  97. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3';
  98. Config::set('I18N', array('locales' => array('en_US' => 'en-us')));
  99. I18N::init();
  100. $this->assertAttributeEquals('en-us', 'locale', 'I18N');
  101. }
  102. /**
  103. * @runInSeparateProcess
  104. */
  105. public function testInitServerLangConfiguredWithWeightsNoLocale()
  106. {
  107. $this->setConstants();
  108. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3';
  109. Config::set('I18N', array('locales' => array('en' => 'en_US')));
  110. I18N::init();
  111. $this->assertAttributeEquals('en_US', 'locale', 'I18N');
  112. }
  113. /**
  114. * @runInSeparateProcess
  115. */
  116. public function testGetLang()
  117. {
  118. $this->setConstants();
  119. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3';
  120. Config::set('I18N', array('locales' => array('en' => 'en_US'), 'default' => 'ru'));
  121. I18N::init();
  122. $this->assertEquals('en', I18N::getLang());
  123. $this->assertEquals('ru', I18N::getDefaultLang());
  124. }
  125. /**
  126. * @runInSeparateProcess
  127. */
  128. public function testLangs()
  129. {
  130. $this->setConstants();
  131. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3';
  132. Config::set('I18N', array('locales' => array('ru' => 'ru-ru'), 'default' => 'ru'));
  133. I18N::init();
  134. I18N::setLangs(array('en' => 'en-us', 'fr' => 'fr-fr'));
  135. $this->assertEquals(array('en' => 'en-us', 'fr' => 'fr-fr'), I18N::getLangs());
  136. }
  137. /**
  138. * @runInSeparateProcess
  139. */
  140. public function testLangName()
  141. {
  142. $this->setConstants();
  143. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3';
  144. Config::set('I18N', array('locales' => array('ru' => 'ru-ru'), 'default' => 'ru'));
  145. I18N::init();
  146. I18N::setLangs(array('ru' => 'ru_ru', 'en' => 'en-us', 'fr' => 'fr-fr'));
  147. $this->assertEquals('ru_ru', I18N::getLangName());
  148. $this->assertFalse(I18N::getLangName('br'));
  149. }
  150. private function setConstants()
  151. {
  152. if (!defined('PATH')) {
  153. define('PATH', '/some/path/');
  154. }
  155. if (!defined('APP')) {
  156. define('APP', '/some/app/');
  157. }
  158. }
  159. }