Пример #1
0
 /**
  * testUrlParsing method
  *
  * @return void
  */
 public 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' => null, 'pass' => array('0' => 'title-of-post-here'));
     $this->assertEquals($expected, $result);
     Router::reload();
     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' => null, 'pass' => array('0' => 'title-of-post-here'));
     $this->assertEquals($expected, $result);
     Router::reload();
     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' => null, 'pass' => array('0' => 'title-of-post-here'));
     $this->assertEquals($expected, $result);
     Router::reload();
     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' => null, 'pass' => array('0' => 'title-of-post-here'));
     $this->assertEquals($expected, $result);
     Router::reload();
     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' => null, 'pass' => array('0' => 'title-of-post-here'));
     $this->assertEquals($expected, $result);
     Router::reload();
     require CAKE . 'Config/routes.php';
     $result = Router::parse('/pages/display/home');
     $expected = array('plugin' => null, 'pass' => array('home'), 'controller' => 'pages', 'action' => 'display');
     $this->assertEquals($expected, $result);
     $result = Router::parse('pages/display/home/');
     $this->assertEquals($expected, $result);
     $result = Router::parse('pages/display/home');
     $this->assertEquals($expected, $result);
     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');
     $this->assertEquals($expected, $result);
     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' => [], 'language' => 'eng', 'plugin' => 'contact', 'controller' => 'contact', 'action' => 'index');
     $this->assertEquals($expected, $result);
     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);
     $this->assertEquals($expected, $result);
     Router::reload();
     Router::connect('/:controller/:action/*');
     Router::connect('/', array('plugin' => 'pages', 'controller' => 'pages', 'action' => 'display'));
     $result = Router::parse('/');
     $expected = array('pass' => [], 'controller' => 'pages', 'action' => 'display', 'plugin' => 'pages');
     $this->assertEquals($expected, $result);
     $result = Router::parse('/posts/edit/0');
     $expected = array('pass' => array(0), 'controller' => 'posts', 'action' => 'edit', 'plugin' => null);
     $this->assertEquals($expected, $result);
     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'), 'id' => 5, 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
     $this->assertEquals($expected, $result);
     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'), 'id' => 5, 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
     $this->assertEquals($expected, $result);
     Router::reload();
     Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'));
     $result = Router::parse('/posts/view/10?id=123&tab=abc');
     $expected = array('pass' => array(10), 'plugin' => null, 'controller' => 'posts', 'action' => 'view', '?' => array('id' => '123', 'tab' => 'abc'));
     $this->assertEquals($expected, $result);
     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'), 'id' => '47fc97a9-019c-41d1-a058-1fa3cbdd56cb', 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
     $this->assertEquals($expected, $result);
     Router::reload();
     Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'));
     $result = Router::parse('/posts/view/foo:bar/routing:fun');
     $expected = array('pass' => array('foo:bar', 'routing:fun'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
     $this->assertEquals($expected, $result);
 }