IntegrationTestCaseTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 building a request, with query parameters
  91. *
  92. * @return void
  93. */
  94. public function testRequestBuildingQueryParameters()
  95. {
  96. $request = $this->_buildRequest('/tasks/view?archived=yes', 'GET', []);
  97. $this->assertEquals('/tasks/view?archived=yes', $request->here());
  98. $this->assertEquals('yes', $request->query('archived'));
  99. }
  100. /**
  101. * Test sending get requests.
  102. *
  103. * @return void
  104. */
  105. public function testGet()
  106. {
  107. $this->assertNull($this->_response);
  108. $this->get('/request_action/test_request_action');
  109. $this->assertNotEmpty($this->_response);
  110. $this->assertInstanceOf('Cake\Network\Response', $this->_response);
  111. $this->assertEquals('This is a test', $this->_response->body());
  112. }
  113. /**
  114. * Test sending requests stores references to controller/view/layout.
  115. *
  116. * @return void
  117. */
  118. public function testRequestSetsProperties()
  119. {
  120. $this->post('/posts/index');
  121. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  122. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  123. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  124. $this->assertTemplate('index');
  125. $this->assertLayout('default');
  126. $this->assertEquals('value', $this->viewVariable('test'));
  127. }
  128. /**
  129. * Assert that the stored template doesn't change when cells are rendered.
  130. *
  131. * @return void
  132. */
  133. public function testAssertTemplateAfterCellRender()
  134. {
  135. $this->get('/posts/get');
  136. $this->assertContains('Template' . DS . 'Posts' . DS . 'get.ctp', $this->_viewName);
  137. $this->assertTemplate('get');
  138. $this->assertResponseContains('cellcontent');
  139. }
  140. /**
  141. * Test array URLs
  142. *
  143. * @return void
  144. */
  145. public function testArrayUrls()
  146. {
  147. $this->post(['controller' => 'Posts', 'action' => 'index']);
  148. $this->assertEquals('value', $this->viewVariable('test'));
  149. }
  150. /**
  151. * Test flash and cookie assertions
  152. *
  153. * @return void
  154. */
  155. public function testFlashSessionAndCookieAsserts()
  156. {
  157. $this->post('/posts/index');
  158. $this->assertSession('An error message', 'Flash.flash.0.message');
  159. $this->assertCookie(1, 'remember_me');
  160. }
  161. /**
  162. * Test error handling and error page rendering.
  163. *
  164. * @return void
  165. */
  166. public function testPostAndErrorHandling()
  167. {
  168. $this->post('/request_action/error_method');
  169. $this->assertResponseNotEmpty();
  170. $this->assertResponseContains('Not there or here');
  171. $this->assertResponseContains('<!DOCTYPE html>');
  172. }
  173. /**
  174. * Test posting to a secured form action action.
  175. *
  176. * @return void
  177. */
  178. public function testPostSecuredForm()
  179. {
  180. $this->enableSecurityToken();
  181. $data = [
  182. 'title' => 'Some title',
  183. 'body' => 'Some text'
  184. ];
  185. $this->post('/posts/securePost', $data);
  186. $this->assertResponseOk();
  187. $this->assertResponseContains('Request was accepted');
  188. }
  189. /**
  190. * Test posting to a secured form action action.
  191. *
  192. * @return void
  193. */
  194. public function testPostSecuredFormFailure()
  195. {
  196. $data = [
  197. 'title' => 'Some title',
  198. 'body' => 'Some text'
  199. ];
  200. $this->post('/posts/securePost', $data);
  201. $this->assertResponseError();
  202. }
  203. /**
  204. * Test that exceptions being thrown are handled correctly.
  205. *
  206. * @return void
  207. */
  208. public function testWithExpectedException()
  209. {
  210. $this->get('/tests_apps/throw_exception');
  211. $this->assertResponseCode(500);
  212. }
  213. /**
  214. * Test that exceptions being thrown are handled correctly.
  215. *
  216. * @expectedException PHPUnit_Framework_AssertionFailedError
  217. * @return void
  218. */
  219. public function testWithUnexpectedException()
  220. {
  221. $this->get('/tests_apps/throw_exception');
  222. $this->assertResponseCode(501);
  223. }
  224. /**
  225. * Test redirecting and integration tests.
  226. *
  227. * @return void
  228. */
  229. public function testRedirect()
  230. {
  231. $this->post('/tests_apps/redirect_to');
  232. $this->assertResponseSuccess();
  233. $this->assertResponseCode(302);
  234. }
  235. /**
  236. * Test redirecting and integration tests.
  237. *
  238. * @return void
  239. */
  240. public function testRedirectPermanent()
  241. {
  242. $this->post('/tests_apps/redirect_to_permanent');
  243. $this->assertResponseSuccess();
  244. $this->assertResponseCode(301);
  245. }
  246. /**
  247. * Test the responseOk status assertion
  248. *
  249. * @return void
  250. */
  251. public function testAssertResponseStatusCodes()
  252. {
  253. $this->_response = new Response();
  254. $this->_response->statusCode(200);
  255. $this->assertResponseOk();
  256. $this->_response->statusCode(201);
  257. $this->assertResponseOk();
  258. $this->_response->statusCode(204);
  259. $this->assertResponseOk();
  260. $this->_response->statusCode(202);
  261. $this->assertResponseSuccess();
  262. $this->_response->statusCode(302);
  263. $this->assertResponseSuccess();
  264. $this->_response->statusCode(400);
  265. $this->assertResponseError();
  266. $this->_response->statusCode(417);
  267. $this->assertResponseError();
  268. $this->_response->statusCode(500);
  269. $this->assertResponseFailure();
  270. $this->_response->statusCode(505);
  271. $this->assertResponseFailure();
  272. $this->_response->statusCode(301);
  273. $this->assertResponseCode(301);
  274. }
  275. /**
  276. * Test the location header assertion.
  277. *
  278. * @return void
  279. */
  280. public function testAssertRedirect()
  281. {
  282. $this->_response = new Response();
  283. $this->_response->header('Location', 'http://localhost/tasks/index');
  284. $this->assertRedirect();
  285. $this->assertRedirect('/tasks/index');
  286. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  287. $this->assertResponseEmpty();
  288. }
  289. /**
  290. * Test the location header assertion.
  291. *
  292. * @return void
  293. */
  294. public function testAssertNoRedirect()
  295. {
  296. $this->_response = new Response();
  297. $this->assertNoRedirect();
  298. }
  299. /**
  300. * Test the location header assertion.
  301. *
  302. * @return void
  303. */
  304. public function testAssertNoRedirectFail()
  305. {
  306. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  307. $result = $test->run();
  308. ob_start();
  309. $this->assertFalse($result->wasSuccessful());
  310. $this->assertEquals(1, $result->failureCount());
  311. }
  312. /**
  313. * Test the location header assertion string contains
  314. *
  315. * @return void
  316. */
  317. public function testAssertRedirectContains()
  318. {
  319. $this->_response = new Response();
  320. $this->_response->header('Location', 'http://localhost/tasks/index');
  321. $this->assertRedirectContains('/tasks/index');
  322. }
  323. /**
  324. * Test the header assertion.
  325. *
  326. * @return void
  327. */
  328. public function testAssertHeader()
  329. {
  330. $this->_response = new Response();
  331. $this->_response->header('Etag', 'abc123');
  332. $this->assertHeader('Etag', 'abc123');
  333. }
  334. /**
  335. * Test the content type assertion.
  336. *
  337. * @return void
  338. */
  339. public function testAssertContentType()
  340. {
  341. $this->_response = new Response();
  342. $this->_response->type('json');
  343. $this->assertContentType('json');
  344. $this->assertContentType('application/json');
  345. }
  346. /**
  347. * Test that type() in an action sets the content-type header.
  348. *
  349. * @return void
  350. */
  351. public function testContentTypeInAction()
  352. {
  353. $this->get('/tests_apps/set_type');
  354. $this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
  355. $this->assertContentType('json');
  356. $this->assertContentType('application/json');
  357. }
  358. /**
  359. * Test the content assertion.
  360. *
  361. * @return void
  362. */
  363. public function testAssertResponseContains()
  364. {
  365. $this->_response = new Response();
  366. $this->_response->body('Some content');
  367. $this->assertResponseContains('content');
  368. }
  369. /**
  370. * Test the negated content assertion.
  371. *
  372. * @return void
  373. */
  374. public function testAssertResponseNotContains()
  375. {
  376. $this->_response = new Response();
  377. $this->_response->body('Some content');
  378. $this->assertResponseNotContains('contents');
  379. }
  380. /**
  381. * Test that works in tandem with testEventManagerReset2 to
  382. * test the EventManager reset.
  383. *
  384. * The return value is passed to testEventManagerReset2 as
  385. * an arguments.
  386. *
  387. * @return \Cake\Event\EventManager
  388. */
  389. public function testEventManagerReset1()
  390. {
  391. return EventManager::instance();
  392. }
  393. /**
  394. * Test if the EventManager is reset between tests.
  395. *
  396. * @depends testEventManagerReset1
  397. * @return void
  398. */
  399. public function testEventManagerReset2($prevEventManager)
  400. {
  401. $this->assertNotSame($prevEventManager, EventManager::instance());
  402. }
  403. }