View source
<?php
namespace Drupal\Tests\Core\Datetime;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Tests\UnitTestCase;
class DrupalDateTimeTest extends UnitTestCase {
public function testDateDiff($input1, $input2, $absolute, \DateInterval $expected) {
$interval = $input1
->diff($input2, $absolute);
$this
->assertEquals($interval, $expected);
}
public function testInvalidDateDiff($input1, $input2, $absolute) {
$this
->expectException(\BadMethodCallException::class);
$this
->expectExceptionMessage('Method Drupal\\Component\\Datetime\\DateTimePlus::diff expects parameter 1 to be a \\DateTime or \\Drupal\\Component\\Datetime\\DateTimePlus object');
$interval = $input1
->diff($input2, $absolute);
}
public function providerTestDateDiff() {
$settings = [
'langcode' => 'en',
];
$utc_tz = new \DateTimeZone('UTC');
$empty_interval = new \DateInterval('PT0S');
$positive_19_hours = new \DateInterval('PT19H');
$positive_18_hours = new \DateInterval('PT18H');
$positive_1_hour = new \DateInterval('PT1H');
$negative_1_hour = new \DateInterval('PT1H');
$negative_1_hour->invert = 1;
return [
[
'input2' => DrupalDateTime::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00', new \DateTimeZone('Australia/Sydney'), $settings),
'input1' => DrupalDateTime::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00', new \DateTimeZone('America/Los_Angeles'), $settings),
'absolute' => FALSE,
'expected' => $positive_19_hours,
],
[
'input2' => DrupalDateTime::createFromFormat('Y-m-d H:i:s', '1970-01-01 00:00:00', new \DateTimeZone('Australia/Sydney'), $settings),
'input1' => DrupalDateTime::createFromFormat('Y-m-d H:i:s', '1970-01-01 00:00:00', new \DateTimeZone('America/Los_Angeles'), $settings),
'absolute' => FALSE,
'expected' => $positive_18_hours,
],
[
'input1' => DrupalDateTime::createFromFormat('U', 3600, new \DateTimeZone('America/Los_Angeles'), $settings),
'input2' => DrupalDateTime::createFromFormat('U', 0, $utc_tz, $settings),
'absolute' => FALSE,
'expected' => $negative_1_hour,
],
[
'input1' => DrupalDateTime::createFromFormat('U', 3600, $utc_tz, $settings),
'input2' => DrupalDateTime::createFromFormat('U', 0, $utc_tz, $settings),
'absolute' => FALSE,
'expected' => $negative_1_hour,
],
[
'input1' => DrupalDateTime::createFromFormat('U', 3600, $utc_tz, $settings),
'input2' => \DateTime::createFromFormat('U', 0),
'absolute' => FALSE,
'expected' => $negative_1_hour,
],
[
'input1' => DrupalDateTime::createFromFormat('U', 3600, $utc_tz, $settings),
'input2' => DrupalDateTime::createFromFormat('U', 0, $utc_tz, $settings),
'absolute' => TRUE,
'expected' => $positive_1_hour,
],
[
'input1' => DrupalDateTime::createFromFormat('U', 3600, $utc_tz, $settings),
'input2' => \DateTime::createFromFormat('U', 0),
'absolute' => TRUE,
'expected' => $positive_1_hour,
],
[
'input1' => DrupalDateTime::createFromFormat('U', 0, $utc_tz, $settings),
'input2' => DrupalDateTime::createFromFormat('U', 0, $utc_tz, $settings),
'absolute' => FALSE,
'expected' => $empty_interval,
],
];
}
public function providerTestInvalidDateDiff() {
$settings = [
'langcode' => 'en',
];
$utc_tz = new \DateTimeZone('UTC');
return [
[
'input1' => DrupalDateTime::createFromFormat('U', 3600, $utc_tz, $settings),
'input2' => '1970-01-01 00:00:00',
'absolute' => FALSE,
],
[
'input1' => DrupalDateTime::createFromFormat('U', 3600, $utc_tz, $settings),
'input2' => NULL,
'absolute' => FALSE,
],
];
}
public function testDefaultDateTime() {
$utc = new \DateTimeZone('UTC');
$date = DrupalDateTime::createFromFormat('Y-m-d H:i:s', '2017-05-23 22:58:00', $utc, [
'langcode' => 'en',
]);
$this
->assertEquals('22:58:00', $date
->format('H:i:s'));
$date
->setDefaultDateTime();
$this
->assertEquals('12:00:00', $date
->format('H:i:s'));
}
public function testChainable() {
$tz = new \DateTimeZone(date_default_timezone_get());
$date = new DrupalDateTime('now', $tz, [
'langcode' => 'en',
]);
$date
->setTimestamp(12345678);
$rendered = $date
->render();
$this
->assertEquals('1970-05-24 07:21:18 Australia/Sydney', $rendered);
$date
->setTimestamp(23456789);
$rendered = $date
->setTimezone(new \DateTimeZone('America/New_York'))
->render();
$this
->assertEquals('1970-09-29 07:46:29 America/New_York', $rendered);
}
public function testChainableNonChainable() {
$tz = new \DateTimeZone(date_default_timezone_get());
$datetime1 = new DrupalDateTime('2009-10-11 12:00:00', $tz, [
'langcode' => 'en',
]);
$datetime2 = new DrupalDateTime('2009-10-13 12:00:00', $tz, [
'langcode' => 'en',
]);
$interval = $datetime1
->diff($datetime2);
$this
->assertInstanceOf(\DateInterval::class, $interval);
$this
->assertEquals('+2 days', $interval
->format('%R%a days'));
}
public function testChainableNonCallable() {
$this
->expectException(\BadMethodCallException::class);
$this
->expectExceptionMessage('Call to undefined method Drupal\\Core\\Datetime\\DrupalDateTime::nonexistent()');
$tz = new \DateTimeZone(date_default_timezone_get());
$date = new DrupalDateTime('now', $tz, [
'langcode' => 'en',
]);
$date
->setTimezone(new \DateTimeZone('America/New_York'))
->nonexistent();
}
public function testGetPhpDateTime() {
$new_york = new \DateTimeZone('America/New_York');
$berlin = new \DateTimeZone('Europe/Berlin');
$drupaldatetime = DrupalDateTime::createFromFormat('Y-m-d H:i:s', '2017-07-13 22:40:00', $new_york, [
'langcode' => 'en',
]);
$this
->assertEquals(1500000000, $drupaldatetime
->getTimestamp());
$this
->assertEquals('America/New_York', $drupaldatetime
->getTimezone()
->getName());
$datetime = $drupaldatetime
->getPhpDateTime();
$this
->assertInstanceOf('DateTime', $datetime);
$this
->assertEquals(1500000000, $datetime
->getTimestamp());
$this
->assertEquals('America/New_York', $datetime
->getTimezone()
->getName());
$datetime
->setTimestamp(1400000000)
->setTimezone($berlin);
$this
->assertEquals(1400000000, $datetime
->getTimestamp());
$this
->assertEquals('Europe/Berlin', $datetime
->getTimezone()
->getName());
$this
->assertEquals(1500000000, $drupaldatetime
->getTimestamp());
$this
->assertEquals('America/New_York', $drupaldatetime
->getTimezone()
->getName());
}
}