IntegrationTestCaseTest.php 30 KB

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