DispatcherTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 1.2.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Routing;
  15. use Cake\Controller\Controller;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\Network\Request;
  19. use Cake\Network\Response;
  20. use Cake\Network\Session;
  21. use Cake\Routing\Dispatcher;
  22. use Cake\Routing\Filter\ControllerFactoryFilter;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * A testing stub that doesn't send headers.
  26. */
  27. class DispatcherMockResponse extends Response
  28. {
  29. protected function _sendHeader($name, $value = null)
  30. {
  31. return $name . ' ' . $value;
  32. }
  33. }
  34. /**
  35. * TestDispatcher class
  36. */
  37. class TestDispatcher extends Dispatcher
  38. {
  39. /**
  40. * Controller instance, made publicly available for testing
  41. *
  42. * @var Controller
  43. */
  44. public $controller;
  45. /**
  46. * invoke method
  47. *
  48. * @param \Cake\Controller\Controller $controller
  49. * @return \Cake\Network\Response $response
  50. */
  51. protected function _invoke(Controller $controller)
  52. {
  53. $this->controller = $controller;
  54. return parent::_invoke($controller);
  55. }
  56. }
  57. /**
  58. * MyPluginAppController class
  59. *
  60. */
  61. class MyPluginAppController extends Controller
  62. {
  63. }
  64. interface DispatcherTestInterfaceController
  65. {
  66. public function index();
  67. }
  68. /**
  69. * MyPluginController class
  70. *
  71. */
  72. class MyPluginController extends MyPluginAppController
  73. {
  74. /**
  75. * name property
  76. *
  77. * @var string
  78. */
  79. public $name = 'MyPlugin';
  80. /**
  81. * index method
  82. *
  83. * @return void
  84. */
  85. public function index()
  86. {
  87. return true;
  88. }
  89. /**
  90. * add method
  91. *
  92. * @return void
  93. */
  94. public function add()
  95. {
  96. return true;
  97. }
  98. /**
  99. * admin_add method
  100. *
  101. * @param mixed $id
  102. * @return void
  103. */
  104. public function admin_add($id = null)
  105. {
  106. return $id;
  107. }
  108. }
  109. /**
  110. * OtherPagesController class
  111. *
  112. */
  113. class OtherPagesController extends MyPluginAppController
  114. {
  115. /**
  116. * name property
  117. *
  118. * @var string
  119. */
  120. public $name = 'OtherPages';
  121. /**
  122. * display method
  123. *
  124. * @param string $page
  125. * @return void
  126. */
  127. public function display($page = null)
  128. {
  129. return $page;
  130. }
  131. /**
  132. * index method
  133. *
  134. * @return void
  135. */
  136. public function index()
  137. {
  138. return true;
  139. }
  140. }
  141. /**
  142. * ArticlesTestAppController class
  143. *
  144. */
  145. class ArticlesTestAppController extends Controller
  146. {
  147. }
  148. /**
  149. * ArticlesTestController class
  150. *
  151. */
  152. class ArticlesTestController extends ArticlesTestAppController
  153. {
  154. /**
  155. * name property
  156. *
  157. * @var string
  158. */
  159. public $name = 'ArticlesTest';
  160. /**
  161. * admin_index method
  162. *
  163. * @return void
  164. */
  165. public function admin_index()
  166. {
  167. return true;
  168. }
  169. /**
  170. * fake index method.
  171. *
  172. * @return void
  173. */
  174. public function index()
  175. {
  176. return true;
  177. }
  178. }
  179. /**
  180. * DispatcherTest class
  181. *
  182. */
  183. class DispatcherTest extends TestCase
  184. {
  185. /**
  186. * setUp method
  187. *
  188. * @return void
  189. */
  190. public function setUp()
  191. {
  192. parent::setUp();
  193. $_GET = [];
  194. Configure::write('App.base', false);
  195. Configure::write('App.baseUrl', false);
  196. Configure::write('App.dir', 'app');
  197. Configure::write('App.webroot', 'webroot');
  198. Configure::write('App.namespace', 'TestApp');
  199. $this->dispatcher = new TestDispatcher();
  200. $this->dispatcher->addFilter(new ControllerFactoryFilter());
  201. }
  202. /**
  203. * tearDown method
  204. *
  205. * @return void
  206. */
  207. public function tearDown()
  208. {
  209. parent::tearDown();
  210. Plugin::unload();
  211. }
  212. /**
  213. * testMissingController method
  214. *
  215. * @expectedException \Cake\Routing\Exception\MissingControllerException
  216. * @expectedExceptionMessage Controller class SomeController could not be found.
  217. * @return void
  218. */
  219. public function testMissingController()
  220. {
  221. $request = new Request([
  222. 'url' => 'some_controller/home',
  223. 'params' => [
  224. 'controller' => 'SomeController',
  225. 'action' => 'home',
  226. ]
  227. ]);
  228. $response = $this->getMock('Cake\Network\Response');
  229. $this->dispatcher->dispatch($request, $response, ['return' => 1]);
  230. }
  231. /**
  232. * testMissingControllerInterface method
  233. *
  234. * @expectedException \Cake\Routing\Exception\MissingControllerException
  235. * @expectedExceptionMessage Controller class DispatcherTestInterface could not be found.
  236. * @return void
  237. */
  238. public function testMissingControllerInterface()
  239. {
  240. $request = new Request([
  241. 'url' => 'dispatcher_test_interface/index',
  242. 'params' => [
  243. 'controller' => 'DispatcherTestInterface',
  244. 'action' => 'index',
  245. ]
  246. ]);
  247. $url = new Request('dispatcher_test_interface/index');
  248. $response = $this->getMock('Cake\Network\Response');
  249. $this->dispatcher->dispatch($request, $response, ['return' => 1]);
  250. }
  251. /**
  252. * testMissingControllerInterface method
  253. *
  254. * @expectedException \Cake\Routing\Exception\MissingControllerException
  255. * @expectedExceptionMessage Controller class Abstract could not be found.
  256. * @return void
  257. */
  258. public function testMissingControllerAbstract()
  259. {
  260. $request = new Request([
  261. 'url' => 'abstract/index',
  262. 'params' => [
  263. 'controller' => 'Abstract',
  264. 'action' => 'index',
  265. ]
  266. ]);
  267. $response = $this->getMock('Cake\Network\Response');
  268. $this->dispatcher->dispatch($request, $response, ['return' => 1]);
  269. }
  270. /**
  271. * Test that lowercase controller names result in missing controller errors.
  272. *
  273. * In case-insensitive file systems, lowercase controller names will kind of work.
  274. * This causes annoying deployment issues for lots of folks.
  275. *
  276. * @expectedException \Cake\Routing\Exception\MissingControllerException
  277. * @expectedExceptionMessage Controller class somepages could not be found.
  278. * @return void
  279. */
  280. public function testMissingControllerLowercase()
  281. {
  282. $request = new Request([
  283. 'url' => 'pages/home',
  284. 'params' => [
  285. 'controller' => 'somepages',
  286. 'action' => 'display',
  287. 'pass' => ['home'],
  288. ]
  289. ]);
  290. $response = $this->getMock('Cake\Network\Response');
  291. $this->dispatcher->dispatch($request, $response, ['return' => 1]);
  292. }
  293. /**
  294. * testDispatch method
  295. *
  296. * @return void
  297. */
  298. public function testDispatchBasic()
  299. {
  300. $url = new Request([
  301. 'url' => 'pages/home',
  302. 'params' => [
  303. 'controller' => 'Pages',
  304. 'action' => 'display',
  305. 'pass' => ['extract'],
  306. 'return' => 1
  307. ]
  308. ]);
  309. $response = $this->getMock('Cake\Network\Response');
  310. $this->dispatcher->dispatch($url, $response);
  311. $expected = 'Pages';
  312. $this->assertEquals($expected, $this->dispatcher->controller->name);
  313. }
  314. /**
  315. * Test that Dispatcher handles actions that return response objects.
  316. *
  317. * @return void
  318. */
  319. public function testDispatchActionReturnsResponse()
  320. {
  321. $request = new Request([
  322. 'url' => 'some_pages/responseGenerator',
  323. 'params' => [
  324. 'controller' => 'SomePages',
  325. 'action' => 'responseGenerator',
  326. 'pass' => []
  327. ]
  328. ]);
  329. $response = $this->getMock('Cake\Network\Response', ['_sendHeader']);
  330. ob_start();
  331. $this->dispatcher->dispatch($request, $response);
  332. $result = ob_get_clean();
  333. $this->assertEquals('new response', $result);
  334. }
  335. /**
  336. * testPrefixDispatch method
  337. *
  338. * @return void
  339. */
  340. public function testPrefixDispatch()
  341. {
  342. $request = new Request([
  343. 'url' => 'admin/posts/index',
  344. 'params' => [
  345. 'prefix' => 'Admin',
  346. 'controller' => 'Posts',
  347. 'action' => 'index',
  348. 'pass' => [],
  349. 'return' => 1
  350. ]
  351. ]);
  352. $response = $this->getMock('Cake\Network\Response');
  353. $this->dispatcher->dispatch($request, $response);
  354. $this->assertInstanceOf(
  355. 'TestApp\Controller\Admin\PostsController',
  356. $this->dispatcher->controller
  357. );
  358. $expected = '/admin/posts/index';
  359. $this->assertSame($expected, $request->here);
  360. }
  361. /**
  362. * test prefix dispatching in a plugin.
  363. *
  364. * @return void
  365. */
  366. public function testPrefixDispatchPlugin()
  367. {
  368. Plugin::load('TestPlugin');
  369. $request = new Request([
  370. 'url' => 'admin/test_plugin/comments/index',
  371. 'params' => [
  372. 'plugin' => 'TestPlugin',
  373. 'prefix' => 'Admin',
  374. 'controller' => 'Comments',
  375. 'action' => 'index',
  376. 'pass' => [],
  377. 'return' => 1
  378. ]
  379. ]);
  380. $response = $this->getMock('Cake\Network\Response');
  381. $this->dispatcher->dispatch($request, $response);
  382. $this->assertInstanceOf(
  383. 'TestPlugin\Controller\Admin\CommentsController',
  384. $this->dispatcher->controller
  385. );
  386. }
  387. /**
  388. * test forbidden controller names.
  389. *
  390. * @expectedException \Cake\Routing\Exception\MissingControllerException
  391. * @expectedExceptionMessage Controller class TestPlugin.Tests could not be found.
  392. * @return void
  393. */
  394. public function testDispatchBadPluginName()
  395. {
  396. Plugin::load('TestPlugin');
  397. $request = new Request([
  398. 'url' => 'TestPlugin.Tests/index',
  399. 'params' => [
  400. 'plugin' => '',
  401. 'controller' => 'TestPlugin.Tests',
  402. 'action' => 'index',
  403. 'pass' => [],
  404. 'return' => 1
  405. ]
  406. ]);
  407. $response = $this->getMock('Cake\Network\Response');
  408. $this->dispatcher->dispatch($request, $response);
  409. }
  410. /**
  411. * test forbidden controller names.
  412. *
  413. * @expectedException \Cake\Routing\Exception\MissingControllerException
  414. * @expectedExceptionMessage Controller class TestApp\Controller\PostsController could not be found.
  415. * @return void
  416. */
  417. public function testDispatchBadName()
  418. {
  419. $request = new Request([
  420. 'url' => 'TestApp%5CController%5CPostsController/index',
  421. 'params' => [
  422. 'plugin' => '',
  423. 'controller' => 'TestApp\Controller\PostsController',
  424. 'action' => 'index',
  425. 'pass' => [],
  426. 'return' => 1
  427. ]
  428. ]);
  429. $response = $this->getMock('Cake\Network\Response');
  430. $this->dispatcher->dispatch($request, $response);
  431. }
  432. /**
  433. * Test dispatcher filters being called.
  434. *
  435. * @return void
  436. */
  437. public function testDispatcherFilter()
  438. {
  439. $filter = $this->getMock(
  440. 'Cake\Routing\DispatcherFilter',
  441. ['beforeDispatch', 'afterDispatch']
  442. );
  443. $filter->expects($this->at(0))
  444. ->method('beforeDispatch');
  445. $filter->expects($this->at(1))
  446. ->method('afterDispatch');
  447. $this->dispatcher->addFilter($filter);
  448. $request = new Request([
  449. 'url' => '/',
  450. 'params' => [
  451. 'controller' => 'Pages',
  452. 'action' => 'display',
  453. 'home',
  454. 'pass' => []
  455. ]
  456. ]);
  457. $response = $this->getMock('Cake\Network\Response', ['send']);
  458. $this->dispatcher->dispatch($request, $response);
  459. }
  460. /**
  461. * Test dispatcher filters being called and changing the response.
  462. *
  463. * @return void
  464. */
  465. public function testBeforeDispatchAbortDispatch()
  466. {
  467. $response = $this->getMock('Cake\Network\Response', ['send']);
  468. $response->expects($this->once())
  469. ->method('send');
  470. $filter = $this->getMock(
  471. 'Cake\Routing\DispatcherFilter',
  472. ['beforeDispatch', 'afterDispatch']
  473. );
  474. $filter->expects($this->once())
  475. ->method('beforeDispatch')
  476. ->will($this->returnValue($response));
  477. $filter->expects($this->never())
  478. ->method('afterDispatch');
  479. $request = new Request();
  480. $res = new Response();
  481. $this->dispatcher->addFilter($filter);
  482. $this->dispatcher->dispatch($request, $res);
  483. }
  484. /**
  485. * Test dispatcher filters being called and changing the response.
  486. *
  487. * @return void
  488. */
  489. public function testAfterDispatchReplaceResponse()
  490. {
  491. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'send']);
  492. $response->expects($this->once())
  493. ->method('send');
  494. $filter = $this->getMock(
  495. 'Cake\Routing\DispatcherFilter',
  496. ['beforeDispatch', 'afterDispatch']
  497. );
  498. $filter->expects($this->once())
  499. ->method('afterDispatch')
  500. ->will($this->returnValue($response));
  501. $request = new Request([
  502. 'url' => '/posts',
  503. 'params' => [
  504. 'plugin' => null,
  505. 'controller' => 'Posts',
  506. 'action' => 'index',
  507. 'pass' => [],
  508. ],
  509. 'session' => new Session
  510. ]);
  511. $this->dispatcher->addFilter($filter);
  512. $this->dispatcher->dispatch($request, $response);
  513. }
  514. }