Version 1
[yaffs-website] / web / core / modules / text / tests / src / Kernel / TextWithSummaryItemTest.php
1 <?php
2
3 namespace Drupal\Tests\text\Kernel;
4
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;
11
12 /**
13  * Tests using entity fields of the text summary field type.
14  *
15  * @group text
16  */
17 class TextWithSummaryItemTest extends FieldKernelTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['filter'];
25
26   /**
27    * Field storage entity.
28    *
29    * @var \Drupal\field\Entity\FieldStorageConfig.
30    */
31   protected $fieldStorage;
32
33   /**
34    * Field entity.
35    *
36    * @var \Drupal\field\Entity\FieldConfig
37    */
38   protected $field;
39
40
41   protected function setUp() {
42     parent::setUp();
43
44     $this->installEntitySchema('entity_test_rev');
45
46     // Create the necessary formats.
47     $this->installConfig(['filter']);
48     FilterFormat::create([
49       'format' => 'no_filters',
50       'filters' => [],
51     ])->save();
52   }
53
54   /**
55    * Tests processed properties.
56    */
57   public function testCrudAndUpdate() {
58     $entity_type = 'entity_test';
59     $this->createField($entity_type);
60
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();
69     $entity->save();
70
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
78     // format is used.
79     $this->assertEqual($entity->summary_field->processed, "<p>$value</p>\n");
80     $this->assertEqual($entity->summary_field->summary_processed, "<p>$summary</p>\n");
81
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);
86
87     // Test the generateSampleValue() method.
88     $entity = $this->container->get('entity_type.manager')
89       ->getStorage($entity_type)
90       ->create();
91     $entity->summary_field->generateSampleItems();
92     $this->entityValidateAndSave($entity);
93   }
94
95   /**
96    * Creates a text_with_summary field storage and field.
97    *
98    * @param string $entity_type
99    *   Entity type for which the field should be created.
100    */
101   protected function createField($entity_type) {
102     // Create a field .
103     $this->fieldStorage = FieldStorageConfig::create([
104       'field_name' => 'summary_field',
105       'entity_type' => $entity_type,
106       'type' => 'text_with_summary',
107       'settings' => [
108         'max_length' => 10,
109       ]
110     ]);
111     $this->fieldStorage->save();
112     $this->field = FieldConfig::create([
113       'field_storage' => $this->fieldStorage,
114       'bundle' => $entity_type,
115     ]);
116     $this->field->save();
117   }
118
119 }