📄 router.test.php
字号:
$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 (0 => '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); }/** * testUrlGenerationWithPrefix method * * @access public * @return void */ function testUrlGenerationWithPrefix() { Configure::write('Routing.admin', 'admin'); Router::reload(); Router::connectNamed(array('event', 'lang')); Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); Router::connect('/pages/contact_us', array('controller' => 'pages', 'action' => 'contact_us')); Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); Router::connect('/reset/*', array('admin' => true, 'controller' => 'users', 'action' => 'reset')); Router::connect('/tests', array('controller' => 'tests', 'action' => 'index')); Router::parseExtensions('rss'); Router::setRequestInfo(array( array('pass' => array(), 'named' => array(), 'controller' => 'registrations', 'action' => 'admin_index', 'plugin' => '', 'prefix' => 'admin', 'admin' => true, 'url' => array('ext' => 'html', 'url' => 'admin/registrations/index'), 'form' => array()), array('base' => '', 'here' => '/admin/registrations/index', 'webroot' => '/') )); $result = Router::url(array('page' => 2)); $expected = '/admin/registrations/index/page:2'; $this->assertEqual($result, $expected); }/** * testUrlGenerationWithExtensions method * * @access public * @return void */ function testUrlGenerationWithExtensions() { Router::parse('/'); $result = Router::url(array('plugin' => null, 'controller' => 'articles', 'action' => 'add', 'id' => null, 'ext' => 'json')); $expected = '/articles/add.json'; $this->assertEqual($result, $expected); $result = Router::url(array('plugin' => null, 'controller' => 'articles', 'action' => 'add', 'ext' => 'json')); $expected = '/articles/add.json'; $this->assertEqual($result, $expected); $result = Router::url(array('plugin' => null, 'controller' => 'articles', 'action' => 'index', 'id' => null, 'ext' => 'json')); $expected = '/articles.json'; $this->assertEqual($result, $expected); $result = Router::url(array('plugin' => null, 'controller' => 'articles', 'action' => 'index', 'ext' => 'json')); $expected = '/articles.json'; $this->assertEqual($result, $expected); }/** * testPluginUrlGeneration method * * @access public * @return void */ function testPluginUrlGeneration() { Router::setRequestInfo(array( array( 'controller' => 'controller', 'action' => 'index', 'form' => array(), 'url' => array(), 'plugin' => 'test' ), array( 'base' => '/base', 'here' => '/clients/sage/portal/donations', 'webroot' => '/base/', 'passedArgs' => array(), 'argSeparator' => ':', 'namedArgs' => array() ) )); $this->assertEqual(Router::url('read/1'), '/base/test/controller/read/1'); Router::reload(); }/** * testUrlParsing method * * @access public * @return void */ function testUrlParsing() { extract(Router::getNamedExpressions()); Router::connect('/posts/:value/:somevalue/:othervalue/*', array('controller' => 'posts', 'action' => 'view'), array('value','somevalue', 'othervalue')); $result = Router::parse('/posts/2007/08/01/title-of-post-here'); $expected = array('value' => '2007', 'somevalue' => '08', 'othervalue' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here'), 'named' => array()); $this->assertEqual($result, $expected); $this->router->routes = array(); Router::connect('/posts/:year/:month/:day/*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day)); $result = Router::parse('/posts/2007/08/01/title-of-post-here'); $expected = array('year' => '2007', 'month' => '08', 'day' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here'), 'named' => array()); $this->assertEqual($result, $expected); $this->router->routes = array(); Router::connect('/posts/:day/:year/:month/*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day)); $result = Router::parse('/posts/01/2007/08/title-of-post-here'); $expected = array('day' => '01', 'year' => '2007', 'month' => '08', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here'), 'named' => array()); $this->assertEqual($result, $expected); $this->router->routes = array(); Router::connect('/posts/:month/:day/:year//*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day)); $result = Router::parse('/posts/08/01/2007/title-of-post-here'); $expected = array('month' => '08', 'day' => '01', 'year' => '2007', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here'), 'named' => array()); $this->assertEqual($result, $expected); $this->router->routes = array(); Router::connect('/posts/:year/:month/:day/*', array('controller' => 'posts', 'action' => 'view')); $result = Router::parse('/posts/2007/08/01/title-of-post-here'); $expected = array('year' => '2007', 'month' => '08', 'day' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here'), 'named' => array()); $this->assertEqual($result, $expected); Router::reload(); $result = Router::parse('/pages/display/home'); $expected = array('plugin' => null, 'pass' => array('home'), 'controller' => 'pages', 'action' => 'display', 'named' => array()); $this->assertEqual($result, $expected); $result = Router::parse('pages/display/home/'); $this->assertEqual($result, $expected); $result = Router::parse('pages/display/home'); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/page/*', array('controller' => 'test')); $result = Router::parse('/page/my-page'); $expected = array('pass' => array('my-page'), 'plugin' => null, 'controller' => 'test', 'action' => 'index'); Router::reload(); Router::connect('/:language/contact', array('language' => 'eng', 'plugin' => 'contact', 'controller' => 'contact', 'action' => 'index'), array('language' => '[a-z]{3}')); $result = Router::parse('/eng/contact'); $expected = array('pass' => array(), 'named' => array(), 'language' => 'eng', 'plugin' => 'contact', 'controller' => 'contact', 'action' => 'index'); $this->assertEqual($result, $expected); Router::reload(); 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::parse('/forestillinger/10/2007/min-forestilling'); $expected = array('pass' => array('min-forestilling'), 'plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'year' => 2007, 'month' => 10, 'named' => array()); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/:controller/:action/*', array(), array('controller' => 'some_controller')); Router::connect('/', array('plugin' => 'pages', 'controller' => 'pages', 'action' => 'display')); $result = Router::parse('/'); $expected = array('pass' => array(), 'named' => array(), 'controller' => 'pages', 'action' => 'display', 'plugin' => 'pages'); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/posts/:id::url_title', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('id', 'url_title'), 'id' => '[\d]+')); $result = Router::parse('/posts/5:sample-post-title'); $expected = array('pass' => array('5', 'sample-post-title'), 'named' => array(), 'id' => 5, 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/posts/:id::url_title/*', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('id', 'url_title'), 'id' => '[\d]+')); $result = Router::parse('/posts/5:sample-post-title/other/params/4'); $expected = array('pass' => array('5', 'sample-post-title', 'other', 'params', '4'), 'named' => array(), 'id' => 5, 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/posts/:url_title-(uuid::id)', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('id', 'url_title'), 'id' => $UUID)); $result = Router::parse('/posts/sample-post-title-(uuid:47fc97a9-019c-41d1-a058-1fa3cbdd56cb)'); $expected = array('pass' => array('47fc97a9-019c-41d1-a058-1fa3cbdd56cb', 'sample-post-title'), 'named' => array(), 'id' => '47fc97a9-019c-41d1-a058-1fa3cbdd56cb', 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => false)); $result = Router::parse('/posts/view/foo:bar/routing:fun'); $expected = array('pass' => array('foo:bar', 'routing:fun'), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'))); $result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42'); $expected = array('pass' => array('routing:fun'), 'named' => array('foo' => 'bar', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'), 'greedy' => true)); $result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42'); $expected = array('pass' => array(), 'named' => array('foo' => 'bar', 'routing' => 'fun', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/:lang/:color/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('persist' => array('lang', 'color'))); Router::connect('/:lang/:color/posts/index', array('controller' => 'posts', 'action' => 'index'), array('persist' => array('lang'))); Router::connect('/:lang/:color/posts/edit/*', array('controller' => 'posts', 'action' => 'index')); Router::connect('/about', array('controller' => 'pages', 'action' => 'view', 'about')); Router::parse('/en/red/posts/view/5'); Router::setRequestInfo(array( array('controller' => 'posts', 'action' => 'view', 'lang' => 'en', 'color' => 'red', 'form' => array(), 'url' => array(), 'plugin' => null), array('base' => '/', 'here' => '/en/red/posts/view/5', 'webroot' => '/', 'passedArgs' => array(), 'argSeparator' => ':', 'namedArgs' => array()) )); $expected = '/en/red/posts/view/6'; $result = Router::url(array('controller' => 'posts', 'action' => 'view', 6)); $this->assertEqual($result, $expected); $expected = '/en/blue/posts/index'; $result = Router::url(array('controller' => 'posts', 'action' => 'index', 'color' => 'blue')); $this->assertEqual($result, $expected); $expected = '/posts'; $result = Router::url(array('controller' => 'posts', 'action' => 'index')); $this->assertEqual($result, $expected); $expected = '/posts/edit/7'; $result = Router::url(array('controller' => 'posts', 'action' => 'edit', 7)); $this->assertEqual($result, $expected); $expected = '/about'; $result = Router::url(array('controller' => 'pages', 'action' => 'view', 'about')); $this->assertEqual($result, $expected); }/** * testUuidRoutes method * * @access public * @return void */ function testUuidRoutes() { Router::connect( '/subjects/add/:category_id', array('controller' => 'subjects', 'action' => 'add'), array('category_id' => '\w{8}-\w{4}-\w{4}-\w{4}-\w{12}') ); $result = Router::parse('/subjects/add/4795d601-19c8-49a6-930e-06a8b01d17b7'); $expected = array('pass' => array(), 'named' => array(), 'category_id' => '4795d601-19c8-49a6-930e-06a8b01d17b7', 'plugin' => null, 'controller' => 'subjects', 'action' => 'add'); $this->assertEqual($result, $expected); }/** * testRouteSymmetry method * * @access public
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -