HeadMetaTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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_HeadMetaTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_HeadMetaTest::main");
  25. }
  26. /** Zend_View_Helper_HeadMeta */
  27. require_once 'Zend/View/Helper/HeadMeta.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. /** Zend_View */
  33. require_once 'Zend/View.php';
  34. /**
  35. * Test class for Zend_View_Helper_HeadMeta.
  36. *
  37. * @category Zend
  38. * @package Zend_View
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_View
  43. * @group Zend_View_Helper
  44. */
  45. class Zend_View_Helper_HeadMetaTest extends PHPUnit_Framework_TestCase
  46. {
  47. /**
  48. * @var Zend_View_Helper_HeadMeta
  49. */
  50. public $helper;
  51. /**
  52. * @var string
  53. */
  54. public $basePath;
  55. /**
  56. * Runs the test methods of this class.
  57. *
  58. * @return void
  59. */
  60. public static function main()
  61. {
  62. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_HeadMetaTest");
  63. $result = PHPUnit_TextUI_TestRunner::run($suite);
  64. }
  65. /**
  66. * Sets up the fixture, for example, open a network connection.
  67. * This method is called before a test is executed.
  68. *
  69. * @return void
  70. */
  71. public function setUp()
  72. {
  73. $this->error = false;
  74. foreach (array(Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY, 'Zend_View_Helper_Doctype') as $key) {
  75. if (Zend_Registry::isRegistered($key)) {
  76. $registry = Zend_Registry::getInstance();
  77. unset($registry[$key]);
  78. }
  79. }
  80. $this->basePath = dirname(__FILE__) . '/_files/modules';
  81. $this->view = new Zend_View();
  82. $this->view->doctype('XHTML1_STRICT');
  83. $this->helper = new Zend_View_Helper_HeadMeta();
  84. $this->helper->setView($this->view);
  85. }
  86. /**
  87. * Tears down the fixture, for example, close a network connection.
  88. * This method is called after a test is executed.
  89. *
  90. * @return void
  91. */
  92. public function tearDown()
  93. {
  94. unset($this->helper);
  95. }
  96. public function handleErrors($errno, $errstr)
  97. {
  98. $this->error = $errstr;
  99. }
  100. public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
  101. {
  102. $registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
  103. if ($registry->containerExists('Zend_View_Helper_HeadMeta')) {
  104. $registry->deleteContainer('Zend_View_Helper_HeadMeta');
  105. }
  106. $this->assertFalse($registry->containerExists('Zend_View_Helper_HeadMeta'));
  107. $helper = new Zend_View_Helper_HeadMeta();
  108. $this->assertTrue($registry->containerExists('Zend_View_Helper_HeadMeta'));
  109. }
  110. public function testHeadMetaReturnsObjectInstance()
  111. {
  112. $placeholder = $this->helper->headMeta();
  113. $this->assertTrue($placeholder instanceof Zend_View_Helper_HeadMeta);
  114. }
  115. public function testAppendPrependAndSetThrowExceptionsWhenNonMetaValueProvided()
  116. {
  117. try {
  118. $this->helper->append('foo');
  119. $this->fail('Non-meta value should not append');
  120. } catch (Zend_View_Exception $e) {
  121. }
  122. try {
  123. $this->helper->offsetSet(3, 'foo');
  124. $this->fail('Non-meta value should not offsetSet');
  125. } catch (Zend_View_Exception $e) {
  126. }
  127. try {
  128. $this->helper->prepend('foo');
  129. $this->fail('Non-meta value should not prepend');
  130. } catch (Zend_View_Exception $e) {
  131. }
  132. try {
  133. $this->helper->set('foo');
  134. $this->fail('Non-meta value should not set');
  135. } catch (Zend_View_Exception $e) {
  136. }
  137. }
  138. protected function _inflectAction($type)
  139. {
  140. $type = str_replace('-', ' ', $type);
  141. $type = ucwords($type);
  142. $type = str_replace(' ', '', $type);
  143. return $type;
  144. }
  145. protected function _testOverloadAppend($type)
  146. {
  147. $action = 'append' . $this->_inflectAction($type);
  148. $string = 'foo';
  149. for ($i = 0; $i < 3; ++$i) {
  150. $string .= ' foo';
  151. $this->helper->$action('keywords', $string);
  152. $values = $this->helper->getArrayCopy();
  153. $this->assertEquals($i + 1, count($values));
  154. $item = $values[$i];
  155. $this->assertObjectHasAttribute('type', $item);
  156. $this->assertObjectHasAttribute('modifiers', $item);
  157. $this->assertObjectHasAttribute('content', $item);
  158. $this->assertObjectHasAttribute($item->type, $item);
  159. $this->assertEquals('keywords', $item->{$item->type});
  160. $this->assertEquals($string, $item->content);
  161. }
  162. }
  163. protected function _testOverloadPrepend($type)
  164. {
  165. $action = 'prepend' . $this->_inflectAction($type);
  166. $string = 'foo';
  167. for ($i = 0; $i < 3; ++$i) {
  168. $string .= ' foo';
  169. $this->helper->$action('keywords', $string);
  170. $values = $this->helper->getArrayCopy();
  171. $this->assertEquals($i + 1, count($values));
  172. $item = array_shift($values);
  173. $this->assertObjectHasAttribute('type', $item);
  174. $this->assertObjectHasAttribute('modifiers', $item);
  175. $this->assertObjectHasAttribute('content', $item);
  176. $this->assertObjectHasAttribute($item->type, $item);
  177. $this->assertEquals('keywords', $item->{$item->type});
  178. $this->assertEquals($string, $item->content);
  179. }
  180. }
  181. protected function _testOverloadSet($type)
  182. {
  183. $setAction = 'set' . $this->_inflectAction($type);
  184. $appendAction = 'append' . $this->_inflectAction($type);
  185. $string = 'foo';
  186. for ($i = 0; $i < 3; ++$i) {
  187. $this->helper->$appendAction('keywords', $string);
  188. $string .= ' foo';
  189. }
  190. $this->helper->$setAction('keywords', $string);
  191. $values = $this->helper->getArrayCopy();
  192. $this->assertEquals(1, count($values));
  193. $item = array_shift($values);
  194. $this->assertObjectHasAttribute('type', $item);
  195. $this->assertObjectHasAttribute('modifiers', $item);
  196. $this->assertObjectHasAttribute('content', $item);
  197. $this->assertObjectHasAttribute($item->type, $item);
  198. $this->assertEquals('keywords', $item->{$item->type});
  199. $this->assertEquals($string, $item->content);
  200. }
  201. public function testOverloadingAppendNameAppendsMetaTagToStack()
  202. {
  203. $this->_testOverloadAppend('name');
  204. }
  205. public function testOverloadingPrependNamePrependsMetaTagToStack()
  206. {
  207. $this->_testOverloadPrepend('name');
  208. }
  209. public function testOverloadingSetNameOverwritesMetaTagStack()
  210. {
  211. $this->_testOverloadSet('name');
  212. }
  213. public function testOverloadingAppendHttpEquivAppendsMetaTagToStack()
  214. {
  215. $this->_testOverloadAppend('http-equiv');
  216. }
  217. public function testOverloadingPrependHttpEquivPrependsMetaTagToStack()
  218. {
  219. $this->_testOverloadPrepend('http-equiv');
  220. }
  221. public function testOverloadingSetHttpEquivOverwritesMetaTagStack()
  222. {
  223. $this->_testOverloadSet('http-equiv');
  224. }
  225. public function testOverloadingThrowsExceptionWithFewerThanTwoArgs()
  226. {
  227. try {
  228. $this->helper->setName('foo');
  229. $this->fail('Overloading should require at least two arguments');
  230. } catch (Zend_View_Exception $e) {
  231. }
  232. }
  233. public function testOverloadingThrowsExceptionWithInvalidMethodType()
  234. {
  235. try {
  236. $this->helper->setFoo('foo');
  237. $this->fail('Overloading should only work for (set|prepend|append)(Name|HttpEquiv)');
  238. } catch (Zend_View_Exception $e) {
  239. }
  240. }
  241. public function testCanBuildMetaTagsWithAttributes()
  242. {
  243. $this->helper->setName('keywords', 'foo bar', array('lang' => 'us_en', 'scheme' => 'foo', 'bogus' => 'unused'));
  244. $value = $this->helper->getValue();
  245. $this->assertObjectHasAttribute('modifiers', $value);
  246. $modifiers = $value->modifiers;
  247. $this->assertTrue(array_key_exists('lang', $modifiers));
  248. $this->assertEquals('us_en', $modifiers['lang']);
  249. $this->assertTrue(array_key_exists('scheme', $modifiers));
  250. $this->assertEquals('foo', $modifiers['scheme']);
  251. }
  252. public function testToStringReturnsValidHtml()
  253. {
  254. $this->helper->setName('keywords', 'foo bar', array('lang' => 'us_en', 'scheme' => 'foo', 'bogus' => 'unused'))
  255. ->prependName('title', 'boo bah')
  256. ->appendHttpEquiv('screen', 'projection');
  257. $string = $this->helper->toString();
  258. $metas = substr_count($string, '<meta ');
  259. $this->assertEquals(3, $metas);
  260. $metas = substr_count($string, '/>');
  261. $this->assertEquals(3, $metas);
  262. $metas = substr_count($string, 'name="');
  263. $this->assertEquals(2, $metas);
  264. $metas = substr_count($string, 'http-equiv="');
  265. $this->assertEquals(1, $metas);
  266. $this->assertContains('http-equiv="screen" content="projection"', $string);
  267. $this->assertContains('name="keywords" content="foo bar"', $string);
  268. $this->assertContains('lang="us_en"', $string);
  269. $this->assertContains('scheme="foo"', $string);
  270. $this->assertNotContains('bogus', $string);
  271. $this->assertNotContains('unused', $string);
  272. $this->assertContains('name="title" content="boo bah"', $string);
  273. }
  274. /**
  275. * @group ZF-6637
  276. */
  277. public function testToStringWhenInvalidKeyProvidedShouldConvertThrownException()
  278. {
  279. $this->helper->headMeta('some-content', 'tag value', 'not allowed key');
  280. set_error_handler(array($this, 'handleErrors'));
  281. $string = @$this->helper->toString();
  282. $this->assertEquals('', $string);
  283. $this->assertTrue(is_string($this->error));
  284. }
  285. public function testHeadMetaHelperCreatesItemEntry()
  286. {
  287. $this->helper->headMeta('foo', 'keywords');
  288. $values = $this->helper->getArrayCopy();
  289. $this->assertEquals(1, count($values));
  290. $item = array_shift($values);
  291. $this->assertEquals('foo', $item->content);
  292. $this->assertEquals('name', $item->type);
  293. $this->assertEquals('keywords', $item->name);
  294. }
  295. public function testOverloadingOffsetInsertsAtOffset()
  296. {
  297. $this->helper->offsetSetName(100, 'keywords', 'foo');
  298. $values = $this->helper->getArrayCopy();
  299. $this->assertEquals(1, count($values));
  300. $this->assertTrue(array_key_exists(100, $values));
  301. $item = $values[100];
  302. $this->assertEquals('foo', $item->content);
  303. $this->assertEquals('name', $item->type);
  304. $this->assertEquals('keywords', $item->name);
  305. }
  306. public function testIndentationIsHonored()
  307. {
  308. $this->helper->setIndent(4);
  309. $this->helper->appendName('keywords', 'foo bar');
  310. $this->helper->appendName('seo', 'baz bat');
  311. $string = $this->helper->toString();
  312. $scripts = substr_count($string, ' <meta name=');
  313. $this->assertEquals(2, $scripts);
  314. }
  315. public function testStringRepresentationReflectsDoctype()
  316. {
  317. $this->view->doctype('HTML4_STRICT');
  318. $this->helper->headMeta('some content', 'foo');
  319. $test = $this->helper->toString();
  320. $this->assertNotContains('/>', $test);
  321. $this->assertContains('some content', $test);
  322. $this->assertContains('foo', $test);
  323. }
  324. /**
  325. * @group ZF-9743
  326. */
  327. public function testPropertyIsSupportedWithRdfaDoctype()
  328. {
  329. $this->view->doctype('XHTML1_RDFA');
  330. $this->helper->headMeta('foo', 'og:title', 'property');
  331. $this->assertEquals('<meta property="og:title" content="foo" />',
  332. $this->helper->toString()
  333. );
  334. }
  335. /**
  336. * @group ZF-9743
  337. */
  338. public function testPropertyIsNotSupportedByDefaultDoctype()
  339. {
  340. try {
  341. $this->helper->headMeta('foo', 'og:title', 'property');
  342. $this->fail('meta property attribute should not be supported on default doctype');
  343. } catch (Zend_View_Exception $e) {
  344. $this->assertContains('Invalid value passed', $e->getMessage());
  345. }
  346. }
  347. /**
  348. * @group ZF-9743
  349. * @depends testPropertyIsSupportedWithRdfaDoctype
  350. */
  351. public function testOverloadingAppendPropertyAppendsMetaTagToStack()
  352. {
  353. $this->view->doctype('XHTML1_RDFA');
  354. $this->_testOverloadAppend('property');
  355. }
  356. /**
  357. * @group ZF-9743
  358. * @depends testPropertyIsSupportedWithRdfaDoctype
  359. */
  360. public function testOverloadingPrependPropertyPrependsMetaTagToStack()
  361. {
  362. $this->view->doctype('XHTML1_RDFA');
  363. $this->_testOverloadPrepend('property');
  364. }
  365. /**
  366. * @group ZF-9743
  367. * @depends testPropertyIsSupportedWithRdfaDoctype
  368. */
  369. public function testOverloadingSetPropertyOverwritesMetaTagStack()
  370. {
  371. $this->view->doctype('XHTML1_RDFA');
  372. $this->_testOverloadSet('property');
  373. }
  374. /**
  375. * @group ZF-2663
  376. */
  377. public function testSetNameDoesntClobber()
  378. {
  379. $view = new Zend_View();
  380. $view->headMeta()->setName('keywords', 'foo');
  381. $view->headMeta()->appendHttpEquiv('pragma', 'bar');
  382. $view->headMeta()->appendHttpEquiv('Cache-control', 'baz');
  383. $view->headMeta()->setName('keywords', 'bat');
  384. $this->assertEquals(
  385. '<meta http-equiv="pragma" content="bar" />' . PHP_EOL . '<meta http-equiv="Cache-control" content="baz" />' . PHP_EOL . '<meta name="keywords" content="bat" />',
  386. $view->headMeta()->toString()
  387. );
  388. }
  389. /**
  390. * @group ZF-2663
  391. */
  392. public function testSetNameDoesntClobberPart2()
  393. {
  394. $view = new Zend_View();
  395. $view->headMeta()->setName('keywords', 'foo');
  396. $view->headMeta()->setName('description', 'foo');
  397. $view->headMeta()->appendHttpEquiv('pragma', 'baz');
  398. $view->headMeta()->appendHttpEquiv('Cache-control', 'baz');
  399. $view->headMeta()->setName('keywords', 'bar');
  400. $this->assertEquals(
  401. '<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" />',
  402. $view->headMeta()->toString()
  403. );
  404. }
  405. /**
  406. * @group ZF-3780
  407. * @link http://framework.zend.com/issues/browse/ZF-3780
  408. */
  409. public function testPlacesMetaTagsInProperOrder()
  410. {
  411. $view = new Zend_View();
  412. $view->headMeta()->setName('keywords', 'foo');
  413. $view->headMeta('some content', 'bar', 'name', array(), Zend_View_Helper_Placeholder_Container_Abstract::PREPEND);
  414. $this->assertEquals(
  415. '<meta name="bar" content="some content" />' . PHP_EOL . '<meta name="keywords" content="foo" />',
  416. $view->headMeta()->toString()
  417. );
  418. }
  419. /**
  420. * @group ZF-5435
  421. */
  422. public function testContainerMaintainsCorrectOrderOfItems()
  423. {
  424. $this->helper->offsetSetName(1, 'keywords', 'foo');
  425. $this->helper->offsetSetName(10, 'description', 'foo');
  426. $this->helper->offsetSetHttpEquiv(20, 'pragma', 'baz');
  427. $this->helper->offsetSetHttpEquiv(5, 'Cache-control', 'baz');
  428. $test = $this->helper->toString();
  429. $expected = '<meta name="keywords" content="foo" />' . PHP_EOL
  430. . '<meta http-equiv="Cache-control" content="baz" />' . PHP_EOL
  431. . '<meta name="description" content="foo" />' . PHP_EOL
  432. . '<meta http-equiv="pragma" content="baz" />';
  433. $this->assertEquals($expected, $test);
  434. }
  435. /**
  436. * @group ZF-7722
  437. */
  438. public function testCharsetValidateFail()
  439. {
  440. $view = new Zend_View();
  441. $view->doctype('HTML4_STRICT');
  442. try {
  443. $view->headMeta()->setCharset('utf-8');
  444. $this->fail('Should not be able to set charset for a HTML4 doctype');
  445. } catch (Zend_View_Exception $e) {}
  446. }
  447. /**
  448. * @group ZF-7722
  449. */
  450. public function testCharset() {
  451. $view = new Zend_View();
  452. $view->doctype('HTML5');
  453. $view->headMeta()->setCharset('utf-8');
  454. $this->assertEquals(
  455. '<meta charset="utf-8">',
  456. $view->headMeta()->toString());
  457. $view->doctype('XHTML5');
  458. $this->assertEquals(
  459. '<meta charset="utf-8"/>',
  460. $view->headMeta()->toString());
  461. }
  462. /**
  463. * @group ZF-11835
  464. */
  465. public function testConditional()
  466. {
  467. $html = $this->helper->appendHttpEquiv('foo', 'bar', array('conditional' => 'lt IE 7'))->toString();
  468. $this->assertRegExp("|^<!--\[if lt IE 7\]>|", $html);
  469. $this->assertRegExp("|<!\[endif\]-->$|", $html);
  470. }
  471. /**
  472. * @group ZF-11910
  473. */
  474. public function testStandaloneInstantiationWithoutViewDoesNotCauseFatalError()
  475. {
  476. $expected = '<meta name="foo" content="bar" />';
  477. $helper = new Zend_View_Helper_HeadMeta();
  478. $result = (string)$helper->headMeta()->appendName('foo','bar');
  479. $this->assertEquals($expected, $result);
  480. }
  481. /**
  482. * @group GH-515
  483. */
  484. public function testConditionalNoIE()
  485. {
  486. $html = $this->helper->appendHttpEquiv('foo', 'bar', array('conditional' => '!IE'))->toString();
  487. $this->assertContains('<!--[if !IE]><!--><', $html);
  488. $this->assertContains('<!--<![endif]-->', $html);
  489. }
  490. /**
  491. * @group GH-515
  492. */
  493. public function testConditionalNoIEWidthSpace()
  494. {
  495. $html = $this->helper->appendHttpEquiv('foo', 'bar', array('conditional' => '! IE'))->toString();
  496. $this->assertContains('<!--[if ! IE]><!--><', $html);
  497. $this->assertContains('<!--<![endif]-->', $html);
  498. }
  499. }
  500. // Call Zend_View_Helper_HeadMetaTest::main() if this source file is executed directly.
  501. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_HeadMetaTest::main") {
  502. Zend_View_Helper_HeadMetaTest::main();
  503. }