poll.test 4.12 KB
Newer Older
1 2 3 4 5
<?php
// $Id$

class PollTestCase extends DrupalWebTestCase {

6 7 8 9 10 11 12 13 14 15 16
  /**
   * 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');

17 18
    $web_user = $this->drupalCreateUser(array('create poll content', 'access content'));
    $this->drupalLogin($web_user);
19 20 21

    // Get the form first to initialize the state of the internal browser
    $this->drupalGet('node/add/poll');
22

23 24 25 26 27 28 29 30 31 32 33 34
    // 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) {
35
      $this->drupalPost(NULL, $edit, t('Preview'));
36
      foreach($choices as $k => $choice_text) {
37
        $this->assertRaw($choice_text, t('Choice @choice found was in preview.', array('@choice' => $k)));
38
      }
39
      list($edit, $index) = $this->_pollGenerateEdit($title, $choices, $index);
40
    }
41

42 43 44
    $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.');
45 46 47
    $this->assertTrue($node->nid, t('Poll has been found in the database'));

    return isset($node->nid) ? $node->nid : FALSE;
48 49
  }

50 51 52 53
  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));
54

55 56 57 58 59 60 61 62 63 64 65
    $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));
  }
66

67 68 69 70 71 72 73
  function _generateChoices($count = 7) {
    $choices = array();
    for($i = 1; $i <= $count; $i++) {
      $choices[] = $this->randomName();
    }
    return $choices;
  }
74 75 76 77 78 79 80 81 82 83 84 85
}

class PollCreateTestCase extends PollTestCase {
  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() {
86 87 88
    $title = $this->randomName();
    $choices = $this->_generateChoices(7);
    $this->pollCreate($title, $choices, TRUE);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  }
}

class PollVoteTestCase extends PollTestCase {
  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() {
106 107 108
    $title = $this->randomName();
    $choices = $this->_generateChoices(7);
    $poll_nid = $this->pollCreate($title, $choices, FALSE);
109
    $this->drupalLogout();
110

111 112
    $web_user = $this->drupalCreateUser(array('cancel own vote', 'inspect all votes', 'vote on polls', 'access content'));
    $this->drupalLogin($web_user);
113

114
    // Record a vote for the first choice.
115 116 117
    $edit = array (
      'choice' => '1',
    );
118
    $this->drupalPost('node/'. $poll_nid, $edit, t('Vote'));
119
    $this->assertText('Your vote was recorded.', 'Your vote was recorded.');
120 121

    $this->drupalGet("node/$poll_nid/votes");
122
    $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.');
123
    $this->assertText($choices[0], 'Vote recorded');
124 125
  }
}
126