IntegrationTestCaseTest.php 26 KB

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