HeadMetaTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. // Call Zend_View_Helper_HeadMetaTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_HeadMetaTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. /** Zend_View_Helper_HeadMeta */
  8. require_once 'Zend/View/Helper/HeadMeta.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. /** Zend_View */
  14. require_once 'Zend/View.php';
  15. /**
  16. * Test class for Zend_View_Helper_HeadMeta.
  17. *
  18. * @category Zend
  19. * @package Zend_View
  20. * @subpackage UnitTests
  21. */
  22. class Zend_View_Helper_HeadMetaTest extends PHPUnit_Framework_TestCase
  23. {
  24. /**
  25. * @var Zend_View_Helper_HeadMeta
  26. */
  27. public $helper;
  28. /**
  29. * @var string
  30. */
  31. public $basePath;
  32. /**
  33. * Runs the test methods of this class.
  34. *
  35. * @return void
  36. */
  37. public static function main()
  38. {
  39. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_HeadMetaTest");
  40. $result = PHPUnit_TextUI_TestRunner::run($suite);
  41. }
  42. /**
  43. * Sets up the fixture, for example, open a network connection.
  44. * This method is called before a test is executed.
  45. *
  46. * @return void
  47. */
  48. public function setUp()
  49. {
  50. $this->error = false;
  51. foreach (array(Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY, 'Zend_View_Helper_Doctype') as $key) {
  52. if (Zend_Registry::isRegistered($key)) {
  53. $registry = Zend_Registry::getInstance();
  54. unset($registry[$key]);
  55. }
  56. }
  57. $this->basePath = dirname(__FILE__) . '/_files/modules';
  58. $this->view = new Zend_View();
  59. $this->view->doctype('XHTML1_STRICT');
  60. $this->helper = new Zend_View_Helper_HeadMeta();
  61. $this->helper->setView($this->view);
  62. }
  63. /**
  64. * Tears down the fixture, for example, close a network connection.
  65. * This method is called after a test is executed.
  66. *
  67. * @return void
  68. */
  69. public function tearDown()
  70. {
  71. unset($this->helper);
  72. }
  73. public function handleErrors($errno, $errstr)
  74. {
  75. $this->error = $errstr;
  76. }
  77. public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
  78. {
  79. $registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
  80. if ($registry->containerExists('Zend_View_Helper_HeadMeta')) {
  81. $registry->deleteContainer('Zend_View_Helper_HeadMeta');
  82. }
  83. $this->assertFalse($registry->containerExists('Zend_View_Helper_HeadMeta'));
  84. $helper = new Zend_View_Helper_HeadMeta();
  85. $this->assertTrue($registry->containerExists('Zend_View_Helper_HeadMeta'));
  86. }
  87. public function testHeadMetaReturnsObjectInstance()
  88. {
  89. $placeholder = $this->helper->headMeta();
  90. $this->assertTrue($placeholder instanceof Zend_View_Helper_HeadMeta);
  91. }
  92. public function testAppendPrependAndSetThrowExceptionsWhenNonMetaValueProvided()
  93. {
  94. try {
  95. $this->helper->append('foo');
  96. $this->fail('Non-meta value should not append');
  97. } catch (Zend_View_Exception $e) {
  98. }
  99. try {
  100. $this->helper->offsetSet(3, 'foo');
  101. $this->fail('Non-meta value should not offsetSet');
  102. } catch (Zend_View_Exception $e) {
  103. }
  104. try {
  105. $this->helper->prepend('foo');
  106. $this->fail('Non-meta value should not prepend');
  107. } catch (Zend_View_Exception $e) {
  108. }
  109. try {
  110. $this->helper->set('foo');
  111. $this->fail('Non-meta value should not set');
  112. } catch (Zend_View_Exception $e) {
  113. }
  114. }
  115. protected function _inflectAction($type)
  116. {
  117. $type = str_replace('-', ' ', $type);
  118. $type = ucwords($type);
  119. $type = str_replace(' ', '', $type);
  120. return $type;
  121. }
  122. protected function _testOverloadAppend($type)
  123. {
  124. $action = 'append' . $this->_inflectAction($type);
  125. $string = 'foo';
  126. for ($i = 0; $i < 3; ++$i) {
  127. $string .= ' foo';
  128. $this->helper->$action('keywords', $string);
  129. $values = $this->helper->getArrayCopy();
  130. $this->assertEquals($i + 1, count($values));
  131. $item = $values[$i];
  132. $this->assertObjectHasAttribute('type', $item);
  133. $this->assertObjectHasAttribute('modifiers', $item);
  134. $this->assertObjectHasAttribute('content', $item);
  135. $this->assertObjectHasAttribute($item->type, $item);
  136. $this->assertEquals('keywords', $item->{$item->type});
  137. $this->assertEquals($string, $item->content);
  138. }
  139. }
  140. protected function _testOverloadPrepend($type)
  141. {
  142. $action = 'prepend' . $this->_inflectAction($type);
  143. $string = 'foo';
  144. for ($i = 0; $i < 3; ++$i) {
  145. $string .= ' foo';
  146. $this->helper->$action('keywords', $string);
  147. $values = $this->helper->getArrayCopy();
  148. $this->assertEquals($i + 1, count($values));
  149. $item = array_shift($values);
  150. $this->assertObjectHasAttribute('type', $item);
  151. $this->assertObjectHasAttribute('modifiers', $item);
  152. $this->assertObjectHasAttribute('content', $item);
  153. $this->assertObjectHasAttribute($item->type, $item);
  154. $this->assertEquals('keywords', $item->{$item->type});
  155. $this->assertEquals($string, $item->content);
  156. }
  157. }
  158. protected function _testOverloadSet($type)
  159. {
  160. $setAction = 'set' . $this->_inflectAction($type);
  161. $appendAction = 'append' . $this->_inflectAction($type);
  162. $string = 'foo';
  163. for ($i = 0; $i < 3; ++$i) {
  164. $this->helper->$appendAction('keywords', $string);
  165. $string .= ' foo';
  166. }
  167. $this->helper->$setAction('keywords', $string);
  168. $values = $this->helper->getArrayCopy();
  169. $this->assertEquals(1, count($values));
  170. $item = array_shift($values);
  171. $this->assertObjectHasAttribute('type', $item);
  172. $this->assertObjectHasAttribute('modifiers', $item);
  173. $this->assertObjectHasAttribute('content', $item);
  174. $this->assertObjectHasAttribute($item->type, $item);
  175. $this->assertEquals('keywords', $item->{$item->type});
  176. $this->assertEquals($string, $item->content);
  177. }
  178. public function testOverloadingAppendNameAppendsMetaTagToStack()
  179. {
  180. $this->_testOverloadAppend('name');
  181. }
  182. public function testOverloadingPrependNamePrependsMetaTagToStack()
  183. {
  184. $this->_testOverloadPrepend('name');
  185. }
  186. public function testOverloadingSetNameOverwritesMetaTagStack()
  187. {
  188. $this->_testOverloadSet('name');
  189. }
  190. public function testOverloadingAppendHttpEquivAppendsMetaTagToStack()
  191. {
  192. $this->_testOverloadAppend('http-equiv');
  193. }
  194. public function testOverloadingPrependHttpEquivPrependsMetaTagToStack()
  195. {
  196. $this->_testOverloadPrepend('http-equiv');
  197. }
  198. public function testOverloadingSetHttpEquivOverwritesMetaTagStack()
  199. {
  200. $this->_testOverloadSet('http-equiv');
  201. }
  202. public function testOverloadingThrowsExceptionWithFewerThanTwoArgs()
  203. {
  204. try {
  205. $this->helper->setName('foo');
  206. $this->fail('Overloading should require at least two arguments');
  207. } catch (Zend_View_Exception $e) {
  208. }
  209. }
  210. public function testOverloadingThrowsExceptionWithInvalidMethodType()
  211. {
  212. try {
  213. $this->helper->setFoo('foo');
  214. $this->fail('Overloading should only work for (set|prepend|append)(Name|HttpEquiv)');
  215. } catch (Zend_View_Exception $e) {
  216. }
  217. }
  218. public function testCanBuildMetaTagsWithAttributes()
  219. {
  220. $this->helper->setName('keywords', 'foo bar', array('lang' => 'us_en', 'scheme' => 'foo', 'bogus' => 'unused'));
  221. $value = $this->helper->getValue();
  222. $this->assertObjectHasAttribute('modifiers', $value);
  223. $modifiers = $value->modifiers;
  224. $this->assertTrue(array_key_exists('lang', $modifiers));
  225. $this->assertEquals('us_en', $modifiers['lang']);
  226. $this->assertTrue(array_key_exists('scheme', $modifiers));
  227. $this->assertEquals('foo', $modifiers['scheme']);
  228. }
  229. public function testToStringReturnsValidHtml()
  230. {
  231. $this->helper->setName('keywords', 'foo bar', array('lang' => 'us_en', 'scheme' => 'foo', 'bogus' => 'unused'))
  232. ->prependName('title', 'boo bah')
  233. ->appendHttpEquiv('screen', 'projection');
  234. $string = $this->helper->toString();
  235. $metas = substr_count($string, '<meta ');
  236. $this->assertEquals(3, $metas);
  237. $metas = substr_count($string, '/>');
  238. $this->assertEquals(3, $metas);
  239. $metas = substr_count($string, 'name="');
  240. $this->assertEquals(2, $metas);
  241. $metas = substr_count($string, 'http-equiv="');
  242. $this->assertEquals(1, $metas);
  243. $this->assertContains('http-equiv="screen" content="projection"', $string);
  244. $this->assertContains('name="keywords" content="foo bar"', $string);
  245. $this->assertContains('lang="us_en"', $string);
  246. $this->assertContains('scheme="foo"', $string);
  247. $this->assertNotContains('bogus', $string);
  248. $this->assertNotContains('unused', $string);
  249. $this->assertContains('name="title" content="boo bah"', $string);
  250. }
  251. /**
  252. * @group #ZF-6637
  253. */
  254. public function testToStringWhenInvalidKeyProvidedShouldConvertThrownException()
  255. {
  256. $this->helper->headMeta('some-content', 'tag value', 'not allowed key');
  257. set_error_handler(array($this, 'handleErrors'));
  258. $string = @$this->helper->toString();
  259. $this->assertEquals('', $string);
  260. $this->assertTrue(is_string($this->error));
  261. }
  262. public function testHeadMetaHelperCreatesItemEntry()
  263. {
  264. $this->helper->headMeta('foo', 'keywords');
  265. $values = $this->helper->getArrayCopy();
  266. $this->assertEquals(1, count($values));
  267. $item = array_shift($values);
  268. $this->assertEquals('foo', $item->content);
  269. $this->assertEquals('name', $item->type);
  270. $this->assertEquals('keywords', $item->name);
  271. }
  272. public function testOverloadingOffsetInsertsAtOffset()
  273. {
  274. $this->helper->offsetSetName(100, 'keywords', 'foo');
  275. $values = $this->helper->getArrayCopy();
  276. $this->assertEquals(1, count($values));
  277. $this->assertTrue(array_key_exists(100, $values));
  278. $item = $values[100];
  279. $this->assertEquals('foo', $item->content);
  280. $this->assertEquals('name', $item->type);
  281. $this->assertEquals('keywords', $item->name);
  282. }
  283. public function testIndentationIsHonored()
  284. {
  285. $this->helper->setIndent(4);
  286. $this->helper->appendName('keywords', 'foo bar');
  287. $this->helper->appendName('seo', 'baz bat');
  288. $string = $this->helper->toString();
  289. $scripts = substr_count($string, ' <meta name=');
  290. $this->assertEquals(2, $scripts);
  291. }
  292. public function testStringRepresentationReflectsDoctype()
  293. {
  294. $this->view->doctype('HTML4_STRICT');
  295. $this->helper->headMeta('some content', 'foo');
  296. $test = $this->helper->toString();
  297. $this->assertNotContains('/>', $test);
  298. $this->assertContains('some content', $test);
  299. $this->assertContains('foo', $test);
  300. }
  301. /**
  302. * @issue ZF-2663
  303. */
  304. public function testSetNameDoesntClobber()
  305. {
  306. $view = new Zend_View();
  307. $view->headMeta()->setName('keywords', 'foo');
  308. $view->headMeta()->appendHttpEquiv('pragma', 'bar');
  309. $view->headMeta()->appendHttpEquiv('Cache-control', 'baz');
  310. $view->headMeta()->setName('keywords', 'bat');
  311. $this->assertEquals(
  312. '<meta http-equiv="pragma" content="bar" />' . PHP_EOL . '<meta http-equiv="Cache-control" content="baz" />' . PHP_EOL . '<meta name="keywords" content="bat" />',
  313. $view->headMeta()->toString()
  314. );
  315. }
  316. /**
  317. * @issue ZF-2663
  318. */
  319. public function testSetNameDoesntClobberPart2()
  320. {
  321. $view = new Zend_View();
  322. $view->headMeta()->setName('keywords', 'foo');
  323. $view->headMeta()->setName('description', 'foo');
  324. $view->headMeta()->appendHttpEquiv('pragma', 'baz');
  325. $view->headMeta()->appendHttpEquiv('Cache-control', 'baz');
  326. $view->headMeta()->setName('keywords', 'bar');
  327. $this->assertEquals(
  328. '<meta name="description" content="foo" />' . PHP_EOL . '<meta http-equiv="pragma" content="baz" />' . PHP_EOL . '<meta http-equiv="Cache-control" content="baz" />' . PHP_EOL . '<meta name="keywords" content="bar" />',
  329. $view->headMeta()->toString()
  330. );
  331. }
  332. /**
  333. * @issue ZF-3780
  334. * @link http://framework.zend.com/issues/browse/ZF-3780
  335. */
  336. public function testPlacesMetaTagsInProperOrder()
  337. {
  338. $view = new Zend_View();
  339. $view->headMeta()->setName('keywords', 'foo');
  340. $view->headMeta('some content', 'bar', 'name', array(), Zend_View_Helper_Placeholder_Container_Abstract::PREPEND);
  341. $this->assertEquals(
  342. '<meta name="bar" content="some content" />' . PHP_EOL . '<meta name="keywords" content="foo" />',
  343. $view->headMeta()->toString()
  344. );
  345. }
  346. /**
  347. * @issue ZF-5435
  348. */
  349. public function testContainerMaintainsCorrectOrderOfItems()
  350. {
  351. $this->helper->offsetSetName(1, 'keywords', 'foo');
  352. $this->helper->offsetSetName(10, 'description', 'foo');
  353. $this->helper->offsetSetHttpEquiv(20, 'pragma', 'baz');
  354. $this->helper->offsetSetHttpEquiv(5, 'Cache-control', 'baz');
  355. $test = $this->helper->toString();
  356. $expected = '<meta name="keywords" content="foo" />
  357. <meta http-equiv="Cache-control" content="baz" />
  358. <meta name="description" content="foo" />
  359. <meta http-equiv="pragma" content="baz" />';
  360. $this->assertEquals($expected, $test);
  361. }
  362. }
  363. // Call Zend_View_Helper_HeadMetaTest::main() if this source file is executed directly.
  364. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_HeadMetaTest::main") {
  365. Zend_View_Helper_HeadMetaTest::main();
  366. }