DateValidatorTest.php 6.64 KB
Newer Older
Suralc committed
1 2 3 4 5 6
<?php

namespace yiiunit\framework\validators;

use DateTime;
use yii\validators\DateValidator;
7
use yiiunit\data\validators\models\FakedValidationModel;
8
use yiiunit\framework\i18n\IntlTestHelper;
Suralc committed
9 10
use yiiunit\TestCase;

11 12 13
/**
 * @group validators
 */
Suralc committed
14 15
class DateValidatorTest extends TestCase
{
16 17 18
    protected function setUp()
    {
        parent::setUp();
19 20 21 22 23 24 25 26 27 28 29 30 31

        IntlTestHelper::setIntlStatus($this);

        $this->mockApplication([
            'timeZone' => 'UTC',
            'language' => 'ru-RU',
        ]);
    }

    protected function tearDown()
    {
        parent::tearDown();
        IntlTestHelper::resetIntlStatus();
32
    }
33

34 35 36 37 38
    public function testEnsureMessageIsSet()
    {
        $val = new DateValidator;
        $this->assertTrue($val->message !== null && strlen($val->message) > 1);
    }
Suralc committed
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
    public function testIntlValidateValue()
    {
        $this->testValidateValue();

        $this->mockApplication([
            'language' => 'en-GB',
            'components' => [
                'formatter' => [
                    'dateFormat' => 'short',
                ]
            ]
        ]);
        $val = new DateValidator();
        $this->assertTrue($val->validate('31/5/2017'));
        $this->assertFalse($val->validate('5/31/2017'));
        $val = new DateValidator(['format' => 'short', 'locale' => 'en-GB']);
        $this->assertTrue($val->validate('31/5/2017'));
        $this->assertFalse($val->validate('5/31/2017'));

        $this->mockApplication([
            'language' => 'de-DE',
            'components' => [
                'formatter' => [
                    'dateFormat' => 'short',
                ]
            ]
        ]);
        $val = new DateValidator();
        $this->assertTrue($val->validate('31.5.2017'));
        $this->assertFalse($val->validate('5.31.2017'));
        $val = new DateValidator(['format' => 'short', 'locale' => 'de-DE']);
        $this->assertTrue($val->validate('31.5.2017'));
        $this->assertFalse($val->validate('5.31.2017'));
    }

75 76
    public function testValidateValue()
    {
77 78
        // test PHP format
        $val = new DateValidator(['format' => 'php:Y-m-d']);
79 80 81 82
        $this->assertFalse($val->validate('3232-32-32'));
        $this->assertTrue($val->validate('2013-09-13'));
        $this->assertFalse($val->validate('31.7.2013'));
        $this->assertFalse($val->validate('31-7-2013'));
83
        $this->assertFalse($val->validate('asdasdfasfd'));
Carsten Brandt committed
84
        $this->assertFalse($val->validate('20121212'));
85
        $this->assertFalse($val->validate(''));
86
        $this->assertFalse($val->validate(time()));
87
        $val->format = 'php:U';
88
        $this->assertTrue($val->validate(time()));
89 90 91 92 93 94 95 96 97 98 99
        $val->format = 'php:d.m.Y';
        $this->assertTrue($val->validate('31.7.2013'));
        $val->format = 'php:Y-m-!d H:i:s';
        $this->assertTrue($val->validate('2009-02-15 15:16:17'));

        // test ICU format
        $val = new DateValidator(['format' => 'yyyy-MM-dd']);
        $this->assertFalse($val->validate('3232-32-32'));
        $this->assertTrue($val->validate('2013-09-13'));
        $this->assertFalse($val->validate('31.7.2013'));
        $this->assertFalse($val->validate('31-7-2013'));
Carsten Brandt committed
100
        $this->assertFalse($val->validate('20121212'));
101 102
        $this->assertFalse($val->validate('asdasdfasfd'));
        $this->assertFalse($val->validate(''));
103 104
        $this->assertFalse($val->validate(time()));
        $val->format = 'dd.MM.yyyy';
105
        $this->assertTrue($val->validate('31.7.2013'));
106
        $val->format = 'yyyy-MM-dd HH:mm:ss';
107 108
        $this->assertTrue($val->validate('2009-02-15 15:16:17'));
    }
Suralc committed
109

110
    public function testIntlValidateAttributePHPFormat()
111 112 113 114 115
    {
        $this->testValidateAttributePHPFormat();
    }

    public function testValidateAttributePHPFormat()
116 117
    {
        // error-array-add
118
        $val = new DateValidator(['format' => 'php:Y-m-d']);
119 120 121 122 123 124 125 126 127
        $model = new FakedValidationModel;
        $model->attr_date = '2013-09-13';
        $val->validateAttribute($model, 'attr_date');
        $this->assertFalse($model->hasErrors('attr_date'));
        $model = new FakedValidationModel;
        $model->attr_date = '1375293913';
        $val->validateAttribute($model, 'attr_date');
        $this->assertTrue($model->hasErrors('attr_date'));
        //// timestamp attribute
128
        $val = new DateValidator(['format' => 'php:Y-m-d', 'timestampAttribute' => 'attr_timestamp']);
129 130 131 132 133 134 135
        $model = new FakedValidationModel;
        $model->attr_date = '2013-09-13';
        $model->attr_timestamp = true;
        $val->validateAttribute($model, 'attr_date');
        $this->assertFalse($model->hasErrors('attr_date'));
        $this->assertFalse($model->hasErrors('attr_timestamp'));
        $this->assertEquals(
136 137
             mktime(0, 0, 0, 9, 13, 2013), // 2013-09-13
//            DateTime::createFromFormat('Y-m-d', '2013-09-13')->getTimestamp(),
138 139
            $model->attr_timestamp
        );
140 141 142 143 144 145 146
        $val = new DateValidator(['format' => 'php:Y-m-d']);
        $model = FakedValidationModel::createWithAttributes(['attr_date' => []]);
        $val->validateAttribute($model, 'attr_date');
        $this->assertTrue($model->hasErrors('attr_date'));

    }

147
    public function testIntlValidateAttributeICUFormat()
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
    {
        $this->testValidateAttributeICUFormat();
    }

    public function testValidateAttributeICUFormat()
    {
        // error-array-add
        $val = new DateValidator(['format' => 'yyyy-MM-dd']);
        $model = new FakedValidationModel;
        $model->attr_date = '2013-09-13';
        $val->validateAttribute($model, 'attr_date');
        $this->assertFalse($model->hasErrors('attr_date'));
        $model = new FakedValidationModel;
        $model->attr_date = '1375293913';
        $val->validateAttribute($model, 'attr_date');
        $this->assertTrue($model->hasErrors('attr_date'));
        //// timestamp attribute
        $val = new DateValidator(['format' => 'yyyy-MM-dd', 'timestampAttribute' => 'attr_timestamp']);
        $model = new FakedValidationModel;
        $model->attr_date = '2013-09-13';
        $model->attr_timestamp = true;
        $val->validateAttribute($model, 'attr_date');
        $this->assertFalse($model->hasErrors('attr_date'));
        $this->assertFalse($model->hasErrors('attr_timestamp'));
        $this->assertEquals(
            mktime(0, 0, 0, 9, 13, 2013), // 2013-09-13
//            DateTime::createFromFormat('Y-m-d', '2013-09-13')->getTimestamp(),
            $model->attr_timestamp
        );
        $val = new DateValidator(['format' => 'yyyy-MM-dd']);
178 179 180
        $model = FakedValidationModel::createWithAttributes(['attr_date' => []]);
        $val->validateAttribute($model, 'attr_date');
        $this->assertTrue($model->hasErrors('attr_date'));
Suralc committed
181

182
    }
Qiang Xue committed
183
}