Newer
Older
<?php
use Drupal\Core\Database\Database;
Larry Garfield
committed
Dries Buytaert
committed
/**
* @file
* Tests for node.module.
*/
class NodeWebTestCase extends DrupalWebTestCase {
function setUp() {
$modules = func_get_args();
if (isset($modules[0]) && is_array($modules[0])) {
$modules = $modules[0];
}
$modules[] = 'node';
parent::setUp($modules);
// Create Basic page and Article node types.
if ($this->profile != 'standard') {
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
}
}
}
/**
* Tests the node_load_multiple() function.
*/
class NodeLoadMultipleUnitTest extends NodeWebTestCase {
Angie Byron
committed
public static function getInfo() {
return array(
'name' => 'Load multiple nodes',
'description' => 'Test the loading of multiple nodes.',
'group' => 'Node',
);
}
function setUp() {
parent::setUp();
$web_user = $this->drupalCreateUser(array('create article content', 'create page content'));
$this->drupalLogin($web_user);
}
/**
* Creates four nodes and ensures that they are loaded correctly.
*/
function testNodeMultipleLoad() {
$node1 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
$node2 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
$node3 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 0));
$node4 = $this->drupalCreateNode(array('type' => 'page', 'promote' => 0));
// Confirm that promoted nodes appear in the default node listing.
$this->drupalGet('node');
$this->assertText($node1->title, t('Node title appears on the default listing.'));
$this->assertText($node2->title, t('Node title appears on the default listing.'));
$this->assertNoText($node3->title, t('Node title does not appear in the default listing.'));
$this->assertNoText($node4->title, t('Node title does not appear in the default listing.'));
// Load nodes with only a condition. Nodes 3 and 4 will be loaded.
$nodes = node_load_multiple(FALSE, array('promote' => 0));
$this->assertEqual($node3->title, $nodes[$node3->nid]->title, t('Node was loaded.'));
$this->assertEqual($node4->title, $nodes[$node4->nid]->title, t('Node was loaded.'));
$count = count($nodes);
$this->assertTrue($count == 2, t('@count nodes loaded.', array('@count' => $count)));
// Load nodes by nid. Nodes 1, 2 and 4 will be loaded.
$nodes = node_load_multiple(array(1, 2, 4));
$count = count($nodes);
$this->assertTrue(count($nodes) == 3, t('@count nodes loaded', array('@count' => $count)));
$this->assertTrue(isset($nodes[$node1->nid]), t('Node is correctly keyed in the array'));
$this->assertTrue(isset($nodes[$node2->nid]), t('Node is correctly keyed in the array'));
$this->assertTrue(isset($nodes[$node4->nid]), t('Node is correctly keyed in the array'));
foreach ($nodes as $node) {
$this->assertTrue(is_object($node), t('Node is an object'));
}
// Load nodes by nid, where type = article. Nodes 1, 2 and 3 will be loaded.
$nodes = node_load_multiple(array(1, 2, 3, 4), array('type' => 'article'));
$count = count($nodes);
$this->assertTrue($count == 3, t('@count nodes loaded', array('@count' => $count)));
$this->assertEqual($nodes[$node1->nid]->title, $node1->title, t('Node successfully loaded.'));
$this->assertEqual($nodes[$node2->nid]->title, $node2->title, t('Node successfully loaded.'));
$this->assertEqual($nodes[$node3->nid]->title, $node3->title, t('Node successfully loaded.'));
$this->assertFalse(isset($nodes[$node4->nid]));
// Now that all nodes have been loaded into the static cache, ensure that
// they are loaded correctly again when a condition is passed.
$nodes = node_load_multiple(array(1, 2, 3, 4), array('type' => 'article'));
$count = count($nodes);
$this->assertTrue($count == 3, t('@count nodes loaded.', array('@count' => $count)));
$this->assertEqual($nodes[$node1->nid]->title, $node1->title, t('Node successfully loaded'));
$this->assertEqual($nodes[$node2->nid]->title, $node2->title, t('Node successfully loaded'));
$this->assertEqual($nodes[$node3->nid]->title, $node3->title, t('Node successfully loaded'));
$this->assertFalse(isset($nodes[$node4->nid]), t('Node was not loaded'));
// Load nodes by nid, where type = article and promote = 0.
$nodes = node_load_multiple(array(1, 2, 3, 4), array('type' => 'article', 'promote' => 0));
$count = count($nodes);
$this->assertTrue($count == 1, t('@count node loaded', array('@count' => $count)));
$this->assertEqual($nodes[$node3->nid]->title, $node3->title, t('Node successfully loaded.'));
}
}
Dries Buytaert
committed
/**
* Tests for the hooks invoked during node_load().
*/
class NodeLoadHooksTestCase extends NodeWebTestCase {
Dries Buytaert
committed
public static function getInfo() {
return array(
'name' => 'Node load hooks',
'description' => 'Test the hooks invoked when a node is being loaded.',
'group' => 'Node',
);
}
function setUp() {
parent::setUp('node_test');
}
/**
* Tests that hook_node_load() is invoked correctly.
Dries Buytaert
committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
*/
function testHookNodeLoad() {
// Create some sample articles and pages.
$node1 = $this->drupalCreateNode(array('type' => 'article', 'status' => NODE_PUBLISHED));
$node2 = $this->drupalCreateNode(array('type' => 'article', 'status' => NODE_PUBLISHED));
$node3 = $this->drupalCreateNode(array('type' => 'article', 'status' => NODE_NOT_PUBLISHED));
$node4 = $this->drupalCreateNode(array('type' => 'page', 'status' => NODE_NOT_PUBLISHED));
// Check that when a set of nodes that only contains articles is loaded,
// the properties added to the node by node_test_load_node() correctly
// reflect the expected values.
$nodes = node_load_multiple(array(), array('status' => NODE_PUBLISHED));
$loaded_node = end($nodes);
$this->assertEqual($loaded_node->node_test_loaded_nids, array($node1->nid, $node2->nid), t('hook_node_load() received the correct list of node IDs the first time it was called.'));
$this->assertEqual($loaded_node->node_test_loaded_types, array('article'), t('hook_node_load() received the correct list of node types the first time it was called.'));
// Now, as part of the same page request, load a set of nodes that contain
// both articles and pages, and make sure the parameters passed to
// node_test_node_load() are correctly updated.
$nodes = node_load_multiple(array(), array('status' => NODE_NOT_PUBLISHED));
$loaded_node = end($nodes);
$this->assertEqual($loaded_node->node_test_loaded_nids, array($node3->nid, $node4->nid), t('hook_node_load() received the correct list of node IDs the second time it was called.'));
$this->assertEqual($loaded_node->node_test_loaded_types, array('article', 'page'), t('hook_node_load() received the correct list of node types the second time it was called.'));
}
}
class NodeRevisionsTestCase extends NodeWebTestCase {
Dries Buytaert
committed
protected $nodes;
protected $logs;
Angie Byron
committed
public static function getInfo() {
return array(
'name' => 'Node revisions',
'description' => 'Create a node with revisions and test viewing, saving, reverting, and deleting revisions.',
'group' => 'Node',
);
}
Dries Buytaert
committed
function setUp() {
parent::setUp();
Dries Buytaert
committed
// Create and login user.
$web_user = $this->drupalCreateUser(array('view revisions', 'revert revisions', 'edit any page content',
Dries Buytaert
committed
'delete revisions', 'delete any page content'));
$this->drupalLogin($web_user);
Dries Buytaert
committed
// Create initial node.
$node = $this->drupalCreateNode();
$settings = get_object_vars($node);
$settings['revision'] = 1;
Dries Buytaert
committed
$nodes = array();
$logs = array();
Dries Buytaert
committed
// Get original node.
$nodes[] = $node;
Dries Buytaert
committed
// Create three revisions.
$revision_count = 3;
for ($i = 0; $i < $revision_count; $i++) {
$logs[] = $settings['log'] = $this->randomName(32);
// Create revision with random title and body and update variables.
$this->drupalCreateNode($settings);
$node = node_load($node->nid); // Make sure we get revision information.
$settings = get_object_vars($node);
$nodes[] = $node;
}
$this->nodes = $nodes;
$this->logs = $logs;
}
/**
* Check node revision related operations.
*/
Dries Buytaert
committed
function testRevisions() {
$nodes = $this->nodes;
$logs = $this->logs;
Dries Buytaert
committed
// Get last node for simple checks.
$node = $nodes[3];
// Confirm the correct revision text appears on "view revisions" page.
$this->drupalGet("node/$node->nid/revisions/$node->vid/view");
$this->assertText($node->body[LANGUAGE_NOT_SPECIFIED][0]['value'], t('Correct text displays for version.'));
Dries Buytaert
committed
// Confirm the correct log message appears on "revisions overview" page.
$this->drupalGet("node/$node->nid/revisions");
foreach ($logs as $log) {
$this->assertText($log, t('Log message found.'));
Dries Buytaert
committed
}
// Confirm that revisions revert properly.
$this->drupalPost("node/$node->nid/revisions/{$nodes[1]->vid}/revert", array(), t('Revert'));
$this->assertRaw(t('@type %title has been reverted back to the revision from %revision-date.',
Angie Byron
committed
array('@type' => 'Basic page', '%title' => $nodes[1]->title,
'%revision-date' => format_date($nodes[1]->revision_timestamp))), t('Revision reverted.'));
Dries Buytaert
committed
$reverted_node = node_load($node->nid);
$this->assertTrue(($nodes[1]->body[LANGUAGE_NOT_SPECIFIED][0]['value'] == $reverted_node->body[LANGUAGE_NOT_SPECIFIED][0]['value']), t('Node reverted correctly.'));
Dries Buytaert
committed
// Confirm revisions delete properly.
$this->drupalPost("node/$node->nid/revisions/{$nodes[1]->vid}/delete", array(), t('Delete'));
$this->assertRaw(t('Revision from %revision-date of @type %title has been deleted.',
array('%revision-date' => format_date($nodes[1]->revision_timestamp),
'@type' => 'Basic page', '%title' => $nodes[1]->title)), t('Revision deleted.'));
$this->assertTrue(db_query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid and vid = :vid', array(':nid' => $node->nid, ':vid' => $nodes[1]->vid))->fetchField() == 0, t('Revision not found.'));
}
/**
* Checks that revisions are correctly saved without log messages.
*/
function testNodeRevisionWithoutLogMessage() {
// Create a node with an initial log message.
$log = $this->randomName(10);
$node = $this->drupalCreateNode(array('log' => $log));
// Save over the same revision and explicitly provide an empty log message
// (for example, to mimic the case of a node form submitted with no text in
// the "log message" field), and check that the original log message is
// preserved.
$new_title = $this->randomName(10) . 'testNodeRevisionWithoutLogMessage1';
$updated_node = (object) array(
'nid' => $node->nid,
'vid' => $node->vid,
'uid' => $node->uid,
'type' => $node->type,
Angie Byron
committed
'title' => $new_title,
'log' => '',
);
node_save($updated_node);
$this->drupalGet('node/' . $node->nid);
$this->assertText($new_title, t('New node title appears on the page.'));
$node_revision = node_load($node->nid, NULL, TRUE);
$this->assertEqual($node_revision->log, $log, t('After an existing node revision is re-saved without a log message, the original log message is preserved.'));
// Create another node with an initial log message.
$node = $this->drupalCreateNode(array('log' => $log));
// Save a new node revision without providing a log message, and check that
// this revision has an empty log message.
$new_title = $this->randomName(10) . 'testNodeRevisionWithoutLogMessage2';
$updated_node = (object) array(
'nid' => $node->nid,
'vid' => $node->vid,
'uid' => $node->uid,
'type' => $node->type,
Angie Byron
committed
'title' => $new_title,
'revision' => 1,
);
node_save($updated_node);
$this->drupalGet('node/' . $node->nid);
Angie Byron
committed
$this->assertText($new_title, 'New node title appears on the page.');
$node_revision = node_load($node->nid, NULL, TRUE);
Angie Byron
committed
$this->assertTrue(empty($node_revision->log), 'After a new node revision is saved with an empty log message, the log message for the node is empty.');
}
}
class PageEditTestCase extends NodeWebTestCase {
Angie Byron
committed
protected $web_user;
protected $admin_user;
Angie Byron
committed
public static function getInfo() {
return array(
'name' => 'Node edit',
'description' => 'Create a node and test node edit functionality.',
'group' => 'Node',
}
function setUp() {
parent::setUp();
Angie Byron
committed
$this->web_user = $this->drupalCreateUser(array('edit own page content', 'create page content'));
$this->admin_user = $this->drupalCreateUser(array('bypass node access', 'administer nodes'));
/**
* Check node edit functionality.
*/
function testPageEdit() {
Angie Byron
committed
$this->drupalLogin($this->web_user);
$langcode = LANGUAGE_NOT_SPECIFIED;
$title_key = "title";
Angie Byron
committed
$body_key = "body[$langcode][0][value]";
// Create node to edit.
$edit = array();
$edit[$title_key] = $this->randomName(8);
Dries Buytaert
committed
$edit[$body_key] = $this->randomName(16);
$this->drupalPost('node/add/page', $edit, t('Save'));
// Check that the node exists in the database.
$node = $this->drupalGetNodeByTitle($edit[$title_key]);
$this->assertTrue($node, t('Node found in database.'));
// Check that "edit" link points to correct page.
$this->clickLink(t('Edit'));
$edit_url = url("node/$node->nid/edit", array('absolute' => TRUE));
$actual_url = $this->getURL();
$this->assertEqual($edit_url, $actual_url, t('On edit page.'));
// Check that the title and body fields are displayed with the correct values.
$active = '<span class="element-invisible">' . t('(active tab)') . '</span>';
Dries Buytaert
committed
$link_text = t('!local-task-title!active', array('!local-task-title' => t('Edit'), '!active' => $active));
$this->assertText(strip_tags($link_text), 0, t('Edit tab found and marked active.'));
$this->assertFieldByName($title_key, $edit[$title_key], t('Title field displayed.'));
$this->assertFieldByName($body_key, $edit[$body_key], t('Body field displayed.'));
// Edit the content of the node.
$edit = array();
$edit[$title_key] = $this->randomName(8);
Dries Buytaert
committed
$edit[$body_key] = $this->randomName(16);
// Stay on the current page, without reloading.
$this->drupalPost(NULL, $edit, t('Save'));
// Check that the title and body fields are displayed with the updated values.
$this->assertText($edit[$title_key], t('Title displayed.'));
$this->assertText($edit[$body_key], t('Body displayed.'));
// Login as a second administrator user.
$second_web_user = $this->drupalCreateUser(array('administer nodes', 'edit any page content'));
$this->drupalLogin($second_web_user);
// Edit the same node, creating a new revision.
$this->drupalGet("node/$node->nid/edit");
$edit = array();
$edit['title'] = $this->randomName(8);
$edit[$body_key] = $this->randomName(16);
$edit['revision'] = TRUE;
$this->drupalPost(NULL, $edit, t('Save'));
// Ensure that the node revision has been created.
$revised_node = $this->drupalGetNodeByTitle($edit['title']);
$this->assertNotIdentical($node->vid, $revised_node->vid, 'A new revision has been created.');
// Ensure that the node author is preserved when it was not changed in the
// edit form.
$this->assertIdentical($node->uid, $revised_node->uid, 'The node author has been preserved.');
// Ensure that the revision authors are different since the revisions were
// made by different users.
$first_node_version = node_load($node->nid, $node->vid);
$second_node_version = node_load($node->nid, $revised_node->vid);
$this->assertNotIdentical($first_node_version->revision_uid, $second_node_version->revision_uid, 'Each revision has a distinct user.');
}
Angie Byron
committed
/**
* Check changing node authored by fields.
*/
function testPageAuthoredBy() {
$this->drupalLogin($this->admin_user);
// Create node to edit.
$langcode = LANGUAGE_NOT_SPECIFIED;
Angie Byron
committed
$body_key = "body[$langcode][0][value]";
$edit = array();
$edit['title'] = $this->randomName(8);
Angie Byron
committed
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
$edit[$body_key] = $this->randomName(16);
$this->drupalPost('node/add/page', $edit, t('Save'));
// Check that the node was authored by the currently logged in user.
$node = $this->drupalGetNodeByTitle($edit['title']);
$this->assertIdentical($node->uid, $this->admin_user->uid, 'Node authored by admin user.');
// Try to change the 'authored by' field to an invalid user name.
$edit = array(
'name' => 'invalid-name',
);
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$this->assertText('The username invalid-name does not exist.');
// Change the authored by field to an empty string, which should assign
// authorship to the anonymous user (uid 0).
$edit['name'] = '';
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$node = node_load($node->nid, NULL, TRUE);
$this->assertIdentical($node->uid, '0', 'Node authored by anonymous user.');
// Change the authored by field to another user's name (that is not
// logged in).
$edit['name'] = $this->web_user->name;
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$node = node_load($node->nid, NULL, TRUE);
$this->assertIdentical($node->uid, $this->web_user->uid, 'Node authored by normal user.');
// Check that normal users cannot change the authored by information.
$this->drupalLogin($this->web_user);
$this->drupalGet('node/' . $node->nid . '/edit');
$this->assertNoFieldByName('name');
}
}
class PagePreviewTestCase extends NodeWebTestCase {
Angie Byron
committed
public static function getInfo() {
return array(
'name' => 'Node preview',
'description' => 'Test node preview functionality.',
'group' => 'Node',
}
function setUp() {
parent::setUp();
$web_user = $this->drupalCreateUser(array('edit own page content', 'create page content'));
$this->drupalLogin($web_user);
/**
* Check the node preview functionality.
*/
function testPagePreview() {
$langcode = LANGUAGE_NOT_SPECIFIED;
$title_key = "title";
Angie Byron
committed
$body_key = "body[$langcode][0][value]";
Dries Buytaert
committed
// Fill in node creation form and preview node.
$edit = array();
$edit[$title_key] = $this->randomName(8);
Dries Buytaert
committed
$edit[$body_key] = $this->randomName(16);
$this->drupalPost('node/add/page', $edit, t('Preview'));
// Check that the preview is displaying the title and body.
$this->assertTitle(t('Preview | Drupal'), t('Basic page title is preview.'));
$this->assertText($edit[$title_key], t('Title displayed.'));
$this->assertText($edit[$body_key], t('Body displayed.'));
// Check that the title and body fields are displayed with the correct values.
$this->assertFieldByName($title_key, $edit[$title_key], t('Title field displayed.'));
$this->assertFieldByName($body_key, $edit[$body_key], t('Body field displayed.'));
}
/**
* Check the node preview functionality, when using revisions.
*/
function testPagePreviewWithRevisions() {
$langcode = LANGUAGE_NOT_SPECIFIED;
$title_key = "title";
Angie Byron
committed
$body_key = "body[$langcode][0][value]";
Angie Byron
committed
// Force revision on "Basic page" content.
variable_set('node_options_page', array('status', 'revision'));
// Fill in node creation form and preview node.
$edit = array();
$edit[$title_key] = $this->randomName(8);
Dries Buytaert
committed
$edit[$body_key] = $this->randomName(16);
$edit['log'] = $this->randomName(32);
$this->drupalPost('node/add/page', $edit, t('Preview'));
// Check that the preview is displaying the title and body.
$this->assertTitle(t('Preview | Drupal'), t('Basic page title is preview.'));
$this->assertText($edit[$title_key], t('Title displayed.'));
$this->assertText($edit[$body_key], t('Body displayed.'));
// Check that the title and body fields are displayed with the correct values.
$this->assertFieldByName($title_key, $edit[$title_key], t('Title field displayed.'));
$this->assertFieldByName($body_key, $edit[$body_key], t('Body field displayed.'));
// Check that the log field has the correct value.
$this->assertFieldByName('log', $edit['log'], t('Log field displayed.'));
}
}
class NodeCreationTestCase extends NodeWebTestCase {
Angie Byron
committed
public static function getInfo() {
return array(
'name' => 'Node creation',
'description' => 'Create a node and test saving it.',
'group' => 'Node',
);
}
function setUp() {
Dries Buytaert
committed
// Enable dummy module that implements hook_node_insert for exceptions.
parent::setUp(array('node_test_exception', 'dblog'));
$web_user = $this->drupalCreateUser(array('create page content', 'edit own page content'));
$this->drupalLogin($web_user);
Angie Byron
committed
* Create a "Basic page" node and verify its consistency in the database.
function testNodeCreation() {
// Create a node.
$edit = array();
$langcode = LANGUAGE_NOT_SPECIFIED;
$edit["title"] = $this->randomName(8);
Angie Byron
committed
$edit["body[$langcode][0][value]"] = $this->randomName(16);
$this->drupalPost('node/add/page', $edit, t('Save'));
Angie Byron
committed
// Check that the Basic page has been created.
$this->assertRaw(t('!post %title has been created.', array('!post' => 'Basic page', '%title' => $edit["title"])), t('Basic page created.'));
// Check that the node exists in the database.
$node = $this->drupalGetNodeByTitle($edit["title"]);
$this->assertTrue($node, t('Node found in database.'));
}
Angie Byron
committed
/**
* Create a page node and verify that a transaction rolls back the failed creation
*/
function testFailedPageCreation() {
// Create a node.
Dries Buytaert
committed
$edit = array(
'uid' => $this->loggedInUser->uid,
'name' => $this->loggedInUser->name,
'type' => 'page',
'langcode' => LANGUAGE_NOT_SPECIFIED,
Dries Buytaert
committed
'title' => 'testing_transaction_exception',
);
try {
node_save((object) $edit);
$this->fail(t('Expected exception has not been thrown.'));
Dries Buytaert
committed
}
catch (Exception $e) {
$this->pass(t('Expected exception has been thrown.'));
Dries Buytaert
committed
}
Angie Byron
committed
if (Database::getConnection()->supportsTransactions()) {
// Check that the node does not exist in the database.
Dries Buytaert
committed
$node = $this->drupalGetNodeByTitle($edit['title']);
$this->assertFalse($node, t('Transactions supported, and node not found in database.'));
Angie Byron
committed
}
else {
// Check that the node exists in the database.
Dries Buytaert
committed
$node = $this->drupalGetNodeByTitle($edit['title']);
$this->assertTrue($node, t('Transactions not supported, and node found in database.'));
Angie Byron
committed
// Check that the failed rollback was logged.
$records = db_query("SELECT wid FROM {watchdog} WHERE message LIKE 'Explicit rollback failed%'")->fetchAll();
$this->assertTrue(count($records) > 0, t('Transactions not supported, and rollback error logged to watchdog.'));
Angie Byron
committed
}
// Check that the rollback error was logged.
Dries Buytaert
committed
$records = db_query("SELECT wid FROM {watchdog} WHERE variables LIKE '%Test exception for rollback.%'")->fetchAll();
$this->assertTrue(count($records) > 0, t('Rollback explanatory error logged to watchdog.'));
Angie Byron
committed
}
}
class PageViewTestCase extends NodeWebTestCase {
Angie Byron
committed
public static function getInfo() {
return array(
'name' => 'Node edit permissions',
'description' => 'Create a node and test edit permissions.',
'group' => 'Node',
);
}
/**
* Creates a node and then an anonymous and unpermissioned user attempt to edit the node.
*/
function testPageView() {
// Create a node to view.
$node = $this->drupalCreateNode();
$this->assertTrue(node_load($node->nid), t('Node created.'));
// Try to edit with anonymous user.
$html = $this->drupalGet("node/$node->nid/edit");
$this->assertResponse(403);
// Create a user without permission to edit node.
$web_user = $this->drupalCreateUser(array('access content'));
$this->drupalLogin($web_user);
// Attempt to access edit page.
$this->drupalGet("node/$node->nid/edit");
$this->assertResponse(403);
// Create user with permission to edit node.
Angie Byron
committed
$web_user = $this->drupalCreateUser(array('bypass node access'));
$this->drupalLogin($web_user);
// Attempt to access edit page.
$this->drupalGet("node/$node->nid/edit");
$this->assertResponse(200);
}
}
class SummaryLengthTestCase extends NodeWebTestCase {
public static function getInfo() {
return array(
'name' => 'Summary length',
'description' => 'Test summary length.',
'group' => 'Node',
);
}
/**
* Creates a node and then an anonymous and unpermissioned user attempt to edit the node.
*/
function testSummaryLength() {
// Create a node to view.
$settings = array(
'body' => array(LANGUAGE_NOT_SPECIFIED => array(array('value' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vitae arcu at leo cursus laoreet. Curabitur dui tortor, adipiscing malesuada tempor in, bibendum ac diam. Cras non tellus a libero pellentesque condimentum. What is a Drupalism? Suspendisse ac lacus libero. Ut non est vel nisl faucibus interdum nec sed leo. Pellentesque sem risus, vulputate eu semper eget, auctor in libero. Ut fermentum est vitae metus convallis scelerisque. Phasellus pellentesque rhoncus tellus, eu dignissim purus posuere id. Quisque eu fringilla ligula. Morbi ullamcorper, lorem et mattis egestas, tortor neque pretium velit, eget eleifend odio turpis eu purus. Donec vitae metus quis leo pretium tincidunt a pulvinar sem. Morbi adipiscing laoreet mauris vel placerat. Nullam elementum, nisl sit amet scelerisque malesuada, dolor nunc hendrerit quam, eu ultrices erat est in orci. Curabitur feugiat egestas nisl sed accumsan.'))),
'promote' => 1,
);
$node = $this->drupalCreateNode($settings);
$this->assertTrue(node_load($node->nid), t('Node created.'));
// Create user with permission to view the node.
$web_user = $this->drupalCreateUser(array('access content', 'administer content types'));
$this->drupalLogin($web_user);
// Attempt to access the front page.
$this->drupalGet("node");
// The node teaser when it has 600 characters in length
$expected = 'What is a Drupalism?';
$this->assertRaw($expected, t('Check that the summary is 600 characters in length'), 'Node');
Dries Buytaert
committed
// Change the teaser length for "Basic page" content type.
$instance = field_info_instance('node', 'body', $node->type);
$instance['display']['teaser']['settings']['trim_length'] = 200;
field_update_instance($instance);
// Attempt to access the front page again and check if the summary is now only 200 characters in length.
$this->drupalGet("node");
$this->assertNoRaw($expected, t('Check that the summary is not longer than 200 characters'), 'Node');
}
}
class NodeTitleXSSTestCase extends NodeWebTestCase {
Angie Byron
committed
public static function getInfo() {
'name' => 'Node title XSS filtering',
'description' => 'Create a node with dangerous tags in its title and test that they are escaped.',
'group' => 'Node',
);
}
function testNodeTitleXSS() {
// Prepare a user to do the stuff.
$web_user = $this->drupalCreateUser(array('create page content', 'edit any page content'));
$this->drupalLogin($web_user);
$xss = '<script>alert("xss")</script>';
$title = $xss . $this->randomName();
$edit = array("title" => $title);
$this->drupalPost('node/add/page', $edit, t('Preview'));
$this->assertNoRaw($xss, t('Harmful tags are escaped when previewing a node.'));
$settings = array('title' => $title);
$node = $this->drupalCreateNode($settings);
$this->drupalGet('node/' . $node->nid);
Angie Byron
committed
// assertTitle() decodes HTML-entities inside the <title> element.
$this->assertTitle($edit["title"] . ' | Drupal', t('Title is diplayed when viewing a node.'));
$this->assertNoRaw($xss, t('Harmful tags are escaped when viewing a node.'));
$this->drupalGet('node/' . $node->nid . '/edit');
$this->assertNoRaw($xss, t('Harmful tags are escaped when editing a node.'));
}
Dries Buytaert
committed
class NodeBlockTestCase extends NodeWebTestCase {
Angie Byron
committed
public static function getInfo() {
Dries Buytaert
committed
return array(
'name' => 'Block availability',
'description' => 'Check if the syndicate block is available.',
'group' => 'Node',
Dries Buytaert
committed
);
}
function setUp() {
parent::setUp(array('block'));
Dries Buytaert
committed
// Create and login user
$admin_user = $this->drupalCreateUser(array('administer blocks'));
$this->drupalLogin($admin_user);
}
function testSearchFormBlock() {
// Set block title to confirm that the interface is available.
Angie Byron
committed
$this->drupalPost('admin/structure/block/manage/node/syndicate/configure', array('title' => $this->randomName(8)), t('Save block'));
$this->assertText(t('The block configuration has been saved.'), t('Block configuration set.'));
Dries Buytaert
committed
// Set the block to a region to confirm block is available.
Dries Buytaert
committed
$edit = array();
$edit['blocks[node_syndicate][region]'] = 'footer';
Dries Buytaert
committed
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
$this->assertText(t('The block settings have been updated.'), t('Block successfully move to footer region.'));
Dries Buytaert
committed
}
}
Angie Byron
committed
/**
* Check that the post information displays when enabled for a content type.
*/
class NodePostSettingsTestCase extends NodeWebTestCase {
Angie Byron
committed
public static function getInfo() {
Angie Byron
committed
return array(
'name' => 'Node post information display',
'description' => 'Check that the post information (submitted by Username on date) text displays appropriately.',
'group' => 'Node',
Angie Byron
committed
);
}
function setUp() {
parent::setUp();
$web_user = $this->drupalCreateUser(array('create page content', 'administer content types', 'access user profiles'));
$this->drupalLogin($web_user);
}
/**
Angie Byron
committed
* Set "Basic page" content type to display post information and confirm its presence on a new node.
Angie Byron
committed
*/
function testPagePostInfo() {
Angie Byron
committed
// Set "Basic page" content type to display post information.
Angie Byron
committed
$edit = array();
$edit['node_submitted'] = TRUE;
Dries Buytaert
committed
$this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
Angie Byron
committed
// Create a node.
$edit = array();
$langcode = LANGUAGE_NOT_SPECIFIED;
$edit["title"] = $this->randomName(8);
Angie Byron
committed
$edit["body[$langcode][0][value]"] = $this->randomName(16);
Angie Byron
committed
$this->drupalPost('node/add/page', $edit, t('Save'));
// Check that the post information is displayed.
$node = $this->drupalGetNodeByTitle($edit["title"]);
$elements = $this->xpath('//*[contains(@class,:class)]', array(':class' => 'submitted'));
$this->assertEqual(count($elements), 1, t('Post information is displayed.'));
Angie Byron
committed
}
/**
Angie Byron
committed
* Set "Basic page" content type to not display post information and confirm its absence on a new node.
Angie Byron
committed
*/
function testPageNotPostInfo() {
Angie Byron
committed
// Set "Basic page" content type to display post information.
Angie Byron
committed
$edit = array();
$edit['node_submitted'] = FALSE;
Dries Buytaert
committed
$this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
Angie Byron
committed
// Create a node.
$edit = array();
$langcode = LANGUAGE_NOT_SPECIFIED;
$edit["title"] = $this->randomName(8);
Angie Byron
committed
$edit["body[$langcode][0][value]"] = $this->randomName(16);
Angie Byron
committed
$this->drupalPost('node/add/page', $edit, t('Save'));
// Check that the post information is displayed.
$node = $this->drupalGetNodeByTitle($edit["title"]);
$this->assertNoRaw('<span class="submitted">', t('Post information is not displayed.'));
Angie Byron
committed
}
Angie Byron
committed
}
/**
* Ensure that data added to nodes by other modules appears in RSS feeds.
Angie Byron
committed
* Create a node, enable the node_test module to ensure that extra data is
* added to the node->content array, then verify that the data appears on the
* sitewide RSS feed at rss.xml.
*/
class NodeRSSContentTestCase extends NodeWebTestCase {
Angie Byron
committed
public static function getInfo() {
Angie Byron
committed
return array(
'name' => 'Node RSS Content',
'description' => 'Ensure that data added to nodes by other modules appears in RSS feeds.',
'group' => 'Node',
Angie Byron
committed
);
}
function setUp() {
// Enable dummy module that implements hook_node_view.
Angie Byron
committed
parent::setUp('node_test');
Angie Byron
committed
// Use bypass node access permission here, because the test class uses
// hook_grants_alter() to deny access to everyone on node_access
// queries.
$user = $this->drupalCreateUser(array('bypass node access', 'access content', 'create article content'));
$this->drupalLogin($user);
Angie Byron
committed
}
/**
* Create a new node and ensure that it includes the custom data when added
* to an RSS feed.
*/
function testNodeRSSContent() {
// Create a node.
$node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
$this->drupalGet('rss.xml');
Dries Buytaert
committed
// Check that content added in 'rss' view mode appear in RSS feed.
Dries Buytaert
committed
$rss_only_content = t('Extra data that should appear only in the RSS feed for node !nid.', array('!nid' => $node->nid));
$this->assertText($rss_only_content, t('Node content designated for RSS appear in RSS feed.'));
Dries Buytaert
committed
// Check that content added in view modes other than 'rss' doesn't
Dries Buytaert
committed
// appear in RSS feed.
$non_rss_content = t('Extra data that should appear everywhere except the RSS feed for node !nid.', array('!nid' => $node->nid));
$this->assertNoText($non_rss_content, t('Node content not designed for RSS doesn\'t appear in RSS feed.'));
Dries Buytaert
committed
// Check that extra RSS elements and namespaces are added to RSS feed.
$test_element = array(
'key' => 'testElement',
'value' => t('Value of testElement RSS element for node !nid.', array('!nid' => $node->nid)),
);
$test_ns = 'xmlns:drupaltest="http://example.com/test-namespace"';
$this->assertRaw(format_xml_elements(array($test_element)), t('Extra RSS elements appear in RSS feed.'));
$this->assertRaw($test_ns, t('Extra namespaces appear in RSS feed.'));
Dries Buytaert
committed
// Check that content added in 'rss' view mode doesn't appear when
// viewing node.
Dries Buytaert
committed
$this->drupalGet("node/$node->nid");
$this->assertNoText($rss_only_content, t('Node content designed for RSS doesn\'t appear when viewing node.'));
Angie Byron
committed
Angie Byron
committed
}
}
Angie Byron
committed
* Test case to verify basic node_access functionality.
Dries Buytaert
committed
* @todo Cover hook_node_access in a separate test class.
* hook_node_access_records is covered in another test class.
class NodeAccessUnitTest extends NodeWebTestCase {
public static function getInfo() {
return array(
'name' => 'Node access',
'description' => 'Test node_access function',
'group' => 'Node',
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
);
}
/**
* 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);
Dries Buytaert
committed
// User cannot create content without permission.
$this->assertNodeAccess(array('create' => FALSE), 'page', $web_user3);
// User can 'view own unpublished content', but another user cannot.
$web_user4 = $this->drupalCreateUser(array('access content', 'view own unpublished content'));
$web_user5 = $this->drupalCreateUser(array('access content', 'view own unpublished content'));
$node4 = $this->drupalCreateNode(array('status' => 0, 'uid' => $web_user4->uid));
$this->assertNodeAccess(array('view' => TRUE, 'update' => FALSE), $node4, $web_user4);
$this->assertNodeAccess(array('view' => FALSE), $node4, $web_user5);
// Tests the default access provided for a published node.
$node5 = $this->drupalCreateNode();
$this->assertNodeAccess(array('view' => TRUE, 'update' => FALSE, 'delete' => FALSE), $node5, $web_user3);
}
}
/**
* Test case to verify hook_node_access_records functionality.
*/
class NodeAccessRecordsUnitTest extends NodeWebTestCase {
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.
Dries Buytaert
committed
$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.'));
Angie Byron
committed
// 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.
Dries Buytaert
committed
$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.'));
Dries Buytaert
committed
Angie Byron
committed
// 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.'));
Angie Byron
committed
// 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.
Dries Buytaert
committed
$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);
Angie Byron
committed
$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)));
Dries Buytaert
committed
// 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.'));
/**
* Tests for Node Access with a non-node base table.
*/
class NodeAccessBaseTableTestCase extends NodeWebTestCase {
// 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.',