protected_node.per_node.test in Protected Node 1.0.x
Same filename and directory in other branches
Test protected node per node functionality.
File
tests/protected_node.per_node.testView source
<?php
/**
* @file
* Test protected node per node functionality.
*/
/**
* Configure protected_node to use per node password.
*/
class ProtectedNodePerNodePassword extends ProtectedNodeBaseTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Protected node per node feature',
'description' => "This tests global features in case of per node protection",
'group' => 'Protected Node',
);
}
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
// Log in an Admin.
$this
->drupalLogin($this->adminUser);
// Submit the configuration form.
$protected_node_settings = array(
'protected_node_use_global_password' => PROTECTED_NODE_PER_NODE_PASSWORD,
);
$this
->drupalPost('admin/config/content/protected_node', $protected_node_settings, t('Save configuration'));
}
/**
* Test that the password is well hashed when stored.
*/
public function testHash() {
// Log in as an Admin.
$this
->drupalLogin($this->adminUser);
// Generate random password.
$password = $this
->randomName(10);
// Create a new page node.
$node = $this
->createProtectedNode($password);
// Check the stored pasword in database.
$stored_node_password = db_select('protected_nodes')
->fields('protected_nodes', array(
'protected_node_passwd',
))
->condition('nid', $node->nid)
->execute()
->fetchField();
$hashed_node_password = hash('sha256', $password);
$this
->assertEqual($stored_node_password, $hashed_node_password, "The node's password is stored hashed and the value correspond to the password.", $this->group);
}
/**
* Test function.
*
* Test that a node protected with per node protection can be seen with the
* right password.
*/
public function testAllowedView() {
// Log in as Admin.
$this
->drupalLogin($this->adminUser);
// Generate random password.
$password = $this
->randomName(10);
// Create a new page node.
$node = $this
->createProtectedNode($password);
// Once the node created logout the user.
$this
->drupalLogout();
// An authenticated user sees the node.
$this
->drupalLogin($this->normalAccessAllowedUser);
$form = array(
'password' => $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 node protection can't be seen with the
* wrong password.
*/
public function testAllowedViewWrongPassword() {
// Log in as Admin.
$this
->drupalLogin($this->adminUser);
// Generate random password.
$password = $this
->randomName(10);
// Create a new page node.
$node = $this
->createProtectedNode($password);
// Once the node created logout the user.
$this
->drupalLogout();
// An authenticated user does not see the node.
$this
->drupalLogin($this->normalAccessAllowedUser);
$another_password = $this
->randomName(12);
$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 node protection can't be seen by an
* authenticated but not allowed user.
*/
public function testAuthenticatedNonAllowedView() {
// Log in as Admin.
$this
->drupalLogin($this->adminUser);
// Generate random password.
$password = $this
->randomName(10);
// Create a new page node.
$node = $this
->createProtectedNode($password);
// Once the node created logout the user.
$this
->drupalLogout();
// User that can see published content sees 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.
*
* Please make sure the user has the permission to create the node before
* calling the method.
*
* @param string $password
* A password.
*
* @return object
* A node object.
*/
public function createProtectedNode($password) {
// 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,
'protected_node_passwd[pass1]' => $password,
'protected_node_passwd[pass2]' => $password,
);
$this
->drupalPost('node/add/page', $node_data, t('Save'));
return $this
->drupalGetNodeByTitle($node_title);
}
}
Classes
Name | Description |
---|---|
ProtectedNodePerNodePassword | Configure protected_node to use per node password. |