Files
opensourcepos/tests/helpers/LocaleHelperTest.php
objecttothis e6388deed8 Fix overly lenient date validation (#4574)
* Fix overly lenient date validation

- In the past date validation would roll over dates that didn't exist into the next month.  Now they return an error.

Signed-off-by: objec <objecttothis@gmail.com>

* Refactor naming

- Refactor parameter
- Refactor function name.

Signed-off-by: objec <objecttothis@gmail.com>

* Add unit tests for LocaleHelper

Signed-off-by: objec <objecttothis@gmail.com>

* Remove files from being tracked.

Signed-off-by: objec <objecttothis@gmail.com>

---------

Signed-off-by: objec <objecttothis@gmail.com>
2026-06-10 23:16:25 +04:00

60 lines
1.5 KiB
PHP

<?php
use CodeIgniter\Config\Factories;
use CodeIgniter\Test\CIUnitTestCase;
use Config\OSPOS;
class LocaleHelperTest extends CIUnitTestCase
{
protected function setUp(): void
{
parent::setUp();
require_once __DIR__ . '/../../app/Helpers/locale_helper.php';
$config = new OSPOS();
$config->settings = ['dateformat' => 'Y-m-d'];
Factories::injectMock('config', OSPOS::class, $config);
}
public function testValidDateReturnsTrue(): void
{
$this->assertTrue(isValidDate('2024-06-10'));
}
public function testInvalidDateFormatReturnsFalse(): void
{
$this->assertFalse(isValidDate('10/06/2024'));
}
public function testImpossibleDateReturnsFalse(): void
{
$this->assertFalse(isValidDate('2024-13-01'));
}
public function testPhpDateOverflowReturnsFalse(): void
{
// PHP silently overflows Feb 30 → Mar 1; the format()===candidate check catches this
$this->assertFalse(isValidDate('2024-02-30'));
}
public function testEmptyStringReturnsFalse(): void
{
$this->assertFalse(isValidDate(''));
}
public function testLeapDayValidReturnsTrue(): void
{
$this->assertTrue(isValidDate('2024-02-29'));
}
public function testLeapDayInvalidYearReturnsFalse(): void
{
$this->assertFalse(isValidDate('2023-02-29'));
}
public function testPartialDateReturnsFalse(): void
{
$this->assertFalse(isValidDate('2024-06'));
}
}