drupal/modules/node/node.test

402 lines
13 KiB
Plaintext

<?php
// $Id$
class NodeRevisionsTestCase extends DrupalWebTestCase {
protected $nodes;
protected $logs;
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Node revisions'),
'description' => t('Creates a node of type Page and then a user tries various revision
actions such as viewing, reverting to, and deleting revisions.'),
'group' => t('Node')
);
}
/**
* Implementation of setUp().
*/
function setUp() {
parent::setUp();
// Create and login user.
$test_user = $this->drupalCreateUser(array('view revisions', 'revert revisions', 'edit any page content',
'delete revisions', 'delete any page content'));
$this->drupalLogin($test_user);
// Create initial node.
$node = $this->drupalCreateNode();
$settings = get_object_vars($node);
$settings['revision'] = 1;
$nodes = array();
$logs = array();
// Get original node.
$nodes[] = $node;
// 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 opperations.
*/
function testRevisions() {
$nodes = $this->nodes;
$logs = $this->logs;
// 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, t('Correct text displays for version.'));
// 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.'));
}
// 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.',
array('@type' => 'Page', '%title' => $nodes[1]->title,
'%revision-date' => format_date($nodes[1]->revision_timestamp))), t('Revision reverted.'));
$reverted_node = node_load($node->nid);
$this->assertTrue(($nodes[1]->body == $reverted_node->body), t('Node reverted correctly.'));
// 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' => 'Page', '%title' => $nodes[1]->title)), t('Revision deleted.'));
$this->assertTrue(db_result(db_query('SELECT COUNT(vid) FROM {node_revisions} WHERE nid = %d and vid = %d', $node->nid, $nodes[1]->vid)) == 0, t('Revision not found.'));
}
}
class NodeTeaserTestCase extends DrupalWebTestCase {
/**
* Implementation of getInfo() for information
*/
function getInfo() {
return array(
'name' => t('Node teaser'),
'description' => t('Calls node_teaser() with different strings and lengths.'),
'group' => t('Node')
);
}
function setUp() {
parent::setUp();
}
function tearDown() {
parent::tearDown();
}
/**
* Tests an edge case where if the first sentence is a question and
* subsequent sentences are not.
*/
function testFirstSentenceQuestion() {
$body = 'A question? A sentence. Another sentence.';
$expectedTeaser = 'A question? A sentence.';
$this->callNodeTeaser($body, $expectedTeaser, NULL, 30);
}
/**
* Simpletest test. A real-life example of the above edge case.
*/
function testFirstSentenceQuestion2() {
$body = 'Are you an UberBabe? (Or an appreciator of UberBabes?) I am most definitely an UberBabe, and I\'m proud of it. Now, before anyone screams "sexism" or "bias" or "cheap" or anything more profane, let me clarify. An UberBabe is not someone who\'s playfully pierced navel protrudes from a belly bearing top. Not necessarily anyway. An UberBabe is a woman who likes being totally feminine, but is also smart as hell, brave, a rule breaker, speaks her mind, finds her own way, goes up against "the system" in a way that allows the system to evolve, and so on. UberBabes, frankly, kick booty - and they just may save the world.';
$expectedTeaser = 'Are you an UberBabe? (Or an appreciator of UberBabes?) I am most definitely an UberBabe, and I\'m proud of it. Now, before anyone screams "sexism" or "bias" or "cheap" or anything more profane, let me clarify.';
$this->callNodeTeaser($body, $expectedTeaser, NULL, 300);
}
/**
* Test various teaser length edge cases.
*/
function testLength() {
// This body string tests a number of edge cases.
$body = "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>";
// The teasers we expect node_teaser() to return when $size is the index
// of each array item.
// Using an input format with no line-break filter:
$teasers = array(
"<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
"<",
"<p",
"<p>",
"<p>\n",
"<p>\nH",
"<p>\nHi",
"<p>\nHi\n",
"<p>\nHi\n<",
"<p>\nHi\n</",
"<p>\nHi\n</p",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
"<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
"<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
);
// And Using an input format WITH the line-break filter.
$teasers_lb = array(
"<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
"<",
"<p",
"<p>",
"<p>",
"<p>",
"<p>",
"<p>\nHi",
"<p>\nHi",
"<p>\nHi",
"<p>\nHi",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>",
"<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
"<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
"<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
);
// Test node_teaser() for different sizes.
for ($i = 0; $i <= 37; $i++) {
$this->callNodeTeaser($body, $teasers[$i], NULL, $i);
$this->callNodeTeaser($body, $teasers_lb[$i], 1, $i);
$this->callNodeTeaser($body, $teasers_lb[$i], 2, $i);
}
}
/**
* Calls node_teaser() and asserts that the expected teaser is returned.
*/
function callNodeTeaser($body, $expectedTeaser, $format = NULL, $size = NULL) {
$teaser = node_teaser($body, $format, $size);
$this->assertIdentical($teaser, $expectedTeaser);
}
}
class PageEditTestCase extends DrupalWebTestCase {
function getInfo() {
return array(
'name' => 'Page edit test',
'description' => t('We want a working edit for pages, uh?'),
'group' => t('Node'));
}
function testPageEdit() {
/* Prepare settings */
variable_set('node_options_page', array('status', 'promote'));
/* Prepare a user to do the stuff */
$web_user = $this->drupalCreateUser(array('edit own page content', 'create page content'));
$this->drupalLogin($web_user);
$edit = array(
'title' => '!SimpleTest! test title' . $this->randomName(20),
'body' => '!SimpleTest! test body' . $this->randomName(200),
);
//Create the page to edit
$this->drupalPost('node/add/page', $edit, t('Save'));
$node = node_load(array('title' => $edit['title']));
$this->assertNotNull($node, 'Node found in database');
$this->clickLink(t('Edit'));
$editurl = url("node/$node->nid/edit", array('absolute' => true));
$acturl = $this->getURL();
$this->assertEqual($editurl, $acturl);
$this->assertText(t('Edit'), 'Edit text is here');
$this->assertText(t($edit['title']), 'Hello, the random title');
$this->assertText(t($edit['body']), 'test is over, the body\'s still there');
$edit = array(
'title' => '!SimpleTest! test title' . $this->randomName(20),
'body' => '!SimpleTest! test body' . $this->randomName(200),
);
//edit the content of the page
$this->drupalPost("node/$node->nid/edit", $edit, t('Save'));
$this->assertText(t($edit['title']), 'Hello, the random title');
$this->assertText(t($edit['body']), 'test is over, the body\'s still there');
}
}
class PagePreviewTestCase extends DrupalWebTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => 'Page preview test',
'description' => t('We want a working preview for pages, uh?'),
'group' => t('Node'));
}
/**
* Test node previews.
*/
function testPagePreview() {
// Prepare settings.
variable_set('node_options_page', array('status', 'promote'));
// Prepare a user to do the stuff.
$web_user = $this->drupalCreateUser(array('edit own page content', 'create page content'));
$this->drupalLogin($web_user);
$edit = array(
'title'=>'!SimpleTest! title' . $this->randomName(20),
'body'=>'!SimpleTest! body' . $this->randomName(200),
);
$this->drupalPost('node/add/page', $edit, t('Preview'));
$this->assertText(t('Preview'), 'Preview text is here');
$this->assertText(t($edit['title']), 'Hello, the random title');
$this->assertText(t($edit['body']), 'test is over, the body\'s still there');
$this->assertFieldByName('title', $edit['title'], 'The title is on it\'s place');
}
}
class PageCreationTestCase extends DrupalWebTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Page node creation'),
'description' => t('Create a page node and verify its consistency in the database.'),
'group' => t('Node'),
);
}
function testPageCreation() {
// Prepare settings.
variable_set('node_options_page', array('status', 'promote'));
// Prepare a user to do the stuff.
$web_user = $this->drupalCreateUser(array('edit own page content', 'create page content'));
$this->drupalLogin($web_user);
$edit = array();
$edit['title'] = '!SimpleTest test node! ' . $this->randomName(10);
$edit['body'] = '!SimpleTest test body! ' . $this->randomName(32) . ' ' . $this->randomName(32);
$this->drupalPost('node/add/page', $edit, t('Save'));
$this->assertRaw(t('!post %title has been created.', array ('!post' => 'Page', '%title' => $edit['title'])), 'Page created');
$node = node_load(array('title' => $edit['title']));
$this->assertNotNull($node, t('Node !title found in database.', array ('!title' => $edit['title'])));
}
}
class PageViewTestCase extends DrupalWebTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Unauthorized node view'),
'description' => t('Creates a node of type page and then an unpermissioned user attempts to edit the node, '
. 'before tries with an anonymous user. Asserts failure.'
. '</ br>WARNING: This is based on default registered user permissions (no administer nodes).')
, 'group' => t('Node'),
);
}
function testPageView() {
// Prepare a node to view
global $user;
$node = $this->drupalCreateNode();
$this->assertNotNull(node_load($node->nid), 'Node created');
// Tries to edit with anonymous user
$html = $this->drupalGet("node/$node->nid/edit");
$this->assertResponse(403);
// Prepare a user to request the node view
$test_user = $this->drupalCreateUser(array('access content'));
$this->drupalLogin($test_user);
$html = $this->drupalGet("node/$node->nid/edit");
$this->assertResponse(403);
$test_user = $this->drupalCreateUser(array('administer nodes'));
//TODO: Add edit page attempt with administer nodes user
node_delete($node->nid);
}
}