QuickEditIntegrationLoadingTest.php 4.21 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\editor\Tests\QuickEditIntegrationLoadingTest.
6 7 8 9
 */

namespace Drupal\editor\Tests;

10
use Drupal\Component\Serialization\Json;
11
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
12 13 14
use Drupal\simpletest\WebTestBase;

/**
15
 * Tests Quick Edit module integration endpoints.
16 17
 *
 * @group editor
18
 */
19
class QuickEditIntegrationLoadingTest extends WebTestBase {
20 21 22 23 24 25

  /**
   * Modules to enable.
   *
   * @var array
   */
26
  public static $modules = array('quickedit', 'filter', 'node', 'editor');
27 28 29 30 31 32

  /**
   * The basic permissions necessary to view content and use in-place editing.
   *
   * @var array
   */
33
  protected static $basicPermissions = array('access content', 'create article content', 'use text format filtered_html', 'access contextual links');
34

35
  protected function setUp() {
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 61 62 63 64 65 66 67 68 69 70 71
    parent::setUp();

    // Create a text format.
    $filtered_html_format = entity_create('filter_format', array(
      'format' => 'filtered_html',
      'name' => 'Filtered HTML',
      'weight' => 0,
      'filters' => array(
        'filter_caption' => array(
          'status' => 1,
        ),
      ),
    ));
    $filtered_html_format->save();

    // Create a node type.
    $this->drupalCreateContentType(array(
      'type' => 'article',
      'name' => 'Article',
    ));

    // Create one node of the above node type using the above text format.
    $this->drupalCreateNode(array(
      'type' => 'article',
      'body' => array(
        0 => array(
          'value' => '<p>Do you also love Drupal?</p><img src="druplicon.png" data-caption="Druplicon" />',
          'format' => 'filtered_html',
        )
      )
    ));
  }

  /**
   * Test loading of untransformed text when a user doesn't have access to it.
   */
72
  public function testUsersWithoutPermission() {
73 74 75 76 77
    // Create 3 users, each with insufficient permissions, i.e. without either
    // or both of the following permissions:
    // - the 'access in-place editing' permission
    // - the 'edit any article content' permission (necessary to edit node 1)
    $users = array(
78 79 80
      $this->drupalCreateUser(static::$basicPermissions),
      $this->drupalCreateUser(array_merge(static::$basicPermissions, array('edit any article content'))),
      $this->drupalCreateUser(array_merge(static::$basicPermissions, array('access in-place editing')))
81 82 83 84 85 86 87 88 89 90 91
    );

    // Now test with each of the 3 users with insufficient permissions.
    foreach ($users as $user) {
      $this->drupalLogin($user);
      $this->drupalGet('node/1');

      // Ensure the text is transformed.
      $this->assertRaw('<p>Do you also love Drupal?</p><figure class="caption caption-img"><img src="druplicon.png" /><figcaption>Druplicon</figcaption></figure>');

      // Retrieving the untransformed text should result in an empty 403 response.
92
      $response = $this->drupalPost('editor/' . 'node/1/body/en/full', '', array(), array('query' => array(MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax')));
93
      $this->assertResponse(403);
94
      $this->assertIdentical('{}', $response);
95 96 97 98 99 100
    }
  }

  /**
   * Test loading of untransformed text when a user does have access to it.
   */
101
  public function testUserWithPermission() {
102
    $user = $this->drupalCreateUser(array_merge(static::$basicPermissions, array('edit any article content', 'access in-place editing')));
103 104 105 106 107 108
    $this->drupalLogin($user);
    $this->drupalGet('node/1');

    // Ensure the text is transformed.
    $this->assertRaw('<p>Do you also love Drupal?</p><figure class="caption caption-img"><img src="druplicon.png" /><figcaption>Druplicon</figcaption></figure>');

109
    $response = $this->drupalPost('editor/' . 'node/1/body/en/full', 'application/vnd.drupal-ajax', array());
110
    $this->assertResponse(200);
111
    $ajax_commands = Json::decode($response);
112 113 114 115 116 117
    $this->assertIdentical(1, count($ajax_commands), 'The untransformed text POST request results in one AJAX command.');
    $this->assertIdentical('editorGetUntransformedText', $ajax_commands[0]['command'], 'The first AJAX command is an editorGetUntransformedText command.');
    $this->assertIdentical('<p>Do you also love Drupal?</p><img src="druplicon.png" data-caption="Druplicon" />', $ajax_commands[0]['data'], 'The editorGetUntransformedText command contains the expected data.');
  }

}