class PublishContentWebCaseTest in Publish Content 5.2
Same name and namespace in other branches
- 6 tests/publishcontent.test \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'
@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.
Hierarchy
- class \DrupalTestCase extends \WebTestCase
- class \PublishContentWebCaseTest
Expanded class hierarchy of PublishContentWebCaseTest
File
- tests/
publishcontent.test, line 12 - 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'
View source
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DrupalTestCase:: |
property | |||
DrupalTestCase:: |
property | |||
DrupalTestCase:: |
property | |||
DrupalTestCase:: |
property | |||
DrupalTestCase:: |
property | |||
DrupalTestCase:: |
function | Will trigger a pass if both parameters refer to different objects. Fail otherwise. | ||
DrupalTestCase:: |
function | Will trigger a pass if the two parameters have the same value only. Otherwise a fail. | ||
DrupalTestCase:: |
function | Confirms that an error has occurred and optionally that the error text matches exactly. | ||
DrupalTestCase:: |
function | Confirms that an error has occurred and that the error text matches a Perl regular expression. | ||
DrupalTestCase:: |
function | Will trigger a pass if the two parameters have the same value and same type. Otherwise a fail. | ||
DrupalTestCase:: |
function | Type and class test. Will pass if class matches the type name or is a subclass or if not an object, but the type is correct. | ||
DrupalTestCase:: |
function | Confirms that no errors have occurred so far in the test method. | ||
DrupalTestCase:: |
function | Type and class mismatch test. Will pass if class name or underling type does not match the one specified. | ||
DrupalTestCase:: |
function | Will trigger a pass if the two parameters have a different value. Otherwise a fail. | ||
DrupalTestCase:: |
function | Will trigger a pass if the two parameters have the different value or different type. | ||
DrupalTestCase:: |
function | Will be true if the value is set. | ||
DrupalTestCase:: |
function | Will trigger a pass if the Perl regex pattern is not present in subject. Fail if found. | ||
DrupalTestCase:: |
function | Will trigger a pass if the raw text is NOT found on the loaded page Fail otherwise. | ||
DrupalTestCase:: |
function | Will be true if the value is null. | ||
DrupalTestCase:: |
function | Will trigger a pass if both parameters refer to the same object. Fail otherwise. | ||
DrupalTestCase:: |
function | Will trigger a pass if the Perl regex pattern is found in the subject. Fail otherwise. | ||
DrupalTestCase:: |
function | Will trigger a pass if the raw text is found on the loaded page Fail otherwise. | ||
DrupalTestCase:: |
function | Follows a link by name. Will click the first link found with this link text by default, or a later one if an index is given. Match is case insensitive with normalised space. Does make assertations if the click was sucessful or not and it does… | ||
DrupalTestCase:: |
function | @abstract Checks to see if we need to send a http-auth header to authenticate when browsing a site. | ||
DrupalTestCase:: |
function | Create a role / perm combination specified by permissions | ||
DrupalTestCase:: |
function | Creates a user / role / permissions combination specified by permissions | ||
DrupalTestCase:: |
function | @abstract Brokder for the get function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com> | ||
DrupalTestCase:: |
function | @TODO: needs documentation | ||
DrupalTestCase:: |
function | Logs in a user with the internal browser | ||
DrupalTestCase:: |
function | Disables a drupal module | ||
DrupalTestCase:: |
function | Enables a drupal module | ||
DrupalTestCase:: |
function | Do a post request on a drupal page. It will be done as usual post request with SimpleBrowser | ||
DrupalTestCase:: |
function | @abstract Broker for the post function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com> | ||
DrupalTestCase:: |
function | |||
DrupalTestCase:: |
function | Set a druapl variable and keep track of the changes for tearDown() | ||
DrupalTestCase:: |
function | Generates a random string, to be used as name or whatever | ||
DrupalTestCase:: |
function | Just some info for the reporter | ||
DrupalTestCase:: |
function | tearDown implementation, setting back switched modules etc | 1 | |
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | HACK: taken from simpletest for Drupal 6 Creates a node based on default settings. | ||
PublishContentWebCaseTest:: |
function | Drupal SimpleTest method: return metadata about the test. | ||
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | |||
PublishContentWebCaseTest:: |
function | Test the access for the owner of a node without the permission to publish or unpublish. |