You are here

protected_node.per_type.test in Protected Node 7

Same filename and directory in other branches
  1. 1.0.x tests/protected_node.per_type.test

Test protected node per type functionality.

File

tests/protected_node.per_type.test
View source
<?php

/**
 * @file
 * Test protected node per type functionality.
 */

/**
 * Configure protected_node to use per type password.
 */
class ProtectedNodePerTypePassword extends ProtectedNodeBaseTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Protected node per type feature',
      'description' => "This tests global features in case of per type protection",
      'group' => 'Protected Node',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    // Log in an Admin.
    $this
      ->drupalLogin($this->adminUser);

    // Configure protected node.
    // Generate random password for form validation. This password is not used.
    $another_password = $this
      ->randomName(12);

    // Submit the configuration form.
    $protected_node_settings = array(
      'protected_node_use_global_password' => PROTECTED_NODE_GLOBAL_PASSWORD,
      'protected_node_global_password_field[pass1]' => $another_password,
      'protected_node_global_password_field[pass2]' => $another_password,
    );
    $this
      ->drupalPost('admin/config/content/protected_node', $protected_node_settings, t('Save configuration'));

    // Configure page content type.
    // Generate random password.
    $this->page_content_type_password = $this
      ->randomName(10);

    // Submit the configuration form.
    $protected_node_settings = array(
      'protected_node_node_type_password_field[pass1]' => $this->page_content_type_password,
      'protected_node_node_type_password_field[pass2]' => $this->page_content_type_password,
    );
    $this
      ->drupalPost('admin/structure/types/manage/page', $protected_node_settings, t('Save content type'));
  }

  /**
   * Test that the password is well hashed when stored.
   */
  public function testHash() {
    $hashed_page_content_type_password = hash('sha256', $this->page_content_type_password);
    $stored_page_content_type_password = variable_get('protected_node_node_type_password_page');
    $this
      ->assertEqual($stored_page_content_type_password, $hashed_page_content_type_password, "The page content type's password is stored hashed and the value correspond to the password.", $this->group);
  }

  /**
   * Test function.
   *
   * Test that a node protected with per type protection can be seen with the
   * right password.
   */
  public function testAllowedView() {

    // Generate a protected node.
    $node = $this
      ->adminCreateProtectedNode();

    // An authenticated user is viewing the node.
    $this
      ->drupalLogin($this->normalAccessAllowedUser);
    $form = array(
      'password' => $this->page_content_type_password,
    );
    $this
      ->drupalPost('node/' . $node->nid, $form, t('OK'));
    $text = $node->body[LANGUAGE_NONE][0]['value'];
    $this
      ->assertText($text, "User with right permission can access a protected node with right password", $this->group);
  }

  /**
   * Test function.
   *
   * Test that a node protected with per type protection can't be seen with the
   * wrong password.
   */
  public function testAllowedViewWrongPassword() {

    // Generate a protected node.
    $node = $this
      ->adminCreateProtectedNode();

    // An authenticated user is viewing the node.
    $this
      ->drupalLogin($this->normalAccessAllowedUser);
    $another_password = $this
      ->randomName(14);
    $form = array(
      'password' => $another_password,
    );
    $this
      ->drupalPost('node/' . $node->nid, $form, t('OK'));
    $text = $node->body[LANGUAGE_NONE][0]['value'];
    $this
      ->assertNoText($text, "User with right permission can't access a protected node with wrong password", $this->group);
  }

  /**
   * Test function.
   *
   * Test that a node protected with per type protection can't be seen by an
   * authenticated but not allowed user.
   */
  public function testAuthenticatedNonAllowedView() {

    // Generate a protected node.
    $node = $this
      ->adminCreateProtectedNode();

    // User that can see published content is viewing the node.
    $this
      ->drupalLogin($this->normalNonAccessAllowedUser);
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertResponse(403, "User with no access permission is not allowed to access a protected node");
  }

  /**
   * Helper method to create a protected node with the admin user.
   *
   * @return object
   *   A node object.
   */
  public function adminCreateProtectedNode() {

    // Log in as Admin.
    $this
      ->drupalLogin($this->adminUser);

    // Create a new page node.
    $node = $this
      ->createProtectedNode();

    // Once the node created logout the user.
    $this
      ->drupalLogout();
    return $node;
  }

  /**
   * Helper method to create a protected node.
   *
   * Please make sure the user has the permission to create the node before
   * calling the method.
   *
   * @return object
   *   A node object.
   */
  public function createProtectedNode() {

    // Add a new page node that is protected.
    $node_title = $this
      ->randomName(8);
    $node_data = array(
      'title' => $node_title,
      'body[und][0][value]' => $this
        ->randomName(32),
      'protected_node_is_protected' => TRUE,
    );
    $this
      ->drupalPost('node/add/page', $node_data, t('Save'));
    return $this
      ->drupalGetNodeByTitle($node_title);
  }

}

Classes

Namesort descending Description
ProtectedNodePerTypePassword Configure protected_node to use per type password.