IntegrationTestCaseTest.php 26 KB

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