TextWithSummaryItemTest.php in Drupal 10
File
core/modules/text/tests/src/Kernel/TextWithSummaryItemTest.php
View source
<?php
namespace Drupal\Tests\text\Kernel;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\filter\Entity\FilterFormat;
class TextWithSummaryItemTest extends FieldKernelTestBase {
protected static $modules = [
'filter',
];
protected $fieldStorage;
protected $field;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('entity_test_rev');
$this
->installConfig([
'filter',
]);
FilterFormat::create([
'format' => 'no_filters',
'name' => 'No filters',
'filters' => [],
])
->save();
}
public function testCrudAndUpdate() {
$entity_type = 'entity_test';
$this
->createField($entity_type);
$storage = $this->container
->get('entity_type.manager')
->getStorage($entity_type);
$entity = $storage
->create();
$entity->summary_field->value = $value = $this
->randomMachineName();
$entity->summary_field->summary = $summary = $this
->randomMachineName();
$entity->summary_field->format = NULL;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$entity = $storage
->load($entity
->id());
$this
->assertInstanceOf(FieldItemListInterface::class, $entity->summary_field);
$this
->assertInstanceOf(FieldItemInterface::class, $entity->summary_field[0]);
$this
->assertEquals($value, $entity->summary_field->value);
$this
->assertEquals($summary, $entity->summary_field->summary);
$this
->assertNull($entity->summary_field->format);
$this
->assertEquals("<p>{$value}</p>\n", $entity->summary_field->processed);
$this
->assertEquals("<p>{$summary}</p>\n", $entity->summary_field->summary_processed);
$entity->summary_field->format = 'no_filters';
$this
->assertEquals($value, $entity->summary_field->processed);
$this
->assertEquals($summary, $entity->summary_field->summary_processed);
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create();
$entity->summary_field
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}
protected function createField($entity_type) {
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => 'summary_field',
'entity_type' => $entity_type,
'type' => 'text_with_summary',
'settings' => [
'max_length' => 10,
],
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => $entity_type,
]);
$this->field
->save();
}
}