ActiveFieldTest.php 12.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
<?php

namespace yiiunit\framework\widgets;

use Yii;
use yii\widgets\ActiveField;
use yii\base\DynamicModel;
use yii\widgets\ActiveForm;
use yii\web\View;
use yii\web\AssetManager;

/**
 * @author Nelson J Morais <njmorais@gmail.com>
 * 
 * @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";
        
Qiang Xue committed
30
        $this->mockWebApplication();
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
        
        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
<div class="form-group field-dynamicmodel-attributename">
<label class="control-label" for="dynamicmodel-attributename">Attribute Name</label>
<input type="text" id="dynamicmodel-attributename" class="form-control" name="DynamicModel[{$this->attributeName}]">

<div class="help-block"></div>
</div>
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 "<div class=\"custom-container\"> $field </div>";
        };

        $expectedValue = <<<EOD
<div class="form-group field-dynamicmodel-attributename">
<div class="custom-container"> <div class="form-group field-dynamicmodel-attributename">
<label class="control-label" for="dynamicmodel-attributename">Attribute Name</label>
<input type="text" id="dynamicmodel-attributename" class="form-control" name="DynamicModel[{$this->attributeName}]">

<div class="help-block"></div>
</div> </div>
</div>
EOD;
        
        $actualValue = $this->activeField->render($content);
        $this->assertEquals($expectedValue, $actualValue);
    }
    
    public function testBeginHasErros()
    {
        $this->helperModel->addError($this->attributeName, "Error Message");
        
        $expectedValue = '<div class="form-group field-dynamicmodel-attributename has-error">';
        $actualValue = $this->activeField->begin();
        
        $this->assertEquals($expectedValue, $actualValue);
    }
    
    public function testBeginAttributeIsRequered()
    {
        $this->helperModel->addRule($this->attributeName, 'required');
        
        $expectedValue = '<div class="form-group field-dynamicmodel-attributename required">';
        $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 = '<div class="form-group field-dynamicmodel-attributename required has-error">';
        $actualValue = $this->activeField->begin();
        
        $this->assertEquals($expectedValue, $actualValue);
    }
    
    public function testEnd()
    {
        $expectedValue = '</div>';
        $actualValue = $this->activeField->end();
        
        $this->assertEquals($expectedValue, $actualValue);
        
        // other tag
        $expectedValue = "</article>";
        $this->activeField->options['tag'] = 'article';
        $actualValue = $this->activeField->end();
        
        $this->assertTrue($actualValue === $expectedValue);
    }
    
    public function testLabel()
    {
        $expectedValue = '<label class="control-label" for="dynamicmodel-attributename">Attribute Name</label>';
        $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 = <<<EOT
<label class="control-label" for="dynamicmodel-attributename">{$label}</label>
EOT;
        $this->activeField->label($label);
        
        $this->assertEquals($expectedValue, $this->activeField->parts['{label}']);
    }

    
    public function testError()
    {
        $expectedValue = '<label class="control-label" for="dynamicmodel-attributename">Attribute Name</label>';
        $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 = <<<EOT
<label class="control-label" for="dynamicmodel-attributename">{$label}</label>
EOT;
        $this->activeField->label($label);
        
        $this->assertEquals($expectedValue, $this->activeField->parts['{label}']);
    }

    public function testHint()
    {
        $expectedValue = '<div class="hint-block">Hint Content</div>';
        $this->activeField->hint('Hint Content');
        
        $this->assertEquals($expectedValue, $this->activeField->parts['{hint}']);
    }
    
    public function testInput()
    {
        $expectedValue = <<<EOD
<input type="password" id="dynamicmodel-attributename" class="form-control" name="DynamicModel[attributeName]">
EOD;
        $this->activeField->input("password");
        
        $this->assertEquals($expectedValue, $this->activeField->parts['{input}']); 
        
        // with options
        $expectedValue = <<<EOD
<input type="password" id="dynamicmodel-attributename" class="form-control" name="DynamicModel[attributeName]" weird="value">
EOD;
        $this->activeField->input("password", ['weird' => 'value']);
        
        $this->assertEquals($expectedValue, $this->activeField->parts['{input}']);         
    }
    
    public function testTextInput()
    {
        $expectedValue = <<<EOD
<input type="text" id="dynamicmodel-attributename" class="form-control" name="DynamicModel[attributeName]">
EOD;
        $this->activeField->textInput();
        $this->assertEquals($expectedValue, $this->activeField->parts['{input}']);
    }
    
    public function testHiddenInput()
    {
        $expectedValue = <<<EOD
<input type="hidden" id="dynamicmodel-attributename" class="form-control" name="DynamicModel[attributeName]">
EOD;
        $this->activeField->hiddenInput();
        $this->assertEquals($expectedValue, $this->activeField->parts['{input}']);
    }
    
    public function testListBox()
    {
        $expectedValue = <<<EOD
<input type="hidden" name="DynamicModel[attributeName]" value=""><select id="dynamicmodel-attributename" class="form-control" name="DynamicModel[attributeName]" size="4">
<option value="1">Item One</option>
<option value="2">Item 2</option>
</select>
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();
269
        $expectedJsExpression = "function (attribute, value, messages, deferred) {return true;}";
Qiang Xue committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283
        $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;

284
        $actualValue = $this->activeField->getClientOptions();
Qiang Xue committed
285

286
        $this->assertTrue($expectedValidateOnChange === $actualValue['validateOnChange']);
287
        $this->assertTrue($expectedValidateOnBlur === $actualValue['validateOnBlur']);
288
        $this->assertTrue($expectedValidateOnType === $actualValue['validateOnType']);
Qiang Xue committed
289 290
        $this->assertTrue($expectedValidationDelay === $actualValue['validationDelay']);         
        $this->assertTrue($expectedEnableAjaxValidation === $actualValue['enableAjaxValidation']);
291
    }
Qiang Xue committed
292

293 294 295 296 297 298 299 300 301 302 303
    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();
Qiang Xue committed
304 305
        $expectedJsExpression = "function (attribute, value, messages, deferred) {if ((function (attribute, value) "
            . "{ return 'yii2' == 'yii2'; })(attribute, value)) { return true; }}";
306
       
Qiang Xue committed
307 308
        $this->assertEquals($expectedJsExpression, $actualValue['validate']->expression);
    }
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    
    /**
     * 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;
    }
    
    /**
344
     * Useful to test other methods from ActiveField, that call ActiveField::getClientOptions()
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
     * 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;
    }
}