You are here

public function DateTimeItemTest::testDateTimeItem in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/datetime/src/Tests/DateTimeItemTest.php \Drupal\datetime\Tests\DateTimeItemTest::testDateTimeItem()

Tests using entity fields of the date field type.

File

core/modules/datetime/src/Tests/DateTimeItemTest.php, line 52
Contains \Drupal\datetime\Tests\DateTimeItemTest.

Class

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

Namespace

Drupal\datetime\Tests

Code

public function testDateTimeItem() {

  // Verify entity creation.
  $entity = entity_create('entity_test');
  $value = '2014-01-01T20:00:00Z';
  $entity->field_datetime = $value;
  $entity->name->value = $this
    ->randomMachineName();
  $entity
    ->save();

  // Verify entity has been created properly.
  $id = $entity
    ->id();
  $entity = entity_load('entity_test', $id);
  $this
    ->assertTrue($entity->field_datetime instanceof FieldItemListInterface, 'Field implements interface.');
  $this
    ->assertTrue($entity->field_datetime[0] instanceof FieldItemInterface, 'Field item implements interface.');
  $this
    ->assertEqual($entity->field_datetime->value, $value);
  $this
    ->assertEqual($entity->field_datetime[0]->value, $value);

  // Verify changing the date value.
  $new_value = $this
    ->randomMachineName();
  $entity->field_datetime->value = $new_value;
  $this
    ->assertEqual($entity->field_datetime->value, $new_value);

  // Read changed entity and assert changed values.
  $entity
    ->save();
  $entity = entity_load('entity_test', $id);
  $this
    ->assertEqual($entity->field_datetime->value, $new_value);

  // Test the generateSampleValue() method.
  $entity = entity_create('entity_test');
  $entity->field_datetime
    ->generateSampleItems();
  $this
    ->entityValidateAndSave($entity);
}