123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877 |
- <?php
- class PollTestCase extends DrupalWebTestCase {
-
- function pollCreate($title, $choices, $preview = TRUE) {
- $this->assertTrue(TRUE, 'Create a poll');
- $admin_user = $this->drupalCreateUser(array('create poll content', 'administer nodes'));
- $web_user = $this->drupalCreateUser(array('create poll content', 'access content', 'edit own poll content'));
- $this->drupalLogin($admin_user);
-
- $this->drupalGet('node/add/poll');
-
- list($edit, $index) = $this->_pollGenerateEdit($title, $choices);
-
- $edit['choice[new:1][chvotes]'] = -1;
- $edit['choice[new:0][chvotes]'] = $this->randomString(7);
- $this->drupalPost(NULL, $edit, t('Save'));
- $this->assertText(t('Negative values are not allowed.'));
- $this->assertText(t('Vote count for new choice must be an integer.'));
-
- $this->drupalLogin($web_user);
- $this->drupalGet('node/add/poll');
- list($edit, $index) = $this->_pollGenerateEdit($title, $choices);
-
- if (count($choices) > 2) {
- while ($index < count($choices)) {
- $this->drupalPost(NULL, $edit, t('More choices'));
- $this->assertPollChoiceOrder($choices, $index);
- list($edit, $index) = $this->_pollGenerateEdit($title, $choices, $index);
- }
- }
- if ($preview) {
- $this->drupalPost(NULL, $edit, t('Preview'));
- $this->assertPollChoiceOrder($choices, $index, TRUE);
- list($edit, $index) = $this->_pollGenerateEdit($title, $choices, $index);
- }
- $this->drupalPost(NULL, $edit, t('Save'));
- $node = $this->drupalGetNodeByTitle($title);
- $this->assertText(t('@type @title has been created.', array('@type' => node_type_get_name('poll'), '@title' => $title)), 'Poll has been created.');
- $this->assertTrue($node->nid, 'Poll has been found in the database.');
- return isset($node->nid) ? $node->nid : FALSE;
- }
-
- function _pollGenerateEdit($title, array $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;
- }
-
- function assertPollChoiceOrder(array $choices, $index = 0, $preview = FALSE) {
- $expected = array();
- $weight = 0;
- foreach ($choices as $id => $label) {
- if ($id < $index) {
-
- $weight++;
-
- $this->assertFieldByName('choice[chid:' . $id . '][weight]', $weight, format_string('Found choice @id with weight @weight.', array(
- '@id' => $id,
- '@weight' => $weight,
- )));
-
- $expected[$weight] = $label;
- }
- }
- ksort($expected);
-
- $elements = $this->xpath('//input[starts-with(@name, :prefix) and contains(@name, :suffix)]', array(
- ':prefix' => 'choice[chid:',
- ':suffix' => '][chtext]',
- ));
- $expected_order = $expected;
- foreach ($elements as $element) {
- $next_label = array_shift($expected_order);
- $this->assertEqual((string) $element['value'], $next_label);
- }
-
- if ($preview) {
- $elements = $this->xpath('//div[contains(@class, :teaser)]/descendant::div[@class=:text]', array(
- ':teaser' => 'node-teaser',
- ':text' => 'text',
- ));
- $expected_order = $expected;
- foreach ($elements as $element) {
- $next_label = array_shift($expected_order);
- $this->assertEqual((string) $element, $next_label, format_string('Found choice @label in preview.', array(
- '@label' => $next_label,
- )));
- }
- }
- }
- function pollUpdate($nid, $title, $edit) {
-
- $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
- $this->assertText(t('@type @title has been updated.', array('@type' => node_type_get_name('poll'), '@title' => $title)), 'Poll has been updated.');
- }
- }
- class PollCreateTestCase extends PollTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Poll create',
- 'description' => 'Adds "more choices", previews and creates a poll.',
- 'group' => 'Poll'
- );
- }
- function setUp() {
- parent::setUp('poll');
- }
- function testPollCreate() {
- $title = $this->randomName();
- $choices = $this->_generateChoices(7);
- $poll_nid = $this->pollCreate($title, $choices, TRUE);
-
- $this->drupalGet('poll');
- $this->assertText($title, 'Poll appears in poll list.');
- $this->assertText('open', 'Poll is active.');
-
- $this->clickLink($title);
- $this->assertText('Total votes: 0', 'Link to poll correct.');
-
-
- $node = node_load($poll_nid);
- $new_option = $this->randomName();
- $vote_count = '2000';
- $node->choice[] = array(
- 'chid' => '',
- 'chtext' => $new_option,
- 'chvotes' => (int) $vote_count,
- 'weight' => 1000,
- );
- node_save($node);
- $this->drupalGet('poll');
- $this->clickLink($title);
- $this->assertText($new_option, 'New option found.');
- $option = $this->xpath('//div[@id="node-1"]//div[@class="poll"]//div[@class="text"]');
- $this->assertEqual(end($option), $new_option, 'Last item is equal to new option.');
- $votes = $this->xpath('//div[@id="node-1"]//div[@class="poll"]//div[@class="percent"]');
- $this->assertTrue(strpos(end($votes), $vote_count) > 0, "Votes saved.");
- }
- function testPollClose() {
- $content_user = $this->drupalCreateUser(array('create poll content', 'edit any poll content', 'access content'));
- $vote_user = $this->drupalCreateUser(array('cancel own vote', 'inspect all votes', 'vote on polls', 'access content'));
-
- $title = $this->randomName();
- $choices = $this->_generateChoices(7);
- $poll_nid = $this->pollCreate($title, $choices, FALSE);
- $this->drupalLogout();
- $this->drupalLogin($content_user);
-
- $close_edit = array('active' => 0);
- $this->pollUpdate($poll_nid, $title, $close_edit);
-
- $this->drupalGet('node/' . $poll_nid);
- $elements = $this->xpath('//input[@id="edit-vote"]');
- $this->assertTrue(empty($elements), "Vote button doesn't appear.");
-
- $this->drupalGet('poll');
- $this->assertText($title, 'Poll appears in poll list.');
- $this->assertText('closed', 'Poll is closed.');
-
- $open_edit = array('active' => 1);
- $this->pollUpdate($poll_nid, $title, $open_edit);
-
- $this->drupalLogout();
- $this->drupalLogin($vote_user);
- $vote_edit = array('choice' => '1');
- $this->drupalPost('node/' . $poll_nid, $vote_edit, t('Vote'));
- $this->assertText('Your vote was recorded.', 'Your vote was recorded.');
- $elements = $this->xpath('//input[@value="Cancel your vote"]');
- $this->assertTrue(isset($elements[0]), "'Cancel your vote' button appears.");
-
- $this->drupalLogout();
- $this->drupalLogin($content_user);
- $close_edit = array('active' => 0);
- $this->pollUpdate($poll_nid, $title, $close_edit);
-
- $this->drupalGet('node/' . $poll_nid);
- $elements = $this->xpath('//input[@value="Cancel your vote"]');
- $this->assertTrue(empty($elements), "'Cancel your vote' button no longer appears.");
- }
- }
- class PollVoteTestCase extends PollTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Poll vote',
- 'description' => 'Vote on a poll',
- 'group' => '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->drupalLogout();
- $vote_user = $this->drupalCreateUser(array('cancel own vote', 'inspect all votes', 'vote on polls', 'access content'));
- $restricted_vote_user = $this->drupalCreateUser(array('vote on polls', 'access content'));
- $this->drupalLogin($vote_user);
-
- $edit = array();
- $this->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
- $this->assertText(t('Your vote could not be recorded because you did not select any of the choices.'), 'Found the empty poll submission error message.');
-
- $edit = array(
- 'choice' => '1',
- );
- $this->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
- $this->assertText('Your vote was recorded.', 'Your vote was recorded.');
- $this->assertText('Total votes: 1', 'Vote count updated correctly.');
- $elements = $this->xpath('//input[@value="Cancel your vote"]');
- $this->assertTrue(isset($elements[0]), "'Cancel your vote' button appears.");
- $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');
-
- $this->drupalGet('poll');
- $this->assertText($title, 'Poll appears in poll list.');
- $this->assertText('1 vote', 'Poll has 1 vote.');
-
- $this->drupalPost('node/' . $poll_nid, array(), t('Cancel your vote'));
- $this->assertText('Your vote was cancelled.', 'Your vote was cancelled.');
- $this->assertNoText('Cancel your vote', "Cancel vote button doesn't appear.");
- $this->drupalGet("node/$poll_nid/votes");
- $this->assertNoText($choices[0], 'Vote cancelled');
-
- $this->drupalGet('poll');
- $this->assertText($title, 'Poll appears in poll list.');
- $this->assertText('0 votes', 'Poll has 0 votes.');
-
- $this->drupalLogout();
- $this->drupalLogin($restricted_vote_user);
-
- $edit = array(
- 'choice' => '1',
- );
- $this->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
- $this->assertText('Your vote was recorded.', 'Your vote was recorded.');
- $this->assertText('Total votes: 1', 'Vote count updated correctly.');
- $elements = $this->xpath('//input[@value="Cancel your vote"]');
- $this->assertTrue(empty($elements), "'Cancel your vote' button does not appear.");
- }
- }
- class PollBlockTestCase extends PollTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Block availability',
- 'description' => 'Check if the most recent poll block is available.',
- 'group' => 'Poll',
- );
- }
- function setUp() {
- parent::setUp('poll');
-
- $admin_user = $this->drupalCreateUser(array('administer blocks'));
- $this->drupalLogin($admin_user);
- }
- function testRecentBlock() {
-
- $this->drupalPost('admin/structure/block/manage/poll/recent/configure', array('title' => $this->randomName(8)), t('Save block'));
- $this->assertText(t('The block configuration has been saved.'), 'Block configuration set.');
-
- $edit = array();
- $edit['blocks[poll_recent][region]'] = 'footer';
- $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
- $this->assertText(t('The block settings have been updated.'), 'Block successfully move to footer region.');
-
- $title = $this->randomName();
- $choices = $this->_generateChoices(7);
- $poll_nid = $this->pollCreate($title, $choices, TRUE);
-
-
- $this->drupalGet('user');
-
-
- $this->assertText($title, 'Poll appears in block.');
-
- $this->drupalLogout();
- $vote_user = $this->drupalCreateUser(array('cancel own vote', 'inspect all votes', 'vote on polls', 'access content'));
- $this->drupalLogin($vote_user);
-
- $edit = array(
- 'choice' => '1',
- );
- $this->drupalPost('user/' . $vote_user->uid, $edit, t('Vote'));
- $this->assertText('Your vote was recorded.', 'Your vote was recorded.');
- $this->assertText('Total votes: 1', 'Vote count updated correctly.');
- $this->assertText('Older polls', 'Link to older polls appears.');
- $this->clickLink('Older polls');
- $this->assertText('1 vote - open', 'Link to poll listing correct.');
-
- $content_user = $this->drupalCreateUser(array('create poll content', 'edit any poll content', 'access content'));
- $this->drupalLogout();
- $this->drupalLogin($content_user);
- $close_edit = array('active' => 0);
- $this->pollUpdate($poll_nid, $title, $close_edit);
- $this->drupalGet('user/' . $content_user->uid);
- $this->assertNoText($title, 'Poll no longer appears in block.');
- }
- }
- class PollJSAddChoice extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Poll add choice',
- 'description' => 'Submits a POST request for an additional poll choice.',
- 'group' => 'Poll'
- );
- }
- function setUp() {
- parent::setUp('poll');
- }
-
- function testAddChoice() {
- $web_user = $this->drupalCreateUser(array('create poll content', 'access content'));
- $this->drupalLogin($web_user);
- $this->drupalGet('node/add/poll');
- $edit = array(
- "title" => $this->randomName(),
- 'choice[new:0][chtext]' => $this->randomName(),
- 'choice[new:1][chtext]' => $this->randomName(),
- );
-
-
- $commands = $this->drupalPostAJAX(NULL, $edit, array('op' => t('More choices')));
- $this->content = $commands[1]['data'];
- $this->assertFieldByName('choice[chid:0][chtext]', $edit['choice[new:0][chtext]'], format_string('Field !i found', array('!i' => 0)));
- $this->assertFieldByName('choice[chid:1][chtext]', $edit['choice[new:1][chtext]'], format_string('Field !i found', array('!i' => 1)));
- $this->assertFieldByName('choice[new:0][chtext]', '', format_string('Field !i found', array('!i' => 2)));
- }
- }
- class PollVoteCheckHostname extends PollTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'User poll vote capability.',
- 'description' => 'Check that users and anonymous users from specified ip-address can only vote once.',
- 'group' => 'Poll'
- );
- }
- function setUp() {
- parent::setUp('poll');
-
- $this->admin_user = $this->drupalCreateUser(array('administer permissions', 'create poll content'));
- $this->drupalLogin($this->admin_user);
-
- user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
- 'access content' => TRUE,
- 'vote on polls' => TRUE,
- 'cancel own vote' => TRUE,
- ));
-
-
- variable_set('cache', 1);
-
- $title = $this->randomName();
- $choices = $this->_generateChoices(3);
- $this->poll_nid = $this->pollCreate($title, $choices, FALSE);
- $this->drupalLogout();
-
- $this->web_user1 = $this->drupalCreateUser(array('access content', 'vote on polls', 'cancel own vote'));
- $this->web_user2 = $this->drupalCreateUser(array('access content', 'vote on polls'));
- }
-
- function testHostnamePollVote() {
-
- $this->drupalLogin($this->web_user1);
- $edit = array(
- 'choice' => '1',
- );
-
- $this->drupalPost('node/' . $this->poll_nid, $edit, t('Vote'));
- $this->assertText(t('Your vote was recorded.'), format_string('%user vote was recorded.', array('%user' => $this->web_user1->name)));
- $this->assertText(t('Total votes: @votes', array('@votes' => 1)), 'Vote count updated correctly.');
-
- $this->drupalGet('node/' . $this->poll_nid);
- $elements = $this->xpath('//input[@value="Vote"]');
- $this->assertTrue(empty($elements), format_string("%user is not able to vote again.", array('%user' => $this->web_user1->name)));
- $elements = $this->xpath('//input[@value="Cancel your vote"]');
- $this->assertTrue(!empty($elements), "'Cancel your vote' button appears.");
-
- $this->drupalLogout();
-
- $this->drupalGet('node/' . $this->poll_nid);
- $this->assertEqual($this->drupalGetHeader('x-drupal-cache'), 'MISS', 'Page was cacheable but was not in the cache.');
- $this->drupalGet('node/' . $this->poll_nid);
- $this->assertEqual($this->drupalGetHeader('x-drupal-cache'), 'HIT', 'Page was cached.');
-
- $this->drupalPost(NULL, $edit, t('Vote'));
- $this->assertText(t('Your vote was recorded.'), 'Anonymous vote was recorded.');
- $this->assertText(t('Total votes: @votes', array('@votes' => 2)), 'Vote count updated correctly.');
- $elements = $this->xpath('//input[@value="Cancel your vote"]');
- $this->assertTrue(!empty($elements), "'Cancel your vote' button appears.");
-
- $this->drupalGet('node/' . $this->poll_nid);
- $this->assertFalse($this->drupalGetHeader('x-drupal-cache'), 'Page was not cacheable.');
- $elements = $this->xpath('//input[@value="Vote"]');
- $this->assertTrue(empty($elements), "Anonymous is not able to vote again.");
- $elements = $this->xpath('//input[@value="Cancel your vote"]');
- $this->assertTrue(!empty($elements), "'Cancel your vote' button appears.");
-
- $this->drupalLogin($this->web_user2);
-
- $this->drupalPost('node/' . $this->poll_nid, $edit, t('Vote'));
- $this->assertText(t('Your vote was recorded.'), format_string('%user vote was recorded.', array('%user' => $this->web_user2->name)));
- $this->assertText(t('Total votes: @votes', array('@votes' => 3)), 'Vote count updated correctly.');
- $elements = $this->xpath('//input[@value="Cancel your vote"]');
- $this->assertTrue(empty($elements), "'Cancel your vote' button does not appear.");
-
- $this->drupalLogout();
-
- db_update('poll_vote')
- ->fields(array(
- 'hostname' => '123.456.789.1',
- ))
- ->condition('hostname', '', '<>')
- ->execute();
-
-
- $this->drupalGet('node/' . $this->poll_nid);
- $this->assertEqual($this->drupalGetHeader('x-drupal-cache'), 'MISS', 'Page was cacheable but was not in the cache.');
- $this->drupalPost(NULL, $edit, t('Vote'));
- $this->assertText(t('Your vote was recorded.'), format_string('%user vote was recorded.', array('%user' => $this->web_user2->name)));
- $this->assertText(t('Total votes: @votes', array('@votes' => 4)), 'Vote count updated correctly.');
- $elements = $this->xpath('//input[@value="Cancel your vote"]');
- $this->assertTrue(!empty($elements), "'Cancel your vote' button appears.");
-
-
- $this->curlClose();
- $this->drupalGet('node/' . $this->poll_nid);
- $this->assertEqual($this->drupalGetHeader('x-drupal-cache'), 'MISS', 'Page was cacheable but was not in the cache.');
- $elements = $this->xpath('//input[@value="Vote"]');
- $this->assertTrue(empty($elements), 'Anonymous is not able to vote again.');
- $elements = $this->xpath('//input[@value="Cancel your vote"]');
- $this->assertTrue(empty($elements), "'Cancel your vote' button does not appear.");
-
- $this->drupalLogin($this->web_user1);
-
- $this->drupalGet('node/' . $this->poll_nid);
- $elements = $this->xpath('//input[@value="Vote"]');
- $this->assertTrue(empty($elements), format_string("%user is not able to vote again.", array('%user' => $this->web_user1->name)));
- $elements = $this->xpath('//input[@value="Cancel your vote"]');
- $this->assertTrue(!empty($elements), "'Cancel your vote' button appears.");
- }
- }
- class PollTokenReplaceTestCase extends PollTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Poll token replacement',
- 'description' => 'Generates text using placeholders for dummy content to check poll token replacement.',
- 'group' => 'Poll',
- );
- }
- function setUp() {
- parent::setUp('poll');
- }
-
- function testPollTokenReplacement() {
- global $language;
-
- $title = $this->randomName();
- $choices = $this->_generateChoices(3);
- $poll_nid = $this->pollCreate($title, $choices, FALSE);
- $this->drupalLogout();
-
- $vote_user1 = $this->drupalCreateUser(array('vote on polls', 'access content'));
- $this->drupalLogin($vote_user1);
- $edit = array(
- 'choice' => '1',
- );
- $this->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
- $this->drupalLogout();
- $vote_user2 = $this->drupalCreateUser(array('vote on polls', 'access content'));
- $this->drupalLogin($vote_user2);
- $edit = array(
- 'choice' => '1',
- );
- $this->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
- $this->drupalLogout();
- $vote_user3 = $this->drupalCreateUser(array('vote on polls', 'access content'));
- $this->drupalLogin($vote_user3);
- $edit = array(
- 'choice' => '2',
- );
- $this->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
- $this->drupalLogout();
- $vote_user4 = $this->drupalCreateUser(array('vote on polls', 'access content'));
- $this->drupalLogin($vote_user4);
- $edit = array(
- 'choice' => '3',
- );
- $this->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
- $this->drupalLogout();
- $poll = node_load($poll_nid, NULL, TRUE);
-
- $tests = array();
- $tests['[node:poll-votes]'] = 4;
- $tests['[node:poll-winner]'] = filter_xss($poll->choice[1]['chtext']);
- $tests['[node:poll-winner-votes]'] = 2;
- $tests['[node:poll-winner-percent]'] = 50;
- $tests['[node:poll-duration]'] = format_interval($poll->runtime, 1, $language->language);
-
- $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
- foreach ($tests as $input => $expected) {
- $output = token_replace($input, array('node' => $poll), array('language' => $language));
- $this->assertEqual($output, $expected, format_string('Sanitized poll token %token replaced.', array('%token' => $input)));
- }
-
- $tests['[node:poll-winner]'] = $poll->choice[1]['chtext'];
- foreach ($tests as $input => $expected) {
- $output = token_replace($input, array('node' => $poll), array('language' => $language, 'sanitize' => FALSE));
- $this->assertEqual($output, $expected, format_string('Unsanitized poll token %token replaced.', array('%token' => $input)));
- }
- }
- }
- class PollExpirationTestCase extends PollTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Poll expiration',
- 'description' => 'Test the poll auto-expiration logic.',
- 'group' => 'Poll',
- );
- }
- function setUp() {
- parent::setUp('poll');
- }
- function testAutoExpire() {
-
- $title = $this->randomName();
- $choices = $this->_generateChoices(2);
- $poll_nid = $this->pollCreate($title, $choices, FALSE);
- $this->assertTrue($poll_nid, 'Poll for auto-expire test created.');
-
-
- $this->drupalGet("node/$poll_nid/edit");
- $this->assertField('runtime', 'Poll expiration setting found.');
- $elements = $this->xpath('//select[@id="edit-runtime"]/option[@selected="selected"]');
- $this->assertTrue(isset($elements[0]['value']) && $elements[0]['value'] == 0, 'Poll expiration set to unlimited.');
-
- $edit = array();
- $poll_expiration = 604800;
- $edit['runtime'] = $poll_expiration;
- $this->drupalPost(NULL, $edit, t('Save'));
- $this->assertRaw(t('Poll %title has been updated.', array('%title' => $title)), 'Poll expiration settings saved.');
-
- $this->drupalGet("node/$poll_nid/edit");
- $elements = $this->xpath('//select[@id="edit-runtime"]/option[@selected="selected"]');
- $this->assertTrue(isset($elements[0]['value']) && $elements[0]['value'] == $poll_expiration, 'Poll expiration set to unlimited.');
-
-
- drupal_cron_run();
- $this->drupalGet("node/$poll_nid/edit");
- $elements = $this->xpath('//input[@id="edit-active-1"]');
- $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), 'Poll is still active.');
-
-
-
- $created = db_query('SELECT created FROM {node} WHERE nid = :nid', array(':nid' => $poll_nid))->fetchField();
- db_update('node')
- ->fields(array('created' => $created - ($poll_expiration * 1.01)))
- ->condition('nid', $poll_nid)
- ->execute();
-
- drupal_cron_run();
- $this->drupalGet("node/$poll_nid/edit");
- $elements = $this->xpath('//input[@id="edit-active-0"]');
- $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), 'Poll has expired.');
- }
- }
- class PollDeleteChoiceTestCase extends PollTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Poll choice deletion',
- 'description' => 'Test the poll choice deletion logic.',
- 'group' => 'Poll',
- );
- }
- function setUp() {
- parent::setUp('poll');
- }
- function testChoiceRemoval() {
-
- $title = $this->randomName();
- $choices = array('First choice', 'Second choice', 'Third choice');
- $poll_nid = $this->pollCreate($title, $choices, FALSE);
- $this->assertTrue($poll_nid, 'Poll for choice deletion logic test created.');
-
- $this->drupalGet("node/$poll_nid/edit");
- $edit['choice[chid:1][chtext]'] = '';
- $this->drupalPost(NULL, $edit, t('Save'));
-
- $this->drupalGet('poll');
- $this->clickLink($title);
-
- $this->assertNoText('First choice', 'First choice removed.');
- $this->assertText('Second choice', 'Second choice remains.');
- $this->assertText('Third choice', 'Third choice remains.');
- }
- }
- class PollTranslateTestCase extends PollTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Poll translation',
- 'description' => 'Test the poll translation logic.',
- 'group' => 'Poll',
- );
- }
- function setUp() {
- parent::setUp('poll', 'translation');
- }
-
- function testPollTranslate() {
- $admin_user = $this->drupalCreateUser(array('administer content types', 'administer languages', 'edit any poll content', 'create poll content', 'administer nodes', 'translate content'));
-
- $title = $this->randomName();
- $choices = array($this->randomName(), $this->randomName());
- $poll_nid = $this->pollCreate($title, $choices, FALSE);
- $this->assertTrue($poll_nid, 'Poll for translation logic test created.');
- $this->drupalLogout();
- $this->drupalLogin($admin_user);
-
- $this->drupalGet('admin/config/regional/language');
- $edit = array();
- $edit['langcode'] = 'nl';
- $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
- $this->assertRaw(t('The language %language has been created and can now be used.', array('%language' => 'Dutch')), 'Language Dutch has been created.');
-
- $this->drupalGet('admin/structure/types/manage/poll');
- $edit = array();
- $edit['language_content_type'] = 2;
- $this->drupalPost('admin/structure/types/manage/poll', $edit, t('Save content type'));
- $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Poll')), 'Poll content type has been updated.');
-
- $this->drupalGet("node/$poll_nid/edit");
- $edit = array();
-
- $edit['choice[chid:1][chvotes]'] = 200;
-
- $edit['language'] = 'nl';
- $this->drupalPost(NULL, $edit, t('Save'));
-
- $this->drupalGet('node/add/poll', array('query' => array('translation' => $poll_nid, 'target' => 'en')));
- $dutch_poll = node_load($poll_nid);
-
-
- $this->assertFieldByName('choice[chid:1][chvotes]', '0', ('Found choice with vote count 0'));
- $this->assertFieldByName('choice[chid:2][chvotes]', '0', ('Found choice with vote count 0'));
-
- $this->assertFieldByName('choice[chid:1][chtext]', $dutch_poll->choice[1]['chtext'], format_string('Found choice with text @text', array('@text' => $dutch_poll->choice[1]['chtext'])));
- $this->assertFieldByName('choice[chid:2][chtext]', $dutch_poll->choice[2]['chtext'], format_string('Found choice with text @text', array('@text' => $dutch_poll->choice[2]['chtext'])));
- }
- }
|