You are here

public function ProtectedNodeRules::testPasswordProtectNoValueGiven in Protected Node 1.0.x

Same name and namespace in other branches
  1. 7 protected_node_rules/tests/protected_node_rules.test \ProtectedNodeRules::testPasswordProtectNoValueGiven()

Test function.

Test that a rule can password protect a node with a random password.

File

protected_node_rules/tests/protected_node_rules.test, line 101
Test protected node rules functionality.

Class

ProtectedNodeRules
Test protected node rules functionality.

Code

public function testPasswordProtectNoValueGiven() {

  // Create an unprotected basic page.
  $node = $this
    ->drupalCreateNode();

  // Make sure it is viewable.
  $this
    ->drupalLogin($this->normalNonAccessAllowedUser);
  $this
    ->drupalGet('node/' . $node->nid);
  $text = $node->body[LANGUAGE_NONE][0]['value'];
  $this
    ->assertText($text, "User with no access permission can access an unprotected node");

  // Create a rule to password protect node on save if the node has no
  // password.
  $this
    ->drupalLogin($this->adminUser);
  $rule = rules_reaction_rule();
  $condition = rules_condition('protected_node_rules_condition_content_has_password')
    ->negate();
  $rule->label = t('Password protect page on presave.');
  $rule
    ->event('node_presave')
    ->condition($condition)
    ->action('protected_node_rules_action_password_protect', array(
    'node:select' => 'node',
    'passwd' => '',
    'show_title' => 1,
  ));
  $rule
    ->save();

  // Save the node so it is now protected.
  node_save($node);

  // Check the generated password.
  $password = $node->protected_node_clear_passwd;
  $this
    ->assertTrue(strlen($password) == 10, "Generated password is 10 characters.", $this->group);

  // Now the view content user can't see 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");

  // User that can access protected node page tries to do so.
  $this
    ->drupalLogin($this->normalAccessAllowedUser);
  $form = array(
    'password' => $password,
  );
  $this
    ->drupalPost('node/' . $node->nid, $form, t('OK'));
  $this
    ->assertText($text, "User with right permission can access a protected node with right password", $this->group);

  // Resave the node to ensure that the password has not been erased with
  // another generated password.
  node_save($node);

  // The view content user still can't see 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");

  // User that can access protected node page tries to do so.
  $this
    ->drupalLogin($this->normalAccessAllowedUser2);
  $form = array(
    'password' => $password,
  );
  $this
    ->drupalPost('node/' . $node->nid, $form, t('OK'));
  $this
    ->assertText($text, "User with right permission can access a protected node with right password", $this->group);
}