3 namespace Drupal\Tests\text\Kernel;
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\FieldItemInterface;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\Tests\field\Kernel\FieldKernelTestBase;
9 use Drupal\field\Entity\FieldStorageConfig;
10 use Drupal\filter\Entity\FilterFormat;
13 * Tests using entity fields of the text summary field type.
17 class TextWithSummaryItemTest extends FieldKernelTestBase {
24 public static $modules = ['filter'];
27 * Field storage entity.
29 * @var \Drupal\field\Entity\FieldStorageConfig.
31 protected $fieldStorage;
36 * @var \Drupal\field\Entity\FieldConfig
41 protected function setUp() {
44 $this->installEntitySchema('entity_test_rev');
46 // Create the necessary formats.
47 $this->installConfig(['filter']);
48 FilterFormat::create([
49 'format' => 'no_filters',
55 * Tests processed properties.
57 public function testCrudAndUpdate() {
58 $entity_type = 'entity_test';
59 $this->createField($entity_type);
61 // Create an entity with a summary and no text format.
62 $storage = $this->container->get('entity_type.manager')
63 ->getStorage($entity_type);
64 $entity = $storage->create();
65 $entity->summary_field->value = $value = $this->randomMachineName();
66 $entity->summary_field->summary = $summary = $this->randomMachineName();
67 $entity->summary_field->format = NULL;
68 $entity->name->value = $this->randomMachineName();
71 $entity = $storage->load($entity->id());
72 $this->assertTrue($entity->summary_field instanceof FieldItemListInterface, 'Field implements interface.');
73 $this->assertTrue($entity->summary_field[0] instanceof FieldItemInterface, 'Field item implements interface.');
74 $this->assertEqual($entity->summary_field->value, $value);
75 $this->assertEqual($entity->summary_field->summary, $summary);
76 $this->assertNull($entity->summary_field->format);
77 // Even if no format is given, if text processing is enabled, the default
79 $this->assertEqual($entity->summary_field->processed, "<p>$value</p>\n");
80 $this->assertEqual($entity->summary_field->summary_processed, "<p>$summary</p>\n");
82 // Change the format, this should update the processed properties.
83 $entity->summary_field->format = 'no_filters';
84 $this->assertEqual($entity->summary_field->processed, $value);
85 $this->assertEqual($entity->summary_field->summary_processed, $summary);
87 // Test the generateSampleValue() method.
88 $entity = $this->container->get('entity_type.manager')
89 ->getStorage($entity_type)
91 $entity->summary_field->generateSampleItems();
92 $this->entityValidateAndSave($entity);
96 * Creates a text_with_summary field storage and field.
98 * @param string $entity_type
99 * Entity type for which the field should be created.
101 protected function createField($entity_type) {
103 $this->fieldStorage = FieldStorageConfig::create([
104 'field_name' => 'summary_field',
105 'entity_type' => $entity_type,
106 'type' => 'text_with_summary',
111 $this->fieldStorage->save();
112 $this->field = FieldConfig::create([
113 'field_storage' => $this->fieldStorage,
114 'bundle' => $entity_type,
116 $this->field->save();