You are here

protected function DateRangeItemTest::setUp in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/datetime_range/tests/src/Kernel/DateRangeItemTest.php \Drupal\Tests\datetime_range\Kernel\DateRangeItemTest::setUp()
  2. 9 core/modules/datetime_range/tests/src/Kernel/DateRangeItemTest.php \Drupal\Tests\datetime_range\Kernel\DateRangeItemTest::setUp()

File

core/modules/datetime_range/tests/src/Kernel/DateRangeItemTest.php, line 44

Class

DateRangeItemTest
Test datetime range field type via API.

Namespace

Drupal\Tests\datetime_range\Kernel

Code

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

  // Add a datetime range field.
  $this->fieldStorage = FieldStorageConfig::create([
    'field_name' => mb_strtolower($this
      ->randomMachineName()),
    'entity_type' => 'entity_test',
    'type' => 'daterange',
    'settings' => [
      'datetime_type' => DateRangeItem::DATETIME_TYPE_DATE,
    ],
  ]);
  $this->fieldStorage
    ->save();
  $this->field = FieldConfig::create([
    'field_storage' => $this->fieldStorage,
    'bundle' => 'entity_test',
    'required' => TRUE,
  ]);
  $this->field
    ->save();
  $display_options = [
    'type' => 'daterange_default',
    'label' => 'hidden',
    'settings' => [
      'format_type' => 'fallback',
      'separator' => 'UNTRANSLATED',
    ],
  ];
  EntityViewDisplay::create([
    'targetEntityType' => $this->field
      ->getTargetEntityTypeId(),
    'bundle' => $this->field
      ->getTargetBundle(),
    'mode' => 'default',
    'status' => TRUE,
  ])
    ->setComponent($this->fieldStorage
    ->getName(), $display_options)
    ->save();
}