mockApplication([ 'timeZone' => 'UTC', 'language' => 'ru-RU', ]); } protected function tearDown() { parent::tearDown(); IntlTestHelper::resetIntlStatus(); } public function testEnsureMessageIsSet() { $val = new DateValidator; $this->assertTrue($val->message !== null && strlen($val->message) > 1); } 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')); } public function testValidateValue() { // test PHP format $val = new DateValidator(['format' => 'php:Y-m-d']); $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')); $this->assertFalse($val->validate('20121212')); $this->assertFalse($val->validate('asdasdfasfd')); $this->assertFalse($val->validate('2012-12-12foo')); $this->assertFalse($val->validate('')); $this->assertFalse($val->validate(time())); $val->format = 'php:U'; $this->assertTrue($val->validate(time())); $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')); $this->assertFalse($val->validate('20121212')); $this->assertFalse($val->validate('asdasdfasfd')); $this->assertFalse($val->validate('2012-12-12foo')); $this->assertFalse($val->validate('')); $this->assertFalse($val->validate(time())); $val->format = 'dd.MM.yyyy'; $this->assertTrue($val->validate('31.7.2013')); $val->format = 'yyyy-MM-dd HH:mm:ss'; $this->assertTrue($val->validate('2009-02-15 15:16:17')); } public function testIntlValidateAttributePHPFormat() { $this->testValidateAttributePHPFormat(); } public function testValidateAttributePHPFormat() { // error-array-add $val = new DateValidator(['format' => 'php:Y-m-d']); $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' => 'php:Y-m-d', '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' => 'php:Y-m-d']); $model = FakedValidationModel::createWithAttributes(['attr_date' => []]); $val->validateAttribute($model, 'attr_date'); $this->assertTrue($model->hasErrors('attr_date')); } public function testIntlValidateAttributeICUFormat() { $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']); $model = FakedValidationModel::createWithAttributes(['attr_date' => []]); $val->validateAttribute($model, 'attr_date'); $this->assertTrue($model->hasErrors('attr_date')); $val = new DateValidator(['format' => 'yyyy-MM-dd']); $model = FakedValidationModel::createWithAttributes(['attr_date' => '2012-12-12foo']); $val->validateAttribute($model, 'attr_date'); $this->assertTrue($model->hasErrors('attr_date')); } public function testIntlMultibyteString() { $val = new DateValidator(['format' => 'dd MMM yyyy', 'locale' => 'de_DE']); $model = FakedValidationModel::createWithAttributes(['attr_date' => '12 Mai 2014']); $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $val = new DateValidator(['format' => 'dd MMM yyyy', 'locale' => 'ru_RU']); $model = FakedValidationModel::createWithAttributes(['attr_date' => '12 мая 2014']); $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); } }