2013-12-16 03:20:34 +00:00
|
|
|
<?php
|
2015-08-30 17:02:55 +12:00
|
|
|
|
2016-10-14 14:30:05 +13:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
2016-06-15 16:03:16 +12:00
|
|
|
|
2016-10-14 14:30:05 +13:00
|
|
|
use SilverStripe\Forms\DropdownField;
|
2016-06-15 16:03:16 +12:00
|
|
|
use SilverStripe\ORM\FieldType\DBYear;
|
2016-08-19 10:51:35 +12:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2024-10-17 17:41:36 +13:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2016-08-19 10:51:35 +12:00
|
|
|
|
2016-12-16 17:34:21 +13:00
|
|
|
class DBYearTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the scaffolding form field works
|
|
|
|
*/
|
|
|
|
public function testScaffoldFormFieldFirst()
|
|
|
|
{
|
|
|
|
$year = new DBYear();
|
|
|
|
$field = $year->scaffoldFormField("YearTest");
|
|
|
|
$this->assertEquals(DropdownField::class, get_class($field));
|
|
|
|
|
2024-10-17 17:41:36 +13:00
|
|
|
//This should be a list of years from the current one, counting down to 1901
|
2016-12-16 17:34:21 +13:00
|
|
|
$source = $field->getSource();
|
|
|
|
|
|
|
|
$lastValue = end($source);
|
2022-04-14 13:12:59 +12:00
|
|
|
$lastKey = key($source ?? []);
|
2016-12-16 17:34:21 +13:00
|
|
|
|
2024-10-17 17:41:36 +13:00
|
|
|
//Keys and values should be the same - and the last one should be 1901
|
|
|
|
$this->assertEquals(1901, $lastValue);
|
|
|
|
$this->assertEquals(1901, $lastKey);
|
2016-12-16 17:34:21 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testScaffoldFormFieldLast()
|
|
|
|
{
|
|
|
|
$year = new DBYear();
|
|
|
|
$field = $year->scaffoldFormField("YearTest");
|
|
|
|
$source = $field->getSource();
|
|
|
|
|
|
|
|
//The first one should be the current year
|
|
|
|
$currentYear = (int)date('Y');
|
|
|
|
$firstValue = reset($source);
|
2022-04-14 13:12:59 +12:00
|
|
|
$firstKey = key($source ?? []);
|
2016-12-16 17:34:21 +13:00
|
|
|
|
|
|
|
$this->assertEquals($currentYear, $firstValue);
|
|
|
|
$this->assertEquals($currentYear, $firstKey);
|
|
|
|
}
|
2024-10-17 17:41:36 +13:00
|
|
|
|
|
|
|
public static function provideSetValue(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'4-int' => [
|
|
|
|
'value' => 2024,
|
|
|
|
'expected' => 2024,
|
|
|
|
],
|
|
|
|
'2-int' => [
|
|
|
|
'value' => 24,
|
|
|
|
'expected' => 2024,
|
|
|
|
],
|
|
|
|
'0-int' => [
|
|
|
|
'value' => 0,
|
|
|
|
'expected' => 0,
|
|
|
|
],
|
|
|
|
'4-string' => [
|
|
|
|
'value' => '2024',
|
|
|
|
'expected' => 2024,
|
|
|
|
],
|
|
|
|
'2-string' => [
|
|
|
|
'value' => '24',
|
|
|
|
'expected' => 2024,
|
|
|
|
],
|
|
|
|
'0-string' => [
|
|
|
|
'value' => '0',
|
|
|
|
'expected' => 0,
|
|
|
|
],
|
|
|
|
'00-string' => [
|
|
|
|
'value' => '00',
|
|
|
|
'expected' => 2000,
|
|
|
|
],
|
|
|
|
'0000-string' => [
|
|
|
|
'value' => '0000',
|
|
|
|
'expected' => 0,
|
|
|
|
],
|
|
|
|
'4-int-low' => [
|
|
|
|
'value' => 1900,
|
|
|
|
'expected' => 1900,
|
|
|
|
],
|
|
|
|
'4-int-low' => [
|
|
|
|
'value' => 2156,
|
|
|
|
'expected' => 2156,
|
|
|
|
],
|
|
|
|
'4-string-low' => [
|
|
|
|
'value' => '1900',
|
|
|
|
'expected' => 1900,
|
|
|
|
],
|
|
|
|
'4-string-low' => [
|
|
|
|
'value' => '2156',
|
|
|
|
'expected' => 2156,
|
|
|
|
],
|
|
|
|
'int-negative' => [
|
|
|
|
'value' => -2024,
|
|
|
|
'expected' => -2024,
|
|
|
|
],
|
|
|
|
'string-negative' => [
|
|
|
|
'value' => '-2024',
|
|
|
|
'expected' => '-2024',
|
|
|
|
],
|
|
|
|
'float' => [
|
|
|
|
'value' => 2024.0,
|
|
|
|
'expected' => 2024.0,
|
|
|
|
],
|
|
|
|
'string-float' => [
|
|
|
|
'value' => '2024.0',
|
|
|
|
'expected' => '2024.0',
|
|
|
|
],
|
|
|
|
'null' => [
|
|
|
|
'value' => null,
|
|
|
|
'expected' => null,
|
|
|
|
],
|
|
|
|
'true' => [
|
|
|
|
'value' => true,
|
|
|
|
'expected' => true,
|
|
|
|
],
|
|
|
|
'false' => [
|
|
|
|
'value' => false,
|
|
|
|
'expected' => false,
|
|
|
|
],
|
|
|
|
'array' => [
|
|
|
|
'value' => [],
|
|
|
|
'expected' => [],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[DataProvider('provideSetValue')]
|
|
|
|
public function testSetValue(mixed $value, mixed $expected): void
|
|
|
|
{
|
|
|
|
$field = new DBYear('MyField');
|
|
|
|
$result = $field->setValue($value);
|
|
|
|
$this->assertSame($expected, $field->getValue());
|
|
|
|
}
|
2013-12-16 03:20:34 +00:00
|
|
|
}
|