Upgrade CakePHP from 2.2.5 to 2.9.5

This commit is contained in:
Brm Ko 2017-02-26 15:29:44 +01:00
parent 5a580df460
commit 235a541597
793 changed files with 60746 additions and 23753 deletions

View file

@ -2,21 +2,22 @@
/**
* DispatcherTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Routing
* @since CakePHP(tm) v 1.2.0.4206
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Dispatcher', 'Routing');
App::uses('DispatcherFilter', 'Routing');
if (!class_exists('AppController', false)) {
require_once CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS . 'AppController.php';
@ -56,12 +57,11 @@ class TestDispatcher extends Dispatcher {
*
* @param Controller $controller
* @param CakeRequest $request
* @param CakeResponse $response
* @return void
* @return CakeResponse
*/
protected function _invoke(Controller $controller, CakeRequest $request, CakeResponse $response) {
protected function _invoke(Controller $controller, CakeRequest $request) {
$this->controller = $controller;
return parent::_invoke($controller, $request, $response);
return parent::_invoke($controller, $request);
}
/**
@ -95,12 +95,18 @@ class TestDispatcher extends Dispatcher {
class MyPluginAppController extends AppController {
}
/**
* Abstract Class DispatcherTestAbstractController
*/
abstract class DispatcherTestAbstractController extends Controller {
abstract public function index();
}
/**
* Interface DispatcherTestInterfaceController
*/
interface DispatcherTestInterfaceController {
public function index();
@ -114,13 +120,6 @@ interface DispatcherTestInterfaceController {
*/
class MyPluginController extends MyPluginAppController {
/**
* name property
*
* @var string 'MyPlugin'
*/
public $name = 'MyPlugin';
/**
* uses property
*
@ -165,13 +164,6 @@ class MyPluginController extends MyPluginAppController {
*/
class SomePagesController extends AppController {
/**
* name property
*
* @var string 'SomePages'
*/
public $name = 'SomePages';
/**
* uses property
*
@ -207,6 +199,16 @@ class SomePagesController extends AppController {
return new CakeResponse(array('body' => 'new response'));
}
/**
* Test file sending
*
* @return CakeResponse
*/
public function sendfile() {
$this->response->file(CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS . 'test_asset.css');
return $this->response;
}
}
/**
@ -216,13 +218,6 @@ class SomePagesController extends AppController {
*/
class OtherPagesController extends MyPluginAppController {
/**
* name property
*
* @var string 'OtherPages'
*/
public $name = 'OtherPages';
/**
* uses property
*
@ -258,13 +253,6 @@ class OtherPagesController extends MyPluginAppController {
*/
class TestDispatchPagesController extends AppController {
/**
* name property
*
* @var string 'TestDispatchPages'
*/
public $name = 'TestDispatchPages';
/**
* uses property
*
@ -307,13 +295,6 @@ class ArticlesTestAppController extends AppController {
*/
class ArticlesTestController extends ArticlesTestAppController {
/**
* name property
*
* @var string 'ArticlesTest'
*/
public $name = 'ArticlesTest';
/**
* uses property
*
@ -348,13 +329,6 @@ class ArticlesTestController extends ArticlesTestAppController {
*/
class SomePostsController extends AppController {
/**
* name property
*
* @var string 'SomePosts'
*/
public $name = 'SomePosts';
/**
* uses property
*
@ -365,7 +339,7 @@ class SomePostsController extends AppController {
/**
* autoRender property
*
* @var bool false
* @var bool
*/
public $autoRender = false;
@ -375,7 +349,7 @@ class SomePostsController extends AppController {
* @return void
*/
public function beforeFilter() {
if ($this->params['action'] == 'index') {
if ($this->params['action'] === 'index') {
$this->params['action'] = 'view';
} else {
$this->params['action'] = 'change';
@ -410,13 +384,6 @@ class SomePostsController extends AppController {
*/
class TestCachedPagesController extends Controller {
/**
* name property
*
* @var string 'TestCachedPages'
*/
public $name = 'TestCachedPages';
/**
* uses property
*
@ -452,7 +419,7 @@ class TestCachedPagesController extends Controller {
/**
* viewPath property
*
* @var string 'posts'
* @var string
*/
public $viewPath = 'Posts';
@ -495,6 +462,8 @@ class TestCachedPagesController extends Controller {
/**
* Test cached views with themes.
*
* @return void
*/
public function themed() {
$this->cacheAction = 10;
@ -511,13 +480,6 @@ class TestCachedPagesController extends Controller {
*/
class TimesheetsController extends Controller {
/**
* name property
*
* @var string 'Timesheets'
*/
public $name = 'Timesheets';
/**
* uses property
*
@ -536,6 +498,39 @@ class TimesheetsController extends Controller {
}
/**
* TestFilterDispatcher class
*
* @package Cake.Test.Case.Routing
*/
class TestFilterDispatcher extends DispatcherFilter {
public $priority = 10;
/**
* TestFilterDispatcher::beforeDispatch()
*
* @param mixed $event
* @return CakeResponse|bool
*/
public function beforeDispatch(CakeEvent $event) {
$event->stopPropagation();
$response = $event->data['request'];
$response->addParams(array('settings' => $this->settings));
return null;
}
/**
* TestFilterDispatcher::afterDispatch()
*
* @param mixed $event
* @return mixed boolean to stop the event dispatching or null to continue
*/
public function afterDispatch(CakeEvent $event) {
}
}
/**
* DispatcherTest class
*
@ -549,6 +544,7 @@ class DispatcherTest extends CakeTestCase {
* @return void
*/
public function setUp() {
parent::setUp();
$this->_get = $_GET;
$_GET = array();
$this->_post = $_POST;
@ -576,6 +572,7 @@ class DispatcherTest extends CakeTestCase {
* @return void
*/
public function tearDown() {
parent::tearDown();
$_GET = $this->_get;
$_POST = $this->_post;
$_FILES = $this->_files;
@ -592,6 +589,7 @@ class DispatcherTest extends CakeTestCase {
* testParseParamsWithoutZerosAndEmptyPost method
*
* @return void
* @triggers DispatcherTest $Dispatcher, array('request' => $request)
*/
public function testParseParamsWithoutZerosAndEmptyPost() {
$Dispatcher = new Dispatcher();
@ -610,6 +608,7 @@ class DispatcherTest extends CakeTestCase {
* testParseParamsReturnsPostedData method
*
* @return void
* @triggers DispatcherTest $Dispatcher, array('request' => $request)
*/
public function testParseParamsReturnsPostedData() {
$_POST['testdata'] = "My Posted Content";
@ -617,7 +616,7 @@ class DispatcherTest extends CakeTestCase {
$request = new CakeRequest("/");
$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $request));
$Dispatcher->parseParams($event);
$test = $Dispatcher->parseParams($event);
$Dispatcher->parseParams($event);
$this->assertEquals("My Posted Content", $request['data']['testdata']);
}
@ -625,6 +624,7 @@ class DispatcherTest extends CakeTestCase {
* testParseParamsWithSingleZero method
*
* @return void
* @triggers DispatcherTest $Dispatcher, array('request' => $test)
*/
public function testParseParamsWithSingleZero() {
$Dispatcher = new Dispatcher();
@ -643,6 +643,7 @@ class DispatcherTest extends CakeTestCase {
* testParseParamsWithManySingleZeros method
*
* @return void
* @triggers DispatcherTest $Dispatcher, array('request' => $test)
*/
public function testParseParamsWithManySingleZeros() {
$Dispatcher = new Dispatcher();
@ -662,6 +663,7 @@ class DispatcherTest extends CakeTestCase {
* testParseParamsWithManyZerosInEachSectionOfUrl method
*
* @return void
* @triggers DispatcherTest $Dispatcher, array('request' => $test)
*/
public function testParseParamsWithManyZerosInEachSectionOfUrl() {
$Dispatcher = new Dispatcher();
@ -681,6 +683,7 @@ class DispatcherTest extends CakeTestCase {
* testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl method
*
* @return void
* @triggers DispatcherTest $Dispatcher, array('request' => $test)
*/
public function testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl() {
$Dispatcher = new Dispatcher();
@ -700,6 +703,8 @@ class DispatcherTest extends CakeTestCase {
* testQueryStringOnRoot method
*
* @return void
* @triggers DispatcherTest $Dispatcher, array('request' => $request)
* @triggers DispatcherTest $Dispatcher, array('request' => $request)
*/
public function testQueryStringOnRoot() {
Router::reload();
@ -865,6 +870,40 @@ class DispatcherTest extends CakeTestCase {
$this->assertEquals('new response', $result);
}
/**
* testDispatchActionSendsFile
*
* @return void
*/
public function testDispatchActionSendsFile() {
Router::connect('/:controller/:action');
$Dispatcher = new Dispatcher();
$request = new CakeRequest('some_pages/sendfile');
$response = $this->getMock('CakeResponse', array(
'header',
'type',
'download',
'_sendHeader',
'_setContentType',
'_isActive',
'_clearBuffer',
'_flushBuffer'
));
$response->expects($this->never())
->method('body');
$response->expects($this->exactly(1))
->method('_isActive')
->will($this->returnValue(true));
ob_start();
$Dispatcher->dispatch($request, $response);
$result = ob_get_clean();
$this->assertEquals("/* this is the test asset css file */\n", $result);
}
/**
* testAdminDispatch method
*
@ -874,7 +913,7 @@ class DispatcherTest extends CakeTestCase {
$_POST = array();
$Dispatcher = new TestDispatcher();
Configure::write('Routing.prefixes', array('admin'));
Configure::write('App.baseUrl','/cake/repo/branches/1.2.x.x/index.php');
Configure::write('App.baseUrl', '/cake/repo/branches/1.2.x.x/index.php');
$url = new CakeRequest('admin/test_dispatch_pages/index/param:value/param2:value2');
$response = $this->getMock('CakeResponse');
@ -897,6 +936,7 @@ class DispatcherTest extends CakeTestCase {
* testPluginDispatch method
*
* @return void
* @triggers DispatcherTest $Dispatcher, array('request' => $url)
*/
public function testPluginDispatch() {
$_POST = array();
@ -1083,7 +1123,7 @@ class DispatcherTest extends CakeTestCase {
}
/**
* test plugin shortcut urls with controllers that need to be loaded,
* test plugin shortcut URLs with controllers that need to be loaded,
* the above test uses a controller that has already been included.
*
* @return void
@ -1195,7 +1235,7 @@ class DispatcherTest extends CakeTestCase {
*
* @return void
*/
public function testDispatcherFilterSuscriber() {
public function testDispatcherFilterSubscriber() {
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
@ -1229,6 +1269,23 @@ class DispatcherTest extends CakeTestCase {
$this->assertNull($dispatcher->controller);
}
/**
* Tests that it is possible to attach filter with config classes to the dispatch cycle
*
* @return void
*/
public function testDispatcherFilterSettings() {
Configure::write('Dispatcher.filters', array(
'TestFilterDispatcher' => array('service' => 'google.com')
));
$Dispatcher = new Dispatcher();
$url = new CakeRequest('some_pages/index');
$response = $this->getMock('CakeResponse');
$Dispatcher->dispatch($url, $response, array('return' => 1));
$settings = $url->param('settings');
$this->assertEquals($settings, array('service' => 'google.com'));
}
/**
* Tests that attaching an inexistent class as filter will throw an exception
*
@ -1280,6 +1337,16 @@ class DispatcherTest extends CakeTestCase {
$dispatcher->dispatch($request, $response);
$this->assertEquals('Dispatcher.afterDispatch', $request->params['eventName']);
$dispatcher = new TestDispatcher();
Configure::write('Dispatcher.filters', array(
'filterTest' => array('callable' => array($dispatcher, 'filterTest'), 'on' => 'before')
));
$request = new CakeRequest('/');
$response = $this->getMock('CakeResponse', array('send'));
$dispatcher->dispatch($request, $response);
$this->assertEquals('Dispatcher.beforeDispatch', $request->params['eventName']);
// Test that it is possible to skip the route connection process
$dispatcher = new TestDispatcher();
Configure::write('Dispatcher.filters', array(
@ -1582,6 +1649,11 @@ class DispatcherTest extends CakeTestCase {
* testHttpMethodOverrides method
*
* @return void
* @triggers DispatcherTest $dispatcher, array('request' => $request)
* @triggers DispatcherTest $dispatcher, array('request' => $request)
* @triggers DispatcherTest $dispatcher, array('request' => $request)
* @triggers DispatcherTest $dispatcher, array('request' => $request)
* @triggers DispatcherTest $dispatcher, array('request' => $request)
*/
public function testHttpMethodOverrides() {
Router::reload();
@ -1665,7 +1737,7 @@ class DispatcherTest extends CakeTestCase {
*/
protected function _cachePath($here) {
$path = $here;
if ($here == '/') {
if ($here === '/') {
$path = 'home';
}
$path = strtolower(Inflector::slug($path));

View file

@ -1,22 +1,28 @@
<?php
/**
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package Cake.Test.Case.Routing.Filter
* @since CakePHP(tm) v 2.2
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('AssetDispatcher', 'Routing/Filter');
App::uses('CakeEvent', 'Event');
App::uses('CakeResponse', 'Network');
/**
* AssetDispatcherTest
*
* @package Cake.Test.Case.Routing.Filter
*/
class AssetDispatcherTest extends CakeTestCase {
/**
@ -25,6 +31,7 @@ class AssetDispatcherTest extends CakeTestCase {
* @return void
*/
public function tearDown() {
parent::tearDown();
Configure::write('Dispatcher.filters', array());
}
@ -32,6 +39,12 @@ class AssetDispatcherTest extends CakeTestCase {
* test that asset filters work for theme and plugin assets
*
* @return void
* @triggers DispatcherTest $this, compact('request', 'response')
* @triggers DispatcherTest $this, compact('request', 'response')
* @triggers DispatcherTest $this, compact('request', 'response')
* @triggers DispatcherTest $this, compact('request', 'response')
* @triggers DispatcherTest $this, compact('request', 'response')
* @triggers DispatcherTest $this, compact('request', 'response')
*/
public function testAssetFilterForThemeAndPlugins() {
$filter = new AssetDispatcher();
@ -43,7 +56,7 @@ class AssetDispatcherTest extends CakeTestCase {
App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
), APP::RESET);
), App::RESET);
$request = new CakeRequest('theme/test_theme/ccss/cake.generic.css');
$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
@ -76,11 +89,41 @@ class AssetDispatcherTest extends CakeTestCase {
$this->assertFalse($event->isStopped());
}
/**
* AssetDispatcher should not 404 extensions that could be handled
* by Routing.
*
* @return void
* @triggers DispatcherTest $this, compact('request', 'response')
*/
public function testNoHandleRoutedExtension() {
$filter = new AssetDispatcher();
$response = $this->getMock('CakeResponse', array('_sendHeader'));
Configure::write('Asset.filter', array(
'js' => '',
'css' => ''
));
App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
), App::RESET);
Router::parseExtensions('json');
Router::connect('/test_plugin/api/v1/:action', array('controller' => 'api'));
CakePlugin::load('TestPlugin');
$request = new CakeRequest('test_plugin/api/v1/forwarding.json');
$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
$this->assertNull($filter->beforeDispatch($event));
$this->assertFalse($event->isStopped(), 'Events for routed extensions should not be stopped');
}
/**
* Tests that $response->checkNotModified() is called and bypasses
* file dispatching
*
* @return void
* @triggers DispatcherTest $this, compact('request', 'response')
* @triggers DispatcherTest $this, compact('request', 'response')
*/
public function testNotModified() {
$filter = new AssetDispatcher();
@ -121,4 +164,102 @@ class AssetDispatcherTest extends CakeTestCase {
$this->assertSame($response, $filter->beforeDispatch($event));
$this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());
}
/**
* Test that no exceptions are thrown for //index.php type URLs.
*
* @return void
* @triggers Dispatcher.beforeRequest $this, compact('request', 'response')
*/
public function test404OnDoubleSlash() {
$filter = new AssetDispatcher();
$response = $this->getMock('CakeResponse', array('_sendHeader'));
$request = new CakeRequest('//index.php');
$event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response'));
$this->assertNull($filter->beforeDispatch($event));
$this->assertFalse($event->isStopped());
}
/**
* Test that attempts to traverse directories are prevented.
*
* @return void
* @triggers Dispatcher.beforeRequest $this, compact('request', 'response')
*/
public function test404OnDoubleDot() {
App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
), App::RESET);
$response = $this->getMock('CakeResponse', array('_sendHeader'));
$request = new CakeRequest('theme/test_theme/../../../../../../VERSION.txt');
$event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response'));
$response->expects($this->never())->method('send');
$filter = new AssetDispatcher();
$this->assertNull($filter->beforeDispatch($event));
$this->assertFalse($event->isStopped());
}
/**
* Test that attempts to traverse directories with urlencoded paths fail.
*
* @return void
* @triggers Dispatcher.beforeRequest $this, compact('request', 'response')
*/
public function test404OnDoubleDotEncoded() {
App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
), App::RESET);
$response = $this->getMock('CakeResponse', array('_sendHeader', 'send'));
$request = new CakeRequest('theme/test_theme/%2e./%2e./%2e./%2e./%2e./%2e./VERSION.txt');
$event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response'));
$response->expects($this->never())->method('send');
$filter = new AssetDispatcher();
$this->assertNull($filter->beforeDispatch($event));
$this->assertFalse($event->isStopped());
}
/**
* Test asset content length is unset
*
* If content length is unset, then the webserver can figure it out.
*
* @outputBuffering enabled
* @return void
*/
public function testAssetContentLength() {
Router::reload();
Configure::write('Dispatcher.filters', array('AssetDispatcher'));
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
));
$url = 'theme/test_theme/css/test_asset.css';
$file = 'View/Themed/TestTheme/webroot/css/test_asset.css';
$request = new CakeRequest($url);
$response = $this->getMock('CakeResponse', array('_sendHeader', 'send'));
$event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response'));
$filter = new AssetDispatcher();
$filter->beforeDispatch($event);
$result = ob_get_clean();
$path = CAKE . 'Test' . DS . 'test_app' . DS . str_replace('/', DS, $file);
$file = file_get_contents($path);
$this->assertEquals($file, $result);
$headers = $response->header();
$this->assertFalse($headers['Content-Length']);
}
}

View file

@ -2,19 +2,18 @@
/**
* CakeRequest Test case file.
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Test.Case.Routing.Route
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('CakeRoute', 'Routing/Route');
@ -24,7 +23,7 @@ App::uses('Router', 'Routing');
* Test case for CakeRoute
*
* @package Cake.Test.Case.Routing.Route
**/
*/
class CakeRouteTest extends CakeTestCase {
/**
@ -41,7 +40,7 @@ class CakeRouteTest extends CakeTestCase {
* Test the construction of a CakeRoute
*
* @return void
**/
*/
public function testConstruction() {
$route = new CakeRoute('/:controller/:action/:id', array(), array('id' => '[0-9]+'));
@ -55,7 +54,7 @@ class CakeRouteTest extends CakeTestCase {
* test Route compiling.
*
* @return void
**/
*/
public function testBasicRouteCompiling() {
$route = new CakeRoute('/', array('controller' => 'pages', 'action' => 'display', 'home'));
$result = $route->compile();
@ -107,7 +106,7 @@ class CakeRouteTest extends CakeTestCase {
* test compiling routes with keys that have patterns
*
* @return void
**/
*/
public function testRouteCompilingWithParamPatterns() {
$route = new CakeRoute(
'/:controller/:action/:id',
@ -119,7 +118,7 @@ class CakeRouteTest extends CakeTestCase {
$this->assertRegExp($result, '/posts/view/518098');
$this->assertNotRegExp($result, '/posts/edit/name-of-post');
$this->assertNotRegExp($result, '/posts/edit/4/other:param');
$this->assertEquals(array('controller', 'action', 'id'), $route->keys);
$this->assertEquals(array('id', 'controller', 'action'), $route->keys);
$route = new CakeRoute(
'/:lang/:controller/:action/:id',
@ -131,7 +130,7 @@ class CakeRouteTest extends CakeTestCase {
$this->assertRegExp($result, '/cze/articles/view/1');
$this->assertNotRegExp($result, '/language/articles/view/2');
$this->assertNotRegExp($result, '/eng/articles/view/name-of-article');
$this->assertEquals(array('lang', 'controller', 'action', 'id'), $route->keys);
$this->assertEquals(array('lang', 'id', 'controller', 'action'), $route->keys);
foreach (array(':', '@', ';', '$', '-') as $delim) {
$route = new CakeRoute('/posts/:id' . $delim . ':title');
@ -142,7 +141,7 @@ class CakeRouteTest extends CakeTestCase {
$this->assertNotRegExp($result, '/posts/11!nameofarticle');
$this->assertNotRegExp($result, '/posts/11');
$this->assertEquals(array('id', 'title'), $route->keys);
$this->assertEquals(array('title', 'id'), $route->keys);
}
$route = new CakeRoute(
@ -156,7 +155,7 @@ class CakeRouteTest extends CakeTestCase {
$this->assertNotRegExp($result, '/posts/hey_now:nameofarticle');
$this->assertNotRegExp($result, '/posts/:nameofarticle/2009');
$this->assertNotRegExp($result, '/posts/:nameofarticle/01');
$this->assertEquals(array('id', 'title', 'year'), $route->keys);
$this->assertEquals(array('year', 'title', 'id'), $route->keys);
$route = new CakeRoute(
'/posts/:url_title-(uuid::id)',
@ -205,7 +204,7 @@ class CakeRouteTest extends CakeTestCase {
$this->assertRegExp($result, '/some_extra/page/this_is_the_slug');
$this->assertRegExp($result, '/page/this_is_the_slug');
$this->assertEquals(array('extra', 'slug'), $route->keys);
$this->assertEquals(array('slug', 'extra'), $route->keys);
$this->assertEquals(array('extra' => '[a-z1-9_]*', 'slug' => '[a-z1-9_]+', 'action' => 'view'), $route->options);
$expected = array(
'controller' => 'pages',
@ -235,7 +234,7 @@ class CakeRouteTest extends CakeTestCase {
* test that routes match their pattern.
*
* @return void
**/
*/
public function testMatchBasic() {
$route = new CakeRoute('/:controller/:action/:id', array('plugin' => null));
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null));
@ -309,6 +308,11 @@ class CakeRouteTest extends CakeTestCase {
$result = $route->match($url);
$expected = '/admin/subscriptions/edit_admin_e/1';
$this->assertEquals($expected, $result);
$url = array('controller' => 'subscribe', 'admin' => true, 'action' => 'admin_edit', 1);
$result = $route->match($url);
$expected = '/admin/subscriptions/edit/1';
$this->assertEquals($expected, $result);
}
/**
@ -483,6 +487,40 @@ class CakeRouteTest extends CakeTestCase {
$this->assertEquals('red', $result['color']);
}
/**
* test persist with a non array value
*
* @return void
*/
public function testPersistParamsNonArray() {
$url = array('controller' => 'posts', 'action' => 'index');
$params = array('lang' => 'en', 'color' => 'blue');
$route = new CakeRoute(
'/:lang/:color/blog/:action',
array('controller' => 'posts')
// No persist options
);
$result = $route->persistParams($url, $params);
$this->assertEquals($url, $result);
$route = new CakeRoute(
'/:lang/:color/blog/:action',
array('controller' => 'posts'),
array('persist' => false)
);
$result = $route->persistParams($url, $params);
$this->assertEquals($url, $result);
$route = new CakeRoute(
'/:lang/:color/blog/:action',
array('controller' => 'posts'),
array('persist' => 'derp')
);
$result = $route->persistParams($url, $params);
$this->assertEquals($url, $result);
}
/**
* test the parse method of CakeRoute.
*
@ -817,6 +855,41 @@ class CakeRouteTest extends CakeTestCase {
$this->assertEquals($expected, $result);
}
/**
* Test matching of parameters where one parameter name starts with another parameter name
*
* @return void
*/
public function testMatchSimilarParameters() {
$route = new CakeRoute('/:thisParam/:thisParamIsLonger');
$url = array(
'thisParamIsLonger' => 'bar',
'thisParam' => 'foo',
);
$result = $route->match($url);
$expected = '/foo/bar';
$this->assertEquals($expected, $result);
}
/**
* Test match() with trailing ** style routes.
*
* @return void
*/
public function testMatchTrailing() {
$route = new CakeRoute('/pages/**', array('controller' => 'pages', 'action' => 'display'));
$id = 'test/ spaces/漢字/la†în';
$result = $route->match(array(
'controller' => 'pages',
'action' => 'display',
$id
));
$expected = '/pages/test/%20spaces/%E6%BC%A2%E5%AD%97/la%E2%80%A0%C3%AEn';
$this->assertEquals($expected, $result);
}
/**
* test restructuring args with pass key
*
@ -869,7 +942,7 @@ class CakeRouteTest extends CakeTestCase {
* @return void
*/
public function testParseTrailingUTF8() {
$route = new CakeRoute('/category/**', array('controller' => 'categories','action' => 'index'));
$route = new CakeRoute('/category/**', array('controller' => 'categories', 'action' => 'index'));
$result = $route->parse('/category/%D9%85%D9%88%D8%A8%D8%A7%DB%8C%D9%84');
$expected = array(
'controller' => 'categories',
@ -880,4 +953,58 @@ class CakeRouteTest extends CakeTestCase {
$this->assertEquals($expected, $result);
}
/**
* test that utf-8 patterns work for :section
*
* @return void
*/
public function testUTF8PatternOnSection() {
$route = new CakeRoute(
'/:section',
array('plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index'),
array(
'persist' => array('section'),
'section' => 'آموزش|weblog'
)
);
$result = $route->parse('/%D8%A2%D9%85%D9%88%D8%B2%D8%B4');
$expected = array('section' => 'آموزش', 'plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index', 'pass' => array(), 'named' => array());
$this->assertEquals($expected, $result);
$result = $route->parse('/weblog');
$expected = array('section' => 'weblog', 'plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index', 'pass' => array(), 'named' => array());
$this->assertEquals($expected, $result);
}
/**
* Test for __set_state magic method on CakeRoute
*
* @return void
*/
public function testSetState() {
$route = CakeRoute::__set_state(array(
'keys' => array(),
'options' => array(),
'defaults' => array(
'controller' => 'pages',
'action' => 'display',
'home',
),
'template' => '/',
'_greedy' => false,
'_compiledRoute' => null,
'_headerMap' => array (
'type' => 'content_type',
'method' => 'request_method',
'server' => 'server_name',
),
));
$this->assertInstanceOf('CakeRoute', $route);
$this->assertSame('/', $route->match(array('controller' => 'pages', 'action' => 'display', 'home')));
$this->assertFalse($route->match(array('controller' => 'pages', 'action' => 'display', 'about')));
$expected = array('controller' => 'pages', 'action' => 'display', 'pass' => array('home'), 'named' => array());
$this->assertEquals($expected, $route->parse('/'));
}
}

View file

@ -2,19 +2,18 @@
/**
* CakeRequest Test case file.
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Test.Case.Routing.Route
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('PluginShortRoute', 'Routing/Route');
@ -25,7 +24,7 @@ App::uses('Router', 'Routing');
*
* @package Cake.Test.Case.Routing.Route
*/
class PluginShortRouteTest extends CakeTestCase {
class PluginShortRouteTest extends CakeTestCase {
/**
* setUp method
@ -56,7 +55,7 @@ class PluginShortRouteTest extends CakeTestCase {
}
/**
* test the reverse routing of the plugin shortcut urls.
* test the reverse routing of the plugin shortcut URLs.
*
* @return void
*/

View file

@ -2,19 +2,18 @@
/**
* CakeRequest Test case file.
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Test.Case.Routing.Route
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('RedirectRoute', 'Routing/Route');
@ -26,7 +25,7 @@ App::uses('Router', 'Routing');
*
* @package Cake.Test.Case.Routing.Route
*/
class RedirectRouteTest extends CakeTestCase {
class RedirectRouteTest extends CakeTestCase {
/**
* setUp method
@ -48,14 +47,14 @@ class RedirectRouteTest extends CakeTestCase {
$route = new RedirectRoute('/home', array('controller' => 'posts'));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/home');
$route->parse('/home');
$header = $route->response->header();
$this->assertEquals(Router::url('/posts', true), $header['Location']);
$route = new RedirectRoute('/home', array('controller' => 'posts', 'action' => 'index'));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/home');
$route->parse('/home');
$header = $route->response->header();
$this->assertEquals(Router::url('/posts', true), $header['Location']);
$this->assertEquals(301, $route->response->statusCode());
@ -63,14 +62,14 @@ class RedirectRouteTest extends CakeTestCase {
$route = new RedirectRoute('/google', 'http://google.com');
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/google');
$route->parse('/google');
$header = $route->response->header();
$this->assertEquals('http://google.com', $header['Location']);
$route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('status' => 302));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/posts/2');
$route->parse('/posts/2');
$header = $route->response->header();
$this->assertEquals(Router::url('/posts/view', true), $header['Location']);
$this->assertEquals(302, $route->response->statusCode());
@ -78,30 +77,46 @@ class RedirectRouteTest extends CakeTestCase {
$route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('persist' => true));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/posts/2');
$route->parse('/posts/2');
$header = $route->response->header();
$this->assertEquals(Router::url('/posts/view/2', true), $header['Location']);
$route = new RedirectRoute('/posts/*', '/test', array('persist' => true));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/posts/2');
$route->parse('/posts/2');
$header = $route->response->header();
$this->assertEquals(Router::url('/test', true), $header['Location']);
$route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add'), array('persist' => true));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/my_controllers/do_something/passme/named:param');
$route->parse('/my_controllers/do_something/passme/named:param');
$header = $route->response->header();
$this->assertEquals(Router::url('/tags/add/passme/named:param', true), $header['Location']);
$route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add'));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/my_controllers/do_something/passme/named:param');
$route->parse('/my_controllers/do_something/passme/named:param');
$header = $route->response->header();
$this->assertEquals(Router::url('/tags/add', true), $header['Location']);
$route = new RedirectRoute('/:lang/my_controllers', array('controller' => 'tags', 'action' => 'add'), array('lang' => '(nl|en)', 'persist' => array('lang')));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$route->parse('/nl/my_controllers/');
$header = $route->response->header();
$this->assertEquals(Router::url('/tags/add/lang:nl', true), $header['Location']);
Router::$routes = array(); // reset default routes
Router::connect('/:lang/preferred_controllers', array('controller' => 'tags', 'action' => 'add'), array('lang' => '(nl|en)', 'persist' => array('lang')));
$route = new RedirectRoute('/:lang/my_controllers', array('controller' => 'tags', 'action' => 'add'), array('lang' => '(nl|en)', 'persist' => array('lang')));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$route->parse('/nl/my_controllers/');
$header = $route->response->header();
$this->assertEquals(Router::url('/nl/preferred_controllers', true), $header['Location']);
}
}

View file

@ -2,19 +2,18 @@
/**
* RouterTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Routing
* @since CakePHP(tm) v 1.2.0.4206
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Router', 'Routing');
@ -49,24 +48,49 @@ class RouterTest extends CakeTestCase {
public function tearDown() {
parent::tearDown();
CakePlugin::unload();
Router::fullBaseUrl('');
Configure::write('App.fullBaseUrl', 'http://localhost');
}
/**
* testFullBaseURL method
* testFullBaseUrl method
*
* @return void
*/
public function testFullBaseURL() {
$skip = PHP_SAPI == 'cli';
if ($skip) {
$this->markTestSkipped('Cannot validate base urls in CLI');
}
public function testFullBaseUrl() {
$this->assertRegExp('/^http(s)?:\/\//', Router::url('/', true));
$this->assertRegExp('/^http(s)?:\/\//', Router::url(null, true));
$this->assertRegExp('/^http(s)?:\/\//', Router::url(array('full_base' => true)));
$this->assertSame(FULL_BASE_URL . '/', Router::url(array('full_base' => true)));
}
/**
* Tests that the base URL can be changed at runtime.
*
* @return void
*/
public function testBaseUrl() {
$this->assertEquals(FULL_BASE_URL, Router::fullBaseUrl());
Router::fullBaseUrl('http://example.com');
$this->assertEquals('http://example.com/', Router::url('/', true));
$this->assertEquals('http://example.com', Configure::read('App.fullBaseUrl'));
Router::fullBaseUrl('https://example.com');
$this->assertEquals('https://example.com/', Router::url('/', true));
$this->assertEquals('https://example.com', Configure::read('App.fullBaseUrl'));
}
/**
* Test that Router uses App.base to build URL's when there are no stored
* request objects.
*
* @return void
*/
public function testBaseUrlWithBasePath() {
Configure::write('App.base', '/cakephp');
Router::fullBaseUrl('http://example.com');
$this->assertEquals('http://example.com/cakephp/tasks', Router::url('/tasks', true));
}
/**
* testRouteDefaultParams method
*
@ -166,6 +190,28 @@ class RouterTest extends CakeTestCase {
$this->assertEquals($expected, $result);
}
/**
* testMapResources with custom connectOptions
*/
public function testMapResourcesConnectOptions() {
App::build(array(
'Plugin' => array(
CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
)
));
CakePlugin::load('TestPlugin');
App::uses('TestRoute', 'TestPlugin.Routing/Route');
Router::mapResources('Posts', array(
'connectOptions' => array(
'routeClass' => 'TestPlugin.TestRoute',
'foo' => '^(bar)$',
),
));
$route = end(Router::$routes);
$this->assertInstanceOf('TestRoute', $route);
$this->assertEquals('^(bar)$', $route->options['foo']);
}
/**
* Test mapResources with a plugin and prefix.
*
@ -191,6 +237,20 @@ class RouterTest extends CakeTestCase {
);
$this->assertEquals($expected, $result);
$this->assertEquals(array('test_plugin'), $resources);
Router::mapResources('Posts', array('prefix' => 'api'));
$_SERVER['REQUEST_METHOD'] = 'GET';
$result = Router::parse('/api/posts');
$expected = array(
'pass' => array(),
'named' => array(),
'plugin' => null,
'controller' => 'posts',
'action' => 'index',
'[method]' => 'GET'
);
$this->assertEquals($expected, $result);
}
/**
@ -315,7 +375,7 @@ class RouterTest extends CakeTestCase {
}
/**
* test generation of basic urls.
* test generation of basic URLs.
*
* @return void
*/
@ -484,7 +544,7 @@ class RouterTest extends CakeTestCase {
$result = Router::url(array('controller' => 'tests', 'pages' => array(
1, 2, 3
)));
$expected = '/tests/index/pages[0]:1/pages[1]:2/pages[2]:3';
$expected = '/tests/index/pages%5B0%5D:1/pages%5B1%5D:2/pages%5B2%5D:3';
$this->assertEquals($expected, $result);
$result = Router::url(array('controller' => 'tests',
@ -496,7 +556,7 @@ class RouterTest extends CakeTestCase {
'three'
)
));
$expected = '/tests/index/pages[param1][0]:one/pages[param1][1]:two/pages[0]:three';
$expected = '/tests/index/pages%5Bparam1%5D%5B0%5D:one/pages%5Bparam1%5D%5B1%5D:two/pages%5B0%5D:three';
$this->assertEquals($expected, $result);
$result = Router::url(array('controller' => 'tests',
@ -508,7 +568,7 @@ class RouterTest extends CakeTestCase {
'three'
)
));
$expected = '/tests/index/pages[param1][one]:1/pages[param1][two]:2/pages[0]:three';
$expected = '/tests/index/pages%5Bparam1%5D%5Bone%5D:1/pages%5Bparam1%5D%5Btwo%5D:2/pages%5B0%5D:three';
$this->assertEquals($expected, $result);
$result = Router::url(array('controller' => 'tests',
@ -520,14 +580,14 @@ class RouterTest extends CakeTestCase {
'cool'
)
));
$expected = '/tests/index/super[nested][array]:awesome/super[nested][something]:else/super[0]:cool';
$expected = '/tests/index/super%5Bnested%5D%5Barray%5D:awesome/super%5Bnested%5D%5Bsomething%5D:else/super%5B0%5D:cool';
$this->assertEquals($expected, $result);
$result = Router::url(array('controller' => 'tests', 'namedParam' => array(
'keyed' => 'is an array',
'test'
)));
$expected = '/tests/index/namedParam[keyed]:is%20an%20array/namedParam[0]:test';
$expected = '/tests/index/namedParam%5Bkeyed%5D:is%20an%20array/namedParam%5B0%5D:test';
$this->assertEquals($expected, $result);
}
@ -535,7 +595,7 @@ class RouterTest extends CakeTestCase {
* Test generation of routes with query string parameters.
*
* @return void
**/
*/
public function testUrlGenerationWithQueryStrings() {
$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => 'var=test&var2=test2'));
$expected = '/posts/index/0?var=test&var2=test2';
@ -559,7 +619,7 @@ class RouterTest extends CakeTestCase {
* test that regex validation of keyed route params is working.
*
* @return void
**/
*/
public function testUrlGenerationWithRegexQualifiedParams() {
Router::connect(
':language/galleries',
@ -639,7 +699,7 @@ class RouterTest extends CakeTestCase {
}
/**
* Test url generation with an admin prefix
* Test URL generation with an admin prefix
*
* @return void
*/
@ -936,6 +996,23 @@ class RouterTest extends CakeTestCase {
$this->assertEquals('/admin/other/posts/index', $result);
}
/**
* Test that URL's fail to parse when they are prefixed with //
*
* @return void
*/
public function testUrlParseFailureDoubleSlash() {
Router::connect('/posts', array('controller' => 'posts', 'action' => 'index'));
$result = Router::parse('/posts');
$this->assertEquals(
array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'index'),
$result
);
$result = Router::parse('//posts');
$this->assertEquals(array(), $result);
}
/**
* testUrlParsing method
*
@ -944,7 +1021,7 @@ class RouterTest extends CakeTestCase {
public function testUrlParsing() {
extract(Router::getNamedExpressions());
Router::connect('/posts/:value/:somevalue/:othervalue/*', array('controller' => 'posts', 'action' => 'view'), array('value','somevalue', 'othervalue'));
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->assertEquals($expected, $result);
@ -1053,6 +1130,12 @@ class RouterTest extends CakeTestCase {
$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->assertEquals($expected, $result);
Router::reload();
Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'), 'greedyNamed' => true));
$result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42?id=123&tab=abc');
$expected = array('pass' => array(), 'named' => array('foo' => 'bar', 'routing' => 'fun', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view', '?' => array('id' => '123', 'tab' => 'abc'));
$this->assertEquals($expected, $result);
}
/**
@ -1065,13 +1148,13 @@ class RouterTest extends CakeTestCase {
Router::connect(
'/:lang/:color/posts/view/*',
array('controller' => 'posts', 'action' => 'view'),
array('persist' => array('lang', 'color')
));
array('persist' => array('lang', 'color'))
);
Router::connect(
'/:lang/:color/posts/index',
array('controller' => 'posts', 'action' => 'index'),
array('persist' => array('lang')
));
array('persist' => array('lang'))
);
Router::connect('/:lang/:color/posts/edit/*', array('controller' => 'posts', 'action' => 'edit'));
Router::connect('/about', array('controller' => 'pages', 'action' => 'view', 'about'));
Router::parse('/en/red/posts/view/5');
@ -1169,6 +1252,33 @@ class RouterTest extends CakeTestCase {
$this->assertEquals($expected, $result);
}
/**
* Test parse and reverse symmetry
*
* @return void
* @dataProvider parseReverseSymmetryData
*/
public function testParseReverseSymmetry($url) {
$this->assertSame($url, Router::reverse(Router::parse($url) + array('url' => array())));
}
/**
* Data for parse and reverse test
*
* @return array
*/
public function parseReverseSymmetryData() {
return array(
array('/'),
array('/controller/action'),
array('/controller/action/param'),
array('/controller/action?param1=value1&param2=value2'),
array('/controller/action/param?param1=value1'),
array('/controller/action/named1:nv1'),
array('/controller/action/named1:nv1?param1=value1')
);
}
/**
* Test that Routing.prefixes are used when a Router instance is created
* or reset
@ -1315,13 +1425,15 @@ class RouterTest extends CakeTestCase {
$this->assertEquals($expected, $result);
$result = Router::parse('/posts/view/1.rss');
$expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'pass' => array('1'), 'named' => array(), 'ext' => 'rss', 'named' => array());
$expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'pass' => array('1'), 'named' => array(), 'ext' => 'rss');
$this->assertEquals($expected, $result);
$result = Router::parse('/posts/view/1.rss?query=test');
$expected['?'] = array('query' => 'test');
$this->assertEquals($expected, $result);
$result = Router::parse('/posts/view/1.atom');
unset($expected['?']);
$expected['ext'] = 'atom';
$this->assertEquals($expected, $result);
@ -1335,7 +1447,7 @@ class RouterTest extends CakeTestCase {
$this->assertEquals($expected, $result);
$result = Router::parse('/posts.atom?hello=goodbye');
$expected = array('plugin' => null, 'controller' => 'posts.atom', 'action' => 'index', 'pass' => array(), 'named' => array());
$expected = array('plugin' => null, 'controller' => 'posts.atom', 'action' => 'index', 'pass' => array(), 'named' => array(), '?' => array('hello' => 'goodbye'));
$this->assertEquals($expected, $result);
Router::reload();
@ -1467,7 +1579,7 @@ class RouterTest extends CakeTestCase {
$request->base = '/';
Router::setRequestInfo($request);
$result = Router::parse('/admin/controller/index/type:whatever');
Router::parse('/admin/controller/index/type:whatever');
$result = Router::url(array('type' => 'new'));
$expected = "/admin/controller/index/type:new";
$this->assertEquals($expected, $result);
@ -1535,7 +1647,7 @@ class RouterTest extends CakeTestCase {
}
/**
* test url generation with legacy (1.2) style prefix routes.
* Test URL generation with legacy (1.2) style prefix routes.
*
* @return void
* @see testUrlGenerationWithAutoPrefixes
@ -1552,7 +1664,7 @@ class RouterTest extends CakeTestCase {
Router::setRequestInfo(
$request->addParams(array(
'plugin' => null, 'controller' => 'images', 'action' => 'index',
'prefix' => null, 'admin' => false,'url' => array('url' => 'images/index')
'prefix' => null, 'admin' => false, 'url' => array('url' => 'images/index')
))->addPaths(array(
'base' => '',
'here' => '/images/index',
@ -2161,6 +2273,16 @@ class RouterTest extends CakeTestCase {
$this->assertEquals(Router::stripPlugin($url, null), $url);
}
/**
* testCurrentRouteWhenNonExistentRoute
*
* @return void
*/
public function testCurrentRouteWhenNonExistentRoute() {
$route = Router::currentRoute();
$this->assertFalse($route);
}
/**
* testCurrentRoute
*
@ -2189,14 +2311,12 @@ class RouterTest extends CakeTestCase {
$this->assertEquals(array_merge($url, array('plugin' => null)), $route->defaults);
// test that the first route is matched
$newUrl = array('controller' => 'products', 'action' => 'display', 6);
Router::connect('/government', $url);
Router::parse('/government');
$route = Router::requestRoute();
$this->assertEquals(array_merge($url, array('plugin' => null)), $route->defaults);
// test that an unmatched route does not change the current route
$newUrl = array('controller' => 'products', 'action' => 'display', 6);
Router::connect('/actor', $url);
Router::parse('/government');
$route = Router::requestRoute();
@ -2218,10 +2338,10 @@ class RouterTest extends CakeTestCase {
'named' => array(), 'pass' => array(),
'param1' => '1', 'param2' => '2',
);
$this->assertEquals(Router::getParams(), $expected);
$this->assertEquals(Router::getParam('controller'), false);
$this->assertEquals(Router::getParam('param1'), '1');
$this->assertEquals(Router::getParam('param2'), '2');
$this->assertEquals($expected, Router::getParams());
$this->assertEquals(false, Router::getParam('controller'));
$this->assertEquals('1', Router::getParam('param1'));
$this->assertEquals('2', Router::getParam('param2'));
Router::reload();
@ -2231,8 +2351,8 @@ class RouterTest extends CakeTestCase {
'plugin' => null, 'controller' => 'pages', 'action' => 'display',
'named' => array(), 'pass' => array(),
);
$this->assertEquals(Router::getParams(), $expected);
$this->assertEquals(Router::getParams(true), $expected);
$this->assertEquals($expected, Router::getParams());
$this->assertEquals($expected, Router::getParams(true));
}
/**
@ -2290,13 +2410,13 @@ class RouterTest extends CakeTestCase {
* @return void
*/
public function testUsingCustomRouteClass() {
$mock = $this->getMock('CakeRoute', array(), array(), 'MockConnectedRoute', false);
$this->getMock('CakeRoute', array(), array(), 'MockConnectedRoute', false);
$routes = Router::connect(
'/:slug',
array('controller' => 'posts', 'action' => 'view'),
array('routeClass' => 'MockConnectedRoute', 'slug' => '[a-z_-]+')
);
$this->assertTrue(is_a($routes[0], 'MockConnectedRoute'), 'Incorrect class used. %s');
$this->assertInstanceOf('MockConnectedRoute', $routes[0], 'Incorrect class used. %s');
$expected = array('controller' => 'posts', 'action' => 'view', 'slug' => 'test');
$routes[0]->expects($this->any())
->method('parse')
@ -2305,6 +2425,28 @@ class RouterTest extends CakeTestCase {
$this->assertEquals($expected, $result);
}
/**
* test using custom route class in PluginDot notation
*
* @return void
*/
public function testUsingCustomRouteClassPluginDotSyntax() {
App::build(array(
'Plugin' => array(
CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
)
));
CakePlugin::load('TestPlugin');
App::uses('TestRoute', 'TestPlugin.Routing/Route');
$routes = Router::connect(
'/:slug',
array('controller' => 'posts', 'action' => 'view'),
array('routeClass' => 'TestPlugin.TestRoute', 'slug' => '[a-z_-]+')
);
$this->assertInstanceOf('TestRoute', $routes[0]);
CakePlugin::unload('TestPlugin');
}
/**
* test that route classes must extend CakeRoute
*
@ -2312,15 +2454,19 @@ class RouterTest extends CakeTestCase {
* @return void
*/
public function testCustomRouteException() {
Router::connect('/:controller', array(), array('routeClass' => 'Object'));
Router::connect('/:controller', array(), array('routeClass' => 'CakeObject'));
}
/**
* test reversing parameter arrays back into strings.
*
* Mark the router as initialized so it doesn't auto-load routes
*
* @return void
*/
public function testRouterReverse() {
Router::$initialized = true;
$params = array(
'controller' => 'posts',
'action' => 'view',
@ -2445,6 +2591,8 @@ class RouterTest extends CakeTestCase {
/**
* Test that Router::url() uses the first request
*
* @return void
*/
public function testUrlWithRequestAction() {
$firstRequest = new CakeRequest('/posts/index');
@ -2474,7 +2622,7 @@ class RouterTest extends CakeTestCase {
}
/**
* test that a route object returning a full url is not modified.
* test that a route object returning a full URL is not modified.
*
* @return void
*/
@ -2507,6 +2655,28 @@ class RouterTest extends CakeTestCase {
$url = '://example.com';
$this->assertEquals($url, Router::url($url));
$url = '//example.com';
$this->assertEquals($url, Router::url($url));
$url = 'javascript:void(0)';
$this->assertEquals($url, Router::url($url));
$url = 'tel:012345-678';
$this->assertEquals($url, Router::url($url));
$url = 'sms:012345-678';
$this->assertEquals($url, Router::url($url));
$url = '#here';
$this->assertEquals($url, Router::url($url));
$url = '?param=0';
$this->assertEquals($url, Router::url($url));
$url = 'posts/index#here';
$expected = FULL_BASE_URL . '/posts/index#here';
$this->assertEquals($expected, Router::url($url, true));
}
/**
@ -2542,22 +2712,22 @@ class RouterTest extends CakeTestCase {
public function testResourceMap() {
$default = Router::resourceMap();
$expected = array(
array('action' => 'index', 'method' => 'GET', 'id' => false),
array('action' => 'view', 'method' => 'GET', 'id' => true),
array('action' => 'add', 'method' => 'POST', 'id' => false),
array('action' => 'edit', 'method' => 'PUT', 'id' => true),
array('action' => 'delete', 'method' => 'DELETE', 'id' => true),
array('action' => 'edit', 'method' => 'POST', 'id' => true)
array('action' => 'index', 'method' => 'GET', 'id' => false),
array('action' => 'view', 'method' => 'GET', 'id' => true),
array('action' => 'add', 'method' => 'POST', 'id' => false),
array('action' => 'edit', 'method' => 'PUT', 'id' => true),
array('action' => 'delete', 'method' => 'DELETE', 'id' => true),
array('action' => 'edit', 'method' => 'POST', 'id' => true)
);
$this->assertEquals($default, $expected);
$this->assertEquals($expected, $default);
$custom = array(
array('action' => 'index', 'method' => 'GET', 'id' => false),
array('action' => 'view', 'method' => 'GET', 'id' => true),
array('action' => 'add', 'method' => 'POST', 'id' => false),
array('action' => 'edit', 'method' => 'PUT', 'id' => true),
array('action' => 'delete', 'method' => 'DELETE', 'id' => true),
array('action' => 'update', 'method' => 'POST', 'id' => true)
array('action' => 'index', 'method' => 'GET', 'id' => false),
array('action' => 'view', 'method' => 'GET', 'id' => true),
array('action' => 'add', 'method' => 'POST', 'id' => false),
array('action' => 'edit', 'method' => 'PUT', 'id' => true),
array('action' => 'delete', 'method' => 'DELETE', 'id' => true),
array('action' => 'update', 'method' => 'POST', 'id' => true)
);
Router::resourceMap($custom);
$this->assertEquals(Router::resourceMap(), $custom);
@ -2631,7 +2801,7 @@ class RouterTest extends CakeTestCase {
* @return void
*/
public function testSettingInvalidDefaultRouteException() {
Router::defaultRouteClass('Object');
Router::defaultRouteClass('CakeObject');
}
/**