| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878 |
- <?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_Navigation
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- require_once 'Zend/Navigation/Page/Mvc.php';
- require_once 'Zend/Controller/Request/Http.php';
- require_once 'Zend/Controller/Router/Route.php';
- require_once 'Zend/Controller/Router/Route/Regex.php';
- require_once 'Zend/Controller/Router/Route/Chain.php';
- /**
- * Tests the class Zend_Navigation_Page_Mvc
- *
- * @category Zend
- * @package Zend_Navigation
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Navigation
- */
- class Zend_Navigation_Page_MvcTest extends PHPUnit_Framework_TestCase
- {
- protected $_front;
- protected $_oldRequest;
- protected $_oldRouter;
- protected function setUp()
- {
- $this->_front = Zend_Controller_Front::getInstance();
- $this->_oldRequest = $this->_front->getRequest();
- $this->_oldRouter = $this->_front->getRouter();
- $this->_front->resetInstance();
- $this->_front->setRequest(new Zend_Controller_Request_Http());
- $this->_front->getRouter()->addDefaultRoutes();
- }
- protected function tearDown()
- {
- if (null !== $this->_oldRequest) {
- $this->_front->setRequest($this->_oldRequest);
- } else {
- $this->_front->setRequest(new Zend_Controller_Request_Http());
- }
- $this->_front->setRouter($this->_oldRouter);
- }
- public function testHrefGeneratedByUrlHelperRequiresNoRoute()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'index',
- 'controller' => 'index'
- ));
- $page->setAction('view');
- $page->setController('news');
- $this->assertEquals('/news/view', $page->getHref());
- }
- public function testHrefGeneratedIsRouteAware()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'myaction',
- 'controller' => 'mycontroller',
- 'route' => 'myroute',
- 'params' => array(
- 'page' => 1337
- )
- ));
- $this->_front->getRouter()->addRoute(
- 'myroute',
- new Zend_Controller_Router_Route(
- 'lolcat/:action/:page',
- array(
- 'module' => 'default',
- 'controller' => 'foobar',
- 'action' => 'bazbat',
- 'page' => 1
- )
- )
- );
- $this->assertEquals('/lolcat/myaction/1337', $page->getHref());
- }
- /**
- * @group ZF-8922
- */
- public function testGetHrefWithFragmentIdentifier()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'fragment' => 'qux',
- 'controller' => 'mycontroller',
- 'action' => 'myaction',
- 'route' => 'myroute',
- 'params' => array(
- 'page' => 1337
- )
- ));
-
- $this->_front->getRouter()->addRoute(
- 'myroute',
- new Zend_Controller_Router_Route(
- 'lolcat/:action/:page',
- array(
- 'module' => 'default',
- 'controller' => 'foobar',
- 'action' => 'bazbat',
- 'page' => 1
- )
- )
- );
-
- $this->assertEquals('/lolcat/myaction/1337#qux', $page->getHref());
- }
- public function testIsActiveReturnsTrueOnIdenticalModuleControllerAction()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'index',
- 'controller' => 'index'
- ));
- $this->_front->getRequest()->setParams(array(
- 'module' => 'default',
- 'controller' => 'index',
- 'action' => 'index'
- ));
- $this->assertEquals(true, $page->isActive());
- }
- public function testIsActiveReturnsFalseOnDifferentModuleControllerAction()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'bar',
- 'controller' => 'index'
- ));
- $this->_front->getRequest()->setParams(array(
- 'module' => 'default',
- 'controller' => 'index',
- 'action' => 'index'
- ));
- $this->assertEquals(false, $page->isActive());
- }
- public function testIsActiveReturnsTrueOnIdenticalIncludingPageParams()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'view',
- 'controller' => 'post',
- 'module' => 'blog',
- 'params' => array(
- 'id' => '1337'
- )
- ));
- $this->_front->getRequest()->setParams(array(
- 'module' => 'blog',
- 'controller' => 'post',
- 'action' => 'view',
- 'id' => '1337'
- ));
- $this->assertEquals(true, $page->isActive());
- }
- public function testIsActiveReturnsTrueWhenRequestHasMoreParams()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'view',
- 'controller' => 'post',
- 'module' => 'blog'
- ));
- $this->_front->getRequest()->setParams(array(
- 'module' => 'blog',
- 'controller' => 'post',
- 'action' => 'view',
- 'id' => '1337'
- ));
- $this->assertEquals(true, $page->isActive());
- }
- public function testIsActiveReturnsFalseWhenRequestHasLessParams()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'view',
- 'controller' => 'post',
- 'module' => 'blog',
- 'params' => array(
- 'id' => '1337'
- )
- ));
- $this->_front->getRequest()->setParams(array(
- 'module' => 'blog',
- 'controller' => 'post',
- 'action' => 'view',
- 'id' => null
- ));
- $this->assertEquals(false, $page->isActive());
- }
- public function testIsActiveIsRouteAware()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'myaction',
- 'route' => 'myroute',
- 'params' => array(
- 'page' => 1337
- )
- ));
- $this->_front->getRouter()->addRoute(
- 'myroute',
- new Zend_Controller_Router_Route(
- 'lolcat/:action/:page',
- array(
- 'module' => 'default',
- 'controller' => 'foobar',
- 'action' => 'bazbat',
- 'page' => 1
- )
- )
- );
- $this->_front->getRequest()->setParams(array(
- 'module' => 'default',
- 'controller' => 'foobar',
- 'action' => 'myaction',
- 'page' => 1337
- ));
- $this->assertEquals(true, $page->isActive());
- }
- /**
- * @group ZF-11664
- */
- public function testIsActiveWithoutAndWithRecursiveOption()
- {
- // Parent
- $page = new Zend_Navigation_Page_Mvc(array(
- 'controller' => 'index',
- 'action' => 'index',
- ));
- // Child
- $page->addPage(new Zend_Navigation_Page_Mvc(array(
- 'controller' => 'index',
- 'action' => 'foo',
- )));
- // Front controller
- $this->_front->getRequest()->setParams(array(
- 'controller' => 'index',
- 'action' => 'foo'
- ));
- $this->assertFalse($page->isActive());
- $this->assertTrue($page->isActive(true));
- }
- public function testActionAndControllerAccessors()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'index',
- 'controller' => 'index'
- ));
- $props = array('Action', 'Controller');
- $valids = array('index', 'help', 'home', 'default', '1', ' ', '', null);
- $invalids = array(42, (object) null);
- foreach ($props as $prop) {
- $setter = "set$prop";
- $getter = "get$prop";
- foreach ($valids as $valid) {
- $page->$setter($valid);
- $this->assertEquals($valid, $page->$getter());
- }
- foreach ($invalids as $invalid) {
- try {
- $page->$setter($invalid);
- $msg = "'$invalid' is invalid for $setter(), but no ";
- $msg .= 'Zend_Navigation_Exception was thrown';
- $this->fail($msg);
- } catch (Zend_Navigation_Exception $e) {
- }
- }
- }
- }
- public function testModuleAndRouteAccessors()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'index',
- 'controller' => 'index'
- ));
- $props = array('Module', 'Route');
- $valids = array('index', 'help', 'home', 'default', '1', ' ', null);
- $invalids = array(42, (object) null);
- foreach ($props as $prop) {
- $setter = "set$prop";
- $getter = "get$prop";
- foreach ($valids as $valid) {
- $page->$setter($valid);
- $this->assertEquals($valid, $page->$getter());
- }
- foreach ($invalids as $invalid) {
- try {
- $page->$setter($invalid);
- $msg = "'$invalid' is invalid for $setter(), but no ";
- $msg .= 'Zend_Navigation_Exception was thrown';
- $this->fail($msg);
- } catch (Zend_Navigation_Exception $e) {
- }
- }
- }
- }
- public function testSetAndGetResetParams()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'index',
- 'controller' => 'index'
- ));
- $valids = array(true, 1, '1', 3.14, 'true', 'yes');
- foreach ($valids as $valid) {
- $page->setResetParams($valid);
- $this->assertEquals(true, $page->getResetParams());
- }
- $invalids = array(false, 0, '0', 0.0, array());
- foreach ($invalids as $invalid) {
- $page->setResetParams($invalid);
- $this->assertEquals(false, $page->getResetParams());
- }
- }
- public function testSetAndGetParams()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'index',
- 'controller' => 'index'
- ));
- $params = array('foo' => 'bar', 'baz' => 'bat');
- $page->setParams($params);
- $this->assertEquals($params, $page->getParams());
- $page->setParams();
- $this->assertEquals(array(), $page->getParams());
- $page->setParams($params);
- $this->assertEquals($params, $page->getParams());
- $page->setParams(array());
- $this->assertEquals(array(), $page->getParams());
- }
-
- /**
- * @group ZF-10727
- */
- public function testSetAndGetParam()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'index',
- 'controller' => 'index'
- ));
-
- $page->setParam('foo', 'bar');
- $this->assertEquals('bar', $page->getParam('foo'));
-
- // Check type conversion
- $page->setParam(null, null);
- $this->assertEquals(null, $page->getParam('null'));
- }
-
- /**
- * @group ZF-10727
- */
- public function testAddParams()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'index',
- 'controller' => 'index'
- ));
-
- $params = array('foo' => 'bar', 'baz' => 'bat');
-
- $page->addParams($params);
- $this->assertEquals($params, $page->getParams());
-
- $params2 = array('qux' => 'foobar');
-
- $page->addParams($params2);
- $this->assertEquals(array_merge($params, $params2), $page->getParams());
- }
-
- /**
- * @group ZF-10727
- */
- public function testRemoveParam()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'index',
- 'controller' => 'index'
- ));
-
- $params = array('foo' => 'bar', 'baz' => 'bat');
-
- $page->setParams($params);
- $page->removeParam('foo');
-
- $this->assertEquals(array('baz' => 'bat'), $page->getParams());
-
- $this->assertNull($page->getParam('foo'));
- }
-
- /**
- * @group ZF-10727
- */
- public function testClearParams()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'index',
- 'controller' => 'index'
- ));
-
- $params = array('foo' => 'bar', 'baz' => 'bat');
-
- $page->setParams($params);
- $page->clearParams();
-
- $this->assertEquals(array(), $page->getParams());
- }
- /**
- * @group ZF-11664
- */
- public function testSetActiveAndIsActive()
- {
- // Page
- $page = new Zend_Navigation_Page_Mvc(array(
- 'controller' => 'foo',
- 'action' => 'bar',
- ));
- // Front controller
- $this->_front->getRequest()->setParams(array(
- 'controller' => 'foo',
- 'action' => 'bar'
- ));
- $this->assertTrue($page->isActive());
- $page->setActive(false);
- $this->assertFalse($page->isActive());
- }
- /**
- * @group ZF-10465
- */
- public function testSetAndGetEncodeUrl()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'action' => 'index',
- 'controller' => 'index',
- ));
-
- $page->setEncodeUrl(false);
- $this->assertEquals(false, $page->getEncodeUrl());
- }
-
- /**
- * @group ZF-10465
- */
- public function testEncodeUrlIsRouteAware()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'label' => 'foo',
- 'route' => 'myroute',
- 'encodeUrl' => false,
- 'params' => array(
- 'contentKey' => 'pagexy/subpage',
- )
- ));
-
- $this->_front->getRouter()->addRoute(
- 'myroute',
- new Zend_Controller_Router_Route_Regex(
- '(.+)\.html',
- array(
- 'module' => 'default',
- 'controller' => 'foobar',
- 'action' => 'bazbat',
- ),
- array(
- 1 => 'contentKey'
- ),
- '%s.html'
- )
- );
- $this->assertEquals('/pagexy/subpage.html', $page->getHref());
- }
- /**
- * @group ZF-7794
- */
- public function testSetScheme()
- {
- $page = new Zend_Navigation_Page_Mvc();
- $page->setScheme('https');
- $this->assertEquals('https', $page->getScheme());
- }
- /**
- * @group ZF-7794
- */
- public function testOptionScheme()
- {
- $page = new Zend_Navigation_Page_Mvc(
- array(
- 'scheme' => 'https',
- )
- );
- $this->assertEquals('https', $page->getScheme());
- }
- /**
- * @group ZF-7794
- */
- public function testHrefGeneratedWithScheme()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'controller' => 'foo',
- 'action' => 'bar',
- 'scheme' => 'https',
- ));
- $_SERVER['HTTP_HOST'] = 'foobar.example.com';
- $this->assertEquals(
- 'https://foobar.example.com/foo/bar',
- $page->getHref()
- );
- }
- /**
- * @group ZF-7794
- */
- public function testHrefGeneratedWithSchemeIsRouteAware()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'action' => 'myaction',
- 'controller' => 'mycontroller',
- 'route' => 'myroute',
- 'params' => array(
- 'page' => 1337,
- ),
- 'scheme' => 'https',
- ));
- $this->_front->getRouter()->addRoute(
- 'myroute',
- new Zend_Controller_Router_Route(
- 'lolcat/:action/:page',
- array(
- 'module' => 'default',
- 'controller' => 'foobar',
- 'action' => 'bazbat',
- 'page' => 1,
- )
- )
- );
- $_SERVER['HTTP_HOST'] = 'foobar.example.com';
- $this->assertEquals(
- 'https://foobar.example.com/lolcat/myaction/1337',
- $page->getHref()
- );
- }
- public function testToArrayMethod()
- {
- $options = array(
- 'label' => 'foo',
- 'action' => 'index',
- 'controller' => 'index',
- 'module' => 'test',
- 'fragment' => 'bar',
- 'id' => 'my-id',
- 'class' => 'my-class',
- 'title' => 'my-title',
- 'target' => 'my-target',
- 'order' => 100,
- 'active' => true,
- 'visible' => false,
- 'encodeUrl' => false,
- 'scheme' => 'https',
- 'foo' => 'bar',
- 'meaning' => 42
- );
- $page = new Zend_Navigation_Page_Mvc($options);
- $toArray = $page->toArray();
- $options['reset_params'] = true;
- $options['route'] = null;
- $options['params'] = array();
- $options['rel'] = array();
- $options['rev'] = array();
- $this->assertEquals(
- array(),
- array_diff_assoc($options, $page->toArray())
- );
- }
- public function testSpecifyingAnotherUrlHelperToGenerateHrefs()
- {
- $path = dirname(dirname(__FILE__)) . '/_files/My/UrlHelper.php';
- require_once $path;
- $newHelper = new My_UrlHelper();
- Zend_Navigation_Page_Mvc::setUrlHelper($newHelper);
- $page = new Zend_Navigation_Page_Mvc();
- $expected = My_UrlHelper::RETURN_URL;
- $actual = $page->getHref();
- $old = Zend_Controller_Action_HelperBroker::getStaticHelper('Url');
- Zend_Navigation_Page_Mvc::setUrlHelper($old);
- $this->assertEquals($expected, $actual);
- }
- /**
- * @group ZF-7794
- */
- public function testSpecifyingAnotherSchemeHelperToGenerateHrefs()
- {
- $path = dirname(dirname(__FILE__)) . '/_files/My/SchemeHelper.php';
- require_once $path;
- $newHelper = new My_SchemeHelper();
- Zend_Navigation_Page_Mvc::setSchemeHelper($newHelper);
- $page = new Zend_Navigation_Page_Mvc(
- array(
- 'scheme' => 'https',
- )
- );
- $expected = My_SchemeHelper::RETURN_URL;
- $actual = $page->getHref();
- $old = new Zend_View_Helper_ServerUrl();
- Zend_Navigation_Page_Mvc::setSchemeHelper($old);
- $this->assertEquals($expected, $actual);
- }
- /**
- * @group ZF-11550
- */
- public function testNullValuesInMatchedRouteWillStillReturnMatchedPage()
- {
- $page = new Zend_Navigation_Page_Mvc(array(
- 'route' => 'default',
- 'module' => 'default',
- 'controller' => 'index',
- 'action' => 'index',
- 'label' => 'Home',
- 'title' => 'Home',
- ));
- $this->_front->getRouter()->addRoute(
- 'default',
- new Zend_Controller_Router_Route(
- ':locale/:module/:controller/:action/*',
- array(
- 'locale' => null,
- 'module' => 'default',
- 'controller' => 'index',
- 'action' => 'index',
- ),
- array(
- 'locale' => '.*',
- 'module' => '.*',
- 'controller' => '.*',
- 'action' => '.*',
- )
- )
- );
- $this->_front->getRequest()->setParams(array(
- 'locale' => 'en_US',
- 'module' => 'default',
- 'controller' => 'index',
- 'action' => 'index',
- ));
- $this->assertEquals(true, $page->isActive());
- }
- /**
- * @group ZF-12414
- */
- public function testNullValueInParameters()
- {
- // Create pages
- $pages = array();
- $pages['home'] = new Zend_Navigation_Page_Mvc(
- array(
- 'label' => 'Home',
- 'route' => 'page',
- 'params' => array(
- 'slug' => '',
- ),
- )
- );
- $pages['news'] = new Zend_Navigation_Page_Mvc(
- array(
- 'label' => 'News',
- 'route' => 'page',
- 'params' => array(
- 'slug' => 'news',
- ),
- )
- );
- // Add route
- $this->_front->getRouter()->addRoute(
- 'page',
- new Zend_Controller_Router_Route_Regex(
- '((?!(admin|page)).*)',
- array(
- 'module' => 'page',
- 'controller' => 'index',
- 'action' => 'index',
- ),
- array(
- 1 => 'slug',
- ),
- '%s'
- )
- );
- // Set request
- $this->_front->getRequest()->setParams(
- array(
- 'module' => 'page',
- 'controller' => 'index',
- 'action' => 'index',
- 'slug' => 'news',
- )
- );
- $this->assertTrue($pages['news']->isActive());
- $this->assertFalse($pages['home']->isActive());
- }
- /**
- * @group ZF-11442
- */
- public function testIsActiveIsChainedRouteAware()
- {
- // Create page
- $page = new Zend_Navigation_Page_Mvc(
- array(
- 'action' => 'myaction',
- 'route' => 'myroute',
- 'params' => array(
- 'page' => 1337,
- 'item' => 1234
- )
- )
- );
- // Create chained route
- $chain = new Zend_Controller_Router_Route_Chain();
- $foo = new Zend_Controller_Router_Route(
- 'lolcat/:action',
- array(
- 'module' => 'default',
- 'controller' => 'foobar',
- 'action' => 'bazbat'
- )
- );
- $bar = new Zend_Controller_Router_Route(
- ':page/:item',
- array(
- 'page' => 1,
- 'item' => 1
- )
- );
- $chain->chain($foo)->chain($bar);
- // Set up router
- $this->_front->getRouter()->addRoute('myroute', $chain);
- $this->_front->getRequest()->setParams(
- array(
- 'module' => 'default',
- 'controller' => 'foobar',
- 'action' => 'myaction',
- 'page' => 1337,
- 'item' => 1234
- )
- );
- // Test
- $this->assertTrue($page->isActive());
- }
- }
|