| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658 |
- <?php
- /**
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * 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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @since 1.2.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Routing;
- use Cake\Core\App;
- use Cake\Core\Configure;
- use Cake\Core\Plugin;
- use Cake\Network\Request;
- use Cake\Routing\RouteCollection;
- use Cake\Routing\Router;
- use Cake\Routing\Route\Route;
- use Cake\TestSuite\TestCase;
- use TestPlugin\Routing\Route\TestRoute;
- /**
- * RouterTest class
- *
- */
- class RouterTest extends TestCase {
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- Configure::write('Routing', array('admin' => null, 'prefixes' => []));
- Router::fullbaseUrl('');
- Configure::write('App.fullBaseUrl', 'http://localhost');
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- Plugin::unload();
- Router::fullBaseUrl('');
- Configure::write('App.fullBaseUrl', 'http://localhost');
- Router::reload();
- }
- /**
- * testFullBaseUrl method
- *
- * @return void
- */
- public function testbaseUrl() {
- $this->assertRegExp('/^http(s)?:\/\//', Router::url('/', true));
- $this->assertRegExp('/^http(s)?:\/\//', Router::url(null, true));
- $this->assertRegExp('/^http(s)?:\/\//', Router::url(array('_full' => true)));
- }
- /**
- * Tests that the base URL can be changed at runtime.
- *
- * @return void
- */
- public function testfullBaseURL() {
- 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
- *
- * @return void
- */
- public function testRouteDefaultParams() {
- Router::connect('/:controller', array('controller' => 'posts'));
- $this->assertEquals(Router::url(array('action' => 'index')), '/');
- }
- /**
- * testMapResources method
- *
- * @return void
- */
- public function testMapResources() {
- Router::mapResources('Posts');
- $_SERVER['REQUEST_METHOD'] = 'GET';
- $expected = [
- 'pass' => [],
- 'plugin' => null,
- 'controller' => 'Posts',
- 'action' => 'index',
- '_method' => 'GET',
- '_ext' => null
- ];
- $result = Router::parse('/posts');
- $this->assertEquals($expected, $result);
- $_SERVER['REQUEST_METHOD'] = 'GET';
- $expected = [
- 'pass' => ['13'],
- 'plugin' => null,
- 'controller' => 'Posts',
- 'action' => 'view',
- 'id' => '13',
- '_method' => 'GET',
- '_ext' => null
- ];
- $result = Router::parse('/posts/13');
- $this->assertEquals($expected, $result);
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $expected = [
- 'pass' => [],
- 'plugin' => null,
- 'controller' => 'Posts',
- 'action' => 'add',
- '_method' => 'POST',
- '_ext' => null
- ];
- $result = Router::parse('/posts');
- $this->assertEquals($expected, $result);
- $_SERVER['REQUEST_METHOD'] = 'PUT';
- $expected = [
- 'pass' => ['13'],
- 'plugin' => null,
- 'controller' => 'Posts',
- 'action' => 'edit',
- 'id' => '13',
- '_method' => ['PUT', 'PATCH'],
- '_ext' => null
- ];
- $result = Router::parse('/posts/13');
- $this->assertEquals($expected, $result);
- $expected = [
- 'pass' => ['475acc39-a328-44d3-95fb-015000000000'],
- 'plugin' => null,
- 'controller' => 'Posts',
- 'action' => 'edit',
- 'id' => '475acc39-a328-44d3-95fb-015000000000',
- '_method' => ['PUT', 'PATCH'],
- '_ext' => null
- ];
- $result = Router::parse('/posts/475acc39-a328-44d3-95fb-015000000000');
- $this->assertEquals($expected, $result);
- $_SERVER['REQUEST_METHOD'] = 'DELETE';
- $expected = [
- 'pass' => ['13'],
- 'plugin' => null,
- 'controller' => 'Posts',
- 'action' => 'delete',
- 'id' => '13',
- '_method' => 'DELETE',
- '_ext' => null
- ];
- $result = Router::parse('/posts/13');
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::mapResources('Posts', ['id' => '[a-z0-9_]+']);
- $_SERVER['REQUEST_METHOD'] = 'GET';
- $expected = [
- 'pass' => ['add'],
- 'plugin' => null,
- 'controller' => 'Posts',
- 'action' => 'view',
- 'id' => 'add',
- '_method' => 'GET',
- '_ext' => null
- ];
- $result = Router::parse('/posts/add');
- $this->assertEquals($expected, $result);
- $_SERVER['REQUEST_METHOD'] = 'PUT';
- $expected = [
- 'pass' => ['name'],
- 'plugin' => null,
- 'controller' => 'Posts',
- 'action' => 'edit',
- 'id' => 'name',
- '_method' => ['PUT', 'PATCH'],
- '_ext' => null
- ];
- $result = Router::parse('/posts/name');
- $this->assertEquals($expected, $result);
- }
- /**
- * testMapResources with plugin controllers.
- *
- * @return void
- */
- public function testPluginMapResources() {
- Router::mapResources('TestPlugin.TestPlugin');
- $_SERVER['REQUEST_METHOD'] = 'GET';
- $result = Router::parse('/test_plugin/test_plugin');
- $expected = array(
- 'pass' => [],
- 'plugin' => 'TestPlugin',
- 'controller' => 'TestPlugin',
- 'action' => 'index',
- '_method' => 'GET',
- '_ext' => null
- );
- $this->assertEquals($expected, $result);
- $_SERVER['REQUEST_METHOD'] = 'GET';
- $result = Router::parse('/test_plugin/test_plugin/13');
- $expected = array(
- 'pass' => array('13'),
- 'plugin' => 'TestPlugin',
- 'controller' => 'TestPlugin',
- 'action' => 'view',
- 'id' => '13',
- '_method' => 'GET',
- '_ext' => null
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * Test mapResources with a prefix.
- *
- * @return void
- */
- public function testMapResourcesWithPrefix() {
- Router::mapResources('Posts', array('prefix' => 'api'));
- $_SERVER['REQUEST_METHOD'] = 'GET';
- $result = Router::parse('/api/posts');
- $expected = array(
- 'plugin' => null,
- 'controller' => 'Posts',
- 'action' => 'index',
- 'pass' => [],
- 'prefix' => 'api',
- '_method' => 'GET',
- '_ext' => null
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * Test mapResources with a default extension.
- *
- * @return void
- */
- public function testMapResourcesWithExtension() {
- Router::parseExtensions(['json', 'xml'], false);
- Router::mapResources('Posts', ['_ext' => 'json']);
- $_SERVER['REQUEST_METHOD'] = 'GET';
- $expected = array(
- 'plugin' => null,
- 'controller' => 'Posts',
- 'action' => 'index',
- 'pass' => [],
- '_method' => 'GET',
- '_ext' => 'json',
- );
- $result = Router::parse('/posts');
- $this->assertEquals($expected, $result);
- $result = Router::parse('/posts.json');
- $this->assertEquals($expected, $result);
- $expected['_ext'] = 'xml';
- $result = Router::parse('/posts.xml');
- $this->assertEquals($expected, $result);
- }
- /**
- * testMapResources with custom connectOptions
- */
- public function testMapResourcesConnectOptions() {
- Plugin::load('TestPlugin');
- Router::mapResources('Posts', array(
- 'connectOptions' => array(
- 'routeClass' => 'TestPlugin.TestRoute',
- 'foo' => '^(bar)$',
- ),
- ));
- $routes = Router::routes();
- $route = $routes[0];
- $this->assertInstanceOf('TestPlugin\Routing\Route\TestRoute', $route);
- $this->assertEquals('^(bar)$', $route->options['foo']);
- }
- /**
- * Test mapResources with a plugin and prefix.
- *
- * @return void
- */
- public function testPluginMapResourcesWithPrefix() {
- Router::mapResources('TestPlugin.TestPlugin', array('prefix' => 'api'));
- $_SERVER['REQUEST_METHOD'] = 'GET';
- $result = Router::parse('/api/test_plugin/test_plugin');
- $expected = array(
- 'pass' => [],
- 'plugin' => 'TestPlugin',
- 'controller' => 'TestPlugin',
- 'prefix' => 'api',
- 'action' => 'index',
- '_method' => 'GET',
- '_ext' => null
- );
- $this->assertEquals($expected, $result);
- $resources = Router::mapResources('Posts', array('prefix' => 'api'));
- $_SERVER['REQUEST_METHOD'] = 'GET';
- $result = Router::parse('/api/posts');
- $expected = array(
- 'pass' => [],
- 'plugin' => null,
- 'controller' => 'Posts',
- 'action' => 'index',
- '_method' => 'GET',
- 'prefix' => 'api',
- '_ext' => null
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testMultipleResourceRoute method
- *
- * @return void
- */
- public function testMultipleResourceRoute() {
- Router::connect('/:controller', array('action' => 'index', '_method' => array('GET', 'POST')));
- $_SERVER['REQUEST_METHOD'] = 'GET';
- $result = Router::parse('/posts');
- $expected = array(
- 'pass' => [],
- 'plugin' => null,
- 'controller' => 'posts',
- 'action' => 'index',
- '_method' => array('GET', 'POST')
- );
- $this->assertEquals($expected, $result);
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $result = Router::parse('/posts');
- $expected = array(
- 'pass' => [],
- 'plugin' => null,
- 'controller' => 'posts',
- 'action' => 'index',
- '_method' => array('GET', 'POST')
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testGenerateUrlResourceRoute method
- *
- * @return void
- */
- public function testGenerateUrlResourceRoute() {
- Router::mapResources('Posts');
- $result = Router::url([
- 'controller' => 'Posts',
- 'action' => 'index',
- '_method' => 'GET'
- ]);
- $expected = '/posts';
- $this->assertEquals($expected, $result);
- $result = Router::url([
- 'controller' => 'Posts',
- 'action' => 'view',
- '_method' => 'GET',
- 'id' => 10
- ]);
- $expected = '/posts/10';
- $this->assertEquals($expected, $result);
- $result = Router::url(['controller' => 'Posts', 'action' => 'add', '_method' => 'POST']);
- $expected = '/posts';
- $this->assertEquals($expected, $result);
- $result = Router::url(['controller' => 'Posts', 'action' => 'edit', '_method' => 'PUT', 'id' => 10]);
- $expected = '/posts/10';
- $this->assertEquals($expected, $result);
- $result = Router::url(['controller' => 'Posts', 'action' => 'delete', '_method' => 'DELETE', 'id' => 10]);
- $expected = '/posts/10';
- $this->assertEquals($expected, $result);
- $result = Router::url(['controller' => 'Posts', 'action' => 'edit', '_method' => 'PATCH', 'id' => 10]);
- $expected = '/posts/10';
- $this->assertEquals($expected, $result);
- }
- /**
- * testUrlNormalization method
- *
- * @return void
- */
- public function testUrlNormalization() {
- Router::connect('/:controller/:action');
- $expected = '/users/logout';
- $result = Router::normalize('/users/logout/');
- $this->assertEquals($expected, $result);
- $result = Router::normalize('//users//logout//');
- $this->assertEquals($expected, $result);
- $result = Router::normalize('users/logout');
- $this->assertEquals($expected, $result);
- $result = Router::normalize(array('controller' => 'users', 'action' => 'logout'));
- $this->assertEquals($expected, $result);
- $result = Router::normalize('/');
- $this->assertEquals('/', $result);
- $result = Router::normalize('http://google.com/');
- $this->assertEquals('http://google.com/', $result);
- $result = Router::normalize('http://google.com//');
- $this->assertEquals('http://google.com//', $result);
- $result = Router::normalize('/users/login/scope://foo');
- $this->assertEquals('/users/login/scope:/foo', $result);
- $result = Router::normalize('/recipe/recipes/add');
- $this->assertEquals('/recipe/recipes/add', $result);
- $request = new Request();
- $request->base = '/us';
- Router::setRequestInfo($request);
- $result = Router::normalize('/us/users/logout/');
- $this->assertEquals('/users/logout', $result);
- Router::reload();
- $request = new Request();
- $request->base = '/cake_12';
- Router::setRequestInfo($request);
- $result = Router::normalize('/cake_12/users/logout/');
- $this->assertEquals('/users/logout', $result);
- Router::reload();
- $_back = Configure::read('App.fullBaseUrl');
- Configure::write('App.fullBaseUrl', '/');
- $request = new Request();
- $request->base = '/';
- Router::setRequestInfo($request);
- $result = Router::normalize('users/login');
- $this->assertEquals('/users/login', $result);
- Configure::write('App.fullBaseUrl', $_back);
- Router::reload();
- $request = new Request();
- $request->base = 'beer';
- Router::setRequestInfo($request);
- $result = Router::normalize('beer/admin/beers_tags/add');
- $this->assertEquals('/admin/beers_tags/add', $result);
- $result = Router::normalize('/admin/beers_tags/add');
- $this->assertEquals('/admin/beers_tags/add', $result);
- }
- /**
- * Test generating urls with base paths.
- *
- * @return void
- */
- public function testUrlGenerationWithBasePath() {
- Router::connect('/:controller/:action/*');
- $request = new Request();
- $request->addParams([
- 'action' => 'index',
- 'plugin' => null,
- 'controller' => 'subscribe',
- ]);
- $request->base = '/magazine';
- $request->here = '/magazine/';
- $request->webroot = '/magazine/';
- Router::pushRequest($request);
- $result = Router::url();
- $this->assertEquals('/magazine/', $result);
- $result = Router::url('/');
- $this->assertEquals('/magazine/', $result);
- $result = Router::url('/articles/');
- $this->assertEquals('/magazine/articles/', $result);
- $result = Router::url('/articles/view');
- $this->assertEquals('/magazine/articles/view', $result);
- $result = Router::url(['controller' => 'articles', 'action' => 'view', 1]);
- $this->assertEquals('/magazine/articles/view/1', $result);
- }
- /**
- * Test that catch all routes work with a variety of falsey inputs.
- *
- * @return void
- */
- public function testUrlCatchAllRoute() {
- Router::connect('/*', array('controller' => 'categories', 'action' => 'index'));
- $result = Router::url(array('controller' => 'categories', 'action' => 'index', '0'));
- $this->assertEquals('/0', $result);
- $expected = [
- 'plugin' => null,
- 'controller' => 'categories',
- 'action' => 'index',
- 'pass' => ['0'],
- ];
- $result = Router::parse('/0');
- $this->assertEquals($expected, $result);
- $result = Router::parse('0');
- $this->assertEquals($expected, $result);
- }
- /**
- * test generation of basic urls.
- *
- * @return void
- */
- public function testUrlGenerationBasic() {
- extract(Router::getNamedExpressions());
- 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));
- $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('id' => $ID));
- $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'));
- $result = Router::url(array('controller' => 'posts', 'action' => 'view', 'id' => '1'));
- $expected = '/posts/1';
- $this->assertEquals($expected, $result);
- Router::connect('/view/*', array('controller' => 'posts', 'action' => 'view'));
- $result = Router::url(array('controller' => 'posts', 'action' => 'view', '1'));
- $expected = '/view/1';
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::connect('/:controller/:action');
- $request = new Request();
- $request->addParams(array(
- 'action' => 'index',
- 'plugin' => null,
- 'controller' => '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('/contact/:action', array('plugin' => 'contact', 'controller' => 'contact'));
- $result = Router::url(array(
- 'plugin' => 'contact',
- 'controller' => 'contact',
- 'action' => 'me'
- ));
- $expected = '/contact/me';
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::connect('/:controller', array('action' => 'index'));
- $request = new Request();
- $request->addParams(array(
- 'action' => 'index',
- 'plugin' => 'myplugin',
- 'controller' => 'mycontroller',
- ));
- $request->base = '/';
- $request->here = '/';
- $request->webroot = '/';
- Router::setRequestInfo($request);
- $result = Router::url(array('plugin' => null, 'controller' => 'myothercontroller'));
- $expected = '/myothercontroller';
- $this->assertEquals($expected, $result);
- }
- /**
- * Test that generated names for routes are case-insensitive.
- *
- * @return void
- */
- public function testRouteNameCasing() {
- Router::connect('/articles/:id', ['controller' => 'Articles', 'action' => 'view']);
- Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
- $result = Router::url(['controller' => 'Articles', 'action' => 'view', 'id' => 10]);
- $expected = '/articles/10';
- $this->assertEquals($expected, $result);
- }
- /**
- * Test generation of routes with query string parameters.
- *
- * @return void
- */
- public function testUrlGenerationWithQueryStrings() {
- Router::connect('/:controller/:action/*');
- $result = Router::url(array(
- 'controller' => 'posts',
- '0',
- '?' => array('var' => 'test', 'var2' => 'test2')
- ));
- $expected = '/posts/index/0?var=test&var2=test2';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('controller' => 'posts', '0', '?' => array('var' => null)));
- $this->assertEquals('/posts/index/0', $result);
- $result = Router::url(array(
- 'controller' => 'posts',
- '0',
- '?' => array(
- 'var' => 'test',
- 'var2' => 'test2'
- ),
- '#' => 'unencoded string %'
- ));
- $expected = '/posts/index/0?var=test&var2=test2#unencoded string %';
- $this->assertEquals($expected, $result);
- }
- /**
- * test that regex validation of keyed route params is working.
- *
- * @return void
- */
- public function testUrlGenerationWithRegexQualifiedParams() {
- 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('language' => '[a-z]{3}')
- );
- $result = Router::url(array('admin' => false, 'language' => 'dan', 'action' => 'index', 'controller' => 'galleries'));
- $expected = '/dan/galleries';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('admin' => false, 'language' => 'eng', 'action' => 'index', 'controller' => 'galleries'));
- $expected = '/eng/galleries';
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::connect('/:language/pages',
- array('controller' => 'pages', 'action' => 'index'),
- array('language' => '[a-z]{3}')
- );
- Router::connect('/:language/:controller/:action/*', [], array('language' => '[a-z]{3}'));
- $result = Router::url(array('language' => 'eng', 'action' => 'index', 'controller' => 'pages'));
- $expected = '/eng/pages';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('language' => 'eng', 'controller' => 'pages'));
- $this->assertEquals($expected, $result);
- $result = Router::url(array('language' => 'eng', 'controller' => 'pages', 'action' => 'add'));
- $expected = '/eng/pages/add';
- $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::url(array(
- 'plugin' => 'shows',
- 'controller' => 'shows',
- 'action' => 'calendar',
- 'month' => 10,
- 'year' => 2007,
- 'min-forestilling'
- ));
- $expected = '/forestillinger/10/2007/min-forestilling';
- $this->assertEquals($expected, $result);
- Router::reload();
- 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->assertEquals($expected, $result);
- $result = Router::url(array(
- 'plugin' => 'shows',
- 'controller' => 'shows',
- 'action' => 'calendar',
- 'year' => 2007,
- 'month' => 10,
- 'min-forestilling'
- ));
- $expected = '/kalender/10/2007/min-forestilling';
- $this->assertEquals($expected, $result);
- }
- /**
- * Test URL generation with an admin prefix
- *
- * @return void
- */
- public function testUrlGenerationWithPrefix() {
- Configure::write('Routing.prefixes', array('admin'));
- Router::reload();
- 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::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
- Router::parseExtensions('rss', false);
- $request = new Request();
- $request->addParams(array(
- 'controller' => 'registrations',
- 'action' => 'admin_index',
- 'plugin' => null,
- 'prefix' => 'admin',
- '_ext' => 'html'
- ));
- $request->base = '';
- $request->here = '/admin/registrations/index';
- $request->webroot = '/';
- Router::setRequestInfo($request);
- $result = Router::url([]);
- $expected = '/admin/registrations/index';
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::connect('/admin/subscriptions/:action/*', array('controller' => 'subscribe', 'prefix' => 'admin'));
- Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
- $request = new Request();
- $request->addParams(array(
- 'action' => 'index',
- 'plugin' => null,
- 'controller' => 'subscribe',
- 'prefix' => 'admin',
- ));
- $request->base = '/magazine';
- $request->here = '/magazine/admin/subscriptions/edit/1';
- $request->webroot = '/magazine/';
- Router::setRequestInfo($request);
- $result = Router::url(array('action' => 'edit', 1));
- $expected = '/magazine/admin/subscriptions/edit/1';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('prefix' => 'admin', 'controller' => 'users', 'action' => 'login'));
- $expected = '/magazine/admin/users/login';
- $this->assertEquals($expected, $result);
- Router::reload();
- $request = new Request();
- $request->addParams(array(
- 'prefix' => 'admin',
- 'action' => 'index',
- 'plugin' => null,
- 'controller' => 'users',
- ));
- $request->base = '/';
- $request->here = '/';
- $request->webroot = '/';
- Router::setRequestInfo($request);
- Router::connect('/page/*', array('controller' => 'pages', 'action' => 'view', 'prefix' => 'admin'));
- $result = Router::url(array('prefix' => 'admin', 'controller' => 'pages', 'action' => 'view', 'my-page'));
- $expected = '/page/my-page';
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
- $request = new Request();
- $request->addParams(array(
- 'plugin' => null,
- 'controller' => 'pages',
- 'action' => 'add',
- 'prefix' => 'admin'
- ));
- $request->base = '';
- $request->here = '/admin/pages/add';
- $request->webroot = '/';
- Router::setRequestInfo($request);
- $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false));
- $expected = '/admin/pages/add';
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
- $request = new Request();
- $request->addParams(array(
- 'plugin' => null,
- 'controller' => 'pages',
- 'action' => 'add',
- 'prefix' => 'admin'
- ));
- $request->base = '';
- $request->here = '/admin/pages/add';
- $request->webroot = '/';
- Router::setRequestInfo($request);
- $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false));
- $expected = '/admin/pages/add';
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::connect('/admin/:controller/:action/:id', array('prefix' => 'admin'), array('id' => '[0-9]+'));
- $request = new Request();
- Router::setRequestInfo(
- $request->addParams(array(
- 'plugin' => null,
- 'controller' => 'pages',
- 'action' => 'edit',
- 'pass' => array('284'),
- 'prefix' => 'admin'
- ))->addPaths(array(
- 'base' => '',
- 'here' => '/admin/pages/edit/284',
- 'webroot' => '/'
- ))
- );
- $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'edit', 'id' => '284'));
- $expected = '/admin/pages/edit/284';
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
- $request = new Request();
- Router::setRequestInfo(
- $request->addParams(array(
- 'plugin' => null, 'controller' => 'pages', 'action' => 'add', 'prefix' => 'admin',
- ))->addPaths(array(
- 'base' => '', 'here' => '/admin/pages/add', 'webroot' => '/'
- ))
- );
- $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false));
- $expected = '/admin/pages/add';
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
- $request = new Request();
- Router::setRequestInfo(
- $request->addParams(array(
- 'plugin' => null, 'controller' => 'pages', 'action' => 'edit', 'prefix' => 'admin',
- 'pass' => array('284')
- ))->addPaths(array(
- 'base' => '', 'here' => '/admin/pages/edit/284', 'webroot' => '/'
- ))
- );
- $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'edit', 284));
- $expected = '/admin/pages/edit/284';
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::connect('/admin/posts/*', array('controller' => 'posts', 'action' => 'index', 'prefix' => 'admin'));
- Router::setRequestInfo(
- $request->addParams(array(
- 'plugin' => null, 'controller' => 'posts', 'action' => 'index', 'prefix' => 'admin',
- 'pass' => array('284')
- ))->addPaths(array(
- 'base' => '', 'here' => '/admin/posts', 'webroot' => '/'
- ))
- );
- $result = Router::url(array('all'));
- $expected = '/admin/posts/all';
- $this->assertEquals($expected, $result);
- }
- /**
- * Test URL generation inside a prefixed plugin.
- *
- * @return void
- */
- public function testUrlGenerationPrefixedPlugin() {
- Router::prefix('admin', function($routes) {
- $routes->plugin('MyPlugin', function($routes) {
- $routes->fallbacks();
- });
- });
- $result = Router::url(['prefix' => 'admin', 'plugin' => 'MyPlugin', 'controller' => 'Forms', 'action' => 'edit', 2]);
- $expected = '/admin/my_plugin/forms/edit/2';
- $this->assertEquals($expected, $result);
- }
- /**
- * testUrlGenerationWithExtensions method
- *
- * @return void
- */
- public function testUrlGenerationWithExtensions() {
- Router::connect('/:controller', array('action' => 'index'));
- Router::connect('/:controller/:action');
- $result = Router::url(array(
- 'plugin' => null,
- 'controller' => 'articles',
- 'action' => 'add',
- 'id' => null,
- '_ext' => 'json'
- ));
- $expected = '/articles/add.json';
- $this->assertEquals($expected, $result);
- $result = Router::url(array(
- 'plugin' => null,
- 'controller' => 'articles',
- 'action' => 'add',
- '_ext' => 'json'
- ));
- $expected = '/articles/add.json';
- $this->assertEquals($expected, $result);
- $result = Router::url(array(
- 'plugin' => null,
- 'controller' => 'articles',
- 'action' => 'index',
- 'id' => null,
- '_ext' => 'json'
- ));
- $expected = '/articles.json';
- $this->assertEquals($expected, $result);
- }
- /**
- * Test url generation with named routes.
- */
- public function testUrlGenerationNamedRoute() {
- Router::connect(
- '/users',
- array('controller' => 'users', 'action' => 'index'),
- array('_name' => 'users-index')
- );
- Router::connect(
- '/users/:name',
- array('controller' => 'users', 'action' => 'view'),
- array('_name' => 'test')
- );
- Router::connect(
- '/view/*',
- ['action' => 'view'],
- ['_name' => 'Articles::view']
- );
- $url = Router::url(['_name' => 'test', 'name' => 'mark']);
- $this->assertEquals('/users/mark', $url);
- $url = Router::url([
- '_name' => 'test', 'name' => 'mark',
- 'page' => 1, 'sort' => 'title', 'dir' => 'desc'
- ]);
- $this->assertEquals('/users/mark?page=1&sort=title&dir=desc', $url);
- $url = Router::url(['_name' => 'Articles::view']);
- $this->assertEquals('/view/', $url);
- $url = Router::url(['_name' => 'Articles::view', '1']);
- $this->assertEquals('/view/1', $url);
- $url = Router::url(['_name' => 'Articles::view', '_full' => true, '1']);
- $this->assertEquals('http://localhost/view/1', $url);
- $url = Router::url(['_name' => 'Articles::view', '1', '#' => 'frag']);
- $this->assertEquals('/view/1#frag', $url);
- }
- /**
- * Test that using invalid names causes exceptions.
- *
- * @expectedException \Cake\Routing\Error\MissingRouteException
- * @return void
- */
- public function testNamedRouteException() {
- Router::connect(
- '/users/:name',
- array('controller' => 'users', 'action' => 'view'),
- array('_name' => 'test')
- );
- $url = Router::url(['_name' => 'junk', 'name' => 'mark']);
- }
- /**
- * Test that url filters are applied to url params.
- *
- * @return void
- */
- public function testUrlGenerationWithUrlFilter() {
- Router::connect('/:lang/:controller/:action/*');
- $request = new Request();
- $request->addParams(array(
- 'lang' => 'en',
- 'controller' => 'posts',
- 'action' => 'index'
- ))->addPaths(array(
- 'base' => '',
- 'here' => '/'
- ));
- Router::pushRequest($request);
- $calledCount = 0;
- Router::addUrlFilter(function ($url, $request) use (&$calledCount) {
- $calledCount++;
- $url['lang'] = $request->lang;
- return $url;
- });
- Router::addUrlFilter(function ($url, $request) use (&$calledCount) {
- $calledCount++;
- $url[] = '1234';
- return $url;
- });
- $result = Router::url(array('controller' => 'tasks', 'action' => 'edit'));
- $this->assertEquals('/en/tasks/edit/1234', $result);
- $this->assertEquals(2, $calledCount);
- }
- /**
- * Test that plain strings urls work
- *
- * @return void
- */
- public function testUrlGenerationPlainString() {
- $mailto = 'mailto:mark@example.com';
- $result = Router::url($mailto);
- $this->assertEquals($mailto, $result);
- $js = 'javascript:alert("hi")';
- $result = Router::url($js);
- $this->assertEquals($js, $result);
- $hash = '#first';
- $result = Router::url($hash);
- $this->assertEquals($hash, $result);
- }
- /**
- * test that you can leave active plugin routes with plugin = null
- *
- * @return void
- */
- public function testCanLeavePlugin() {
- Router::connect('/admin/:controller', array('action' => 'index', 'prefix' => 'admin'));
- Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
- $request = new Request();
- Router::setRequestInfo(
- $request->addParams(array(
- 'pass' => [],
- 'prefix' => 'admin',
- 'plugin' => 'this',
- 'action' => 'index',
- 'controller' => 'interesting',
- ))->addPaths(array(
- 'base' => '',
- 'here' => '/admin/this/interesting/index',
- 'webroot' => '/',
- ))
- );
- $result = Router::url(array('plugin' => null, 'controller' => 'posts', 'action' => 'index'));
- $this->assertEquals('/admin/posts', $result);
- }
- /**
- * 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();
- $this->_connectDefaultRoutes();
- $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);
- }
- /**
- * testUuidRoutes method
- *
- * @return void
- */
- public 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' => [],
- 'category_id' => '4795d601-19c8-49a6-930e-06a8b01d17b7',
- 'plugin' => null,
- 'controller' => 'subjects',
- 'action' => 'add'
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testRouteSymmetry method
- *
- * @return void
- */
- public function testRouteSymmetry() {
- Router::connect(
- "/:extra/page/:slug/*",
- array('controller' => 'pages', 'action' => 'view', 'extra' => null),
- array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+', "action" => 'view')
- );
- $result = Router::parse('/some_extra/page/this_is_the_slug');
- $expected = array(
- 'pass' => [],
- 'plugin' => null,
- 'controller' => 'pages',
- 'action' => 'view',
- 'slug' => 'this_is_the_slug',
- 'extra' => 'some_extra'
- );
- $this->assertEquals($expected, $result);
- $result = Router::parse('/page/this_is_the_slug');
- $expected = array(
- 'pass' => [],
- 'plugin' => null,
- 'controller' => 'pages',
- 'action' => 'view',
- 'slug' => 'this_is_the_slug',
- 'extra' => null
- );
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::connect(
- "/:extra/page/:slug/*",
- array('controller' => 'pages', 'action' => 'view', 'extra' => null),
- array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+')
- );
- $result = Router::url(array(
- 'admin' => null,
- 'plugin' => null,
- 'controller' => 'pages',
- 'action' => 'view',
- 'slug' => 'this_is_the_slug',
- 'extra' => null
- ));
- $expected = '/page/this_is_the_slug';
- $this->assertEquals($expected, $result);
- $result = Router::url(array(
- 'admin' => null,
- 'plugin' => null,
- 'controller' => 'pages',
- 'action' => 'view',
- 'slug' => 'this_is_the_slug',
- 'extra' => 'some_extra'
- ));
- $expected = '/some_extra/page/this_is_the_slug';
- $this->assertEquals($expected, $result);
- }
- /**
- * Test exceptions when parsing fails.
- *
- * @expectedException Cake\Routing\Error\MissingRouteException
- */
- public function testParseError() {
- Router::connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
- Router::parse('/nope');
- }
- /**
- * Test parse and reverse symmetry
- *
- * @return void
- * @dataProvider parseReverseSymmetryData
- */
- public function testParseReverseSymmetry($url) {
- $this->_connectDefaultRoutes();
- $this->assertSame($url, Router::reverse(Router::parse($url) + array('url' => [])));
- }
- /**
- * Data for parse and reverse test
- *
- * @return array
- */
- public function parseReverseSymmetryData() {
- return array(
- array('/controller/action'),
- array('/controller/action/param'),
- array('/controller/action?param1=value1¶m2=value2'),
- array('/controller/action/param?param1=value1'),
- );
- }
- /**
- * testParseExtensions method
- *
- * @return void
- */
- public function testParseExtensions() {
- Router::extensions();
- Router::parseExtensions('rss', false);
- $this->assertContains('rss', Router::extensions());
- }
- /**
- * testSetExtensions method
- *
- * @return void
- */
- public function testSetExtensions() {
- Router::parseExtensions('rss', false);
- $this->assertContains('rss', Router::extensions());
- $this->_connectDefaultRoutes();
- $result = Router::parse('/posts.rss');
- $this->assertEquals('rss', $result['_ext']);
- $result = Router::parse('/posts.xml');
- $this->assertFalse(isset($result['_ext']));
- Router::parseExtensions(array('xml'));
- }
- /**
- * testExtensionParsing method
- *
- * @return void
- */
- public function testExtensionParsing() {
- Router::parseExtensions('rss', false);
- $this->_connectDefaultRoutes();
- $result = Router::parse('/posts.rss');
- $expected = array(
- 'plugin' => null,
- 'controller' => 'Posts',
- 'action' => 'index',
- '_ext' => 'rss',
- 'pass' => []
- );
- $this->assertEquals($expected, $result);
- $result = Router::parse('/posts/view/1.rss');
- $expected = array(
- 'plugin' => null,
- 'controller' => 'Posts',
- 'action' => 'view',
- 'pass' => array('1'),
- '_ext' => 'rss'
- );
- $this->assertEquals($expected, $result);
- $result = Router::parse('/posts/view/1.rss?query=test');
- $expected['?'] = array('query' => 'test');
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::parseExtensions(['rss', 'xml'], false);
- $this->_connectDefaultRoutes();
- $result = Router::parse('/posts.xml');
- $expected = array(
- 'plugin' => null,
- 'controller' => 'Posts',
- 'action' => 'index',
- '_ext' => 'xml',
- 'pass' => []
- );
- $this->assertEquals($expected, $result);
- $result = Router::parse('/posts.atom?hello=goodbye');
- $expected = array(
- 'plugin' => null,
- 'controller' => 'Posts.atom',
- 'action' => 'index',
- 'pass' => [],
- '?' => array('hello' => 'goodbye')
- );
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', '_ext' => 'rss'));
- $result = Router::parse('/controller/action');
- $expected = array('controller' => 'controller', 'action' => 'action', 'plugin' => null, '_ext' => 'rss', 'pass' => []);
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', '_ext' => 'rss'));
- $result = Router::parse('/controller/action');
- $expected = array(
- 'controller' => 'controller',
- 'action' => 'action',
- 'plugin' => null,
- '_ext' => 'rss',
- 'pass' => []
- );
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::parseExtensions('rss', false);
- Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', '_ext' => 'rss'));
- $result = Router::parse('/controller/action');
- $expected = array(
- 'controller' => 'controller',
- 'action' => 'action',
- 'plugin' => null,
- '_ext' => 'rss',
- 'pass' => []
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * Test newer style automatically generated prefix routes.
- *
- * @return void
- * @see testUrlGenerationWithAutoPrefixes
- */
- public function testUrlGenerationWithAutoPrefixes() {
- Router::reload();
- Router::connect('/protected/:controller/:action/*', array('prefix' => 'protected'));
- Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
- Router::connect('/:controller/:action/*');
- $request = new Request();
- Router::setRequestInfo(
- $request->addParams(array(
- 'plugin' => null, 'controller' => 'images', 'action' => 'index',
- 'prefix' => null, 'protected' => false, 'url' => array('url' => 'images/index')
- ))->addPaths(array(
- 'base' => '',
- 'here' => '/images/index',
- 'webroot' => '/',
- ))
- );
- $result = Router::url(array('controller' => 'images', 'action' => 'add'));
- $expected = '/images/add';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('controller' => 'images', 'action' => 'add', 'prefix' => 'protected'));
- $expected = '/protected/images/add';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('controller' => 'images', 'action' => 'add_protected_test', 'prefix' => 'protected'));
- $expected = '/protected/images/add_protected_test';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('action' => 'edit', 1));
- $expected = '/images/edit/1';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('action' => 'edit', 1, 'prefix' => 'protected'));
- $expected = '/protected/images/edit/1';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('action' => 'protectededit', 1, 'prefix' => 'protected'));
- $expected = '/protected/images/protectededit/1';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('action' => 'edit', 1, 'prefix' => 'protected'));
- $expected = '/protected/images/edit/1';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('controller' => 'others', 'action' => 'edit', 1));
- $expected = '/others/edit/1';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'prefix' => 'protected'));
- $expected = '/protected/others/edit/1';
- $this->assertEquals($expected, $result);
- }
- /**
- * Test that the ssl option works.
- *
- * @return void
- */
- public function testGenerationWithSslOption() {
- Router::connect('/:controller/:action/*');
- $request = new Request();
- $request->env('HTTP_HOST', 'localhost');
- Router::pushRequest(
- $request->addParams(array(
- 'plugin' => null, 'controller' => 'images', 'action' => 'index'
- ))->addPaths(array(
- 'base' => '',
- 'here' => '/images/index',
- 'webroot' => '/',
- ))
- );
- $result = Router::url(array(
- '_ssl' => true
- ));
- $this->assertEquals('https://localhost/images/index', $result);
- $result = Router::url(array(
- '_ssl' => false
- ));
- $this->assertEquals('http://localhost/images/index', $result);
- }
- /**
- * Test ssl option when the current request is ssl.
- *
- * @return void
- */
- public function testGenerateWithSslInSsl() {
- Router::connect('/:controller/:action/*');
- $request = new Request();
- $request->env('HTTP_HOST', 'localhost');
- $request->env('HTTPS', 'on');
- Router::pushRequest(
- $request->addParams(array(
- 'plugin' => null,
- 'controller' => 'images',
- 'action' => 'index'
- ))->addPaths(array(
- 'base' => '',
- 'here' => '/images/index',
- 'webroot' => '/',
- ))
- );
- $result = Router::url(array(
- '_ssl' => false
- ));
- $this->assertEquals('http://localhost/images/index', $result);
- $result = Router::url(array(
- '_ssl' => true
- ));
- $this->assertEquals('https://localhost/images/index', $result);
- }
- /**
- * test that prefix routes persist when they are in the current request.
- *
- * @return void
- */
- public function testPrefixRoutePersistence() {
- Router::reload();
- Router::connect('/protected/:controller/:action', array('prefix' => 'protected'));
- Router::connect('/:controller/:action');
- $request = new Request();
- Router::setRequestInfo(
- $request->addParams(array(
- 'plugin' => null,
- 'controller' => 'images',
- 'action' => 'index',
- 'prefix' => 'protected',
- ))->addPaths(array(
- 'base' => '',
- 'here' => '/protected/images/index',
- 'webroot' => '/',
- ))
- );
- $result = Router::url(array('prefix' => 'protected', 'controller' => 'images', 'action' => 'add'));
- $expected = '/protected/images/add';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('controller' => 'images', 'action' => 'add'));
- $expected = '/protected/images/add';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('controller' => 'images', 'action' => 'add', 'prefix' => false));
- $expected = '/images/add';
- $this->assertEquals($expected, $result);
- }
- /**
- * test that setting a prefix override the current one
- *
- * @return void
- */
- public function testPrefixOverride() {
- Router::connect('/admin/:controller/:action', array('prefix' => 'admin'));
- Router::connect('/protected/:controller/:action', array('prefix' => 'protected'));
- $request = new Request();
- Router::setRequestInfo(
- $request->addParams(array(
- 'plugin' => null, 'controller' => 'images', 'action' => 'index', 'prefix' => 'protected',
- ))->addPaths(array(
- 'base' => '',
- 'here' => '/protected/images/index',
- 'webroot' => '/',
- ))
- );
- $result = Router::url(array('controller' => 'images', 'action' => 'add', 'prefix' => 'admin'));
- $expected = '/admin/images/add';
- $this->assertEquals($expected, $result);
- $request = new Request();
- Router::setRequestInfo(
- $request->addParams(array(
- 'plugin' => null,
- 'controller' => 'images',
- 'action' => 'index',
- 'prefix' => 'admin',
- ))->addPaths(array(
- 'base' => '',
- 'here' => '/admin/images/index',
- 'webroot' => '/',
- ))
- );
- $result = Router::url(array('controller' => 'images', 'action' => 'add', 'prefix' => 'protected'));
- $expected = '/protected/images/add';
- $this->assertEquals($expected, $result);
- }
- /**
- * Test that well known route parameters are passed through.
- *
- * @return void
- */
- public function testRouteParamDefaults() {
- Router::connect('/cache/*', array('prefix' => false, 'plugin' => true, 'controller' => 0, 'action' => 1));
- $url = Router::url(array('prefix' => 0, 'plugin' => 1, 'controller' => 0, 'action' => 1, 'test'));
- $expected = '/cache/test';
- $this->assertEquals($expected, $url);
- try {
- Router::url(array('controller' => 0, 'action' => 1, 'test'));
- $this->fail('No exception raised');
- } catch (\Exception $e) {
- $this->assertTrue(true, 'Exception was raised');
- }
- try {
- Router::url(array('prefix' => 1, 'controller' => 0, 'action' => 1, 'test'));
- $this->fail('No exception raised');
- } catch (\Exception $e) {
- $this->assertTrue(true, 'Exception was raised');
- }
- }
- /**
- * testRemoveBase method
- *
- * @return void
- */
- public function testRemoveBase() {
- Router::connect('/:controller/:action');
- $request = new Request();
- Router::setRequestInfo(
- $request->addParams(array(
- 'plugin' => null, 'controller' => 'controller', 'action' => 'index',
- ))->addPaths(array(
- 'base' => '/base',
- 'here' => '/',
- 'webroot' => '/base/',
- ))
- );
- $result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action'));
- $expected = '/base/my_controller/my_action';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action', '_base' => false));
- $expected = '/my_controller/my_action';
- $this->assertEquals($expected, $result);
- }
- /**
- * testPagesUrlParsing method
- *
- * @return void
- */
- public function testPagesUrlParsing() {
- Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
- Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
- $result = Router::parse('/');
- $expected = array('pass' => array('home'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
- $this->assertEquals($expected, $result);
- $result = Router::parse('/pages/home/');
- $expected = array('pass' => array('home'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
- $result = Router::parse('/');
- $expected = array('pass' => array('home'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
- $this->assertEquals($expected, $result);
- Router::reload();
- Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
- Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
- $result = Router::parse('/pages/contact/');
- $expected = array('pass' => array('contact'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
- $this->assertEquals($expected, $result);
- }
- /**
- * test that requests with a trailing dot don't loose the do.
- *
- * @return void
- */
- public function testParsingWithTrailingPeriod() {
- Router::reload();
- Router::connect('/:controller/:action/*');
- $result = Router::parse('/posts/view/something.');
- $this->assertEquals('something.', $result['pass'][0], 'Period was chopped off %s');
- $result = Router::parse('/posts/view/something. . .');
- $this->assertEquals('something. . .', $result['pass'][0], 'Period was chopped off %s');
- }
- /**
- * test that requests with a trailing dot don't loose the do.
- *
- * @return void
- */
- public function testParsingWithTrailingPeriodAndParseExtensions() {
- Router::reload();
- Router::connect('/:controller/:action/*');
- Router::parseExtensions('json', false);
- $result = Router::parse('/posts/view/something.');
- $this->assertEquals('something.', $result['pass'][0], 'Period was chopped off %s');
- $result = Router::parse('/posts/view/something. . .');
- $this->assertEquals('something. . .', $result['pass'][0], 'Period was chopped off %s');
- }
- /**
- * test that patterns work for :action
- *
- * @expectedException Cake\Routing\Error\MissingRouteException
- * @return void
- */
- public function testParsingWithPatternOnAction() {
- Router::connect(
- '/blog/:action/*',
- array('controller' => 'blog_posts'),
- array('action' => 'other|actions')
- );
- $result = Router::parse('/blog/other');
- $expected = array(
- 'plugin' => null,
- 'controller' => 'blog_posts',
- 'action' => 'other',
- 'pass' => [],
- );
- $this->assertEquals($expected, $result);
- Router::parse('/blog/foobar');
- }
- /**
- * Test url() works with patterns on :action
- *
- * @expectedException Cake\Routing\Error\MissingRouteException
- * @return void
- */
- public function testUrlPatternOnAction() {
- Router::connect(
- '/blog/:action/*',
- array('controller' => 'blog_posts'),
- array('action' => 'other|actions')
- );
- $result = Router::url(array('controller' => 'blog_posts', 'action' => 'actions'));
- $this->assertEquals('/blog/actions', $result);
- $result = Router::url(array('controller' => 'blog_posts', 'action' => 'foo'));
- $this->assertEquals('/', $result);
- }
- /**
- * testParsingWithLiteralPrefixes method
- *
- * @return void
- */
- public function testParsingWithLiteralPrefixes() {
- Router::reload();
- $adminParams = array('prefix' => 'admin');
- Router::connect('/admin/:controller', $adminParams);
- Router::connect('/admin/:controller/:action/*', $adminParams);
- $request = new Request();
- Router::setRequestInfo(
- $request->addParams(array(
- 'plugin' => null, 'controller' => 'controller', 'action' => 'index'
- ))->addPaths(array(
- 'base' => '/base',
- 'here' => '/',
- 'webroot' => '/base/',
- ))
- );
- $result = Router::parse('/admin/posts/');
- $expected = array('pass' => [], 'prefix' => 'admin', 'plugin' => null, 'controller' => 'posts', 'action' => 'index');
- $this->assertEquals($expected, $result);
- $result = Router::parse('/admin/posts');
- $this->assertEquals($expected, $result);
- $result = Router::url(array('prefix' => 'admin', 'controller' => 'posts'));
- $expected = '/base/admin/posts';
- $this->assertEquals($expected, $result);
- Router::reload();
- $prefixParams = array('prefix' => 'members');
- Router::connect('/members/:controller', $prefixParams);
- Router::connect('/members/:controller/:action', $prefixParams);
- Router::connect('/members/:controller/:action/*', $prefixParams);
- $request = new Request();
- Router::setRequestInfo(
- $request->addParams(array(
- 'plugin' => null, 'controller' => 'controller', 'action' => 'index',
- ))->addPaths(array(
- 'base' => '/base',
- 'here' => '/',
- 'webroot' => '/',
- ))
- );
- $result = Router::parse('/members/posts/index');
- $expected = array('pass' => [], 'prefix' => 'members', 'plugin' => null, 'controller' => 'posts', 'action' => 'index');
- $this->assertEquals($expected, $result);
- $result = Router::url(array('prefix' => 'members', 'controller' => 'users', 'action' => 'add'));
- $expected = '/base/members/users/add';
- $this->assertEquals($expected, $result);
- }
- /**
- * Tests URL generation with flags and prefixes in and out of context
- *
- * @return void
- */
- public function testUrlWritingWithPrefixes() {
- Router::connect('/company/:controller/:action/*', array('prefix' => 'company'));
- Router::connect('/:action', array('controller' => 'users'));
- $result = Router::url(array('controller' => 'users', 'action' => 'login', 'prefix' => 'company'));
- $expected = '/company/users/login';
- $this->assertEquals($expected, $result);
- $request = new Request();
- Router::setRequestInfo(
- $request->addParams(array(
- 'plugin' => null,
- 'controller' => 'users',
- 'action' => 'login',
- 'prefix' => 'company'
- ))->addPaths(array(
- 'base' => '/',
- 'here' => '/',
- 'webroot' => '/base/',
- ))
- );
- $result = Router::url(array('controller' => 'users', 'action' => 'login', 'prefix' => false));
- $expected = '/login';
- $this->assertEquals($expected, $result);
- }
- /**
- * test url generation with prefixes and custom routes
- *
- * @return void
- */
- public function testUrlWritingWithPrefixesAndCustomRoutes() {
- Router::connect(
- '/admin/login',
- array('controller' => 'users', 'action' => 'login', 'prefix' => 'admin')
- );
- $request = new Request();
- Router::setRequestInfo(
- $request->addParams(array(
- 'plugin' => null, 'controller' => 'posts', 'action' => 'index',
- 'prefix' => 'admin'
- ))->addPaths(array(
- 'base' => '/',
- 'here' => '/',
- 'webroot' => '/',
- ))
- );
- $result = Router::url(array('controller' => 'users', 'action' => 'login'));
- $this->assertEquals('/admin/login', $result);
- $result = Router::url(array('controller' => 'users', 'action' => 'login'));
- $this->assertEquals('/admin/login', $result);
- }
- /**
- * testPassedArgsOrder method
- *
- * @return void
- */
- public function testPassedArgsOrder() {
- Router::connect('/test-passed/*', array('controller' => 'pages', 'action' => 'display', 'home'));
- Router::connect('/test2/*', array('controller' => 'pages', 'action' => 'display', 2));
- Router::connect('/test/*', array('controller' => 'pages', 'action' => 'display', 1));
- $result = Router::url(array('controller' => 'pages', 'action' => 'display', 1, 'whatever'));
- $expected = '/test/whatever';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('controller' => 'pages', 'action' => 'display', 2, 'whatever'));
- $expected = '/test2/whatever';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('controller' => 'pages', 'action' => 'display', 'home', 'whatever'));
- $expected = '/test-passed/whatever';
- $this->assertEquals($expected, $result);
- }
- /**
- * testRegexRouteMatching method
- *
- * @return void
- */
- public function testRegexRouteMatching() {
- Router::connect('/:locale/:controller/:action/*', [], array('locale' => 'dan|eng'));
- $result = Router::parse('/eng/test/test_action');
- $expected = array('pass' => [], 'locale' => 'eng', 'controller' => 'test', 'action' => 'test_action', 'plugin' => null);
- $this->assertEquals($expected, $result);
- }
- /**
- * testRegexRouteMatching error
- *
- * @expectedException Cake\Routing\Error\MissingRouteException
- * @return void
- */
- public function testRegexRouteMatchingError() {
- Router::connect('/:locale/:controller/:action/*', [], array('locale' => 'dan|eng'));
- Router::parse('/badness/test/test_action');
- }
- /**
- * testRegexRouteMatching method
- *
- * @expectedException Cake\Routing\Error\MissingRouteException
- * @return void
- */
- public function testRegexRouteMatchUrl() {
- Router::connect('/:locale/:controller/:action/*', [], array('locale' => 'dan|eng'));
- $request = new Request();
- Router::setRequestInfo(
- $request->addParams(array(
- 'plugin' => null,
- 'controller' => 'test',
- 'action' => 'index',
- 'url' => array('url' => 'test/test_action')
- ))->addPaths(array(
- 'base' => '',
- 'here' => '/test/test_action',
- 'webroot' => '/',
- ))
- );
- $result = Router::url(array('action' => 'test_another_action', 'locale' => 'eng'));
- $expected = '/eng/test/test_another_action';
- $this->assertEquals($expected, $result);
- $result = Router::url(array('action' => 'test_another_action'));
- $expected = '/';
- $this->assertEquals($expected, $result);
- }
- /**
- * test using a custom route class for route connection
- *
- * @return void
- */
- public function testUsingCustomRouteClass() {
- Plugin::load('TestPlugin');
- Router::connect(
- '/:slug',
- array('plugin' => 'TestPlugin', 'action' => 'index'),
- array('routeClass' => 'PluginShortRoute', 'slug' => '[a-z_-]+')
- );
- $result = Router::parse('/the-best');
- $expected = [
- 'plugin' => 'TestPlugin',
- 'controller' => 'TestPlugin',
- 'action' => 'index',
- 'slug' => 'the-best',
- 'pass' => [],
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * test using custom route class in PluginDot notation
- *
- * @return void
- */
- public function testUsingCustomRouteClassPluginDotSyntax() {
- Plugin::load('TestPlugin');
- Router::connect(
- '/:slug',
- array('controller' => 'posts', 'action' => 'view'),
- array('routeClass' => 'TestPlugin.TestRoute', 'slug' => '[a-z_-]+')
- );
- $this->assertTrue(true); // Just to make sure the connect do not throw exception
- Plugin::unload('TestPlugin');
- }
- /**
- * test that route classes must extend \Cake\Routing\Route\Route
- *
- * @expectedException \Cake\Error\Exception
- * @return void
- */
- public function testCustomRouteException() {
- Router::connect('/:controller', [], array('routeClass' => 'Object'));
- }
- /**
- * test reversing parameter arrays back into strings.
- *
- * Mark the router as initialized so it doesn't auto-load routes
- *
- * @return void
- */
- public function testReverse() {
- Router::connect('/:controller/:action/*');
- $params = array(
- 'controller' => 'posts',
- 'action' => 'view',
- 'pass' => array(1),
- 'url' => [],
- 'autoRender' => 1,
- 'bare' => 1,
- 'return' => 1,
- 'requested' => 1,
- '_Token' => array('key' => 'sekret')
- );
- $result = Router::reverse($params);
- $this->assertEquals('/posts/view/1', $result);
- Router::reload();
- Router::connect('/:lang/:controller/:action/*', [], array('lang' => '[a-z]{3}'));
- $params = array(
- 'lang' => 'eng',
- 'controller' => 'posts',
- 'action' => 'view',
- 'pass' => array(1),
- 'url' => array('url' => 'eng/posts/view/1')
- );
- $result = Router::reverse($params);
- $this->assertEquals('/eng/posts/view/1', $result);
- $params = array(
- 'lang' => 'eng',
- 'controller' => 'posts',
- 'action' => 'view',
- 1,
- '?' => ['foo' => 'bar']
- );
- $result = Router::reverse($params);
- $this->assertEquals('/eng/posts/view/1?foo=bar', $result);
- $params = array(
- 'lang' => 'eng',
- 'controller' => 'posts',
- 'action' => 'view',
- 'pass' => array(1),
- 'url' => array('url' => 'eng/posts/view/1', 'foo' => 'bar', 'baz' => 'quu'),
- 'paging' => [],
- 'models' => []
- );
- $result = Router::reverse($params);
- $this->assertEquals('/eng/posts/view/1?foo=bar&baz=quu', $result);
- $request = new Request('/eng/posts/view/1');
- $request->addParams(array(
- 'lang' => 'eng',
- 'controller' => 'posts',
- 'action' => 'view',
- 'pass' => array(1),
- ));
- $request->query = array('url' => 'eng/posts/view/1', 'test' => 'value');
- $result = Router::reverse($request);
- $expected = '/eng/posts/view/1?test=value';
- $this->assertEquals($expected, $result);
- $params = array(
- 'lang' => 'eng',
- 'controller' => 'posts',
- 'action' => 'view',
- 'pass' => array(1),
- 'url' => array('url' => 'eng/posts/view/1')
- );
- $result = Router::reverse($params, true);
- $this->assertRegExp('/^http(s)?:\/\//', $result);
- }
- /**
- * Test that extensions work with Router::reverse()
- *
- * @return void
- */
- public function testReverseWithExtension() {
- Router::connect('/:controller/:action/*');
- Router::parseExtensions('json', false);
- $request = new Request('/posts/view/1.json');
- $request->addParams(array(
- 'controller' => 'posts',
- 'action' => 'view',
- 'pass' => array(1),
- '_ext' => 'json',
- ));
- $request->query = [];
- $result = Router::reverse($request);
- $expected = '/posts/view/1.json';
- $this->assertEquals($expected, $result);
- }
- /**
- * test that setRequestInfo can accept arrays and turn that into a Request object.
- *
- * @return void
- */
- public function testSetRequestInfoLegacy() {
- Router::setRequestInfo(array(
- array(
- 'plugin' => null, 'controller' => 'images', 'action' => 'index',
- 'url' => array('url' => 'protected/images/index')
- ),
- array(
- 'base' => '',
- 'here' => '/protected/images/index',
- 'webroot' => '/',
- )
- ));
- $result = Router::getRequest();
- $this->assertEquals('images', $result->controller);
- $this->assertEquals('index', $result->action);
- $this->assertEquals('', $result->base);
- $this->assertEquals('/protected/images/index', $result->here);
- $this->assertEquals('/', $result->webroot);
- }
- /**
- * test get request.
- *
- * @return void
- */
- public function testGetRequest() {
- $requestA = new Request('/');
- $requestB = new Request('/posts');
- Router::pushRequest($requestA);
- Router::pushRequest($requestB);
- $this->assertSame($requestA, Router::getRequest(false));
- $this->assertSame($requestB, Router::getRequest(true));
- }
- /**
- * Test that Router::url() uses the first request
- */
- public function testUrlWithRequestAction() {
- Router::connect('/:controller', array('action' => 'index'));
- Router::connect('/:controller/:action');
- $firstRequest = new Request('/posts/index');
- $firstRequest->addParams(array(
- 'plugin' => null,
- 'controller' => 'posts',
- 'action' => 'index'
- ))->addPaths(array('base' => ''));
- $secondRequest = new Request('/posts/index');
- $secondRequest->addParams(array(
- 'requested' => 1,
- 'plugin' => null,
- 'controller' => 'comments',
- 'action' => 'listing'
- ))->addPaths(array('base' => ''));
- Router::setRequestInfo($firstRequest);
- Router::setRequestInfo($secondRequest);
- $result = Router::url(array('_base' => false));
- $this->assertEquals('/comments/listing', $result, 'with second requests, the last should win.');
- Router::popRequest();
- $result = Router::url(array('_base' => false));
- $this->assertEquals('/posts', $result, 'with second requests, the last should win.');
- // Make sure that popping an empty request doesn't fail.
- Router::popRequest();
- }
- /**
- * test that a route object returning a full URL is not modified.
- *
- * @return void
- */
- public function testUrlFullUrlReturnFromRoute() {
- $url = 'http://example.com/posts/view/1';
- $route = $this->getMock(
- 'Cake\Routing\Route\Route',
- ['match'],
- ['/:controller/:action/*']
- );
- $route->expects($this->any())
- ->method('match')
- ->will($this->returnValue($url));
- Router::connect($route);
- $result = Router::url(array('controller' => 'posts', 'action' => 'view', 1));
- $this->assertEquals($url, $result);
- }
- /**
- * test protocol in url
- *
- * @return void
- */
- public function testUrlProtocol() {
- $url = 'http://example.com';
- $this->assertEquals($url, Router::url($url));
- $url = 'ed2k://example.com';
- $this->assertEquals($url, Router::url($url));
- $url = 'svn+ssh://example.com';
- $this->assertEquals($url, Router::url($url));
- $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 = Configure::read('App.fullBaseUrl') . '/posts/index#here';
- $this->assertEquals($expected, Router::url($url, true));
- }
- /**
- * Testing that patterns on the :action param work properly.
- *
- * @return void
- */
- public function testPatternOnAction() {
- $route = new Route(
- '/blog/:action/*',
- array('controller' => 'blog_posts'),
- array('action' => 'other|actions')
- );
- $result = $route->match(array('controller' => 'blog_posts', 'action' => 'foo'));
- $this->assertFalse($result);
- $result = $route->match(array('controller' => 'blog_posts', 'action' => 'actions'));
- $this->assertEquals('/blog/actions/', $result);
- $result = $route->parse('/blog/other');
- $expected = array('controller' => 'blog_posts', 'action' => 'other', 'pass' => []);
- $this->assertEquals($expected, $result);
- $result = $route->parse('/blog/foobar');
- $this->assertFalse($result);
- }
- /**
- * Test that redirect() works.
- *
- * @return void
- */
- public function testRedirect() {
- Router::redirect('/mobile', '/', ['status' => 301]);
- $routes = Router::routes();
- $route = $routes[0];
- $this->assertInstanceOf('Cake\Routing\Route\RedirectRoute', $route);
- }
- /**
- * Test that the compatibility method for incoming urls works.
- *
- * @return void
- */
- public function testParseNamedParameters() {
- $request = new Request();
- $request->addParams(array(
- 'controller' => 'posts',
- 'action' => 'index',
- ));
- $result = Router::parseNamedParams($request);
- $this->assertSame([], $result->params['named']);
- $request = new Request();
- $request->addParams(array(
- 'controller' => 'posts',
- 'action' => 'index',
- 'pass' => array('home', 'one:two', 'three:four', 'five[nested][0]:six', 'five[nested][1]:seven')
- ));
- Router::parseNamedParams($request);
- $expected = array(
- 'plugin' => null,
- 'controller' => 'posts',
- 'action' => 'index',
- '_ext' => null,
- 'pass' => array('home'),
- 'named' => array(
- 'one' => 'two',
- 'three' => 'four',
- 'five' => array(
- 'nested' => array('six', 'seven')
- )
- )
- );
- $this->assertEquals($expected, $request->params);
- }
- /**
- * Test the scope() method
- *
- * @return void
- */
- public function testScope() {
- Router::scope('/path', ['param' => 'value'], function($routes) {
- $this->assertInstanceOf('Cake\Routing\RouteBuilder', $routes);
- $this->assertEquals('/path', $routes->path());
- $this->assertEquals(['param' => 'value'], $routes->params());
- $routes->connect('/articles', ['controller' => 'Articles']);
- });
- }
- /**
- * Test the scope() method
- *
- * @expectedException \InvalidArgumentException
- * @return void
- */
- public function testScopeError() {
- Router::scope('/path', 'derpy');
- }
- /**
- * Test that prefix() creates a scope.
- *
- * @return void
- */
- public function testPrefix() {
- Router::prefix('admin', function($routes) {
- $this->assertInstanceOf('Cake\Routing\RouteBuilder', $routes);
- $this->assertEquals('/admin', $routes->path());
- $this->assertEquals(['prefix' => 'admin'], $routes->params());
- });
- }
- /**
- * Test that plugin() creates a scope.
- *
- * @return void
- */
- public function testPlugin() {
- Router::plugin('DebugKit', function($routes) {
- $this->assertInstanceOf('Cake\Routing\RouteBuilder', $routes);
- $this->assertEquals('/debug_kit', $routes->path());
- $this->assertEquals(['plugin' => 'DebugKit'], $routes->params());
- });
- }
- /**
- * Test that plugin() accepts options
- *
- * @return void
- */
- public function testPluginOptions() {
- Router::plugin('DebugKit', ['path' => '/debugger'], function($routes) {
- $this->assertInstanceOf('Cake\Routing\RouteBuilder', $routes);
- $this->assertEquals('/debugger', $routes->path());
- $this->assertEquals(['plugin' => 'DebugKit'], $routes->params());
- });
- }
- /**
- * Connect some fallback routes for testing router behavior.
- *
- * @return void
- */
- protected function _connectDefaultRoutes() {
- Router::scope('/', function($routes) {
- $routes->fallbacks();
- });
- }
- }
|