function DateTimeItemTest::testSetValue

Same name in other branches
  1. 9 core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php \Drupal\Tests\datetime\Kernel\DateTimeItemTest::testSetValue()
  2. 8.9.x core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php \Drupal\Tests\datetime\Kernel\DateTimeItemTest::testSetValue()
  3. 11.x 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 168

Class

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

Namespace

Drupal\Tests\datetime\Kernel

Code

public function testSetValue() : void {
    // 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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.