| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_View
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- // Call Zend_View_Helper_HeadMetaTest::main() if this source file is executed directly.
- if (!defined("PHPUnit_MAIN_METHOD")) {
- define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_HeadMetaTest::main");
- }
- /** Zend_View_Helper_HeadMeta */
- require_once 'Zend/View/Helper/HeadMeta.php';
- /** Zend_View_Helper_Placeholder_Registry */
- require_once 'Zend/View/Helper/Placeholder/Registry.php';
- /** Zend_Registry */
- require_once 'Zend/Registry.php';
- /** Zend_View */
- require_once 'Zend/View.php';
- /**
- * Test class for Zend_View_Helper_HeadMeta.
- *
- * @category Zend
- * @package Zend_View
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_View
- * @group Zend_View_Helper
- */
- class Zend_View_Helper_HeadMetaTest extends PHPUnit_Framework_TestCase
- {
- /**
- * @var Zend_View_Helper_HeadMeta
- */
- public $helper;
- /**
- * @var string
- */
- public $basePath;
- /**
- * Runs the test methods of this class.
- *
- * @return void
- */
- public static function main()
- {
- $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_HeadMetaTest");
- $result = PHPUnit_TextUI_TestRunner::run($suite);
- }
- /**
- * Sets up the fixture, for example, open a network connection.
- * This method is called before a test is executed.
- *
- * @return void
- */
- public function setUp()
- {
- $this->error = false;
- foreach (array(Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY, 'Zend_View_Helper_Doctype') as $key) {
- if (Zend_Registry::isRegistered($key)) {
- $registry = Zend_Registry::getInstance();
- unset($registry[$key]);
- }
- }
- $this->basePath = dirname(__FILE__) . '/_files/modules';
- $this->view = new Zend_View();
- $this->view->doctype('XHTML1_STRICT');
- $this->helper = new Zend_View_Helper_HeadMeta();
- $this->helper->setView($this->view);
- }
- /**
- * Tears down the fixture, for example, close a network connection.
- * This method is called after a test is executed.
- *
- * @return void
- */
- public function tearDown()
- {
- unset($this->helper);
- }
- public function handleErrors($errno, $errstr)
- {
- $this->error = $errstr;
- }
- public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
- {
- $registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
- if ($registry->containerExists('Zend_View_Helper_HeadMeta')) {
- $registry->deleteContainer('Zend_View_Helper_HeadMeta');
- }
- $this->assertFalse($registry->containerExists('Zend_View_Helper_HeadMeta'));
- $helper = new Zend_View_Helper_HeadMeta();
- $this->assertTrue($registry->containerExists('Zend_View_Helper_HeadMeta'));
- }
- public function testHeadMetaReturnsObjectInstance()
- {
- $placeholder = $this->helper->headMeta();
- $this->assertTrue($placeholder instanceof Zend_View_Helper_HeadMeta);
- }
- public function testAppendPrependAndSetThrowExceptionsWhenNonMetaValueProvided()
- {
- try {
- $this->helper->append('foo');
- $this->fail('Non-meta value should not append');
- } catch (Zend_View_Exception $e) {
- }
- try {
- $this->helper->offsetSet(3, 'foo');
- $this->fail('Non-meta value should not offsetSet');
- } catch (Zend_View_Exception $e) {
- }
- try {
- $this->helper->prepend('foo');
- $this->fail('Non-meta value should not prepend');
- } catch (Zend_View_Exception $e) {
- }
- try {
- $this->helper->set('foo');
- $this->fail('Non-meta value should not set');
- } catch (Zend_View_Exception $e) {
- }
- }
- protected function _inflectAction($type)
- {
- $type = str_replace('-', ' ', $type);
- $type = ucwords($type);
- $type = str_replace(' ', '', $type);
- return $type;
- }
- protected function _testOverloadAppend($type)
- {
- $action = 'append' . $this->_inflectAction($type);
- $string = 'foo';
- for ($i = 0; $i < 3; ++$i) {
- $string .= ' foo';
- $this->helper->$action('keywords', $string);
- $values = $this->helper->getArrayCopy();
- $this->assertEquals($i + 1, count($values));
- $item = $values[$i];
- $this->assertObjectHasAttribute('type', $item);
- $this->assertObjectHasAttribute('modifiers', $item);
- $this->assertObjectHasAttribute('content', $item);
- $this->assertObjectHasAttribute($item->type, $item);
- $this->assertEquals('keywords', $item->{$item->type});
- $this->assertEquals($string, $item->content);
- }
- }
- protected function _testOverloadPrepend($type)
- {
- $action = 'prepend' . $this->_inflectAction($type);
- $string = 'foo';
- for ($i = 0; $i < 3; ++$i) {
- $string .= ' foo';
- $this->helper->$action('keywords', $string);
- $values = $this->helper->getArrayCopy();
- $this->assertEquals($i + 1, count($values));
- $item = array_shift($values);
- $this->assertObjectHasAttribute('type', $item);
- $this->assertObjectHasAttribute('modifiers', $item);
- $this->assertObjectHasAttribute('content', $item);
- $this->assertObjectHasAttribute($item->type, $item);
- $this->assertEquals('keywords', $item->{$item->type});
- $this->assertEquals($string, $item->content);
- }
- }
- protected function _testOverloadSet($type)
- {
- $setAction = 'set' . $this->_inflectAction($type);
- $appendAction = 'append' . $this->_inflectAction($type);
- $string = 'foo';
- for ($i = 0; $i < 3; ++$i) {
- $this->helper->$appendAction('keywords', $string);
- $string .= ' foo';
- }
- $this->helper->$setAction('keywords', $string);
- $values = $this->helper->getArrayCopy();
- $this->assertEquals(1, count($values));
- $item = array_shift($values);
- $this->assertObjectHasAttribute('type', $item);
- $this->assertObjectHasAttribute('modifiers', $item);
- $this->assertObjectHasAttribute('content', $item);
- $this->assertObjectHasAttribute($item->type, $item);
- $this->assertEquals('keywords', $item->{$item->type});
- $this->assertEquals($string, $item->content);
- }
- public function testOverloadingAppendNameAppendsMetaTagToStack()
- {
- $this->_testOverloadAppend('name');
- }
- public function testOverloadingPrependNamePrependsMetaTagToStack()
- {
- $this->_testOverloadPrepend('name');
- }
- public function testOverloadingSetNameOverwritesMetaTagStack()
- {
- $this->_testOverloadSet('name');
- }
- public function testOverloadingAppendHttpEquivAppendsMetaTagToStack()
- {
- $this->_testOverloadAppend('http-equiv');
- }
- public function testOverloadingPrependHttpEquivPrependsMetaTagToStack()
- {
- $this->_testOverloadPrepend('http-equiv');
- }
- public function testOverloadingSetHttpEquivOverwritesMetaTagStack()
- {
- $this->_testOverloadSet('http-equiv');
- }
- public function testOverloadingThrowsExceptionWithFewerThanTwoArgs()
- {
- try {
- $this->helper->setName('foo');
- $this->fail('Overloading should require at least two arguments');
- } catch (Zend_View_Exception $e) {
- }
- }
- public function testOverloadingThrowsExceptionWithInvalidMethodType()
- {
- try {
- $this->helper->setFoo('foo');
- $this->fail('Overloading should only work for (set|prepend|append)(Name|HttpEquiv)');
- } catch (Zend_View_Exception $e) {
- }
- }
- public function testCanBuildMetaTagsWithAttributes()
- {
- $this->helper->setName('keywords', 'foo bar', array('lang' => 'us_en', 'scheme' => 'foo', 'bogus' => 'unused'));
- $value = $this->helper->getValue();
- $this->assertObjectHasAttribute('modifiers', $value);
- $modifiers = $value->modifiers;
- $this->assertTrue(array_key_exists('lang', $modifiers));
- $this->assertEquals('us_en', $modifiers['lang']);
- $this->assertTrue(array_key_exists('scheme', $modifiers));
- $this->assertEquals('foo', $modifiers['scheme']);
- }
- public function testToStringReturnsValidHtml()
- {
- $this->helper->setName('keywords', 'foo bar', array('lang' => 'us_en', 'scheme' => 'foo', 'bogus' => 'unused'))
- ->prependName('title', 'boo bah')
- ->appendHttpEquiv('screen', 'projection');
- $string = $this->helper->toString();
- $metas = substr_count($string, '<meta ');
- $this->assertEquals(3, $metas);
- $metas = substr_count($string, '/>');
- $this->assertEquals(3, $metas);
- $metas = substr_count($string, 'name="');
- $this->assertEquals(2, $metas);
- $metas = substr_count($string, 'http-equiv="');
- $this->assertEquals(1, $metas);
- $this->assertContains('http-equiv="screen" content="projection"', $string);
- $this->assertContains('name="keywords" content="foo bar"', $string);
- $this->assertContains('lang="us_en"', $string);
- $this->assertContains('scheme="foo"', $string);
- $this->assertNotContains('bogus', $string);
- $this->assertNotContains('unused', $string);
- $this->assertContains('name="title" content="boo bah"', $string);
- }
- /**
- * @group ZF-6637
- */
- public function testToStringWhenInvalidKeyProvidedShouldConvertThrownException()
- {
- $this->helper->headMeta('some-content', 'tag value', 'not allowed key');
- set_error_handler(array($this, 'handleErrors'));
- $string = @$this->helper->toString();
- $this->assertEquals('', $string);
- $this->assertTrue(is_string($this->error));
- }
- public function testHeadMetaHelperCreatesItemEntry()
- {
- $this->helper->headMeta('foo', 'keywords');
- $values = $this->helper->getArrayCopy();
- $this->assertEquals(1, count($values));
- $item = array_shift($values);
- $this->assertEquals('foo', $item->content);
- $this->assertEquals('name', $item->type);
- $this->assertEquals('keywords', $item->name);
- }
- public function testOverloadingOffsetInsertsAtOffset()
- {
- $this->helper->offsetSetName(100, 'keywords', 'foo');
- $values = $this->helper->getArrayCopy();
- $this->assertEquals(1, count($values));
- $this->assertTrue(array_key_exists(100, $values));
- $item = $values[100];
- $this->assertEquals('foo', $item->content);
- $this->assertEquals('name', $item->type);
- $this->assertEquals('keywords', $item->name);
- }
- public function testIndentationIsHonored()
- {
- $this->helper->setIndent(4);
- $this->helper->appendName('keywords', 'foo bar');
- $this->helper->appendName('seo', 'baz bat');
- $string = $this->helper->toString();
- $scripts = substr_count($string, ' <meta name=');
- $this->assertEquals(2, $scripts);
- }
- public function testStringRepresentationReflectsDoctype()
- {
- $this->view->doctype('HTML4_STRICT');
- $this->helper->headMeta('some content', 'foo');
- $test = $this->helper->toString();
- $this->assertNotContains('/>', $test);
- $this->assertContains('some content', $test);
- $this->assertContains('foo', $test);
- }
- /**
- * @group ZF-9743
- */
- public function testPropertyIsSupportedWithRdfaDoctype()
- {
- $this->view->doctype('XHTML1_RDFA');
- $this->helper->headMeta('foo', 'og:title', 'property');
- $this->assertEquals('<meta property="og:title" content="foo" />',
- $this->helper->toString()
- );
- }
- /**
- * @group ZF-9743
- */
- public function testPropertyIsNotSupportedByDefaultDoctype()
- {
- try {
- $this->helper->headMeta('foo', 'og:title', 'property');
- $this->fail('meta property attribute should not be supported on default doctype');
- } catch (Zend_View_Exception $e) {
- $this->assertContains('Invalid value passed', $e->getMessage());
- }
- }
- /**
- * @group ZF-9743
- * @depends testPropertyIsSupportedWithRdfaDoctype
- */
- public function testOverloadingAppendPropertyAppendsMetaTagToStack()
- {
- $this->view->doctype('XHTML1_RDFA');
- $this->_testOverloadAppend('property');
- }
- /**
- * @group ZF-9743
- * @depends testPropertyIsSupportedWithRdfaDoctype
- */
- public function testOverloadingPrependPropertyPrependsMetaTagToStack()
- {
- $this->view->doctype('XHTML1_RDFA');
- $this->_testOverloadPrepend('property');
- }
- /**
- * @group ZF-9743
- * @depends testPropertyIsSupportedWithRdfaDoctype
- */
- public function testOverloadingSetPropertyOverwritesMetaTagStack()
- {
- $this->view->doctype('XHTML1_RDFA');
- $this->_testOverloadSet('property');
- }
- /**
- * @group ZF-2663
- */
- public function testSetNameDoesntClobber()
- {
- $view = new Zend_View();
- $view->headMeta()->setName('keywords', 'foo');
- $view->headMeta()->appendHttpEquiv('pragma', 'bar');
- $view->headMeta()->appendHttpEquiv('Cache-control', 'baz');
- $view->headMeta()->setName('keywords', 'bat');
- $this->assertEquals(
- '<meta http-equiv="pragma" content="bar" />' . PHP_EOL . '<meta http-equiv="Cache-control" content="baz" />' . PHP_EOL . '<meta name="keywords" content="bat" />',
- $view->headMeta()->toString()
- );
- }
- /**
- * @group ZF-2663
- */
- public function testSetNameDoesntClobberPart2()
- {
- $view = new Zend_View();
- $view->headMeta()->setName('keywords', 'foo');
- $view->headMeta()->setName('description', 'foo');
- $view->headMeta()->appendHttpEquiv('pragma', 'baz');
- $view->headMeta()->appendHttpEquiv('Cache-control', 'baz');
- $view->headMeta()->setName('keywords', 'bar');
- $this->assertEquals(
- '<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" />',
- $view->headMeta()->toString()
- );
- }
- /**
- * @group ZF-3780
- * @link http://framework.zend.com/issues/browse/ZF-3780
- */
- public function testPlacesMetaTagsInProperOrder()
- {
- $view = new Zend_View();
- $view->headMeta()->setName('keywords', 'foo');
- $view->headMeta('some content', 'bar', 'name', array(), Zend_View_Helper_Placeholder_Container_Abstract::PREPEND);
- $this->assertEquals(
- '<meta name="bar" content="some content" />' . PHP_EOL . '<meta name="keywords" content="foo" />',
- $view->headMeta()->toString()
- );
- }
- /**
- * @group ZF-5435
- */
- public function testContainerMaintainsCorrectOrderOfItems()
- {
- $this->helper->offsetSetName(1, 'keywords', 'foo');
- $this->helper->offsetSetName(10, 'description', 'foo');
- $this->helper->offsetSetHttpEquiv(20, 'pragma', 'baz');
- $this->helper->offsetSetHttpEquiv(5, 'Cache-control', 'baz');
- $test = $this->helper->toString();
- $expected = '<meta name="keywords" content="foo" />' . PHP_EOL
- . '<meta http-equiv="Cache-control" content="baz" />' . PHP_EOL
- . '<meta name="description" content="foo" />' . PHP_EOL
- . '<meta http-equiv="pragma" content="baz" />';
- $this->assertEquals($expected, $test);
- }
- /**
- * @group ZF-7722
- */
- public function testCharsetValidateFail()
- {
- $view = new Zend_View();
- $view->doctype('HTML4_STRICT');
- try {
- $view->headMeta()->setCharset('utf-8');
- $this->fail('Should not be able to set charset for a HTML4 doctype');
- } catch (Zend_View_Exception $e) {}
- }
- /**
- * @group ZF-7722
- */
- public function testCharset() {
- $view = new Zend_View();
- $view->doctype('HTML5');
- $view->headMeta()->setCharset('utf-8');
- $this->assertEquals(
- '<meta charset="utf-8">',
- $view->headMeta()->toString());
- $view->doctype('XHTML5');
- $this->assertEquals(
- '<meta charset="utf-8"/>',
- $view->headMeta()->toString());
- }
-
- /**
- * @group ZF-11835
- */
- public function testConditional()
- {
- $html = $this->helper->appendHttpEquiv('foo', 'bar', array('conditional' => 'lt IE 7'))->toString();
-
- $this->assertRegExp("|^<!--\[if lt IE 7\]>|", $html);
- $this->assertRegExp("|<!\[endif\]-->$|", $html);
- }
- /**
- * @group ZF-11910
- */
- public function testStandaloneInstantiationWithoutViewDoesNotCauseFatalError()
- {
- $expected = '<meta name="foo" content="bar" />';
- $helper = new Zend_View_Helper_HeadMeta();
- $result = (string)$helper->headMeta()->appendName('foo','bar');
- $this->assertEquals($expected, $result);
- }
- /**
- * @group GH-515
- */
- public function testConditionalNoIE()
- {
- $html = $this->helper->appendHttpEquiv('foo', 'bar', array('conditional' => '!IE'))->toString();
- $this->assertContains('<!--[if !IE]><!--><', $html);
- $this->assertContains('<!--<![endif]-->', $html);
- }
- /**
- * @group GH-515
- */
- public function testConditionalNoIEWidthSpace()
- {
- $html = $this->helper->appendHttpEquiv('foo', 'bar', array('conditional' => '! IE'))->toString();
- $this->assertContains('<!--[if ! IE]><!--><', $html);
- $this->assertContains('<!--<![endif]-->', $html);
- }
- }
- // Call Zend_View_Helper_HeadMetaTest::main() if this source file is executed directly.
- if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_HeadMetaTest::main") {
- Zend_View_Helper_HeadMetaTest::main();
- }
|