ContextualDynamicContextTest.php 5.79 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\contextual\Tests\ContextualDynamicContextTest.
6 7
 */

8 9
namespace Drupal\contextual\Tests;

10
use Drupal\simpletest\WebTestBase;
11
use Drupal\Core\Template\Attribute;
12

13 14 15
/**
 * Tests accessible links after inaccessible links on dynamic context.
 */
16
class ContextualDynamicContextTest extends WebTestBase {
17 18 19 20 21 22

  /**
   * Modules to enable.
   *
   * @var array
   */
23
  public static $modules = array('contextual', 'node', 'views', 'views_ui');
24

25 26 27 28 29 30 31 32 33
  public static function getInfo() {
    return array(
      'name' => 'Contextual links on node lists',
      'description' => 'Tests if contextual links are showing on the front page depending on permissions.',
      'group' => 'Contextual',
    );
  }

  function setUp() {
34
    parent::setUp();
35

36 37
    $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
    $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
38 39 40 41

    $this->editor_user = $this->drupalCreateUser(array('access content', 'access contextual links', 'edit any article content'));
    $this->authenticated_user = $this->drupalCreateUser(array('access content', 'access contextual links'));
    $this->anonymous_user = $this->drupalCreateUser(array('access content'));
42 43 44
  }

  /**
45 46 47 48
   * Tests contextual links with different permissions.
   *
   * Ensures that contextual link placeholders always exist, even if the user is
   * not allowed to use contextual links.
49
   */
50 51 52
  function testDifferentPermissions() {
    $this->drupalLogin($this->editor_user);

53 54 55 56
    // Create three nodes in the following order:
    // - An article, which should be user-editable.
    // - A page, which should not be user-editable.
    // - A second article, which should also be user-editable.
57 58 59 60
    $node1 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
    $node2 = $this->drupalCreateNode(array('type' => 'page', 'promote' => 1));
    $node3 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));

61 62 63
    // Now, on the front page, all article nodes should have contextual links
    // placeholders, as should the view that contains them.
    $ids = array(
64 65 66 67
      'node:node=' . $node1->id() . ':',
      'node:node=' . $node2->id() . ':',
      'node:node=' . $node3->id() . ':',
      'views_ui_edit:view=frontpage:location=page&name=frontpage&display_id=page_1',
68 69 70 71 72 73 74 75 76 77 78 79 80
    );

    // Editor user: can access contextual links and can edit articles.
    $this->drupalGet('node');
    for ($i = 0; $i < count($ids); $i++) {
      $this->assertContextualLinkPlaceHolder($ids[$i]);
    }
    $this->renderContextualLinks(array(), 'node');
    $this->assertResponse(400);
    $this->assertRaw('No contextual ids specified.');
    $response = $this->renderContextualLinks($ids, 'node');
    $this->assertResponse(200);
    $json = drupal_json_decode($response);
81
    $this->assertIdentical($json[$ids[0]], '<ul class="contextual-links"><li class="nodepage-edit"><a href="' . base_path() . 'node/1/edit?destination=node">Edit</a></li></ul>');
82
    $this->assertIdentical($json[$ids[1]], '');
83
    $this->assertIdentical($json[$ids[2]], '<ul class="contextual-links"><li class="nodepage-edit"><a href="' . base_path() . 'node/3/edit?destination=node">Edit</a></li></ul>');
84 85 86 87
    $this->assertIdentical($json[$ids[3]], '');

    // Authenticated user: can access contextual links, cannot edit articles.
    $this->drupalLogin($this->authenticated_user);
88
    $this->drupalGet('node');
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    for ($i = 0; $i < count($ids); $i++) {
      $this->assertContextualLinkPlaceHolder($ids[$i]);
    }
    $this->renderContextualLinks(array(), 'node');
    $this->assertResponse(400);
    $this->assertRaw('No contextual ids specified.');
    $response = $this->renderContextualLinks($ids, 'node');
    $this->assertResponse(200);
    $json = drupal_json_decode($response);
    $this->assertIdentical($json[$ids[0]], '');
    $this->assertIdentical($json[$ids[1]], '');
    $this->assertIdentical($json[$ids[2]], '');
    $this->assertIdentical($json[$ids[3]], '');

    // Anonymous user: cannot access contextual links.
    $this->drupalLogin($this->anonymous_user);
    $this->drupalGet('node');
    for ($i = 0; $i < count($ids); $i++) {
      $this->assertContextualLinkPlaceHolder($ids[$i]);
    }
    $this->renderContextualLinks(array(), 'node');
    $this->assertResponse(403);
    $this->renderContextualLinks($ids, 'node');
    $this->assertResponse(403);
  }

  /**
   * Asserts that a contextual link placeholder with the given id exists.
   *
   * @param string $id
   *   A contextual link id.
   *
   * @return bool
   */
  protected function assertContextualLinkPlaceHolder($id) {
124
    $this->assertRaw('<div' . new Attribute(array('data-contextual-id' => $id)) . '></div>', format_string('Contextual link placeholder with id @id exists.', array('@id' => $id)));
125 126 127 128 129 130 131 132 133 134 135
  }

  /**
   * Asserts that a contextual link placeholder with the given id does not exist.
   *
   * @param string $id
   *   A contextual link id.
   *
   * @return bool
   */
  protected function assertNoContextualLinkPlaceHolder($id) {
136
    $this->assertNoRaw('<div' . new Attribute(array('data-contextual-id' => $id)) . '></div>', format_string('Contextual link placeholder with id @id does not exist.', array('@id' => $id)));
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  }

  /**
   * Get server-rendered contextual links for the given contextual link ids.
   *
   * @param array $ids
   *   An array of contextual link ids.
   * @param string $current_path
   *   The Drupal path for the page for which the contextual links are rendered.
   *
   * @return string
   *   The response body.
   */
  protected function renderContextualLinks($ids, $current_path) {
    $post = array();
    for ($i = 0; $i < count($ids); $i++) {
      $post['ids[' . $i . ']'] = $ids[$i];
    }
155
    return $this->drupalPost('contextual/render', 'application/json', $post, array('query' => array('destination' => $current_path)));
156 157
  }
}