From 92c66758ded0d2a79b790084de08b2edb2495d72 Mon Sep 17 00:00:00 2001 From: Pavel Ivanov Date: Sat, 5 Jan 2019 01:45:49 +0200 Subject: [PATCH] Added tests for DateValidator when no time is specified for 'format' (#17018) Related issues: * #10737 * #14299 * #14795 --- .../validators/DateValidatorTest.php | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/tests/framework/validators/DateValidatorTest.php b/tests/framework/validators/DateValidatorTest.php index 14f41f0618..ac2a6a89f4 100644 --- a/tests/framework/validators/DateValidatorTest.php +++ b/tests/framework/validators/DateValidatorTest.php @@ -485,6 +485,143 @@ class DateValidatorTest extends TestCase $this->assertSame('2013-09-13 10:23:15', $model->attr_timestamp); } + /** + * The following cases (when no time is specified for 'format') usually raise questions. + * See the discussion here: https://github.com/yiisoft/yii2/issues/14795 + * + * @dataProvider provideTimezones + * @param string $timezone + */ + public function testValidationWithoutTime($timezone) + { + date_default_timezone_set($timezone); + + // timeZone => UTC, timestampAttributeTimeZone => + $val = new DateValidator([ + 'format' => 'yyyy-MM-dd', + 'timestampAttribute' => 'attr_timestamp', + 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', + 'timeZone' => 'UTC', + ]); + $model = new FakedValidationModel(); + $model->attr_date = '2017-06-15'; + $model->attr_timestamp = true; + $val->validateAttribute($model, 'attr_date'); + $this->assertFalse($model->hasErrors('attr_date')); + $this->assertFalse($model->hasErrors('attr_timestamp')); + $this->assertSame('2017-06-15 00:00:00', $model->attr_timestamp); + $val = new DateValidator([ + 'format' => 'php:Y-m-d', + 'timestampAttribute' => 'attr_timestamp', + 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', + 'timeZone' => 'UTC', + ]); + $model = new FakedValidationModel(); + $model->attr_date = '2017-06-15'; + $model->attr_timestamp = true; + $val->validateAttribute($model, 'attr_date'); + $this->assertFalse($model->hasErrors('attr_date')); + $this->assertFalse($model->hasErrors('attr_timestamp')); + $this->assertSame('2017-06-15 00:00:00', $model->attr_timestamp); + + // timeZone => Europe/Berlin, timestampAttributeTimeZone => + $val = new DateValidator([ + 'format' => 'yyyy-MM-dd', + 'timestampAttribute' => 'attr_timestamp', + 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', + 'timeZone' => 'Europe/Berlin', + ]); + $model = new FakedValidationModel(); + $model->attr_date = '2017-06-15'; + $model->attr_timestamp = true; + $val->validateAttribute($model, 'attr_date'); + $this->assertFalse($model->hasErrors('attr_date')); + $this->assertFalse($model->hasErrors('attr_timestamp')); + $this->assertSame('2017-06-15 00:00:00', $model->attr_timestamp); + $val = new DateValidator([ + 'format' => 'php:Y-m-d', + 'timestampAttribute' => 'attr_timestamp', + 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', + 'timeZone' => 'Europe/Berlin', + ]); + $model = new FakedValidationModel(); + $model->attr_date = '2017-06-15'; + $model->attr_timestamp = true; + $val->validateAttribute($model, 'attr_date'); + $this->assertFalse($model->hasErrors('attr_date')); + $this->assertFalse($model->hasErrors('attr_timestamp')); + $this->assertSame('2017-06-15 00:00:00', $model->attr_timestamp); + + // timeZone => UTC, timestampAttributeTimeZone => Europe/Berlin + $val = new DateValidator([ + 'format' => 'yyyy-MM-dd', + 'timestampAttribute' => 'attr_timestamp', + 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', + 'timeZone' => 'UTC', + 'timestampAttributeTimeZone' => 'Europe/Berlin', + ]); + $model = new FakedValidationModel(); + $model->attr_date = '2017-06-15'; + $model->attr_timestamp = true; + $val->validateAttribute($model, 'attr_date'); + $this->assertFalse($model->hasErrors('attr_date')); + $this->assertFalse($model->hasErrors('attr_timestamp')); + $this->assertSame('2017-06-15 02:00:00', $model->attr_timestamp); + $val = new DateValidator([ + 'format' => 'php:Y-m-d', + 'timestampAttribute' => 'attr_timestamp', + 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', + 'timeZone' => 'UTC', + 'timestampAttributeTimeZone' => 'Europe/Berlin', + ]); + $model = new FakedValidationModel(); + $model->attr_date = '2017-06-15'; + $model->attr_timestamp = true; + $val->validateAttribute($model, 'attr_date'); + $this->assertFalse($model->hasErrors('attr_date')); + $this->assertFalse($model->hasErrors('attr_timestamp')); + $this->assertSame('2017-06-15 02:00:00', $model->attr_timestamp); + + // timeZone => Europe/Berlin, timestampAttributeTimeZone => Europe/Berlin + $val = new DateValidator([ + 'format' => 'yyyy-MM-dd', + 'timestampAttribute' => 'attr_timestamp', + 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', + 'timeZone' => 'Europe/Berlin', + 'timestampAttributeTimeZone' => 'Europe/Berlin', + ]); + $model = new FakedValidationModel(); + $model->attr_date = '2017-06-15'; + $model->attr_timestamp = true; + $val->validateAttribute($model, 'attr_date'); + $this->assertFalse($model->hasErrors('attr_date')); + $this->assertFalse($model->hasErrors('attr_timestamp')); + $this->assertSame('2017-06-15 02:00:00', $model->attr_timestamp); + $val = new DateValidator([ + 'format' => 'php:Y-m-d', + 'timestampAttribute' => 'attr_timestamp', + 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', + 'timeZone' => 'Europe/Berlin', + 'timestampAttributeTimeZone' => 'Europe/Berlin', + ]); + $model = new FakedValidationModel(); + $model->attr_date = '2017-06-15'; + $model->attr_timestamp = true; + $val->validateAttribute($model, 'attr_date'); + $this->assertFalse($model->hasErrors('attr_date')); + $this->assertFalse($model->hasErrors('attr_timestamp')); + $this->assertSame('2017-06-15 02:00:00', $model->attr_timestamp); + } + + /** + * @dataProvider provideTimezones + * @param string $timezone + */ + public function testIntlValidationWithoutTime($timezone) + { + $this->testValidationWithoutTime($timezone); + } + public function testIntlValidateRange() { $this->testValidateValueRange();