3 namespace Drupal\Tests\editor\Kernel;
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;
15 * Tests tracking of file usage by the Text Editor module.
19 class EditorFileUsageTest extends EntityKernelTestBase {
26 public static $modules = ['editor', 'editor_test', 'node', 'file'];
28 protected function setUp() {
30 $this->installEntitySchema('file');
31 $this->installSchema('node', ['node_access']);
32 $this->installSchema('file', ['file_usage']);
33 $this->installConfig(['node']);
36 $filtered_html_format = FilterFormat::create([
37 'format' => 'filtered_html',
38 'name' => 'Filtered HTML',
42 $filtered_html_format->save();
44 // Set cardinality for body field.
45 FieldStorageConfig::loadByName('node', 'body')
46 ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
49 // Set up text editor.
50 $editor = Editor::create([
51 'format' => 'filtered_html',
52 'editor' => 'unicorn',
56 // Create a node type for testing.
57 $type = NodeType::create(['type' => 'page', 'name' => 'page']);
59 node_add_body_field($type);
63 * Tests the configurable text editor manager.
65 public function testEditorEntityHooks() {
67 0 => 'core/misc/druplicon.png',
68 1 => 'core/misc/tree.png',
69 2 => 'core/misc/help.png',
73 foreach ($image_paths as $key => $image_path) {
74 $image = File::create();
75 $image->setFileUri($image_path);
76 $image->setFilename(drupal_basename($image->getFileUri()));
79 $file_usage = $this->container->get('file.usage');
80 $this->assertIdentical([], $file_usage->listUsage($image), 'The image ' . $image_paths[$key] . ' has zero usages.');
82 $image_entities[] = $image;
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" />';
99 'value' => $body_value,
100 'format' => 'filtered_html',
104 // Test editor_entity_insert(): increment.
106 $node = $node = Node::create([
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.');
117 // Test editor_entity_update(): increment, twice, by creating new revisions.
118 $node->setNewRevision(TRUE);
120 $second_revision_id = $node->getRevisionId();
121 $node->setNewRevision(TRUE);
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.');
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;
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.');
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;
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.');
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;
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.');
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;
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.');
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.');
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;
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.');
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;
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.');
202 // Test editor_entity_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.');