Version 1
[yaffs-website] / web / core / modules / editor / tests / src / Kernel / EditorFileUsageTest.php
1 <?php
2
3 namespace Drupal\Tests\editor\Kernel;
4
5 use Drupal\editor\Entity\Editor;
6 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\file\Entity\File;
10 use Drupal\field\Entity\FieldStorageConfig;
11 use Drupal\Core\Field\FieldStorageDefinitionInterface;
12 use Drupal\filter\Entity\FilterFormat;
13
14 /**
15  * Tests tracking of file usage by the Text Editor module.
16  *
17  * @group editor
18  */
19 class EditorFileUsageTest extends EntityKernelTestBase {
20
21   /**
22    * Modules to enable.
23    *
24    * @var array
25    */
26   public static $modules = ['editor', 'editor_test', 'node', 'file'];
27
28   protected function setUp() {
29     parent::setUp();
30     $this->installEntitySchema('file');
31     $this->installSchema('node', ['node_access']);
32     $this->installSchema('file', ['file_usage']);
33     $this->installConfig(['node']);
34
35     // Add text formats.
36     $filtered_html_format = FilterFormat::create([
37       'format' => 'filtered_html',
38       'name' => 'Filtered HTML',
39       'weight' => 0,
40       'filters' => [],
41     ]);
42     $filtered_html_format->save();
43
44     // Set cardinality for body field.
45     FieldStorageConfig::loadByName('node', 'body')
46       ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
47       ->save();
48
49     // Set up text editor.
50     $editor = Editor::create([
51       'format' => 'filtered_html',
52       'editor' => 'unicorn',
53     ]);
54     $editor->save();
55
56     // Create a node type for testing.
57     $type = NodeType::create(['type' => 'page', 'name' => 'page']);
58     $type->save();
59     node_add_body_field($type);
60   }
61
62   /**
63    * Tests the configurable text editor manager.
64    */
65   public function testEditorEntityHooks() {
66     $image_paths = [
67       0 => 'core/misc/druplicon.png',
68       1 => 'core/misc/tree.png',
69       2 => 'core/misc/help.png',
70     ];
71
72     $image_entities = [];
73     foreach ($image_paths as $key => $image_path) {
74       $image = File::create();
75       $image->setFileUri($image_path);
76       $image->setFilename(drupal_basename($image->getFileUri()));
77       $image->save();
78
79       $file_usage = $this->container->get('file.usage');
80       $this->assertIdentical([], $file_usage->listUsage($image), 'The image ' . $image_paths[$key] . ' has zero usages.');
81
82       $image_entities[] = $image;
83     }
84
85     $body = [];
86     foreach ($image_entities as $key => $image_entity) {
87       // Don't be rude, say hello.
88       $body_value = '<p>Hello, world!</p>';
89       // Test handling of a valid image entry.
90       $body_value .= '<img src="awesome-llama-' . $key . '.jpg" data-entity-type="file" data-entity-uuid="' . $image_entity->uuid() . '" />';
91       // Test handling of an invalid data-entity-uuid attribute.
92       $body_value .= '<img src="awesome-llama-' . $key . '.jpg" data-entity-type="file" data-entity-uuid="invalid-entity-uuid-value" />';
93       // Test handling of an invalid data-entity-type attribute.
94       $body_value .= '<img src="awesome-llama-' . $key . '.jpg" data-entity-type="invalid-entity-type-value" data-entity-uuid="' . $image_entity->uuid() . '" />';
95       // Test handling of a non-existing UUID.
96       $body_value .= '<img src="awesome-llama-' . $key . '.jpg" data-entity-type="file" data-entity-uuid="30aac704-ba2c-40fc-b609-9ed121aa90f4" />';
97
98       $body[] = [
99         'value' => $body_value,
100         'format' => 'filtered_html',
101       ];
102     }
103
104     // Test editor_entity_insert(): increment.
105     $this->createUser();
106     $node = $node = Node::create([
107       'type' => 'page',
108       'title' => 'test',
109       'body' => $body,
110       'uid' => 1,
111     ]);
112     $node->save();
113     foreach ($image_entities as $key => $image_entity) {
114       $this->assertIdentical(['editor' => ['node' => [1 => '1']]], $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 1 usage.');
115     }
116
117     // Test editor_entity_update(): increment, twice, by creating new revisions.
118     $node->setNewRevision(TRUE);
119     $node->save();
120     $second_revision_id = $node->getRevisionId();
121     $node->setNewRevision(TRUE);
122     $node->save();
123     foreach ($image_entities as $key => $image_entity) {
124       $this->assertIdentical(['editor' => ['node' => [1 => '3']]], $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 3 usages.');
125     }
126
127     // Test hook_entity_update(): decrement, by modifying the last revision:
128     // remove the data-entity-type attribute from the body field.
129     $original_values = [];
130     for ($i = 0; $i < count($image_entities); $i++) {
131       $original_value = $node->body[$i]->value;
132       $new_value = str_replace('data-entity-type', 'data-entity-type-modified', $original_value);
133       $node->body[$i]->value = $new_value;
134       $original_values[$i] = $original_value;
135     }
136     $node->save();
137     foreach ($image_entities as $key => $image_entity) {
138       $this->assertIdentical(['editor' => ['node' => [1 => '2']]], $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 2 usages.');
139     }
140
141     // Test editor_entity_update(): increment again by creating a new revision:
142     // read the data- attributes to the body field.
143     $node->setNewRevision(TRUE);
144     foreach ($original_values as $key => $original_value) {
145       $node->body[$key]->value = $original_value;
146     }
147     $node->save();
148     foreach ($image_entities as $key => $image_entity) {
149       $this->assertIdentical(['editor' => ['node' => [1 => '3']]], $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 3 usages.');
150     }
151
152     // Test hook_entity_update(): decrement, by modifying the last revision:
153     // remove the data-entity-uuid attribute from the body field.
154     foreach ($original_values as $key => $original_value) {
155       $original_value = $node->body[$key]->value;
156       $new_value = str_replace('data-entity-type', 'data-entity-type-modified', $original_value);
157       $node->body[$key]->value = $new_value;
158     }
159     $node->save();
160     foreach ($image_entities as $key => $image_entity) {
161       $this->assertIdentical(['editor' => ['node' => [1 => '2']]], $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 2 usages.');
162     }
163
164     // Test hook_entity_update(): increment, by modifying the last revision:
165     // read the data- attributes to the body field.
166     foreach ($original_values as $key => $original_value) {
167       $node->body[$key]->value = $original_value;
168     }
169     $node->save();
170     foreach ($image_entities as $key => $image_entity) {
171       $this->assertIdentical(['editor' => ['node' => [1 => '3']]], $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 3 usages.');
172     }
173
174     // Test editor_entity_revision_delete(): decrement, by deleting a revision.
175     $this->container->get('entity_type.manager')->getStorage('node')->deleteRevision($second_revision_id);
176     foreach ($image_entities as $key => $image_entity) {
177       $this->assertIdentical(['editor' => ['node' => [1 => '2']]], $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 2 usages.');
178     }
179
180     // Populate both the body and summary. Because this will be the same
181     // revision of the same node, it will record only one usage.
182     foreach ($original_values as $key => $original_value) {
183       $node->body[$key]->value = $original_value;
184       $node->body[$key]->summary = $original_value;
185     }
186     $node->save();
187     foreach ($image_entities as $key => $image_entity) {
188       $this->assertIdentical(['editor' => ['node' => [1 => '2']]], $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 2 usages.');
189     }
190
191     // Empty out the body value, but keep the summary. The number of usages
192     // should not change.
193     foreach ($original_values as $key => $original_value) {
194       $node->body[$key]->value = '';
195       $node->body[$key]->summary = $original_value;
196     }
197     $node->save();
198     foreach ($image_entities as $key => $image_entity) {
199       $this->assertIdentical(['editor' => ['node' => [1 => '2']]], $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 2 usages.');
200     }
201
202     // Test editor_entity_delete().
203     $node->delete();
204     foreach ($image_entities as $key => $image_entity) {
205       $this->assertIdentical([], $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has zero usages again.');
206     }
207   }
208
209 }