HeadMetaTest.php 14 KB

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