You are here

public function DateTimeItemTest::testSetValue in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php \Drupal\Tests\datetime\Kernel\DateTimeItemTest::testSetValue()
  2. 9 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
    ->assertEquals($value, $entity->field_datetime[0]->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
    ->assertEquals($value, $entity->field_datetime[0]->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
    ->assertEquals($value, $entity->field_datetime[0]->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
    ->assertEquals($value, $entity->field_datetime[0]->value, 'DateTimeItem::setValue() works with array value.');
  $this
    ->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date
    ->getTimeZone()
    ->getName());
}