EditorFileUsageTest.php 4.04 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

/**
 * Unit tests for editor.module's entity hooks to track file usage.
 */
15
class EditorFileUsageTest extends EntityUnitTestBase {
16 17 18 19 20 21

  /**
   * Modules to enable.
   *
   * @var array
   */
22
  public static $modules = array('editor', 'editor_test', 'node', 'file');
23 24 25 26 27 28 29 30 31

  public static function getInfo() {
    return array(
      'name' => 'Text Editor file usage',
      'description' => 'Tests tracking of file usage by the Text Editor module.',
      'group' => 'Text Editor',
    );
  }

32
  public function setUp() {
33
    parent::setUp();
34
    $this->installSchema('node', array('node', 'node_access', 'node_field_data', 'node_field_revision', 'node_revision'));
35
    $this->installSchema('file', array('file_managed', 'file_usage'));
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

    // 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();
  }

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

    // Test editor_entity_insert(): increment.
70
    $this->createUser();
71 72 73 74 75 76
    $node = entity_create('node', array(
      'type' => 'page',
      'title' => 'test',
      'body' => array(
        'value' => '<p>Hello, world!</p><img src="awesome-llama.jpg" data-editor-file-uuid="' . $image->uuid() . '" />',
        'format' => 'filtered_html',
77 78
      ),
      'uid' => 1,
79 80
    ));
    $node->save();
81
    $this->assertIdentical(array('editor' => array('node' => array(1 => '1'))), $file_usage->listUsage($image), 'The image has 1 usage.');
82 83 84 85 86 87 88

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

    // Test hook_entity_update(): decrement, by modifying the last revision:
    // remove the data- attribute from the body field.
93
    $body = $node->get('body')->first()->get('value');
94 95 96 97
    $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();
98
    $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), $file_usage->listUsage($image), 'The image has 2 usages.');
99 100 101

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

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

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

}