View source
<?php
namespace Drupal\Tests\text\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\filter\Render\FilteredMarkup;
use Drupal\KernelTests\KernelTestBase;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\user\Traits\UserCreationTrait;
class TextSummaryTest extends KernelTestBase {
use UserCreationTrait;
protected static $modules = [
'system',
'user',
'filter',
'text',
'field',
'entity_test',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'text',
]);
}
public function testFirstSentenceQuestion() {
$text = 'A question? A sentence. Another sentence.';
$expected = 'A question? A sentence.';
$this
->assertTextSummary($text, $expected, NULL, 30);
}
public function testLongSentence() {
$text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ' . 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ' . 'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ' . 'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
$expected = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ' . 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ' . 'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.';
$this
->assertTextSummary($text, $expected, NULL, 340);
}
public function testLength() {
FilterFormat::create([
'format' => 'autop',
'name' => 'Autop',
'filters' => [
'filter_autop' => [
'status' => 1,
],
],
])
->save();
FilterFormat::create([
'format' => 'autop_correct',
'name' => 'Autop correct',
'filters' => [
'filter_autop' => [
'status' => 1,
],
'filter_htmlcorrector' => [
'status' => 1,
],
],
])
->save();
$text = "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>";
$format = NULL;
$i = 0;
$this
->assertTextSummary($text, "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<", $format, $i++);
$this
->assertTextSummary($text, "<p", $format, $i++);
$this
->assertTextSummary($text, "<p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\n", $format, $i++);
$this
->assertTextSummary($text, "<p>\nH", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n<", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>", $format, $i++);
$format = 'autop';
$i = 0;
$this
->assertTextSummary($text, "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<", $format, $i++);
$this
->assertTextSummary($text, "<p", $format, $i++);
$this
->assertTextSummary($text, "<p>", $format, $i++);
$this
->assertTextSummary($text, "<p>", $format, $i++);
$this
->assertTextSummary($text, "<p>", $format, $i++);
$this
->assertTextSummary($text, "<p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>", $format, $i++);
$format = 'autop_correct';
$i = 0;
$this
->assertTextSummary($text, "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>", $format, $i++);
$this
->assertTextSummary($text, "", $format, $i++);
$this
->assertTextSummary($text, "<p></p>", $format, $i++);
$this
->assertTextSummary($text, "<p></p>", $format, $i++);
$this
->assertTextSummary($text, "<p></p>", $format, $i++);
$this
->assertTextSummary($text, "<p></p>", $format, $i++);
$this
->assertTextSummary($text, "<p></p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>", $format, $i++);
$this
->assertTextSummary($text, "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>", $format, $i++);
}
public function testInvalidFilterFormat() {
$this
->assertTextSummary($this
->randomString(100), '', 'non_existent_format');
}
public function assertTextSummary(string $text, string $expected, ?string $format = NULL, int $size = NULL) : void {
$summary = text_summary($text, $format, $size);
$this
->assertSame($expected, $summary, new FormattableMarkup('<pre style="white-space: pre-wrap">@actual</pre> is identical to <pre style="white-space: pre-wrap">@expected</pre>', [
'@actual' => $summary,
'@expected' => $expected,
]));
}
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();
$entity = EntityTest::create([
'name' => $this
->randomMachineName(),
'type' => 'entity_test',
'test_textwithsummary' => [
'value' => $this
->randomMachineName(),
],
]);
$form = \Drupal::service('entity.form_builder')
->getForm($entity);
$this
->assertNotEmpty($form['test_textwithsummary']['widget'][0]['summary'], 'Summary field is shown');
$this
->assertNotEmpty($form['test_textwithsummary']['widget'][0]['summary']['#required'], 'Summary field is required');
$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());
}
public function testNormalization() {
FilterFormat::create([
'format' => 'filter_html_enabled',
'name' => 'Filter HTML enabled',
'filters' => [
'filter_html' => [
'status' => 1,
'settings' => [
'allowed_html' => '<strong>',
],
],
],
])
->save();
FilterFormat::create([
'format' => 'filter_htmlcorrector_enabled',
'name' => 'Filter HTML corrector enabled',
'filters' => [
'filter_htmlcorrector' => [
'status' => 1,
],
],
])
->save();
FilterFormat::create([
'format' => 'neither_filter_enabled',
'name' => 'Neither filter enabled',
'filters' => [],
])
->save();
$filtered_markup = FilteredMarkup::create('<div><strong><span>Hello World</span></strong></div>');
$summary = text_summary($filtered_markup, 'filter_html_enabled', 30);
$this
->assertStringContainsString('<div><strong><span>', $summary);
$this
->assertStringContainsString('</span></strong></div>', $summary);
$summary = text_summary($filtered_markup, 'filter_htmlcorrector_enabled', 30);
$this
->assertStringContainsString('<div><strong><span>', $summary);
$this
->assertStringContainsString('</span></strong></div>', $summary);
$summary = text_summary($filtered_markup, 'neither_filter_enabled', 30);
$this
->assertStringContainsString('<div><strong><span>', $summary);
$this
->assertStringNotContainsString('</span></strong></div>', $summary);
}
}