IntegrationTestCaseTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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. Configure::write('App.namespace', 'TestApp');
  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->assertEquals('/tasks/view', $request['url']);
  143. $this->assertEquals('yes', $request['query']['archived']);
  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 testQueryStringHttpServer()
  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. }
  262. /**
  263. * Test that the PSR7 requests get cookies
  264. *
  265. * @return void
  266. */
  267. public function testGetCookiesHttpServer()
  268. {
  269. $this->useHttpServer(true);
  270. $this->configRequest(['cookies' => ['split_test' => 'abc']]);
  271. $this->get('/request_action/cookie_pass');
  272. $this->assertResponseOk();
  273. $this->assertResponseContains('"split_test":"abc"');
  274. $this->assertHeader('X-Middleware', 'true');
  275. }
  276. /**
  277. * Test that the PSR7 requests receive post data
  278. *
  279. * @return void
  280. */
  281. public function testPostDataHttpServer()
  282. {
  283. $this->useHttpServer(true);
  284. $this->post('/request_action/post_pass', ['title' => 'value']);
  285. $data = json_decode($this->_response->getBody());
  286. $this->assertEquals('value', $data->title);
  287. $this->assertHeader('X-Middleware', 'true');
  288. }
  289. /**
  290. * Test that the PSR7 requests receive encoded data.
  291. *
  292. * @return void
  293. */
  294. public function testInputDataHttpServer()
  295. {
  296. $this->useHttpServer(true);
  297. $this->post('/request_action/input_test', '{"hello":"world"}');
  298. if ($this->_response->getBody()->isSeekable()) {
  299. $this->_response->getBody()->rewind();
  300. }
  301. $this->assertSame('world', $this->_response->getBody()->getContents());
  302. $this->assertHeader('X-Middleware', 'true');
  303. }
  304. /**
  305. * Test that the PSR7 requests get cookies
  306. *
  307. * @return void
  308. */
  309. public function testSessionHttpServer()
  310. {
  311. $this->useHttpServer(true);
  312. $this->session(['foo' => 'session data']);
  313. $this->get('/request_action/session_test');
  314. $this->assertResponseOk();
  315. $this->assertResponseContains('session data');
  316. $this->assertHeader('X-Middleware', 'true');
  317. }
  318. /**
  319. * Test sending requests stores references to controller/view/layout.
  320. *
  321. * @return void
  322. */
  323. public function testRequestSetsProperties()
  324. {
  325. $this->post('/posts/index');
  326. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  327. $this->assertNotEmpty($this->_viewName, 'View name not set');
  328. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  329. $this->assertNotEmpty($this->_layoutName, 'Layout name not set');
  330. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  331. $this->assertTemplate('index');
  332. $this->assertLayout('default');
  333. $this->assertEquals('value', $this->viewVariable('test'));
  334. }
  335. /**
  336. * Test PSR7 requests store references to controller/view/layout
  337. *
  338. * @return void
  339. */
  340. public function testRequestSetsPropertiesHttpServer()
  341. {
  342. $this->useHttpServer(true);
  343. DispatcherFactory::clear();
  344. $this->post('/posts/index');
  345. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  346. $this->assertNotEmpty($this->_viewName, 'View name not set');
  347. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  348. $this->assertNotEmpty($this->_layoutName, 'Layout name not set');
  349. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  350. $this->assertTemplate('index');
  351. $this->assertLayout('default');
  352. $this->assertEquals('value', $this->viewVariable('test'));
  353. }
  354. /**
  355. * Assert that the stored template doesn't change when cells are rendered.
  356. *
  357. * @return void
  358. */
  359. public function testAssertTemplateAfterCellRender()
  360. {
  361. $this->get('/posts/get');
  362. $this->assertContains('Template' . DS . 'Posts' . DS . 'get.ctp', $this->_viewName);
  363. $this->assertTemplate('get');
  364. $this->assertResponseContains('cellcontent');
  365. }
  366. /**
  367. * Test array URLs
  368. *
  369. * @return void
  370. */
  371. public function testArrayUrls()
  372. {
  373. $this->post(['controller' => 'Posts', 'action' => 'index']);
  374. $this->assertEquals('value', $this->viewVariable('test'));
  375. }
  376. /**
  377. * Test flash and cookie assertions
  378. *
  379. * @return void
  380. */
  381. public function testFlashSessionAndCookieAsserts()
  382. {
  383. $this->post('/posts/index');
  384. $this->assertSession('An error message', 'Flash.flash.0.message');
  385. $this->assertCookie(1, 'remember_me');
  386. $this->assertCookieNotSet('user_id');
  387. }
  388. /**
  389. * Test flash and cookie assertions
  390. *
  391. * @return void
  392. */
  393. public function testFlashSessionAndCookieAssertsHttpServer()
  394. {
  395. $this->useHttpServer(true);
  396. $this->post('/posts/index');
  397. $this->assertSession('An error message', 'Flash.flash.0.message');
  398. $this->assertCookieNotSet('user_id');
  399. $this->assertCookie(1, 'remember_me');
  400. }
  401. /**
  402. * Test flash assertions stored with enableRememberFlashMessages() after they
  403. * are rendered
  404. *
  405. * @return void
  406. */
  407. public function testFlashAssertionsAfterRender()
  408. {
  409. $this->enableRetainFlashMessages();
  410. $this->get('/posts/index/with_flash');
  411. $this->assertSession('An error message', 'Flash.flash.0.message');
  412. }
  413. /**
  414. * Test flash assertions stored with enableRememberFlashMessages() even if
  415. * no view is rendered
  416. *
  417. * @return void
  418. */
  419. public function testFlashAssertionsWithNoRender()
  420. {
  421. $this->enableRetainFlashMessages();
  422. $this->get('/posts/flashNoRender');
  423. $this->assertRedirect();
  424. $this->assertSession('An error message', 'Flash.flash.0.message');
  425. }
  426. /**
  427. * Tests the failure message for assertCookieNotSet
  428. *
  429. * @expectedException \PHPUnit\Framework\AssertionFailedError
  430. * @expectedExceptionMessage Cookie 'remember_me' has been set
  431. * @return void
  432. */
  433. public function testCookieNotSetFailure()
  434. {
  435. $this->post('/posts/index');
  436. $this->assertCookieNotSet('remember_me');
  437. }
  438. /**
  439. * Tests the failure message for assertCookieNotSet when no
  440. * response whas generated
  441. *
  442. * @expectedException \PHPUnit\Framework\AssertionFailedError
  443. * @expectedExceptionMessage No response set, cannot assert cookies.
  444. * @return void
  445. */
  446. public function testCookieNotSetFailureNoResponse()
  447. {
  448. $this->assertCookieNotSet('remember_me');
  449. }
  450. /**
  451. * Test error handling and error page rendering.
  452. *
  453. * @return void
  454. */
  455. public function testPostAndErrorHandling()
  456. {
  457. $this->post('/request_action/error_method');
  458. $this->assertResponseNotEmpty();
  459. $this->assertResponseContains('Not there or here');
  460. $this->assertResponseContains('<!DOCTYPE html>');
  461. }
  462. /**
  463. * Test posting to a secured form action.
  464. *
  465. * @return void
  466. */
  467. public function testPostSecuredForm()
  468. {
  469. $this->enableSecurityToken();
  470. $data = [
  471. 'title' => 'Some title',
  472. 'body' => 'Some text'
  473. ];
  474. $this->post('/posts/securePost', $data);
  475. $this->assertResponseOk();
  476. $this->assertResponseContains('Request was accepted');
  477. }
  478. /**
  479. * Test posting to a secured form action with nested data.
  480. *
  481. * @return void
  482. */
  483. public function testPostSecuredFormNestedData()
  484. {
  485. $this->enableSecurityToken();
  486. $data = [
  487. 'title' => 'New post',
  488. 'comments' => [
  489. ['comment' => 'A new comment']
  490. ],
  491. 'tags' => ['_ids' => [1, 2, 3, 4]]
  492. ];
  493. $this->post('/posts/securePost', $data);
  494. $this->assertResponseOk();
  495. $this->assertResponseContains('Request was accepted');
  496. }
  497. /**
  498. * Test posting to a secured form action.
  499. *
  500. * @return void
  501. */
  502. public function testPostSecuredFormWithQuery()
  503. {
  504. $this->enableSecurityToken();
  505. $data = [
  506. 'title' => 'Some title',
  507. 'body' => 'Some text'
  508. ];
  509. $this->post('/posts/securePost?foo=bar', $data);
  510. $this->assertResponseOk();
  511. $this->assertResponseContains('Request was accepted');
  512. }
  513. /**
  514. * Test posting to a secured form action action.
  515. *
  516. * @return void
  517. */
  518. public function testPostSecuredFormFailure()
  519. {
  520. $data = [
  521. 'title' => 'Some title',
  522. 'body' => 'Some text'
  523. ];
  524. $this->post('/posts/securePost', $data);
  525. $this->assertResponseError();
  526. }
  527. /**
  528. * Test that exceptions being thrown are handled correctly.
  529. *
  530. * @return void
  531. */
  532. public function testWithExpectedException()
  533. {
  534. $this->get('/tests_apps/throw_exception');
  535. $this->assertResponseCode(500);
  536. }
  537. /**
  538. * Test that exceptions being thrown are handled correctly by the psr7 stack.
  539. *
  540. * @return void
  541. */
  542. public function testWithExpectedExceptionHttpServer()
  543. {
  544. DispatcherFactory::clear();
  545. $this->useHttpServer(true);
  546. $this->get('/tests_apps/throw_exception');
  547. $this->assertResponseCode(500);
  548. }
  549. /**
  550. * Test that exceptions being thrown are handled correctly.
  551. *
  552. * @expectedException \PHPUnit\Framework\AssertionFailedError
  553. * @return void
  554. */
  555. public function testWithUnexpectedException()
  556. {
  557. $this->get('/tests_apps/throw_exception');
  558. $this->assertResponseCode(501);
  559. }
  560. /**
  561. * Test redirecting and integration tests.
  562. *
  563. * @return void
  564. */
  565. public function testRedirect()
  566. {
  567. $this->post('/tests_apps/redirect_to');
  568. $this->assertResponseSuccess();
  569. $this->assertResponseCode(302);
  570. }
  571. /**
  572. * Test redirecting and psr7 stack
  573. *
  574. * @return void
  575. */
  576. public function testRedirectHttpServer()
  577. {
  578. DispatcherFactory::clear();
  579. $this->useHttpServer(true);
  580. $this->post('/tests_apps/redirect_to');
  581. $this->assertResponseCode(302);
  582. $this->assertHeader('X-Middleware', 'true');
  583. }
  584. /**
  585. * Test redirecting and integration tests.
  586. *
  587. * @return void
  588. */
  589. public function testRedirectPermanent()
  590. {
  591. $this->post('/tests_apps/redirect_to_permanent');
  592. $this->assertResponseSuccess();
  593. $this->assertResponseCode(301);
  594. }
  595. /**
  596. * Test the responseOk status assertion
  597. *
  598. * @return void
  599. */
  600. public function testAssertResponseStatusCodes()
  601. {
  602. $this->_response = new Response();
  603. $this->_response = $this->_response->withStatus(200);
  604. $this->assertResponseOk();
  605. $this->_response = $this->_response->withStatus(201);
  606. $this->assertResponseOk();
  607. $this->_response = $this->_response->withStatus(204);
  608. $this->assertResponseOk();
  609. $this->_response = $this->_response->withStatus(202);
  610. $this->assertResponseSuccess();
  611. $this->_response = $this->_response->withStatus(302);
  612. $this->assertResponseSuccess();
  613. $this->_response = $this->_response->withStatus(400);
  614. $this->assertResponseError();
  615. $this->_response = $this->_response->withStatus(417);
  616. $this->assertResponseError();
  617. $this->_response = $this->_response->withStatus(500);
  618. $this->assertResponseFailure();
  619. $this->_response = $this->_response->withStatus(505);
  620. $this->assertResponseFailure();
  621. $this->_response = $this->_response->withStatus(301);
  622. $this->assertResponseCode(301);
  623. }
  624. /**
  625. * Test the location header assertion.
  626. *
  627. * @return void
  628. */
  629. public function testAssertRedirect()
  630. {
  631. $this->_response = new Response();
  632. $this->_response = $this->_response->withHeader('Location', 'http://localhost/tasks/index');
  633. $this->assertRedirect();
  634. $this->assertRedirect('/tasks/index');
  635. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  636. $this->assertResponseEmpty();
  637. }
  638. /**
  639. * Test the location header assertion.
  640. *
  641. * @return void
  642. */
  643. public function testAssertNoRedirect()
  644. {
  645. $this->_response = new Response();
  646. $this->assertNoRedirect();
  647. }
  648. /**
  649. * Test the location header assertion.
  650. *
  651. * @return void
  652. */
  653. public function testAssertNoRedirectFail()
  654. {
  655. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  656. $result = $test->run();
  657. $this->assertFalse($result->wasSuccessful());
  658. $this->assertEquals(1, $result->failureCount());
  659. }
  660. /**
  661. * Test the location header assertion string contains
  662. *
  663. * @return void
  664. */
  665. public function testAssertRedirectContains()
  666. {
  667. $this->_response = new Response();
  668. $this->_response = $this->_response->withHeader('Location', 'http://localhost/tasks/index');
  669. $this->assertRedirectContains('/tasks/index');
  670. }
  671. /**
  672. * Test the header assertion.
  673. *
  674. * @return void
  675. */
  676. public function testAssertHeader()
  677. {
  678. $this->_response = new Response();
  679. $this->_response = $this->_response->withHeader('Etag', 'abc123');
  680. $this->assertHeader('Etag', 'abc123');
  681. }
  682. /**
  683. * Test the header contains assertion.
  684. *
  685. * @return void
  686. */
  687. public function testAssertHeaderContains()
  688. {
  689. $this->_response = new Response();
  690. $this->_response = $this->_response->withHeader('Etag', 'abc123');
  691. $this->assertHeaderContains('Etag', 'abc');
  692. }
  693. /**
  694. * Test the content type assertion.
  695. *
  696. * @return void
  697. */
  698. public function testAssertContentType()
  699. {
  700. $this->_response = new Response();
  701. $this->_response->type('json');
  702. $this->assertContentType('json');
  703. $this->assertContentType('application/json');
  704. }
  705. /**
  706. * Test that type() in an action sets the content-type header.
  707. *
  708. * @return void
  709. */
  710. public function testContentTypeInAction()
  711. {
  712. $this->get('/tests_apps/set_type');
  713. $this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
  714. $this->assertContentType('json');
  715. $this->assertContentType('application/json');
  716. }
  717. /**
  718. * Test the content assertion.
  719. *
  720. * @return void
  721. */
  722. public function testAssertResponseContains()
  723. {
  724. $this->_response = new Response();
  725. $this->_response = $this->_response->withStringBody('Some content');
  726. $this->assertResponseContains('content');
  727. }
  728. /**
  729. * Test the negated content assertion.
  730. *
  731. * @return void
  732. */
  733. public function testAssertResponseNotContains()
  734. {
  735. $this->_response = new Response();
  736. $this->_response = $this->_response->withStringBody('Some content');
  737. $this->assertResponseNotContains('contents');
  738. }
  739. /**
  740. * Test the content regexp assertion.
  741. *
  742. * @return void
  743. */
  744. public function testAssertResponseRegExp()
  745. {
  746. $this->_response = new Response();
  747. $this->_response = $this->_response->withStringBody('Some content');
  748. $this->assertResponseRegExp('/cont/');
  749. }
  750. /**
  751. * Test the content regexp assertion failing
  752. *
  753. * @expectedException \PHPUnit\Framework\AssertionFailedError
  754. * @expectedExceptionMessage No response set
  755. * @return void
  756. */
  757. public function testAssertResponseRegExpNoResponse()
  758. {
  759. $this->assertResponseRegExp('/cont/');
  760. }
  761. /**
  762. * Test the negated content regexp assertion.
  763. *
  764. * @return void
  765. */
  766. public function testAssertResponseNotRegExp()
  767. {
  768. $this->_response = new Response();
  769. $this->_response = $this->_response->withStringBody('Some content');
  770. $this->assertResponseNotRegExp('/cant/');
  771. }
  772. /**
  773. * Test negated content regexp assertion failing
  774. *
  775. * @expectedException \PHPUnit\Framework\AssertionFailedError
  776. * @expectedExceptionMessage No response set
  777. * @return void
  778. */
  779. public function testAssertResponseNotRegExpNoResponse()
  780. {
  781. $this->assertResponseNotRegExp('/cont/');
  782. }
  783. /**
  784. * Test that works in tandem with testEventManagerReset2 to
  785. * test the EventManager reset.
  786. *
  787. * The return value is passed to testEventManagerReset2 as
  788. * an arguments.
  789. *
  790. * @return \Cake\Event\EventManager
  791. */
  792. public function testEventManagerReset1()
  793. {
  794. $eventManager = EventManager::instance();
  795. $this->assertInstanceOf('Cake\Event\EventManager', $eventManager);
  796. return $eventManager;
  797. }
  798. /**
  799. * Test if the EventManager is reset between tests.
  800. *
  801. * @depends testEventManagerReset1
  802. * @return void
  803. */
  804. public function testEventManagerReset2($prevEventManager)
  805. {
  806. $this->assertInstanceOf('Cake\Event\EventManager', $prevEventManager);
  807. $this->assertNotSame($prevEventManager, EventManager::instance());
  808. }
  809. /**
  810. * Test sending file in requests.
  811. *
  812. * @return void
  813. */
  814. public function testSendFile()
  815. {
  816. $this->get('/posts/file');
  817. $this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
  818. }
  819. /**
  820. * Test sending file with psr7 stack
  821. *
  822. * @return void
  823. */
  824. public function testSendFileHttpServer()
  825. {
  826. DispatcherFactory::clear();
  827. $this->useHttpServer(true);
  828. $this->get('/posts/file');
  829. $this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
  830. }
  831. /**
  832. * Test that assertFile requires a response
  833. *
  834. * @expectedException \PHPUnit\Framework\AssertionFailedError
  835. * @expectedExceptionMessage No response set, cannot assert file
  836. * @return void
  837. */
  838. public function testAssertFileNoResponse()
  839. {
  840. $this->assertFileResponse('foo');
  841. }
  842. /**
  843. * Test that assertFile requires a file
  844. *
  845. * @expectedException \PHPUnit\Framework\AssertionFailedError
  846. * @expectedExceptionMessage No file was sent in this response
  847. * @return void
  848. */
  849. public function testAssertFileNoFile()
  850. {
  851. $this->get('/posts/get');
  852. $this->assertFileResponse('foo');
  853. }
  854. }