You are here

public function TextSummaryTest::testRequiredSummary in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/text/tests/src/Kernel/TextSummaryTest.php \Drupal\Tests\text\Kernel\TextSummaryTest::testRequiredSummary()

Tests required summary.

File

core/modules/text/tests/src/Kernel/TextSummaryTest.php, line 248

Class

TextSummaryTest
Tests text_summary() with different strings and lengths.

Namespace

Drupal\Tests\text\Kernel

Code

public function testRequiredSummary() {
  $this
    ->installEntitySchema('entity_test');
  $this
    ->setUpCurrentUser();
  $field_definition = FieldStorageConfig::create([
    'field_name' => 'test_textwithsummary',
    'type' => 'text_with_summary',
    'entity_type' => 'entity_test',
    'cardinality' => 1,
    'settings' => [
      'max_length' => 200,
    ],
  ]);
  $field_definition
    ->save();
  $instance = FieldConfig::create([
    'field_name' => 'test_textwithsummary',
    'label' => 'A text field',
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
    'settings' => [
      'text_processing' => TRUE,
      'display_summary' => TRUE,
      'required_summary' => TRUE,
    ],
  ]);
  $instance
    ->save();
  EntityFormDisplay::create([
    'targetEntityType' => 'entity_test',
    'bundle' => 'entity_test',
    'mode' => 'default',
    'status' => TRUE,
  ])
    ->setComponent('test_textwithsummary', [
    'type' => 'text_textarea_with_summary',
    'settings' => [
      'summary_rows' => 2,
      'show_summary' => TRUE,
    ],
  ])
    ->save();

  // Check the required summary.
  $entity = EntityTest::create([
    'name' => $this
      ->randomMachineName(),
    'type' => 'entity_test',
    'test_textwithsummary' => [
      'value' => $this
        ->randomMachineName(),
    ],
  ]);
  $form = \Drupal::service('entity.form_builder')
    ->getForm($entity);
  $this
    ->assertTrue(!empty($form['test_textwithsummary']['widget'][0]['summary']), 'Summary field is shown');
  $this
    ->assertTrue(!empty($form['test_textwithsummary']['widget'][0]['summary']['#required']), 'Summary field is required');

  // Test validation.

  /** @var \Symfony\Component\Validator\ConstraintViolation[] $violations */
  $violations = $entity
    ->validate();
  $this
    ->assertCount(1, $violations);
  $this
    ->assertEquals('test_textwithsummary.0.summary', $violations[0]
    ->getPropertyPath());
  $this
    ->assertEquals('The summary field is required for A text field', $violations[0]
    ->getMessage());
}