You are here

public function DateTimeItemTest::testDateOnly in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php \Drupal\Tests\datetime\Kernel\DateTimeItemTest::testDateOnly()
  2. 10 core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php \Drupal\Tests\datetime\Kernel\DateTimeItemTest::testDateOnly()

Tests using entity fields of the date field type.

File

core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php, line 111

Class

DateTimeItemTest
Tests the new entity API for the date field type.

Namespace

Drupal\Tests\datetime\Kernel

Code

public function testDateOnly() {
  $this->fieldStorage
    ->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
  $this->fieldStorage
    ->save();

  // Verify entity creation.
  $entity = EntityTest::create();
  $value = '2014-01-01';
  $entity->field_datetime = $value;
  $entity->name->value = $this
    ->randomMachineName();
  $this
    ->entityValidateAndSave($entity);

  // Verify entity has been created properly.
  $id = $entity
    ->id();
  $entity = EntityTest::load($id);
  $this
    ->assertInstanceOf(FieldItemListInterface::class, $entity->field_datetime);
  $this
    ->assertInstanceOf(FieldItemInterface::class, $entity->field_datetime[0]);
  $this
    ->assertEqual($entity->field_datetime->value, $value);
  $this
    ->assertEqual($entity->field_datetime[0]->value, $value);
  $this
    ->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date
    ->getTimeZone()
    ->getName());
  $this
    ->assertEquals('12:00:00', $entity->field_datetime->date
    ->format('H:i:s'));
  $entity->field_datetime->date
    ->setDefaultDateTime();
  $this
    ->assertEquals('12:00:00', $entity->field_datetime->date
    ->format('H:i:s'));

  // Verify changing the date value.
  $new_value = '2016-11-04';
  $entity->field_datetime->value = $new_value;
  $this
    ->assertEqual($entity->field_datetime->value, $new_value);
  $this
    ->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date
    ->getTimeZone()
    ->getName());
  $this
    ->assertEquals('12:00:00', $entity->field_datetime->date
    ->format('H:i:s'));
  $entity->field_datetime->date
    ->setDefaultDateTime();
  $this
    ->assertEquals('12:00:00', $entity->field_datetime->date
    ->format('H:i:s'));

  // Read changed entity and assert changed values.
  $this
    ->entityValidateAndSave($entity);
  $entity = EntityTest::load($id);
  $this
    ->assertEqual($entity->field_datetime->value, $new_value);
  $this
    ->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date
    ->getTimeZone()
    ->getName());
  $this
    ->assertEquals('12:00:00', $entity->field_datetime->date
    ->format('H:i:s'));
  $entity->field_datetime->date
    ->setDefaultDateTime();
  $this
    ->assertEquals('12:00:00', $entity->field_datetime->date
    ->format('H:i:s'));

  // Test the generateSampleValue() method.
  $entity = EntityTest::create();
  $entity->field_datetime
    ->generateSampleItems();
  $this
    ->assertEquals('12:00:00', $entity->field_datetime->date
    ->format('H:i:s'));
  $entity->field_datetime->date
    ->setDefaultDateTime();
  $this
    ->assertEquals('12:00:00', $entity->field_datetime->date
    ->format('H:i:s'));
  $this
    ->entityValidateAndSave($entity);
}