function TextareaWithSummaryTest::testTextAreaWithSummaryValidation

Tests validation constraints for a field with delta-specific violations.

See also

\Drupal\text_test\Hook\TextTestHooks::entityBundleFieldInfoAlter()

File

core/modules/text/tests/src/Functional/TextareaWithSummaryTest.php, line 48

Class

TextareaWithSummaryTest
Tests the functionality of the text_textarea_with_summary widget.

Namespace

Drupal\Tests\text\Functional

Code

public function testTextAreaWithSummaryValidation() : void {
  // Create a field for validation testing.
  $entity_type_id = 'node';
  $field_name = $this->randomMachineName();
  $entity_type_manager = $this->container
    ->get('entity_type.manager');
  $field_storage = $entity_type_manager->getStorage('field_storage_config')
    ->create([
    'field_name' => $field_name,
    'entity_type' => $entity_type_id,
    'type' => 'text_with_summary',
    'cardinality' => 2,
  ]);
  $field_storage->save();
  $bundle_id = 'page';
  $field_label = $this->randomMachineName() . '_label';
  $entity_type_manager->getStorage('field_config')
    ->create([
    'field_storage' => $field_storage,
    'bundle' => $bundle_id,
    'label' => $field_label,
    'settings' => [
      'display_summary' => TRUE,
      'required_summary' => FALSE,
    ],
  ])
    ->save();
  // Add the created field to the form.
  $this->container
    ->get('entity_display.repository')
    ->getFormDisplay($entity_type_id, $bundle_id)
    ->setComponent($field_name, [
    'type' => 'text_textarea_with_summary',
  ])
    ->save();
  // Enable delta-specific validation for the field.
  $this->container
    ->get('state')
    ->set('field_test_constraint', $field_name);
  $this->container
    ->get('entity_field.manager')
    ->clearCachedFieldDefinitions();
  // Create a node to verify that validation works.
  $value = $this->randomMachineName();
  $this->drupalGet('node/add/page');
  $edit = [
    'title[0][value]' => $this->randomMachineName(),
    "{$field_name}[0][value]" => $value,
    "{$field_name}[1][value]" => $value,
  ];
  $this->submitForm($edit, 'Save');
  $this->assertSession()
    ->statusMessageContains("A content item with {$field_label} {$value} already exists.");
}

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