* * @group widgets */ class ActiveFieldTest extends \yiiunit\TestCase { private $activeField; private $helperModel; private $helperForm; private $attributeName = 'attributeName'; public function setUp() { // dirty way to have Request object not throwing exception when running testHomeLinkNull() $_SERVER['SCRIPT_FILENAME'] = "index.php"; $_SERVER['SCRIPT_NAME'] = "index.php"; $this->mockWebApplication(); Yii::setAlias('@testWeb', '/'); Yii::setAlias('@testWebRoot', '@yiiunit/data/web'); $this->helperModel = new DynamicModel(['attributeName']); ob_start(); $this->helperForm = new ActiveForm(['action' => '/something']); ob_end_clean(); $this->activeField = new ActiveFieldExtend(true); $this->activeField->form = $this->helperForm; $this->activeField->form->setView($this->getView()); $this->activeField->model = $this->helperModel; $this->activeField->attribute = $this->attributeName; } public function testRenderNoContent() { $expectedValue = <<
EOD; $actualValue = $this->activeField->render(); $this->assertEquals($expectedValue, $actualValue); } /** * @todo discuss|review Expected HTML shouldn't be wrapped only by the $content? */ public function testRenderWithCallableContent() { // field will be the html of the model's attribute wrapped with the return string below. $field = $this->attributeName; $content = function($field) { return "
$field
"; }; $expectedValue = <<
EOD; $actualValue = $this->activeField->render($content); $this->assertEquals($expectedValue, $actualValue); } public function testBeginHasErros() { $this->helperModel->addError($this->attributeName, "Error Message"); $expectedValue = '
'; $actualValue = $this->activeField->begin(); $this->assertEquals($expectedValue, $actualValue); } public function testBeginAttributeIsRequered() { $this->helperModel->addRule($this->attributeName, 'required'); $expectedValue = '
'; $actualValue = $this->activeField->begin(); $this->assertEquals($expectedValue, $actualValue); } public function testBeginHasErrorAndRequired() { $this->helperModel->addError($this->attributeName, "Error Message"); $this->helperModel->addRule($this->attributeName, 'required'); $expectedValue = '
'; $actualValue = $this->activeField->begin(); $this->assertEquals($expectedValue, $actualValue); } public function testEnd() { $expectedValue = '
'; $actualValue = $this->activeField->end(); $this->assertEquals($expectedValue, $actualValue); // other tag $expectedValue = ""; $this->activeField->options['tag'] = 'article'; $actualValue = $this->activeField->end(); $this->assertTrue($actualValue === $expectedValue); } public function testLabel() { $expectedValue = ''; $this->activeField->label(); $this->assertEquals($expectedValue, $this->activeField->parts['{label}']); // label = false $expectedValue = ''; $this->activeField->label(false); $this->assertEquals($expectedValue, $this->activeField->parts['{label}']); // $label = 'Label Name' $label = 'Label Name'; $expectedValue = <<{$label} EOT; $this->activeField->label($label); $this->assertEquals($expectedValue, $this->activeField->parts['{label}']); } public function testError() { $expectedValue = ''; $this->activeField->label(); $this->assertEquals($expectedValue, $this->activeField->parts['{label}']); // label = false $expectedValue = ''; $this->activeField->label(false); $this->assertEquals($expectedValue, $this->activeField->parts['{label}']); // $label = 'Label Name' $label = 'Label Name'; $expectedValue = <<{$label} EOT; $this->activeField->label($label); $this->assertEquals($expectedValue, $this->activeField->parts['{label}']); } public function testHint() { $expectedValue = '
Hint Content
'; $this->activeField->hint('Hint Content'); $this->assertEquals($expectedValue, $this->activeField->parts['{hint}']); } public function testInput() { $expectedValue = << EOD; $this->activeField->input("password"); $this->assertEquals($expectedValue, $this->activeField->parts['{input}']); // with options $expectedValue = << EOD; $this->activeField->input("password", ['weird' => 'value']); $this->assertEquals($expectedValue, $this->activeField->parts['{input}']); } public function testTextInput() { $expectedValue = << EOD; $this->activeField->textInput(); $this->assertEquals($expectedValue, $this->activeField->parts['{input}']); } public function testHiddenInput() { $expectedValue = << EOD; $this->activeField->hiddenInput(); $this->assertEquals($expectedValue, $this->activeField->parts['{input}']); } public function testListBox() { $expectedValue = << EOD; $this->activeField->listBox(["1" => "Item One", "2" => "Item 2"]); $this->assertEquals($expectedValue, $this->activeField->parts['{input}']); } public function testGetClientOptionsReturnEmpty() { // setup: we want the real deal here! $this->activeField->setClientOptionsEmpty(false); // expected empty $actualValue = $this->activeField->getClientOptions(); $this->assertTrue(empty($actualValue) === true); } public function testGetClientOptionsWithActiveAttributeInScenario() { $this->activeField->setClientOptionsEmpty(false); $this->activeField->model->addRule($this->attributeName, 'yiiunit\framework\widgets\TestValidator'); $this->activeField->form->enableClientValidation = false; // expected empty $actualValue = $this->activeField->getClientOptions(); $this->assertTrue(empty($actualValue) === true); } public function testGetClientOptionsClientValidation() { $this->activeField->setClientOptionsEmpty(false); $this->activeField->model->addRule($this->attributeName, 'yiiunit\framework\widgets\TestValidator'); $this->activeField->enableClientValidation = true; $actualValue = $this->activeField->getClientOptions(); $expectedJsExpression = "function (attribute, value, messages, deferred) {return true;}"; $this->assertEquals($expectedJsExpression, $actualValue['validate']); $this->assertTrue(!isset($actualValue['validateOnChange'])); $this->assertTrue(!isset($actualValue['validateOnBlur'])); $this->assertTrue(!isset($actualValue['validateOnType'])); $this->assertTrue(!isset($actualValue['validationDelay'])); $this->assertTrue(!isset($actualValue['enableAjaxValidation'])); $this->activeField->validateOnChange = $expectedValidateOnChange = false; $this->activeField->validateOnBlur = $expectedValidateOnBlur = false; $this->activeField->validateOnType = $expectedValidateOnType = true; $this->activeField->validationDelay = $expectedValidationDelay = 100; $this->activeField->enableAjaxValidation = $expectedEnableAjaxValidation = true; $actualValue = $this->activeField->getClientOptions(); $this->assertTrue($expectedValidateOnChange === $actualValue['validateOnChange']); $this->assertTrue($expectedValidateOnBlur === $actualValue['validateOnBlur']); $this->assertTrue($expectedValidateOnType === $actualValue['validateOnType']); $this->assertTrue($expectedValidationDelay === $actualValue['validationDelay']); $this->assertTrue($expectedEnableAjaxValidation === $actualValue['enableAjaxValidation']); } public function testGetClientOptionsValidatorWhenClientSet() { $this->activeField->setClientOptionsEmpty(false); $this->activeField->enableAjaxValidation = true; $this->activeField->model->addRule($this->attributeName, 'yiiunit\framework\widgets\TestValidator'); foreach($this->activeField->model->validators as $validator) { $validator->whenClient = "function (attribute, value) { return 'yii2' == 'yii2'; }"; // js } $actualValue = $this->activeField->getClientOptions(); $expectedJsExpression = "function (attribute, value, messages, deferred) {if ((function (attribute, value) " . "{ return 'yii2' == 'yii2'; })(attribute, value)) { return true; }}"; $this->assertEquals($expectedJsExpression, $actualValue['validate']->expression); } /** * Helper methods */ protected function getView() { $view = new View(); $view->setAssetManager(new AssetManager([ 'basePath' => '@testWebRoot/assets', 'baseUrl' => '@testWeb/assets', ])); return $view; } } /** * Helper Classes */ class ActiveFieldExtend extends ActiveField { private $getClientOptionsEmpty; public function __construct($getClientOptionsEmpty = true) { $this->getClientOptionsEmpty = $getClientOptionsEmpty; } public function setClientOptionsEmpty($value) { $this->getClientOptionsEmpty = (bool) $value; } /** * Useful to test other methods from ActiveField, that call ActiveField::getClientOptions() * but it's return value is not relevant for the test being run. */ public function getClientOptions() { return ($this->getClientOptionsEmpty) ? [] : parent::getClientOptions(); } } class TestValidator extends \yii\validators\Validator { public function clientValidateAttribute($object, $attribute, $view) { return "return true;"; } public function setWhenClient($js) { $this->whenClient = $js; } }