function TextSummaryTest::testRequiredSummary

Same name and namespace in other branches
  1. 9 core/modules/text/tests/src/Kernel/TextSummaryTest.php \Drupal\Tests\text\Kernel\TextSummaryTest::testRequiredSummary()
  2. 8.9.x core/modules/text/tests/src/Kernel/TextSummaryTest.php \Drupal\Tests\text\Kernel\TextSummaryTest::testRequiredSummary()
  3. 10 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 255

Class

TextSummaryTest
Tests <a href="/api/drupal/core%21modules%21text%21text.module/function/text_summary/11.x" title="Generates a trimmed, formatted version of a text field value." class="local">text_summary</a>() with different strings and lengths.

Namespace

Drupal\Tests\text\Kernel

Code

public function testRequiredSummary() : void {
    $this->installEntitySchema('entity_test');
    $this->setUpCurrentUser();
    $field_definition = FieldStorageConfig::create([
        'field_name' => 'test_text_with_summary',
        'type' => 'text_with_summary',
        'entity_type' => 'entity_test',
        'cardinality' => 1,
        'settings' => [
            'max_length' => 200,
        ],
    ]);
    $field_definition->save();
    $instance = FieldConfig::create([
        'field_name' => 'test_text_with_summary',
        '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_text_with_summary', [
        '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_text_with_summary' => [
            'value' => $this->randomMachineName(),
        ],
    ]);
    $form = \Drupal::service('entity.form_builder')->getForm($entity);
    $this->assertNotEmpty($form['test_text_with_summary']['widget'][0]['summary'], 'Summary field is shown');
    $this->assertNotEmpty($form['test_text_with_summary']['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_text_with_summary.0.summary', $violations[0]->getPropertyPath());
    $this->assertEquals('The summary field is required for A text field', $violations[0]->getMessage());
}

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