EditorFileUsageTest.php 8.37 KB
Newer Older
1 2
<?php

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 11
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
12
use Drupal\filter\Entity\FilterFormat;
13 14

/**
15 16 17
 * Tests tracking of file usage by the Text Editor module.
 *
 * @group editor
18
 */
19
class EditorFileUsageTest extends EntityKernelTestBase {
20 21 22 23 24 25

  /**
   * Modules to enable.
   *
   * @var array
   */
26
  public static $modules = array('editor', 'editor_test', 'node', 'file');
27

28
  protected function setUp() {
29
    parent::setUp();
30 31 32
    $this->installEntitySchema('file');
    $this->installSchema('node', array('node_access'));
    $this->installSchema('file', array('file_usage'));
33
    $this->installConfig(['node']);
34 35

    // Add text formats.
36
    $filtered_html_format = FilterFormat::create(array(
37 38 39 40 41 42 43
      'format' => 'filtered_html',
      'name' => 'Filtered HTML',
      'weight' => 0,
      'filters' => array(),
    ));
    $filtered_html_format->save();

44 45 46 47 48
    // Set cardinality for body field.
    FieldStorageConfig::loadByName('node', 'body')
      ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
      ->save();

49
    // Set up text editor.
50
    $editor = Editor::create([
51 52
      'format' => 'filtered_html',
      'editor' => 'unicorn',
53
    ]);
54 55 56
    $editor->save();

    // 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 64
  }

  /**
   * Tests the configurable text editor manager.
   */
65
  public function testEditorEntityHooks() {
66 67 68 69 70 71 72 73
    $image_paths = array(
      0 => 'core/misc/druplicon.png',
      1 => 'core/misc/tree.png',
      2 => 'core/misc/help.png',
    );

    $image_entities = array();
    foreach ($image_paths as $key => $image_path) {
74
      $image = File::create();
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
      $image->setFileUri($image_path);
      $image->setFilename(drupal_basename($image->getFileUri()));
      $image->save();

      $file_usage = $this->container->get('file.usage');
      $this->assertIdentical(array(), $file_usage->listUsage($image), 'The image ' . $image_paths[$key] . ' has zero usages.');

      $image_entities[] = $image;
    }

    $body = array();
    foreach ($image_entities as $key => $image_entity) {
      // Don't be rude, say hello.
      $body_value = '<p>Hello, world!</p>';
      // Test handling of a valid image entry.
      $body_value .= '<img src="awesome-llama-' . $key . '.jpg" data-entity-type="file" data-entity-uuid="' . $image_entity->uuid() . '" />';
      // Test handling of an invalid data-entity-uuid attribute.
      $body_value .= '<img src="awesome-llama-' . $key . '.jpg" data-entity-type="file" data-entity-uuid="invalid-entity-uuid-value" />';
      // Test handling of an invalid data-entity-type attribute.
      $body_value .= '<img src="awesome-llama-' . $key . '.jpg" data-entity-type="invalid-entity-type-value" data-entity-uuid="' . $image_entity->uuid() . '" />';
      // Test handling of a non-existing UUID.
      $body_value .= '<img src="awesome-llama-' . $key . '.jpg" data-entity-type="file" data-entity-uuid="30aac704-ba2c-40fc-b609-9ed121aa90f4" />';

      $body[] = array(
        'value' => $body_value,
        'format' => 'filtered_html',
      );
    }

104
    // Test editor_entity_insert(): increment.
105
    $this->createUser();
106
    $node = $node = Node::create([
107 108
      'type' => 'page',
      'title' => 'test',
109
      'body' => $body,
110
      'uid' => 1,
111
    ]);
112
    $node->save();
113 114 115
    foreach ($image_entities as $key => $image_entity) {
      $this->assertIdentical(array('editor' => array('node' => array(1 => '1'))), $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 1 usage.');
    }
116 117 118 119 120 121 122

    // Test editor_entity_update(): increment, twice, by creating new revisions.
    $node->setNewRevision(TRUE);
    $node->save();
    $second_revision_id = $node->getRevisionId();
    $node->setNewRevision(TRUE);
    $node->save();
123 124 125
    foreach ($image_entities as $key => $image_entity) {
      $this->assertIdentical(array('editor' => array('node' => array(1 => '3'))), $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 3 usages.');
    }
126 127

    // Test hook_entity_update(): decrement, by modifying the last revision:
128
    // remove the data-entity-type attribute from the body field.
129 130 131 132 133 134 135
    $original_values = array();
    for ($i = 0; $i < count($image_entities); $i++) {
      $original_value = $node->body[$i]->value;
      $new_value = str_replace('data-entity-type', 'data-entity-type-modified', $original_value);
      $node->body[$i]->value = $new_value;
      $original_values[$i] = $original_value;
    }
136
    $node->save();
137 138 139
    foreach ($image_entities as $key => $image_entity) {
      $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 2 usages.');
    }
140 141 142 143

    // Test editor_entity_update(): increment again by creating a new revision:
    // read the data- attributes to the body field.
    $node->setNewRevision(TRUE);
144 145 146
    foreach ($original_values as $key => $original_value) {
      $node->body[$key]->value = $original_value;
    }
147
    $node->save();
148 149 150
    foreach ($image_entities as $key => $image_entity) {
      $this->assertIdentical(array('editor' => array('node' => array(1 => '3'))), $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 3 usages.');
    }
151 152 153

    // Test hook_entity_update(): decrement, by modifying the last revision:
    // remove the data-entity-uuid attribute from the body field.
154 155 156 157 158
    foreach ($original_values as $key => $original_value) {
      $original_value = $node->body[$key]->value;
      $new_value = str_replace('data-entity-type', 'data-entity-type-modified', $original_value);
      $node->body[$key]->value = $new_value;
    }
159
    $node->save();
160 161 162
    foreach ($image_entities as $key => $image_entity) {
      $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 2 usages.');
    }
163 164

    // Test hook_entity_update(): increment, by modifying the last revision:
165
    // read the data- attributes to the body field.
166 167 168
    foreach ($original_values as $key => $original_value) {
      $node->body[$key]->value = $original_value;
    }
169
    $node->save();
170 171 172
    foreach ($image_entities as $key => $image_entity) {
      $this->assertIdentical(array('editor' => array('node' => array(1 => '3'))), $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 3 usages.');
    }
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 177 178
    foreach ($image_entities as $key => $image_entity) {
      $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 2 usages.');
    }
179

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    // Populate both the body and summary. Because this will be the same
    // revision of the same node, it will record only one usage.
    foreach ($original_values as $key => $original_value) {
      $node->body[$key]->value = $original_value;
      $node->body[$key]->summary = $original_value;
    }
    $node->save();
    foreach ($image_entities as $key => $image_entity) {
      $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 2 usages.');
    }

    // Empty out the body value, but keep the summary. The number of usages
    // should not change.
    foreach ($original_values as $key => $original_value) {
      $node->body[$key]->value = '';
      $node->body[$key]->summary = $original_value;
    }
    $node->save();
    foreach ($image_entities as $key => $image_entity) {
      $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 2 usages.');
    }

202 203
    // Test editor_entity_delete().
    $node->delete();
204 205 206
    foreach ($image_entities as $key => $image_entity) {
      $this->assertIdentical(array(), $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has zero usages again.');
    }
207 208 209
  }

}