IntegrationTestCaseTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. /**
  24. * Self test of the IntegrationTestCase
  25. */
  26. class IntegrationTestCaseTest extends IntegrationTestCase
  27. {
  28. /**
  29. * Setup method
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. Configure::write('App.namespace', 'TestApp');
  37. Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
  38. DispatcherFactory::clear();
  39. DispatcherFactory::add('Routing');
  40. DispatcherFactory::add('ControllerFactory');
  41. }
  42. /**
  43. * Test building a request.
  44. *
  45. * @return void
  46. */
  47. public function testRequestBuilding()
  48. {
  49. $this->configRequest([
  50. 'headers' => ['X-CSRF-Token' => 'abc123'],
  51. 'base' => '',
  52. 'webroot' => '/',
  53. 'environment' => [
  54. 'PHP_AUTH_USER' => 'foo',
  55. 'PHP_AUTH_PW' => 'bar'
  56. ]
  57. ]);
  58. $this->cookie('split_token', 'def345');
  59. $this->session(['User' => ['id' => 1, 'username' => 'mark']]);
  60. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  61. $this->assertEquals('abc123', $request->header('X-CSRF-Token'));
  62. $this->assertEquals('tasks/add', $request->url);
  63. $this->assertArrayHasKey('split_token', $request->cookies);
  64. $this->assertEquals('def345', $request->cookies['split_token']);
  65. $this->assertEquals(['id' => '1', 'username' => 'mark'], $request->session()->read('User'));
  66. $this->assertEquals('foo', $request->env('PHP_AUTH_USER'));
  67. $this->assertEquals('bar', $request->env('PHP_AUTH_PW'));
  68. }
  69. /**
  70. * Test request building adds csrf tokens
  71. *
  72. * @return void
  73. */
  74. public function testRequestBuildingCsrfTokens()
  75. {
  76. $this->enableCsrfToken();
  77. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  78. $this->assertArrayHasKey('csrfToken', $request->cookies);
  79. $this->assertArrayHasKey('_csrfToken', $request->data);
  80. $this->assertSame($request->cookies['csrfToken'], $request->data['_csrfToken']);
  81. $this->cookie('csrfToken', '');
  82. $request = $this->_buildRequest('/tasks/add', 'POST', [
  83. '_csrfToken' => 'fale',
  84. 'title' => 'First post'
  85. ]);
  86. $this->assertSame('', $request->cookies['csrfToken']);
  87. $this->assertSame('fale', $request->data['_csrfToken']);
  88. }
  89. /**
  90. * Test multiple actions using CSRF tokens don't fail
  91. *
  92. * @return void
  93. */
  94. public function testEnableCsrfMultipleRequests()
  95. {
  96. $this->enableCsrfToken();
  97. $first = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  98. $second = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'Second post']);
  99. $this->assertSame($first->cookies['csrfToken'], $second->data['_csrfToken'], 'Csrf token should match cookie');
  100. $this->assertSame(
  101. $first->data['_csrfToken'],
  102. $second->data['_csrfToken'],
  103. 'Tokens should be consistent per test method'
  104. );
  105. }
  106. /**
  107. * Test pre-determined CSRF tokens.
  108. *
  109. * @return void
  110. */
  111. public function testEnableCsrfPredeterminedCookie()
  112. {
  113. $this->enableCsrfToken();
  114. $value = 'I am a teapot';
  115. $this->cookie('csrfToken', $value);
  116. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  117. $this->assertSame($value, $request->cookies['csrfToken'], 'Csrf token should match cookie');
  118. $this->assertSame($value, $request->data['_csrfToken'], 'Tokens should match');
  119. }
  120. /**
  121. * Test building a request, with query parameters
  122. *
  123. * @return void
  124. */
  125. public function testRequestBuildingQueryParameters()
  126. {
  127. $request = $this->_buildRequest('/tasks/view?archived=yes', 'GET', []);
  128. $this->assertEquals('/tasks/view?archived=yes', $request->here());
  129. $this->assertEquals('yes', $request->query('archived'));
  130. }
  131. /**
  132. * Test sending get requests.
  133. *
  134. * @return void
  135. */
  136. public function testGet()
  137. {
  138. $this->assertNull($this->_response);
  139. $this->get('/request_action/test_request_action');
  140. $this->assertNotEmpty($this->_response);
  141. $this->assertInstanceOf('Cake\Network\Response', $this->_response);
  142. $this->assertEquals('This is a test', $this->_response->body());
  143. }
  144. /**
  145. * Test sending requests stores references to controller/view/layout.
  146. *
  147. * @return void
  148. */
  149. public function testRequestSetsProperties()
  150. {
  151. $this->post('/posts/index');
  152. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  153. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  154. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  155. $this->assertTemplate('index');
  156. $this->assertLayout('default');
  157. $this->assertEquals('value', $this->viewVariable('test'));
  158. }
  159. /**
  160. * Assert that the stored template doesn't change when cells are rendered.
  161. *
  162. * @return void
  163. */
  164. public function testAssertTemplateAfterCellRender()
  165. {
  166. $this->get('/posts/get');
  167. $this->assertContains('Template' . DS . 'Posts' . DS . 'get.ctp', $this->_viewName);
  168. $this->assertTemplate('get');
  169. $this->assertResponseContains('cellcontent');
  170. }
  171. /**
  172. * Test array URLs
  173. *
  174. * @return void
  175. */
  176. public function testArrayUrls()
  177. {
  178. $this->post(['controller' => 'Posts', 'action' => 'index']);
  179. $this->assertEquals('value', $this->viewVariable('test'));
  180. }
  181. /**
  182. * Test flash and cookie assertions
  183. *
  184. * @return void
  185. */
  186. public function testFlashSessionAndCookieAsserts()
  187. {
  188. $this->post('/posts/index');
  189. $this->assertSession('An error message', 'Flash.flash.0.message');
  190. $this->assertCookie(1, 'remember_me');
  191. }
  192. /**
  193. * Test error handling and error page rendering.
  194. *
  195. * @return void
  196. */
  197. public function testPostAndErrorHandling()
  198. {
  199. $this->post('/request_action/error_method');
  200. $this->assertResponseNotEmpty();
  201. $this->assertResponseContains('Not there or here');
  202. $this->assertResponseContains('<!DOCTYPE html>');
  203. }
  204. /**
  205. * Test posting to a secured form action action.
  206. *
  207. * @return void
  208. */
  209. public function testPostSecuredForm()
  210. {
  211. $this->enableSecurityToken();
  212. $data = [
  213. 'title' => 'Some title',
  214. 'body' => 'Some text'
  215. ];
  216. $this->post('/posts/securePost', $data);
  217. $this->assertResponseOk();
  218. $this->assertResponseContains('Request was accepted');
  219. }
  220. /**
  221. * Test posting to a secured form action action.
  222. *
  223. * @return void
  224. */
  225. public function testPostSecuredFormFailure()
  226. {
  227. $data = [
  228. 'title' => 'Some title',
  229. 'body' => 'Some text'
  230. ];
  231. $this->post('/posts/securePost', $data);
  232. $this->assertResponseError();
  233. }
  234. /**
  235. * Test that exceptions being thrown are handled correctly.
  236. *
  237. * @return void
  238. */
  239. public function testWithExpectedException()
  240. {
  241. $this->get('/tests_apps/throw_exception');
  242. $this->assertResponseCode(500);
  243. }
  244. /**
  245. * Test that exceptions being thrown are handled correctly.
  246. *
  247. * @expectedException PHPUnit_Framework_AssertionFailedError
  248. * @return void
  249. */
  250. public function testWithUnexpectedException()
  251. {
  252. $this->get('/tests_apps/throw_exception');
  253. $this->assertResponseCode(501);
  254. }
  255. /**
  256. * Test redirecting and integration tests.
  257. *
  258. * @return void
  259. */
  260. public function testRedirect()
  261. {
  262. $this->post('/tests_apps/redirect_to');
  263. $this->assertResponseSuccess();
  264. $this->assertResponseCode(302);
  265. }
  266. /**
  267. * Test redirecting and integration tests.
  268. *
  269. * @return void
  270. */
  271. public function testRedirectPermanent()
  272. {
  273. $this->post('/tests_apps/redirect_to_permanent');
  274. $this->assertResponseSuccess();
  275. $this->assertResponseCode(301);
  276. }
  277. /**
  278. * Test the responseOk status assertion
  279. *
  280. * @return void
  281. */
  282. public function testAssertResponseStatusCodes()
  283. {
  284. $this->_response = new Response();
  285. $this->_response->statusCode(200);
  286. $this->assertResponseOk();
  287. $this->_response->statusCode(201);
  288. $this->assertResponseOk();
  289. $this->_response->statusCode(204);
  290. $this->assertResponseOk();
  291. $this->_response->statusCode(202);
  292. $this->assertResponseSuccess();
  293. $this->_response->statusCode(302);
  294. $this->assertResponseSuccess();
  295. $this->_response->statusCode(400);
  296. $this->assertResponseError();
  297. $this->_response->statusCode(417);
  298. $this->assertResponseError();
  299. $this->_response->statusCode(500);
  300. $this->assertResponseFailure();
  301. $this->_response->statusCode(505);
  302. $this->assertResponseFailure();
  303. $this->_response->statusCode(301);
  304. $this->assertResponseCode(301);
  305. }
  306. /**
  307. * Test the location header assertion.
  308. *
  309. * @return void
  310. */
  311. public function testAssertRedirect()
  312. {
  313. $this->_response = new Response();
  314. $this->_response->header('Location', 'http://localhost/tasks/index');
  315. $this->assertRedirect();
  316. $this->assertRedirect('/tasks/index');
  317. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  318. $this->assertResponseEmpty();
  319. }
  320. /**
  321. * Test the location header assertion.
  322. *
  323. * @return void
  324. */
  325. public function testAssertNoRedirect()
  326. {
  327. $this->_response = new Response();
  328. $this->assertNoRedirect();
  329. }
  330. /**
  331. * Test the location header assertion.
  332. *
  333. * @return void
  334. */
  335. public function testAssertNoRedirectFail()
  336. {
  337. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  338. $result = $test->run();
  339. ob_start();
  340. $this->assertFalse($result->wasSuccessful());
  341. $this->assertEquals(1, $result->failureCount());
  342. }
  343. /**
  344. * Test the location header assertion string contains
  345. *
  346. * @return void
  347. */
  348. public function testAssertRedirectContains()
  349. {
  350. $this->_response = new Response();
  351. $this->_response->header('Location', 'http://localhost/tasks/index');
  352. $this->assertRedirectContains('/tasks/index');
  353. }
  354. /**
  355. * Test the header assertion.
  356. *
  357. * @return void
  358. */
  359. public function testAssertHeader()
  360. {
  361. $this->_response = new Response();
  362. $this->_response->header('Etag', 'abc123');
  363. $this->assertHeader('Etag', 'abc123');
  364. }
  365. /**
  366. * Test the content type assertion.
  367. *
  368. * @return void
  369. */
  370. public function testAssertContentType()
  371. {
  372. $this->_response = new Response();
  373. $this->_response->type('json');
  374. $this->assertContentType('json');
  375. $this->assertContentType('application/json');
  376. }
  377. /**
  378. * Test that type() in an action sets the content-type header.
  379. *
  380. * @return void
  381. */
  382. public function testContentTypeInAction()
  383. {
  384. $this->get('/tests_apps/set_type');
  385. $this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
  386. $this->assertContentType('json');
  387. $this->assertContentType('application/json');
  388. }
  389. /**
  390. * Test the content assertion.
  391. *
  392. * @return void
  393. */
  394. public function testAssertResponseContains()
  395. {
  396. $this->_response = new Response();
  397. $this->_response->body('Some content');
  398. $this->assertResponseContains('content');
  399. }
  400. /**
  401. * Test the negated content assertion.
  402. *
  403. * @return void
  404. */
  405. public function testAssertResponseNotContains()
  406. {
  407. $this->_response = new Response();
  408. $this->_response->body('Some content');
  409. $this->assertResponseNotContains('contents');
  410. }
  411. /**
  412. * Test that works in tandem with testEventManagerReset2 to
  413. * test the EventManager reset.
  414. *
  415. * The return value is passed to testEventManagerReset2 as
  416. * an arguments.
  417. *
  418. * @return \Cake\Event\EventManager
  419. */
  420. public function testEventManagerReset1()
  421. {
  422. return EventManager::instance();
  423. }
  424. /**
  425. * Test if the EventManager is reset between tests.
  426. *
  427. * @depends testEventManagerReset1
  428. * @return void
  429. */
  430. public function testEventManagerReset2($prevEventManager)
  431. {
  432. $this->assertNotSame($prevEventManager, EventManager::instance());
  433. }
  434. }