DateValidatorTest.php 6.53 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 84
        $this->assertFalse($val->validate('asdasdfasfd'));
        $this->assertFalse($val->validate(''));
85
        $this->assertFalse($val->validate(time()));
86
        $val->format = 'php:U';
87
        $this->assertTrue($val->validate(time()));
88 89 90 91 92 93 94 95 96 97 98
        $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'));
99 100
        $this->assertFalse($val->validate('asdasdfasfd'));
        $this->assertFalse($val->validate(''));
101 102
        $this->assertFalse($val->validate(time()));
        $val->format = 'dd.MM.yyyy';
103
        $this->assertTrue($val->validate('31.7.2013'));
104
        $val->format = 'yyyy-MM-dd HH:mm:ss';
105 106
        $this->assertTrue($val->validate('2009-02-15 15:16:17'));
    }
Suralc committed
107

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

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

    }

145
    public function testIntlValidateAttributeICUFormat()
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
    {
        $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']);
176 177 178
        $model = FakedValidationModel::createWithAttributes(['attr_date' => []]);
        $val->validateAttribute($model, 'attr_date');
        $this->assertTrue($model->hasErrors('attr_date'));
Suralc committed
179

180
    }
Qiang Xue committed
181
}