HeadTitleTest.php 8.3 KB

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