You are here

public function DateTimeFieldTest::testDateStorageSettings in Drupal 10

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

Tests that 'Date' field storage setting form is disabled if field has data.

File

core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php, line 885

Class

DateTimeFieldTest
Tests Datetime field functionality.

Namespace

Drupal\Tests\datetime\Functional

Code

public function testDateStorageSettings() {

  // Create a test content type.
  $this
    ->drupalCreateContentType([
    'type' => 'date_content',
  ]);

  // Create a field storage with settings to validate.
  $field_name = mb_strtolower($this
    ->randomMachineName());
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'node',
    'type' => 'datetime',
    'settings' => [
      'datetime_type' => 'date',
    ],
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'field_name' => $field_name,
    'bundle' => 'date_content',
  ]);
  $field
    ->save();
  \Drupal::service('entity_display.repository')
    ->getFormDisplay('node', 'date_content')
    ->setComponent($field_name, [
    'type' => 'datetime_default',
  ])
    ->save();
  $edit = [
    'title[0][value]' => $this
      ->randomString(),
    'body[0][value]' => $this
      ->randomString(),
    $field_name . '[0][value][date]' => '2016-04-01',
  ];
  $this
    ->drupalGet('node/add/date_content');
  $this
    ->submitForm($edit, 'Save');
  $this
    ->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name . '/storage');
  $result = $this
    ->xpath("//*[@id='edit-settings-datetime-type' and contains(@disabled, 'disabled')]");
  $this
    ->assertCount(1, $result, "Changing datetime setting is disabled.");
  $this
    ->assertSession()
    ->pageTextContains('There is data for this field in the database. The field settings can no longer be changed.');
}