IntegrationTestTraitTest.php 46 KB

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