IntegrationTestCaseTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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 action.
  284. *
  285. * @return void
  286. */
  287. public function testPostSecuredFormFailure()
  288. {
  289. $data = [
  290. 'title' => 'Some title',
  291. 'body' => 'Some text'
  292. ];
  293. $this->post('/posts/securePost', $data);
  294. $this->assertResponseError();
  295. }
  296. /**
  297. * Test that exceptions being thrown are handled correctly.
  298. *
  299. * @return void
  300. */
  301. public function testWithExpectedException()
  302. {
  303. $this->get('/tests_apps/throw_exception');
  304. $this->assertResponseCode(500);
  305. }
  306. /**
  307. * Test that exceptions being thrown are handled correctly.
  308. *
  309. * @expectedException PHPUnit_Framework_AssertionFailedError
  310. * @return void
  311. */
  312. public function testWithUnexpectedException()
  313. {
  314. $this->get('/tests_apps/throw_exception');
  315. $this->assertResponseCode(501);
  316. }
  317. /**
  318. * Test redirecting and integration tests.
  319. *
  320. * @return void
  321. */
  322. public function testRedirect()
  323. {
  324. $this->post('/tests_apps/redirect_to');
  325. $this->assertResponseSuccess();
  326. $this->assertResponseCode(302);
  327. }
  328. /**
  329. * Test redirecting and integration tests.
  330. *
  331. * @return void
  332. */
  333. public function testRedirectPermanent()
  334. {
  335. $this->post('/tests_apps/redirect_to_permanent');
  336. $this->assertResponseSuccess();
  337. $this->assertResponseCode(301);
  338. }
  339. /**
  340. * Test the responseOk status assertion
  341. *
  342. * @return void
  343. */
  344. public function testAssertResponseStatusCodes()
  345. {
  346. $this->_response = new Response();
  347. $this->_response->statusCode(200);
  348. $this->assertResponseOk();
  349. $this->_response->statusCode(201);
  350. $this->assertResponseOk();
  351. $this->_response->statusCode(204);
  352. $this->assertResponseOk();
  353. $this->_response->statusCode(202);
  354. $this->assertResponseSuccess();
  355. $this->_response->statusCode(302);
  356. $this->assertResponseSuccess();
  357. $this->_response->statusCode(400);
  358. $this->assertResponseError();
  359. $this->_response->statusCode(417);
  360. $this->assertResponseError();
  361. $this->_response->statusCode(500);
  362. $this->assertResponseFailure();
  363. $this->_response->statusCode(505);
  364. $this->assertResponseFailure();
  365. $this->_response->statusCode(301);
  366. $this->assertResponseCode(301);
  367. }
  368. /**
  369. * Test the location header assertion.
  370. *
  371. * @return void
  372. */
  373. public function testAssertRedirect()
  374. {
  375. $this->_response = new Response();
  376. $this->_response->header('Location', 'http://localhost/tasks/index');
  377. $this->assertRedirect();
  378. $this->assertRedirect('/tasks/index');
  379. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  380. $this->assertResponseEmpty();
  381. }
  382. /**
  383. * Test the location header assertion.
  384. *
  385. * @return void
  386. */
  387. public function testAssertNoRedirect()
  388. {
  389. $this->_response = new Response();
  390. $this->assertNoRedirect();
  391. }
  392. /**
  393. * Test the location header assertion.
  394. *
  395. * @return void
  396. */
  397. public function testAssertNoRedirectFail()
  398. {
  399. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  400. $result = $test->run();
  401. ob_start();
  402. $this->assertFalse($result->wasSuccessful());
  403. $this->assertEquals(1, $result->failureCount());
  404. }
  405. /**
  406. * Test the location header assertion string contains
  407. *
  408. * @return void
  409. */
  410. public function testAssertRedirectContains()
  411. {
  412. $this->_response = new Response();
  413. $this->_response->header('Location', 'http://localhost/tasks/index');
  414. $this->assertRedirectContains('/tasks/index');
  415. }
  416. /**
  417. * Test the header assertion.
  418. *
  419. * @return void
  420. */
  421. public function testAssertHeader()
  422. {
  423. $this->_response = new Response();
  424. $this->_response->header('Etag', 'abc123');
  425. $this->assertHeader('Etag', 'abc123');
  426. }
  427. /**
  428. * Test the header contains assertion.
  429. *
  430. * @return void
  431. */
  432. public function testAssertHeaderContains()
  433. {
  434. $this->_response = new Response();
  435. $this->_response->header('Etag', 'abc123');
  436. $this->assertHeaderContains('Etag', 'abc');
  437. }
  438. /**
  439. * Test the content type assertion.
  440. *
  441. * @return void
  442. */
  443. public function testAssertContentType()
  444. {
  445. $this->_response = new Response();
  446. $this->_response->type('json');
  447. $this->assertContentType('json');
  448. $this->assertContentType('application/json');
  449. }
  450. /**
  451. * Test that type() in an action sets the content-type header.
  452. *
  453. * @return void
  454. */
  455. public function testContentTypeInAction()
  456. {
  457. $this->get('/tests_apps/set_type');
  458. $this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
  459. $this->assertContentType('json');
  460. $this->assertContentType('application/json');
  461. }
  462. /**
  463. * Test the content assertion.
  464. *
  465. * @return void
  466. */
  467. public function testAssertResponseContains()
  468. {
  469. $this->_response = new Response();
  470. $this->_response->body('Some content');
  471. $this->assertResponseContains('content');
  472. }
  473. /**
  474. * Test the negated content assertion.
  475. *
  476. * @return void
  477. */
  478. public function testAssertResponseNotContains()
  479. {
  480. $this->_response = new Response();
  481. $this->_response->body('Some content');
  482. $this->assertResponseNotContains('contents');
  483. }
  484. /**
  485. * Test that works in tandem with testEventManagerReset2 to
  486. * test the EventManager reset.
  487. *
  488. * The return value is passed to testEventManagerReset2 as
  489. * an arguments.
  490. *
  491. * @return \Cake\Event\EventManager
  492. */
  493. public function testEventManagerReset1()
  494. {
  495. return EventManager::instance();
  496. }
  497. /**
  498. * Test if the EventManager is reset between tests.
  499. *
  500. * @depends testEventManagerReset1
  501. * @return void
  502. */
  503. public function testEventManagerReset2($prevEventManager)
  504. {
  505. $this->assertNotSame($prevEventManager, EventManager::instance());
  506. }
  507. /**
  508. * Test sending file in requests.
  509. *
  510. * @return void
  511. */
  512. public function testSendFile()
  513. {
  514. $this->get('/posts/file');
  515. $this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
  516. }
  517. /**
  518. * Test that assertFile requires a response
  519. *
  520. * @expectedException PHPUnit_Framework_AssertionFailedError
  521. * @expectedExceptionMessage No response set, cannot assert file
  522. * @return void
  523. */
  524. public function testAssertFileNoReponse()
  525. {
  526. $this->assertFileResponse('foo');
  527. }
  528. /**
  529. * Test that assertFile requires a file
  530. *
  531. * @expectedException PHPUnit_Framework_AssertionFailedError
  532. * @expectedExceptionMessage No file was sent in this response
  533. * @return void
  534. */
  535. public function testAssertFileNoFile()
  536. {
  537. $this->get('/posts/get');
  538. $this->assertFileResponse('foo');
  539. }
  540. }