Browse Source

Tests for 13393 (#13439)

* add some unit tests regarding redirection for ResponseTest

* also tests yii\web\controller redirect
tags/2.0.11
Herbert Maschke 8 years ago committed by Alexander Makarov
parent
commit
d4d6c1dabf
  1. 64
      tests/framework/web/ControllerTest.php

64
tests/framework/web/ControllerTest.php

@ -8,13 +8,11 @@
namespace yiiunit\framework\web; namespace yiiunit\framework\web;
use Yii; use Yii;
use yii\web\Controller; use yii\base\InlineAction;
use yii\web\Response; use yii\web\Response;
use yiiunit\TestCase;
use yiiunit\framework\di\stubs\Qux;
use yiiunit\framework\web\stubs\Bar; use yiiunit\framework\web\stubs\Bar;
use yiiunit\framework\web\stubs\OtherQux; use yiiunit\framework\web\stubs\OtherQux;
use yii\base\InlineAction; use yiiunit\TestCase;
/** /**
* @group web * @group web
@ -24,18 +22,16 @@ class ControllerTest extends TestCase
public function testBindActionParams() public function testBindActionParams()
{ {
$this->mockApplication();
$controller = new FakeController('fake', Yii::$app); $aksi1 = new InlineAction('aksi1', $this->controller, 'actionAksi1');
$aksi1 = new InlineAction('aksi1', $controller, 'actionAksi1');
$params = ['fromGet'=>'from query params','q'=>'d426','validator'=>'avaliable']; $params = ['fromGet' => 'from query params', 'q' => 'd426', 'validator' => 'avaliable'];
list($fromGet, $other) = $controller->bindActionParams($aksi1, $params); list($fromGet, $other) = $this->controller->bindActionParams($aksi1, $params);
$this->assertEquals('from query params', $fromGet); $this->assertEquals('from query params', $fromGet);
$this->assertEquals('default', $other); $this->assertEquals('default', $other);
$params = ['fromGet'=>'from query params','q'=>'d426','other'=>'avaliable']; $params = ['fromGet' => 'from query params', 'q' => 'd426', 'other' => 'avaliable'];
list($fromGet, $other) = $controller->bindActionParams($aksi1, $params); list($fromGet, $other) = $this->controller->bindActionParams($aksi1, $params);
$this->assertEquals('from query params', $fromGet); $this->assertEquals('from query params', $fromGet);
$this->assertEquals('avaliable', $other); $this->assertEquals('avaliable', $other);
@ -43,14 +39,11 @@ class ControllerTest extends TestCase
public function testAsJson() public function testAsJson()
{ {
$this->mockWebApplication();
$controller = new Controller('test', Yii::$app);
$data = [ $data = [
'test' => 123, 'test' => 123,
'example' => 'data', 'example' => 'data',
]; ];
$result = $controller->asJson($data); $result = $this->controller->asJson($data);
$this->assertInstanceOf('yii\web\Response', $result); $this->assertInstanceOf('yii\web\Response', $result);
$this->assertSame(Yii::$app->response, $result, 'response should be the same as Yii::$app->response'); $this->assertSame(Yii::$app->response, $result, 'response should be the same as Yii::$app->response');
$this->assertEquals(Response::FORMAT_JSON, $result->format); $this->assertEquals(Response::FORMAT_JSON, $result->format);
@ -59,18 +52,51 @@ class ControllerTest extends TestCase
public function testAsXml() public function testAsXml()
{ {
$this->mockWebApplication();
$controller = new Controller('test', Yii::$app);
$data = [ $data = [
'test' => 123, 'test' => 123,
'example' => 'data', 'example' => 'data',
]; ];
$result = $controller->asXml($data); $result = $this->controller->asXml($data);
$this->assertInstanceOf('yii\web\Response', $result); $this->assertInstanceOf('yii\web\Response', $result);
$this->assertSame(Yii::$app->response, $result, 'response should be the same as Yii::$app->response'); $this->assertSame(Yii::$app->response, $result, 'response should be the same as Yii::$app->response');
$this->assertEquals(Response::FORMAT_XML, $result->format); $this->assertEquals(Response::FORMAT_XML, $result->format);
$this->assertEquals($data, $result->data); $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]);
}
} }

Loading…
Cancel
Save