IntegrationTestCaseTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\TestSuite;
  16. use Cake\Core\Configure;
  17. use Cake\Event\EventManager;
  18. use Cake\Network\Response;
  19. use Cake\Routing\DispatcherFactory;
  20. use Cake\Routing\Router;
  21. use Cake\TestSuite\IntegrationTestCase;
  22. use Cake\Test\Fixture\AssertIntegrationTestCase;
  23. use Cake\Utility\Security;
  24. /**
  25. * Self test of the IntegrationTestCase
  26. */
  27. class IntegrationTestCaseTest extends IntegrationTestCase
  28. {
  29. /**
  30. * Setup method
  31. *
  32. * @return void
  33. */
  34. public function setUp()
  35. {
  36. parent::setUp();
  37. Configure::write('App.namespace', 'TestApp');
  38. Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
  39. DispatcherFactory::clear();
  40. DispatcherFactory::add('Routing');
  41. DispatcherFactory::add('ControllerFactory');
  42. $this->useHttpServer(false);
  43. }
  44. /**
  45. * Test building a request.
  46. *
  47. * @return void
  48. */
  49. public function testRequestBuilding()
  50. {
  51. $this->configRequest([
  52. 'headers' => ['X-CSRF-Token' => 'abc123'],
  53. 'base' => '',
  54. 'webroot' => '/',
  55. 'environment' => [
  56. 'PHP_AUTH_USER' => 'foo',
  57. 'PHP_AUTH_PW' => 'bar'
  58. ]
  59. ]);
  60. $this->cookie('split_token', 'def345');
  61. $this->session(['User' => ['id' => 1, 'username' => 'mark']]);
  62. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  63. $this->assertEquals('abc123', $request->header('X-CSRF-Token'));
  64. $this->assertEquals('tasks/add', $request->url);
  65. $this->assertArrayHasKey('split_token', $request->cookies);
  66. $this->assertEquals('def345', $request->cookies['split_token']);
  67. $this->assertEquals(['id' => '1', 'username' => 'mark'], $request->session()->read('User'));
  68. $this->assertEquals('foo', $request->env('PHP_AUTH_USER'));
  69. $this->assertEquals('bar', $request->env('PHP_AUTH_PW'));
  70. }
  71. /**
  72. * Test request building adds csrf tokens
  73. *
  74. * @return void
  75. */
  76. public function testRequestBuildingCsrfTokens()
  77. {
  78. $this->enableCsrfToken();
  79. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  80. $this->assertArrayHasKey('csrfToken', $request->cookies);
  81. $this->assertArrayHasKey('_csrfToken', $request->data);
  82. $this->assertSame($request->cookies['csrfToken'], $request->data['_csrfToken']);
  83. $this->cookie('csrfToken', '');
  84. $request = $this->_buildRequest('/tasks/add', 'POST', [
  85. '_csrfToken' => 'fale',
  86. 'title' => 'First post'
  87. ]);
  88. $this->assertSame('', $request->cookies['csrfToken']);
  89. $this->assertSame('fale', $request->data['_csrfToken']);
  90. }
  91. /**
  92. * Test multiple actions using CSRF tokens don't fail
  93. *
  94. * @return void
  95. */
  96. public function testEnableCsrfMultipleRequests()
  97. {
  98. $this->enableCsrfToken();
  99. $first = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  100. $second = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'Second post']);
  101. $this->assertSame($first->cookies['csrfToken'], $second->data['_csrfToken'], 'Csrf token should match cookie');
  102. $this->assertSame(
  103. $first->data['_csrfToken'],
  104. $second->data['_csrfToken'],
  105. 'Tokens should be consistent per test method'
  106. );
  107. }
  108. /**
  109. * Test pre-determined CSRF tokens.
  110. *
  111. * @return void
  112. */
  113. public function testEnableCsrfPredeterminedCookie()
  114. {
  115. $this->enableCsrfToken();
  116. $value = 'I am a teapot';
  117. $this->cookie('csrfToken', $value);
  118. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  119. $this->assertSame($value, $request->cookies['csrfToken'], 'Csrf token should match cookie');
  120. $this->assertSame($value, $request->data['_csrfToken'], 'Tokens should match');
  121. }
  122. /**
  123. * Test building a request, with query parameters
  124. *
  125. * @return void
  126. */
  127. public function testRequestBuildingQueryParameters()
  128. {
  129. $request = $this->_buildRequest('/tasks/view?archived=yes', 'GET', []);
  130. $this->assertEquals('/tasks/view?archived=yes', $request->here());
  131. $this->assertEquals('yes', $request->query('archived'));
  132. }
  133. /**
  134. * Test cookie encrypted
  135. *
  136. * @see CookieComponentControllerTest
  137. */
  138. public function testCookieEncrypted()
  139. {
  140. Security::salt('abcdabcdabcdabcdabcdabcdabcdabcdabcd');
  141. $this->cookieEncrypted('KeyOfCookie', 'Encrypted with aes by default');
  142. $request = $this->_buildRequest('/tasks/view', 'GET', []);
  143. $this->assertStringStartsWith('Q2FrZQ==.', $request->cookies['KeyOfCookie']);
  144. }
  145. /**
  146. * Test sending get requests.
  147. *
  148. * @return void
  149. */
  150. public function testGet()
  151. {
  152. $this->assertNull($this->_response);
  153. $this->get('/request_action/test_request_action');
  154. $this->assertNotEmpty($this->_response);
  155. $this->assertInstanceOf('Cake\Network\Response', $this->_response);
  156. $this->assertEquals('This is a test', $this->_response->body());
  157. }
  158. /**
  159. * Test sending get requests with Http\Server
  160. *
  161. * @return void
  162. */
  163. public function testGetHttpServer()
  164. {
  165. DispatcherFactory::clear();
  166. $this->useHttpServer(true);
  167. $this->assertNull($this->_response);
  168. $this->get('/request_action/test_request_action');
  169. $this->assertNotEmpty($this->_response);
  170. $this->assertInstanceOf('Cake\Network\Response', $this->_response);
  171. $this->assertEquals('This is a test', $this->_response->body());
  172. }
  173. /**
  174. * Test sending requests stores references to controller/view/layout.
  175. *
  176. * @return void
  177. */
  178. public function testRequestSetsProperties()
  179. {
  180. $this->post('/posts/index');
  181. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  182. $this->assertNotEmpty($this->_viewName, 'View name not set');
  183. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  184. $this->assertNotEmpty($this->_layoutName, 'Layout name not set');
  185. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  186. $this->assertTemplate('index');
  187. $this->assertLayout('default');
  188. $this->assertEquals('value', $this->viewVariable('test'));
  189. }
  190. /**
  191. * Test PSR7 requests store references to controller/view/layout
  192. *
  193. * @return void
  194. */
  195. public function testRequestSetsPropertiesHttpServer()
  196. {
  197. $this->markTestIncomplete('not done');
  198. DispatcherFactory::clear();
  199. $this->useHttpServer(true);
  200. $this->post('/posts/index');
  201. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  202. $this->assertNotEmpty($this->_viewName, 'View name not set');
  203. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  204. $this->assertNotEmpty($this->_layoutName, 'Layout name not set');
  205. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  206. $this->assertTemplate('index');
  207. $this->assertLayout('default');
  208. $this->assertEquals('value', $this->viewVariable('test'));
  209. }
  210. /**
  211. * Test that the PSR7 requests get post, cookies, and other request data passed along.
  212. *
  213. * @return void
  214. */
  215. public function testPsrRequestData()
  216. {
  217. $this->markTestIncomplete('not done');
  218. }
  219. /**
  220. * Assert that the stored template doesn't change when cells are rendered.
  221. *
  222. * @return void
  223. */
  224. public function testAssertTemplateAfterCellRender()
  225. {
  226. $this->get('/posts/get');
  227. $this->assertContains('Template' . DS . 'Posts' . DS . 'get.ctp', $this->_viewName);
  228. $this->assertTemplate('get');
  229. $this->assertResponseContains('cellcontent');
  230. }
  231. /**
  232. * Test array URLs
  233. *
  234. * @return void
  235. */
  236. public function testArrayUrls()
  237. {
  238. $this->post(['controller' => 'Posts', 'action' => 'index']);
  239. $this->assertEquals('value', $this->viewVariable('test'));
  240. }
  241. /**
  242. * Test flash and cookie assertions
  243. *
  244. * @return void
  245. */
  246. public function testFlashSessionAndCookieAsserts()
  247. {
  248. $this->post('/posts/index');
  249. $this->assertSession('An error message', 'Flash.flash.0.message');
  250. $this->assertCookie(1, 'remember_me');
  251. $this->assertCookieNotSet('user_id');
  252. }
  253. /**
  254. * Tests the failure message for assertCookieNotSet
  255. *
  256. * @expectedException PHPUnit_Framework_AssertionFailedError
  257. * @expectedExceptionMessage Cookie 'remember_me' has been set
  258. * @return void
  259. */
  260. public function testCookieNotSetFailure()
  261. {
  262. $this->post('/posts/index');
  263. $this->assertCookieNotSet('remember_me');
  264. }
  265. /**
  266. * Tests the failure message for assertCookieNotSet when no
  267. * response whas generated
  268. *
  269. * @expectedException PHPUnit_Framework_AssertionFailedError
  270. * @expectedExceptionMessage No response set, cannot assert cookies.
  271. * @return void
  272. */
  273. public function testCookieNotSetFailureNoResponse()
  274. {
  275. $this->assertCookieNotSet('remember_me');
  276. }
  277. /**
  278. * Test error handling and error page rendering.
  279. *
  280. * @return void
  281. */
  282. public function testPostAndErrorHandling()
  283. {
  284. $this->post('/request_action/error_method');
  285. $this->assertResponseNotEmpty();
  286. $this->assertResponseContains('Not there or here');
  287. $this->assertResponseContains('<!DOCTYPE html>');
  288. }
  289. /**
  290. * Test posting to a secured form action.
  291. *
  292. * @return void
  293. */
  294. public function testPostSecuredForm()
  295. {
  296. $this->enableSecurityToken();
  297. $data = [
  298. 'title' => 'Some title',
  299. 'body' => 'Some text'
  300. ];
  301. $this->post('/posts/securePost', $data);
  302. $this->assertResponseOk();
  303. $this->assertResponseContains('Request was accepted');
  304. }
  305. /**
  306. * Test posting to a secured form action with nested data.
  307. *
  308. * @return void
  309. */
  310. public function testPostSecuredFormNestedData()
  311. {
  312. $this->enableSecurityToken();
  313. $data = [
  314. 'title' => 'New post',
  315. 'comments' => [
  316. ['comment' => 'A new comment']
  317. ],
  318. 'tags' => ['_ids' => [1, 2, 3, 4]]
  319. ];
  320. $this->post('/posts/securePost', $data);
  321. $this->assertResponseOk();
  322. $this->assertResponseContains('Request was accepted');
  323. }
  324. /**
  325. * Test posting to a secured form action action.
  326. *
  327. * @return void
  328. */
  329. public function testPostSecuredFormFailure()
  330. {
  331. $data = [
  332. 'title' => 'Some title',
  333. 'body' => 'Some text'
  334. ];
  335. $this->post('/posts/securePost', $data);
  336. $this->assertResponseError();
  337. }
  338. /**
  339. * Test that exceptions being thrown are handled correctly.
  340. *
  341. * @return void
  342. */
  343. public function testWithExpectedException()
  344. {
  345. $this->get('/tests_apps/throw_exception');
  346. $this->assertResponseCode(500);
  347. }
  348. /**
  349. * Test that exceptions being thrown are handled correctly.
  350. *
  351. * @expectedException PHPUnit_Framework_AssertionFailedError
  352. * @return void
  353. */
  354. public function testWithUnexpectedException()
  355. {
  356. $this->get('/tests_apps/throw_exception');
  357. $this->assertResponseCode(501);
  358. }
  359. /**
  360. * Test redirecting and integration tests.
  361. *
  362. * @return void
  363. */
  364. public function testRedirect()
  365. {
  366. $this->post('/tests_apps/redirect_to');
  367. $this->assertResponseSuccess();
  368. $this->assertResponseCode(302);
  369. }
  370. /**
  371. * Test redirecting and integration tests.
  372. *
  373. * @return void
  374. */
  375. public function testRedirectPermanent()
  376. {
  377. $this->post('/tests_apps/redirect_to_permanent');
  378. $this->assertResponseSuccess();
  379. $this->assertResponseCode(301);
  380. }
  381. /**
  382. * Test the responseOk status assertion
  383. *
  384. * @return void
  385. */
  386. public function testAssertResponseStatusCodes()
  387. {
  388. $this->_response = new Response();
  389. $this->_response->statusCode(200);
  390. $this->assertResponseOk();
  391. $this->_response->statusCode(201);
  392. $this->assertResponseOk();
  393. $this->_response->statusCode(204);
  394. $this->assertResponseOk();
  395. $this->_response->statusCode(202);
  396. $this->assertResponseSuccess();
  397. $this->_response->statusCode(302);
  398. $this->assertResponseSuccess();
  399. $this->_response->statusCode(400);
  400. $this->assertResponseError();
  401. $this->_response->statusCode(417);
  402. $this->assertResponseError();
  403. $this->_response->statusCode(500);
  404. $this->assertResponseFailure();
  405. $this->_response->statusCode(505);
  406. $this->assertResponseFailure();
  407. $this->_response->statusCode(301);
  408. $this->assertResponseCode(301);
  409. }
  410. /**
  411. * Test the location header assertion.
  412. *
  413. * @return void
  414. */
  415. public function testAssertRedirect()
  416. {
  417. $this->_response = new Response();
  418. $this->_response->header('Location', 'http://localhost/tasks/index');
  419. $this->assertRedirect();
  420. $this->assertRedirect('/tasks/index');
  421. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  422. $this->assertResponseEmpty();
  423. }
  424. /**
  425. * Test the location header assertion.
  426. *
  427. * @return void
  428. */
  429. public function testAssertNoRedirect()
  430. {
  431. $this->_response = new Response();
  432. $this->assertNoRedirect();
  433. }
  434. /**
  435. * Test the location header assertion.
  436. *
  437. * @return void
  438. */
  439. public function testAssertNoRedirectFail()
  440. {
  441. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  442. $result = $test->run();
  443. ob_start();
  444. $this->assertFalse($result->wasSuccessful());
  445. $this->assertEquals(1, $result->failureCount());
  446. }
  447. /**
  448. * Test the location header assertion string contains
  449. *
  450. * @return void
  451. */
  452. public function testAssertRedirectContains()
  453. {
  454. $this->_response = new Response();
  455. $this->_response->header('Location', 'http://localhost/tasks/index');
  456. $this->assertRedirectContains('/tasks/index');
  457. }
  458. /**
  459. * Test the header assertion.
  460. *
  461. * @return void
  462. */
  463. public function testAssertHeader()
  464. {
  465. $this->_response = new Response();
  466. $this->_response->header('Etag', 'abc123');
  467. $this->assertHeader('Etag', 'abc123');
  468. }
  469. /**
  470. * Test the header contains assertion.
  471. *
  472. * @return void
  473. */
  474. public function testAssertHeaderContains()
  475. {
  476. $this->_response = new Response();
  477. $this->_response->header('Etag', 'abc123');
  478. $this->assertHeaderContains('Etag', 'abc');
  479. }
  480. /**
  481. * Test the content type assertion.
  482. *
  483. * @return void
  484. */
  485. public function testAssertContentType()
  486. {
  487. $this->_response = new Response();
  488. $this->_response->type('json');
  489. $this->assertContentType('json');
  490. $this->assertContentType('application/json');
  491. }
  492. /**
  493. * Test that type() in an action sets the content-type header.
  494. *
  495. * @return void
  496. */
  497. public function testContentTypeInAction()
  498. {
  499. $this->get('/tests_apps/set_type');
  500. $this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
  501. $this->assertContentType('json');
  502. $this->assertContentType('application/json');
  503. }
  504. /**
  505. * Test the content assertion.
  506. *
  507. * @return void
  508. */
  509. public function testAssertResponseContains()
  510. {
  511. $this->_response = new Response();
  512. $this->_response->body('Some content');
  513. $this->assertResponseContains('content');
  514. }
  515. /**
  516. * Test the negated content assertion.
  517. *
  518. * @return void
  519. */
  520. public function testAssertResponseNotContains()
  521. {
  522. $this->_response = new Response();
  523. $this->_response->body('Some content');
  524. $this->assertResponseNotContains('contents');
  525. }
  526. /**
  527. * Test that works in tandem with testEventManagerReset2 to
  528. * test the EventManager reset.
  529. *
  530. * The return value is passed to testEventManagerReset2 as
  531. * an arguments.
  532. *
  533. * @return \Cake\Event\EventManager
  534. */
  535. public function testEventManagerReset1()
  536. {
  537. return EventManager::instance();
  538. }
  539. /**
  540. * Test if the EventManager is reset between tests.
  541. *
  542. * @depends testEventManagerReset1
  543. * @return void
  544. */
  545. public function testEventManagerReset2($prevEventManager)
  546. {
  547. $this->assertNotSame($prevEventManager, EventManager::instance());
  548. }
  549. /**
  550. * Test sending file in requests.
  551. *
  552. * @return void
  553. */
  554. public function testSendFile()
  555. {
  556. $this->get('/posts/file');
  557. $this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
  558. }
  559. /**
  560. * Test that assertFile requires a response
  561. *
  562. * @expectedException PHPUnit_Framework_AssertionFailedError
  563. * @expectedExceptionMessage No response set, cannot assert file
  564. * @return void
  565. */
  566. public function testAssertFileNoReponse()
  567. {
  568. $this->assertFileResponse('foo');
  569. }
  570. /**
  571. * Test that assertFile requires a file
  572. *
  573. * @expectedException PHPUnit_Framework_AssertionFailedError
  574. * @expectedExceptionMessage No file was sent in this response
  575. * @return void
  576. */
  577. public function testAssertFileNoFile()
  578. {
  579. $this->get('/posts/get');
  580. $this->assertFileResponse('foo');
  581. }
  582. }