2013-08-07 17:14:58 +02:00
|
|
|
<?php
|
2014-04-10 10:32:50 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-08-07 17:14:58 +02:00
|
|
|
|
2014-04-09 14:18:14 +02:00
|
|
|
namespace Tests\Icinga\Web\Form\Element;
|
2013-08-07 17:14:58 +02:00
|
|
|
|
2014-04-09 14:18:14 +02:00
|
|
|
use Icinga\Test\BaseTestCase;
|
|
|
|
use Icinga\Web\Form\Element\DateTimePicker;
|
2013-08-07 17:14:58 +02:00
|
|
|
|
2014-04-11 15:31:29 +02:00
|
|
|
class DateTimePickerTest extends BaseTestCase
|
2013-08-07 17:14:58 +02:00
|
|
|
{
|
2013-08-12 13:56:14 +02:00
|
|
|
/**
|
|
|
|
* Test that DateTimePicker::isValid() returns false if the input is not valid in terms of being a date/time string
|
|
|
|
* or a timestamp
|
|
|
|
*/
|
2013-08-07 17:14:58 +02:00
|
|
|
public function testValidateInvalidInput()
|
|
|
|
{
|
2013-08-27 16:00:45 +02:00
|
|
|
$dt = new DateTimePicker(
|
|
|
|
'foo',
|
|
|
|
array(
|
|
|
|
'patterns' => array(
|
|
|
|
'd/m/Y g:i A',
|
|
|
|
'd.m.Y H:i:s'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2013-08-07 17:14:58 +02:00
|
|
|
|
2013-08-27 16:00:45 +02:00
|
|
|
$this->assertFalse(
|
|
|
|
$dt->isValid('08/27/2013 12:40 PM'),
|
|
|
|
'Wrong placed month/day must not be valid input'
|
|
|
|
);
|
2013-08-07 17:14:58 +02:00
|
|
|
$this->assertFalse(
|
|
|
|
$dt->isValid('bar'),
|
|
|
|
'Arbitrary strings must not be valid input'
|
|
|
|
);
|
2013-08-27 16:00:45 +02:00
|
|
|
$this->assertFalse(
|
|
|
|
$dt->isValid('12:40 AM'),
|
|
|
|
'Time strings must not be valid input'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$dt->isValid('27/08/2013'),
|
|
|
|
'Date strings must not be valid input'
|
|
|
|
);
|
2013-08-07 17:14:58 +02:00
|
|
|
$this->assertFalse(
|
|
|
|
$dt->isValid('13736a16223'),
|
|
|
|
'Invalid Unix timestamps must not be valid input'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-08-12 13:56:14 +02:00
|
|
|
/**
|
|
|
|
* Test that DateTimePicker::isValid() returns true if the input is valid in terms of being a date/time string
|
|
|
|
* or a timestamp
|
|
|
|
*/
|
2013-08-07 17:14:58 +02:00
|
|
|
public function testValidateValidInput()
|
|
|
|
{
|
2013-08-27 16:00:45 +02:00
|
|
|
$dt = new DateTimePicker(
|
|
|
|
'foo',
|
|
|
|
array(
|
|
|
|
'patterns' => array(
|
|
|
|
'd/m/Y g:i A',
|
|
|
|
'd.m.Y H:i:s'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2013-08-07 17:14:58 +02:00
|
|
|
|
|
|
|
$this->assertTrue(
|
2013-08-27 16:00:45 +02:00
|
|
|
$dt->isValid('27/08/2013 12:40 PM'),
|
|
|
|
'Using a valid date/time string must not fail'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$dt->isValid('12.07.2013 08:03:43'),
|
2013-08-07 17:14:58 +02:00
|
|
|
'Using a valid date/time string must not fail'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$dt->isValid(1373616223),
|
|
|
|
'Using valid Unix timestamps must not fail'
|
|
|
|
);
|
2013-08-27 16:00:45 +02:00
|
|
|
$this->assertTrue(
|
|
|
|
$dt->isValid('1373616223'),
|
|
|
|
'Using strings as Unix timestamps must not fail'
|
|
|
|
);
|
2013-08-07 17:14:58 +02:00
|
|
|
}
|
|
|
|
|
2013-08-12 13:56:14 +02:00
|
|
|
/**
|
2013-08-27 16:00:45 +02:00
|
|
|
* Test that DateTimePicker::getValue() returns a timestamp after a successful call to isValid
|
2013-08-12 13:56:14 +02:00
|
|
|
*/
|
|
|
|
public function testGetValueReturnsUnixTimestampAfterSuccessfulIsValidCall()
|
2013-08-07 17:14:58 +02:00
|
|
|
{
|
2013-08-27 16:00:45 +02:00
|
|
|
$dt = new DateTimePicker(
|
|
|
|
'foo',
|
|
|
|
array(
|
|
|
|
'patterns' => array(
|
|
|
|
'd.m.Y H:i:s'
|
|
|
|
)
|
|
|
|
)
|
2013-08-12 13:56:14 +02:00
|
|
|
);
|
2013-08-27 16:00:45 +02:00
|
|
|
$dt->isValid('12.07.2013 08:03:43');
|
2013-08-07 17:14:58 +02:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
1373616223,
|
2013-08-27 16:00:45 +02:00
|
|
|
$dt->getValue(),
|
|
|
|
'getValue did not return the correct Unix timestamp according to the given date/time string'
|
2013-08-07 17:14:58 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|