IntegrationTestCaseTest.php 27 KB

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