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

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

    // Test hook_entity_update(): decrement, by modifying the last revision:
95
    // remove the data-entity-type attribute from the body field.
96
    $body = $node->get('body')->first()->get('value');
97
    $original_value = $body->getValue();
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    $new_value = str_replace('data-entity-type', 'data-entity-type-modified', $original_value);
    $body->setValue($new_value);
    $node->save();
    $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), $file_usage->listUsage($image), 'The image has 2 usages.');

    // Test editor_entity_update(): increment again by creating a new revision:
    // read the data- attributes to the body field.
    $node->setNewRevision(TRUE);
    $node->get('body')->first()->get('value')->setValue($original_value);
    $node->save();
    $this->assertIdentical(array('editor' => array('node' => array(1 => '3'))), $file_usage->listUsage($image), 'The image has 3 usages.');

    // Test hook_entity_update(): decrement, by modifying the last revision:
    // remove the data-entity-uuid attribute from the body field.
    $body = $node->get('body')->first()->get('value');
    $new_value = str_replace('data-entity-uuid', 'data-entity-uuid-modified', $original_value);
114 115
    $body->setValue($new_value);
    $node->save();
116
    $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), $file_usage->listUsage($image), 'The image has 2 usages.');
117 118

    // Test hook_entity_update(): increment, by modifying the last revision:
119
    // read the data- attributes to the body field.
120
    $node->get('body')->first()->get('value')->setValue($original_value);
121
    $node->save();
122
    $this->assertIdentical(array('editor' => array('node' => array(1 => '3'))), $file_usage->listUsage($image), 'The image has 3 usages.');
123 124 125

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

    // Test editor_entity_delete().
    $node->delete();
130
    $this->assertIdentical(array(), $file_usage->listUsage($image), 'The image has zero usages again.');
131 132 133
  }

}