IntegrationTestCaseTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. }
  205. /**
  206. * Test error handling and error page rendering.
  207. *
  208. * @return void
  209. */
  210. public function testPostAndErrorHandling()
  211. {
  212. $this->post('/request_action/error_method');
  213. $this->assertResponseNotEmpty();
  214. $this->assertResponseContains('Not there or here');
  215. $this->assertResponseContains('<!DOCTYPE html>');
  216. }
  217. /**
  218. * Test posting to a secured form action action.
  219. *
  220. * @return void
  221. */
  222. public function testPostSecuredForm()
  223. {
  224. $this->enableSecurityToken();
  225. $data = [
  226. 'title' => 'Some title',
  227. 'body' => 'Some text'
  228. ];
  229. $this->post('/posts/securePost', $data);
  230. $this->assertResponseOk();
  231. $this->assertResponseContains('Request was accepted');
  232. }
  233. /**
  234. * Test posting to a secured form action action.
  235. *
  236. * @return void
  237. */
  238. public function testPostSecuredFormFailure()
  239. {
  240. $data = [
  241. 'title' => 'Some title',
  242. 'body' => 'Some text'
  243. ];
  244. $this->post('/posts/securePost', $data);
  245. $this->assertResponseError();
  246. }
  247. /**
  248. * Test that exceptions being thrown are handled correctly.
  249. *
  250. * @return void
  251. */
  252. public function testWithExpectedException()
  253. {
  254. $this->get('/tests_apps/throw_exception');
  255. $this->assertResponseCode(500);
  256. }
  257. /**
  258. * Test that exceptions being thrown are handled correctly.
  259. *
  260. * @expectedException PHPUnit_Framework_AssertionFailedError
  261. * @return void
  262. */
  263. public function testWithUnexpectedException()
  264. {
  265. $this->get('/tests_apps/throw_exception');
  266. $this->assertResponseCode(501);
  267. }
  268. /**
  269. * Test redirecting and integration tests.
  270. *
  271. * @return void
  272. */
  273. public function testRedirect()
  274. {
  275. $this->post('/tests_apps/redirect_to');
  276. $this->assertResponseSuccess();
  277. $this->assertResponseCode(302);
  278. }
  279. /**
  280. * Test redirecting and integration tests.
  281. *
  282. * @return void
  283. */
  284. public function testRedirectPermanent()
  285. {
  286. $this->post('/tests_apps/redirect_to_permanent');
  287. $this->assertResponseSuccess();
  288. $this->assertResponseCode(301);
  289. }
  290. /**
  291. * Test the responseOk status assertion
  292. *
  293. * @return void
  294. */
  295. public function testAssertResponseStatusCodes()
  296. {
  297. $this->_response = new Response();
  298. $this->_response->statusCode(200);
  299. $this->assertResponseOk();
  300. $this->_response->statusCode(201);
  301. $this->assertResponseOk();
  302. $this->_response->statusCode(204);
  303. $this->assertResponseOk();
  304. $this->_response->statusCode(202);
  305. $this->assertResponseSuccess();
  306. $this->_response->statusCode(302);
  307. $this->assertResponseSuccess();
  308. $this->_response->statusCode(400);
  309. $this->assertResponseError();
  310. $this->_response->statusCode(417);
  311. $this->assertResponseError();
  312. $this->_response->statusCode(500);
  313. $this->assertResponseFailure();
  314. $this->_response->statusCode(505);
  315. $this->assertResponseFailure();
  316. $this->_response->statusCode(301);
  317. $this->assertResponseCode(301);
  318. }
  319. /**
  320. * Test the location header assertion.
  321. *
  322. * @return void
  323. */
  324. public function testAssertRedirect()
  325. {
  326. $this->_response = new Response();
  327. $this->_response->header('Location', 'http://localhost/tasks/index');
  328. $this->assertRedirect();
  329. $this->assertRedirect('/tasks/index');
  330. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  331. $this->assertResponseEmpty();
  332. }
  333. /**
  334. * Test the location header assertion.
  335. *
  336. * @return void
  337. */
  338. public function testAssertNoRedirect()
  339. {
  340. $this->_response = new Response();
  341. $this->assertNoRedirect();
  342. }
  343. /**
  344. * Test the location header assertion.
  345. *
  346. * @return void
  347. */
  348. public function testAssertNoRedirectFail()
  349. {
  350. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  351. $result = $test->run();
  352. ob_start();
  353. $this->assertFalse($result->wasSuccessful());
  354. $this->assertEquals(1, $result->failureCount());
  355. }
  356. /**
  357. * Test the location header assertion string contains
  358. *
  359. * @return void
  360. */
  361. public function testAssertRedirectContains()
  362. {
  363. $this->_response = new Response();
  364. $this->_response->header('Location', 'http://localhost/tasks/index');
  365. $this->assertRedirectContains('/tasks/index');
  366. }
  367. /**
  368. * Test the header assertion.
  369. *
  370. * @return void
  371. */
  372. public function testAssertHeader()
  373. {
  374. $this->_response = new Response();
  375. $this->_response->header('Etag', 'abc123');
  376. $this->assertHeader('Etag', 'abc123');
  377. }
  378. /**
  379. * Test the content type assertion.
  380. *
  381. * @return void
  382. */
  383. public function testAssertContentType()
  384. {
  385. $this->_response = new Response();
  386. $this->_response->type('json');
  387. $this->assertContentType('json');
  388. $this->assertContentType('application/json');
  389. }
  390. /**
  391. * Test that type() in an action sets the content-type header.
  392. *
  393. * @return void
  394. */
  395. public function testContentTypeInAction()
  396. {
  397. $this->get('/tests_apps/set_type');
  398. $this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
  399. $this->assertContentType('json');
  400. $this->assertContentType('application/json');
  401. }
  402. /**
  403. * Test the content assertion.
  404. *
  405. * @return void
  406. */
  407. public function testAssertResponseContains()
  408. {
  409. $this->_response = new Response();
  410. $this->_response->body('Some content');
  411. $this->assertResponseContains('content');
  412. }
  413. /**
  414. * Test the negated content assertion.
  415. *
  416. * @return void
  417. */
  418. public function testAssertResponseNotContains()
  419. {
  420. $this->_response = new Response();
  421. $this->_response->body('Some content');
  422. $this->assertResponseNotContains('contents');
  423. }
  424. /**
  425. * Test that works in tandem with testEventManagerReset2 to
  426. * test the EventManager reset.
  427. *
  428. * The return value is passed to testEventManagerReset2 as
  429. * an arguments.
  430. *
  431. * @return \Cake\Event\EventManager
  432. */
  433. public function testEventManagerReset1()
  434. {
  435. return EventManager::instance();
  436. }
  437. /**
  438. * Test if the EventManager is reset between tests.
  439. *
  440. * @depends testEventManagerReset1
  441. * @return void
  442. */
  443. public function testEventManagerReset2($prevEventManager)
  444. {
  445. $this->assertNotSame($prevEventManager, EventManager::instance());
  446. }
  447. }