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.

188 lines
5.5 KiB

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