IntegrationTestCaseTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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('/get/:controller/:action', ['_method' => 'GET'], ['routeClass' => 'InflectedRoute']);
  39. Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
  40. DispatcherFactory::clear();
  41. DispatcherFactory::add('Routing');
  42. DispatcherFactory::add('ControllerFactory');
  43. $this->useHttpServer(false);
  44. // Load aliases, or tests fail in isolation.
  45. class_exists('Cake\Network\Request');
  46. }
  47. /**
  48. * Test building a request.
  49. *
  50. * @return void
  51. */
  52. public function testRequestBuilding()
  53. {
  54. $this->configRequest([
  55. 'headers' => [
  56. 'X-CSRF-Token' => 'abc123',
  57. 'Content-Type' => 'application/json',
  58. 'Accept' => 'application/json'
  59. ],
  60. 'base' => '',
  61. 'webroot' => '/',
  62. 'environment' => [
  63. 'PHP_AUTH_USER' => 'foo',
  64. 'PHP_AUTH_PW' => 'bar'
  65. ]
  66. ]);
  67. $this->cookie('split_token', 'def345');
  68. $this->session(['User' => ['id' => 1, 'username' => 'mark']]);
  69. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  70. $this->assertEquals('abc123', $request['environment']['HTTP_X_CSRF_TOKEN']);
  71. $this->assertEquals('application/json', $request['environment']['CONTENT_TYPE']);
  72. $this->assertEquals('/tasks/add', $request['url']);
  73. $this->assertArrayHasKey('split_token', $request['cookies']);
  74. $this->assertEquals('def345', $request['cookies']['split_token']);
  75. $this->assertEquals(['id' => '1', 'username' => 'mark'], $request['session']->read('User'));
  76. $this->assertEquals('foo', $request['environment']['PHP_AUTH_USER']);
  77. $this->assertEquals('bar', $request['environment']['PHP_AUTH_PW']);
  78. }
  79. /**
  80. * Test request building adds csrf tokens
  81. *
  82. * @return void
  83. */
  84. public function testRequestBuildingCsrfTokens()
  85. {
  86. $this->enableCsrfToken();
  87. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  88. $this->assertArrayHasKey('csrfToken', $request['cookies']);
  89. $this->assertArrayHasKey('_csrfToken', $request['post']);
  90. $this->assertSame($request['cookies']['csrfToken'], $request['post']['_csrfToken']);
  91. $this->cookie('csrfToken', '');
  92. $request = $this->_buildRequest('/tasks/add', 'POST', [
  93. '_csrfToken' => 'fale',
  94. 'title' => 'First post'
  95. ]);
  96. $this->assertSame('', $request['cookies']['csrfToken']);
  97. $this->assertSame('fale', $request['post']['_csrfToken']);
  98. }
  99. /**
  100. * Test multiple actions using CSRF tokens don't fail
  101. *
  102. * @return void
  103. */
  104. public function testEnableCsrfMultipleRequests()
  105. {
  106. $this->enableCsrfToken();
  107. $first = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  108. $second = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'Second post']);
  109. $this->assertSame(
  110. $first['cookies']['csrfToken'],
  111. $second['post']['_csrfToken'],
  112. 'Csrf token should match cookie'
  113. );
  114. $this->assertSame(
  115. $first['post']['_csrfToken'],
  116. $second['post']['_csrfToken'],
  117. 'Tokens should be consistent per test method'
  118. );
  119. }
  120. /**
  121. * Test pre-determined CSRF tokens.
  122. *
  123. * @return void
  124. */
  125. public function testEnableCsrfPredeterminedCookie()
  126. {
  127. $this->enableCsrfToken();
  128. $value = 'I am a teapot';
  129. $this->cookie('csrfToken', $value);
  130. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  131. $this->assertSame($value, $request['cookies']['csrfToken'], 'Csrf token should match cookie');
  132. $this->assertSame($value, $request['post']['_csrfToken'], 'Tokens should match');
  133. }
  134. /**
  135. * Test building a request, with query parameters
  136. *
  137. * @return void
  138. */
  139. public function testRequestBuildingQueryParameters()
  140. {
  141. $request = $this->_buildRequest('/tasks/view?archived=yes', 'GET', []);
  142. $this->assertEquals('/tasks/view', $request['url']);
  143. $this->assertEquals('yes', $request['query']['archived']);
  144. }
  145. /**
  146. * Test cookie encrypted
  147. *
  148. * @see CookieComponentControllerTest
  149. */
  150. public function testCookieEncrypted()
  151. {
  152. Security::salt('abcdabcdabcdabcdabcdabcdabcdabcdabcd');
  153. $this->cookieEncrypted('KeyOfCookie', 'Encrypted with aes by default');
  154. $request = $this->_buildRequest('/tasks/view', 'GET', []);
  155. $this->assertStringStartsWith('Q2FrZQ==.', $request['cookies']['KeyOfCookie']);
  156. }
  157. /**
  158. * Test sending get requests.
  159. *
  160. * @return void
  161. */
  162. public function testGet()
  163. {
  164. $this->assertNull($this->_response);
  165. $this->get('/request_action/test_request_action');
  166. $this->assertNotEmpty($this->_response);
  167. $this->assertInstanceOf('Cake\Network\Response', $this->_response);
  168. $this->assertEquals('This is a test', $this->_response->body());
  169. $this->_response = null;
  170. $this->get('/get/request_action/test_request_action');
  171. $this->assertEquals('This is a test', $this->_response->body());
  172. }
  173. /**
  174. * Test sending get requests sets the request method
  175. *
  176. * @return void
  177. */
  178. public function testGetSpecificRouteHttpServer()
  179. {
  180. $this->useHttpServer(true);
  181. $this->get('/get/request_action/test_request_action');
  182. $this->assertResponseOk();
  183. $this->assertEquals('This is a test', $this->_response->body());
  184. }
  185. /**
  186. * Test customizing the app class.
  187. *
  188. * @expectedException \LogicException
  189. * @expectedExceptionMessage Cannot load "TestApp\MissingApp" for use in integration
  190. * @return void
  191. */
  192. public function testConfigApplication()
  193. {
  194. DispatcherFactory::clear();
  195. $this->useHttpServer(true);
  196. $this->configApplication('TestApp\MissingApp', []);
  197. $this->get('/request_action/test_request_action');
  198. }
  199. /**
  200. * Test sending get requests with Http\Server
  201. *
  202. * @return void
  203. */
  204. public function testGetHttpServer()
  205. {
  206. DispatcherFactory::clear();
  207. $this->useHttpServer(true);
  208. $this->assertNull($this->_response);
  209. $this->get('/request_action/test_request_action');
  210. $this->assertNotEmpty($this->_response);
  211. $this->assertInstanceOf('Cake\Network\Response', $this->_response);
  212. $this->assertEquals('This is a test', $this->_response->body());
  213. $this->assertHeader('X-Middleware', 'true');
  214. }
  215. /**
  216. * Test that the PSR7 requests get query string data
  217. *
  218. * @return void
  219. */
  220. public function testQueryStringHttpServer()
  221. {
  222. $this->useHttpServer(true);
  223. $this->configRequest(['headers' => ['Content-Type' => 'text/plain']]);
  224. $this->get('/request_action/params_pass?q=query');
  225. $this->assertResponseOk();
  226. $this->assertResponseContains('"q":"query"');
  227. $this->assertResponseContains('"contentType":"text\/plain"');
  228. $this->assertHeader('X-Middleware', 'true');
  229. }
  230. /**
  231. * Test that the PSR7 requests get cookies
  232. *
  233. * @return void
  234. */
  235. public function testGetCookiesHttpServer()
  236. {
  237. $this->useHttpServer(true);
  238. $this->configRequest(['cookies' => ['split_test' => 'abc']]);
  239. $this->get('/request_action/cookie_pass');
  240. $this->assertResponseOk();
  241. $this->assertResponseContains('"split_test":"abc"');
  242. $this->assertHeader('X-Middleware', 'true');
  243. }
  244. /**
  245. * Test that the PSR7 requests receive post data
  246. *
  247. * @return void
  248. */
  249. public function testPostDataHttpServer()
  250. {
  251. $this->useHttpServer(true);
  252. $this->post('/request_action/post_pass', ['title' => 'value']);
  253. $data = json_decode($this->_response->body());
  254. $this->assertEquals('value', $data->title);
  255. $this->assertHeader('X-Middleware', 'true');
  256. }
  257. /**
  258. * Test that the PSR7 requests receive encoded data.
  259. *
  260. * @return void
  261. */
  262. public function testInputDataHttpServer()
  263. {
  264. $this->useHttpServer(true);
  265. $this->post('/request_action/input_test', '{"hello":"world"}');
  266. $this->assertSame('world', $this->_response->body());
  267. $this->assertHeader('X-Middleware', 'true');
  268. }
  269. /**
  270. * Test that the PSR7 requests get cookies
  271. *
  272. * @return void
  273. */
  274. public function testSessionHttpServer()
  275. {
  276. $this->useHttpServer(true);
  277. $this->session(['foo' => 'session data']);
  278. $this->get('/request_action/session_test');
  279. $this->assertResponseOk();
  280. $this->assertResponseContains('session data');
  281. $this->assertHeader('X-Middleware', 'true');
  282. }
  283. /**
  284. * Test sending requests stores references to controller/view/layout.
  285. *
  286. * @return void
  287. */
  288. public function testRequestSetsProperties()
  289. {
  290. $this->post('/posts/index');
  291. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  292. $this->assertNotEmpty($this->_viewName, 'View name not set');
  293. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  294. $this->assertNotEmpty($this->_layoutName, 'Layout name not set');
  295. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  296. $this->assertTemplate('index');
  297. $this->assertLayout('default');
  298. $this->assertEquals('value', $this->viewVariable('test'));
  299. }
  300. /**
  301. * Test PSR7 requests store references to controller/view/layout
  302. *
  303. * @return void
  304. */
  305. public function testRequestSetsPropertiesHttpServer()
  306. {
  307. $this->useHttpServer(true);
  308. DispatcherFactory::clear();
  309. $this->post('/posts/index');
  310. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  311. $this->assertNotEmpty($this->_viewName, 'View name not set');
  312. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  313. $this->assertNotEmpty($this->_layoutName, 'Layout name not set');
  314. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  315. $this->assertTemplate('index');
  316. $this->assertLayout('default');
  317. $this->assertEquals('value', $this->viewVariable('test'));
  318. }
  319. /**
  320. * Assert that the stored template doesn't change when cells are rendered.
  321. *
  322. * @return void
  323. */
  324. public function testAssertTemplateAfterCellRender()
  325. {
  326. $this->get('/posts/get');
  327. $this->assertContains('Template' . DS . 'Posts' . DS . 'get.ctp', $this->_viewName);
  328. $this->assertTemplate('get');
  329. $this->assertResponseContains('cellcontent');
  330. }
  331. /**
  332. * Test array URLs
  333. *
  334. * @return void
  335. */
  336. public function testArrayUrls()
  337. {
  338. $this->post(['controller' => 'Posts', 'action' => 'index']);
  339. $this->assertEquals('value', $this->viewVariable('test'));
  340. }
  341. /**
  342. * Test flash and cookie assertions
  343. *
  344. * @return void
  345. */
  346. public function testFlashSessionAndCookieAsserts()
  347. {
  348. $this->post('/posts/index');
  349. $this->assertSession('An error message', 'Flash.flash.0.message');
  350. $this->assertCookie(1, 'remember_me');
  351. $this->assertCookieNotSet('user_id');
  352. }
  353. /**
  354. * Test flash and cookie assertions
  355. *
  356. * @return void
  357. */
  358. public function testFlashSessionAndCookieAssertsHttpServer()
  359. {
  360. $this->useHttpServer(true);
  361. $this->post('/posts/index');
  362. $this->assertSession('An error message', 'Flash.flash.0.message');
  363. $this->assertCookieNotSet('user_id');
  364. $this->assertCookie(1, 'remember_me');
  365. }
  366. /**
  367. * Tests the failure message for assertCookieNotSet
  368. *
  369. * @expectedException \PHPUnit_Framework_AssertionFailedError
  370. * @expectedExceptionMessage Cookie 'remember_me' has been set
  371. * @return void
  372. */
  373. public function testCookieNotSetFailure()
  374. {
  375. $this->post('/posts/index');
  376. $this->assertCookieNotSet('remember_me');
  377. }
  378. /**
  379. * Tests the failure message for assertCookieNotSet when no
  380. * response whas generated
  381. *
  382. * @expectedException \PHPUnit_Framework_AssertionFailedError
  383. * @expectedExceptionMessage No response set, cannot assert cookies.
  384. * @return void
  385. */
  386. public function testCookieNotSetFailureNoResponse()
  387. {
  388. $this->assertCookieNotSet('remember_me');
  389. }
  390. /**
  391. * Test error handling and error page rendering.
  392. *
  393. * @return void
  394. */
  395. public function testPostAndErrorHandling()
  396. {
  397. $this->post('/request_action/error_method');
  398. $this->assertResponseNotEmpty();
  399. $this->assertResponseContains('Not there or here');
  400. $this->assertResponseContains('<!DOCTYPE html>');
  401. }
  402. /**
  403. * Test posting to a secured form action.
  404. *
  405. * @return void
  406. */
  407. public function testPostSecuredForm()
  408. {
  409. $this->enableSecurityToken();
  410. $data = [
  411. 'title' => 'Some title',
  412. 'body' => 'Some text'
  413. ];
  414. $this->post('/posts/securePost', $data);
  415. $this->assertResponseOk();
  416. $this->assertResponseContains('Request was accepted');
  417. }
  418. /**
  419. * Test posting to a secured form action with nested data.
  420. *
  421. * @return void
  422. */
  423. public function testPostSecuredFormNestedData()
  424. {
  425. $this->enableSecurityToken();
  426. $data = [
  427. 'title' => 'New post',
  428. 'comments' => [
  429. ['comment' => 'A new comment']
  430. ],
  431. 'tags' => ['_ids' => [1, 2, 3, 4]]
  432. ];
  433. $this->post('/posts/securePost', $data);
  434. $this->assertResponseOk();
  435. $this->assertResponseContains('Request was accepted');
  436. }
  437. /**
  438. * Test posting to a secured form action.
  439. *
  440. * @return void
  441. */
  442. public function testPostSecuredFormWithQuery()
  443. {
  444. $this->enableSecurityToken();
  445. $data = [
  446. 'title' => 'Some title',
  447. 'body' => 'Some text'
  448. ];
  449. $this->post('/posts/securePost?foo=bar', $data);
  450. $this->assertResponseOk();
  451. $this->assertResponseContains('Request was accepted');
  452. }
  453. /**
  454. * Test posting to a secured form action action.
  455. *
  456. * @return void
  457. */
  458. public function testPostSecuredFormFailure()
  459. {
  460. $data = [
  461. 'title' => 'Some title',
  462. 'body' => 'Some text'
  463. ];
  464. $this->post('/posts/securePost', $data);
  465. $this->assertResponseError();
  466. }
  467. /**
  468. * Test that exceptions being thrown are handled correctly.
  469. *
  470. * @return void
  471. */
  472. public function testWithExpectedException()
  473. {
  474. $this->get('/tests_apps/throw_exception');
  475. $this->assertResponseCode(500);
  476. }
  477. /**
  478. * Test that exceptions being thrown are handled correctly by the psr7 stack.
  479. *
  480. * @return void
  481. */
  482. public function testWithExpectedExceptionHttpServer()
  483. {
  484. DispatcherFactory::clear();
  485. $this->useHttpServer(true);
  486. $this->get('/tests_apps/throw_exception');
  487. $this->assertResponseCode(500);
  488. }
  489. /**
  490. * Test that exceptions being thrown are handled correctly.
  491. *
  492. * @expectedException \PHPUnit_Framework_AssertionFailedError
  493. * @return void
  494. */
  495. public function testWithUnexpectedException()
  496. {
  497. $this->get('/tests_apps/throw_exception');
  498. $this->assertResponseCode(501);
  499. }
  500. /**
  501. * Test redirecting and integration tests.
  502. *
  503. * @return void
  504. */
  505. public function testRedirect()
  506. {
  507. $this->post('/tests_apps/redirect_to');
  508. $this->assertResponseSuccess();
  509. $this->assertResponseCode(302);
  510. }
  511. /**
  512. * Test redirecting and psr7 stack
  513. *
  514. * @return void
  515. */
  516. public function testRedirectHttpServer()
  517. {
  518. DispatcherFactory::clear();
  519. $this->useHttpServer(true);
  520. $this->post('/tests_apps/redirect_to');
  521. $this->assertResponseCode(302);
  522. $this->assertHeader('X-Middleware', 'true');
  523. }
  524. /**
  525. * Test redirecting and integration tests.
  526. *
  527. * @return void
  528. */
  529. public function testRedirectPermanent()
  530. {
  531. $this->post('/tests_apps/redirect_to_permanent');
  532. $this->assertResponseSuccess();
  533. $this->assertResponseCode(301);
  534. }
  535. /**
  536. * Test the responseOk status assertion
  537. *
  538. * @return void
  539. */
  540. public function testAssertResponseStatusCodes()
  541. {
  542. $this->_response = new Response();
  543. $this->_response->statusCode(200);
  544. $this->assertResponseOk();
  545. $this->_response->statusCode(201);
  546. $this->assertResponseOk();
  547. $this->_response->statusCode(204);
  548. $this->assertResponseOk();
  549. $this->_response->statusCode(202);
  550. $this->assertResponseSuccess();
  551. $this->_response->statusCode(302);
  552. $this->assertResponseSuccess();
  553. $this->_response->statusCode(400);
  554. $this->assertResponseError();
  555. $this->_response->statusCode(417);
  556. $this->assertResponseError();
  557. $this->_response->statusCode(500);
  558. $this->assertResponseFailure();
  559. $this->_response->statusCode(505);
  560. $this->assertResponseFailure();
  561. $this->_response->statusCode(301);
  562. $this->assertResponseCode(301);
  563. }
  564. /**
  565. * Test the location header assertion.
  566. *
  567. * @return void
  568. */
  569. public function testAssertRedirect()
  570. {
  571. $this->_response = new Response();
  572. $this->_response->header('Location', 'http://localhost/tasks/index');
  573. $this->assertRedirect();
  574. $this->assertRedirect('/tasks/index');
  575. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  576. $this->assertResponseEmpty();
  577. }
  578. /**
  579. * Test the location header assertion.
  580. *
  581. * @return void
  582. */
  583. public function testAssertNoRedirect()
  584. {
  585. $this->_response = new Response();
  586. $this->assertNoRedirect();
  587. }
  588. /**
  589. * Test the location header assertion.
  590. *
  591. * @return void
  592. */
  593. public function testAssertNoRedirectFail()
  594. {
  595. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  596. $result = $test->run();
  597. $this->assertFalse($result->wasSuccessful());
  598. $this->assertEquals(1, $result->failureCount());
  599. }
  600. /**
  601. * Test the location header assertion string contains
  602. *
  603. * @return void
  604. */
  605. public function testAssertRedirectContains()
  606. {
  607. $this->_response = new Response();
  608. $this->_response->header('Location', 'http://localhost/tasks/index');
  609. $this->assertRedirectContains('/tasks/index');
  610. }
  611. /**
  612. * Test the header assertion.
  613. *
  614. * @return void
  615. */
  616. public function testAssertHeader()
  617. {
  618. $this->_response = new Response();
  619. $this->_response->header('Etag', 'abc123');
  620. $this->assertHeader('Etag', 'abc123');
  621. }
  622. /**
  623. * Test the header contains assertion.
  624. *
  625. * @return void
  626. */
  627. public function testAssertHeaderContains()
  628. {
  629. $this->_response = new Response();
  630. $this->_response->header('Etag', 'abc123');
  631. $this->assertHeaderContains('Etag', 'abc');
  632. }
  633. /**
  634. * Test the content type assertion.
  635. *
  636. * @return void
  637. */
  638. public function testAssertContentType()
  639. {
  640. $this->_response = new Response();
  641. $this->_response->type('json');
  642. $this->assertContentType('json');
  643. $this->assertContentType('application/json');
  644. }
  645. /**
  646. * Test that type() in an action sets the content-type header.
  647. *
  648. * @return void
  649. */
  650. public function testContentTypeInAction()
  651. {
  652. $this->get('/tests_apps/set_type');
  653. $this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
  654. $this->assertContentType('json');
  655. $this->assertContentType('application/json');
  656. }
  657. /**
  658. * Test the content assertion.
  659. *
  660. * @return void
  661. */
  662. public function testAssertResponseContains()
  663. {
  664. $this->_response = new Response();
  665. $this->_response->body('Some content');
  666. $this->assertResponseContains('content');
  667. }
  668. /**
  669. * Test the negated content assertion.
  670. *
  671. * @return void
  672. */
  673. public function testAssertResponseNotContains()
  674. {
  675. $this->_response = new Response();
  676. $this->_response->body('Some content');
  677. $this->assertResponseNotContains('contents');
  678. }
  679. /**
  680. * Test the content regexp assertion.
  681. *
  682. * @return void
  683. */
  684. public function testAssertResponseRegExp()
  685. {
  686. $this->_response = new Response();
  687. $this->_response->body('Some content');
  688. $this->assertResponseRegExp('/cont/');
  689. }
  690. /**
  691. * Test the content regexp assertion failing
  692. *
  693. * @expectedException \PHPUnit_Framework_AssertionFailedError
  694. * @expectedExceptionMessage No response set
  695. * @return void
  696. */
  697. public function testAssertResponseRegExpNoResponse()
  698. {
  699. $this->assertResponseRegExp('/cont/');
  700. }
  701. /**
  702. * Test the negated content regexp assertion.
  703. *
  704. * @return void
  705. */
  706. public function testAssertResponseNotRegExp()
  707. {
  708. $this->_response = new Response();
  709. $this->_response->body('Some content');
  710. $this->assertResponseNotRegExp('/cant/');
  711. }
  712. /**
  713. * Test negated content regexp assertion failing
  714. *
  715. * @expectedException \PHPUnit_Framework_AssertionFailedError
  716. * @expectedExceptionMessage No response set
  717. * @return void
  718. */
  719. public function testAssertResponseNotRegExpNoResponse()
  720. {
  721. $this->assertResponseNotRegExp('/cont/');
  722. }
  723. /**
  724. * Test that works in tandem with testEventManagerReset2 to
  725. * test the EventManager reset.
  726. *
  727. * The return value is passed to testEventManagerReset2 as
  728. * an arguments.
  729. *
  730. * @return \Cake\Event\EventManager
  731. */
  732. public function testEventManagerReset1()
  733. {
  734. return EventManager::instance();
  735. }
  736. /**
  737. * Test if the EventManager is reset between tests.
  738. *
  739. * @depends testEventManagerReset1
  740. * @return void
  741. */
  742. public function testEventManagerReset2($prevEventManager)
  743. {
  744. $this->assertNotSame($prevEventManager, EventManager::instance());
  745. }
  746. /**
  747. * Test sending file in requests.
  748. *
  749. * @return void
  750. */
  751. public function testSendFile()
  752. {
  753. $this->get('/posts/file');
  754. $this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
  755. }
  756. /**
  757. * Test sending file with psr7 stack
  758. *
  759. * @return void
  760. */
  761. public function testSendFileHttpServer()
  762. {
  763. DispatcherFactory::clear();
  764. $this->useHttpServer(true);
  765. $this->get('/posts/file');
  766. $this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
  767. }
  768. /**
  769. * Test that assertFile requires a response
  770. *
  771. * @expectedException \PHPUnit_Framework_AssertionFailedError
  772. * @expectedExceptionMessage No response set, cannot assert file
  773. * @return void
  774. */
  775. public function testAssertFileNoResponse()
  776. {
  777. $this->assertFileResponse('foo');
  778. }
  779. /**
  780. * Test that assertFile requires a file
  781. *
  782. * @expectedException \PHPUnit_Framework_AssertionFailedError
  783. * @expectedExceptionMessage No file was sent in this response
  784. * @return void
  785. */
  786. public function testAssertFileNoFile()
  787. {
  788. $this->get('/posts/get');
  789. $this->assertFileResponse('foo');
  790. }
  791. }