function DateTimeItemTest::testSetValueProperty

Same name in other branches
  1. 9 core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php \Drupal\Tests\datetime\Kernel\DateTimeItemTest::testSetValueProperty()
  2. 10 core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php \Drupal\Tests\datetime\Kernel\DateTimeItemTest::testSetValueProperty()
  3. 11.x core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php \Drupal\Tests\datetime\Kernel\DateTimeItemTest::testSetValueProperty()

Tests setting the value of the DateTimeItem directly.

File

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

Class

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

Namespace

Drupal\Tests\datetime\Kernel

Code

public function testSetValueProperty() {
    // Test Date::setValue() with a date+time field.
    // Test a date+time field.
    $this->fieldStorage
        ->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
    $this->fieldStorage
        ->save();
    $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, '"Value" property can be set directly.');
    $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date
        ->getTimeZone()
        ->getName());
    // Test Date::setValue() with a date-only field.
    // Test a date+time field.
    $this->fieldStorage
        ->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
    $this->fieldStorage
        ->save();
    $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, '"Value" property can be set directly.');
    $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.