IntegrationTestCaseTest.php 28 KB

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