annotate modules/poll/poll.test @ 8:49b8ebaaad78

Added all the needed modules
author danieleb <danielebarchiesi@me.com>
date Fri, 20 Sep 2013 11:24:36 +0100
parents ff03f76ab3fe
children
rev   line source
danielebarchiesi@0 1 <?php
danielebarchiesi@0 2
danielebarchiesi@0 3 /**
danielebarchiesi@0 4 * @file
danielebarchiesi@0 5 * Tests for poll.module.
danielebarchiesi@0 6 */
danielebarchiesi@0 7
danielebarchiesi@0 8 class PollTestCase extends DrupalWebTestCase {
danielebarchiesi@0 9
danielebarchiesi@0 10 /**
danielebarchiesi@0 11 * Creates a poll.
danielebarchiesi@0 12 *
danielebarchiesi@0 13 * @param string $title
danielebarchiesi@0 14 * The title of the poll.
danielebarchiesi@0 15 * @param array $choices
danielebarchiesi@0 16 * A list of choice labels.
danielebarchiesi@0 17 * @param boolean $preview
danielebarchiesi@0 18 * (optional) Whether to test if the preview is working or not. Defaults to
danielebarchiesi@0 19 * TRUE.
danielebarchiesi@0 20 *
danielebarchiesi@0 21 * @return
danielebarchiesi@0 22 * The node id of the created poll, or FALSE on error.
danielebarchiesi@0 23 */
danielebarchiesi@0 24 function pollCreate($title, $choices, $preview = TRUE) {
danielebarchiesi@0 25 $this->assertTrue(TRUE, 'Create a poll');
danielebarchiesi@0 26
danielebarchiesi@0 27 $admin_user = $this->drupalCreateUser(array('create poll content', 'administer nodes'));
danielebarchiesi@0 28 $web_user = $this->drupalCreateUser(array('create poll content', 'access content', 'edit own poll content'));
danielebarchiesi@0 29 $this->drupalLogin($admin_user);
danielebarchiesi@0 30
danielebarchiesi@0 31 // Get the form first to initialize the state of the internal browser.
danielebarchiesi@0 32 $this->drupalGet('node/add/poll');
danielebarchiesi@0 33
danielebarchiesi@0 34 // Prepare a form with two choices.
danielebarchiesi@0 35 list($edit, $index) = $this->_pollGenerateEdit($title, $choices);
danielebarchiesi@0 36
danielebarchiesi@0 37 // Verify that the vote count element only allows non-negative integers.
danielebarchiesi@0 38 $edit['choice[new:1][chvotes]'] = -1;
danielebarchiesi@0 39 $edit['choice[new:0][chvotes]'] = $this->randomString(7);
danielebarchiesi@0 40 $this->drupalPost(NULL, $edit, t('Save'));
danielebarchiesi@0 41 $this->assertText(t('Negative values are not allowed.'));
danielebarchiesi@0 42 $this->assertText(t('Vote count for new choice must be an integer.'));
danielebarchiesi@0 43
danielebarchiesi@0 44 // Repeat steps for initializing the state of the internal browser.
danielebarchiesi@0 45 $this->drupalLogin($web_user);
danielebarchiesi@0 46 $this->drupalGet('node/add/poll');
danielebarchiesi@0 47 list($edit, $index) = $this->_pollGenerateEdit($title, $choices);
danielebarchiesi@0 48
danielebarchiesi@0 49 // Re-submit the form until all choices are filled in.
danielebarchiesi@0 50 if (count($choices) > 2) {
danielebarchiesi@0 51 while ($index < count($choices)) {
danielebarchiesi@0 52 $this->drupalPost(NULL, $edit, t('More choices'));
danielebarchiesi@0 53 $this->assertPollChoiceOrder($choices, $index);
danielebarchiesi@0 54 list($edit, $index) = $this->_pollGenerateEdit($title, $choices, $index);
danielebarchiesi@0 55 }
danielebarchiesi@0 56 }
danielebarchiesi@0 57
danielebarchiesi@0 58 if ($preview) {
danielebarchiesi@0 59 $this->drupalPost(NULL, $edit, t('Preview'));
danielebarchiesi@0 60 $this->assertPollChoiceOrder($choices, $index, TRUE);
danielebarchiesi@0 61 list($edit, $index) = $this->_pollGenerateEdit($title, $choices, $index);
danielebarchiesi@0 62 }
danielebarchiesi@0 63
danielebarchiesi@0 64 $this->drupalPost(NULL, $edit, t('Save'));
danielebarchiesi@0 65 $node = $this->drupalGetNodeByTitle($title);
danielebarchiesi@0 66 $this->assertText(t('@type @title has been created.', array('@type' => node_type_get_name('poll'), '@title' => $title)), 'Poll has been created.');
danielebarchiesi@0 67 $this->assertTrue($node->nid, 'Poll has been found in the database.');
danielebarchiesi@0 68
danielebarchiesi@0 69 return isset($node->nid) ? $node->nid : FALSE;
danielebarchiesi@0 70 }
danielebarchiesi@0 71
danielebarchiesi@0 72 /**
danielebarchiesi@0 73 * Generates POST values for the poll node form, specifically poll choices.
danielebarchiesi@0 74 *
danielebarchiesi@0 75 * @param $title
danielebarchiesi@0 76 * The title for the poll node.
danielebarchiesi@0 77 * @param $choices
danielebarchiesi@0 78 * An array containing poll choices, as generated by
danielebarchiesi@0 79 * PollTestCase::_generateChoices().
danielebarchiesi@0 80 * @param $index
danielebarchiesi@0 81 * (optional) The amount/number of already submitted poll choices. Defaults
danielebarchiesi@0 82 * to 0.
danielebarchiesi@0 83 *
danielebarchiesi@0 84 * @return
danielebarchiesi@0 85 * An indexed array containing:
danielebarchiesi@0 86 * - The generated POST values, suitable for
danielebarchiesi@0 87 * DrupalWebTestCase::drupalPost().
danielebarchiesi@0 88 * - The number of poll choices contained in 'edit', for potential re-usage
danielebarchiesi@0 89 * in subsequent invocations of this function.
danielebarchiesi@0 90 */
danielebarchiesi@0 91 function _pollGenerateEdit($title, array $choices, $index = 0) {
danielebarchiesi@0 92 $max_new_choices = ($index == 0 ? 2 : 5);
danielebarchiesi@0 93 $already_submitted_choices = array_slice($choices, 0, $index);
danielebarchiesi@0 94 $new_choices = array_values(array_slice($choices, $index, $max_new_choices));
danielebarchiesi@0 95
danielebarchiesi@0 96 $edit = array(
danielebarchiesi@0 97 'title' => $title,
danielebarchiesi@0 98 );
danielebarchiesi@0 99 foreach ($already_submitted_choices as $k => $text) {
danielebarchiesi@0 100 $edit['choice[chid:' . $k . '][chtext]'] = $text;
danielebarchiesi@0 101 }
danielebarchiesi@0 102 foreach ($new_choices as $k => $text) {
danielebarchiesi@0 103 $edit['choice[new:' . $k . '][chtext]'] = $text;
danielebarchiesi@0 104 }
danielebarchiesi@0 105 return array($edit, count($already_submitted_choices) + count($new_choices));
danielebarchiesi@0 106 }
danielebarchiesi@0 107
danielebarchiesi@0 108 function _generateChoices($count = 7) {
danielebarchiesi@0 109 $choices = array();
danielebarchiesi@0 110 for ($i = 1; $i <= $count; $i++) {
danielebarchiesi@0 111 $choices[] = $this->randomName();
danielebarchiesi@0 112 }
danielebarchiesi@0 113 return $choices;
danielebarchiesi@0 114 }
danielebarchiesi@0 115
danielebarchiesi@0 116 /**
danielebarchiesi@0 117 * Assert correct poll choice order in the node form after submission.
danielebarchiesi@0 118 *
danielebarchiesi@0 119 * Verifies both the order in the DOM and in the 'weight' form elements.
danielebarchiesi@0 120 *
danielebarchiesi@0 121 * @param $choices
danielebarchiesi@0 122 * An array containing poll choices, as generated by
danielebarchiesi@0 123 * PollTestCase::_generateChoices().
danielebarchiesi@0 124 * @param $index
danielebarchiesi@0 125 * (optional) The amount/number of already submitted poll choices. Defaults
danielebarchiesi@0 126 * to 0.
danielebarchiesi@0 127 * @param $preview
danielebarchiesi@0 128 * (optional) Whether to also check the poll preview.
danielebarchiesi@0 129 *
danielebarchiesi@0 130 * @see PollTestCase::_pollGenerateEdit()
danielebarchiesi@0 131 */
danielebarchiesi@0 132 function assertPollChoiceOrder(array $choices, $index = 0, $preview = FALSE) {
danielebarchiesi@0 133 $expected = array();
danielebarchiesi@0 134 $weight = 0;
danielebarchiesi@0 135 foreach ($choices as $id => $label) {
danielebarchiesi@0 136 if ($id < $index) {
danielebarchiesi@0 137 // The expected weight of each choice is higher than the previous one.
danielebarchiesi@0 138 $weight++;
danielebarchiesi@0 139 // Directly assert the weight form element value for this choice.
danielebarchiesi@0 140 $this->assertFieldByName('choice[chid:' . $id . '][weight]', $weight, format_string('Found choice @id with weight @weight.', array(
danielebarchiesi@0 141 '@id' => $id,
danielebarchiesi@0 142 '@weight' => $weight,
danielebarchiesi@0 143 )));
danielebarchiesi@0 144 // Append to our (to be reversed) stack of labels.
danielebarchiesi@0 145 $expected[$weight] = $label;
danielebarchiesi@0 146 }
danielebarchiesi@0 147 }
danielebarchiesi@0 148 ksort($expected);
danielebarchiesi@0 149
danielebarchiesi@0 150 // Verify DOM order of poll choices (i.e., #weight of form elements).
danielebarchiesi@0 151 $elements = $this->xpath('//input[starts-with(@name, :prefix) and contains(@name, :suffix)]', array(
danielebarchiesi@0 152 ':prefix' => 'choice[chid:',
danielebarchiesi@0 153 ':suffix' => '][chtext]',
danielebarchiesi@0 154 ));
danielebarchiesi@0 155 $expected_order = $expected;
danielebarchiesi@0 156 foreach ($elements as $element) {
danielebarchiesi@0 157 $next_label = array_shift($expected_order);
danielebarchiesi@0 158 $this->assertEqual((string) $element['value'], $next_label);
danielebarchiesi@0 159 }
danielebarchiesi@0 160
danielebarchiesi@0 161 // If requested, also verify DOM order in preview.
danielebarchiesi@0 162 if ($preview) {
danielebarchiesi@0 163 $elements = $this->xpath('//div[contains(@class, :teaser)]/descendant::div[@class=:text]', array(
danielebarchiesi@0 164 ':teaser' => 'node-teaser',
danielebarchiesi@0 165 ':text' => 'text',
danielebarchiesi@0 166 ));
danielebarchiesi@0 167 $expected_order = $expected;
danielebarchiesi@0 168 foreach ($elements as $element) {
danielebarchiesi@0 169 $next_label = array_shift($expected_order);
danielebarchiesi@0 170 $this->assertEqual((string) $element, $next_label, format_string('Found choice @label in preview.', array(
danielebarchiesi@0 171 '@label' => $next_label,
danielebarchiesi@0 172 )));
danielebarchiesi@0 173 }
danielebarchiesi@0 174 }
danielebarchiesi@0 175 }
danielebarchiesi@0 176
danielebarchiesi@0 177 function pollUpdate($nid, $title, $edit) {
danielebarchiesi@0 178 // Edit the poll node.
danielebarchiesi@0 179 $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
danielebarchiesi@0 180 $this->assertText(t('@type @title has been updated.', array('@type' => node_type_get_name('poll'), '@title' => $title)), 'Poll has been updated.');
danielebarchiesi@0 181 }
danielebarchiesi@0 182 }
danielebarchiesi@0 183
danielebarchiesi@0 184 class PollCreateTestCase extends PollTestCase {
danielebarchiesi@0 185 public static function getInfo() {
danielebarchiesi@0 186 return array(
danielebarchiesi@0 187 'name' => 'Poll create',
danielebarchiesi@0 188 'description' => 'Adds "more choices", previews and creates a poll.',
danielebarchiesi@0 189 'group' => 'Poll'
danielebarchiesi@0 190 );
danielebarchiesi@0 191 }
danielebarchiesi@0 192
danielebarchiesi@0 193 function setUp() {
danielebarchiesi@0 194 parent::setUp('poll');
danielebarchiesi@0 195 }
danielebarchiesi@0 196
danielebarchiesi@0 197 function testPollCreate() {
danielebarchiesi@0 198 $title = $this->randomName();
danielebarchiesi@0 199 $choices = $this->_generateChoices(7);
danielebarchiesi@0 200 $poll_nid = $this->pollCreate($title, $choices, TRUE);
danielebarchiesi@0 201
danielebarchiesi@0 202 // Verify poll appears on 'poll' page.
danielebarchiesi@0 203 $this->drupalGet('poll');
danielebarchiesi@0 204 $this->assertText($title, 'Poll appears in poll list.');
danielebarchiesi@0 205 $this->assertText('open', 'Poll is active.');
danielebarchiesi@0 206
danielebarchiesi@0 207 // Click on the poll title to go to node page.
danielebarchiesi@0 208 $this->clickLink($title);
danielebarchiesi@0 209 $this->assertText('Total votes: 0', 'Link to poll correct.');
danielebarchiesi@0 210
danielebarchiesi@0 211 // Now add a new option to make sure that when we update the node the
danielebarchiesi@0 212 // option is displayed.
danielebarchiesi@0 213 $node = node_load($poll_nid);
danielebarchiesi@0 214
danielebarchiesi@0 215 $new_option = $this->randomName();
danielebarchiesi@0 216
danielebarchiesi@0 217 $vote_count = '2000';
danielebarchiesi@0 218 $node->choice[] = array(
danielebarchiesi@0 219 'chid' => '',
danielebarchiesi@0 220 'chtext' => $new_option,
danielebarchiesi@0 221 'chvotes' => (int) $vote_count,
danielebarchiesi@0 222 'weight' => 1000,
danielebarchiesi@0 223 );
danielebarchiesi@0 224
danielebarchiesi@0 225 node_save($node);
danielebarchiesi@0 226
danielebarchiesi@0 227 $this->drupalGet('poll');
danielebarchiesi@0 228 $this->clickLink($title);
danielebarchiesi@0 229 $this->assertText($new_option, 'New option found.');
danielebarchiesi@0 230
danielebarchiesi@0 231 $option = $this->xpath('//div[@id="node-1"]//div[@class="poll"]//div[@class="text"]');
danielebarchiesi@0 232 $this->assertEqual(end($option), $new_option, 'Last item is equal to new option.');
danielebarchiesi@0 233
danielebarchiesi@0 234 $votes = $this->xpath('//div[@id="node-1"]//div[@class="poll"]//div[@class="percent"]');
danielebarchiesi@0 235 $this->assertTrue(strpos(end($votes), $vote_count) > 0, "Votes saved.");
danielebarchiesi@0 236 }
danielebarchiesi@0 237
danielebarchiesi@0 238 function testPollClose() {
danielebarchiesi@0 239 $content_user = $this->drupalCreateUser(array('create poll content', 'edit any poll content', 'access content'));
danielebarchiesi@0 240 $vote_user = $this->drupalCreateUser(array('cancel own vote', 'inspect all votes', 'vote on polls', 'access content'));
danielebarchiesi@0 241
danielebarchiesi@0 242 // Create poll.
danielebarchiesi@0 243 $title = $this->randomName();
danielebarchiesi@0 244 $choices = $this->_generateChoices(7);
danielebarchiesi@0 245 $poll_nid = $this->pollCreate($title, $choices, FALSE);
danielebarchiesi@0 246
danielebarchiesi@0 247 $this->drupalLogout();
danielebarchiesi@0 248 $this->drupalLogin($content_user);
danielebarchiesi@0 249
danielebarchiesi@0 250 // Edit the poll node and close the poll.
danielebarchiesi@0 251 $close_edit = array('active' => 0);
danielebarchiesi@0 252 $this->pollUpdate($poll_nid, $title, $close_edit);
danielebarchiesi@0 253
danielebarchiesi@0 254 // Verify 'Vote' button no longer appears.
danielebarchiesi@0 255 $this->drupalGet('node/' . $poll_nid);
danielebarchiesi@0 256 $elements = $this->xpath('//input[@id="edit-vote"]');
danielebarchiesi@0 257 $this->assertTrue(empty($elements), "Vote button doesn't appear.");
danielebarchiesi@0 258
danielebarchiesi@0 259 // Verify status on 'poll' page is 'closed'.
danielebarchiesi@0 260 $this->drupalGet('poll');
danielebarchiesi@0 261 $this->assertText($title, 'Poll appears in poll list.');
danielebarchiesi@0 262 $this->assertText('closed', 'Poll is closed.');
danielebarchiesi@0 263
danielebarchiesi@0 264 // Edit the poll node and re-activate.
danielebarchiesi@0 265 $open_edit = array('active' => 1);
danielebarchiesi@0 266 $this->pollUpdate($poll_nid, $title, $open_edit);
danielebarchiesi@0 267
danielebarchiesi@0 268 // Vote on the poll.
danielebarchiesi@0 269 $this->drupalLogout();
danielebarchiesi@0 270 $this->drupalLogin($vote_user);
danielebarchiesi@0 271 $vote_edit = array('choice' => '1');
danielebarchiesi@0 272 $this->drupalPost('node/' . $poll_nid, $vote_edit, t('Vote'));
danielebarchiesi@0 273 $this->assertText('Your vote was recorded.', 'Your vote was recorded.');
danielebarchiesi@0 274 $elements = $this->xpath('//input[@value="Cancel your vote"]');
danielebarchiesi@0 275 $this->assertTrue(isset($elements[0]), "'Cancel your vote' button appears.");
danielebarchiesi@0 276
danielebarchiesi@0 277 // Edit the poll node and close the poll.
danielebarchiesi@0 278 $this->drupalLogout();
danielebarchiesi@0 279 $this->drupalLogin($content_user);
danielebarchiesi@0 280 $close_edit = array('active' => 0);
danielebarchiesi@0 281 $this->pollUpdate($poll_nid, $title, $close_edit);
danielebarchiesi@0 282
danielebarchiesi@0 283 // Verify 'Cancel your vote' button no longer appears.
danielebarchiesi@0 284 $this->drupalGet('node/' . $poll_nid);
danielebarchiesi@0 285 $elements = $this->xpath('//input[@value="Cancel your vote"]');
danielebarchiesi@0 286 $this->assertTrue(empty($elements), "'Cancel your vote' button no longer appears.");
danielebarchiesi@0 287 }
danielebarchiesi@0 288 }
danielebarchiesi@0 289
danielebarchiesi@0 290 class PollVoteTestCase extends PollTestCase {
danielebarchiesi@0 291 public static function getInfo() {
danielebarchiesi@0 292 return array(
danielebarchiesi@0 293 'name' => 'Poll vote',
danielebarchiesi@0 294 'description' => 'Vote on a poll',
danielebarchiesi@0 295 'group' => 'Poll'
danielebarchiesi@0 296 );
danielebarchiesi@0 297 }
danielebarchiesi@0 298
danielebarchiesi@0 299 function setUp() {
danielebarchiesi@0 300 parent::setUp('poll');
danielebarchiesi@0 301 }
danielebarchiesi@0 302
danielebarchiesi@0 303 function tearDown() {
danielebarchiesi@0 304 parent::tearDown();
danielebarchiesi@0 305 }
danielebarchiesi@0 306
danielebarchiesi@0 307 function testPollVote() {
danielebarchiesi@0 308 $title = $this->randomName();
danielebarchiesi@0 309 $choices = $this->_generateChoices(7);
danielebarchiesi@0 310 $poll_nid = $this->pollCreate($title, $choices, FALSE);
danielebarchiesi@0 311 $this->drupalLogout();
danielebarchiesi@0 312
danielebarchiesi@0 313 $vote_user = $this->drupalCreateUser(array('cancel own vote', 'inspect all votes', 'vote on polls', 'access content'));
danielebarchiesi@0 314 $restricted_vote_user = $this->drupalCreateUser(array('vote on polls', 'access content'));
danielebarchiesi@0 315
danielebarchiesi@0 316 $this->drupalLogin($vote_user);
danielebarchiesi@0 317
danielebarchiesi@0 318 // Record a vote for the first choice.
danielebarchiesi@0 319 $edit = array(
danielebarchiesi@0 320 'choice' => '1',
danielebarchiesi@0 321 );
danielebarchiesi@0 322 $this->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
danielebarchiesi@0 323 $this->assertText('Your vote was recorded.', 'Your vote was recorded.');
danielebarchiesi@0 324 $this->assertText('Total votes: 1', 'Vote count updated correctly.');
danielebarchiesi@0 325 $elements = $this->xpath('//input[@value="Cancel your vote"]');
danielebarchiesi@0 326 $this->assertTrue(isset($elements[0]), "'Cancel your vote' button appears.");
danielebarchiesi@0 327
danielebarchiesi@0 328 $this->drupalGet("node/$poll_nid/votes");
danielebarchiesi@0 329 $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.');
danielebarchiesi@0 330 $this->assertText($choices[0], 'Vote recorded');
danielebarchiesi@0 331
danielebarchiesi@0 332 // Ensure poll listing page has correct number of votes.
danielebarchiesi@0 333 $this->drupalGet('poll');
danielebarchiesi@0 334 $this->assertText($title, 'Poll appears in poll list.');
danielebarchiesi@0 335 $this->assertText('1 vote', 'Poll has 1 vote.');
danielebarchiesi@0 336
danielebarchiesi@0 337 // Cancel a vote.
danielebarchiesi@0 338 $this->drupalPost('node/' . $poll_nid, array(), t('Cancel your vote'));
danielebarchiesi@0 339 $this->assertText('Your vote was cancelled.', 'Your vote was cancelled.');
danielebarchiesi@0 340 $this->assertNoText('Cancel your vote', "Cancel vote button doesn't appear.");
danielebarchiesi@0 341
danielebarchiesi@0 342 $this->drupalGet("node/$poll_nid/votes");
danielebarchiesi@0 343 $this->assertNoText($choices[0], 'Vote cancelled');
danielebarchiesi@0 344
danielebarchiesi@0 345 // Ensure poll listing page has correct number of votes.
danielebarchiesi@0 346 $this->drupalGet('poll');
danielebarchiesi@0 347 $this->assertText($title, 'Poll appears in poll list.');
danielebarchiesi@0 348 $this->assertText('0 votes', 'Poll has 0 votes.');
danielebarchiesi@0 349
danielebarchiesi@0 350 // Log in as a user who can only vote on polls.
danielebarchiesi@0 351 $this->drupalLogout();
danielebarchiesi@0 352 $this->drupalLogin($restricted_vote_user);
danielebarchiesi@0 353
danielebarchiesi@0 354 // Vote on a poll.
danielebarchiesi@0 355 $edit = array(
danielebarchiesi@0 356 'choice' => '1',
danielebarchiesi@0 357 );
danielebarchiesi@0 358 $this->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
danielebarchiesi@0 359 $this->assertText('Your vote was recorded.', 'Your vote was recorded.');
danielebarchiesi@0 360 $this->assertText('Total votes: 1', 'Vote count updated correctly.');
danielebarchiesi@0 361 $elements = $this->xpath('//input[@value="Cancel your vote"]');
danielebarchiesi@0 362 $this->assertTrue(empty($elements), "'Cancel your vote' button does not appear.");
danielebarchiesi@0 363 }
danielebarchiesi@0 364 }
danielebarchiesi@0 365
danielebarchiesi@0 366 class PollBlockTestCase extends PollTestCase {
danielebarchiesi@0 367 public static function getInfo() {
danielebarchiesi@0 368 return array(
danielebarchiesi@0 369 'name' => 'Block availability',
danielebarchiesi@0 370 'description' => 'Check if the most recent poll block is available.',
danielebarchiesi@0 371 'group' => 'Poll',
danielebarchiesi@0 372 );
danielebarchiesi@0 373 }
danielebarchiesi@0 374
danielebarchiesi@0 375 function setUp() {
danielebarchiesi@0 376 parent::setUp('poll');
danielebarchiesi@0 377
danielebarchiesi@0 378 // Create and login user
danielebarchiesi@0 379 $admin_user = $this->drupalCreateUser(array('administer blocks'));
danielebarchiesi@0 380 $this->drupalLogin($admin_user);
danielebarchiesi@0 381 }
danielebarchiesi@0 382
danielebarchiesi@0 383 function testRecentBlock() {
danielebarchiesi@0 384 // Set block title to confirm that the interface is available.
danielebarchiesi@0 385 $this->drupalPost('admin/structure/block/manage/poll/recent/configure', array('title' => $this->randomName(8)), t('Save block'));
danielebarchiesi@0 386 $this->assertText(t('The block configuration has been saved.'), 'Block configuration set.');
danielebarchiesi@0 387
danielebarchiesi@0 388 // Set the block to a region to confirm block is available.
danielebarchiesi@0 389 $edit = array();
danielebarchiesi@0 390 $edit['blocks[poll_recent][region]'] = 'footer';
danielebarchiesi@0 391 $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
danielebarchiesi@0 392 $this->assertText(t('The block settings have been updated.'), 'Block successfully move to footer region.');
danielebarchiesi@0 393
danielebarchiesi@0 394 // Create a poll which should appear in recent polls block.
danielebarchiesi@0 395 $title = $this->randomName();
danielebarchiesi@0 396 $choices = $this->_generateChoices(7);
danielebarchiesi@0 397 $poll_nid = $this->pollCreate($title, $choices, TRUE);
danielebarchiesi@0 398
danielebarchiesi@0 399 // Verify poll appears in a block.
danielebarchiesi@0 400 // View user page so we're not matching the poll node on front page.
danielebarchiesi@0 401 $this->drupalGet('user');
danielebarchiesi@0 402 // If a 'block' view not generated, this title would not appear even though
danielebarchiesi@0 403 // the choices might.
danielebarchiesi@0 404 $this->assertText($title, 'Poll appears in block.');
danielebarchiesi@0 405
danielebarchiesi@0 406 // Logout and login back in as a user who can vote.
danielebarchiesi@0 407 $this->drupalLogout();
danielebarchiesi@0 408 $vote_user = $this->drupalCreateUser(array('cancel own vote', 'inspect all votes', 'vote on polls', 'access content'));
danielebarchiesi@0 409 $this->drupalLogin($vote_user);
danielebarchiesi@0 410
danielebarchiesi@0 411 // Verify we can vote via the block.
danielebarchiesi@0 412 $edit = array(
danielebarchiesi@0 413 'choice' => '1',
danielebarchiesi@0 414 );
danielebarchiesi@0 415 $this->drupalPost('user/' . $vote_user->uid, $edit, t('Vote'));
danielebarchiesi@0 416 $this->assertText('Your vote was recorded.', 'Your vote was recorded.');
danielebarchiesi@0 417 $this->assertText('Total votes: 1', 'Vote count updated correctly.');
danielebarchiesi@0 418 $this->assertText('Older polls', 'Link to older polls appears.');
danielebarchiesi@0 419 $this->clickLink('Older polls');
danielebarchiesi@0 420 $this->assertText('1 vote - open', 'Link to poll listing correct.');
danielebarchiesi@0 421
danielebarchiesi@0 422 // Close the poll and verify block doesn't appear.
danielebarchiesi@0 423 $content_user = $this->drupalCreateUser(array('create poll content', 'edit any poll content', 'access content'));
danielebarchiesi@0 424 $this->drupalLogout();
danielebarchiesi@0 425 $this->drupalLogin($content_user);
danielebarchiesi@0 426 $close_edit = array('active' => 0);
danielebarchiesi@0 427 $this->pollUpdate($poll_nid, $title, $close_edit);
danielebarchiesi@0 428 $this->drupalGet('user/' . $content_user->uid);
danielebarchiesi@0 429 $this->assertNoText($title, 'Poll no longer appears in block.');
danielebarchiesi@0 430 }
danielebarchiesi@0 431 }
danielebarchiesi@0 432
danielebarchiesi@0 433 /**
danielebarchiesi@0 434 * Test adding new choices.
danielebarchiesi@0 435 */
danielebarchiesi@0 436 class PollJSAddChoice extends DrupalWebTestCase {
danielebarchiesi@0 437
danielebarchiesi@0 438 public static function getInfo() {
danielebarchiesi@0 439 return array(
danielebarchiesi@0 440 'name' => 'Poll add choice',
danielebarchiesi@0 441 'description' => 'Submits a POST request for an additional poll choice.',
danielebarchiesi@0 442 'group' => 'Poll'
danielebarchiesi@0 443 );
danielebarchiesi@0 444 }
danielebarchiesi@0 445
danielebarchiesi@0 446 function setUp() {
danielebarchiesi@0 447 parent::setUp('poll');
danielebarchiesi@0 448 }
danielebarchiesi@0 449
danielebarchiesi@0 450 /**
danielebarchiesi@0 451 * Test adding a new choice.
danielebarchiesi@0 452 */
danielebarchiesi@0 453 function testAddChoice() {
danielebarchiesi@0 454 $web_user = $this->drupalCreateUser(array('create poll content', 'access content'));
danielebarchiesi@0 455 $this->drupalLogin($web_user);
danielebarchiesi@0 456 $this->drupalGet('node/add/poll');
danielebarchiesi@0 457 $edit = array(
danielebarchiesi@0 458 "title" => $this->randomName(),
danielebarchiesi@0 459 'choice[new:0][chtext]' => $this->randomName(),
danielebarchiesi@0 460 'choice[new:1][chtext]' => $this->randomName(),
danielebarchiesi@0 461 );
danielebarchiesi@0 462
danielebarchiesi@0 463 // Press 'add choice' button through Ajax, and place the expected HTML result
danielebarchiesi@0 464 // as the tested content.
danielebarchiesi@0 465 $commands = $this->drupalPostAJAX(NULL, $edit, array('op' => t('More choices')));
danielebarchiesi@0 466 $this->content = $commands[1]['data'];
danielebarchiesi@0 467
danielebarchiesi@0 468 $this->assertFieldByName('choice[chid:0][chtext]', $edit['choice[new:0][chtext]'], format_string('Field !i found', array('!i' => 0)));
danielebarchiesi@0 469 $this->assertFieldByName('choice[chid:1][chtext]', $edit['choice[new:1][chtext]'], format_string('Field !i found', array('!i' => 1)));
danielebarchiesi@0 470 $this->assertFieldByName('choice[new:0][chtext]', '', format_string('Field !i found', array('!i' => 2)));
danielebarchiesi@0 471 }
danielebarchiesi@0 472 }
danielebarchiesi@0 473
danielebarchiesi@0 474 class PollVoteCheckHostname extends PollTestCase {
danielebarchiesi@0 475 public static function getInfo() {
danielebarchiesi@0 476 return array(
danielebarchiesi@0 477 'name' => 'User poll vote capability.',
danielebarchiesi@0 478 'description' => 'Check that users and anonymous users from specified ip-address can only vote once.',
danielebarchiesi@0 479 'group' => 'Poll'
danielebarchiesi@0 480 );
danielebarchiesi@0 481 }
danielebarchiesi@0 482
danielebarchiesi@0 483 function setUp() {
danielebarchiesi@0 484 parent::setUp('poll');
danielebarchiesi@0 485
danielebarchiesi@0 486 // Create and login user.
danielebarchiesi@0 487 $this->admin_user = $this->drupalCreateUser(array('administer permissions', 'create poll content'));
danielebarchiesi@0 488 $this->drupalLogin($this->admin_user);
danielebarchiesi@0 489
danielebarchiesi@0 490 // Allow anonymous users to vote on polls.
danielebarchiesi@0 491 user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
danielebarchiesi@0 492 'access content' => TRUE,
danielebarchiesi@0 493 'vote on polls' => TRUE,
danielebarchiesi@0 494 'cancel own vote' => TRUE,
danielebarchiesi@0 495 ));
danielebarchiesi@0 496
danielebarchiesi@0 497 // Enable page cache to verify that the result page is not saved in the
danielebarchiesi@0 498 // cache when anonymous voting is allowed.
danielebarchiesi@0 499 variable_set('cache', 1);
danielebarchiesi@0 500
danielebarchiesi@0 501 // Create poll.
danielebarchiesi@0 502 $title = $this->randomName();
danielebarchiesi@0 503 $choices = $this->_generateChoices(3);
danielebarchiesi@0 504 $this->poll_nid = $this->pollCreate($title, $choices, FALSE);
danielebarchiesi@0 505
danielebarchiesi@0 506 $this->drupalLogout();
danielebarchiesi@0 507
danielebarchiesi@0 508 // Create web users.
danielebarchiesi@0 509 $this->web_user1 = $this->drupalCreateUser(array('access content', 'vote on polls', 'cancel own vote'));
danielebarchiesi@0 510 $this->web_user2 = $this->drupalCreateUser(array('access content', 'vote on polls'));
danielebarchiesi@0 511 }
danielebarchiesi@0 512
danielebarchiesi@0 513 /**
danielebarchiesi@0 514 * Check that anonymous users with same ip cannot vote on poll more than once
danielebarchiesi@0 515 * unless user is logged in.
danielebarchiesi@0 516 */
danielebarchiesi@0 517 function testHostnamePollVote() {
danielebarchiesi@0 518 // Login User1.
danielebarchiesi@0 519 $this->drupalLogin($this->web_user1);
danielebarchiesi@0 520
danielebarchiesi@0 521 $edit = array(
danielebarchiesi@0 522 'choice' => '1',
danielebarchiesi@0 523 );
danielebarchiesi@0 524
danielebarchiesi@0 525 // User1 vote on Poll.
danielebarchiesi@0 526 $this->drupalPost('node/' . $this->poll_nid, $edit, t('Vote'));
danielebarchiesi@0 527 $this->assertText(t('Your vote was recorded.'), format_string('%user vote was recorded.', array('%user' => $this->web_user1->name)));
danielebarchiesi@0 528 $this->assertText(t('Total votes: @votes', array('@votes' => 1)), 'Vote count updated correctly.');
danielebarchiesi@0 529
danielebarchiesi@0 530 // Check to make sure User1 cannot vote again.
danielebarchiesi@0 531 $this->drupalGet('node/' . $this->poll_nid);
danielebarchiesi@0 532 $elements = $this->xpath('//input[@value="Vote"]');
danielebarchiesi@0 533 $this->assertTrue(empty($elements), format_string("%user is not able to vote again.", array('%user' => $this->web_user1->name)));
danielebarchiesi@0 534 $elements = $this->xpath('//input[@value="Cancel your vote"]');
danielebarchiesi@0 535 $this->assertTrue(!empty($elements), "'Cancel your vote' button appears.");
danielebarchiesi@0 536
danielebarchiesi@0 537 // Logout User1.
danielebarchiesi@0 538 $this->drupalLogout();
danielebarchiesi@0 539
danielebarchiesi@0 540 // Fill the page cache by requesting the poll.
danielebarchiesi@0 541 $this->drupalGet('node/' . $this->poll_nid);
danielebarchiesi@0 542 $this->assertEqual($this->drupalGetHeader('x-drupal-cache'), 'MISS', 'Page was cacheable but was not in the cache.');
danielebarchiesi@0 543 $this->drupalGet('node/' . $this->poll_nid);
danielebarchiesi@0 544 $this->assertEqual($this->drupalGetHeader('x-drupal-cache'), 'HIT', 'Page was cached.');
danielebarchiesi@0 545
danielebarchiesi@0 546 // Anonymous user vote on Poll.
danielebarchiesi@0 547 $this->drupalPost(NULL, $edit, t('Vote'));
danielebarchiesi@0 548 $this->assertText(t('Your vote was recorded.'), 'Anonymous vote was recorded.');
danielebarchiesi@0 549 $this->assertText(t('Total votes: @votes', array('@votes' => 2)), 'Vote count updated correctly.');
danielebarchiesi@0 550 $elements = $this->xpath('//input[@value="Cancel your vote"]');
danielebarchiesi@0 551 $this->assertTrue(!empty($elements), "'Cancel your vote' button appears.");
danielebarchiesi@0 552
danielebarchiesi@0 553 // Check to make sure Anonymous user cannot vote again.
danielebarchiesi@0 554 $this->drupalGet('node/' . $this->poll_nid);
danielebarchiesi@0 555 $this->assertFalse($this->drupalGetHeader('x-drupal-cache'), 'Page was not cacheable.');
danielebarchiesi@0 556 $elements = $this->xpath('//input[@value="Vote"]');
danielebarchiesi@0 557 $this->assertTrue(empty($elements), "Anonymous is not able to vote again.");
danielebarchiesi@0 558 $elements = $this->xpath('//input[@value="Cancel your vote"]');
danielebarchiesi@0 559 $this->assertTrue(!empty($elements), "'Cancel your vote' button appears.");
danielebarchiesi@0 560
danielebarchiesi@0 561 // Login User2.
danielebarchiesi@0 562 $this->drupalLogin($this->web_user2);
danielebarchiesi@0 563
danielebarchiesi@0 564 // User2 vote on poll.
danielebarchiesi@0 565 $this->drupalPost('node/' . $this->poll_nid, $edit, t('Vote'));
danielebarchiesi@0 566 $this->assertText(t('Your vote was recorded.'), format_string('%user vote was recorded.', array('%user' => $this->web_user2->name)));
danielebarchiesi@0 567 $this->assertText(t('Total votes: @votes', array('@votes' => 3)), 'Vote count updated correctly.');
danielebarchiesi@0 568 $elements = $this->xpath('//input[@value="Cancel your vote"]');
danielebarchiesi@0 569 $this->assertTrue(empty($elements), "'Cancel your vote' button does not appear.");
danielebarchiesi@0 570
danielebarchiesi@0 571 // Logout User2.
danielebarchiesi@0 572 $this->drupalLogout();
danielebarchiesi@0 573
danielebarchiesi@0 574 // Change host name for anonymous users.
danielebarchiesi@0 575 db_update('poll_vote')
danielebarchiesi@0 576 ->fields(array(
danielebarchiesi@0 577 'hostname' => '123.456.789.1',
danielebarchiesi@0 578 ))
danielebarchiesi@0 579 ->condition('hostname', '', '<>')
danielebarchiesi@0 580 ->execute();
danielebarchiesi@0 581
danielebarchiesi@0 582 // Check to make sure Anonymous user can vote again with a new session after
danielebarchiesi@0 583 // a hostname change.
danielebarchiesi@0 584 $this->drupalGet('node/' . $this->poll_nid);
danielebarchiesi@0 585 $this->assertEqual($this->drupalGetHeader('x-drupal-cache'), 'MISS', 'Page was cacheable but was not in the cache.');
danielebarchiesi@0 586 $this->drupalPost(NULL, $edit, t('Vote'));
danielebarchiesi@0 587 $this->assertText(t('Your vote was recorded.'), format_string('%user vote was recorded.', array('%user' => $this->web_user2->name)));
danielebarchiesi@0 588 $this->assertText(t('Total votes: @votes', array('@votes' => 4)), 'Vote count updated correctly.');
danielebarchiesi@0 589 $elements = $this->xpath('//input[@value="Cancel your vote"]');
danielebarchiesi@0 590 $this->assertTrue(!empty($elements), "'Cancel your vote' button appears.");
danielebarchiesi@0 591
danielebarchiesi@0 592 // Check to make sure Anonymous user cannot vote again with a new session,
danielebarchiesi@0 593 // and that the vote from the previous session cannot be cancelledd.
danielebarchiesi@0 594 $this->curlClose();
danielebarchiesi@0 595 $this->drupalGet('node/' . $this->poll_nid);
danielebarchiesi@0 596 $this->assertEqual($this->drupalGetHeader('x-drupal-cache'), 'MISS', 'Page was cacheable but was not in the cache.');
danielebarchiesi@0 597 $elements = $this->xpath('//input[@value="Vote"]');
danielebarchiesi@0 598 $this->assertTrue(empty($elements), 'Anonymous is not able to vote again.');
danielebarchiesi@0 599 $elements = $this->xpath('//input[@value="Cancel your vote"]');
danielebarchiesi@0 600 $this->assertTrue(empty($elements), "'Cancel your vote' button does not appear.");
danielebarchiesi@0 601
danielebarchiesi@0 602 // Login User1.
danielebarchiesi@0 603 $this->drupalLogin($this->web_user1);
danielebarchiesi@0 604
danielebarchiesi@0 605 // Check to make sure User1 still cannot vote even after hostname changed.
danielebarchiesi@0 606 $this->drupalGet('node/' . $this->poll_nid);
danielebarchiesi@0 607 $elements = $this->xpath('//input[@value="Vote"]');
danielebarchiesi@0 608 $this->assertTrue(empty($elements), format_string("%user is not able to vote again.", array('%user' => $this->web_user1->name)));
danielebarchiesi@0 609 $elements = $this->xpath('//input[@value="Cancel your vote"]');
danielebarchiesi@0 610 $this->assertTrue(!empty($elements), "'Cancel your vote' button appears.");
danielebarchiesi@0 611 }
danielebarchiesi@0 612 }
danielebarchiesi@0 613
danielebarchiesi@0 614 /**
danielebarchiesi@0 615 * Test poll token replacement in strings.
danielebarchiesi@0 616 */
danielebarchiesi@0 617 class PollTokenReplaceTestCase extends PollTestCase {
danielebarchiesi@0 618 public static function getInfo() {
danielebarchiesi@0 619 return array(
danielebarchiesi@0 620 'name' => 'Poll token replacement',
danielebarchiesi@0 621 'description' => 'Generates text using placeholders for dummy content to check poll token replacement.',
danielebarchiesi@0 622 'group' => 'Poll',
danielebarchiesi@0 623 );
danielebarchiesi@0 624 }
danielebarchiesi@0 625
danielebarchiesi@0 626 function setUp() {
danielebarchiesi@0 627 parent::setUp('poll');
danielebarchiesi@0 628 }
danielebarchiesi@0 629
danielebarchiesi@0 630 /**
danielebarchiesi@0 631 * Creates a poll, then tests the tokens generated from it.
danielebarchiesi@0 632 */
danielebarchiesi@0 633 function testPollTokenReplacement() {
danielebarchiesi@0 634 global $language;
danielebarchiesi@0 635
danielebarchiesi@0 636 // Craete a poll with three choices.
danielebarchiesi@0 637 $title = $this->randomName();
danielebarchiesi@0 638 $choices = $this->_generateChoices(3);
danielebarchiesi@0 639 $poll_nid = $this->pollCreate($title, $choices, FALSE);
danielebarchiesi@0 640 $this->drupalLogout();
danielebarchiesi@0 641
danielebarchiesi@0 642 // Create four users and have each of them vote.
danielebarchiesi@0 643 $vote_user1 = $this->drupalCreateUser(array('vote on polls', 'access content'));
danielebarchiesi@0 644 $this->drupalLogin($vote_user1);
danielebarchiesi@0 645 $edit = array(
danielebarchiesi@0 646 'choice' => '1',
danielebarchiesi@0 647 );
danielebarchiesi@0 648 $this->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
danielebarchiesi@0 649 $this->drupalLogout();
danielebarchiesi@0 650
danielebarchiesi@0 651 $vote_user2 = $this->drupalCreateUser(array('vote on polls', 'access content'));
danielebarchiesi@0 652 $this->drupalLogin($vote_user2);
danielebarchiesi@0 653 $edit = array(
danielebarchiesi@0 654 'choice' => '1',
danielebarchiesi@0 655 );
danielebarchiesi@0 656 $this->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
danielebarchiesi@0 657 $this->drupalLogout();
danielebarchiesi@0 658
danielebarchiesi@0 659 $vote_user3 = $this->drupalCreateUser(array('vote on polls', 'access content'));
danielebarchiesi@0 660 $this->drupalLogin($vote_user3);
danielebarchiesi@0 661 $edit = array(
danielebarchiesi@0 662 'choice' => '2',
danielebarchiesi@0 663 );
danielebarchiesi@0 664 $this->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
danielebarchiesi@0 665 $this->drupalLogout();
danielebarchiesi@0 666
danielebarchiesi@0 667 $vote_user4 = $this->drupalCreateUser(array('vote on polls', 'access content'));
danielebarchiesi@0 668 $this->drupalLogin($vote_user4);
danielebarchiesi@0 669 $edit = array(
danielebarchiesi@0 670 'choice' => '3',
danielebarchiesi@0 671 );
danielebarchiesi@0 672 $this->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
danielebarchiesi@0 673 $this->drupalLogout();
danielebarchiesi@0 674
danielebarchiesi@0 675 $poll = node_load($poll_nid, NULL, TRUE);
danielebarchiesi@0 676
danielebarchiesi@0 677 // Generate and test sanitized tokens.
danielebarchiesi@0 678 $tests = array();
danielebarchiesi@0 679 $tests['[node:poll-votes]'] = 4;
danielebarchiesi@0 680 $tests['[node:poll-winner]'] = filter_xss($poll->choice[1]['chtext']);
danielebarchiesi@0 681 $tests['[node:poll-winner-votes]'] = 2;
danielebarchiesi@0 682 $tests['[node:poll-winner-percent]'] = 50;
danielebarchiesi@0 683 $tests['[node:poll-duration]'] = format_interval($poll->runtime, 1, $language->language);
danielebarchiesi@0 684
danielebarchiesi@0 685 // Test to make sure that we generated something for each token.
danielebarchiesi@0 686 $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
danielebarchiesi@0 687
danielebarchiesi@0 688 foreach ($tests as $input => $expected) {
danielebarchiesi@0 689 $output = token_replace($input, array('node' => $poll), array('language' => $language));
danielebarchiesi@0 690 $this->assertEqual($output, $expected, format_string('Sanitized poll token %token replaced.', array('%token' => $input)));
danielebarchiesi@0 691 }
danielebarchiesi@0 692
danielebarchiesi@0 693 // Generate and test unsanitized tokens.
danielebarchiesi@0 694 $tests['[node:poll-winner]'] = $poll->choice[1]['chtext'];
danielebarchiesi@0 695
danielebarchiesi@0 696 foreach ($tests as $input => $expected) {
danielebarchiesi@0 697 $output = token_replace($input, array('node' => $poll), array('language' => $language, 'sanitize' => FALSE));
danielebarchiesi@0 698 $this->assertEqual($output, $expected, format_string('Unsanitized poll token %token replaced.', array('%token' => $input)));
danielebarchiesi@0 699 }
danielebarchiesi@0 700 }
danielebarchiesi@0 701 }
danielebarchiesi@0 702
danielebarchiesi@0 703 class PollExpirationTestCase extends PollTestCase {
danielebarchiesi@0 704 public static function getInfo() {
danielebarchiesi@0 705 return array(
danielebarchiesi@0 706 'name' => 'Poll expiration',
danielebarchiesi@0 707 'description' => 'Test the poll auto-expiration logic.',
danielebarchiesi@0 708 'group' => 'Poll',
danielebarchiesi@0 709 );
danielebarchiesi@0 710 }
danielebarchiesi@0 711
danielebarchiesi@0 712 function setUp() {
danielebarchiesi@0 713 parent::setUp('poll');
danielebarchiesi@0 714 }
danielebarchiesi@0 715
danielebarchiesi@0 716 function testAutoExpire() {
danielebarchiesi@0 717 // Set up a poll.
danielebarchiesi@0 718 $title = $this->randomName();
danielebarchiesi@0 719 $choices = $this->_generateChoices(2);
danielebarchiesi@0 720 $poll_nid = $this->pollCreate($title, $choices, FALSE);
danielebarchiesi@0 721 $this->assertTrue($poll_nid, 'Poll for auto-expire test created.');
danielebarchiesi@0 722
danielebarchiesi@0 723 // Visit the poll edit page and verify that by default, expiration
danielebarchiesi@0 724 // is set to unlimited.
danielebarchiesi@0 725 $this->drupalGet("node/$poll_nid/edit");
danielebarchiesi@0 726 $this->assertField('runtime', 'Poll expiration setting found.');
danielebarchiesi@0 727 $elements = $this->xpath('//select[@id="edit-runtime"]/option[@selected="selected"]');
danielebarchiesi@0 728 $this->assertTrue(isset($elements[0]['value']) && $elements[0]['value'] == 0, 'Poll expiration set to unlimited.');
danielebarchiesi@0 729
danielebarchiesi@0 730 // Set the expiration to one week.
danielebarchiesi@0 731 $edit = array();
danielebarchiesi@0 732 $poll_expiration = 604800; // One week.
danielebarchiesi@0 733 $edit['runtime'] = $poll_expiration;
danielebarchiesi@0 734 $this->drupalPost(NULL, $edit, t('Save'));
danielebarchiesi@0 735 $this->assertRaw(t('Poll %title has been updated.', array('%title' => $title)), 'Poll expiration settings saved.');
danielebarchiesi@0 736
danielebarchiesi@0 737 // Make sure that the changed expiration settings is kept.
danielebarchiesi@0 738 $this->drupalGet("node/$poll_nid/edit");
danielebarchiesi@0 739 $elements = $this->xpath('//select[@id="edit-runtime"]/option[@selected="selected"]');
danielebarchiesi@0 740 $this->assertTrue(isset($elements[0]['value']) && $elements[0]['value'] == $poll_expiration, 'Poll expiration set to unlimited.');
danielebarchiesi@0 741
danielebarchiesi@0 742 // Force a cron run. Since the expiration date has not yet been reached,
danielebarchiesi@0 743 // the poll should remain active.
danielebarchiesi@0 744 drupal_cron_run();
danielebarchiesi@0 745 $this->drupalGet("node/$poll_nid/edit");
danielebarchiesi@0 746 $elements = $this->xpath('//input[@id="edit-active-1"]');
danielebarchiesi@0 747 $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), 'Poll is still active.');
danielebarchiesi@0 748
danielebarchiesi@0 749 // Test expiration. Since REQUEST_TIME is a constant and we don't
danielebarchiesi@0 750 // want to keep SimpleTest waiting until the moment of expiration arrives,
danielebarchiesi@0 751 // we forcibly change the expiration date in the database.
danielebarchiesi@0 752 $created = db_query('SELECT created FROM {node} WHERE nid = :nid', array(':nid' => $poll_nid))->fetchField();
danielebarchiesi@0 753 db_update('node')
danielebarchiesi@0 754 ->fields(array('created' => $created - ($poll_expiration * 1.01)))
danielebarchiesi@0 755 ->condition('nid', $poll_nid)
danielebarchiesi@0 756 ->execute();
danielebarchiesi@0 757
danielebarchiesi@0 758 // Run cron and verify that the poll is now marked as "closed".
danielebarchiesi@0 759 drupal_cron_run();
danielebarchiesi@0 760 $this->drupalGet("node/$poll_nid/edit");
danielebarchiesi@0 761 $elements = $this->xpath('//input[@id="edit-active-0"]');
danielebarchiesi@0 762 $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), 'Poll has expired.');
danielebarchiesi@0 763 }
danielebarchiesi@0 764 }
danielebarchiesi@0 765
danielebarchiesi@0 766 class PollDeleteChoiceTestCase extends PollTestCase {
danielebarchiesi@0 767 public static function getInfo() {
danielebarchiesi@0 768 return array(
danielebarchiesi@0 769 'name' => 'Poll choice deletion',
danielebarchiesi@0 770 'description' => 'Test the poll choice deletion logic.',
danielebarchiesi@0 771 'group' => 'Poll',
danielebarchiesi@0 772 );
danielebarchiesi@0 773 }
danielebarchiesi@0 774
danielebarchiesi@0 775 function setUp() {
danielebarchiesi@0 776 parent::setUp('poll');
danielebarchiesi@0 777 }
danielebarchiesi@0 778
danielebarchiesi@0 779 function testChoiceRemoval() {
danielebarchiesi@0 780 // Set up a poll with three choices.
danielebarchiesi@0 781 $title = $this->randomName();
danielebarchiesi@0 782 $choices = array('First choice', 'Second choice', 'Third choice');
danielebarchiesi@0 783 $poll_nid = $this->pollCreate($title, $choices, FALSE);
danielebarchiesi@0 784 $this->assertTrue($poll_nid, 'Poll for choice deletion logic test created.');
danielebarchiesi@0 785
danielebarchiesi@0 786 // Edit the poll, and try to delete first poll choice.
danielebarchiesi@0 787 $this->drupalGet("node/$poll_nid/edit");
danielebarchiesi@0 788 $edit['choice[chid:1][chtext]'] = '';
danielebarchiesi@0 789 $this->drupalPost(NULL, $edit, t('Save'));
danielebarchiesi@0 790
danielebarchiesi@0 791 // Click on the poll title to go to node page.
danielebarchiesi@0 792 $this->drupalGet('poll');
danielebarchiesi@0 793 $this->clickLink($title);
danielebarchiesi@0 794
danielebarchiesi@0 795 // Check the first poll choice is deleted, while the others remain.
danielebarchiesi@0 796 $this->assertNoText('First choice', 'First choice removed.');
danielebarchiesi@0 797 $this->assertText('Second choice', 'Second choice remains.');
danielebarchiesi@0 798 $this->assertText('Third choice', 'Third choice remains.');
danielebarchiesi@0 799 }
danielebarchiesi@0 800 }
danielebarchiesi@0 801
danielebarchiesi@0 802 /**
danielebarchiesi@0 803 * Tests poll translation logic.
danielebarchiesi@0 804 */
danielebarchiesi@0 805 class PollTranslateTestCase extends PollTestCase {
danielebarchiesi@0 806 public static function getInfo() {
danielebarchiesi@0 807 return array(
danielebarchiesi@0 808 'name' => 'Poll translation',
danielebarchiesi@0 809 'description' => 'Test the poll translation logic.',
danielebarchiesi@0 810 'group' => 'Poll',
danielebarchiesi@0 811 );
danielebarchiesi@0 812 }
danielebarchiesi@0 813
danielebarchiesi@0 814 function setUp() {
danielebarchiesi@0 815 parent::setUp('poll', 'translation');
danielebarchiesi@0 816 }
danielebarchiesi@0 817
danielebarchiesi@0 818 /**
danielebarchiesi@0 819 * Tests poll creation and translation.
danielebarchiesi@0 820 *
danielebarchiesi@0 821 * Checks that the choice names get copied from the original poll and that
danielebarchiesi@0 822 * the vote count values are set to 0.
danielebarchiesi@0 823 */
danielebarchiesi@0 824 function testPollTranslate() {
danielebarchiesi@0 825 $admin_user = $this->drupalCreateUser(array('administer content types', 'administer languages', 'edit any poll content', 'create poll content', 'administer nodes', 'translate content'));
danielebarchiesi@0 826
danielebarchiesi@0 827 // Set up a poll with two choices.
danielebarchiesi@0 828 $title = $this->randomName();
danielebarchiesi@0 829 $choices = array($this->randomName(), $this->randomName());
danielebarchiesi@0 830 $poll_nid = $this->pollCreate($title, $choices, FALSE);
danielebarchiesi@0 831 $this->assertTrue($poll_nid, 'Poll for translation logic test created.');
danielebarchiesi@0 832
danielebarchiesi@0 833 $this->drupalLogout();
danielebarchiesi@0 834 $this->drupalLogin($admin_user);
danielebarchiesi@0 835
danielebarchiesi@0 836 // Enable a second language.
danielebarchiesi@0 837 $this->drupalGet('admin/config/regional/language');
danielebarchiesi@0 838 $edit = array();
danielebarchiesi@0 839 $edit['langcode'] = 'nl';
danielebarchiesi@0 840 $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
danielebarchiesi@0 841 $this->assertRaw(t('The language %language has been created and can now be used.', array('%language' => 'Dutch')), 'Language Dutch has been created.');
danielebarchiesi@0 842
danielebarchiesi@0 843 // Set "Poll" content type to use multilingual support with translation.
danielebarchiesi@0 844 $this->drupalGet('admin/structure/types/manage/poll');
danielebarchiesi@0 845 $edit = array();
danielebarchiesi@0 846 $edit['language_content_type'] = 2;
danielebarchiesi@0 847 $this->drupalPost('admin/structure/types/manage/poll', $edit, t('Save content type'));
danielebarchiesi@0 848 $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Poll')), 'Poll content type has been updated.');
danielebarchiesi@0 849
danielebarchiesi@0 850 // Edit poll.
danielebarchiesi@0 851 $this->drupalGet("node/$poll_nid/edit");
danielebarchiesi@0 852 $edit = array();
danielebarchiesi@0 853 // Set the poll's first choice count to 200.
danielebarchiesi@0 854 $edit['choice[chid:1][chvotes]'] = 200;
danielebarchiesi@0 855 // Set the language to Dutch.
danielebarchiesi@0 856 $edit['language'] = 'nl';
danielebarchiesi@0 857 $this->drupalPost(NULL, $edit, t('Save'));
danielebarchiesi@0 858
danielebarchiesi@0 859 // Translate the Dutch poll.
danielebarchiesi@0 860 $this->drupalGet('node/add/poll', array('query' => array('translation' => $poll_nid, 'target' => 'en')));
danielebarchiesi@0 861
danielebarchiesi@0 862 $dutch_poll = node_load($poll_nid);
danielebarchiesi@0 863
danielebarchiesi@0 864 // Check that the vote count values didn't get copied from the Dutch poll
danielebarchiesi@0 865 // and are set to 0.
danielebarchiesi@0 866 $this->assertFieldByName('choice[chid:1][chvotes]', '0', ('Found choice with vote count 0'));
danielebarchiesi@0 867 $this->assertFieldByName('choice[chid:2][chvotes]', '0', ('Found choice with vote count 0'));
danielebarchiesi@0 868 // Check that the choice names got copied from the Dutch poll.
danielebarchiesi@0 869 $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'])));
danielebarchiesi@0 870 $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'])));
danielebarchiesi@0 871 }
danielebarchiesi@0 872 }