| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?php
- namespace Tools\Test\TestCase\Controller\Component;
- use App\Controller\CommonComponentTestController;
- use Cake\Core\Configure;
- use Cake\Event\Event;
- use Cake\Http\ServerRequest;
- use Tools\Controller\Component\CommonComponent;
- use Tools\TestSuite\TestCase;
- /**
- */
- class CommonComponentTest extends TestCase {
- /**
- * @var \App\Controller\CommonComponentTestController
- */
- public $Controller;
- /**
- * @var \Cake\Http\ServerRequest
- */
- public $request;
- /**
- * @return void
- */
- public function setUp() {
- parent::setUp();
- Configure::write('App.fullBaseUrl', 'http://localhost');
- $this->request = new ServerRequest('/my_controller/foo');
- $this->request = $this->request->withParam('controller', 'MyController')
- ->withParam('action', 'foo');
- $this->Controller = new CommonComponentTestController($this->request);
- $this->Controller->startupProcess();
- }
- /**
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- unset($this->Controller);
- }
- /**
- * @return void
- */
- public function testLoadComponent() {
- $this->assertTrue(!isset($this->Controller->Apple));
- $this->Controller->Common->loadComponent('Apple');
- $this->assertTrue(isset($this->Controller->Apple));
- // with plugin
- $this->Controller->Session = null;
- $this->assertTrue(!isset($this->Controller->Session));
- $this->Controller->Common->loadComponent('Shim.Session', ['foo' => 'bar']);
- $this->Controller->components()->unload('Session');
- $this->Controller->Common->loadComponent('Shim.Session', ['foo' => 'baz']);
- $this->assertTrue(isset($this->Controller->Session));
- // with options
- $this->Controller->Test = null;
- $this->assertTrue(!isset($this->Controller->Test));
- $this->Controller->Common->loadComponent('Test', ['x' => 'z'], false);
- $this->assertTrue(isset($this->Controller->Test));
- $this->assertFalse($this->Controller->Test->isInit);
- $this->assertFalse($this->Controller->Test->isStartup);
- // with options
- $this->Controller->components()->unload('Test');
- $this->Controller->Test = null;
- $this->assertTrue(!isset($this->Controller->Test));
- $this->Controller->Common->loadComponent('Test', ['x' => 'y']);
- $this->assertTrue(isset($this->Controller->Test));
- $this->assertTrue($this->Controller->Test->isInit);
- $this->assertTrue($this->Controller->Test->isStartup);
- $config = $this->Controller->Test->getConfig();
- $this->assertEquals(['x' => 'y'], $config);
- }
- /**
- * @return void
- */
- public function testGetParams() {
- $is = $this->Controller->Common->getPassedParam('x');
- $this->assertNull($is);
- $is = $this->Controller->Common->getPassedParam('x', 'y');
- $this->assertSame('y', $is);
- }
- /**
- * @return void
- */
- public function testGetDefaultUrlParams() {
- $is = $this->Controller->Common->defaultUrlParams();
- $this->assertNotEmpty($is);
- }
- /**
- * CommonComponentTest::testcurrentUrl()
- *
- * @return void
- */
- public function testCurrentUrl() {
- $is = $this->Controller->Common->currentUrl();
- $this->assertTrue(is_array($is) && !empty($is));
- $is = $this->Controller->Common->currentUrl(true);
- $this->assertTrue(!is_array($is) && !empty($is));
- }
- /**
- * @return void
- */
- public function testIsForeignReferer() {
- $ref = 'http://www.spiegel.de';
- $is = $this->Controller->Common->isForeignReferer($ref);
- $this->assertTrue($is);
- $ref = Configure::read('App.fullBaseUrl') . '/some/controller/action';
- $is = $this->Controller->Common->isForeignReferer($ref);
- $this->assertFalse($is);
- $ref = '';
- $is = $this->Controller->Common->isForeignReferer($ref);
- $this->assertFalse($is);
- $is = $this->Controller->Common->isForeignReferer();
- $this->assertFalse($is);
- }
- /**
- * @return void
- */
- public function testPostRedirect() {
- $this->Controller->Common->postRedirect(['action' => 'foo']);
- $is = $this->Controller->getResponse()->getHeaderLine('Location');
- $this->assertSame('http://localhost/foo', $is);
- $this->assertSame(302, $this->Controller->getResponse()->getStatusCode());
- }
- /**
- * @return void
- */
- public function testAutoRedirect() {
- $this->Controller->Common->autoRedirect(['action' => 'foo']);
- $is = $this->Controller->getResponse()->getHeaderLine('Location');
- $this->assertSame('http://localhost/foo', $is);
- $this->assertSame(302, $this->Controller->getResponse()->getStatusCode());
- }
- /**
- * @return void
- */
- public function testAutoRedirectReferer() {
- $url = 'http://localhost/my_controller/some-referer-action';
- $this->Controller->setRequest($this->Controller->getRequest()->withEnv('HTTP_REFERER', $url));
- $this->Controller->Common->autoRedirect(['action' => 'foo'], true);
- $is = $this->Controller->getResponse()->getHeaderLine('Location');
- $this->assertSame($url, $is);
- $this->assertSame(302, $this->Controller->getResponse()->getStatusCode());
- }
- /**
- * @return void
- */
- public function testAutoPostRedirect() {
- $this->Controller->Common->autoPostRedirect(['action' => 'foo'], true);
- $is = $this->Controller->getResponse()->getHeaderLine('Location');
- $this->assertSame('http://localhost/foo', $is);
- $this->assertSame(302, $this->Controller->getResponse()->getStatusCode());
- }
- /**
- * @return void
- */
- public function testAutoPostRedirectReferer() {
- $url = 'http://localhost/my_controller/allowed';
- $this->Controller->setRequest($this->Controller->getRequest()->withEnv('HTTP_REFERER', $url));
- $this->Controller->Common->autoPostRedirect(['controller' => 'MyController', 'action' => 'foo'], true);
- $is = $this->Controller->getResponse()->getHeaderLine('Location');
- $this->assertSame($url, $is);
- $this->assertSame(302, $this->Controller->getResponse()->getStatusCode());
- }
- /**
- * @return void
- */
- public function testListActions() {
- $actions = $this->Controller->Common->listActions();
- $this->assertSame([], $actions);
- }
- /**
- * @return void
- */
- public function testAutoPostRedirectRefererNotWhitelisted() {
- $this->Controller->setRequest($this->Controller->getRequest()->withEnv('HTTP_REFERER', 'http://localhost/my_controller/wrong'));
- $is = $this->Controller->Common->autoPostRedirect(['controller' => 'MyController', 'action' => 'foo'], true);
- $is = $this->Controller->getResponse()->getHeaderLine('Location');
- $this->assertSame('http://localhost/my_controller/foo', $is);
- $this->assertSame(302, $this->Controller->getResponse()->getStatusCode());
- }
- /**
- * @return void
- */
- public function testGetSafeRedirectUrl() {
- $result = $this->Controller->Common->getSafeRedirectUrl(['action' => 'default']);
- $this->assertSame(['action' => 'default'], $result);
- $this->request = $this->request->withQueryParams(['redirect' => '/foo/bar']);
- $this->Controller->setRequest($this->request);
- $result = $this->Controller->Common->getSafeRedirectUrl(['action' => 'default']);
- $this->assertSame('/foo/bar', $result);
- $this->request = $this->request->withQueryParams(['redirect' => 'https://dangerous.url/foo/bar']);
- $this->Controller->setRequest($this->request);
- $result = $this->Controller->Common->getSafeRedirectUrl(['action' => 'default']);
- $this->assertSame(['action' => 'default'], $result);
- }
- /**
- * @return void
- */
- public function testIsPosted() {
- $this->Controller->setRequest($this->Controller->getRequest()->withMethod('POST'));
- $this->assertTrue($this->Controller->Common->isPosted());
- $this->Controller->setRequest($this->Controller->getRequest()->withMethod('PUT'));
- $this->assertTrue($this->Controller->Common->isPosted());
- $this->Controller->setRequest($this->Controller->getRequest()->withMethod('PATCH'));
- $this->assertTrue($this->Controller->Common->isPosted());
- }
- /**
- * @return void
- */
- public function testLoadHelper() {
- $this->Controller->Common->loadHelper('Tester');
- $helpers = $this->Controller->viewBuilder()->getHelpers();
- $this->assertEquals(['Tester'], $helpers);
- $this->Controller->Common->loadHelper(['Tester123']);
- $helpers = $this->Controller->viewBuilder()->getHelpers();
- $this->assertEquals(['Tester', 'Tester123'], $helpers);
- }
- /**
- * @return void
- */
- public function testDefaultUrlParams() {
- Configure::write('Routing.prefixes', ['admin', 'tests']);
- $result = CommonComponent::defaultUrlParams();
- $expected = [
- 'plugin' => false,
- 'admin' => false,
- 'tests' => false,
- ];
- $this->assertEquals($expected, $result);
- Configure::write('Routing.prefixes', 'admin');
- $result = CommonComponent::defaultUrlParams();
- $expected = [
- 'plugin' => false,
- 'admin' => false,
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * @return void
- */
- public function testForceCache() {
- $this->Controller->Common->forceCache();
- $cache_control = $this->Controller->getResponse()->getHeaderLine('Cache-Control');
- $this->assertEquals('public, max-age=' . HOUR, $cache_control);
- }
- /**
- * @return void
- */
- public function testTrimQuery() {
- Configure::write('DataPreparation.notrim', false);
- $request = $this->Controller->getRequest();
- $request = $request->withQueryParams([
- 'a' => [
- 'b' => [
- ' c '
- ]
- ],
- ' d ',
- ' e',
- 'f '
- ]);
- $this->Controller->setRequest($request);
- $this->Controller->Common->startup(new Event('Test'));
- $query = $this->Controller->getRequest()->getQuery();
- $expected = [
- 'a' => [
- 'b' => [
- 'c'
- ]
- ],
- 'd',
- 'e',
- 'f'
- ];
- $this->assertSame($expected, $query);
- }
- /**
- * @return void
- */
- public function testTrimPass() {
- Configure::write('DataPreparation.notrim', false);
- $request = $this->Controller->getRequest();
- $request = $request->withParam('pass', [
- 'a' => [
- 'b' => [
- ' c '
- ]
- ],
- ' d ',
- ' e',
- 'f '
- ]);
- $this->Controller->setRequest($request);
- $this->Controller->Common->startup(new Event('Test'));
- $pass = $this->Controller->getRequest()->getParam('pass');
- $expected = [
- 'a' => [
- 'b' => [
- 'c'
- ]
- ],
- 'd',
- 'e',
- 'f'
- ];
- $this->assertSame($expected, $pass);
- }
- }
|