| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\TestSuite;
- use Cake\Core\Configure;
- use Cake\Core\Plugin;
- use Cake\Event\EventManager;
- use Cake\Http\Response;
- use Cake\Routing\DispatcherFactory;
- use Cake\Routing\Router;
- use Cake\Routing\Route\InflectedRoute;
- use Cake\TestSuite\IntegrationTestCase;
- use Cake\Test\Fixture\AssertIntegrationTestCase;
- use Cake\Utility\Security;
- use Zend\Diactoros\UploadedFile;
- /**
- * Self test of the IntegrationTestCase
- */
- class IntegrationTestCaseTest extends IntegrationTestCase
- {
- /**
- * Setup method
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- static::setAppNamespace();
- Router::reload();
- Router::scope('/', function ($routes) {
- $routes->setRouteClass(InflectedRoute::class);
- $routes->get('/get/:controller/:action', []);
- $routes->head('/head/:controller/:action', []);
- $routes->options('/options/:controller/:action', []);
- $routes->connect('/:controller/:action/*', []);
- });
- Router::$initialized = true;
- $this->useHttpServer(true);
- $this->configApplication(Configure::read('App.namespace') . '\Application', null);
- DispatcherFactory::clear();
- }
- /**
- * Helper for enabling the legacy stack for backwards compatibility testing.
- *
- * @return void
- */
- protected function useLegacyDispatcher()
- {
- DispatcherFactory::add('Routing');
- DispatcherFactory::add('ControllerFactory');
- $this->useHttpServer(false);
- }
- /**
- * Tests that all data that used by the request is cast to strings
- *
- * @return void
- */
- public function testDataCastToString()
- {
- $data = [
- 'title' => 'Blog Post',
- 'status' => 1,
- 'published' => true,
- 'not_published' => false,
- 'comments' => [
- [
- 'body' => 'Comment',
- 'status' => 1,
- ]
- ],
- 'file' => [
- 'tmp_name' => __FILE__,
- 'size' => 42,
- 'error' => 0,
- 'type' => 'text/plain',
- 'name' => 'Uploaded file'
- ],
- 'pictures' => [
- 'name' => [
- ['file' => 'a-file.png'],
- ['file' => 'a-moose.png']
- ],
- 'type' => [
- ['file' => 'image/png'],
- ['file' => 'image/jpg']
- ],
- 'tmp_name' => [
- ['file' => __FILE__],
- ['file' => __FILE__]
- ],
- 'error' => [
- ['file' => 0],
- ['file' => 0]
- ],
- 'size' => [
- ['file' => 17188],
- ['file' => 2010]
- ],
- ],
- 'upload' => new UploadedFile(__FILE__, 42, 0)
- ];
- $request = $this->_buildRequest('/posts/add', 'POST', $data);
- $this->assertInternalType('string', $request['post']['status']);
- $this->assertInternalType('string', $request['post']['published']);
- $this->assertSame('0', $request['post']['not_published']);
- $this->assertInternalType('string', $request['post']['comments'][0]['status']);
- $this->assertInternalType('integer', $request['post']['file']['error']);
- $this->assertInternalType('integer', $request['post']['file']['size']);
- $this->assertInternalType('integer', $request['post']['pictures']['error'][0]['file']);
- $this->assertInternalType('integer', $request['post']['pictures']['error'][1]['file']);
- $this->assertInternalType('integer', $request['post']['pictures']['size'][0]['file']);
- $this->assertInternalType('integer', $request['post']['pictures']['size'][1]['file']);
- $this->assertInstanceOf(UploadedFile::class, $request['post']['upload']);
- }
- /**
- * Test building a request.
- *
- * @return void
- */
- public function testRequestBuilding()
- {
- $this->configRequest([
- 'headers' => [
- 'X-CSRF-Token' => 'abc123',
- 'Content-Type' => 'application/json',
- 'Accept' => 'application/json'
- ],
- 'base' => '',
- 'webroot' => '/',
- 'environment' => [
- 'PHP_AUTH_USER' => 'foo',
- 'PHP_AUTH_PW' => 'bar'
- ]
- ]);
- $this->cookie('split_token', 'def345');
- $this->session(['User' => ['id' => 1, 'username' => 'mark']]);
- $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
- $this->assertEquals('abc123', $request['environment']['HTTP_X_CSRF_TOKEN']);
- $this->assertEquals('application/json', $request['environment']['CONTENT_TYPE']);
- $this->assertEquals('/tasks/add', $request['url']);
- $this->assertArrayHasKey('split_token', $request['cookies']);
- $this->assertEquals('def345', $request['cookies']['split_token']);
- $this->assertEquals(['id' => '1', 'username' => 'mark'], $request['session']->read('User'));
- $this->assertEquals('foo', $request['environment']['PHP_AUTH_USER']);
- $this->assertEquals('bar', $request['environment']['PHP_AUTH_PW']);
- }
- /**
- * Test request building adds csrf tokens
- *
- * @return void
- */
- public function testRequestBuildingCsrfTokens()
- {
- $this->enableCsrfToken();
- $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
- $this->assertArrayHasKey('csrfToken', $request['cookies']);
- $this->assertArrayHasKey('_csrfToken', $request['post']);
- $this->assertSame($request['cookies']['csrfToken'], $request['post']['_csrfToken']);
- $this->cookie('csrfToken', '');
- $request = $this->_buildRequest('/tasks/add', 'POST', [
- '_csrfToken' => 'fale',
- 'title' => 'First post'
- ]);
- $this->assertSame('', $request['cookies']['csrfToken']);
- $this->assertSame('fale', $request['post']['_csrfToken']);
- }
- /**
- * Test multiple actions using CSRF tokens don't fail
- *
- * @return void
- */
- public function testEnableCsrfMultipleRequests()
- {
- $this->enableCsrfToken();
- $first = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
- $second = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'Second post']);
- $this->assertSame(
- $first['cookies']['csrfToken'],
- $second['post']['_csrfToken'],
- 'Csrf token should match cookie'
- );
- $this->assertSame(
- $first['post']['_csrfToken'],
- $second['post']['_csrfToken'],
- 'Tokens should be consistent per test method'
- );
- }
- /**
- * Test pre-determined CSRF tokens.
- *
- * @return void
- */
- public function testEnableCsrfPredeterminedCookie()
- {
- $this->enableCsrfToken();
- $value = 'I am a teapot';
- $this->cookie('csrfToken', $value);
- $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
- $this->assertSame($value, $request['cookies']['csrfToken'], 'Csrf token should match cookie');
- $this->assertSame($value, $request['post']['_csrfToken'], 'Tokens should match');
- }
- /**
- * Test building a request, with query parameters
- *
- * @return void
- */
- public function testRequestBuildingQueryParameters()
- {
- $request = $this->_buildRequest('/tasks/view?archived=yes', 'GET', []);
- $this->assertSame('/tasks/view', $request['url']);
- $this->assertSame('archived=yes', $request['environment']['QUERY_STRING']);
- $this->assertSame('/tasks/view', $request['environment']['REQUEST_URI']);
- }
- /**
- * Test cookie encrypted
- *
- * @see CookieComponentControllerTest
- */
- public function testCookieEncrypted()
- {
- Security::setSalt('abcdabcdabcdabcdabcdabcdabcdabcdabcd');
- $this->cookieEncrypted('KeyOfCookie', 'Encrypted with aes by default');
- $request = $this->_buildRequest('/tasks/view', 'GET', []);
- $this->assertStringStartsWith('Q2FrZQ==.', $request['cookies']['KeyOfCookie']);
- }
- /**
- * Test sending get requests.
- *
- * @group deprecated
- * @return void
- */
- public function testGetLegacy()
- {
- $this->useLegacyDispatcher();
- $this->deprecated(function () {
- $this->assertNull($this->_response);
- $this->get('/request_action/test_request_action');
- $this->assertNotEmpty($this->_response);
- $this->assertInstanceOf('Cake\Http\Response', $this->_response);
- $this->assertEquals('This is a test', $this->_response->getBody());
- $this->_response = null;
- $this->get('/get/request_action/test_request_action');
- $this->assertEquals('This is a test', $this->_response->getBody());
- });
- }
- /**
- * Test sending get request and using default `test_app/config/routes.php`.
- *
- * @return void
- */
- public function testGetUsingApplicationWithPluginRoutes()
- {
- // first clean routes to have Router::$initailized === false
- Router::reload();
- Plugin::unload();
- $this->configApplication(Configure::read('App.namespace') . '\ApplicationWithPluginRoutes', null);
- $this->get('/test_plugin');
- $this->assertResponseOk();
- }
- /**
- * Test sending get request and using default `test_app/config/routes.php`.
- *
- * @return void
- */
- public function testGetUsingApplicationWithDefaultRoutes()
- {
- // first clean routes to have Router::$initailized === false
- Router::reload();
- $this->configApplication(Configure::read('App.namespace') . '\ApplicationWithDefaultRoutes', null);
- $this->get('/some_alias');
- $this->assertResponseOk();
- $this->assertEquals('5', $this->_getBodyAsString());
- }
- /**
- * Test sending head requests.
- *
- * @return void
- */
- public function testHead()
- {
- $this->assertNull($this->_response);
- $this->head('/request_action/test_request_action');
- $this->assertNotEmpty($this->_response);
- $this->assertInstanceOf('Cake\Http\Response', $this->_response);
- $this->assertResponseSuccess();
- $this->_response = null;
- $this->head('/head/request_action/test_request_action');
- $this->assertResponseSuccess();
- }
- /**
- * Test sending options requests.
- *
- * @return void
- */
- public function testOptions()
- {
- $this->assertNull($this->_response);
- $this->options('/request_action/test_request_action');
- $this->assertNotEmpty($this->_response);
- $this->assertInstanceOf('Cake\Http\Response', $this->_response);
- $this->assertResponseSuccess();
- $this->_response = null;
- $this->options('/options/request_action/test_request_action');
- $this->assertResponseSuccess();
- }
- /**
- * Test sending get requests sets the request method
- *
- * @return void
- */
- public function testGetSpecificRouteLegacy()
- {
- $this->useLegacyDispatcher();
- $this->deprecated(function () {
- $this->get('/get/request_action/test_request_action');
- $this->assertResponseOk();
- $this->assertEquals('This is a test', $this->_response->getBody());
- });
- }
- /**
- * Test sending get requests sets the request method
- *
- * @return void
- */
- public function testGetSpecificRouteHttpServer()
- {
- $this->get('/get/request_action/test_request_action');
- $this->assertResponseOk();
- $this->assertEquals('This is a test', $this->_response->getBody());
- }
- /**
- * Test customizing the app class.
- *
- * @return void
- */
- public function testConfigApplication()
- {
- $this->expectException(\LogicException::class);
- $this->expectExceptionMessage('Cannot load "TestApp\MissingApp" for use in integration');
- $this->configApplication('TestApp\MissingApp', []);
- $this->get('/request_action/test_request_action');
- }
- /**
- * Test sending get requests with Http\Server
- *
- * @return void
- */
- public function testGetHttpServer()
- {
- $this->assertNull($this->_response);
- $this->get('/request_action/test_request_action');
- $this->assertNotEmpty($this->_response);
- $this->assertInstanceOf('Cake\Http\Response', $this->_response);
- $this->assertEquals('This is a test', $this->_response->getBody());
- $this->assertHeader('X-Middleware', 'true');
- }
- /**
- * Test that the PSR7 requests get query string data
- *
- * @return void
- */
- public function testGetQueryStringHttpServer()
- {
- $this->configRequest(['headers' => ['Content-Type' => 'text/plain']]);
- $this->get('/request_action/params_pass?q=query');
- $this->assertResponseOk();
- $this->assertResponseContains('"q":"query"');
- $this->assertResponseContains('"contentType":"text\/plain"');
- $this->assertHeader('X-Middleware', 'true');
- $request = $this->_controller->request;
- $this->assertContains('/request_action/params_pass?q=query', $request->getRequestTarget());
- }
- /**
- * Test that the PSR7 requests get query string data
- *
- * @group deprecated
- * @return void
- */
- public function testGetQueryStringSetsHere()
- {
- $this->deprecated(function () {
- $this->configRequest(['headers' => ['Content-Type' => 'text/plain']]);
- $this->get('/request_action/params_pass?q=query');
- $this->assertResponseOk();
- $this->assertResponseContains('"q":"query"');
- $this->assertResponseContains('"contentType":"text\/plain"');
- $this->assertHeader('X-Middleware', 'true');
- $request = $this->_controller->request;
- $this->assertContains('/request_action/params_pass?q=query', $request->here());
- $this->assertContains('/request_action/params_pass?q=query', $request->getRequestTarget());
- });
- }
- /**
- * Test that the PSR7 requests get cookies
- *
- * @return void
- */
- public function testGetCookiesHttpServer()
- {
- $this->configRequest(['cookies' => ['split_test' => 'abc']]);
- $this->get('/request_action/cookie_pass');
- $this->assertResponseOk();
- $this->assertResponseContains('"split_test":"abc"');
- $this->assertHeader('X-Middleware', 'true');
- }
- /**
- * Test that the PSR7 requests receive post data
- *
- * @return void
- */
- public function testPostDataLegacyDispatcher()
- {
- $this->useLegacyDispatcher();
- $this->deprecated(function () {
- $this->post('/request_action/post_pass', ['title' => 'value']);
- $data = json_decode($this->_response->getBody());
- $this->assertEquals('value', $data->title);
- });
- }
- /**
- * Test that the PSR7 requests receive post data
- *
- * @return void
- */
- public function testPostDataHttpServer()
- {
- $this->post('/request_action/post_pass', ['title' => 'value']);
- $data = json_decode($this->_response->getBody());
- $this->assertEquals('value', $data->title);
- $this->assertHeader('X-Middleware', 'true');
- }
- /**
- * Test that the PSR7 requests receive encoded data.
- *
- * @return void
- */
- public function testInputDataHttpServer()
- {
- $this->post('/request_action/input_test', '{"hello":"world"}');
- if ($this->_response->getBody()->isSeekable()) {
- $this->_response->getBody()->rewind();
- }
- $this->assertSame('world', $this->_response->getBody()->getContents());
- $this->assertHeader('X-Middleware', 'true');
- }
- /**
- * Test that the PSR7 requests receive encoded data.
- *
- * @return void
- */
- public function testInputDataSecurityToken()
- {
- $this->enableSecurityToken();
- $this->post('/request_action/input_test', '{"hello":"world"}');
- $this->assertSame('world', '' . $this->_response->getBody());
- $this->assertHeader('X-Middleware', 'true');
- }
- /**
- * Test that the PSR7 requests get cookies
- *
- * @return void
- */
- public function testSessionHttpServer()
- {
- $this->session(['foo' => 'session data']);
- $this->get('/request_action/session_test');
- $this->assertResponseOk();
- $this->assertResponseContains('session data');
- $this->assertHeader('X-Middleware', 'true');
- }
- /**
- * Test sending requests stores references to controller/view/layout.
- *
- * @return void
- */
- public function testRequestSetsProperties()
- {
- $this->post('/posts/index');
- $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
- $this->assertNotEmpty($this->_viewName, 'View name not set');
- $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
- $this->assertNotEmpty($this->_layoutName, 'Layout name not set');
- $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
- $this->assertTemplate('index');
- $this->assertLayout('default');
- $this->assertEquals('value', $this->viewVariable('test'));
- }
- /**
- * Test PSR7 requests store references to controller/view/layout
- *
- * @return void
- */
- public function testRequestSetsPropertiesHttpServer()
- {
- $this->post('/posts/index');
- $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
- $this->assertNotEmpty($this->_viewName, 'View name not set');
- $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
- $this->assertNotEmpty($this->_layoutName, 'Layout name not set');
- $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
- $this->assertTemplate('index');
- $this->assertLayout('default');
- $this->assertEquals('value', $this->viewVariable('test'));
- }
- /**
- * Assert that the stored template doesn't change when cells are rendered.
- *
- * @return void
- */
- public function testAssertTemplateAfterCellRender()
- {
- $this->get('/posts/get');
- $this->assertContains('Template' . DS . 'Posts' . DS . 'get.ctp', $this->_viewName);
- $this->assertTemplate('get');
- $this->assertResponseContains('cellcontent');
- }
- /**
- * Test array URLs
- *
- * @return void
- */
- public function testArrayUrls()
- {
- $this->post(['controller' => 'Posts', 'action' => 'index']);
- $this->assertEquals('value', $this->viewVariable('test'));
- }
- /**
- * Test array URLs with an empty router.
- *
- * @return void
- */
- public function testArrayUrlsEmptyRouter()
- {
- Router::reload();
- $this->assertFalse(Router::$initialized);
- $this->post(['controller' => 'Posts', 'action' => 'index']);
- $this->assertEquals('value', $this->viewVariable('test'));
- }
- /**
- * Test flash and cookie assertions
- *
- * @return void
- */
- public function testFlashSessionAndCookieAsserts()
- {
- $this->post('/posts/index');
- $this->assertSession('An error message', 'Flash.flash.0.message');
- $this->assertCookie(1, 'remember_me');
- $this->assertCookieNotSet('user_id');
- }
- /**
- * Test flash and cookie assertions
- *
- * @return void
- */
- public function testFlashSessionAndCookieAssertsHttpServer()
- {
- $this->post('/posts/index');
- $this->assertSession('An error message', 'Flash.flash.0.message');
- $this->assertCookieNotSet('user_id');
- $this->assertCookie(1, 'remember_me');
- }
- /**
- * Test flash assertions stored with enableRememberFlashMessages() after they
- * are rendered
- *
- * @return void
- */
- public function testFlashAssertionsAfterRender()
- {
- $this->enableRetainFlashMessages();
- $this->get('/posts/index/with_flash');
- $this->assertSession('An error message', 'Flash.flash.0.message');
- }
- /**
- * Test flash assertions stored with enableRememberFlashMessages() even if
- * no view is rendered
- *
- * @return void
- */
- public function testFlashAssertionsWithNoRender()
- {
- $this->enableRetainFlashMessages();
- $this->get('/posts/flashNoRender');
- $this->assertRedirect();
- $this->assertSession('An error message', 'Flash.flash.0.message');
- }
- /**
- * Tests the failure message for assertCookieNotSet
- *
- * @return void
- */
- public function testCookieNotSetFailure()
- {
- $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
- $this->expectExceptionMessage('Cookie \'remember_me\' has been set');
- $this->post('/posts/index');
- $this->assertCookieNotSet('remember_me');
- }
- /**
- * Tests the failure message for assertCookieNotSet when no
- * response whas generated
- *
- * @return void
- */
- public function testCookieNotSetFailureNoResponse()
- {
- $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
- $this->expectExceptionMessage('No response set, cannot assert cookies.');
- $this->assertCookieNotSet('remember_me');
- }
- /**
- * Test error handling and error page rendering.
- *
- * @return void
- */
- public function testPostAndErrorHandling()
- {
- $this->post('/request_action/error_method');
- $this->assertResponseNotEmpty();
- $this->assertResponseContains('Not there or here');
- $this->assertResponseContains('<!DOCTYPE html>');
- }
- /**
- * Test posting to a secured form action.
- *
- * @return void
- */
- public function testPostSecuredForm()
- {
- $this->enableSecurityToken();
- $data = [
- 'title' => 'Some title',
- 'body' => 'Some text'
- ];
- $this->post('/posts/securePost', $data);
- $this->assertResponseOk();
- $this->assertResponseContains('Request was accepted');
- }
- /**
- * Test posting to a secured form action with nested data.
- *
- * @return void
- */
- public function testPostSecuredFormNestedData()
- {
- $this->enableSecurityToken();
- $data = [
- 'title' => 'New post',
- 'comments' => [
- ['comment' => 'A new comment']
- ],
- 'tags' => ['_ids' => [1, 2, 3, 4]]
- ];
- $this->post('/posts/securePost', $data);
- $this->assertResponseOk();
- $this->assertResponseContains('Request was accepted');
- }
- /**
- * Test posting to a secured form action.
- *
- * @return void
- */
- public function testPostSecuredFormWithQuery()
- {
- $this->enableSecurityToken();
- $data = [
- 'title' => 'Some title',
- 'body' => 'Some text'
- ];
- $this->post('/posts/securePost?foo=bar', $data);
- $this->assertResponseOk();
- $this->assertResponseContains('Request was accepted');
- }
- /**
- * Test posting to a secured form action with a query that has a part that
- * will be encoded by the security component
- *
- * @return void
- */
- public function testPostSecuredFormWithUnencodedQuery()
- {
- $this->enableSecurityToken();
- $data = [
- 'title' => 'Some title',
- 'body' => 'Some text'
- ];
- $this->post('/posts/securePost?foo=/', $data);
- $this->assertResponseOk();
- $this->assertResponseContains('Request was accepted');
- }
- /**
- * Test posting to a secured form action action.
- *
- * @return void
- */
- public function testPostSecuredFormFailure()
- {
- $data = [
- 'title' => 'Some title',
- 'body' => 'Some text'
- ];
- $this->post('/posts/securePost', $data);
- $this->assertResponseError();
- }
- /**
- * Test that exceptions being thrown are handled correctly.
- *
- * @return void
- */
- public function testWithExpectedException()
- {
- $this->get('/tests_apps/throw_exception');
- $this->assertResponseCode(500);
- }
- /**
- * Test that exceptions being thrown are handled correctly by the psr7 stack.
- *
- * @return void
- */
- public function testWithExpectedExceptionHttpServer()
- {
- DispatcherFactory::clear();
- $this->get('/tests_apps/throw_exception');
- $this->assertResponseCode(500);
- }
- /**
- * Test that exceptions being thrown are handled correctly.
- *
- * @return void
- */
- public function testWithUnexpectedException()
- {
- $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
- $this->get('/tests_apps/throw_exception');
- $this->assertResponseCode(501);
- }
- /**
- * Test redirecting and integration tests.
- *
- * @return void
- */
- public function testRedirect()
- {
- $this->post('/tests_apps/redirect_to');
- $this->assertResponseSuccess();
- $this->assertResponseCode(302);
- }
- /**
- * Test redirecting and psr7 stack
- *
- * @return void
- */
- public function testRedirectHttpServer()
- {
- DispatcherFactory::clear();
- $this->post('/tests_apps/redirect_to');
- $this->assertResponseCode(302);
- $this->assertHeader('X-Middleware', 'true');
- }
- /**
- * Test redirecting and integration tests.
- *
- * @return void
- */
- public function testRedirectPermanent()
- {
- $this->post('/tests_apps/redirect_to_permanent');
- $this->assertResponseSuccess();
- $this->assertResponseCode(301);
- }
- /**
- * Test the responseOk status assertion
- *
- * @return void
- */
- public function testAssertResponseStatusCodes()
- {
- $this->_response = new Response();
- $this->_response = $this->_response->withStatus(200);
- $this->assertResponseOk();
- $this->_response = $this->_response->withStatus(201);
- $this->assertResponseOk();
- $this->_response = $this->_response->withStatus(204);
- $this->assertResponseOk();
- $this->_response = $this->_response->withStatus(202);
- $this->assertResponseSuccess();
- $this->_response = $this->_response->withStatus(302);
- $this->assertResponseSuccess();
- $this->_response = $this->_response->withStatus(400);
- $this->assertResponseError();
- $this->_response = $this->_response->withStatus(417);
- $this->assertResponseError();
- $this->_response = $this->_response->withStatus(500);
- $this->assertResponseFailure();
- $this->_response = $this->_response->withStatus(505);
- $this->assertResponseFailure();
- $this->_response = $this->_response->withStatus(301);
- $this->assertResponseCode(301);
- }
- /**
- * Test the location header assertion.
- *
- * @return void
- */
- public function testAssertRedirect()
- {
- $this->_response = new Response();
- $this->_response = $this->_response->withHeader('Location', 'http://localhost/tasks/index');
- $this->assertRedirect();
- $this->assertRedirect('/tasks/index');
- $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
- $this->assertResponseEmpty();
- }
- /**
- * Test the location header assertion.
- *
- * @return void
- */
- public function testAssertNoRedirect()
- {
- $this->_response = new Response();
- $this->assertNoRedirect();
- }
- /**
- * Test the location header assertion.
- *
- * @return void
- */
- public function testAssertNoRedirectFail()
- {
- $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
- $result = $test->run();
- $this->assertFalse($result->wasSuccessful());
- $this->assertEquals(1, $result->failureCount());
- }
- /**
- * Test the location header assertion string contains
- *
- * @return void
- */
- public function testAssertRedirectContains()
- {
- $this->_response = new Response();
- $this->_response = $this->_response->withHeader('Location', 'http://localhost/tasks/index');
- $this->assertRedirectContains('/tasks/index');
- }
- /**
- * Test the header assertion.
- *
- * @return void
- */
- public function testAssertHeader()
- {
- $this->_response = new Response();
- $this->_response = $this->_response->withHeader('Etag', 'abc123');
- $this->assertHeader('Etag', 'abc123');
- }
- /**
- * Test the header contains assertion.
- *
- * @return void
- */
- public function testAssertHeaderContains()
- {
- $this->_response = new Response();
- $this->_response = $this->_response->withHeader('Etag', 'abc123');
- $this->assertHeaderContains('Etag', 'abc');
- }
- /**
- * Test the content type assertion.
- *
- * @return void
- */
- public function testAssertContentType()
- {
- $this->_response = new Response();
- $this->_response = $this->_response->withType('json');
- $this->assertContentType('json');
- $this->assertContentType('application/json');
- }
- /**
- * Test that type() in an action sets the content-type header.
- *
- * @return void
- */
- public function testContentTypeInAction()
- {
- $this->get('/tests_apps/set_type');
- $this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
- $this->assertContentType('json');
- $this->assertContentType('application/json');
- }
- /**
- * Test the content assertion.
- *
- * @return void
- */
- public function testAssertResponseContains()
- {
- $this->_response = new Response();
- $this->_response = $this->_response->withStringBody('Some content');
- $this->assertResponseContains('content');
- }
- /**
- * Test the content assertion with no case sensitivity.
- *
- * @return void
- */
- public function testAssertResponseContainsWithIgnoreCaseFlag()
- {
- $this->_response = new Response();
- $this->_response = $this->_response->withStringBody('Some content');
- $this->assertResponseContains('some', 'Failed asserting that the body contains given content', true);
- }
- /**
- * Test the negated content assertion.
- *
- * @return void
- */
- public function testAssertResponseNotContains()
- {
- $this->_response = new Response();
- $this->_response = $this->_response->withStringBody('Some content');
- $this->assertResponseNotContains('contents');
- }
- /**
- * Test the content regexp assertion.
- *
- * @return void
- */
- public function testAssertResponseRegExp()
- {
- $this->_response = new Response();
- $this->_response = $this->_response->withStringBody('Some content');
- $this->assertResponseRegExp('/cont/');
- }
- /**
- * Test the content regexp assertion failing
- *
- * @return void
- */
- public function testAssertResponseRegExpNoResponse()
- {
- $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
- $this->expectExceptionMessage('No response set');
- $this->assertResponseRegExp('/cont/');
- }
- /**
- * Test the negated content regexp assertion.
- *
- * @return void
- */
- public function testAssertResponseNotRegExp()
- {
- $this->_response = new Response();
- $this->_response = $this->_response->withStringBody('Some content');
- $this->assertResponseNotRegExp('/cant/');
- }
- /**
- * Test negated content regexp assertion failing
- *
- * @return void
- */
- public function testAssertResponseNotRegExpNoResponse()
- {
- $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
- $this->expectExceptionMessage('No response set');
- $this->assertResponseNotRegExp('/cont/');
- }
- /**
- * Test that works in tandem with testEventManagerReset2 to
- * test the EventManager reset.
- *
- * The return value is passed to testEventManagerReset2 as
- * an arguments.
- *
- * @return \Cake\Event\EventManager
- */
- public function testEventManagerReset1()
- {
- $eventManager = EventManager::instance();
- $this->assertInstanceOf('Cake\Event\EventManager', $eventManager);
- return $eventManager;
- }
- /**
- * Test if the EventManager is reset between tests.
- *
- * @depends testEventManagerReset1
- * @return void
- */
- public function testEventManagerReset2($prevEventManager)
- {
- $this->assertInstanceOf('Cake\Event\EventManager', $prevEventManager);
- $this->assertNotSame($prevEventManager, EventManager::instance());
- }
- /**
- * Test sending file in requests.
- *
- * @return void
- */
- public function testSendFile()
- {
- $this->get('/posts/file');
- $this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
- }
- /**
- * Test sending file with psr7 stack
- *
- * @return void
- */
- public function testSendFileHttpServer()
- {
- $this->get('/posts/file');
- $this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
- }
- /**
- * Test that assertFile requires a response
- *
- * @return void
- */
- public function testAssertFileNoResponse()
- {
- $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
- $this->expectExceptionMessage('No response set, cannot assert file');
- $this->assertFileResponse('foo');
- }
- /**
- * Test that assertFile requires a file
- *
- * @return void
- */
- public function testAssertFileNoFile()
- {
- $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
- $this->expectExceptionMessage('No file was sent in this response');
- $this->get('/posts/get');
- $this->assertFileResponse('foo');
- }
- /**
- * Test disabling the error handler middleware.
- *
- * @return void
- */
- public function testDisableErrorHandlerMiddleware()
- {
- $this->expectException(\Cake\Routing\Exception\MissingRouteException::class);
- $this->expectExceptionMessage('A route matching "/foo" could not be found.');
- $this->disableErrorHandlerMiddleware();
- $this->get('/foo');
- }
- /**
- * tests getting a secure action while passing a query string
- *
- * @return void
- * @dataProvider methodsProvider
- */
- public function testSecureWithQueryString($method)
- {
- $this->enableSecurityToken();
- $this->{$method}('/posts/securePost/?ids[]=1&ids[]=2');
- $this->assertResponseOk();
- }
- /**
- * data provider for HTTP methods
- *
- * @return array
- */
- public function methodsProvider()
- {
- return [
- 'GET' => ['get'],
- 'POST' => ['post'],
- 'PATCH' => ['patch'],
- 'PUT' => ['put'],
- 'DELETE' => ['delete'],
- ];
- }
- }
|