You are here

node_view_permissions.test in Node View Permissions 7

File

tests/node_view_permissions.test
View source
<?php

/**
 * Tests for Node View Permissions.
 *
 * @group node_view_permissions
 */
class NodeViewPermissionsTest extends DrupalWebTestCase {

  /**
   * Constants for the HTTP response codes.
   */
  const HTTP_FORBIDDEN = 403;
  const HTTP_OK = 200;

  /**
   * Implements getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Node View Permissions',
      'description' => 'Tests permissions for viewing nodes.',
      'group' => 'Node View Permissions',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp(array(
      'node_view_permissions',
    ));

    // Ensure there are no more foreach() errors after module installation.
    $this
      ->drupalGet('<front>');
    $this
      ->assertResponse(self::HTTP_OK);
    variable_set('node_view_permissions_article', TRUE);
    $this
      ->refreshVariables();
    $this
      ->assertEqual(array(
      'article',
    ), node_view_permissions_get_configured_types());
    $this
      ->checkPermissions(array(
      'view own article content',
    ), TRUE);
    node_access_rebuild();
  }

  /**
   * Test users with a "view own content" permission.
   *
   * Ensure that these users can view nodes of this type that they created.
   *
   * @throws \Behat\Mink\Exception\ExpectationException
   */
  public function testViewOwn() {
    $user1 = $this
      ->drupalCreateUser(array(
      'view own article content',
    ));
    $user2 = $this
      ->drupalCreateUser(array(
      'view own article content',
    ));
    $node = $this
      ->drupalCreateNode([
      'type' => 'article',
      'uid' => $user1->uid,
    ]);
    $lookup = [
      [
        $user1,
        self::HTTP_OK,
      ],
      [
        $user2,
        self::HTTP_FORBIDDEN,
      ],
    ];
    foreach ($lookup as $i) {
      list($user, $expected) = $i;
      $this
        ->drupalLogin($user);
      $this
        ->drupalGet("node/{$node->nid}");
      $this
        ->assertResponse($expected);
    }
  }

  /**
   * Test users with a "view any content" permission.
   *
   * Ensure that these users can view any node of this type, including ones
   * that they did not create.
   *
   * @throws \Behat\Mink\Exception\ExpectationException
   */
  public function testViewAny() {
    $user1 = $this
      ->drupalCreateUser(array(
      'view any article content',
    ));
    $user2 = $this
      ->drupalCreateUser(array(
      'view any article content',
    ));
    $node = $this
      ->drupalCreateNode([
      'type' => 'article',
      'uid' => $user1->uid,
    ]);
    foreach ([
      $user1,
      $user2,
    ] as $user) {
      $this
        ->drupalLogin($user);
      $this
        ->drupalGet("node/{$node->nid}");
      $this
        ->assertResponse(self::HTTP_OK);
    }
  }

}

Classes

Namesort descending Description
NodeViewPermissionsTest Tests for Node View Permissions.