HeadTitleTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. // Call Zend_View_Helper_HeadTitleTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_HeadTitleTest::main");
  5. }
  6. require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/TestHelper.php';
  7. /** Zend_View_Helper_HeadTitle */
  8. require_once 'Zend/View/Helper/HeadTitle.php';
  9. /** Zend_View_Helper_Placeholder_Registry */
  10. require_once 'Zend/View/Helper/Placeholder/Registry.php';
  11. /** Zend_Registry */
  12. require_once 'Zend/Registry.php';
  13. /**
  14. * Test class for Zend_View_Helper_HeadTitle.
  15. *
  16. * @category Zend
  17. * @package Zend_View
  18. * @subpackage UnitTests
  19. */
  20. class Zend_View_Helper_HeadTitleTest extends PHPUnit_Framework_TestCase
  21. {
  22. /**
  23. * @var Zend_View_Helper_HeadTitle
  24. */
  25. public $helper;
  26. /**
  27. * @var string
  28. */
  29. public $basePath;
  30. /**
  31. * Runs the test methods of this class.
  32. *
  33. * @return void
  34. */
  35. public static function main()
  36. {
  37. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_HeadTitleTest");
  38. $result = PHPUnit_TextUI_TestRunner::run($suite);
  39. }
  40. /**
  41. * Sets up the fixture, for example, open a network connection.
  42. * This method is called before a test is executed.
  43. *
  44. * @return void
  45. */
  46. public function setUp()
  47. {
  48. $regKey = Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY;
  49. if (Zend_Registry::isRegistered($regKey)) {
  50. $registry = Zend_Registry::getInstance();
  51. unset($registry[$regKey]);
  52. }
  53. $this->basePath = dirname(__FILE__) . '/_files/modules';
  54. $this->helper = new Zend_View_Helper_HeadTitle();
  55. }
  56. /**
  57. * Tears down the fixture, for example, close a network connection.
  58. * This method is called after a test is executed.
  59. *
  60. * @return void
  61. */
  62. public function tearDown()
  63. {
  64. unset($this->helper);
  65. }
  66. public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
  67. {
  68. $registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
  69. if ($registry->containerExists('Zend_View_Helper_HeadTitle')) {
  70. $registry->deleteContainer('Zend_View_Helper_HeadTitle');
  71. }
  72. $this->assertFalse($registry->containerExists('Zend_View_Helper_HeadTitle'));
  73. $helper = new Zend_View_Helper_HeadTitle();
  74. $this->assertTrue($registry->containerExists('Zend_View_Helper_HeadTitle'));
  75. }
  76. public function testHeadTitleReturnsObjectInstance()
  77. {
  78. $placeholder = $this->helper->headTitle();
  79. $this->assertTrue($placeholder instanceof Zend_View_Helper_HeadTitle);
  80. }
  81. public function testCanSetTitleViaHeadTitle()
  82. {
  83. $placeholder = $this->helper->headTitle('Foo Bar', 'SET');
  84. $this->assertContains('Foo Bar', $placeholder->toString());
  85. }
  86. public function testCanAppendTitleViaHeadTitle()
  87. {
  88. $placeholder = $this->helper->headTitle('Foo');
  89. $placeholder = $this->helper->headTitle('Bar');
  90. $this->assertContains('FooBar', $placeholder->toString());
  91. }
  92. public function testCanPrependTitleViaHeadTitle()
  93. {
  94. $placeholder = $this->helper->headTitle('Foo');
  95. $placeholder = $this->helper->headTitle('Bar', 'PREPEND');
  96. $this->assertContains('BarFoo', $placeholder->toString());
  97. }
  98. public function testReturnedPlaceholderToStringContainsFullTitleElement()
  99. {
  100. $placeholder = $this->helper->headTitle('Foo');
  101. $placeholder = $this->helper->headTitle('Bar', 'APPEND')->setSeparator(' :: ');
  102. $this->assertEquals('<title>Foo :: Bar</title>', $placeholder->toString());
  103. }
  104. public function testToStringEscapesEntries()
  105. {
  106. $this->helper->headTitle('<script type="text/javascript">alert("foo");</script>');
  107. $string = $this->helper->toString();
  108. $this->assertNotContains('<script', $string);
  109. $this->assertNotContains('</script>', $string);
  110. }
  111. public function testToStringEscapesSeparator()
  112. {
  113. $this->helper->headTitle('Foo')
  114. ->headTitle('Bar')
  115. ->setSeparator(' <br /> ');
  116. $string = $this->helper->toString();
  117. $this->assertNotContains('<br />', $string);
  118. $this->assertContains('Foo', $string);
  119. $this->assertContains('Bar', $string);
  120. $this->assertContains('br /', $string);
  121. }
  122. public function testIndentationIsHonored()
  123. {
  124. $this->helper->setIndent(4);
  125. $this->helper->headTitle('foo');
  126. $string = $this->helper->toString();
  127. $this->assertContains(' <title>', $string);
  128. }
  129. public function testAutoEscapeIsHonored()
  130. {
  131. $this->helper->headTitle('Some Title &copyright;');
  132. $this->assertEquals('<title>Some Title &amp;copyright;</title>', $this->helper->toString());
  133. $this->assertTrue($this->helper->headTitle()->getAutoEscape());
  134. $this->helper->headTitle()->setAutoEscape(false);
  135. $this->assertFalse($this->helper->headTitle()->getAutoEscape());
  136. $this->assertEquals('<title>Some Title &copyright;</title>', $this->helper->toString());
  137. }
  138. /**
  139. * @issue ZF-2918
  140. * @link http://framework.zend.com/issues/browse/ZF-2918
  141. */
  142. public function testZF2918()
  143. {
  144. $this->helper->headTitle('Some Title');
  145. $this->helper->setPrefix('Prefix: ');
  146. $this->helper->setPostfix(' :Postfix');
  147. $this->assertEquals('<title>Prefix: Some Title :Postfix</title>', $this->helper->toString());
  148. }
  149. /**
  150. * @issue ZF-3577
  151. * @link http://framework.zend.com/issues/browse/ZF-3577
  152. */
  153. public function testZF3577()
  154. {
  155. $this->helper->setAutoEscape(true);
  156. $this->helper->headTitle('Some Title');
  157. $this->helper->setPrefix('Prefix & ');
  158. $this->helper->setPostfix(' & Postfix');
  159. $this->assertEquals('<title>Prefix &amp; Some Title &amp; Postfix</title>', $this->helper->toString());
  160. }
  161. public function testCanTranslateTitle()
  162. {
  163. require_once 'Zend/Translate/Adapter/Ini.php';
  164. require_once 'Zend/Registry.php';
  165. $adapter = new Zend_Translate_Adapter_Ini(dirname(__FILE__) . '/../../Translate/Adapter/_files/translation_en.ini', 'en');
  166. Zend_Registry::set('Zend_Translate', $adapter);
  167. $this->helper->enableTranslation();
  168. $this->helper->headTitle('Message_1');
  169. $this->assertEquals('<title>Message 1 (en)</title>', $this->helper->toString());
  170. }
  171. }
  172. // Call Zend_View_Helper_HeadTitleTest::main() if this source file is executed directly.
  173. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_HeadTitleTest::main") {
  174. Zend_View_Helper_HeadTitleTest::main();
  175. }