summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2016-05-08 10:03:48 -0500
committerAlex Pott2016-05-08 10:03:48 -0500
commit95ed8ce29d955c8b44440ce10236d26e79ec2077 (patch)
tree74a1895ffd76de5a6fb5c3e56c4da4eaddbcd162
parent6347dec521bc807f9a2a189b59d5302002efe9d5 (diff)
Issue #2704457 by thtas, mpdonadio: Fix DrupalDateTime::diff()
-rw-r--r--core/lib/Drupal/Component/Datetime/DateTimePlus.php24
-rw-r--r--core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php138
-rw-r--r--core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php159
3 files changed, 321 insertions, 0 deletions
diff --git a/core/lib/Drupal/Component/Datetime/DateTimePlus.php b/core/lib/Drupal/Component/Datetime/DateTimePlus.php
index 1d8bbb7..1d71ae6 100644
--- a/core/lib/Drupal/Component/Datetime/DateTimePlus.php
+++ b/core/lib/Drupal/Component/Datetime/DateTimePlus.php
@@ -295,6 +295,30 @@ class DateTimePlus {
}
/**
+ * Returns the difference between two DateTimePlus objects.
+ *
+ * @param \Drupal\Component\Datetime\DateTimePlus|\DateTime $datetime2
+ * The date to compare to.
+ * @param bool $absolute
+ * Should the interval be forced to be positive?
+ *
+ * @return \DateInterval
+ * A DateInterval object representing the difference between the two dates.
+ *
+ * @throws \BadMethodCallException
+ * If the input isn't a DateTime or DateTimePlus object.
+ */
+ public function diff($datetime2, $absolute = FALSE) {
+ if ($datetime2 instanceof DateTimePlus) {
+ $datetime2 = $datetime2->dateTimeObject;
+ }
+ if (!($datetime2 instanceof \DateTime)) {
+ throw new \BadMethodCallException(sprintf('Method %s expects parameter 1 to be a \DateTime or \Drupal\Component\Datetime\DateTimePlus object', __METHOD__));
+ }
+ return $this->dateTimeObject->diff($datetime2, $absolute);
+ }
+
+ /**
* Implements the magic __callStatic method.
*
* Passes through all unknown static calls onto the DateTime object.
diff --git a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php
index 7f5e200..d4cd751 100644
--- a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php
+++ b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php
@@ -56,6 +56,42 @@ class DateTimePlusTest extends UnitTestCase {
}
/**
+ * Test date diffs.
+ *
+ * @param mixed $input1
+ * A DateTimePlus object.
+ * @param mixed $input2
+ * Date argument for DateTimePlus::diff method.
+ * @param bool $absolute
+ * Absolute flag for DateTimePlus::diff method.
+ * @param \DateInterval $expected
+ * The expected result of the DateTimePlus::diff operation.
+ *
+ * @dataProvider providerTestDateDiff
+ */
+ public function testDateDiff($input1, $input2, $absolute, \DateInterval $expected) {
+ $interval = $input1->diff($input2, $absolute);
+ $this->assertEquals($interval, $expected);
+ }
+
+ /**
+ * Test date diff exception caused by invalid input.
+ *
+ * @param mixed $input1
+ * A DateTimePlus object.
+ * @param mixed $input2
+ * Date argument for DateTimePlus::diff method.
+ * @param bool $absolute
+ * Absolute flag for DateTimePlus::diff method.
+ *
+ * @dataProvider providerTestInvalidDateDiff
+ */
+ public function testInvalidDateDiff($input1, $input2, $absolute) {
+ $this->setExpectedException(\BadMethodCallException::class, 'Method Drupal\Component\Datetime\DateTimePlus::diff expects parameter 1 to be a \DateTime or \Drupal\Component\Datetime\DateTimePlus object');
+ $interval = $input1->diff($input2, $absolute);
+ }
+
+ /**
* Test creating dates from invalid array input.
*
* @param mixed $input
@@ -528,4 +564,106 @@ class DateTimePlusTest extends UnitTestCase {
);
}
+ /**
+ * Provides data for date tests.
+ *
+ * @return array
+ * An array of arrays, each containing the input parameters for
+ * DateTimePlusTest::testDateDiff().
+ *
+ * @see DateTimePlusTest::testDateDiff()
+ */
+ public function providerTestDateDiff() {
+
+ $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 array(
+ // There should be a 19 hour time interval between
+ // new years in Sydney and new years in LA in year 2000.
+ array(
+ 'input2' => DateTimePlus::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00', new \DateTimeZone('Australia/Sydney')),
+ 'input1' => DateTimePlus::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00', new \DateTimeZone('America/Los_Angeles')),
+ 'absolute' => FALSE,
+ 'expected' => $positive_19_hours,
+ ),
+ // In 1970 Sydney did not observe daylight savings time
+ // So there is only a 18 hour time interval.
+ array(
+ 'input2' => DateTimePlus::createFromFormat('Y-m-d H:i:s', '1970-01-01 00:00:00', new \DateTimeZone('Australia/Sydney')),
+ 'input1' => DateTimePlus::createFromFormat('Y-m-d H:i:s', '1970-01-01 00:00:00', new \DateTimeZone('America/Los_Angeles')),
+ 'absolute' => FALSE,
+ 'expected' => $positive_18_hours,
+ ),
+ array(
+ 'input1' => DateTimePlus::createFromFormat('U', 3600, new \DateTimeZone('America/Los_Angeles')),
+ 'input2' => DateTimePlus::createFromFormat('U', 0, new \DateTimeZone('UTC')),
+ 'absolute' => FALSE,
+ 'expected' => $negative_1_hour,
+ ),
+ array(
+ 'input1' => DateTimePlus::createFromFormat('U', 3600),
+ 'input2' => DateTimePlus::createFromFormat('U', 0),
+ 'absolute' => FALSE,
+ 'expected' => $negative_1_hour,
+ ),
+ array(
+ 'input1' => DateTimePlus::createFromFormat('U', 3600),
+ 'input2' => \DateTime::createFromFormat('U', 0),
+ 'absolute' => FALSE,
+ 'expected' => $negative_1_hour,
+ ),
+ array(
+ 'input1' => DateTimePlus::createFromFormat('U', 3600),
+ 'input2' => DateTimePlus::createFromFormat('U', 0),
+ 'absolute' => TRUE,
+ 'expected' => $positive_1_hour,
+ ),
+ array(
+ 'input1' => DateTimePlus::createFromFormat('U', 3600),
+ 'input2' => \DateTime::createFromFormat('U', 0),
+ 'absolute' => TRUE,
+ 'expected' => $positive_1_hour,
+ ),
+ array(
+ 'input1' => DateTimePlus::createFromFormat('U', 0),
+ 'input2' => DateTimePlus::createFromFormat('U', 0),
+ 'absolute' => FALSE,
+ 'expected' => $empty_interval,
+ ),
+ );
+ }
+
+ /**
+ * Provides data for date tests.
+ *
+ * @return array
+ * An array of arrays, each containing the input parameters for
+ * DateTimePlusTest::testInvalidDateDiff().
+ *
+ * @see DateTimePlusTest::testInvalidDateDiff()
+ */
+ public function providerTestInvalidDateDiff() {
+ return array(
+ array(
+ 'input1' => DateTimePlus::createFromFormat('U', 3600),
+ 'input2' => '1970-01-01 00:00:00',
+ 'absolute' => FALSE,
+ ),
+ array(
+ 'input1' => DateTimePlus::createFromFormat('U', 3600),
+ 'input2' => NULL,
+ 'absolute' => FALSE,
+ ),
+ );
+ }
+
}
diff --git a/core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php b/core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php
new file mode 100644
index 0000000..e77542b
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php
@@ -0,0 +1,159 @@
+<?php
+
+namespace Drupal\Tests\Core\Datetime;
+
+use Drupal\Core\Datetime\DrupalDateTime;
+use Drupal\Tests\UnitTestCase;
+
+/**
+ * @coversDefaultClass \Drupal\Core\Datetime\DrupalDateTime
+ * @group Datetime
+ */
+class DrupalDateTimeTest extends UnitTestCase {
+
+ /**
+ * Test date diffs.
+ *
+ * @param mixed $input1
+ * A DrupalDateTime object.
+ * @param mixed $input2
+ * Date argument for DrupalDateTime::diff method.
+ * @param bool $absolute
+ * Absolute flag for DrupalDateTime::diff method.
+ * @param \DateInterval $expected
+ * The expected result of the DrupalDateTime::diff operation.
+ *
+ * @dataProvider providerTestDateDiff
+ */
+ public function testDateDiff($input1, $input2, $absolute, \DateInterval $expected) {
+ $interval = $input1->diff($input2, $absolute);
+ $this->assertEquals($interval, $expected);
+ }
+
+ /**
+ * Test date diff exception caused by invalid input.
+ *
+ * @param mixed $input1
+ * A DateTimePlus object.
+ * @param mixed $input2
+ * Date argument for DateTimePlus::diff method.
+ * @param bool $absolute
+ * Absolute flag for DateTimePlus::diff method.
+ *
+ * @dataProvider providerTestInvalidDateDiff
+ */
+ public function testInvalidDateDiff($input1, $input2, $absolute) {
+ $this->setExpectedException(\BadMethodCallException::class, 'Method Drupal\Component\Datetime\DateTimePlus::diff expects parameter 1 to be a \DateTime or \Drupal\Component\Datetime\DateTimePlus object');
+ $interval = $input1->diff($input2, $absolute);
+ }
+
+ /**
+ * Provides data for date tests.
+ *
+ * @return array
+ * An array of arrays, each containing the input parameters for
+ * DrupalDateTimeTest::testDateDiff().
+ *
+ * @see DrupalDateTimeTest::testDateDiff()
+ */
+ 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 array(
+
+ // There should be a 19 hour time interval between
+ // new years in Sydney and new years in LA in year 2000.
+ array(
+ '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,
+ ),
+ // In 1970 Sydney did not observe daylight savings time
+ // So there is only a 18 hour time interval.
+ array(
+ '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,
+ ),
+ array(
+ '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,
+ ),
+ array(
+ 'input1' => DrupalDateTime::createFromFormat('U', 3600, $utc_tz, $settings),
+ 'input2' => DrupalDateTime::createFromFormat('U', 0, $utc_tz, $settings),
+ 'absolute' => FALSE,
+ 'expected' => $negative_1_hour,
+ ),
+ array(
+ 'input1' => DrupalDateTime::createFromFormat('U', 3600, $utc_tz, $settings),
+ 'input2' => \DateTime::createFromFormat('U', 0),
+ 'absolute' => FALSE,
+ 'expected' => $negative_1_hour,
+ ),
+ array(
+ 'input1' => DrupalDateTime::createFromFormat('U', 3600, $utc_tz, $settings),
+ 'input2' => DrupalDateTime::createFromFormat('U', 0, $utc_tz, $settings),
+ 'absolute' => TRUE,
+ 'expected' => $positive_1_hour,
+ ),
+ array(
+ 'input1' => DrupalDateTime::createFromFormat('U', 3600, $utc_tz, $settings),
+ 'input2' => \DateTime::createFromFormat('U', 0),
+ 'absolute' => TRUE,
+ 'expected' => $positive_1_hour,
+ ),
+ array(
+ 'input1' => DrupalDateTime::createFromFormat('U', 0, $utc_tz, $settings),
+ 'input2' => DrupalDateTime::createFromFormat('U', 0, $utc_tz, $settings),
+ 'absolute' => FALSE,
+ 'expected' => $empty_interval,
+ ),
+ );
+ }
+
+ /**
+ * Provides data for date tests.
+ *
+ * @return array
+ * An array of arrays, each containing the input parameters for
+ * DateTimePlusTest::testInvalidDateDiff().
+ *
+ * @see DateTimePlusTest::testInvalidDateDiff()
+ */
+ public function providerTestInvalidDateDiff() {
+ $settings = ['langcode' => 'en'];
+ $utc_tz = new \DateTimeZone('UTC');
+ return array(
+ array(
+ 'input1' => DrupalDateTime::createFromFormat('U', 3600, $utc_tz, $settings),
+ 'input2' => '1970-01-01 00:00:00',
+ 'absolute' => FALSE,
+ ),
+ array(
+ 'input1' => DrupalDateTime::createFromFormat('U', 3600, $utc_tz, $settings),
+ 'input2' => NULL,
+ 'absolute' => FALSE,
+ ),
+ );
+ }
+
+}