|
|
@@ -4,7 +4,9 @@ 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;
|
|
|
|
|
|
/**
|
|
|
@@ -30,8 +32,8 @@ class CommonComponentTest extends TestCase {
|
|
|
Configure::write('App.fullBaseUrl', 'http://localhost');
|
|
|
|
|
|
$this->request = new ServerRequest('/my_controller/foo');
|
|
|
- $this->request->params['controller'] = 'MyController';
|
|
|
- $this->request->params['action'] = 'foo';
|
|
|
+ $this->request = $this->request->withParam('controller', 'MyController')
|
|
|
+ ->withParam('action', 'foo');
|
|
|
$this->Controller = new CommonComponentTestController($this->request);
|
|
|
$this->Controller->startupProcess();
|
|
|
}
|
|
|
@@ -42,7 +44,6 @@ class CommonComponentTest extends TestCase {
|
|
|
public function tearDown() {
|
|
|
parent::tearDown();
|
|
|
|
|
|
- unset($this->Controller->Common);
|
|
|
unset($this->Controller);
|
|
|
}
|
|
|
|
|
|
@@ -130,26 +131,29 @@ class CommonComponentTest extends TestCase {
|
|
|
$ref = '';
|
|
|
$is = $this->Controller->Common->isForeignReferer($ref);
|
|
|
$this->assertFalse($is);
|
|
|
+
|
|
|
+ $is = $this->Controller->Common->isForeignReferer();
|
|
|
+ $this->assertFalse($is);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @return void
|
|
|
*/
|
|
|
public function testPostRedirect() {
|
|
|
- $is = $this->Controller->Common->postRedirect(['action' => 'foo']);
|
|
|
- $is = $this->Controller->response->header();
|
|
|
- $this->assertSame('http://localhost/foo', $is['Location']);
|
|
|
- $this->assertSame(302, $this->Controller->response->getStatusCode());
|
|
|
+ $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() {
|
|
|
- $is = $this->Controller->Common->autoRedirect(['action' => 'foo']);
|
|
|
- $is = $this->Controller->response->header();
|
|
|
- $this->assertSame('http://localhost/foo', $is['Location']);
|
|
|
- $this->assertSame(302, $this->Controller->response->getStatusCode());
|
|
|
+ $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());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -157,12 +161,12 @@ class CommonComponentTest extends TestCase {
|
|
|
*/
|
|
|
public function testAutoRedirectReferer() {
|
|
|
$url = 'http://localhost/my_controller/some-referer-action';
|
|
|
- $this->Controller->setRequest($this->request->withEnv('HTTP_REFERER', $url));
|
|
|
+ $this->Controller->setRequest($this->Controller->getRequest()->withEnv('HTTP_REFERER', $url));
|
|
|
|
|
|
$this->Controller->Common->autoRedirect(['action' => 'foo'], true);
|
|
|
- $headers = $this->Controller->response->getHeaders();
|
|
|
- $this->assertSame([$url], $headers['Location']);
|
|
|
- $this->assertSame(302, $this->Controller->response->getStatusCode());
|
|
|
+ $is = $this->Controller->getResponse()->getHeaderLine('Location');
|
|
|
+ $this->assertSame($url, $is);
|
|
|
+ $this->assertSame(302, $this->Controller->getResponse()->getStatusCode());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -170,9 +174,9 @@ class CommonComponentTest extends TestCase {
|
|
|
*/
|
|
|
public function testAutoPostRedirect() {
|
|
|
$this->Controller->Common->autoPostRedirect(['action' => 'foo'], true);
|
|
|
- $is = $this->Controller->response->header();
|
|
|
- $this->assertSame('http://localhost/foo', $is['Location']);
|
|
|
- $this->assertSame(302, $this->Controller->response->getStatusCode());
|
|
|
+ $is = $this->Controller->getResponse()->getHeaderLine('Location');
|
|
|
+ $this->assertSame('http://localhost/foo', $is);
|
|
|
+ $this->assertSame(302, $this->Controller->getResponse()->getStatusCode());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -180,12 +184,12 @@ class CommonComponentTest extends TestCase {
|
|
|
*/
|
|
|
public function testAutoPostRedirectReferer() {
|
|
|
$url = 'http://localhost/my_controller/allowed';
|
|
|
- $this->Controller->setRequest($this->request->withEnv('HTTP_REFERER', $url));
|
|
|
+ $this->Controller->setRequest($this->Controller->getRequest()->withEnv('HTTP_REFERER', $url));
|
|
|
|
|
|
$this->Controller->Common->autoPostRedirect(['controller' => 'MyController', 'action' => 'foo'], true);
|
|
|
- $headers = $this->Controller->response->getHeaders();
|
|
|
- $this->assertSame([$url], $headers['Location']);
|
|
|
- $this->assertSame(302, $this->Controller->response->getStatusCode());
|
|
|
+ $is = $this->Controller->getResponse()->getHeaderLine('Location');
|
|
|
+ $this->assertSame($url, $is);
|
|
|
+ $this->assertSame(302, $this->Controller->getResponse()->getStatusCode());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -200,12 +204,12 @@ class CommonComponentTest extends TestCase {
|
|
|
* @return void
|
|
|
*/
|
|
|
public function testAutoPostRedirectRefererNotWhitelisted() {
|
|
|
- $this->request->env('HTTP_REFERER', 'http://localhost/my_controller/wrong');
|
|
|
+ $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->response->header();
|
|
|
- $this->assertSame('http://localhost/my_controller/foo', $is['Location']);
|
|
|
- $this->assertSame(302, $this->Controller->response->getStatusCode());
|
|
|
+ $is = $this->Controller->getResponse()->getHeaderLine('Location');
|
|
|
+ $this->assertSame('http://localhost/my_controller/foo', $is);
|
|
|
+ $this->assertSame(302, $this->Controller->getResponse()->getStatusCode());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -228,4 +232,117 @@ class CommonComponentTest extends TestCase {
|
|
|
$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);
|
|
|
+ }
|
|
|
+
|
|
|
}
|