IntegrationTestCaseTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  167. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  168. $this->assertTemplate('index');
  169. $this->assertLayout('default');
  170. $this->assertEquals('value', $this->viewVariable('test'));
  171. }
  172. /**
  173. * Assert that the stored template doesn't change when cells are rendered.
  174. *
  175. * @return void
  176. */
  177. public function testAssertTemplateAfterCellRender()
  178. {
  179. $this->get('/posts/get');
  180. $this->assertContains('Template' . DS . 'Posts' . DS . 'get.ctp', $this->_viewName);
  181. $this->assertTemplate('get');
  182. $this->assertResponseContains('cellcontent');
  183. }
  184. /**
  185. * Test array URLs
  186. *
  187. * @return void
  188. */
  189. public function testArrayUrls()
  190. {
  191. $this->post(['controller' => 'Posts', 'action' => 'index']);
  192. $this->assertEquals('value', $this->viewVariable('test'));
  193. }
  194. /**
  195. * Test flash and cookie assertions
  196. *
  197. * @return void
  198. */
  199. public function testFlashSessionAndCookieAsserts()
  200. {
  201. $this->post('/posts/index');
  202. $this->assertSession('An error message', 'Flash.flash.0.message');
  203. $this->assertCookie(1, 'remember_me');
  204. $this->assertCookieNotSet('user_id');
  205. }
  206. /**
  207. * Tests the failure message for assertCookieNotSet
  208. *
  209. * @expectedException PHPUnit_Framework_AssertionFailedError
  210. * @expectedExceptionMessage Cookie 'remember_me' has been set
  211. * @return void
  212. */
  213. public function testCookieNotSetFailure()
  214. {
  215. $this->post('/posts/index');
  216. $this->assertCookieNotSet('remember_me');
  217. }
  218. /**
  219. * Tests the failure message for assertCookieNotSet when no
  220. * response whas generated
  221. *
  222. * @expectedException PHPUnit_Framework_AssertionFailedError
  223. * @expectedExceptionMessage No response set, cannot assert cookies.
  224. * @return void
  225. */
  226. public function testCookieNotSetFailureNoResponse()
  227. {
  228. $this->assertCookieNotSet('remember_me');
  229. }
  230. /**
  231. * Test error handling and error page rendering.
  232. *
  233. * @return void
  234. */
  235. public function testPostAndErrorHandling()
  236. {
  237. $this->post('/request_action/error_method');
  238. $this->assertResponseNotEmpty();
  239. $this->assertResponseContains('Not there or here');
  240. $this->assertResponseContains('<!DOCTYPE html>');
  241. }
  242. /**
  243. * Test posting to a secured form action.
  244. *
  245. * @return void
  246. */
  247. public function testPostSecuredForm()
  248. {
  249. $this->enableSecurityToken();
  250. $data = [
  251. 'title' => 'Some title',
  252. 'body' => 'Some text'
  253. ];
  254. $this->post('/posts/securePost', $data);
  255. $this->assertResponseOk();
  256. $this->assertResponseContains('Request was accepted');
  257. }
  258. /**
  259. * Test posting to a secured form action with nested data.
  260. *
  261. * @return void
  262. */
  263. public function testPostSecuredFormNestedData()
  264. {
  265. $this->enableSecurityToken();
  266. $data = [
  267. 'title' => 'New post',
  268. 'comments' => [
  269. ['comment' => 'A new comment']
  270. ],
  271. 'tags' => ['_ids' => [1, 2, 3, 4]]
  272. ];
  273. $this->post('/posts/securePost', $data);
  274. $this->assertResponseOk();
  275. $this->assertResponseContains('Request was accepted');
  276. }
  277. /**
  278. * Test posting to a secured form action action.
  279. *
  280. * @return void
  281. */
  282. public function testPostSecuredFormFailure()
  283. {
  284. $data = [
  285. 'title' => 'Some title',
  286. 'body' => 'Some text'
  287. ];
  288. $this->post('/posts/securePost', $data);
  289. $this->assertResponseError();
  290. }
  291. /**
  292. * Test that exceptions being thrown are handled correctly.
  293. *
  294. * @return void
  295. */
  296. public function testWithExpectedException()
  297. {
  298. $this->get('/tests_apps/throw_exception');
  299. $this->assertResponseCode(500);
  300. }
  301. /**
  302. * Test that exceptions being thrown are handled correctly.
  303. *
  304. * @expectedException PHPUnit_Framework_AssertionFailedError
  305. * @return void
  306. */
  307. public function testWithUnexpectedException()
  308. {
  309. $this->get('/tests_apps/throw_exception');
  310. $this->assertResponseCode(501);
  311. }
  312. /**
  313. * Test redirecting and integration tests.
  314. *
  315. * @return void
  316. */
  317. public function testRedirect()
  318. {
  319. $this->post('/tests_apps/redirect_to');
  320. $this->assertResponseSuccess();
  321. $this->assertResponseCode(302);
  322. }
  323. /**
  324. * Test redirecting and integration tests.
  325. *
  326. * @return void
  327. */
  328. public function testRedirectPermanent()
  329. {
  330. $this->post('/tests_apps/redirect_to_permanent');
  331. $this->assertResponseSuccess();
  332. $this->assertResponseCode(301);
  333. }
  334. /**
  335. * Test the responseOk status assertion
  336. *
  337. * @return void
  338. */
  339. public function testAssertResponseStatusCodes()
  340. {
  341. $this->_response = new Response();
  342. $this->_response->statusCode(200);
  343. $this->assertResponseOk();
  344. $this->_response->statusCode(201);
  345. $this->assertResponseOk();
  346. $this->_response->statusCode(204);
  347. $this->assertResponseOk();
  348. $this->_response->statusCode(202);
  349. $this->assertResponseSuccess();
  350. $this->_response->statusCode(302);
  351. $this->assertResponseSuccess();
  352. $this->_response->statusCode(400);
  353. $this->assertResponseError();
  354. $this->_response->statusCode(417);
  355. $this->assertResponseError();
  356. $this->_response->statusCode(500);
  357. $this->assertResponseFailure();
  358. $this->_response->statusCode(505);
  359. $this->assertResponseFailure();
  360. $this->_response->statusCode(301);
  361. $this->assertResponseCode(301);
  362. }
  363. /**
  364. * Test the location header assertion.
  365. *
  366. * @return void
  367. */
  368. public function testAssertRedirect()
  369. {
  370. $this->_response = new Response();
  371. $this->_response->header('Location', 'http://localhost/tasks/index');
  372. $this->assertRedirect();
  373. $this->assertRedirect('/tasks/index');
  374. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  375. $this->assertResponseEmpty();
  376. }
  377. /**
  378. * Test the location header assertion.
  379. *
  380. * @return void
  381. */
  382. public function testAssertNoRedirect()
  383. {
  384. $this->_response = new Response();
  385. $this->assertNoRedirect();
  386. }
  387. /**
  388. * Test the location header assertion.
  389. *
  390. * @return void
  391. */
  392. public function testAssertNoRedirectFail()
  393. {
  394. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  395. $result = $test->run();
  396. ob_start();
  397. $this->assertFalse($result->wasSuccessful());
  398. $this->assertEquals(1, $result->failureCount());
  399. }
  400. /**
  401. * Test the location header assertion string contains
  402. *
  403. * @return void
  404. */
  405. public function testAssertRedirectContains()
  406. {
  407. $this->_response = new Response();
  408. $this->_response->header('Location', 'http://localhost/tasks/index');
  409. $this->assertRedirectContains('/tasks/index');
  410. }
  411. /**
  412. * Test the header assertion.
  413. *
  414. * @return void
  415. */
  416. public function testAssertHeader()
  417. {
  418. $this->_response = new Response();
  419. $this->_response->header('Etag', 'abc123');
  420. $this->assertHeader('Etag', 'abc123');
  421. }
  422. /**
  423. * Test the header contains assertion.
  424. *
  425. * @return void
  426. */
  427. public function testAssertHeaderContains()
  428. {
  429. $this->_response = new Response();
  430. $this->_response->header('Etag', 'abc123');
  431. $this->assertHeaderContains('Etag', 'abc');
  432. }
  433. /**
  434. * Test the content type assertion.
  435. *
  436. * @return void
  437. */
  438. public function testAssertContentType()
  439. {
  440. $this->_response = new Response();
  441. $this->_response->type('json');
  442. $this->assertContentType('json');
  443. $this->assertContentType('application/json');
  444. }
  445. /**
  446. * Test that type() in an action sets the content-type header.
  447. *
  448. * @return void
  449. */
  450. public function testContentTypeInAction()
  451. {
  452. $this->get('/tests_apps/set_type');
  453. $this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
  454. $this->assertContentType('json');
  455. $this->assertContentType('application/json');
  456. }
  457. /**
  458. * Test the content assertion.
  459. *
  460. * @return void
  461. */
  462. public function testAssertResponseContains()
  463. {
  464. $this->_response = new Response();
  465. $this->_response->body('Some content');
  466. $this->assertResponseContains('content');
  467. }
  468. /**
  469. * Test the negated content assertion.
  470. *
  471. * @return void
  472. */
  473. public function testAssertResponseNotContains()
  474. {
  475. $this->_response = new Response();
  476. $this->_response->body('Some content');
  477. $this->assertResponseNotContains('contents');
  478. }
  479. /**
  480. * Test that works in tandem with testEventManagerReset2 to
  481. * test the EventManager reset.
  482. *
  483. * The return value is passed to testEventManagerReset2 as
  484. * an arguments.
  485. *
  486. * @return \Cake\Event\EventManager
  487. */
  488. public function testEventManagerReset1()
  489. {
  490. return EventManager::instance();
  491. }
  492. /**
  493. * Test if the EventManager is reset between tests.
  494. *
  495. * @depends testEventManagerReset1
  496. * @return void
  497. */
  498. public function testEventManagerReset2($prevEventManager)
  499. {
  500. $this->assertNotSame($prevEventManager, EventManager::instance());
  501. }
  502. /**
  503. * Test sending file in requests.
  504. *
  505. * @return void
  506. */
  507. public function testSendFile()
  508. {
  509. $this->get('/posts/file');
  510. $this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
  511. }
  512. /**
  513. * Test that assertFile requires a response
  514. *
  515. * @expectedException PHPUnit_Framework_AssertionFailedError
  516. * @expectedExceptionMessage No response set, cannot assert file
  517. * @return void
  518. */
  519. public function testAssertFileNoReponse()
  520. {
  521. $this->assertFileResponse('foo');
  522. }
  523. /**
  524. * Test that assertFile requires a file
  525. *
  526. * @expectedException PHPUnit_Framework_AssertionFailedError
  527. * @expectedExceptionMessage No file was sent in this response
  528. * @return void
  529. */
  530. public function testAssertFileNoFile()
  531. {
  532. $this->get('/posts/get');
  533. $this->assertFileResponse('foo');
  534. }
  535. }