Yii2 framework backup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
6.6 KiB

<?php
/**
Added php-cs-fixer coding standards validation to Travis CI (#14100) * php-cs-fixer: PSR2 rule. * php-cs-fixer: PSR2 rule - fix views. * Travis setup refactoring. * Add php-cs-fixer to travis cs tests. * Fix tests on hhvm-3.12 * improve travis config * composer update * revert composer update * improve travis config * Fix CS. * Extract config to separate classes. * Extract config to separate classes. * Add file header. * Force short array syntax. * binary_operator_spaces fixer * Fix broken tests * cast_spaces fixer * concat_space fixer * dir_constant fixer * ereg_to_preg fixer * function_typehint_space fixer * hash_to_slash_comment fixer * is_null fixer * linebreak_after_opening_tag fixer * lowercase_cast fixer * magic_constant_casing fixer * modernize_types_casting fixer * native_function_casing fixer * new_with_braces fixer * no_alias_functions fixer * no_blank_lines_after_class_opening fixer * no_blank_lines_after_phpdoc fixer * no_empty_comment fixer * no_empty_phpdoc fixer * no_empty_statement fixer * no_extra_consecutive_blank_lines fixer * no_leading_import_slash fixer * no_leading_namespace_whitespace fixer * no_mixed_echo_print fixer * no_multiline_whitespace_around_double_arrow fixer * no_multiline_whitespace_before_semicolons fixer * no_php4_constructor fixer * no_short_bool_cast fixer * no_singleline_whitespace_before_semicolons fixer * no_spaces_around_offset fixer * no_trailing_comma_in_list_call fixer * no_trailing_comma_in_singleline_array fixer * no_unneeded_control_parentheses fixer * no_unused_imports fixer * no_useless_return fixer * no_whitespace_before_comma_in_array fixer * no_whitespace_in_blank_line fixer * not_operator_with_successor_space fixer * object_operator_without_whitespace fixer * ordered_imports fixer * php_unit_construct fixer * php_unit_dedicate_assert fixer * php_unit_fqcn_annotation fixer * phpdoc_indent fixer * phpdoc_no_access fixer * phpdoc_no_empty_return fixer * phpdoc_no_package fixer * phpdoc_no_useless_inheritdoc fixer * Fix broken tests * phpdoc_return_self_reference fixer * phpdoc_single_line_var_spacing fixer * phpdoc_single_line_var_spacing fixer * phpdoc_to_comment fixer * phpdoc_trim fixer * phpdoc_var_without_name fixer * psr4 fixer * self_accessor fixer * short_scalar_cast fixer * single_blank_line_before_namespace fixer * single_quote fixer * standardize_not_equals fixer * ternary_operator_spaces fixer * trailing_comma_in_multiline_array fixer * trim_array_spaces fixer * protected_to_private fixer * unary_operator_spaces fixer * whitespace_after_comma_in_array fixer * `parent::setRules()` -> `$this->setRules()` * blank_line_after_opening_tag fixer * Update finder config. * Revert changes for YiiRequirementChecker. * Fix array formatting. * Add missing import. * Fix CS for new code merged from master. * Fix some indentation issues.
7 years ago
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\framework\widgets;
use yii\base\DynamicModel;
8 years ago
use yii\base\Widget;
use yii\web\View;
use yii\widgets\ActiveForm;
/**
* @group widgets
*/
class ActiveFormTest extends \yiiunit\TestCase
{
protected function setUp()
{
parent::setUp();
$this->mockApplication();
}
public function testBooleanAttributes()
{
$o = ['template' => '{input}'];
$model = new DynamicModel(['name']);
ob_start();
$form = ActiveForm::begin(['action' => '/something', 'enableClientScript' => false]);
ActiveForm::end();
ob_end_clean();
$this->assertEqualsWithoutLE(<<<'EOF'
<div class="form-group field-dynamicmodel-name">
<input type="email" id="dynamicmodel-name" class="form-control" name="DynamicModel[name]" required>
</div>
EOF
, (string) $form->field($model, 'name', $o)->input('email', ['required' => true]));
$this->assertEqualsWithoutLE(<<<'EOF'
<div class="form-group field-dynamicmodel-name">
<input type="email" id="dynamicmodel-name" class="form-control" name="DynamicModel[name]">
</div>
EOF
, (string) $form->field($model, 'name', $o)->input('email', ['required' => false]));
$this->assertEqualsWithoutLE(<<<'EOF'
<div class="form-group field-dynamicmodel-name">
<input type="email" id="dynamicmodel-name" class="form-control" name="DynamicModel[name]" required="test">
</div>
EOF
, (string) $form->field($model, 'name', $o)->input('email', ['required' => 'test']));
}
public function testIssue5356()
{
10 years ago
$o = ['template' => '{input}'];
$model = new DynamicModel(['categories']);
$model->categories = 1;
ob_start();
$form = ActiveForm::begin(['action' => '/something', 'enableClientScript' => false]);
ActiveForm::end();
ob_end_clean();
// https://github.com/yiisoft/yii2/issues/5356
$this->assertEqualsWithoutLE(<<<'EOF'
10 years ago
<div class="form-group field-dynamicmodel-categories">
<input type="hidden" name="DynamicModel[categories]" value=""><select id="dynamicmodel-categories" class="form-control" name="DynamicModel[categories][]" multiple size="4">
<option value="0">apple</option>
<option value="1" selected>banana</option>
<option value="2">avocado</option>
</select>
</div>
EOF
10 years ago
, (string) $form->field($model, 'categories', $o)->listBox(['apple', 'banana', 'avocado'], ['multiple' => true]));
}
public function testOutputBuffering()
{
$obLevel = ob_get_level();
ob_start();
$model = new DynamicModel(['name']);
$form = ActiveForm::begin(['id' => 'someform', 'action' => '/someform', 'enableClientScript' => false]);
echo "\n" . $form->field($model, 'name') . "\n";
ActiveForm::end();
$content = ob_get_clean();
$this->assertEquals($obLevel, ob_get_level(), 'Output buffers not closed correctly.');
$this->assertEqualsWithoutLE(<<<'HTML'
<form id="someform" action="/someform" method="post">
<div class="form-group field-dynamicmodel-name">
<label class="control-label" for="dynamicmodel-name">Name</label>
<input type="text" id="dynamicmodel-name" class="form-control" name="DynamicModel[name]">
<div class="help-block"></div>
</div>
</form>
HTML
, $content);
}
public function testRegisterClientScript()
{
$this->mockWebApplication();
$_SERVER['REQUEST_URI'] = 'http://example.com/';
$model = new DynamicModel(['name']);
$model->addRule(['name'], 'required');
$view = $this->getMockBuilder(View::className())->getMock();
8 years ago
$view->method('registerJs')->with($this->matches("jQuery('#w0').yiiActiveForm([], {\"validateOnSubmit\":false});"));
$view->method('registerAssetBundle')->willReturn(true);
8 years ago
Widget::$counter = 0;
ob_start();
ob_implicit_flush(false);
$form = ActiveForm::begin(['view' => $view, 'validateOnSubmit' => false]);
$form->field($model, 'name');
$form::end();
// Disable clientScript will not call `View->registerJs()`
$form = ActiveForm::begin(['view' => $view, 'enableClientScript' => false]);
$form->field($model, 'name');
$form::end();
8 years ago
ob_get_clean();
}
/**
* @see https://github.com/yiisoft/yii2/issues/15536
*/
public function testShouldTriggerInitEvent()
{
$initTriggered = false;
ob_start();
$form = ActiveForm::begin(
[
'action' => '/something',
'enableClientScript' => false,
'on init' => function () use (&$initTriggered) {
$initTriggered = true;
}
]
);
ActiveForm::end();
ob_end_clean();
$this->assertTrue($initTriggered);
}
/**
* @see https://github.com/yiisoft/yii2/issues/15476
* @see https://github.com/yiisoft/yii2/issues/16892
*/
public function testValidationStateOnInput()
{
$model = new DynamicModel(['name']);
$model->addError('name', 'I have an error!');
ob_start();
$form = ActiveForm::begin([
'action' => '/something',
'enableClientScript' => false,
'validationStateOn' => ActiveForm::VALIDATION_STATE_ON_INPUT,
]);
ActiveForm::end();
ob_end_clean();
$this->assertEqualsWithoutLE(<<<'EOF'
<div class="form-group field-dynamicmodel-name">
<label class="control-label" for="dynamicmodel-name">Name</label>
<input type="text" id="dynamicmodel-name" class="form-control has-error" name="DynamicModel[name]" aria-invalid="true">
<div class="help-block">I have an error!</div>
</div>
EOF
, (string) $form->field($model, 'name'));
$this->assertEqualsWithoutLE(<<<'EOF'
<div class="form-group field-dynamicmodel-name">
<input type="hidden" name="DynamicModel[name]" value="0"><label><input type="checkbox" id="dynamicmodel-name" class="has-error" name="DynamicModel[name]" value="1" aria-invalid="true"> Name</label>
<div class="help-block">I have an error!</div>
</div>
EOF
, (string) $form->field($model, 'name')->checkbox());
$this->assertEqualsWithoutLE(<<<'EOF'
<div class="form-group field-dynamicmodel-name">
<input type="hidden" name="DynamicModel[name]" value="0"><label><input type="radio" id="dynamicmodel-name" class="has-error" name="DynamicModel[name]" value="1" aria-invalid="true"> Name</label>
<div class="help-block">I have an error!</div>
</div>
EOF
, (string) $form->field($model, 'name')->radio());
}
}