IntegrationTestCaseTest.php 36 KB

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