/** * testHomeRoute */ public function testHomeRoute() { $promoted = array('plugin' => 'nodes', 'controller' => 'nodes', 'action' => 'promoted'); $result = CroogoRouter::connect('/', $promoted); $translateLoaded = CakePlugin::loaded('Translate'); $expected = $translateLoaded ? 2 : 1; $this->assertEquals($expected, count($result)); $this->assertNotEmpty($result[0]); $this->assertInstanceOf('CakeRoute', $result[0]); $reversed = Router::parse('/'); $this->assertEquals($promoted, array_intersect_key($promoted, $reversed)); // another route $index = array('plugin' => 'nodes', 'controller' => 'nodes', 'action' => 'index'); $result = CroogoRouter::connect('/nodes', $index); $expected = $translateLoaded ? 4 : 2; $this->assertEquals($expected, count($result)); $reversed = Router::parse('/'); $this->assertEquals($promoted, array_intersect_key($promoted, $reversed)); $terms = array('plugin' => 'nodes', 'controller' => 'nodes', 'action' => 'terms'); $result = CroogoRouter::connect('/', $terms); $expected = $translateLoaded ? 6 : 3; $this->assertEquals($expected, count($result)); // override '/' route Router::promote(); $reversed = Router::parse('/'); $this->assertEquals($terms, array_intersect_key($terms, $reversed)); }
/** * * @see Router::connect() */ private static function __connect($route, $default = array(), $params = array(), $options = array()) { $options = Hash::merge(array('promote' => false), $options); $localizedRoute = $route == '/' ? '' : $route; if (CakePlugin::loaded('Translate')) { Router::connect('/:locale' . $localizedRoute, $default, array_merge(array('locale' => '[a-z]{3}'), $params)); if ($options['promote']) { Router::promote(); } } $return = Router::connect($route, $default, $params); if ($options['promote']) { Router::promote(); } return $return; }
/** * testUrlGeneration method * * @access public * @return void */ function testUrlGeneration() { extract(Router::getNamedExpressions()); Router::setRequestInfo(array(array('pass' => array(), 'action' => 'index', 'plugin' => null, 'controller' => 'subscribe', 'admin' => true, 'url' => array('url' => '')), array('base' => '/magazine', 'here' => '/magazine', 'webroot' => '/magazine/', 'passedArgs' => array('page' => 2), 'namedArgs' => array('page' => 2)))); $result = Router::url(); $this->assertEqual('/magazine', $result); Router::reload(); Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); $out = Router::url(array('controller' => 'pages', 'action' => 'display', 'home')); $this->assertEqual($out, '/'); Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); $result = Router::url(array('controller' => 'pages', 'action' => 'display', 'about')); $expected = '/pages/about'; $this->assertEqual($result, $expected); Router::reload(); Router::parse('/'); Router::connect('/:plugin/:id/*', array('controller' => 'posts', 'action' => 'view'), array('id' => $ID)); $result = Router::url(array('plugin' => 'cake_plugin', 'controller' => 'posts', 'action' => 'view', 'id' => '1')); $expected = '/cake_plugin/1'; $this->assertEqual($result, $expected); $result = Router::url(array('plugin' => 'cake_plugin', 'controller' => 'posts', 'action' => 'view', 'id' => '1', '0')); $expected = '/cake_plugin/1/0'; $this->assertEqual($result, $expected); Router::reload(); Router::parse('/'); Router::connect('/:controller/:action/:id', array(), array('id' => $ID)); $result = Router::url(array('controller' => 'posts', 'action' => 'view', 'id' => '1')); $expected = '/posts/view/1'; $this->assertEqual($result, $expected); Router::reload(); Router::parse('/'); Router::connect('/:controller/:id', array('action' => 'view', 'id' => '1')); $result = Router::url(array('controller' => 'posts', 'action' => 'view', 'id' => '1')); $expected = '/posts/1'; $this->assertEqual($result, $expected); $result = Router::url(array('controller' => 'posts', 'action' => 'index', '0')); $expected = '/posts/index/0'; $this->assertEqual($result, $expected); $result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => 'var=test&var2=test2')); $expected = '/posts/index/0?var=test&var2=test2'; $this->assertEqual($result, $expected); $result = Router::url(array('controller' => 'posts', '0', '?' => 'var=test&var2=test2')); $this->assertEqual($result, $expected); $result = Router::url(array('controller' => 'posts', '0', '?' => array('var' => 'test', 'var2' => 'test2'))); $this->assertEqual($result, $expected); $result = Router::url(array('controller' => 'posts', '0', '?' => array('var' => null))); $this->assertEqual($result, '/posts/index/0'); $result = Router::url(array('controller' => 'posts', '0', '?' => 'var=test&var2=test2', '#' => 'unencoded string %')); $expected = '/posts/index/0?var=test&var2=test2#unencoded+string+%25'; $this->assertEqual($result, $expected); Router::connect('/view/*', array('controller' => 'posts', 'action' => 'view')); Router::promote(); $result = Router::url(array('controller' => 'posts', 'action' => 'view', '1')); $expected = '/view/1'; $this->assertEqual($result, $expected); Configure::write('Routing.admin', 'admin'); Router::reload(); Router::setRequestInfo(array(array('pass' => array(), 'action' => 'admin_index', 'plugin' => null, 'controller' => 'subscriptions', 'admin' => true, 'url' => array('url' => 'admin/subscriptions/index/page:2')), array('base' => '/magazine', 'here' => '/magazine/admin/subscriptions/index/page:2', 'webroot' => '/magazine/', 'passedArgs' => array('page' => 2)))); Router::parse('/'); $result = Router::url(array('page' => 3)); $expected = '/magazine/admin/subscriptions/index/page:3'; $this->assertEqual($result, $expected); Configure::write('Routing.admin', 'admin'); Router::reload(); Router::connect('/admin/subscriptions/:action/*', array('controller' => 'subscribe', 'admin' => true, 'prefix' => 'admin')); Router::setRequestInfo(array(array('pass' => array(), 'action' => 'admin_index', 'plugin' => null, 'controller' => 'subscribe', 'admin' => true, 'url' => array('url' => 'admin/subscriptions/edit/1')), array('base' => '/magazine', 'here' => '/magazine/admin/subscriptions/edit/1', 'webroot' => '/magazine/', 'passedArgs' => array('page' => 2), 'namedArgs' => array('page' => 2)))); Router::parse('/'); $result = Router::url(array('action' => 'edit', 1)); $expected = '/magazine/admin/subscriptions/edit/1'; $this->assertEqual($result, $expected); Router::reload(); Router::setRequestInfo(array(array('pass' => array(), 'action' => 'index', 'plugin' => null, 'controller' => 'real_controller_name', 'url' => array('url' => '')), array('base' => '/', 'here' => '/', 'webroot' => '/', 'passedArgs' => array('page' => 2), 'namedArgs' => array('page' => 2)))); Router::connect('short_controller_name/:action/*', array('controller' => 'real_controller_name')); Router::parse('/'); $result = Router::url(array('controller' => 'real_controller_name', 'page' => '1')); $expected = '/short_controller_name/index/page:1'; $this->assertEqual($result, $expected); $result = Router::url(array('action' => 'add')); $expected = '/short_controller_name/add'; $this->assertEqual($result, $expected); Router::reload(); Router::connect(':language/galleries', array('controller' => 'galleries', 'action' => 'index'), array('language' => '[a-z]{3}')); Router::connect('/:language/:admin/:controller/:action/*', array('admin' => 'admin'), array('language' => '[a-z]{3}', 'admin' => 'admin')); Router::connect('/:language/:controller/:action/*', array(), array('language' => '[a-z]{3}')); $result = Router::url(array('admin' => false, 'language' => 'dan', 'action' => 'index', 'controller' => 'galleries')); $expected = '/dan/galleries'; $this->assertEqual($result, $expected); $result = Router::url(array('admin' => false, 'language' => 'eng', 'action' => 'index', 'controller' => 'galleries')); $expected = '/eng/galleries'; $this->assertEqual($result, $expected); Router::reload(); Router::connect('/:language/pages', array('controller' => 'pages', 'action' => 'index'), array('language' => '[a-z]{3}')); Router::connect('/:language/:controller/:action/*', array(), array('language' => '[a-z]{3}')); $result = Router::url(array('language' => 'eng', 'action' => 'index', 'controller' => 'pages')); $expected = '/eng/pages'; $this->assertEqual($result, $expected); $result = Router::url(array('language' => 'eng', 'controller' => 'pages')); $this->assertEqual($result, $expected); $result = Router::url(array('language' => 'eng', 'controller' => 'pages', 'action' => 'add')); $expected = '/eng/pages/add'; $this->assertEqual($result, $expected); Router::reload(); Router::parse('/'); Router::setRequestInfo(array(array('pass' => array(), 'action' => 'index', 'plugin' => null, 'controller' => 'users', 'url' => array('url' => 'users')), array('base' => '/', 'here' => '/', 'webroot' => '/', 'passedArgs' => array(), 'argSeparator' => ':', 'namedArgs' => array()))); $result = Router::url(array('action' => 'login')); $expected = '/users/login'; $this->assertEqual($result, $expected); Router::reload(); Router::parse('/'); Router::connect('/page/*', array('plugin' => null, 'controller' => 'pages', 'action' => 'view')); $result = Router::url(array('plugin' => 'my_plugin', 'controller' => 'pages', 'action' => 'view', 'my-page')); $expected = '/my_plugin/pages/view/my-page'; $this->assertEqual($result, $expected); Router::reload(); Router::parse('/'); Router::connect('/forestillinger/:month/:year/*', array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar'), array('month' => '0[1-9]|1[012]', 'year' => '[12][0-9]{3}')); $result = Router::url(array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'month' => 10, 'year' => 2007, 'min-forestilling')); $expected = '/forestillinger/10/2007/min-forestilling'; $this->assertEqual($result, $expected); Router::reload(); Router::parse('/'); Router::connect('/contact/:action', array('plugin' => 'contact', 'controller' => 'contact')); $result = Router::url(array('plugin' => 'contact', 'controller' => 'contact', 'action' => 'me')); $expected = '/contact/me'; $this->assertEqual($result, $expected); Configure::write('Routing.admin', 'admin'); Router::reload(); Router::parse('/'); $result = Router::url(array('admin' => true, 'controller' => 'users', 'action' => 'login')); $expected = '/admin/users/login'; $this->assertEqual($result, $expected); Router::reload(); Router::parse('/'); Router::connect('/kalender/:month/:year/*', array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar'), array('month' => '0[1-9]|1[012]', 'year' => '[12][0-9]{3}')); Router::connect('/kalender/*', array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar')); $result = Router::url(array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'min-forestilling')); $expected = '/kalender/min-forestilling'; $this->assertEqual($result, $expected); $result = Router::url(array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'year' => 2007, 'month' => 10, 'min-forestilling')); $expected = '/kalender/10/2007/min-forestilling'; $this->assertEqual($result, $expected); Configure::write('Routing.admin', 'admin'); Router::reload(); Router::setRequestInfo(array(array('pass' => array(), 'admin' => true, 'action' => 'index', 'plugin' => null, 'controller' => 'users', 'url' => array('url' => 'users')), array('base' => '/', 'here' => '/', 'webroot' => '/', 'passedArgs' => array(), 'argSeparator' => ':', 'namedArgs' => array()))); Router::connect('/page/*', array('controller' => 'pages', 'action' => 'view', 'admin' => true, 'prefix' => 'admin')); Router::parse('/'); $result = Router::url(array('admin' => true, 'controller' => 'pages', 'action' => 'view', 'my-page')); $expected = '/page/my-page'; $this->assertEqual($result, $expected); Router::reload(); Router::setRequestInfo(array(array('pass' => array(), 'action' => 'index', 'plugin' => 'myplugin', 'controller' => 'mycontroller', 'admin' => false, 'url' => array('url' => array())), array('base' => '/', 'here' => '/', 'webroot' => '/', 'passedArgs' => array(), 'namedArgs' => array()))); $result = Router::url(array('plugin' => null, 'controller' => 'myothercontroller')); $expected = '/myothercontroller/'; $this->assertEqual($result, $expected); Configure::write('Routing.admin', 'admin'); Router::reload(); Router::setRequestInfo(array(array('plugin' => null, 'controller' => 'pages', 'action' => 'admin_add', 'pass' => array(), 'prefix' => 'admin', 'admin' => true, 'form' => array(), 'url' => array('url' => 'admin/pages/add')), array('plugin' => null, 'controller' => null, 'action' => null, 'base' => '', 'here' => '/admin/pages/add', 'webroot' => '/'))); Router::parse('/'); $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false)); $expected = '/admin/pages/add'; $this->assertEqual($result, $expected); Router::reload(); Router::setRequestInfo(array(array('plugin' => null, 'controller' => 'pages', 'action' => 'admin_edit', 'pass' => array('284'), 'prefix' => 'admin', 'admin' => true, 'form' => array(), 'url' => array('url' => 'admin/pages/edit/284')), array('plugin' => null, 'controller' => null, 'action' => null, 'base' => '', 'here' => '/admin/pages/edit/284', 'webroot' => '/'))); Router::connect('/admin/:controller/:action/:id', array('admin' => true), array('id' => '[0-9]+')); Router::parse('/'); $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'edit', 'id' => '284')); $expected = '/admin/pages/edit/284'; $this->assertEqual($result, $expected); Configure::write('Routing.admin', 'admin'); Router::reload(); Router::setRequestInfo(array(array('plugin' => null, 'controller' => 'pages', 'action' => 'admin_add', 'pass' => array(), 'prefix' => 'admin', 'admin' => true, 'form' => array(), 'url' => array('url' => 'admin/pages/add')), array('plugin' => null, 'controller' => null, 'action' => null, 'base' => '', 'here' => '/admin/pages/add', 'webroot' => '/'))); Router::parse('/'); $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false)); $expected = '/admin/pages/add'; $this->assertEqual($result, $expected); Router::reload(); Router::setRequestInfo(array(array('plugin' => null, 'controller' => 'pages', 'action' => 'admin_edit', 'pass' => array('284'), 'prefix' => 'admin', 'admin' => true, 'form' => array(), 'url' => array('url' => 'admin/pages/edit/284')), array('plugin' => null, 'controller' => null, 'action' => null, 'base' => '', 'here' => '/admin/pages/edit/284', 'webroot' => '/'))); Router::parse('/'); $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'edit', 'id' => '284')); $expected = '/admin/pages/edit/284'; $this->assertEqual($result, $expected); Router::reload(); Router::setRequestInfo(array(array('plugin' => 'shows', 'controller' => 'show_tickets', 'action' => 'admin_edit', 'pass' => array('6'), 'prefix' => 'admin', 'admin' => true, 'form' => array(), 'url' => array('url' => 'admin/shows/show_tickets/edit/6')), array('plugin' => null, 'controller' => null, 'action' => null, 'base' => '', 'here' => '/admin/shows/show_tickets/edit/6', 'webroot' => '/'))); Router::parse('/'); $result = Router::url(array('plugin' => 'shows', 'controller' => 'show_tickets', 'action' => 'edit', 'id' => '6', 'admin' => true, 'prefix' => 'admin')); $expected = '/admin/shows/show_tickets/edit/6'; $this->assertEqual($result, $expected); Router::reload(); Router::setRequestInfo(array(array('pass' => array(), 'action' => 'admin_index', 'plugin' => null, 'controller' => 'posts', 'prefix' => 'admin', 'admin' => true, 'url' => array('url' => 'admin/posts')), array('base' => '', 'here' => '/admin/posts', 'webroot' => '/'))); Router::connect('/admin/posts/*', array('controller' => 'posts', 'action' => 'index', 'admin' => true)); Router::parse('/'); $result = Router::url(array('all')); $expected = '/admin/posts/all'; $this->assertEqual($result, $expected); }
/** * test generation of basic urls. * * @return void */ public function testUrlGenerationBasic() { extract(Router::getNamedExpressions()); $request = new CakeRequest(); $request->addParams(array('action' => 'index', 'plugin' => null, 'controller' => 'subscribe', 'admin' => true)); $request->base = '/magazine'; $request->here = '/magazine'; $request->webroot = '/magazine/'; Router::setRequestInfo($request); $result = Router::url(); $this->assertEquals('/magazine', $result); Router::reload(); Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); $out = Router::url(array('controller' => 'pages', 'action' => 'display', 'home')); $this->assertEquals($out, '/'); Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); $result = Router::url(array('controller' => 'pages', 'action' => 'display', 'about')); $expected = '/pages/about'; $this->assertEquals($expected, $result); Router::reload(); Router::connect('/:plugin/:id/*', array('controller' => 'posts', 'action' => 'view'), array('id' => $ID)); Router::parse('/'); $result = Router::url(array('plugin' => 'cake_plugin', 'controller' => 'posts', 'action' => 'view', 'id' => '1')); $expected = '/cake_plugin/1'; $this->assertEquals($expected, $result); $result = Router::url(array('plugin' => 'cake_plugin', 'controller' => 'posts', 'action' => 'view', 'id' => '1', '0')); $expected = '/cake_plugin/1/0'; $this->assertEquals($expected, $result); Router::reload(); Router::connect('/:controller/:action/:id', array(), array('id' => $ID)); Router::parse('/'); $result = Router::url(array('controller' => 'posts', 'action' => 'view', 'id' => '1')); $expected = '/posts/view/1'; $this->assertEquals($expected, $result); Router::reload(); Router::connect('/:controller/:id', array('action' => 'view')); Router::parse('/'); $result = Router::url(array('controller' => 'posts', 'action' => 'view', 'id' => '1')); $expected = '/posts/1'; $this->assertEquals($expected, $result); $result = Router::url(array('controller' => 'posts', 'action' => 'index', '0')); $expected = '/posts/index/0'; $this->assertEquals($expected, $result); Router::connect('/view/*', array('controller' => 'posts', 'action' => 'view')); Router::promote(); $result = Router::url(array('controller' => 'posts', 'action' => 'view', '1')); $expected = '/view/1'; $this->assertEquals($expected, $result); Router::reload(); $request = new CakeRequest(); $request->addParams(array('action' => 'index', 'plugin' => null, 'controller' => 'real_controller_name')); $request->base = '/'; $request->here = '/'; $request->webroot = '/'; Router::setRequestInfo($request); Router::connect('short_controller_name/:action/*', array('controller' => 'real_controller_name')); Router::parse('/'); $result = Router::url(array('controller' => 'real_controller_name', 'page' => '1')); $expected = '/short_controller_name/index/page:1'; $this->assertEquals($expected, $result); $result = Router::url(array('action' => 'add')); $expected = '/short_controller_name/add'; $this->assertEquals($expected, $result); Router::reload(); Router::parse('/'); $request = new CakeRequest(); $request->addParams(array('action' => 'index', 'plugin' => null, 'controller' => 'users', 'url' => array('url' => 'users'))); $request->base = '/'; $request->here = '/'; $request->webroot = '/'; Router::setRequestInfo($request); $result = Router::url(array('action' => 'login')); $expected = '/users/login'; $this->assertEquals($expected, $result); Router::reload(); Router::connect('/page/*', array('plugin' => null, 'controller' => 'pages', 'action' => 'view')); Router::parse('/'); $result = Router::url(array('plugin' => 'my_plugin', 'controller' => 'pages', 'action' => 'view', 'my-page')); $expected = '/my_plugin/pages/view/my-page'; $this->assertEquals($expected, $result); Router::reload(); Router::connect('/contact/:action', array('plugin' => 'contact', 'controller' => 'contact')); Router::parse('/'); $result = Router::url(array('plugin' => 'contact', 'controller' => 'contact', 'action' => 'me')); $expected = '/contact/me'; $this->assertEquals($expected, $result); Router::reload(); $request = new CakeRequest(); $request->addParams(array('action' => 'index', 'plugin' => 'myplugin', 'controller' => 'mycontroller', 'admin' => false)); $request->base = '/'; $request->here = '/'; $request->webroot = '/'; Router::setRequestInfo($request); $result = Router::url(array('plugin' => null, 'controller' => 'myothercontroller')); $expected = '/myothercontroller'; $this->assertEquals($expected, $result); }
/** * test updateAllNodesPaths */ public function testUpdateAllNodesPaths() { $this->Node->id = 1; $result = $this->Node->saveField('path', 'invalid one'); $this->assertTrue((bool) $result); CroogoRouter::connect('/blog/:slug', array('plugin' => 'nodes', 'controller' => 'nodes', 'action' => 'view', 'type' => 'blog')); Router::promote(); $result = $this->Node->updateAllNodesPaths(); $this->assertTrue($result); $this->Node->type = 'blog'; $node = $this->Node->findById(1); $this->assertEquals('/blog/hello-world', $node['Node']['path']); }
/** * Promote all the lang routes before their automatically created route for the default language * * @return void */ public static function promoteLangRoutes() { $routesList = Configure::read('I18nRoute.routes'); if (!empty($routesList)) { $Router = Router::getInstance(); $lastIndex = count($Router->routes) - 1; rsort($routesList); foreach($routesList as $langRouteIndex) { while ($langRouteIndex < $lastIndex) { Router::promote(); $lastIndex--; } Router::promote(count($Router->routes) - 2); Router::promote(); $lastIndex = $langRouteIndex - 2; } Configure::write('I18nRoute.routes', array()); } }
<?php $routePrefix = strtolower(Configure::read('Admin.routingPrefix')); $routeName = strtolower(Configure::read('Admin.routingName')); $pluginName = Configure::read('Admin.pluginName'); Router::connect("/users/login", array('plugin' => 'users', 'controller' => 'users', 'action' => 'login')); Router::connect("/users/:action/*", array('plugin' => 'users', 'controller' => 'users', 'action' => 'index')); Router::promote(); Router::connect("/users/:action", array('plugin' => 'users', 'controller' => 'users')); Router::promote(); Router::connect("/{$routeName}/users/:action/*", array($routePrefix => true, 'plugin' => 'users', 'prefix' => $routePrefix, 'controller' => 'users', 'action' => 'index')); Router::promote(); Router::connect("/{$routeName}/users", array($routePrefix => true, 'plugin' => 'users', 'prefix' => $routePrefix, 'controller' => 'users', 'action' => 'index')); Router::promote();