You are here

public function DateTimeItemTest::testSetValue 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::testSetValue()
  2. 10 core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php \Drupal\Tests\datetime\Kernel\DateTimeItemTest::testSetValue()

Tests DateTimeItem::setValue().

File

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

Class

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

Namespace

Drupal\Tests\datetime\Kernel

Code

public function testSetValue() {

  // Test a date+time field.
  $this->fieldStorage
    ->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
  $this->fieldStorage
    ->save();

  // Test DateTimeItem::setValue() using string.
  $entity = EntityTest::create();
  $value = '2014-01-01T20:00:00';
  $entity
    ->get('field_datetime')
    ->set(0, $value);
  $this
    ->entityValidateAndSave($entity);

  // Load the entity and ensure the field was saved correctly.
  $id = $entity
    ->id();
  $entity = EntityTest::load($id);
  $this
    ->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with string value.');
  $this
    ->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date
    ->getTimeZone()
    ->getName());

  // Test DateTimeItem::setValue() using property array.
  $entity = EntityTest::create();
  $value = '2014-01-01T20:00:00';
  $entity
    ->set('field_datetime', $value);
  $this
    ->entityValidateAndSave($entity);

  // Load the entity and ensure the field was saved correctly.
  $id = $entity
    ->id();
  $entity = EntityTest::load($id);
  $this
    ->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with array value.');
  $this
    ->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date
    ->getTimeZone()
    ->getName());

  // Test a date-only field.
  $this->fieldStorage
    ->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
  $this->fieldStorage
    ->save();

  // Test DateTimeItem::setValue() using string.
  $entity = EntityTest::create();
  $value = '2014-01-01';
  $entity
    ->get('field_datetime')
    ->set(0, $value);
  $this
    ->entityValidateAndSave($entity);

  // Load the entity and ensure the field was saved correctly.
  $id = $entity
    ->id();
  $entity = EntityTest::load($id);
  $this
    ->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with string value.');
  $this
    ->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date
    ->getTimeZone()
    ->getName());

  // Test DateTimeItem::setValue() using property array.
  $entity = EntityTest::create();
  $value = '2014-01-01';
  $entity
    ->set('field_datetime', $value);
  $this
    ->entityValidateAndSave($entity);

  // Load the entity and ensure the field was saved correctly.
  $id = $entity
    ->id();
  $entity = EntityTest::load($id);
  $this
    ->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with array value.');
  $this
    ->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date
    ->getTimeZone()
    ->getName());
}