IntegrationTestCaseTest.php 30 KB

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