IntegrationTestCaseTest.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\TestSuite;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\Event\EventManager;
  19. use Cake\Http\Response;
  20. use Cake\Routing\DispatcherFactory;
  21. use Cake\Routing\RouteBuilder;
  22. use Cake\Routing\Router;
  23. use Cake\Routing\Route\InflectedRoute;
  24. use Cake\TestSuite\IntegrationTestCase;
  25. use Cake\Test\Fixture\AssertIntegrationTestCase;
  26. use Cake\Utility\Security;
  27. use Zend\Diactoros\UploadedFile;
  28. /**
  29. * Self test of the IntegrationTestCase
  30. */
  31. class IntegrationTestCaseTest extends IntegrationTestCase
  32. {
  33. /**
  34. * Setup method
  35. *
  36. * @return void
  37. */
  38. public function setUp()
  39. {
  40. parent::setUp();
  41. static::setAppNamespace();
  42. Router::reload();
  43. Router::extensions(['json']);
  44. Router::scope('/', function (RouteBuilder $routes) {
  45. $routes->setRouteClass(InflectedRoute::class);
  46. $routes->get('/get/:controller/:action', []);
  47. $routes->head('/head/:controller/:action', []);
  48. $routes->options('/options/:controller/:action', []);
  49. $routes->connect('/:controller/:action/*', []);
  50. });
  51. Router::$initialized = true;
  52. $this->useHttpServer(true);
  53. $this->configApplication(Configure::read('App.namespace') . '\Application', null);
  54. DispatcherFactory::clear();
  55. }
  56. /**
  57. * Helper for enabling the legacy stack for backwards compatibility testing.
  58. *
  59. * @return void
  60. */
  61. protected function useLegacyDispatcher()
  62. {
  63. DispatcherFactory::add('Routing');
  64. DispatcherFactory::add('ControllerFactory');
  65. $this->useHttpServer(false);
  66. }
  67. /**
  68. * Tests that all data that used by the request is cast to strings
  69. *
  70. * @return void
  71. */
  72. public function testDataCastToString()
  73. {
  74. $data = [
  75. 'title' => 'Blog Post',
  76. 'status' => 1,
  77. 'published' => true,
  78. 'not_published' => false,
  79. 'comments' => [
  80. [
  81. 'body' => 'Comment',
  82. 'status' => 1,
  83. ]
  84. ],
  85. 'file' => [
  86. 'tmp_name' => __FILE__,
  87. 'size' => 42,
  88. 'error' => 0,
  89. 'type' => 'text/plain',
  90. 'name' => 'Uploaded file'
  91. ],
  92. 'pictures' => [
  93. 'name' => [
  94. ['file' => 'a-file.png'],
  95. ['file' => 'a-moose.png']
  96. ],
  97. 'type' => [
  98. ['file' => 'image/png'],
  99. ['file' => 'image/jpg']
  100. ],
  101. 'tmp_name' => [
  102. ['file' => __FILE__],
  103. ['file' => __FILE__]
  104. ],
  105. 'error' => [
  106. ['file' => 0],
  107. ['file' => 0]
  108. ],
  109. 'size' => [
  110. ['file' => 17188],
  111. ['file' => 2010]
  112. ],
  113. ],
  114. 'upload' => new UploadedFile(__FILE__, 42, 0)
  115. ];
  116. $request = $this->_buildRequest('/posts/add', 'POST', $data);
  117. $this->assertInternalType('string', $request['post']['status']);
  118. $this->assertInternalType('string', $request['post']['published']);
  119. $this->assertSame('0', $request['post']['not_published']);
  120. $this->assertInternalType('string', $request['post']['comments'][0]['status']);
  121. $this->assertInternalType('integer', $request['post']['file']['error']);
  122. $this->assertInternalType('integer', $request['post']['file']['size']);
  123. $this->assertInternalType('integer', $request['post']['pictures']['error'][0]['file']);
  124. $this->assertInternalType('integer', $request['post']['pictures']['error'][1]['file']);
  125. $this->assertInternalType('integer', $request['post']['pictures']['size'][0]['file']);
  126. $this->assertInternalType('integer', $request['post']['pictures']['size'][1]['file']);
  127. $this->assertInstanceOf(UploadedFile::class, $request['post']['upload']);
  128. }
  129. /**
  130. * Test building a request.
  131. *
  132. * @return void
  133. */
  134. public function testRequestBuilding()
  135. {
  136. $this->configRequest([
  137. 'headers' => [
  138. 'X-CSRF-Token' => 'abc123',
  139. 'Content-Type' => 'application/json',
  140. 'Accept' => 'application/json'
  141. ],
  142. 'base' => '',
  143. 'webroot' => '/',
  144. 'environment' => [
  145. 'PHP_AUTH_USER' => 'foo',
  146. 'PHP_AUTH_PW' => 'bar'
  147. ]
  148. ]);
  149. $this->cookie('split_token', 'def345');
  150. $this->session(['User' => ['id' => 1, 'username' => 'mark']]);
  151. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  152. $this->assertEquals('abc123', $request['environment']['HTTP_X_CSRF_TOKEN']);
  153. $this->assertEquals('application/json', $request['environment']['CONTENT_TYPE']);
  154. $this->assertEquals('/tasks/add', $request['url']);
  155. $this->assertArrayHasKey('split_token', $request['cookies']);
  156. $this->assertEquals('def345', $request['cookies']['split_token']);
  157. $this->assertEquals(['id' => '1', 'username' => 'mark'], $request['session']->read('User'));
  158. $this->assertEquals('foo', $request['environment']['PHP_AUTH_USER']);
  159. $this->assertEquals('bar', $request['environment']['PHP_AUTH_PW']);
  160. }
  161. /**
  162. * Test request building adds csrf tokens
  163. *
  164. * @return void
  165. */
  166. public function testRequestBuildingCsrfTokens()
  167. {
  168. $this->enableCsrfToken();
  169. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  170. $this->assertArrayHasKey('csrfToken', $request['cookies']);
  171. $this->assertArrayHasKey('_csrfToken', $request['post']);
  172. $this->assertSame($request['cookies']['csrfToken'], $request['post']['_csrfToken']);
  173. $this->cookie('csrfToken', '');
  174. $request = $this->_buildRequest('/tasks/add', 'POST', [
  175. '_csrfToken' => 'fale',
  176. 'title' => 'First post'
  177. ]);
  178. $this->assertSame('', $request['cookies']['csrfToken']);
  179. $this->assertSame('fale', $request['post']['_csrfToken']);
  180. }
  181. /**
  182. * Test multiple actions using CSRF tokens don't fail
  183. *
  184. * @return void
  185. */
  186. public function testEnableCsrfMultipleRequests()
  187. {
  188. $this->enableCsrfToken();
  189. $first = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  190. $second = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'Second post']);
  191. $this->assertSame(
  192. $first['cookies']['csrfToken'],
  193. $second['post']['_csrfToken'],
  194. 'Csrf token should match cookie'
  195. );
  196. $this->assertSame(
  197. $first['post']['_csrfToken'],
  198. $second['post']['_csrfToken'],
  199. 'Tokens should be consistent per test method'
  200. );
  201. }
  202. /**
  203. * Test pre-determined CSRF tokens.
  204. *
  205. * @return void
  206. */
  207. public function testEnableCsrfPredeterminedCookie()
  208. {
  209. $this->enableCsrfToken();
  210. $value = 'I am a teapot';
  211. $this->cookie('csrfToken', $value);
  212. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  213. $this->assertSame($value, $request['cookies']['csrfToken'], 'Csrf token should match cookie');
  214. $this->assertSame($value, $request['post']['_csrfToken'], 'Tokens should match');
  215. }
  216. /**
  217. * Test building a request, with query parameters
  218. *
  219. * @return void
  220. */
  221. public function testRequestBuildingQueryParameters()
  222. {
  223. $request = $this->_buildRequest('/tasks/view?archived=yes', 'GET', []);
  224. $this->assertSame('/tasks/view', $request['url']);
  225. $this->assertSame('archived=yes', $request['environment']['QUERY_STRING']);
  226. $this->assertSame('/tasks/view', $request['environment']['REQUEST_URI']);
  227. }
  228. /**
  229. * Test cookie encrypted
  230. *
  231. * @see CookieComponentControllerTest
  232. */
  233. public function testCookieEncrypted()
  234. {
  235. Security::setSalt('abcdabcdabcdabcdabcdabcdabcdabcdabcd');
  236. $this->cookieEncrypted('KeyOfCookie', 'Encrypted with aes by default');
  237. $request = $this->_buildRequest('/tasks/view', 'GET', []);
  238. $this->assertStringStartsWith('Q2FrZQ==.', $request['cookies']['KeyOfCookie']);
  239. }
  240. /**
  241. * Test sending get requests.
  242. *
  243. * @group deprecated
  244. * @return void
  245. */
  246. public function testGetLegacy()
  247. {
  248. $this->useLegacyDispatcher();
  249. $this->deprecated(function () {
  250. $this->assertNull($this->_response);
  251. $this->get('/request_action/test_request_action');
  252. $this->assertNotEmpty($this->_response);
  253. $this->assertInstanceOf('Cake\Http\Response', $this->_response);
  254. $this->assertEquals('This is a test', $this->_response->getBody());
  255. $this->_response = null;
  256. $this->get('/get/request_action/test_request_action');
  257. $this->assertEquals('This is a test', $this->_response->getBody());
  258. });
  259. }
  260. /**
  261. * Test sending get request and using default `test_app/config/routes.php`.
  262. *
  263. * @return void
  264. */
  265. public function testGetUsingApplicationWithPluginRoutes()
  266. {
  267. // first clean routes to have Router::$initailized === false
  268. Router::reload();
  269. Plugin::unload();
  270. $this->configApplication(Configure::read('App.namespace') . '\ApplicationWithPluginRoutes', null);
  271. $this->get('/test_plugin');
  272. $this->assertResponseOk();
  273. }
  274. /**
  275. * Test sending get request and using default `test_app/config/routes.php`.
  276. *
  277. * @return void
  278. */
  279. public function testGetUsingApplicationWithDefaultRoutes()
  280. {
  281. // first clean routes to have Router::$initailized === false
  282. Router::reload();
  283. $this->configApplication(Configure::read('App.namespace') . '\ApplicationWithDefaultRoutes', null);
  284. $this->get('/some_alias');
  285. $this->assertResponseOk();
  286. $this->assertEquals('5', $this->_getBodyAsString());
  287. }
  288. /**
  289. * Test sending head requests.
  290. *
  291. * @return void
  292. */
  293. public function testHead()
  294. {
  295. $this->assertNull($this->_response);
  296. $this->head('/request_action/test_request_action');
  297. $this->assertNotEmpty($this->_response);
  298. $this->assertInstanceOf('Cake\Http\Response', $this->_response);
  299. $this->assertResponseSuccess();
  300. $this->_response = null;
  301. $this->head('/head/request_action/test_request_action');
  302. $this->assertResponseSuccess();
  303. }
  304. /**
  305. * Test sending options requests.
  306. *
  307. * @return void
  308. */
  309. public function testOptions()
  310. {
  311. $this->assertNull($this->_response);
  312. $this->options('/request_action/test_request_action');
  313. $this->assertNotEmpty($this->_response);
  314. $this->assertInstanceOf('Cake\Http\Response', $this->_response);
  315. $this->assertResponseSuccess();
  316. $this->_response = null;
  317. $this->options('/options/request_action/test_request_action');
  318. $this->assertResponseSuccess();
  319. }
  320. /**
  321. * Test sending get requests sets the request method
  322. *
  323. * @return void
  324. */
  325. public function testGetSpecificRouteLegacy()
  326. {
  327. $this->useLegacyDispatcher();
  328. $this->deprecated(function () {
  329. $this->get('/get/request_action/test_request_action');
  330. $this->assertResponseOk();
  331. $this->assertEquals('This is a test', $this->_response->getBody());
  332. });
  333. }
  334. /**
  335. * Test sending get requests sets the request method
  336. *
  337. * @return void
  338. */
  339. public function testGetSpecificRouteHttpServer()
  340. {
  341. $this->get('/get/request_action/test_request_action');
  342. $this->assertResponseOk();
  343. $this->assertEquals('This is a test', $this->_response->getBody());
  344. }
  345. /**
  346. * Test customizing the app class.
  347. *
  348. * @return void
  349. */
  350. public function testConfigApplication()
  351. {
  352. $this->expectException(\LogicException::class);
  353. $this->expectExceptionMessage('Cannot load "TestApp\MissingApp" for use in integration');
  354. $this->configApplication('TestApp\MissingApp', []);
  355. $this->get('/request_action/test_request_action');
  356. }
  357. /**
  358. * Test sending get requests with Http\Server
  359. *
  360. * @return void
  361. */
  362. public function testGetHttpServer()
  363. {
  364. $this->assertNull($this->_response);
  365. $this->get('/request_action/test_request_action');
  366. $this->assertNotEmpty($this->_response);
  367. $this->assertInstanceOf('Cake\Http\Response', $this->_response);
  368. $this->assertEquals('This is a test', $this->_response->getBody());
  369. $this->assertHeader('X-Middleware', 'true');
  370. }
  371. /**
  372. * Test that the PSR7 requests get query string data
  373. *
  374. * @return void
  375. */
  376. public function testGetQueryStringHttpServer()
  377. {
  378. $this->configRequest(['headers' => ['Content-Type' => 'text/plain']]);
  379. $this->get('/request_action/params_pass?q=query');
  380. $this->assertResponseOk();
  381. $this->assertResponseContains('"q":"query"');
  382. $this->assertResponseContains('"contentType":"text\/plain"');
  383. $this->assertHeader('X-Middleware', 'true');
  384. $request = $this->_controller->request;
  385. $this->assertContains('/request_action/params_pass?q=query', $request->getRequestTarget());
  386. }
  387. /**
  388. * Test that the PSR7 requests get query string data
  389. *
  390. * @group deprecated
  391. * @return void
  392. */
  393. public function testGetQueryStringSetsHere()
  394. {
  395. $this->deprecated(function () {
  396. $this->configRequest(['headers' => ['Content-Type' => 'text/plain']]);
  397. $this->get('/request_action/params_pass?q=query');
  398. $this->assertResponseOk();
  399. $this->assertResponseContains('"q":"query"');
  400. $this->assertResponseContains('"contentType":"text\/plain"');
  401. $this->assertHeader('X-Middleware', 'true');
  402. $request = $this->_controller->request;
  403. $this->assertContains('/request_action/params_pass?q=query', $request->here());
  404. $this->assertContains('/request_action/params_pass?q=query', $request->getRequestTarget());
  405. });
  406. }
  407. /**
  408. * Test that the PSR7 requests get cookies
  409. *
  410. * @return void
  411. */
  412. public function testGetCookiesHttpServer()
  413. {
  414. $this->configRequest(['cookies' => ['split_test' => 'abc']]);
  415. $this->get('/request_action/cookie_pass');
  416. $this->assertResponseOk();
  417. $this->assertResponseContains('"split_test":"abc"');
  418. $this->assertHeader('X-Middleware', 'true');
  419. }
  420. /**
  421. * Test that the PSR7 requests receive post data
  422. *
  423. * @return void
  424. */
  425. public function testPostDataLegacyDispatcher()
  426. {
  427. $this->useLegacyDispatcher();
  428. $this->deprecated(function () {
  429. $this->post('/request_action/post_pass', ['title' => 'value']);
  430. $data = json_decode($this->_response->getBody());
  431. $this->assertEquals('value', $data->title);
  432. });
  433. }
  434. /**
  435. * Test that the PSR7 requests receive post data
  436. *
  437. * @return void
  438. */
  439. public function testPostDataHttpServer()
  440. {
  441. $this->post('/request_action/post_pass', ['title' => 'value']);
  442. $data = json_decode($this->_response->getBody());
  443. $this->assertEquals('value', $data->title);
  444. $this->assertHeader('X-Middleware', 'true');
  445. }
  446. /**
  447. * Test that the PSR7 requests receive encoded data.
  448. *
  449. * @return void
  450. */
  451. public function testInputDataHttpServer()
  452. {
  453. $this->post('/request_action/input_test', '{"hello":"world"}');
  454. if ($this->_response->getBody()->isSeekable()) {
  455. $this->_response->getBody()->rewind();
  456. }
  457. $this->assertSame('world', $this->_response->getBody()->getContents());
  458. $this->assertHeader('X-Middleware', 'true');
  459. }
  460. /**
  461. * Test that the PSR7 requests receive encoded data.
  462. *
  463. * @return void
  464. */
  465. public function testInputDataSecurityToken()
  466. {
  467. $this->enableSecurityToken();
  468. $this->post('/request_action/input_test', '{"hello":"world"}');
  469. $this->assertSame('world', '' . $this->_response->getBody());
  470. $this->assertHeader('X-Middleware', 'true');
  471. }
  472. /**
  473. * Test that the PSR7 requests get cookies
  474. *
  475. * @return void
  476. */
  477. public function testSessionHttpServer()
  478. {
  479. $this->session(['foo' => 'session data']);
  480. $this->get('/request_action/session_test');
  481. $this->assertResponseOk();
  482. $this->assertResponseContains('session data');
  483. $this->assertHeader('X-Middleware', 'true');
  484. }
  485. /**
  486. * Test sending requests stores references to controller/view/layout.
  487. *
  488. * @return void
  489. */
  490. public function testRequestSetsProperties()
  491. {
  492. $this->post('/posts/index');
  493. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  494. $this->assertNotEmpty($this->_viewName, 'View name not set');
  495. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  496. $this->assertNotEmpty($this->_layoutName, 'Layout name not set');
  497. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  498. $this->assertTemplate('index');
  499. $this->assertLayout('default');
  500. $this->assertEquals('value', $this->viewVariable('test'));
  501. }
  502. /**
  503. * Test PSR7 requests store references to controller/view/layout
  504. *
  505. * @return void
  506. */
  507. public function testRequestSetsPropertiesHttpServer()
  508. {
  509. $this->post('/posts/index');
  510. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  511. $this->assertNotEmpty($this->_viewName, 'View name not set');
  512. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  513. $this->assertNotEmpty($this->_layoutName, 'Layout name not set');
  514. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  515. $this->assertTemplate('index');
  516. $this->assertLayout('default');
  517. $this->assertEquals('value', $this->viewVariable('test'));
  518. }
  519. /**
  520. * Tests URLs containing extensions.
  521. *
  522. * @return void
  523. */
  524. public function testRequestWithExt()
  525. {
  526. $this->get(['controller' => 'Posts', 'action' => 'ajax', '_ext' => 'json']);
  527. $this->assertResponseCode(200);
  528. }
  529. /**
  530. * Assert that the stored template doesn't change when cells are rendered.
  531. *
  532. * @return void
  533. */
  534. public function testAssertTemplateAfterCellRender()
  535. {
  536. $this->get('/posts/get');
  537. $this->assertContains('Template' . DS . 'Posts' . DS . 'get.ctp', $this->_viewName);
  538. $this->assertTemplate('get');
  539. $this->assertResponseContains('cellcontent');
  540. }
  541. /**
  542. * Test array URLs
  543. *
  544. * @return void
  545. */
  546. public function testArrayUrls()
  547. {
  548. $this->post(['controller' => 'Posts', 'action' => 'index']);
  549. $this->assertEquals('value', $this->viewVariable('test'));
  550. }
  551. /**
  552. * Test array URLs with an empty router.
  553. *
  554. * @return void
  555. */
  556. public function testArrayUrlsEmptyRouter()
  557. {
  558. Router::reload();
  559. $this->assertFalse(Router::$initialized);
  560. $this->post(['controller' => 'Posts', 'action' => 'index']);
  561. $this->assertEquals('value', $this->viewVariable('test'));
  562. }
  563. /**
  564. * Test flash and cookie assertions
  565. *
  566. * @return void
  567. */
  568. public function testFlashSessionAndCookieAsserts()
  569. {
  570. $this->post('/posts/index');
  571. $this->assertSession('An error message', 'Flash.flash.0.message');
  572. $this->assertCookie(1, 'remember_me');
  573. $this->assertCookieNotSet('user_id');
  574. }
  575. /**
  576. * Test flash and cookie assertions
  577. *
  578. * @return void
  579. */
  580. public function testFlashSessionAndCookieAssertsHttpServer()
  581. {
  582. $this->post('/posts/index');
  583. $this->assertSession('An error message', 'Flash.flash.0.message');
  584. $this->assertCookieNotSet('user_id');
  585. $this->assertCookie(1, 'remember_me');
  586. }
  587. /**
  588. * Test flash assertions stored with enableRememberFlashMessages() after they
  589. * are rendered
  590. *
  591. * @return void
  592. */
  593. public function testFlashAssertionsAfterRender()
  594. {
  595. $this->enableRetainFlashMessages();
  596. $this->get('/posts/index/with_flash');
  597. $this->assertSession('An error message', 'Flash.flash.0.message');
  598. }
  599. /**
  600. * Test flash assertions stored with enableRememberFlashMessages() even if
  601. * no view is rendered
  602. *
  603. * @return void
  604. */
  605. public function testFlashAssertionsWithNoRender()
  606. {
  607. $this->enableRetainFlashMessages();
  608. $this->get('/posts/flashNoRender');
  609. $this->assertRedirect();
  610. $this->assertSession('An error message', 'Flash.flash.0.message');
  611. }
  612. /**
  613. * Tests the failure message for assertCookieNotSet
  614. *
  615. * @return void
  616. */
  617. public function testCookieNotSetFailure()
  618. {
  619. $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
  620. $this->expectExceptionMessage('Cookie \'remember_me\' has been set');
  621. $this->post('/posts/index');
  622. $this->assertCookieNotSet('remember_me');
  623. }
  624. /**
  625. * Tests the failure message for assertCookieNotSet when no
  626. * response whas generated
  627. *
  628. * @return void
  629. */
  630. public function testCookieNotSetFailureNoResponse()
  631. {
  632. $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
  633. $this->expectExceptionMessage('No response set, cannot assert cookies.');
  634. $this->assertCookieNotSet('remember_me');
  635. }
  636. /**
  637. * Test error handling and error page rendering.
  638. *
  639. * @return void
  640. */
  641. public function testPostAndErrorHandling()
  642. {
  643. $this->post('/request_action/error_method');
  644. $this->assertResponseNotEmpty();
  645. $this->assertResponseContains('Not there or here');
  646. $this->assertResponseContains('<!DOCTYPE html>');
  647. }
  648. /**
  649. * Test posting to a secured form action.
  650. *
  651. * @return void
  652. */
  653. public function testPostSecuredForm()
  654. {
  655. $this->enableSecurityToken();
  656. $data = [
  657. 'title' => 'Some title',
  658. 'body' => 'Some text'
  659. ];
  660. $this->post('/posts/securePost', $data);
  661. $this->assertResponseOk();
  662. $this->assertResponseContains('Request was accepted');
  663. }
  664. /**
  665. * Test posting to a secured form action with nested data.
  666. *
  667. * @return void
  668. */
  669. public function testPostSecuredFormNestedData()
  670. {
  671. $this->enableSecurityToken();
  672. $data = [
  673. 'title' => 'New post',
  674. 'comments' => [
  675. ['comment' => 'A new comment']
  676. ],
  677. 'tags' => ['_ids' => [1, 2, 3, 4]]
  678. ];
  679. $this->post('/posts/securePost', $data);
  680. $this->assertResponseOk();
  681. $this->assertResponseContains('Request was accepted');
  682. }
  683. /**
  684. * Test posting to a secured form action.
  685. *
  686. * @return void
  687. */
  688. public function testPostSecuredFormWithQuery()
  689. {
  690. $this->enableSecurityToken();
  691. $data = [
  692. 'title' => 'Some title',
  693. 'body' => 'Some text'
  694. ];
  695. $this->post('/posts/securePost?foo=bar', $data);
  696. $this->assertResponseOk();
  697. $this->assertResponseContains('Request was accepted');
  698. }
  699. /**
  700. * Test posting to a secured form action with a query that has a part that
  701. * will be encoded by the security component
  702. *
  703. * @return void
  704. */
  705. public function testPostSecuredFormWithUnencodedQuery()
  706. {
  707. $this->enableSecurityToken();
  708. $data = [
  709. 'title' => 'Some title',
  710. 'body' => 'Some text'
  711. ];
  712. $this->post('/posts/securePost?foo=/', $data);
  713. $this->assertResponseOk();
  714. $this->assertResponseContains('Request was accepted');
  715. }
  716. /**
  717. * Test posting to a secured form action action.
  718. *
  719. * @return void
  720. */
  721. public function testPostSecuredFormFailure()
  722. {
  723. $data = [
  724. 'title' => 'Some title',
  725. 'body' => 'Some text'
  726. ];
  727. $this->post('/posts/securePost', $data);
  728. $this->assertResponseError();
  729. }
  730. /**
  731. * Test that exceptions being thrown are handled correctly.
  732. *
  733. * @return void
  734. */
  735. public function testWithExpectedException()
  736. {
  737. $this->get('/tests_apps/throw_exception');
  738. $this->assertResponseCode(500);
  739. }
  740. /**
  741. * Test that exceptions being thrown are handled correctly by the psr7 stack.
  742. *
  743. * @return void
  744. */
  745. public function testWithExpectedExceptionHttpServer()
  746. {
  747. DispatcherFactory::clear();
  748. $this->get('/tests_apps/throw_exception');
  749. $this->assertResponseCode(500);
  750. }
  751. /**
  752. * Test that exceptions being thrown are handled correctly.
  753. *
  754. * @return void
  755. */
  756. public function testWithUnexpectedException()
  757. {
  758. $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
  759. $this->get('/tests_apps/throw_exception');
  760. $this->assertResponseCode(501);
  761. }
  762. /**
  763. * Test redirecting and integration tests.
  764. *
  765. * @return void
  766. */
  767. public function testRedirect()
  768. {
  769. $this->post('/tests_apps/redirect_to');
  770. $this->assertResponseSuccess();
  771. $this->assertResponseCode(302);
  772. }
  773. /**
  774. * Test redirecting and psr7 stack
  775. *
  776. * @return void
  777. */
  778. public function testRedirectHttpServer()
  779. {
  780. DispatcherFactory::clear();
  781. $this->post('/tests_apps/redirect_to');
  782. $this->assertResponseCode(302);
  783. $this->assertHeader('X-Middleware', 'true');
  784. }
  785. /**
  786. * Test redirecting and integration tests.
  787. *
  788. * @return void
  789. */
  790. public function testRedirectPermanent()
  791. {
  792. $this->post('/tests_apps/redirect_to_permanent');
  793. $this->assertResponseSuccess();
  794. $this->assertResponseCode(301);
  795. }
  796. /**
  797. * Test the responseOk status assertion
  798. *
  799. * @return void
  800. */
  801. public function testAssertResponseStatusCodes()
  802. {
  803. $this->_response = new Response();
  804. $this->_response = $this->_response->withStatus(200);
  805. $this->assertResponseOk();
  806. $this->_response = $this->_response->withStatus(201);
  807. $this->assertResponseOk();
  808. $this->_response = $this->_response->withStatus(204);
  809. $this->assertResponseOk();
  810. $this->_response = $this->_response->withStatus(202);
  811. $this->assertResponseSuccess();
  812. $this->_response = $this->_response->withStatus(302);
  813. $this->assertResponseSuccess();
  814. $this->_response = $this->_response->withStatus(400);
  815. $this->assertResponseError();
  816. $this->_response = $this->_response->withStatus(417);
  817. $this->assertResponseError();
  818. $this->_response = $this->_response->withStatus(500);
  819. $this->assertResponseFailure();
  820. $this->_response = $this->_response->withStatus(505);
  821. $this->assertResponseFailure();
  822. $this->_response = $this->_response->withStatus(301);
  823. $this->assertResponseCode(301);
  824. }
  825. /**
  826. * Test the location header assertion.
  827. *
  828. * @return void
  829. */
  830. public function testAssertRedirect()
  831. {
  832. $this->_response = new Response();
  833. $this->_response = $this->_response->withHeader('Location', 'http://localhost/tasks/index');
  834. $this->assertRedirect();
  835. $this->assertRedirect('/tasks/index');
  836. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  837. $this->assertResponseEmpty();
  838. }
  839. /**
  840. * Test the location header assertion.
  841. *
  842. * @return void
  843. */
  844. public function testAssertNoRedirect()
  845. {
  846. $this->_response = new Response();
  847. $this->assertNoRedirect();
  848. }
  849. /**
  850. * Test the location header assertion.
  851. *
  852. * @return void
  853. */
  854. public function testAssertNoRedirectFail()
  855. {
  856. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  857. $result = $test->run();
  858. $this->assertFalse($result->wasSuccessful());
  859. $this->assertEquals(1, $result->failureCount());
  860. }
  861. /**
  862. * Test the location header assertion string contains
  863. *
  864. * @return void
  865. */
  866. public function testAssertRedirectContains()
  867. {
  868. $this->_response = new Response();
  869. $this->_response = $this->_response->withHeader('Location', 'http://localhost/tasks/index');
  870. $this->assertRedirectContains('/tasks/index');
  871. }
  872. /**
  873. * Test the header assertion.
  874. *
  875. * @return void
  876. */
  877. public function testAssertHeader()
  878. {
  879. $this->_response = new Response();
  880. $this->_response = $this->_response->withHeader('Etag', 'abc123');
  881. $this->assertHeader('Etag', 'abc123');
  882. }
  883. /**
  884. * Test the header contains assertion.
  885. *
  886. * @return void
  887. */
  888. public function testAssertHeaderContains()
  889. {
  890. $this->_response = new Response();
  891. $this->_response = $this->_response->withHeader('Etag', 'abc123');
  892. $this->assertHeaderContains('Etag', 'abc');
  893. }
  894. /**
  895. * Test the content type assertion.
  896. *
  897. * @return void
  898. */
  899. public function testAssertContentType()
  900. {
  901. $this->_response = new Response();
  902. $this->_response = $this->_response->withType('json');
  903. $this->assertContentType('json');
  904. $this->assertContentType('application/json');
  905. }
  906. /**
  907. * Test that type() in an action sets the content-type header.
  908. *
  909. * @return void
  910. */
  911. public function testContentTypeInAction()
  912. {
  913. $this->get('/tests_apps/set_type');
  914. $this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
  915. $this->assertContentType('json');
  916. $this->assertContentType('application/json');
  917. }
  918. /**
  919. * Test the content assertion.
  920. *
  921. * @return void
  922. */
  923. public function testAssertResponseContains()
  924. {
  925. $this->_response = new Response();
  926. $this->_response = $this->_response->withStringBody('Some content');
  927. $this->assertResponseContains('content');
  928. }
  929. /**
  930. * Test the content assertion with no case sensitivity.
  931. *
  932. * @return void
  933. */
  934. public function testAssertResponseContainsWithIgnoreCaseFlag()
  935. {
  936. $this->_response = new Response();
  937. $this->_response = $this->_response->withStringBody('Some content');
  938. $this->assertResponseContains('some', 'Failed asserting that the body contains given content', true);
  939. }
  940. /**
  941. * Test the negated content assertion.
  942. *
  943. * @return void
  944. */
  945. public function testAssertResponseNotContains()
  946. {
  947. $this->_response = new Response();
  948. $this->_response = $this->_response->withStringBody('Some content');
  949. $this->assertResponseNotContains('contents');
  950. }
  951. /**
  952. * Test the content regexp assertion.
  953. *
  954. * @return void
  955. */
  956. public function testAssertResponseRegExp()
  957. {
  958. $this->_response = new Response();
  959. $this->_response = $this->_response->withStringBody('Some content');
  960. $this->assertResponseRegExp('/cont/');
  961. }
  962. /**
  963. * Test the content regexp assertion failing
  964. *
  965. * @return void
  966. */
  967. public function testAssertResponseRegExpNoResponse()
  968. {
  969. $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
  970. $this->expectExceptionMessage('No response set');
  971. $this->assertResponseRegExp('/cont/');
  972. }
  973. /**
  974. * Test the negated content regexp assertion.
  975. *
  976. * @return void
  977. */
  978. public function testAssertResponseNotRegExp()
  979. {
  980. $this->_response = new Response();
  981. $this->_response = $this->_response->withStringBody('Some content');
  982. $this->assertResponseNotRegExp('/cant/');
  983. }
  984. /**
  985. * Test negated content regexp assertion failing
  986. *
  987. * @return void
  988. */
  989. public function testAssertResponseNotRegExpNoResponse()
  990. {
  991. $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
  992. $this->expectExceptionMessage('No response set');
  993. $this->assertResponseNotRegExp('/cont/');
  994. }
  995. /**
  996. * Test that works in tandem with testEventManagerReset2 to
  997. * test the EventManager reset.
  998. *
  999. * The return value is passed to testEventManagerReset2 as
  1000. * an arguments.
  1001. *
  1002. * @return \Cake\Event\EventManager
  1003. */
  1004. public function testEventManagerReset1()
  1005. {
  1006. $eventManager = EventManager::instance();
  1007. $this->assertInstanceOf('Cake\Event\EventManager', $eventManager);
  1008. return $eventManager;
  1009. }
  1010. /**
  1011. * Test if the EventManager is reset between tests.
  1012. *
  1013. * @depends testEventManagerReset1
  1014. * @return void
  1015. */
  1016. public function testEventManagerReset2($prevEventManager)
  1017. {
  1018. $this->assertInstanceOf('Cake\Event\EventManager', $prevEventManager);
  1019. $this->assertNotSame($prevEventManager, EventManager::instance());
  1020. }
  1021. /**
  1022. * Test sending file in requests.
  1023. *
  1024. * @return void
  1025. */
  1026. public function testSendFile()
  1027. {
  1028. $this->get('/posts/file');
  1029. $this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
  1030. }
  1031. /**
  1032. * Test sending file with psr7 stack
  1033. *
  1034. * @return void
  1035. */
  1036. public function testSendFileHttpServer()
  1037. {
  1038. $this->get('/posts/file');
  1039. $this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
  1040. }
  1041. /**
  1042. * Test that assertFile requires a response
  1043. *
  1044. * @return void
  1045. */
  1046. public function testAssertFileNoResponse()
  1047. {
  1048. $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
  1049. $this->expectExceptionMessage('No response set, cannot assert file');
  1050. $this->assertFileResponse('foo');
  1051. }
  1052. /**
  1053. * Test that assertFile requires a file
  1054. *
  1055. * @return void
  1056. */
  1057. public function testAssertFileNoFile()
  1058. {
  1059. $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
  1060. $this->expectExceptionMessage('No file was sent in this response');
  1061. $this->get('/posts/get');
  1062. $this->assertFileResponse('foo');
  1063. }
  1064. /**
  1065. * Test disabling the error handler middleware.
  1066. *
  1067. * @return void
  1068. */
  1069. public function testDisableErrorHandlerMiddleware()
  1070. {
  1071. $this->expectException(\Cake\Routing\Exception\MissingRouteException::class);
  1072. $this->expectExceptionMessage('A route matching "/foo" could not be found.');
  1073. $this->disableErrorHandlerMiddleware();
  1074. $this->get('/foo');
  1075. }
  1076. /**
  1077. * tests getting a secure action while passing a query string
  1078. *
  1079. * @return void
  1080. * @dataProvider methodsProvider
  1081. */
  1082. public function testSecureWithQueryString($method)
  1083. {
  1084. $this->enableSecurityToken();
  1085. $this->{$method}('/posts/securePost/?ids[]=1&ids[]=2');
  1086. $this->assertResponseOk();
  1087. }
  1088. /**
  1089. * data provider for HTTP methods
  1090. *
  1091. * @return array
  1092. */
  1093. public function methodsProvider()
  1094. {
  1095. return [
  1096. 'GET' => ['get'],
  1097. 'POST' => ['post'],
  1098. 'PATCH' => ['patch'],
  1099. 'PUT' => ['put'],
  1100. 'DELETE' => ['delete'],
  1101. ];
  1102. }
  1103. }