You are here

protected function EntityTestDatetimeTest::setUp in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/datetime/tests/src/Functional/EntityResource/EntityTest/EntityTestDatetimeTest.php \Drupal\Tests\datetime\Functional\EntityResource\EntityTest\EntityTestDatetimeTest::setUp()
  2. 9 core/modules/datetime/tests/src/Functional/EntityResource/EntityTest/EntityTestDatetimeTest.php \Drupal\Tests\datetime\Functional\EntityResource\EntityTest\EntityTestDatetimeTest::setUp()

File

core/modules/datetime/tests/src/Functional/EntityResource/EntityTest/EntityTestDatetimeTest.php, line 50

Class

EntityTestDatetimeTest
Tests the datetime field constraint with 'datetime' items.

Namespace

Drupal\Tests\datetime\Functional\EntityResource\EntityTest

Code

protected function setUp() : void {
  parent::setUp();

  // Add datetime field.
  FieldStorageConfig::create([
    'field_name' => static::$fieldName,
    'type' => 'datetime',
    'entity_type' => static::$entityTypeId,
    'settings' => [
      'datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME,
    ],
  ])
    ->save();
  FieldConfig::create([
    'field_name' => static::$fieldName,
    'entity_type' => static::$entityTypeId,
    'bundle' => $this->entity
      ->bundle(),
    'settings' => [
      'default_value' => static::$dateString,
    ],
  ])
    ->save();

  // Reload entity so that it has the new field.
  $this->entity = $this->entityStorage
    ->load($this->entity
    ->id());
  $this->entity
    ->set(static::$fieldName, [
    'value' => static::$dateString,
  ]);
  $this->entity
    ->save();
}