From d4d6c1dabf555e6f14a668df6fcc515bceef2830 Mon Sep 17 00:00:00 2001 From: Herbert Maschke Date: Tue, 24 Jan 2017 14:58:01 +0100 Subject: [PATCH] Tests for 13393 (#13439) * add some unit tests regarding redirection for ResponseTest * also tests yii\web\controller redirect --- tests/framework/web/ControllerTest.php | 64 ++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/tests/framework/web/ControllerTest.php b/tests/framework/web/ControllerTest.php index 11c59a3..6dbce18 100644 --- a/tests/framework/web/ControllerTest.php +++ b/tests/framework/web/ControllerTest.php @@ -8,13 +8,11 @@ namespace yiiunit\framework\web; use Yii; -use yii\web\Controller; +use yii\base\InlineAction; use yii\web\Response; -use yiiunit\TestCase; -use yiiunit\framework\di\stubs\Qux; use yiiunit\framework\web\stubs\Bar; use yiiunit\framework\web\stubs\OtherQux; -use yii\base\InlineAction; +use yiiunit\TestCase; /** * @group web @@ -24,18 +22,16 @@ class ControllerTest extends TestCase public function testBindActionParams() { - $this->mockApplication(); - $controller = new FakeController('fake', Yii::$app); - $aksi1 = new InlineAction('aksi1', $controller, 'actionAksi1'); + $aksi1 = new InlineAction('aksi1', $this->controller, 'actionAksi1'); - $params = ['fromGet'=>'from query params','q'=>'d426','validator'=>'avaliable']; - list($fromGet, $other) = $controller->bindActionParams($aksi1, $params); + $params = ['fromGet' => 'from query params', 'q' => 'd426', 'validator' => 'avaliable']; + list($fromGet, $other) = $this->controller->bindActionParams($aksi1, $params); $this->assertEquals('from query params', $fromGet); $this->assertEquals('default', $other); - $params = ['fromGet'=>'from query params','q'=>'d426','other'=>'avaliable']; - list($fromGet, $other) = $controller->bindActionParams($aksi1, $params); + $params = ['fromGet' => 'from query params', 'q' => 'd426', 'other' => 'avaliable']; + list($fromGet, $other) = $this->controller->bindActionParams($aksi1, $params); $this->assertEquals('from query params', $fromGet); $this->assertEquals('avaliable', $other); @@ -43,14 +39,11 @@ class ControllerTest extends TestCase public function testAsJson() { - $this->mockWebApplication(); - - $controller = new Controller('test', Yii::$app); $data = [ 'test' => 123, 'example' => 'data', ]; - $result = $controller->asJson($data); + $result = $this->controller->asJson($data); $this->assertInstanceOf('yii\web\Response', $result); $this->assertSame(Yii::$app->response, $result, 'response should be the same as Yii::$app->response'); $this->assertEquals(Response::FORMAT_JSON, $result->format); @@ -59,18 +52,51 @@ class ControllerTest extends TestCase public function testAsXml() { - $this->mockWebApplication(); - - $controller = new Controller('test', Yii::$app); $data = [ 'test' => 123, 'example' => 'data', ]; - $result = $controller->asXml($data); + $result = $this->controller->asXml($data); $this->assertInstanceOf('yii\web\Response', $result); $this->assertSame(Yii::$app->response, $result, 'response should be the same as Yii::$app->response'); $this->assertEquals(Response::FORMAT_XML, $result->format); $this->assertEquals($data, $result->data); } + public function testRedirect() + { + $_SERVER['REQUEST_URI'] = 'http://test-domain.com/'; + $this->assertEquals($this->controller->redirect('')->headers->get('location'), '/'); + $this->assertEquals($this->controller->redirect('http://some-external-domain.com')->headers->get('location'), 'http://some-external-domain.com'); + $this->assertEquals($this->controller->redirect('/')->headers->get('location'), '/'); + $this->assertEquals($this->controller->redirect('/something-relative')->headers->get('location'), '/something-relative'); + $this->assertEquals($this->controller->redirect(['/'])->headers->get('location'), '/index.php?r='); + $this->assertEquals($this->controller->redirect(['view'])->headers->get('location'), '/index.php?r=fake%2Fview'); + $this->assertEquals($this->controller->redirect(['/controller'])->headers->get('location'), '/index.php?r=controller'); + $this->assertEquals($this->controller->redirect(['/controller/index'])->headers->get('location'), '/index.php?r=controller%2Findex'); + $this->assertEquals($this->controller->redirect(['//controller/index'])->headers->get('location'), '/index.php?r=controller%2Findex'); + $this->assertEquals($this->controller->redirect(['//controller/index', 'id' => 3])->headers->get('location'), '/index.php?r=controller%2Findex&id=3'); + $this->assertEquals($this->controller->redirect(['//controller/index', 'id_1' => 3, 'id_2' => 4])->headers->get('location'), '/index.php?r=controller%2Findex&id_1=3&id_2=4'); + $this->assertEquals($this->controller->redirect(['//controller/index', 'slug' => 'äöüß!"§$%&/()'])->headers->get('location'), '/index.php?r=controller%2Findex&slug=%C3%A4%C3%B6%C3%BC%C3%9F%21%22%C2%A7%24%25%26%2F%28%29'); + + } + + protected function setUp() + { + parent::setUp(); + $this->controller = new FakeController('fake', new \yii\web\Application([ + 'id' => 'app', + 'basePath' => __DIR__, + + 'components' => [ + 'request' => [ + 'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq', + 'scriptFile' => __DIR__ . '/index.php', + 'scriptUrl' => '/index.php', + ], + ] + ])); + $this->mockWebApplication(['controller' => $this->controller]); + } + }