IntegrationTestCaseTest.php 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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\Event\EventManager;
  17. use Cake\Http\Response;
  18. use Cake\Routing\DispatcherFactory;
  19. use Cake\Routing\Router;
  20. use Cake\TestSuite\IntegrationTestCase;
  21. use Cake\Test\Fixture\AssertIntegrationTestCase;
  22. use Cake\Utility\Security;
  23. /**
  24. * Self test of the IntegrationTestCase
  25. */
  26. class IntegrationTestCaseTest extends IntegrationTestCase
  27. {
  28. /**
  29. * Setup method
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. static::setAppNamespace();
  37. Router::connect('/get/:controller/:action', ['_method' => 'GET'], ['routeClass' => 'InflectedRoute']);
  38. Router::connect('/head/:controller/:action', ['_method' => 'HEAD'], ['routeClass' => 'InflectedRoute']);
  39. Router::connect('/options/:controller/:action', ['_method' => 'OPTIONS'], ['routeClass' => 'InflectedRoute']);
  40. Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
  41. DispatcherFactory::clear();
  42. DispatcherFactory::add('Routing');
  43. DispatcherFactory::add('ControllerFactory');
  44. $this->useHttpServer(false);
  45. }
  46. /**
  47. * Test building a request.
  48. *
  49. * @return void
  50. */
  51. public function testRequestBuilding()
  52. {
  53. $this->configRequest([
  54. 'headers' => [
  55. 'X-CSRF-Token' => 'abc123',
  56. 'Content-Type' => 'application/json',
  57. 'Accept' => 'application/json'
  58. ],
  59. 'base' => '',
  60. 'webroot' => '/',
  61. 'environment' => [
  62. 'PHP_AUTH_USER' => 'foo',
  63. 'PHP_AUTH_PW' => 'bar'
  64. ]
  65. ]);
  66. $this->cookie('split_token', 'def345');
  67. $this->session(['User' => ['id' => 1, 'username' => 'mark']]);
  68. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  69. $this->assertEquals('abc123', $request['environment']['HTTP_X_CSRF_TOKEN']);
  70. $this->assertEquals('application/json', $request['environment']['CONTENT_TYPE']);
  71. $this->assertEquals('/tasks/add', $request['url']);
  72. $this->assertArrayHasKey('split_token', $request['cookies']);
  73. $this->assertEquals('def345', $request['cookies']['split_token']);
  74. $this->assertEquals(['id' => '1', 'username' => 'mark'], $request['session']->read('User'));
  75. $this->assertEquals('foo', $request['environment']['PHP_AUTH_USER']);
  76. $this->assertEquals('bar', $request['environment']['PHP_AUTH_PW']);
  77. }
  78. /**
  79. * Test request building adds csrf tokens
  80. *
  81. * @return void
  82. */
  83. public function testRequestBuildingCsrfTokens()
  84. {
  85. $this->enableCsrfToken();
  86. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  87. $this->assertArrayHasKey('csrfToken', $request['cookies']);
  88. $this->assertArrayHasKey('_csrfToken', $request['post']);
  89. $this->assertSame($request['cookies']['csrfToken'], $request['post']['_csrfToken']);
  90. $this->cookie('csrfToken', '');
  91. $request = $this->_buildRequest('/tasks/add', 'POST', [
  92. '_csrfToken' => 'fale',
  93. 'title' => 'First post'
  94. ]);
  95. $this->assertSame('', $request['cookies']['csrfToken']);
  96. $this->assertSame('fale', $request['post']['_csrfToken']);
  97. }
  98. /**
  99. * Test multiple actions using CSRF tokens don't fail
  100. *
  101. * @return void
  102. */
  103. public function testEnableCsrfMultipleRequests()
  104. {
  105. $this->enableCsrfToken();
  106. $first = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  107. $second = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'Second post']);
  108. $this->assertSame(
  109. $first['cookies']['csrfToken'],
  110. $second['post']['_csrfToken'],
  111. 'Csrf token should match cookie'
  112. );
  113. $this->assertSame(
  114. $first['post']['_csrfToken'],
  115. $second['post']['_csrfToken'],
  116. 'Tokens should be consistent per test method'
  117. );
  118. }
  119. /**
  120. * Test pre-determined CSRF tokens.
  121. *
  122. * @return void
  123. */
  124. public function testEnableCsrfPredeterminedCookie()
  125. {
  126. $this->enableCsrfToken();
  127. $value = 'I am a teapot';
  128. $this->cookie('csrfToken', $value);
  129. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  130. $this->assertSame($value, $request['cookies']['csrfToken'], 'Csrf token should match cookie');
  131. $this->assertSame($value, $request['post']['_csrfToken'], 'Tokens should match');
  132. }
  133. /**
  134. * Test building a request, with query parameters
  135. *
  136. * @return void
  137. */
  138. public function testRequestBuildingQueryParameters()
  139. {
  140. $request = $this->_buildRequest('/tasks/view?archived=yes', 'GET', []);
  141. $this->assertSame('/tasks/view', $request['url']);
  142. $this->assertSame('archived=yes', $request['environment']['QUERY_STRING']);
  143. $this->assertSame('/tasks/view', $request['environment']['REQUEST_URI']);
  144. }
  145. /**
  146. * Test cookie encrypted
  147. *
  148. * @see CookieComponentControllerTest
  149. */
  150. public function testCookieEncrypted()
  151. {
  152. Security::salt('abcdabcdabcdabcdabcdabcdabcdabcdabcd');
  153. $this->cookieEncrypted('KeyOfCookie', 'Encrypted with aes by default');
  154. $request = $this->_buildRequest('/tasks/view', 'GET', []);
  155. $this->assertStringStartsWith('Q2FrZQ==.', $request['cookies']['KeyOfCookie']);
  156. }
  157. /**
  158. * Test sending get requests.
  159. *
  160. * @return void
  161. */
  162. public function testGet()
  163. {
  164. $this->assertNull($this->_response);
  165. $this->get('/request_action/test_request_action');
  166. $this->assertNotEmpty($this->_response);
  167. $this->assertInstanceOf('Cake\Http\Response', $this->_response);
  168. $this->assertEquals('This is a test', $this->_response->getBody());
  169. $this->_response = null;
  170. $this->get('/get/request_action/test_request_action');
  171. $this->assertEquals('This is a test', $this->_response->getBody());
  172. }
  173. /**
  174. * Test sending head requests.
  175. *
  176. * @return void
  177. */
  178. public function testHead()
  179. {
  180. $this->assertNull($this->_response);
  181. $this->head('/request_action/test_request_action');
  182. $this->assertNotEmpty($this->_response);
  183. $this->assertInstanceOf('Cake\Http\Response', $this->_response);
  184. $this->assertResponseSuccess();
  185. $this->_response = null;
  186. $this->head('/head/request_action/test_request_action');
  187. $this->assertResponseSuccess();
  188. }
  189. /**
  190. * Test sending options requests.
  191. *
  192. * @return void
  193. */
  194. public function testOptions()
  195. {
  196. $this->assertNull($this->_response);
  197. $this->options('/request_action/test_request_action');
  198. $this->assertNotEmpty($this->_response);
  199. $this->assertInstanceOf('Cake\Http\Response', $this->_response);
  200. $this->assertResponseSuccess();
  201. $this->_response = null;
  202. $this->options('/options/request_action/test_request_action');
  203. $this->assertResponseSuccess();
  204. }
  205. /**
  206. * Test sending get requests sets the request method
  207. *
  208. * @return void
  209. */
  210. public function testGetSpecificRouteHttpServer()
  211. {
  212. $this->useHttpServer(true);
  213. $this->get('/get/request_action/test_request_action');
  214. $this->assertResponseOk();
  215. $this->assertEquals('This is a test', $this->_response->getBody());
  216. }
  217. /**
  218. * Test customizing the app class.
  219. *
  220. * @expectedException \LogicException
  221. * @expectedExceptionMessage Cannot load "TestApp\MissingApp" for use in integration
  222. * @return void
  223. */
  224. public function testConfigApplication()
  225. {
  226. DispatcherFactory::clear();
  227. $this->useHttpServer(true);
  228. $this->configApplication('TestApp\MissingApp', []);
  229. $this->get('/request_action/test_request_action');
  230. }
  231. /**
  232. * Test sending get requests with Http\Server
  233. *
  234. * @return void
  235. */
  236. public function testGetHttpServer()
  237. {
  238. DispatcherFactory::clear();
  239. $this->useHttpServer(true);
  240. $this->assertNull($this->_response);
  241. $this->get('/request_action/test_request_action');
  242. $this->assertNotEmpty($this->_response);
  243. $this->assertInstanceOf('Cake\Http\Response', $this->_response);
  244. $this->assertEquals('This is a test', $this->_response->getBody());
  245. $this->assertHeader('X-Middleware', 'true');
  246. }
  247. /**
  248. * Test that the PSR7 requests get query string data
  249. *
  250. * @return void
  251. */
  252. public function testGetQueryStringHttpServer()
  253. {
  254. $this->useHttpServer(true);
  255. $this->configRequest(['headers' => ['Content-Type' => 'text/plain']]);
  256. $this->get('/request_action/params_pass?q=query');
  257. $this->assertResponseOk();
  258. $this->assertResponseContains('"q":"query"');
  259. $this->assertResponseContains('"contentType":"text\/plain"');
  260. $this->assertHeader('X-Middleware', 'true');
  261. $request = $this->_controller->request;
  262. $this->assertContains('/request_action/params_pass?q=query', $request->here());
  263. $this->assertContains('/request_action/params_pass?q=query', $request->getRequestTarget());
  264. }
  265. /**
  266. * Test that the PSR7 requests get cookies
  267. *
  268. * @return void
  269. */
  270. public function testGetCookiesHttpServer()
  271. {
  272. $this->useHttpServer(true);
  273. $this->configRequest(['cookies' => ['split_test' => 'abc']]);
  274. $this->get('/request_action/cookie_pass');
  275. $this->assertResponseOk();
  276. $this->assertResponseContains('"split_test":"abc"');
  277. $this->assertHeader('X-Middleware', 'true');
  278. }
  279. /**
  280. * Test that the PSR7 requests receive post data
  281. *
  282. * @return void
  283. */
  284. public function testPostDataHttpServer()
  285. {
  286. $this->useHttpServer(true);
  287. $this->post('/request_action/post_pass', ['title' => 'value']);
  288. $data = json_decode($this->_response->getBody());
  289. $this->assertEquals('value', $data->title);
  290. $this->assertHeader('X-Middleware', 'true');
  291. }
  292. /**
  293. * Test that the PSR7 requests receive encoded data.
  294. *
  295. * @return void
  296. */
  297. public function testInputDataHttpServer()
  298. {
  299. $this->useHttpServer(true);
  300. $this->post('/request_action/input_test', '{"hello":"world"}');
  301. if ($this->_response->getBody()->isSeekable()) {
  302. $this->_response->getBody()->rewind();
  303. }
  304. $this->assertSame('world', $this->_response->getBody()->getContents());
  305. $this->assertHeader('X-Middleware', 'true');
  306. }
  307. /**
  308. * Test that the PSR7 requests get cookies
  309. *
  310. * @return void
  311. */
  312. public function testSessionHttpServer()
  313. {
  314. $this->useHttpServer(true);
  315. $this->session(['foo' => 'session data']);
  316. $this->get('/request_action/session_test');
  317. $this->assertResponseOk();
  318. $this->assertResponseContains('session data');
  319. $this->assertHeader('X-Middleware', 'true');
  320. }
  321. /**
  322. * Test sending requests stores references to controller/view/layout.
  323. *
  324. * @return void
  325. */
  326. public function testRequestSetsProperties()
  327. {
  328. $this->post('/posts/index');
  329. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  330. $this->assertNotEmpty($this->_viewName, 'View name not set');
  331. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  332. $this->assertNotEmpty($this->_layoutName, 'Layout name not set');
  333. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  334. $this->assertTemplate('index');
  335. $this->assertLayout('default');
  336. $this->assertEquals('value', $this->viewVariable('test'));
  337. }
  338. /**
  339. * Test PSR7 requests store references to controller/view/layout
  340. *
  341. * @return void
  342. */
  343. public function testRequestSetsPropertiesHttpServer()
  344. {
  345. $this->useHttpServer(true);
  346. DispatcherFactory::clear();
  347. $this->post('/posts/index');
  348. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  349. $this->assertNotEmpty($this->_viewName, 'View name not set');
  350. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  351. $this->assertNotEmpty($this->_layoutName, 'Layout name not set');
  352. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  353. $this->assertTemplate('index');
  354. $this->assertLayout('default');
  355. $this->assertEquals('value', $this->viewVariable('test'));
  356. }
  357. /**
  358. * Assert that the stored template doesn't change when cells are rendered.
  359. *
  360. * @return void
  361. */
  362. public function testAssertTemplateAfterCellRender()
  363. {
  364. $this->get('/posts/get');
  365. $this->assertContains('Template' . DS . 'Posts' . DS . 'get.ctp', $this->_viewName);
  366. $this->assertTemplate('get');
  367. $this->assertResponseContains('cellcontent');
  368. }
  369. /**
  370. * Test array URLs
  371. *
  372. * @return void
  373. */
  374. public function testArrayUrls()
  375. {
  376. $this->post(['controller' => 'Posts', 'action' => 'index']);
  377. $this->assertEquals('value', $this->viewVariable('test'));
  378. }
  379. /**
  380. * Test flash and cookie assertions
  381. *
  382. * @return void
  383. */
  384. public function testFlashSessionAndCookieAsserts()
  385. {
  386. $this->post('/posts/index');
  387. $this->assertSession('An error message', 'Flash.flash.0.message');
  388. $this->assertCookie(1, 'remember_me');
  389. $this->assertCookieNotSet('user_id');
  390. }
  391. /**
  392. * Test flash and cookie assertions
  393. *
  394. * @return void
  395. */
  396. public function testFlashSessionAndCookieAssertsHttpServer()
  397. {
  398. $this->useHttpServer(true);
  399. $this->post('/posts/index');
  400. $this->assertSession('An error message', 'Flash.flash.0.message');
  401. $this->assertCookieNotSet('user_id');
  402. $this->assertCookie(1, 'remember_me');
  403. }
  404. /**
  405. * Test flash assertions stored with enableRememberFlashMessages() after they
  406. * are rendered
  407. *
  408. * @return void
  409. */
  410. public function testFlashAssertionsAfterRender()
  411. {
  412. $this->enableRetainFlashMessages();
  413. $this->get('/posts/index/with_flash');
  414. $this->assertSession('An error message', 'Flash.flash.0.message');
  415. }
  416. /**
  417. * Test flash assertions stored with enableRememberFlashMessages() even if
  418. * no view is rendered
  419. *
  420. * @return void
  421. */
  422. public function testFlashAssertionsWithNoRender()
  423. {
  424. $this->enableRetainFlashMessages();
  425. $this->get('/posts/flashNoRender');
  426. $this->assertRedirect();
  427. $this->assertSession('An error message', 'Flash.flash.0.message');
  428. }
  429. /**
  430. * Tests the failure message for assertCookieNotSet
  431. *
  432. * @expectedException \PHPUnit\Framework\AssertionFailedError
  433. * @expectedExceptionMessage Cookie 'remember_me' has been set
  434. * @return void
  435. */
  436. public function testCookieNotSetFailure()
  437. {
  438. $this->post('/posts/index');
  439. $this->assertCookieNotSet('remember_me');
  440. }
  441. /**
  442. * Tests the failure message for assertCookieNotSet when no
  443. * response whas generated
  444. *
  445. * @expectedException \PHPUnit\Framework\AssertionFailedError
  446. * @expectedExceptionMessage No response set, cannot assert cookies.
  447. * @return void
  448. */
  449. public function testCookieNotSetFailureNoResponse()
  450. {
  451. $this->assertCookieNotSet('remember_me');
  452. }
  453. /**
  454. * Test error handling and error page rendering.
  455. *
  456. * @return void
  457. */
  458. public function testPostAndErrorHandling()
  459. {
  460. $this->post('/request_action/error_method');
  461. $this->assertResponseNotEmpty();
  462. $this->assertResponseContains('Not there or here');
  463. $this->assertResponseContains('<!DOCTYPE html>');
  464. }
  465. /**
  466. * Test posting to a secured form action.
  467. *
  468. * @return void
  469. */
  470. public function testPostSecuredForm()
  471. {
  472. $this->enableSecurityToken();
  473. $data = [
  474. 'title' => 'Some title',
  475. 'body' => 'Some text'
  476. ];
  477. $this->post('/posts/securePost', $data);
  478. $this->assertResponseOk();
  479. $this->assertResponseContains('Request was accepted');
  480. }
  481. /**
  482. * Test posting to a secured form action with nested data.
  483. *
  484. * @return void
  485. */
  486. public function testPostSecuredFormNestedData()
  487. {
  488. $this->enableSecurityToken();
  489. $data = [
  490. 'title' => 'New post',
  491. 'comments' => [
  492. ['comment' => 'A new comment']
  493. ],
  494. 'tags' => ['_ids' => [1, 2, 3, 4]]
  495. ];
  496. $this->post('/posts/securePost', $data);
  497. $this->assertResponseOk();
  498. $this->assertResponseContains('Request was accepted');
  499. }
  500. /**
  501. * Test posting to a secured form action.
  502. *
  503. * @return void
  504. */
  505. public function testPostSecuredFormWithQuery()
  506. {
  507. $this->enableSecurityToken();
  508. $data = [
  509. 'title' => 'Some title',
  510. 'body' => 'Some text'
  511. ];
  512. $this->post('/posts/securePost?foo=bar', $data);
  513. $this->assertResponseOk();
  514. $this->assertResponseContains('Request was accepted');
  515. }
  516. /**
  517. * Test posting to a secured form action with a query that has a part that
  518. * will be encoded by the security component
  519. *
  520. * @return void
  521. */
  522. public function testPostSecuredFormWithUnencodedQuery()
  523. {
  524. $this->enableSecurityToken();
  525. $data = [
  526. 'title' => 'Some title',
  527. 'body' => 'Some text'
  528. ];
  529. $this->post('/posts/securePost?foo=/', $data);
  530. $this->assertResponseOk();
  531. $this->assertResponseContains('Request was accepted');
  532. }
  533. /**
  534. * Test posting to a secured form action action.
  535. *
  536. * @return void
  537. */
  538. public function testPostSecuredFormFailure()
  539. {
  540. $data = [
  541. 'title' => 'Some title',
  542. 'body' => 'Some text'
  543. ];
  544. $this->post('/posts/securePost', $data);
  545. $this->assertResponseError();
  546. }
  547. /**
  548. * Test that exceptions being thrown are handled correctly.
  549. *
  550. * @return void
  551. */
  552. public function testWithExpectedException()
  553. {
  554. $this->get('/tests_apps/throw_exception');
  555. $this->assertResponseCode(500);
  556. }
  557. /**
  558. * Test that exceptions being thrown are handled correctly by the psr7 stack.
  559. *
  560. * @return void
  561. */
  562. public function testWithExpectedExceptionHttpServer()
  563. {
  564. DispatcherFactory::clear();
  565. $this->useHttpServer(true);
  566. $this->get('/tests_apps/throw_exception');
  567. $this->assertResponseCode(500);
  568. }
  569. /**
  570. * Test that exceptions being thrown are handled correctly.
  571. *
  572. * @expectedException \PHPUnit\Framework\AssertionFailedError
  573. * @return void
  574. */
  575. public function testWithUnexpectedException()
  576. {
  577. $this->get('/tests_apps/throw_exception');
  578. $this->assertResponseCode(501);
  579. }
  580. /**
  581. * Test redirecting and integration tests.
  582. *
  583. * @return void
  584. */
  585. public function testRedirect()
  586. {
  587. $this->post('/tests_apps/redirect_to');
  588. $this->assertResponseSuccess();
  589. $this->assertResponseCode(302);
  590. }
  591. /**
  592. * Test redirecting and psr7 stack
  593. *
  594. * @return void
  595. */
  596. public function testRedirectHttpServer()
  597. {
  598. DispatcherFactory::clear();
  599. $this->useHttpServer(true);
  600. $this->post('/tests_apps/redirect_to');
  601. $this->assertResponseCode(302);
  602. $this->assertHeader('X-Middleware', 'true');
  603. }
  604. /**
  605. * Test redirecting and integration tests.
  606. *
  607. * @return void
  608. */
  609. public function testRedirectPermanent()
  610. {
  611. $this->post('/tests_apps/redirect_to_permanent');
  612. $this->assertResponseSuccess();
  613. $this->assertResponseCode(301);
  614. }
  615. /**
  616. * Test the responseOk status assertion
  617. *
  618. * @return void
  619. */
  620. public function testAssertResponseStatusCodes()
  621. {
  622. $this->_response = new Response();
  623. $this->_response = $this->_response->withStatus(200);
  624. $this->assertResponseOk();
  625. $this->_response = $this->_response->withStatus(201);
  626. $this->assertResponseOk();
  627. $this->_response = $this->_response->withStatus(204);
  628. $this->assertResponseOk();
  629. $this->_response = $this->_response->withStatus(202);
  630. $this->assertResponseSuccess();
  631. $this->_response = $this->_response->withStatus(302);
  632. $this->assertResponseSuccess();
  633. $this->_response = $this->_response->withStatus(400);
  634. $this->assertResponseError();
  635. $this->_response = $this->_response->withStatus(417);
  636. $this->assertResponseError();
  637. $this->_response = $this->_response->withStatus(500);
  638. $this->assertResponseFailure();
  639. $this->_response = $this->_response->withStatus(505);
  640. $this->assertResponseFailure();
  641. $this->_response = $this->_response->withStatus(301);
  642. $this->assertResponseCode(301);
  643. }
  644. /**
  645. * Test the location header assertion.
  646. *
  647. * @return void
  648. */
  649. public function testAssertRedirect()
  650. {
  651. $this->_response = new Response();
  652. $this->_response = $this->_response->withHeader('Location', 'http://localhost/tasks/index');
  653. $this->assertRedirect();
  654. $this->assertRedirect('/tasks/index');
  655. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  656. $this->assertResponseEmpty();
  657. }
  658. /**
  659. * Test the location header assertion.
  660. *
  661. * @return void
  662. */
  663. public function testAssertNoRedirect()
  664. {
  665. $this->_response = new Response();
  666. $this->assertNoRedirect();
  667. }
  668. /**
  669. * Test the location header assertion.
  670. *
  671. * @return void
  672. */
  673. public function testAssertNoRedirectFail()
  674. {
  675. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  676. $result = $test->run();
  677. $this->assertFalse($result->wasSuccessful());
  678. $this->assertEquals(1, $result->failureCount());
  679. }
  680. /**
  681. * Test the location header assertion string contains
  682. *
  683. * @return void
  684. */
  685. public function testAssertRedirectContains()
  686. {
  687. $this->_response = new Response();
  688. $this->_response = $this->_response->withHeader('Location', 'http://localhost/tasks/index');
  689. $this->assertRedirectContains('/tasks/index');
  690. }
  691. /**
  692. * Test the header assertion.
  693. *
  694. * @return void
  695. */
  696. public function testAssertHeader()
  697. {
  698. $this->_response = new Response();
  699. $this->_response = $this->_response->withHeader('Etag', 'abc123');
  700. $this->assertHeader('Etag', 'abc123');
  701. }
  702. /**
  703. * Test the header contains assertion.
  704. *
  705. * @return void
  706. */
  707. public function testAssertHeaderContains()
  708. {
  709. $this->_response = new Response();
  710. $this->_response = $this->_response->withHeader('Etag', 'abc123');
  711. $this->assertHeaderContains('Etag', 'abc');
  712. }
  713. /**
  714. * Test the content type assertion.
  715. *
  716. * @return void
  717. */
  718. public function testAssertContentType()
  719. {
  720. $this->_response = new Response();
  721. $this->_response->type('json');
  722. $this->assertContentType('json');
  723. $this->assertContentType('application/json');
  724. }
  725. /**
  726. * Test that type() in an action sets the content-type header.
  727. *
  728. * @return void
  729. */
  730. public function testContentTypeInAction()
  731. {
  732. $this->get('/tests_apps/set_type');
  733. $this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
  734. $this->assertContentType('json');
  735. $this->assertContentType('application/json');
  736. }
  737. /**
  738. * Test the content assertion.
  739. *
  740. * @return void
  741. */
  742. public function testAssertResponseContains()
  743. {
  744. $this->_response = new Response();
  745. $this->_response = $this->_response->withStringBody('Some content');
  746. $this->assertResponseContains('content');
  747. }
  748. /**
  749. * Test the content assertion with no case sensitivity.
  750. *
  751. * @return void
  752. */
  753. public function testAssertResponseContainsWithIgnoreCaseFlag()
  754. {
  755. $this->_response = new Response();
  756. $this->_response = $this->_response->withStringBody('Some content');
  757. $this->assertResponseContains('some', 'Failed asserting that the body contains given content', true);
  758. }
  759. /**
  760. * Test the negated content assertion.
  761. *
  762. * @return void
  763. */
  764. public function testAssertResponseNotContains()
  765. {
  766. $this->_response = new Response();
  767. $this->_response = $this->_response->withStringBody('Some content');
  768. $this->assertResponseNotContains('contents');
  769. }
  770. /**
  771. * Test the content regexp assertion.
  772. *
  773. * @return void
  774. */
  775. public function testAssertResponseRegExp()
  776. {
  777. $this->_response = new Response();
  778. $this->_response = $this->_response->withStringBody('Some content');
  779. $this->assertResponseRegExp('/cont/');
  780. }
  781. /**
  782. * Test the content regexp assertion failing
  783. *
  784. * @expectedException \PHPUnit\Framework\AssertionFailedError
  785. * @expectedExceptionMessage No response set
  786. * @return void
  787. */
  788. public function testAssertResponseRegExpNoResponse()
  789. {
  790. $this->assertResponseRegExp('/cont/');
  791. }
  792. /**
  793. * Test the negated content regexp assertion.
  794. *
  795. * @return void
  796. */
  797. public function testAssertResponseNotRegExp()
  798. {
  799. $this->_response = new Response();
  800. $this->_response = $this->_response->withStringBody('Some content');
  801. $this->assertResponseNotRegExp('/cant/');
  802. }
  803. /**
  804. * Test negated content regexp assertion failing
  805. *
  806. * @expectedException \PHPUnit\Framework\AssertionFailedError
  807. * @expectedExceptionMessage No response set
  808. * @return void
  809. */
  810. public function testAssertResponseNotRegExpNoResponse()
  811. {
  812. $this->assertResponseNotRegExp('/cont/');
  813. }
  814. /**
  815. * Test that works in tandem with testEventManagerReset2 to
  816. * test the EventManager reset.
  817. *
  818. * The return value is passed to testEventManagerReset2 as
  819. * an arguments.
  820. *
  821. * @return \Cake\Event\EventManager
  822. */
  823. public function testEventManagerReset1()
  824. {
  825. $eventManager = EventManager::instance();
  826. $this->assertInstanceOf('Cake\Event\EventManager', $eventManager);
  827. return $eventManager;
  828. }
  829. /**
  830. * Test if the EventManager is reset between tests.
  831. *
  832. * @depends testEventManagerReset1
  833. * @return void
  834. */
  835. public function testEventManagerReset2($prevEventManager)
  836. {
  837. $this->assertInstanceOf('Cake\Event\EventManager', $prevEventManager);
  838. $this->assertNotSame($prevEventManager, EventManager::instance());
  839. }
  840. /**
  841. * Test sending file in requests.
  842. *
  843. * @return void
  844. */
  845. public function testSendFile()
  846. {
  847. $this->get('/posts/file');
  848. $this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
  849. }
  850. /**
  851. * Test sending file with psr7 stack
  852. *
  853. * @return void
  854. */
  855. public function testSendFileHttpServer()
  856. {
  857. DispatcherFactory::clear();
  858. $this->useHttpServer(true);
  859. $this->get('/posts/file');
  860. $this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
  861. }
  862. /**
  863. * Test that assertFile requires a response
  864. *
  865. * @expectedException \PHPUnit\Framework\AssertionFailedError
  866. * @expectedExceptionMessage No response set, cannot assert file
  867. * @return void
  868. */
  869. public function testAssertFileNoResponse()
  870. {
  871. $this->assertFileResponse('foo');
  872. }
  873. /**
  874. * Test that assertFile requires a file
  875. *
  876. * @expectedException \PHPUnit\Framework\AssertionFailedError
  877. * @expectedExceptionMessage No file was sent in this response
  878. * @return void
  879. */
  880. public function testAssertFileNoFile()
  881. {
  882. $this->get('/posts/get');
  883. $this->assertFileResponse('foo');
  884. }
  885. /**
  886. * Test disabling the error handler middleware.
  887. *
  888. * @expectedException \Cake\Routing\Exception\MissingRouteException
  889. * @expectedExceptionMessage A route matching "/foo" could not be found.
  890. * @return void
  891. */
  892. public function testDisableErrorHandlerMiddleware()
  893. {
  894. $this->disableErrorHandlerMiddleware();
  895. $this->get('/foo');
  896. }
  897. }