HeadMetaTest.php 16 KB

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