IntegrationTestCaseTest.php 17 KB

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