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 35 36 37
    $this->installEntitySchema('node');
    $this->installEntitySchema('file');
    $this->installSchema('node', array('node_access'));
    $this->installSchema('file', array('file_usage'));
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

    // 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.
   */
63
  public function testEditorEntityHooks() {
64
    $image = entity_create('file');
65 66 67
    $image->setFileUri('core/misc/druplicon.png');
    $image->setFilename(drupal_basename($image->getFileUri()));
    $image->save();
68 69
    $file_usage = $this->container->get('file.usage');
    $this->assertIdentical(array(), $file_usage->listUsage($image), 'The image has zero usages.');
70 71

    // Test editor_entity_insert(): increment.
72
    $this->createUser();
73 74 75 76 77 78
    $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',
79 80
      ),
      'uid' => 1,
81 82
    ));
    $node->save();
83
    $this->assertIdentical(array('editor' => array('node' => array(1 => '1'))), $file_usage->listUsage($image), 'The image has 1 usage.');
84 85 86 87 88 89 90

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

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

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

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

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

}