Commit 5fc51d33 authored by catch's avatar catch

Issue #1592966 by aspilicious: Convert node tests to PSR-0.

parent 1445dab9
<?php
/**
* @file
* Definition of Drupal\node\Tests\MultiStepNodeFormBasicOptionsTest.
*/
namespace Drupal\node\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Test multistep node forms basic options.
*/
class MultiStepNodeFormBasicOptionsTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Multistep node form basic options',
'description' => 'Test the persistence of basic options through multiple steps.',
'group' => 'Node',
);
}
function setUp() {
parent::setUp('poll');
$web_user = $this->drupalCreateUser(array('administer nodes', 'create poll content'));
$this->drupalLogin($web_user);
}
/**
* Change the default values of basic options to ensure they persist.
*/
function testMultiStepNodeFormBasicOptions() {
$edit = array(
'title' => 'a',
'status' => FALSE,
'promote' => FALSE,
'sticky' => 1,
'choice[new:0][chtext]' => 'a',
'choice[new:1][chtext]' => 'a',
);
$this->drupalPost('node/add/poll', $edit, t('Add another choice'));
$this->assertNoFieldChecked('edit-status', 'status stayed unchecked');
$this->assertNoFieldChecked('edit-promote', 'promote stayed unchecked');
$this->assertFieldChecked('edit-sticky', 'sticky stayed checked');
}
}
<?php
/**
* @file
* Definition of Drupal\node\Tests\NodeAccessBaseTableTest.
*/
namespace Drupal\node\Tests;
/**
* Tests for Node Access with a non-node base table.
*/
class NodeAccessBaseTableTest extends NodeTestBase {
// Requires tags taxonomy field.
protected $profile = 'standard';
public static function getInfo() {
return array(
'name' => 'Node access on any table',
'description' => 'Checks behavior of the node access subsystem if the base table is not node.',
'group' => 'Node',
);
}
/**
* Enable modules and create user with specific permissions.
*/
public function setUp() {
parent::setUp('node_access_test');
node_access_rebuild();
variable_set('node_access_test_private', TRUE);
}
/**
* Test the "private" node access.
*
* - Create 2 users with "access content" and "create article" permissions.
* - Each user creates one private and one not private article.
*
* - Test that each user can view the other user's non-private article.
* - Test that each user cannot view the other user's private article.
* - Test that each user finds only appropriate (non-private + own private)
* in taxonomy listing.
* - Create another user with 'view any private content'.
* - Test that user 4 can view all content created above.
* - Test that user 4 can view all content on taxonomy listing.
*/
function testNodeAccessBasic() {
$num_simple_users = 2;
$simple_users = array();
// nodes keyed by uid and nid: $nodes[$uid][$nid] = $is_private;
$this->nodesByUser = array();
$titles = array(); // Titles keyed by nid
$private_nodes = array(); // Array of nids marked private.
for ($i = 0; $i < $num_simple_users; $i++) {
$simple_users[$i] = $this->drupalCreateUser(array('access content', 'create article content'));
}
foreach ($simple_users as $this->webUser) {
$this->drupalLogin($this->webUser);
foreach (array(0 => 'Public', 1 => 'Private') as $is_private => $type) {
$edit = array(
'title' => t('@private_public Article created by @user', array('@private_public' => $type, '@user' => $this->webUser->name)),
);
if ($is_private) {
$edit['private'] = TRUE;
$edit['body[und][0][value]'] = 'private node';
$edit['field_tags[und]'] = 'private';
}
else {
$edit['body[und][0][value]'] = 'public node';
$edit['field_tags[und]'] = 'public';
}
$this->drupalPost('node/add/article', $edit, t('Save'));
$nid = db_query('SELECT nid FROM {node} WHERE title = :title', array(':title' => $edit['title']))->fetchField();
$private_status = db_query('SELECT private FROM {node_access_test} where nid = :nid', array(':nid' => $nid))->fetchField();
$this->assertTrue($is_private == $private_status, t('The private status of the node was properly set in the node_access_test table.'));
if ($is_private) {
$private_nodes[] = $nid;
}
$titles[$nid] = $edit['title'];
$this->nodesByUser[$this->webUser->uid][$nid] = $is_private;
}
}
$this->publicTid = db_query('SELECT tid FROM {taxonomy_term_data} WHERE name = :name', array(':name' => 'public'))->fetchField();
$this->privateTid = db_query('SELECT tid FROM {taxonomy_term_data} WHERE name = :name', array(':name' => 'private'))->fetchField();
$this->assertTrue($this->publicTid, t('Public tid was found'));
$this->assertTrue($this->privateTid, t('Private tid was found'));
foreach ($simple_users as $this->webUser) {
$this->drupalLogin($this->webUser);
// Check own nodes to see that all are readable.
foreach ($this->nodesByUser as $uid => $data) {
foreach ($data as $nid => $is_private) {
$this->drupalGet('node/' . $nid);
if ($is_private) {
$should_be_visible = $uid == $this->webUser->uid;
}
else {
$should_be_visible = TRUE;
}
$this->assertResponse($should_be_visible ? 200 : 403, strtr('A %private node by user %uid is %visible for user %current_uid.', array(
'%private' => $is_private ? 'private' : 'public',
'%uid' => $uid,
'%visible' => $should_be_visible ? 'visible' : 'not visible',
'%current_uid' => $this->webUser->uid,
)));
}
}
// Check to see that the correct nodes are shown on taxonomy/private
// and taxonomy/public.
$this->assertTaxonomyPage(FALSE);
}
// Now test that a user with 'access any private content' can view content.
$access_user = $this->drupalCreateUser(array('access content', 'create article content', 'node test view', 'search content'));
$this->drupalLogin($access_user);
foreach ($this->nodesByUser as $uid => $private_status) {
foreach ($private_status as $nid => $is_private) {
$this->drupalGet('node/' . $nid);
$this->assertResponse(200);
}
}
// This user should be able to see all of the nodes on the relevant
// taxonomy pages.
$this->assertTaxonomyPage(TRUE);
}
/**
* Checks taxonomy/term listings to ensure only accessible nodes are listed.
*
* @param $is_admin
* A boolean indicating whether the current user is an administrator. If
* TRUE, all nodes should be listed. If FALSE, only public nodes and the
* user's own private nodes should be listed.
*/
protected function assertTaxonomyPage($is_admin) {
foreach (array($this->publicTid, $this->privateTid) as $tid_is_private => $tid) {
$this->drupalGet("taxonomy/term/$tid");
$this->nids_visible = array();
foreach ($this->xpath("//a[text()='Read more']") as $link) {
$this->assertTrue(preg_match('|node/(\d+)$|', (string) $link['href'], $matches), 'Read more points to a node');
$this->nids_visible[$matches[1]] = TRUE;
}
foreach ($this->nodesByUser as $uid => $data) {
foreach ($data as $nid => $is_private) {
// Private nodes should be visible on the private term page,
// public nodes should be visible on the public term page.
$should_be_visible = $tid_is_private == $is_private;
// Non-administrators can only see their own nodes on the private
// term page.
if (!$is_admin && $tid_is_private) {
$should_be_visible = $should_be_visible && $uid == $this->webUser->uid;
}
$this->assertIdentical(isset($this->nids_visible[$nid]), $should_be_visible, strtr('A %private node by user %uid is %visible for user %current_uid on the %tid_is_private page.', array(
'%private' => $is_private ? 'private' : 'public',
'%uid' => $uid,
'%visible' => isset($this->nids_visible[$nid]) ? 'visible' : 'not visible',
'%current_uid' => $this->webUser->uid,
'%tid_is_private' => $tid_is_private ? 'private' : 'public',
)));
}
}
}
}
}
<?php
/**
* @file
* Definition of Drupal\node\Tests\NodeAccessFieldTest.
*/
namespace Drupal\node\Tests;
/**
* Tests the interaction of the node access system with fields.
*/
class NodeAccessFieldTest extends NodeTestBase {
public static function getInfo() {
return array(
'name' => 'Node access and fields',
'description' => 'Tests the interaction of the node access system with fields.',
'group' => 'Node',
);
}
public function setUp() {
parent::setUp('node_access_test', 'field_ui');
node_access_rebuild();
// Create some users.
$this->admin_user = $this->drupalCreateUser(array('access content', 'bypass node access'));
$this->content_admin_user = $this->drupalCreateUser(array('access content', 'administer content types'));
// Add a custom field to the page content type.
$this->field_name = drupal_strtolower($this->randomName() . '_field_name');
$this->field = field_create_field(array('field_name' => $this->field_name, 'type' => 'text'));
$this->instance = field_create_instance(array(
'field_name' => $this->field_name,
'entity_type' => 'node',
'bundle' => 'page',
));
}
/**
* Tests administering fields when node access is restricted.
*/
function testNodeAccessAdministerField() {
// Create a page node.
$langcode = LANGUAGE_NOT_SPECIFIED;
$field_data = array();
$value = $field_data[$langcode][0]['value'] = $this->randomName();
$node = $this->drupalCreateNode(array($this->field_name => $field_data));
// Log in as the administrator and confirm that the field value is present.
$this->drupalLogin($this->admin_user);
$this->drupalGet("node/{$node->nid}");
$this->assertText($value, 'The saved field value is visible to an administrator.');
// Log in as the content admin and try to view the node.
$this->drupalLogin($this->content_admin_user);
$this->drupalGet("node/{$node->nid}");
$this->assertText('Access denied', 'Access is denied for the content admin.');
// Modify the field default as the content admin.
$edit = array();
$default = 'Sometimes words have two meanings';
$edit["{$this->field_name}[$langcode][0][value]"] = $default;
$this->drupalPost(
"admin/structure/types/manage/page/fields/{$this->field_name}",
$edit,
t('Save settings')
);
// Log in as the administrator.
$this->drupalLogin($this->admin_user);
// Confirm that the existing node still has the correct field value.
$this->drupalGet("node/{$node->nid}");
$this->assertText($value, 'The original field value is visible to an administrator.');
// Confirm that the new default value appears when creating a new node.
$this->drupalGet('node/add/page');
$this->assertRaw($default, 'The updated default value is displayed when creating a new node.');
}
}
<?php
/**
* @file
* Definition of Drupal\node\Tests\NodeAccessPagerTest.
*/
namespace Drupal\node\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests pagination with a node access module enabled.
*/
class NodeAccessPagerTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Node access pagination',
'description' => 'Test access controlled node views have the right amount of comment pages.',
'group' => 'Node',
);
}
public function setUp() {
parent::setUp('node_access_test', 'comment', 'forum');
node_access_rebuild();
$this->web_user = $this->drupalCreateUser(array('access content', 'access comments', 'node test view'));
}
/**
* Tests the comment pager for nodes with multiple grants per realm.
*/
public function testCommentPager() {
// Create a node.
$node = $this->drupalCreateNode();
// Create 60 comments.
for ($i = 0; $i < 60; $i++) {
$comment = entity_create('comment', array(
'nid' => $node->nid,
'subject' => $this->randomName(),
'comment_body' => array(
LANGUAGE_NOT_SPECIFIED => array(
array('value' => $this->randomName()),
),
),
));
$comment->save();
}
$this->drupalLogin($this->web_user);
// View the node page. With the default 50 comments per page there should
// be two pages (0, 1) but no third (2) page.
$this->drupalGet('node/' . $node->nid);
$this->assertText($node->title, t('Node title found.'));
$this->assertText(t('Comments'), t('Has a comments section.'));
$this->assertRaw('page=1', t('Secound page exists.'));
$this->assertNoRaw('page=2', t('No third page exists.'));
}
/**
* Tests the forum node pager for nodes with multiple grants per realm.
*/
public function testForumPager() {
// Lookup the forums vocabulary vid.
$vid = variable_get('forum_nav_vocabulary', 0);
$this->assertTrue($vid, t('Forum navigation vocabulary found.'));
// Lookup the general discussion term.
$tree = taxonomy_get_tree($vid, 0, 1);
$tid = reset($tree)->tid;
$this->assertTrue($tid, t('General discussion term found.'));
// Create 30 nodes.
for ($i = 0; $i < 30; $i++) {
$this->drupalCreateNode(array(
'nid' => NULL,
'type' => 'forum',
'taxonomy_forums' => array(
LANGUAGE_NOT_SPECIFIED => array(
array('tid' => $tid, 'vid' => $vid, 'vocabulary_machine_name' => 'forums'),
),
),
));
}
// View the general discussion forum page. With the default 25 nodes per
// page there should be two pages for 30 nodes, no more.
$this->drupalLogin($this->web_user);
$this->drupalGet('forum/' . $tid);
$this->assertRaw('page=1', t('Secound page exists.'));
$this->assertNoRaw('page=2', t('No third page exists.'));
}
}
<?php
/**
* @file
* Definition of Drupal\node\Tests\NodeAccessRebuildTest.
*/
namespace Drupal\node\Tests;
/**
* Rebuild the node_access table.
*/
class NodeAccessRebuildTest extends NodeTestBase {
public static function getInfo() {
return array(
'name' => 'Node access rebuild',
'description' => 'Ensures that node access rebuild functions work correctly.',
'group' => 'Node',
);
}
function setUp() {
parent::setUp();
$web_user = $this->drupalCreateUser(array('administer site configuration', 'access administration pages', 'access site reports'));
$this->drupalLogin($web_user);
$this->web_user = $web_user;
}
function testNodeAccessRebuild() {
$this->drupalGet('admin/reports/status');
$this->clickLink(t('Rebuild permissions'));
$this->drupalPost(NULL, array(), t('Rebuild permissions'));
$this->assertText(t('Content permissions have been rebuilt.'));
}
}
<?php
/**
* @file
* Definition of Drupal\node\Tests\NodeAccessRecordsTest.
*/
namespace Drupal\node\Tests;
/**
* Test case to verify hook_node_access_records functionality.
*/
class NodeAccessRecordsTest extends NodeTestBase {
public static function getInfo() {
return array(
'name' => 'Node access records',
'description' => 'Test hook_node_access_records when acquiring grants.',
'group' => 'Node',
);
}
function setUp() {
// Enable dummy module that implements hook_node_grants(),
// hook_node_access_records(), hook_node_grants_alter() and
// hook_node_access_records_alter().
parent::setUp('node_test');
}
/**
* Create a node and test the creation of node access rules.
*/
function testNodeAccessRecords() {
// Create an article node.
$node1 = $this->drupalCreateNode(array('type' => 'article'));
$this->assertTrue(node_load($node1->nid), t('Article node created.'));
// Check to see if grants added by node_test_node_access_records made it in.
$records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(':nid' => $node1->nid))->fetchAll();
$this->assertEqual(count($records), 1, t('Returned the correct number of rows.'));
$this->assertEqual($records[0]->realm, 'test_article_realm', t('Grant with article_realm acquired for node without alteration.'));
$this->assertEqual($records[0]->gid, 1, t('Grant with gid = 1 acquired for node without alteration.'));
// Create an unpromoted "Basic page" node.
$node2 = $this->drupalCreateNode(array('type' => 'page', 'promote' => 0));
$this->assertTrue(node_load($node2->nid), t('Unpromoted basic page node created.'));
// Check to see if grants added by node_test_node_access_records made it in.
$records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(':nid' => $node2->nid))->fetchAll();
$this->assertEqual(count($records), 1, t('Returned the correct number of rows.'));
$this->assertEqual($records[0]->realm, 'test_page_realm', t('Grant with page_realm acquired for node without alteration.'));
$this->assertEqual($records[0]->gid, 1, t('Grant with gid = 1 acquired for node without alteration.'));
// Create an unpromoted, unpublished "Basic page" node.
$node3 = $this->drupalCreateNode(array('type' => 'page', 'promote' => 0, 'status' => 0));
$this->assertTrue(node_load($node3->nid), t('Unpromoted, unpublished basic page node created.'));
// Check to see if grants added by node_test_node_access_records made it in.
$records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(':nid' => $node3->nid))->fetchAll();
$this->assertEqual(count($records), 1, t('Returned the correct number of rows.'));
$this->assertEqual($records[0]->realm, 'test_page_realm', t('Grant with page_realm acquired for node without alteration.'));
$this->assertEqual($records[0]->gid, 1, t('Grant with gid = 1 acquired for node without alteration.'));
// Create a promoted "Basic page" node.
$node4 = $this->drupalCreateNode(array('type' => 'page', 'promote' => 1));
$this->assertTrue(node_load($node4->nid), t('Promoted basic page node created.'));
// Check to see if grant added by node_test_node_access_records was altered
// by node_test_node_access_records_alter.
$records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(':nid' => $node4->nid))->fetchAll();
$this->assertEqual(count($records), 1, t('Returned the correct number of rows.'));
$this->assertEqual($records[0]->realm, 'test_alter_realm', t('Altered grant with alter_realm acquired for node.'));
$this->assertEqual($records[0]->gid, 2, t('Altered grant with gid = 2 acquired for node.'));
// Check to see if we can alter grants with hook_node_grants_alter().
$operations = array('view', 'update', 'delete');
// Create a user that is allowed to access content.
$web_user = $this->drupalCreateUser(array('access content'));
foreach ($operations as $op) {
$grants = node_test_node_grants($op, $web_user);
$altered_grants = $grants;
drupal_alter('node_grants', $altered_grants, $web_user, $op);
$this->assertNotEqual($grants, $altered_grants, t('Altered the %op grant for a user.', array('%op' => $op)));
}
// Check that core does not grant access to an unpublished node when an
// empty $grants array is returned.
$node6 = $this->drupalCreateNode(array('status' => 0, 'disable_node_access' => TRUE));
$records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(':nid' => $node6->nid))->fetchAll();
$this->assertEqual(count($records), 0, t('Returned no records for unpublished node.'));
}
}
<?php
/**
* @file
* Definition of Drupal\node\Tests\NodeAccessTest.
*/
namespace Drupal\node\Tests;
/**
* Test case to verify basic node_access functionality.
* @todo Cover hook_node_access in a separate test class.
* hook_node_access_records is covered in another test class.
*/
class NodeAccessTest extends NodeTestBase {
public static function getInfo() {
return array(
'name' => 'Node access',
'description' => 'Test node_access function',
'group' => 'Node',
);
}
/**
* Asserts node_access correctly grants or denies access.
*/
function assertNodeAccess($ops, $node, $account) {
foreach ($ops as $op => $result) {
$msg = t("node_access returns @result with operation '@op'.", array('@result' => $result ? 'true' : 'false', '@op' => $op));
$this->assertEqual($result, node_access($op, $node, $account), $msg);
}
}
function setUp() {
parent::setUp();
// Clear permissions for authenticated users.
db_delete('role_permission')
->condition('rid', DRUPAL_AUTHENTICATED_RID)
->execute();
}
/**
* Runs basic tests for node_access function.
*/
function testNodeAccess() {
// Ensures user without 'access content' permission can do nothing.
$web_user1 = $this->drupalCreateUser(array('create page content', 'edit any page content', 'delete any page content'));
$node1 = $this->drupalCreateNode(array('type' => 'page'));
$this->assertNodeAccess(array('create' => FALSE), 'page', $web_user1);
$this->assertNodeAccess(array('view' => FALSE, 'update' => FALSE, 'delete' => FALSE), $node1, $web_user1);
// Ensures user with 'bypass node access' permission can do everything.
$web_user2 = $this->drupalCreateUser(array('bypass node access'));
$node2 = $this->drupalCreateNode(array('type' => 'page'));
$this->assertNodeAccess(array('create' => TRUE), 'page', $web_user2);
$this->assertNodeAccess(array('view' => TRUE, 'update' => TRUE, 'delete' => TRUE), $node2, $web_user2);
// User cannot 'view own unpublished content'.
$web_user3 = $this->drupalCreateUser(array('access content'));
$node3 = $this->drupalCreateNode(array('status' => 0, 'uid' => $web_user3->uid));
$this->assertNodeAccess(array('view' => FALSE), $node3, $web_user3);
// User cannot create content without permission.
$this->assertNodeAccess(array('create' => FALSE), 'page', $web_user3);
// User can 'view own unpublished content', but another user cannot.