NodeRevisionPermissionsTest.php 5.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php

/**
 * @file
 * Definition of Drupal\node\Tests\NodeRevisionPermissionsTest.
 */

namespace Drupal\node\Tests;

/**
 * Tests user permissions for node revisions.
 */
class NodeRevisionPermissionsTest extends NodeTestBase {
  protected $node_revisions = array();
  protected $accounts = array();

  // Map revision permission names to node revision access ops.
  protected $map = array(
19 20 21 22 23 24 25 26 27 28
    'view' => 'view all revisions',
    'update' => 'revert all revisions',
    'delete' => 'delete all revisions',
  );

  // Map revision permission names to node type revision access ops.
  protected $type_map = array(
    'view' => 'view page revisions',
    'update' => 'revert page revisions',
    'delete' => 'delete page revisions',
29 30 31 32 33 34 35 36 37 38 39 40 41
  );

  public static function getInfo() {
    return array(
      'name' => 'Node revision permissions',
      'description' => 'Tests user permissions for node revision operations.',
      'group' => 'Node',
    );
  }

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

42 43 44 45 46 47 48 49 50 51
    $types = array('page', 'article');

    foreach ($types as $type) {
      // Create a node with several revisions.
      $nodes[$type] = $this->drupalCreateNode(array('type' => $type));
      $this->node_revisions[$type][] = $nodes[$type];

      for ($i = 0; $i < 3; $i++) {
        // Create a revision for the same nid and settings with a random log.
        $revision = clone $nodes[$type];
52
        $revision->setNewRevision();
53
        $revision->log = $this->randomName(32);
54
        $revision->save();
55 56
        $this->node_revisions[$type][] = $revision;
      }
57
    }
58
  }
59

60 61 62 63
  /**
   * Tests general revision access permissions.
   */
  function testNodeRevisionAccessAnyType() {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    // Create three users, one with each revision permission.
    foreach ($this->map as $op => $permission) {
      // Create the user.
      $account = $this->drupalCreateUser(
        array(
          'access content',
          'edit any page content',
          'delete any page content',
          $permission,
        )
      );
      $account->op = $op;
      $this->accounts[] = $account;
    }

    // Create an admin account (returns TRUE for all revision permissions).
    $admin_account = $this->drupalCreateUser(array('access content', 'administer nodes'));
    $admin_account->is_admin = TRUE;
    $this->accounts['admin'] = $admin_account;
83
    $accounts['admin'] = $admin_account;
84 85 86 87 88

    // Create a normal account (returns FALSE for all revision permissions).
    $normal_account = $this->drupalCreateUser();
    $normal_account->op = FALSE;
    $this->accounts[] = $normal_account;
89 90
    $accounts[] = $normal_account;
    $revision = $this->node_revisions['page'][1];
91 92 93 94 95 96 97

    $parameters = array(
      'op' => array_keys($this->map),
      'account' => $this->accounts,
    );

    $permutations = $this->generatePermutations($parameters);
98

99
    foreach ($permutations as $case) {
100
      // Skip this test if there are no revisions for the node.
101
      if (!($revision->isDefaultRevision() && (db_query('SELECT COUNT(vid) FROM {node_field_revision} WHERE nid = :nid', array(':nid' => $revision->nid))->fetchField() == 1 || $case['op'] == 'update' || $case['op'] == 'delete'))) {
102 103 104 105 106 107
        if (!empty($case['account']->is_admin) || user_access($this->map[$case['op']], $case['account'])) {
          $this->assertTrue(_node_revision_access($revision, $case['op'], $case['account']), "{$this->map[$case['op']]} granted.");
        }
        else {
          $this->assertFalse(_node_revision_access($revision, $case['op'], $case['account']), "{$this->map[$case['op']]} not granted.");
        }
108 109 110 111 112
      }
    }

    // Test that access is FALSE for a node administrator with an invalid $node
    // or $op parameters.
113
    $admin_account = $accounts['admin'];
114
    $this->assertFalse(_node_revision_access($revision, 'invalid-op', $admin_account), '_node_revision_access() returns FALSE with an invalid op.');
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  }

  /**
   * Tests revision access permissions for a specific content type.
   */
  function testNodeRevisionAccessPerType() {
    // Create three users, one with each revision permission.
    foreach ($this->type_map as $op => $permission) {
      // Create the user.
      $account = $this->drupalCreateUser(
        array(
          'access content',
          'edit any page content',
          'delete any page content',
          $permission,
        )
      );
      $account->op = $op;
      $accounts[] = $account;
    }
135

136 137 138 139 140 141 142 143 144 145 146
    $parameters = array(
      'op' => array_keys($this->type_map),
      'account' => $accounts,
    );

    // Test that the accounts have access to the correspoding page revision permissions.
    $revision = $this->node_revisions['page'][1];

    $permutations = $this->generatePermutations($parameters);
    foreach ($permutations as $case) {
      // Skip this test if there are no revisions for the node.
147
      if (!($revision->isDefaultRevision() && (db_query('SELECT COUNT(vid) FROM {node_field_revision} WHERE nid = :nid', array(':nid' => $revision->nid))->fetchField() == 1 || $case['op'] == 'update' || $case['op'] == 'delete'))) {
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
        if (!empty($case['account']->is_admin) || user_access($this->type_map[$case['op']], $case['account'])) {
          $this->assertTrue(_node_revision_access($revision, $case['op'], $case['account']), "{$this->type_map[$case['op']]} granted.");
        }
        else {
          $this->assertFalse(_node_revision_access($revision, $case['op'], $case['account']), "{$this->type_map[$case['op']]} not granted.");
        }
      }
    }

    // Test that the accounts have no access to the article revisions.
    $revision = $this->node_revisions['article'][1];

    foreach ($permutations as $case) {
      $this->assertFalse(_node_revision_access($revision, $case['op'], $case['account']), "{$this->type_map[$case['op']]} did not grant revision permission for articles.");
    }
163 164
  }
}