Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Datetime / DateTimePlusTest.php
index f120007d48209945a95c26c7790883b000052e01..b755be8325ae7c77fb5457c9fc4318339dfa69c3 100644 (file)
@@ -2,14 +2,14 @@
 
 namespace Drupal\Tests\Component\Datetime;
 
-use Drupal\Tests\UnitTestCase;
 use Drupal\Component\Datetime\DateTimePlus;
+use PHPUnit\Framework\TestCase;
 
 /**
  * @coversDefaultClass \Drupal\Component\Datetime\DateTimePlus
  * @group Datetime
  */
-class DateTimePlusTest extends UnitTestCase {
+class DateTimePlusTest extends TestCase {
 
   /**
    * Test creating dates from string and array input.
@@ -167,7 +167,7 @@ class DateTimePlusTest extends UnitTestCase {
    * Assertion helper for testTimestamp and testDateTimestamp since they need
    * different dataProviders.
    *
-   * @param DateTimePlus $date
+   * @param \Drupal\Component\Datetime\DateTimePlus $date
    *   DateTimePlus to test.
    * @input mixed $input
    *   The original input passed to the test method.
@@ -293,7 +293,7 @@ class DateTimePlusTest extends UnitTestCase {
    * @see DateTimePlusTest::testDates()
    */
   public function providerTestDates() {
-    return [
+    $dates = [
       // String input.
       // Create date object from datetime string.
       ['2009-03-07 10:30', 'America/Chicago', '2009-03-07T10:30:00-06:00'],
@@ -308,6 +308,19 @@ class DateTimePlusTest extends UnitTestCase {
       // Same during daylight savings time.
       ['2009-06-07 10:30', 'Australia/Canberra', '2009-06-07T10:30:00+10:00'],
     ];
+
+    // On 32-bit systems, timestamps are limited to 1901-2038.
+    if (PHP_INT_SIZE > 4) {
+      // Create a date object in the distant past.
+      // @see https://www.drupal.org/node/2795489#comment-12127088
+      if (version_compare(PHP_VERSION, '5.6.15', '>=')) {
+        $dates[] = ['1809-02-12 10:30', 'America/Chicago', '1809-02-12T10:30:00-06:00'];
+      }
+      // Create a date object in the far future.
+      $dates[] = ['2345-01-02 02:04', 'UTC', '2345-01-02T02:04:00+00:00'];
+    }
+
+    return $dates;
   }
 
   /**
@@ -320,7 +333,7 @@ class DateTimePlusTest extends UnitTestCase {
    * @see DateTimePlusTest::testDates()
    */
   public function providerTestDateArrays() {
-    return [
+    $dates = [
       // Array input.
       // Create date object from date array, date only.
       [['year' => 2010, 'month' => 2, 'day' => 28], 'America/Chicago', '2010-02-28T00:00:00-06:00'],
@@ -331,6 +344,19 @@ class DateTimePlusTest extends UnitTestCase {
       // Create date object from date array with hour.
       [['year' => 2010, 'month' => 2, 'day' => 28, 'hour' => 10], 'Europe/Berlin', '2010-02-28T10:00:00+01:00'],
     ];
+
+    // On 32-bit systems, timestamps are limited to 1901-2038.
+    if (PHP_INT_SIZE > 4) {
+      // Create a date object in the distant past.
+      // @see https://www.drupal.org/node/2795489#comment-12127088
+      if (version_compare(PHP_VERSION, '5.6.15', '>=')) {
+        $dates[] = [['year' => 1809, 'month' => 2, 'day' => 12], 'America/Chicago', '1809-02-12T00:00:00-06:00'];
+      }
+      // Create a date object in the far future.
+      $dates[] = [['year' => 2345, 'month' => 1, 'day' => 2], 'UTC', '2345-01-02T00:00:00+00:00'];
+    }
+
+    return $dates;
   }
 
   /**
@@ -778,4 +804,52 @@ class DateTimePlusTest extends UnitTestCase {
     $date = DateTimePlus::createFromFormat('Y-m-d H:i:s', '11-03-31 17:44:00', 'UTC', ['validate_format' => TRUE]);
   }
 
+  /**
+   * Tests that object methods are chainable.
+   *
+   * @covers ::__call
+   */
+  public function testChainable() {
+    $date = new DateTimePlus('now', 'Australia/Sydney');
+
+    $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);
+
+    $date = DateTimePlus::createFromFormat('Y-m-d H:i:s', '1970-05-24 07:21:18', new \DateTimeZone('Australia/Sydney'))
+      ->setTimezone(new \DateTimeZone('America/New_York'));
+    $rendered = $date->render();
+    $this->assertInstanceOf(DateTimePlus::class, $date);
+    $this->assertEquals(12345678, $date->getTimestamp());
+    $this->assertEquals('1970-05-23 17:21:18 America/New_York', $rendered);
+  }
+
+  /**
+   * Tests that non-chainable methods work.
+   *
+   * @covers ::__call
+   */
+  public function testChainableNonChainable() {
+    $datetime1 = new DateTimePlus('2009-10-11 12:00:00');
+    $datetime2 = new DateTimePlus('2009-10-13 12:00:00');
+    $interval = $datetime1->diff($datetime2);
+    $this->assertInstanceOf(\DateInterval::class, $interval);
+    $this->assertEquals('+2 days', $interval->format('%R%a days'));
+  }
+
+  /**
+   * Tests that chained calls to non-existent functions throw an exception.
+   *
+   * @covers ::__call
+   */
+  public function testChainableNonCallable() {
+    $this->setExpectedException(\BadMethodCallException::class, 'Call to undefined method Drupal\Component\Datetime\DateTimePlus::nonexistent()');
+    $date = new DateTimePlus('now', 'Australia/Sydney');
+    $date->setTimezone(new \DateTimeZone('America/New_York'))->nonexistent();
+  }
+
 }