HeadTitleTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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_HeadTitleTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_HeadTitleTest::main");
  25. }
  26. /** Zend_View_Helper_HeadTitle */
  27. require_once 'Zend/View/Helper/HeadTitle.php';
  28. /** Zend_View_Helper_Placeholder_Registry */
  29. require_once 'Zend/View/Helper/Placeholder/Registry.php';
  30. /** Zend_Registry */
  31. require_once 'Zend/Registry.php';
  32. /**
  33. * Test class for Zend_View_Helper_HeadTitle.
  34. *
  35. * @category Zend
  36. * @package Zend_View
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_View
  41. * @group Zend_View_Helper
  42. */
  43. class Zend_View_Helper_HeadTitleTest extends PHPUnit_Framework_TestCase
  44. {
  45. /**
  46. * @var Zend_View_Helper_HeadTitle
  47. */
  48. public $helper;
  49. /**
  50. * @var string
  51. */
  52. public $basePath;
  53. /**
  54. * Runs the test methods of this class.
  55. *
  56. * @return void
  57. */
  58. public static function main()
  59. {
  60. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_HeadTitleTest");
  61. $result = PHPUnit_TextUI_TestRunner::run($suite);
  62. }
  63. /**
  64. * Sets up the fixture, for example, open a network connection.
  65. * This method is called before a test is executed.
  66. *
  67. * @return void
  68. */
  69. public function setUp()
  70. {
  71. $regKey = Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY;
  72. if (Zend_Registry::isRegistered($regKey)) {
  73. $registry = Zend_Registry::getInstance();
  74. unset($registry[$regKey]);
  75. }
  76. $this->basePath = dirname(__FILE__) . '/_files/modules';
  77. $this->helper = new Zend_View_Helper_HeadTitle();
  78. }
  79. /**
  80. * Tears down the fixture, for example, close a network connection.
  81. * This method is called after a test is executed.
  82. *
  83. * @return void
  84. */
  85. public function tearDown()
  86. {
  87. unset($this->helper);
  88. }
  89. public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
  90. {
  91. $registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
  92. if ($registry->containerExists('Zend_View_Helper_HeadTitle')) {
  93. $registry->deleteContainer('Zend_View_Helper_HeadTitle');
  94. }
  95. $this->assertFalse($registry->containerExists('Zend_View_Helper_HeadTitle'));
  96. $helper = new Zend_View_Helper_HeadTitle();
  97. $this->assertTrue($registry->containerExists('Zend_View_Helper_HeadTitle'));
  98. }
  99. public function testHeadTitleReturnsObjectInstance()
  100. {
  101. $placeholder = $this->helper->headTitle();
  102. $this->assertTrue($placeholder instanceof Zend_View_Helper_HeadTitle);
  103. }
  104. public function testCanSetTitleViaHeadTitle()
  105. {
  106. $placeholder = $this->helper->headTitle('Foo Bar', 'SET');
  107. $this->assertContains('Foo Bar', $placeholder->toString());
  108. }
  109. public function testCanAppendTitleViaHeadTitle()
  110. {
  111. $placeholder = $this->helper->headTitle('Foo');
  112. $placeholder = $this->helper->headTitle('Bar');
  113. $this->assertContains('FooBar', $placeholder->toString());
  114. }
  115. public function testCanPrependTitleViaHeadTitle()
  116. {
  117. $placeholder = $this->helper->headTitle('Foo');
  118. $placeholder = $this->helper->headTitle('Bar', 'PREPEND');
  119. $this->assertContains('BarFoo', $placeholder->toString());
  120. }
  121. public function testReturnedPlaceholderToStringContainsFullTitleElement()
  122. {
  123. $placeholder = $this->helper->headTitle('Foo');
  124. $placeholder = $this->helper->headTitle('Bar', 'APPEND')->setSeparator(' :: ');
  125. $this->assertEquals('<title>Foo :: Bar</title>', $placeholder->toString());
  126. }
  127. public function testToStringEscapesEntries()
  128. {
  129. $this->helper->headTitle('<script type="text/javascript">alert("foo");</script>');
  130. $string = $this->helper->toString();
  131. $this->assertNotContains('<script', $string);
  132. $this->assertNotContains('</script>', $string);
  133. }
  134. public function testToStringEscapesSeparator()
  135. {
  136. $this->helper->headTitle('Foo')
  137. ->headTitle('Bar')
  138. ->setSeparator(' <br /> ');
  139. $string = $this->helper->toString();
  140. $this->assertNotContains('<br />', $string);
  141. $this->assertContains('Foo', $string);
  142. $this->assertContains('Bar', $string);
  143. $this->assertContains('br /', $string);
  144. }
  145. public function testIndentationIsHonored()
  146. {
  147. $this->helper->setIndent(4);
  148. $this->helper->headTitle('foo');
  149. $string = $this->helper->toString();
  150. $this->assertContains(' <title>', $string);
  151. }
  152. public function testAutoEscapeIsHonored()
  153. {
  154. $this->helper->headTitle('Some Title &copyright;');
  155. $this->assertEquals('<title>Some Title &amp;copyright;</title>', $this->helper->toString());
  156. $this->assertTrue($this->helper->headTitle()->getAutoEscape());
  157. $this->helper->headTitle()->setAutoEscape(false);
  158. $this->assertFalse($this->helper->headTitle()->getAutoEscape());
  159. $this->assertEquals('<title>Some Title &copyright;</title>', $this->helper->toString());
  160. }
  161. /**
  162. * @group ZF-2918
  163. * @link http://framework.zend.com/issues/browse/ZF-2918
  164. */
  165. public function testZF2918()
  166. {
  167. $this->helper->headTitle('Some Title');
  168. $this->helper->setPrefix('Prefix: ');
  169. $this->helper->setPostfix(' :Postfix');
  170. $this->assertEquals('<title>Prefix: Some Title :Postfix</title>', $this->helper->toString());
  171. }
  172. /**
  173. * @group ZF-3577
  174. * @link http://framework.zend.com/issues/browse/ZF-3577
  175. */
  176. public function testZF3577()
  177. {
  178. $this->helper->setAutoEscape(true);
  179. $this->helper->headTitle('Some Title');
  180. $this->helper->setPrefix('Prefix & ');
  181. $this->helper->setPostfix(' & Postfix');
  182. $this->assertEquals('<title>Prefix &amp; Some Title &amp; Postfix</title>', $this->helper->toString());
  183. }
  184. public function testCanTranslateTitle()
  185. {
  186. require_once 'Zend/Translate/Adapter/Ini.php';
  187. require_once 'Zend/Registry.php';
  188. $adapter = new Zend_Translate_Adapter_Ini(dirname(__FILE__) . '/../../Translate/Adapter/_files/translation_en.ini', 'en');
  189. Zend_Registry::set('Zend_Translate', $adapter);
  190. $this->helper->enableTranslation();
  191. $this->helper->headTitle('Message_1');
  192. $this->assertEquals('<title>Message 1 (en)</title>', $this->helper->toString());
  193. }
  194. /**
  195. * @group ZF-8036
  196. */
  197. public function testHeadTitleZero()
  198. {
  199. $this->helper->headTitle('0');
  200. $this->assertEquals('<title>0</title>', $this->helper->toString());
  201. }
  202. public function testCanPrependTitlesUsingDefaultAttachOrder()
  203. {
  204. $this->helper->setDefaultAttachOrder('PREPEND');
  205. $placeholder = $this->helper->headTitle('Foo');
  206. $placeholder = $this->helper->headTitle('Bar');
  207. $this->assertContains('BarFoo', $placeholder->toString());
  208. }
  209. /**
  210. * @group ZF-10284
  211. */
  212. public function testReturnTypeDefaultAttachOrder()
  213. {
  214. $this->assertTrue($this->helper->setDefaultAttachOrder('PREPEND') instanceof Zend_View_Helper_HeadTitle);
  215. $this->assertEquals('PREPEND', $this->helper->getDefaultAttachOrder());
  216. }
  217. }
  218. // Call Zend_View_Helper_HeadTitleTest::main() if this source file is executed directly.
  219. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_HeadTitleTest::main") {
  220. Zend_View_Helper_HeadTitleTest::main();
  221. }