drupal/modules/poll/poll.test

134 lines
4.2 KiB
Plaintext

<?php
// $Id$
class PollTestCase extends DrupalWebTestCase {
/**
* Creates a poll.
*
* @param string $title The title of the poll.
* @param array $choices Choices.
* @param boolean $test_preview Whether to test if the preview is working or not.
* @return integer The nid of the created poll, or FALSE on error.
*/
function pollCreate($title, $choices, $test_preview = TRUE) {
$this->assertTrue(TRUE, 'Create a poll');
$web_user = $this->drupalCreateUser(array('create poll content', 'access content'));
$this->drupalLogin($web_user);
// Get the form first to initialize the state of the internal browser
$this->drupalGet('node/add/poll');
// Prepare a form with two choices
list($edit, $index) = $this->_pollGenerateEdit($title, $choices);
if (count($choices) > 2) {
// Re-submit the form while the choices are all in
while($index < count($choices)) {
$this->drupalPost(NULL, $edit, t('More choices'));
list($edit, $index) = $this->_pollGenerateEdit($title, $choices, $index);
}
}
if ($test_preview) {
$this->drupalPost(NULL, $edit, t('Preview'));
foreach($choices as $k => $choice_text) {
$this->assertRaw($choice_text, t('Choice @choice found was in preview.', array('@choice' => $k)));
}
list($edit, $index) = $this->_pollGenerateEdit($title, $choices, $index);
}
$this->drupalPost(NULL, $edit, t('Save'));
$node = node_load(array('title' => $title));
$this->assertRaw(t('@type %title has been created.', array('@type' => node_get_types('name', 'poll'), '%title' => $title)), 'Poll has been created.');
$this->assertTrue($node->nid, t('Poll has been found in the database'));
return isset($node->nid) ? $node->nid : FALSE;
}
function _pollGenerateEdit($title, $choices, $index = 0) {
$max_new_choices = $index == 0 ? 2 : 5;
$already_submitted_choices = array_slice($choices, 0, $index);
$new_choices = array_values(array_slice($choices, $index, $max_new_choices));
$edit = array(
'title' => $title
);
foreach($already_submitted_choices as $k => $text) {
$edit['choice[chid:' . $k . '][chtext]'] = $text;
}
foreach($new_choices as $k => $text) {
$edit['choice[new:' . $k . '][chtext]'] = $text;
}
return array($edit, count($already_submitted_choices) + count($new_choices));
}
function _generateChoices($count = 7) {
$choices = array();
for($i = 1; $i <= $count; $i++) {
$choices[] = $this->randomName();
}
return $choices;
}
}
class PollCreateTestCase extends PollTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array('name' => t('Poll create'), 'description' => 'Adds "more choices", previews and creates a poll.', 'group' => t('Poll'));
}
function setUp() {
parent::setUp('poll');
}
function testPollCreate() {
$title = $this->randomName();
$choices = $this->_generateChoices(7);
$this->pollCreate($title, $choices, TRUE);
}
}
class PollVoteTestCase extends PollTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array('name' => t('Poll vote'), 'description' => 'Vote on a poll', 'group' => t('Poll'));
}
function setUp() {
parent::setUp('poll');
}
function tearDown() {
parent::tearDown();
}
function testPollVote() {
$title = $this->randomName();
$choices = $this->_generateChoices(7);
$poll_nid = $this->pollCreate($title, $choices, FALSE);
$this->drupalGet('logout');
$web_user = $this->drupalCreateUser(array('cancel own vote', 'inspect all votes', 'vote on polls', 'access content'));
$this->drupalLogin($web_user);
// Record a vote for the first choice.
$edit = array (
'choice' => '1',
);
$this->drupalPost('node/'. $poll_nid, $edit, t('Vote'));
$this->assertText('Your vote was recorded.', 'Your vote was recorded.');
$this->drupalGet("node/$poll_nid/votes");
$this->assertText(t('This table lists all the recorded votes for this poll. If anonymous users are allowed to vote, they will be identified by the IP address of the computer they used when they voted.'), 'Vote table text.');
$this->assertText($choices[0], 'Vote recorded');
}
}