TranslateTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_View_Helper_TranslateTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_TranslateTest::main");
  25. }
  26. /** Zend_View_Helper_Translate */
  27. require_once 'Zend/View/Helper/Translate.php';
  28. /** Zend_Registry */
  29. require_once 'Zend/Registry.php';
  30. /** Zend_Translate */
  31. require_once 'Zend/Translate.php';
  32. require_once 'Zend/Translate/Adapter/Array.php';
  33. /**
  34. * Test class for Zend_View_Helper_Translate.
  35. *
  36. * @category Zend
  37. * @package Zend_View
  38. * @subpackage UnitTests
  39. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. * @group Zend_View
  42. * @group Zend_View_Helper
  43. */
  44. class Zend_View_Helper_TranslateTest extends PHPUnit_Framework_TestCase
  45. {
  46. /**
  47. * @var Zend_View_Helper_Translate
  48. */
  49. public $helper;
  50. /**
  51. * @var string
  52. */
  53. public $basePath;
  54. /**
  55. * Runs the test methods of this class.
  56. *
  57. * @return void
  58. */
  59. public static function main()
  60. {
  61. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_TranslateTest");
  62. $result = PHPUnit_TextUI_TestRunner::run($suite);
  63. }
  64. public function clearRegistry()
  65. {
  66. $regKey = 'Zend_Translate';
  67. if (Zend_Registry::isRegistered($regKey)) {
  68. $registry = Zend_Registry::getInstance();
  69. unset($registry[$regKey]);
  70. }
  71. }
  72. /**
  73. * Sets up the fixture, for example, open a network connection.
  74. * This method is called before a test is executed.
  75. *
  76. * @return void
  77. */
  78. public function setUp()
  79. {
  80. $this->clearRegistry();
  81. $this->helper = new Zend_View_Helper_Translate();
  82. }
  83. /**
  84. * Tears down the fixture, for example, close a network connection.
  85. * This method is called after a test is executed.
  86. *
  87. * @return void
  88. */
  89. public function tearDown()
  90. {
  91. unset($this->helper);
  92. $this->clearRegistry();
  93. }
  94. public function testTranslationObjectPassedToConstructorUsedForTranslation()
  95. {
  96. $trans = new Zend_Translate('array', array('one' => 'eins', 'two %1\$s' => 'zwei %1\$s'), 'de');
  97. $helper = new Zend_View_Helper_Translate($trans);
  98. $this->assertEquals('eins', $helper->translate('one'));
  99. $this->assertEquals('three', $helper->translate('three'));
  100. }
  101. public function testLocalTranslationObjectUsedForTranslationsWhenPresent()
  102. {
  103. $trans = new Zend_Translate('array', array('one' => 'eins', 'two %1\$s' => 'zwei %1\$s'), 'de');
  104. $this->helper->setTranslator($trans);
  105. $this->assertEquals('eins', $this->helper->translate('one'));
  106. $this->assertEquals('three', $this->helper->translate('three'));
  107. }
  108. public function testTranslationObjectInRegistryUsedForTranslationsInAbsenceOfLocalTranslationObject()
  109. {
  110. $trans = new Zend_Translate('array', array('one' => 'eins', 'two %1\$s' => 'zwei %1\$s'), 'de');
  111. Zend_Registry::set('Zend_Translate', $trans);
  112. $this->assertEquals('eins', $this->helper->translate('one'));
  113. }
  114. public function testOriginalMessagesAreReturnedWhenNoTranslationObjectPresent()
  115. {
  116. $this->assertEquals('one', $this->helper->translate('one'));
  117. $this->assertEquals('three', $this->helper->translate('three'));
  118. }
  119. public function testPassingNonNullNonTranslationObjectToConstructorThrowsException()
  120. {
  121. try {
  122. $helper = new Zend_View_Helper_Translate('something');
  123. } catch (Zend_View_Exception $e) {
  124. $this->assertContains('must set an instance of Zend_Translate', $e->getMessage());
  125. }
  126. }
  127. public function testPassingNonTranslationObjectToSetTranslatorThrowsException()
  128. {
  129. try {
  130. $this->helper->setTranslator('something');
  131. } catch (Zend_View_Exception $e) {
  132. $this->assertContains('must set an instance of Zend_Translate', $e->getMessage());
  133. }
  134. }
  135. public function testRetrievingLocaleWhenNoTranslationObjectSetThrowsException()
  136. {
  137. try {
  138. $this->helper->getLocale();
  139. } catch (Zend_View_Exception $e) {
  140. $this->assertContains('must set an instance of Zend_Translate', $e->getMessage());
  141. }
  142. }
  143. public function testSettingLocaleWhenNoTranslationObjectSetThrowsException()
  144. {
  145. try {
  146. $this->helper->setLocale('de');
  147. } catch (Zend_View_Exception $e) {
  148. $this->assertContains('must set an instance of Zend_Translate', $e->getMessage());
  149. }
  150. }
  151. public function testCanSetLocale()
  152. {
  153. $trans = new Zend_Translate('array', array('one' => 'eins', 'two %1\$s' => 'zwei %1\$s'), 'de');
  154. $trans->addTranslation(array('one' => 'uno', 'two %1\$s' => 'duo %2\$s'), 'it');
  155. $trans->setLocale('de');
  156. $this->helper->setTranslator($trans);
  157. $this->assertEquals('eins', $this->helper->translate('one'));
  158. $new = $this->helper->setLocale('it');
  159. $this->assertTrue($new instanceof Zend_View_Helper_Translate);
  160. $this->assertEquals('it', $new->getLocale());
  161. $this->assertEquals('uno', $this->helper->translate('one'));
  162. }
  163. public function testHelperImplementsFluentInterface()
  164. {
  165. $trans = new Zend_Translate('array', array('one' => 'eins', 'two %1\$s' => 'zwei %1\$s'), 'de');
  166. $trans->addTranslation(array('one' => 'uno', 'two %1\$s' => 'duo %2\$s'), 'it');
  167. $trans->setLocale('de');
  168. $locale = $this->helper->translate()->setTranslator($trans)->getLocale();
  169. $this->assertEquals('de', $locale);
  170. }
  171. public function testCanTranslateWithOptions()
  172. {
  173. $trans = new Zend_Translate('array', array('one' => 'eins', "two %1\$s" => "zwei %1\$s",
  174. "three %1\$s %2\$s" => "drei %1\$s %2\$s"), 'de');
  175. $trans->addTranslation(array('one' => 'uno', "two %1\$s" => "duo %2\$s",
  176. "three %1\$s %2\$s" => "tre %1\$s %2\$s"), 'it');
  177. $trans->setLocale('de');
  178. $this->helper->setTranslator($trans);
  179. $this->assertEquals("drei 100 200", $this->helper->translate("three %1\$s %2\$s", "100", "200"));
  180. $this->assertEquals("tre 100 200", $this->helper->translate("three %1\$s %2\$s", "100", "200", 'it'));
  181. $this->assertEquals("drei 100 200", $this->helper->translate("three %1\$s %2\$s", array("100", "200")));
  182. $this->assertEquals("tre 100 200", $this->helper->translate("three %1\$s %2\$s", array("100", "200"), 'it'));
  183. }
  184. public function testTranslationObjectNullByDefault()
  185. {
  186. $this->assertNull($this->helper->getTranslator());
  187. }
  188. public function testLocalTranslationObjectIsPreferredOverRegistry()
  189. {
  190. $transReg = new Zend_Translate('array', array('one' => 'eins'));
  191. Zend_Registry::set('Zend_Translate', $transReg);
  192. $this->assertSame($transReg->getAdapter(), $this->helper->getTranslator());
  193. $transLoc = new Zend_Translate('array', array('one' => 'uno'));
  194. $this->helper->setTranslator($transLoc);
  195. $this->assertSame($transLoc->getAdapter(), $this->helper->getTranslator());
  196. $this->assertNotSame($transLoc->getAdapter(), $transReg->getAdapter());
  197. }
  198. public function testHelperObjectReturnedWhenNoArgumentsPassed()
  199. {
  200. $helper = $this->helper->translate();
  201. $this->assertSame($this->helper, $helper);
  202. $transLoc = new Zend_Translate('array', array('one' => 'eins'));
  203. $this->helper->setTranslator($transLoc);
  204. $helper = $this->helper->translate();
  205. $this->assertSame($this->helper, $helper);
  206. }
  207. /**
  208. * ZF-6724
  209. */
  210. public function testTranslationWithPercent()
  211. {
  212. $trans = new Zend_Translate('array', array('one' => 'eins', "two %1\$s" => "zwei %1\$s",
  213. "three %1\$s %2\$s" => "drei %1\$s %2\$s", 'vier%ig' => 'four%'), 'de');
  214. $trans->setLocale('de');
  215. $this->helper->setTranslator($trans);
  216. $this->assertEquals("four%", $this->helper->translate("vier%ig"));
  217. $this->assertEquals("zwei 100", $this->helper->translate("two %1\$s", "100"));
  218. }
  219. /**
  220. * ZF-7937
  221. */
  222. public function testTranslationWithoutTranslator()
  223. {
  224. $result = $this->helper->translate("test %1\$s", "100");
  225. $this->assertEquals('test 100', $result);
  226. }
  227. }
  228. // Call Zend_View_Helper_TranslateTest::main() if this source file is executed directly.
  229. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_TranslateTest::main") {
  230. Zend_View_Helper_TranslateTest::main();
  231. }