yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

[Bug test] yiiunit\framework\validators\DateValidatorTest #8584

Closed mdmunir closed 9 years ago

mdmunir commented 9 years ago

Please verify this lines https://github.com/yiisoft/yii2/blob/master/tests/framework/validators/DateValidatorTest.php#L421-L423. Your date are '14-09-13' and your format 'yyyy-MM-dd' but you use assertTrue().

klimov-paul commented 9 years ago

Date '14-09-13' matches format 'yyyy-MM-dd' just fine: it represents 14 year A.D. (not 2014!). Everything is fine.

mdmunir commented 9 years ago

screenshot from 2015-05-28 14 15 18

OK, am i miss something?

klimov-paul commented 9 years ago

Unit tests are passing correctly on Travis: https://travis-ci.org/yiisoft/yii2/builds/64257842

mdmunir commented 9 years ago

The point is, Date '14-09-13' and format 'yyyy-MM-dd' did not matches in any situasion. its may depend with php config (i am not sure). So, when i have rule

[
    [['date'], 'date', 'format'=>'yyyy-MM-dd'],

]

then my input are $model->date = '14-09-13', they can be valid and can be not. CMIIW

cebe commented 9 years ago

it depends on the version of ICU and intl extension. It is a known problem which we can not solve. You may use the PHP format: php:Y-m-d instead which will cover your case. See also https://github.com/yiisoft/yii2/blob/a003a8fb487dfa60c0f88ecfacf18a7407ced18b/framework/validators/DateValidator.php#L51-L57

mdmunir commented 9 years ago

@cebe I have no case with DateValidator :D. My case is phpunit not pass.

cebe commented 9 years ago

what is the ICU version compiled with PHP? here is how you get it: http://www.yiiframework.com/doc-2.0/guide-tutorial-i18n.html#setup-environment

mdmunir commented 9 years ago
PHP: 5.4.39-0+deb7u2
ICU: 4.8.1.1
cebe commented 9 years ago

Hm... I have the exact same ICU version on my system and the test passes...

mdmunir commented 9 years ago

i dont know way, but when i check

            if (extension_loaded('intl')) {
                echo "yes\n";
                return $this->parseDateValueIntl($value, $format);
            } else {
                echo "no\n";
                // fallback to PHP if intl is not installed
                $format = FormatConverter::convertDateIcuToPhp($format, 'date');
            }

sometime yes, sometime no. maybe my phpunit.

mdmunir commented 9 years ago

Its realy not make sense for me. When i create new test in base\ModelTest

    public function testDateValidator()
    {
        $date = '14-09-13';

        $model = new \yii\base\DynamicModel([
            'date' => $date,
        ]);
        $model->addRule('date', 'date', ['format'=>'yyyy-MM-dd']);
        $this->assertTrue($model->validate(),"$date is valid");

        $val = new \yii\validators\DateValidator(['format'=>'yyyy-MM-dd']);
        $this->assertTrue($val->validate($date),"$date is valid");

    }

the test passes... but not in validators\DateValidatorTest. :disappointed_relieved: Please dont close this issue. I want to investigate it more

mdmunir commented 9 years ago

Test not pass when not using extension intl. Mean when this return false. This code

        $date = '14-09-13';
        $val = new DateValidator(['format' => 'php:Y-m-d']);
        $this->assertTrue($val->validate($date), "$date is valid");

also not pass. Is this normal?

mdmunir commented 9 years ago

I think i know what happen, but i dont know how to solve it. This code, work OK in other machine but not in my local.

$format = 'Y-m-d';
$value = '14-09-13';

$hasTimeInfo = (strpbrk($format, 'HhGgis') !== false);
$date = DateTime::createFromFormat($format, $value, new DateTimeZone($hasTimeInfo ? 'Asia/Jakarta' : 'UTC'));
$errors = DateTime::getLastErrors();
if ($date === false || $errors['error_count'] || $errors['warning_count']) {
    echo 'return false';
    print_r($error);
    return;
}

if (!$hasTimeInfo) {
    $date->setTime(0, 0, 0);
}
echo $date->getTimestamp();

No error but $date->getTimestamp() return false. @klimov-paul , @cebe , any suggestion for me? http://3v4l.org/qLMTg

mdmunir commented 9 years ago

$date->getTimestamp() return false if value < -2^31

nkovacs commented 9 years ago

Are you running the failing test on a 32bit system?

mdmunir commented 9 years ago

My OS

Debian
Release 7.8 (wheezy) 32-bit
Kernel Linux 3.2.0-4-686-pae

PHP

PHP Version 5.4.39-0+deb7u2
nkovacs commented 9 years ago

Then that's the problem. On a 32 bit machine you won't be able to parse dates before 1901-12-13 and after 2038-01-19. The point of the test case was just to test that really old dates are valid without a range. It could be skipped on 32 bit php.

As for the intl extension, when the test function's name begins with testIntl, the intl extension is used, otherwise it's not. That's why you sometimes got yes and sometimes no.

The intl tests didn't fail because your intl is old, and it parses 14 as 2014, so it didn't run out of the 32 bit range.

cebe commented 9 years ago

@mdmunir I have adjusted the test, let me know if it passes now

nkovacs commented 9 years ago

You also need to fix testValidateAttributeRange

cebe commented 9 years ago

Good catch, thanks!

mdmunir commented 9 years ago

Exception :D

Time: 596 ms, Memory: 6.25Mb

There were 2 errors:

1) yiiunit\framework\validators\DateValidatorTest::testValidateValueRange
yii\base\InvalidConfigException: Invalid min date value: 1900-01-01

/home/mdmunir/NetBeansProjects/mdmunir/yii2/framework/validators/DateValidator.php:199
/home/mdmunir/NetBeansProjects/mdmunir/yii2/framework/base/Object.php:107
/home/mdmunir/NetBeansProjects/mdmunir/yii2/tests/framework/validators/DateValidatorTest.php:429
/home/mdmunir/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:152
/home/mdmunir/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:104
/home/mdmunir/.composer/vendor/phpunit/phpunit/phpunit:36

2) yiiunit\framework\validators\DateValidatorTest::testValidateAttributeRange
yii\base\InvalidConfigException: Invalid min date value: 1900-01-01

/home/mdmunir/NetBeansProjects/mdmunir/yii2/framework/validators/DateValidator.php:199
/home/mdmunir/NetBeansProjects/mdmunir/yii2/framework/base/Object.php:107
/home/mdmunir/NetBeansProjects/mdmunir/yii2/tests/framework/validators/DateValidatorTest.php:473
/home/mdmunir/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:152
/home/mdmunir/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:104
/home/mdmunir/.composer/vendor/phpunit/phpunit/phpunit:36

FAILURES!
Tests: 146, Assertions: 799, Errors: 2, Skipped: 2.
cebe commented 9 years ago

try again :)

mdmunir commented 9 years ago

work OK