EditorFileUsageTest.php 4.17 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\editor\Tests\EditorFileUsageTest.
 */

namespace Drupal\editor\Tests;

10
use Drupal\system\Tests\Entity\EntityUnitTestBase;
11 12

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

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

26
  protected function setUp() {
27
    parent::setUp();
28 29 30
    $this->installEntitySchema('file');
    $this->installSchema('node', array('node_access'));
    $this->installSchema('file', array('file_usage'));
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

    // Add text formats.
    $filtered_html_format = entity_create('filter_format', array(
      'format' => 'filtered_html',
      'name' => 'Filtered HTML',
      'weight' => 0,
      'filters' => array(),
    ));
    $filtered_html_format->save();

    // Set up text editor.
    $editor = entity_create('editor', array(
      'format' => 'filtered_html',
      'editor' => 'unicorn',
    ));
    $editor->save();

    // Create a node type for testing.
    $type = entity_create('node_type', array('type' => 'page', 'name' => 'page'));
    $type->save();
51
    node_add_body_field($type);
52 53 54 55 56
  }

  /**
   * Tests the configurable text editor manager.
   */
57
  public function testEditorEntityHooks() {
58
    $image = entity_create('file');
59 60 61
    $image->setFileUri('core/misc/druplicon.png');
    $image->setFilename(drupal_basename($image->getFileUri()));
    $image->save();
62 63
    $file_usage = $this->container->get('file.usage');
    $this->assertIdentical(array(), $file_usage->listUsage($image), 'The image has zero usages.');
64

65 66 67 68 69
    $body_value = '<p>Hello, world!</p><img src="awesome-llama.jpg" data-editor-file-uuid="' . $image->uuid() . '" />';
    // Test handling of an invalid data- attribute.
    $body_value .= '<img src="awesome-llama.jpg" data-editor-file-uuid="invalid-editor-file-uuid-value" />';
    // Test handling of a non-existing UUID.
    $body_value .= '<img src="awesome-llama.jpg" data-editor-file-uuid="30aac704-ba2c-40fc-b609-9ed121aa90f4" />';
70
    // Test editor_entity_insert(): increment.
71
    $this->createUser();
72 73 74 75
    $node = entity_create('node', array(
      'type' => 'page',
      'title' => 'test',
      'body' => array(
76
        'value' => $body_value,
77
        'format' => 'filtered_html',
78 79
      ),
      'uid' => 1,
80 81
    ));
    $node->save();
82
    $this->assertIdentical(array('editor' => array('node' => array(1 => '1'))), $file_usage->listUsage($image), 'The image has 1 usage.');
83 84 85 86 87 88 89

    // 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();
90
    $this->assertIdentical(array('editor' => array('node' => array(1 => '3'))), $file_usage->listUsage($image), 'The image has 3 usages.');
91 92 93

    // Test hook_entity_update(): decrement, by modifying the last revision:
    // remove the data- attribute from the body field.
94
    $body = $node->get('body')->first()->get('value');
95 96 97 98
    $original_value = $body->getValue();
    $new_value = str_replace('data-editor-file-uuid', 'data-editor-file-uuid-modified', $original_value);
    $body->setValue($new_value);
    $node->save();
99
    $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), $file_usage->listUsage($image), 'The image has 2 usages.');
100 101 102

    // Test hook_entity_update(): increment, by modifying the last revision:
    // readd the data- attribute to the body field.
103
    $node->get('body')->first()->get('value')->setValue($original_value);
104
    $node->save();
105
    $this->assertIdentical(array('editor' => array('node' => array(1 => '3'))), $file_usage->listUsage($image), 'The image has 3 usages.');
106 107 108

    // Test editor_entity_revision_delete(): decrement, by deleting a revision.
    entity_revision_delete('node', $second_revision_id);
109
    $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), $file_usage->listUsage($image), 'The image has 2 usages.');
110 111 112

    // Test editor_entity_delete().
    $node->delete();
113
    $this->assertIdentical(array(), $file_usage->listUsage($image), 'The image has zero usages again.');
114 115 116
  }

}