publishcontent.test in Publish Content 5.2
Same filename and directory in other branches
Unit tests for Publish Content module. prerequesite: make sure that 'authenticated user' does not have any access like 'publish [content type] content' or 'unpublish [content type] content'
@note: We test to ensure we are not messing up with the default Drupal access for view node i.e. a owner of a node can view it even if unpublished.
File
tests/publishcontent.testView source
<?php
/**
* @file
* Unit tests for Publish Content module.
* prerequesite: make sure that 'authenticated user' does not have any access like
* 'publish [content type] content' or 'unpublish [content type] content'
*
* @note: We test to ensure we are not messing up with the default Drupal access for view node
* i.e. a owner of a node can view it even if unpublished.
*/
class PublishContentWebCaseTest extends DrupalTestCase {
/**
* Drupal SimpleTest method: return metadata about the test.
*/
function get_info() {
return array(
'name' => t('Publish Content: access control'),
'desc' => t('Executes test suite for Publish Content module.'),
'group' => t('Publish Content module'),
);
}
function setUp() {
parent::setUp();
$this
->drupalModuleEnable('publishcontent');
}
function publishcontent_do_operation($nid, $op, $expected_status, $msg = NULL) {
$this
->drupalGet(url("node/{$nid}/{$op}", NULL, NULL, TRUE));
$node = node_load($nid, NULL, TRUE);
$this
->assertEqual($node->status, $expected_status, $msg);
}
function assert_access_node($node, $msg = NULL) {
$this
->drupalGet(url('node/' . $node->nid, NULL, NULL, TRUE));
$this
->assertResponse(200);
$this
->assertTitle($node->title . ' | ' . variable_get('site_name', 'Drupal'), $msg);
}
function assert_access_denied($url, $msg = NULL) {
$this
->drupalGet(url($url, NULL, NULL, TRUE));
$this
->assertResponse(403);
$this
->assertText('Access denied' . ' | ' . variable_get('site_name', 'Drupal'), $msg);
}
function assert_node_status($nid, $status, $msg = 'node status mismatches') {
$result = node_load($nid, NULL, TRUE);
$this
->assertEqual($result->status, $status, $msg);
}
function set_node_status(&$node, $status, $msg = 'unable to set correct node status') {
$node->status = $status;
node_save($node);
$this
->assert_node_status($node->nid, $status, $msg);
}
function assert_current_user_cannot_publish_node(&$node) {
$this
->assertEqual($node->status, 1, 'pre-requesite: status MUST be 1');
$this
->assert_access_denied("node/{$node->nid}/publish", "no publish permission --> access denied");
$this
->assert_node_status($node->nid, 1, 'node should be still published');
$this
->assert_access_node($node, 'node MUST BE viewable');
$this
->set_node_status($node, 0);
$this
->publishcontent_do_operation($node->nid, 'unpublish', 0, 'node should still be unpublished');
$this
->set_node_status($node, 1, 'post-requesite: status MUST be 1');
}
function assert_current_user_cannot_unpublish_node(&$node) {
$this
->assertEqual($node->status, 1, 'pre-requesite: status MUST be 1');
$this
->publishcontent_do_operation($node->nid, 'unpublish', 1, 'node should still be published');
$this
->set_node_status($node, 0);
$this
->publishcontent_do_operation($node->nid, 'unpublish', 0, 'node should still be published');
$this
->set_node_status($node, 1, 'post-requesite: status MUST be 1');
}
function assert_current_user_can_publish_node(&$node) {
$this
->assertEqual($node->status, 1, 'pre-requesite: status MUST be 1');
$this
->publishcontent_do_operation($node->nid, 'publish', 1, 'node should be still published');
$this
->assert_access_node($node, 'node MUST BE viewable');
$this
->set_node_status($node, 0);
$this
->assert_access_node($node, 'node MUST BE viewable even if unpublished');
$this
->publishcontent_do_operation($node->nid, 'publish', 1, 'node should be now published');
$this
->assertText(_publishcontent_get_message($node->nid, $node->title, TRUE), 'drupal_set_message not working for publish.');
$this
->set_node_status($node, 1, 'post-requesite: status MUST be 1');
}
function assert_current_user_can_unpublish_node(&$node) {
$this
->assertEqual($node->status, 1, 'pre-requesite: status MUST be 1');
$this
->publishcontent_do_operation($node->nid, 'unpublish', 0, 'node should be published');
$this
->assertText(_publishcontent_get_message($node->nid, $node->title, FALSE), 'drupal_set_message not working for unpublish.');
$this
->assert_access_node($node, 'node MUST BE viewable even if unpublished');
$this
->publishcontent_do_operation($node->nid, 'unpublish', 0, 'node should be still unpublished');
$this
->set_node_status($node, 1, 'post-requesite: status MUST be 1');
}
/**
* Test the access for the owner of a node without the permission to
* publish or unpublish.
*
* @note: node's owner can see it even if unpublished by default in Drupal
*/
function testNoPermissionByOwner() {
// Prepare a user to do the stuff
$web_user = $this
->drupalCreateUserRolePerm();
$this
->drupalLoginUser($web_user);
$node = $this
->drupalCreateNode(array(
'type' => 'page',
'uid' => $web_user->uid,
'status' => 1,
));
$this
->assert_current_user_cannot_publish_node($node);
$this
->assert_current_user_cannot_unpublish_node($node);
$this
->set_node_status($node, 0);
$this
->assert_access_node($node, 'node MUST BE viewable if unpublished');
}
function testNoPermissionAndNotOwner() {
$node = $this
->drupalCreateNode(array(
'type' => 'page',
'uid' => 0,
'status' => 1,
));
$this
->drupalLoginUser($this
->drupalCreateUserRolePerm());
$this
->assert_current_user_cannot_publish_node($node);
$this
->assert_current_user_cannot_unpublish_node($node);
}
function testDoPublishByNodeOwner() {
$type = 'page';
$web_user = $this
->drupalCreateUserRolePerm(array(
' publish ' . $type . ' content',
'access content',
));
$this
->drupalLoginUser($web_user);
$node = $this
->drupalCreateNode(array(
'type' => $type,
'uid' => $web_user->uid,
'status' => 1,
));
$this
->assert_current_user_can_publish_node($node);
$this
->assert_current_user_cannot_unpublish_node($node);
}
function testDoUnpublishByNodeOwner() {
$type = 'page';
$web_user = $this
->drupalCreateUserRolePerm(array(
'unpublish ' . $type . ' content',
'access content',
));
$this
->drupalLoginUser($web_user);
$node = $this
->drupalCreateNode(array(
'type' => $type,
'uid' => $web_user->uid,
'status' => 1,
));
$this
->assert_current_user_cannot_publish_node($node);
$this
->assert_current_user_can_unpublish_node($node);
}
function testDoPublishNotByNodeOwner($perm = ' publish *all* content', $type = 'page') {
$node = $this
->drupalCreateNode(array(
'type' => $type,
'uid' => 0,
'status' => 1,
));
$this
->drupalLoginUser($this
->drupalCreateUserRolePerm(array(
'access content',
$perm,
)));
$this
->assert_current_user_can_publish_node($node);
$this
->assert_current_user_cannot_unpublish_node($node);
}
function testDoPublishNotByNodeOwner_permNodeType() {
$this
->testDoPublishNotByNodeOwner(' publish page content', 'page');
}
function testDoUnpublishNotByNodeOwner($perm = 'unpublish *all* content', $type = 'page') {
$node = $this
->drupalCreateNode(array(
'type' => $type,
'uid' => 0,
'status' => 1,
));
$this
->drupalLoginUser($this
->drupalCreateUserRolePerm(array(
'access content',
$perm,
)));
$this
->assert_current_user_cannot_publish_node($node);
$this
->assert_current_user_can_unpublish_node($node);
}
function testDoUnpublishNotByNodeOwner_permNodeType() {
$this
->testDoUnpublishNotByNodeOwner('unpublish page content', 'page');
}
/**
* HACK: taken from simpletest for Drupal 6
* Creates a node based on default settings.
*
* @param $settings
* An associative array of settings to change from the defaults, keys are
* node properties, for example 'body' => 'Hello, world!'.
* @return object Created node object.
*/
function drupalCreateNode($settings = array()) {
// Populate defaults array
$defaults = array(
'body' => $this
->randomName(32),
'title' => $this
->randomName(8),
'comment' => 2,
'changed' => time(),
'format' => FILTER_FORMAT_DEFAULT,
'moderate' => 0,
'promote' => 0,
'revision' => 1,
'log' => '',
'status' => 1,
'sticky' => 0,
'type' => 'page',
'revisions' => NULL,
'taxonomy' => NULL,
);
$defaults['teaser'] = $defaults['body'];
// If we already have a node, we use the original node's created time, and this
if (isset($defaults['created'])) {
$defaults['date'] = format_date($defaults['created'], 'custom', 'Y-m-d H:i:s O');
}
if (empty($settings['uid'])) {
global $user;
$defaults['uid'] = $user->uid;
}
$node = $settings + $defaults;
$node = (object) $node;
node_save($node);
// small hack to link revisions to our test user
db_query('UPDATE {node_revisions} SET uid = %d WHERE vid = %d', $node->uid, $node->vid);
return $node;
}
}
Classes
Name | Description |
---|---|
PublishContentWebCaseTest | @file Unit tests for Publish Content module. prerequesite: make sure that 'authenticated user' does not have any access like 'publish [content type] content' or 'unpublish [content type] content' |