IntegrationTestCaseTest.php 17 KB

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