function TextItemBaseTest::testTextFieldSampleValue

Same name and namespace in other branches
  1. 11.x core/modules/text/tests/src/Kernel/TextItemBaseTest.php \Drupal\Tests\text\Kernel\TextItemBaseTest::testTextFieldSampleValue()
  2. 10 core/modules/text/tests/src/Kernel/TextItemBaseTest.php \Drupal\Tests\text\Kernel\TextItemBaseTest::testTextFieldSampleValue()
  3. 9 core/modules/text/tests/src/Kernel/TextItemBaseTest.php \Drupal\Tests\text\Kernel\TextItemBaseTest::testTextFieldSampleValue()

Tests creation of sample values.

@legacy-covers ::generateSampleValue

Attributes

#[DataProvider('providerTextFieldSampleValue')]

File

core/modules/text/tests/src/Kernel/TextItemBaseTest.php, line 37

Class

TextItemBaseTest
Tests TextItemBase.

Namespace

Drupal\Tests\text\Kernel

Code

public function testTextFieldSampleValue(int $max_length, array $formats, string $expected_format) : void {
  // Install filter config so plain_text is registered as the site fallback.
  $this->installConfig([
    'filter',
  ]);
  $this->assertSame('plain_text', \Drupal::service(FilterFormatRepositoryInterface::class)->getFallbackFormatId());
  // Create a text field.
  $field_definition = BaseFieldDefinition::create('text')->setTargetEntityTypeId('foo');
  foreach ($formats as $format_id) {
    if ($format_id === 'plain_text') {
      continue;
    }
    FilterFormat::create([
      'format' => $format_id,
      'name' => $format_id,
    ])->save();
  }
  if (!empty($formats)) {
    $field_definition->setSetting('allowed_formats', $formats);
  }
  // Ensure testing of max_lengths from 1 to 3 because generateSampleValue
  // creates a sentence with a maximum number of words set to 1/3 of the
  // max_length of the field.
  $field_definition->setSetting('max_length', $max_length);
  $sample_value = TextItemBase::generateSampleValue($field_definition);
  $this->assertEquals($max_length, strlen($sample_value['value']));
  $this->assertSame($expected_format, $sample_value['format']);
}

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