poll.test 29.7 KB
Newer Older
1
2
<?php

3
4
/**
 * @file
5
 * Tests for poll.module.
6
7
 */

8
9
class PollTestCase extends DrupalWebTestCase {

10
11
12
  /**
   * Creates a poll.
   *
13
14
15
16
17
18
19
20
21
22
   * @param string $title
   *   The title of the poll.
   * @param array $choices
   *   A list of choice labels.
   * @param boolean $preview
   *   (optional) Whether to test if the preview is working or not. Defaults to
   *   TRUE.
   *
   * @return
   *   The node id of the created poll, or FALSE on error.
23
   */
24
  function pollCreate($title, $choices, $preview = TRUE) {
25
26
    $this->assertTrue(TRUE, 'Create a poll');

27
    $web_user = $this->drupalCreateUser(array('create poll content', 'access content', 'edit own poll content'));
28
    $this->drupalLogin($web_user);
29

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

33
    // Prepare a form with two choices.
34
35
    list($edit, $index) = $this->_pollGenerateEdit($title, $choices);

36
    // Re-submit the form until all choices are filled in.
37
    if (count($choices) > 2) {
38
      while ($index < count($choices)) {
39
        $this->drupalPost(NULL, $edit, t('Add another choice'));
40
        $this->assertPollChoiceOrder($choices, $index);
41
42
43
44
        list($edit, $index) = $this->_pollGenerateEdit($title, $choices, $index);
      }
    }

45
    if ($preview) {
46
      $this->drupalPost(NULL, $edit, t('Preview'));
47
      $this->assertPollChoiceOrder($choices, $index, TRUE);
48
      list($edit, $index) = $this->_pollGenerateEdit($title, $choices, $index);
49
    }
50

51
    $this->drupalPost(NULL, $edit, t('Save'));
52
    $node = $this->drupalGetNodeByTitle($title);
53
    $this->assertText(t('@type @title has been created.', array('@type' => node_type_get_name('poll'), '@title' => $title)), 'Poll has been created.');
54
    $this->assertTrue($node->nid, t('Poll has been found in the database.'));
55
56

    return isset($node->nid) ? $node->nid : FALSE;
57
58
  }

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  /**
   * Generates POST values for the poll node form, specifically poll choices.
   *
   * @param $title
   *   The title for the poll node.
   * @param $choices
   *   An array containing poll choices, as generated by
   *   PollTestCase::_generateChoices().
   * @param $index
   *   (optional) The amount/number of already submitted poll choices. Defaults
   *   to 0.
   *
   * @return
   *   An indexed array containing:
   *   - The generated POST values, suitable for
   *     DrupalWebTestCase::drupalPost().
   *   - The number of poll choices contained in 'edit', for potential re-usage
   *     in subsequent invocations of this function.
   */
  function _pollGenerateEdit($title, array $choices, $index = 0) {
79
    $max_new_choices = ($index == 0 ? 2 : 1);
80
81
    $already_submitted_choices = array_slice($choices, 0, $index);
    $new_choices = array_values(array_slice($choices, $index, $max_new_choices));
82

83
    $edit = array(
84
      'title' => $title,
85
    );
86
    foreach ($already_submitted_choices as $k => $text) {
87
88
      $edit['choice[chid:' . $k . '][chtext]'] = $text;
    }
89
    foreach ($new_choices as $k => $text) {
90
      $edit['choice[new:' . $k . '][chtext]'] = $text;
91
92
93
94
      // To test poll choice weights, every new choice is sorted in front of
      // existing choices. Existing/already submitted choices should keep their
      // weight.
      $edit['choice[new:' . $k . '][weight]'] = (- $index - $k);
95
96
97
    }
    return array($edit, count($already_submitted_choices) + count($new_choices));
  }
98

99
100
  function _generateChoices($count = 7) {
    $choices = array();
101
    for ($i = 1; $i <= $count; $i++) {
102
103
104
105
      $choices[] = $this->randomName();
    }
    return $choices;
  }
106

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
  /**
   * Assert correct poll choice order in the node form after submission.
   *
   * Verifies both the order in the DOM and in the 'weight' form elements.
   *
   * @param $choices
   *   An array containing poll choices, as generated by
   *   PollTestCase::_generateChoices().
   * @param $index
   *   (optional) The amount/number of already submitted poll choices. Defaults
   *   to 0.
   * @param $preview
   *   (optional) Whether to also check the poll preview.
   *
   * @see PollTestCase::_pollGenerateEdit()
   */
  function assertPollChoiceOrder(array $choices, $index = 0, $preview = FALSE) {
    $expected = array();
    foreach ($choices as $id => $label) {
      if ($id < $index) {
        // The expected weight of each choice is exactly the negated id.
        // @see PollTestCase::_pollGenerateEdit()
        $weight = -$id;
        // Directly assert the weight form element value for this choice.
        $this->assertFieldByName('choice[chid:' . $id . '][weight]', $weight, t('Found choice @id with weight @weight.', array(
          '@id' => $id,
          '@weight' => $weight,
        )));
        // Append to our (to be reversed) stack of labels.
        $expected[$weight] = $label;
      }
    }
    ksort($expected);

    // Verify DOM order of poll choices (i.e., #weight of form elements).
    $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 requested, also verify DOM order in preview.
    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, t('Found choice @label in preview.', array(
          '@label' => $next_label,
        )));
      }
    }
  }

168
169
170
  function pollUpdate($nid, $title, $edit) {
    // Edit the poll node.
    $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
171
    $this->assertText(t('@type @title has been updated.', array('@type' => node_type_get_name('poll'), '@title' => $title)), 'Poll has been updated.');
172
  }
173
174
175
}

class PollCreateTestCase extends PollTestCase {
176
  public static function getInfo() {
177
178
179
180
181
    return array(
      'name' => 'Poll create',
      'description' => 'Adds "more choices", previews and creates a poll.',
      'group' => 'Poll'
    );
182
183
184
185
186
187
188
  }

  function setUp() {
    parent::setUp('poll');
  }

  function testPollCreate() {
189
190
    $title = $this->randomName();
    $choices = $this->_generateChoices(7);
191
    $poll_nid = $this->pollCreate($title, $choices, TRUE);
192
193
194
195
196
197
198
199
200

    // Verify poll appears on 'poll' page.
    $this->drupalGet('poll');
    $this->assertText($title, 'Poll appears in poll list.');
    $this->assertText('open', 'Poll is active.');

    // Click on the poll title to go to node page.
    $this->clickLink($title);
    $this->assertText('Total votes: 0', 'Link to poll correct.');
201
202
203
204
205
206
207

    // Now add a new option to make sure that when we update the node the
    // option is displayed.
    $node = node_load($poll_nid);

    $new_option = $this->randomName();

208
    $vote_count = '2000';
209
210
211
    $node->choice[] = array(
      'chid' => '',
      'chtext' => $new_option,
212
213
      'chvotes' => (int) $vote_count,
      'weight' => 1000,
214
215
216
217
218
219
220
    );

    node_save($node);

    $this->drupalGet('poll');
    $this->clickLink($title);
    $this->assertText($new_option, 'New option found.');
221
222
223
224
225
226

    $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, t("Votes saved."));
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
  }

  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'));

    // Create poll.
    $title = $this->randomName();
    $choices = $this->_generateChoices(7);
    $poll_nid = $this->pollCreate($title, $choices, FALSE);

    $this->drupalLogout();
    $this->drupalLogin($content_user);

    // Edit the poll node and close the poll.
    $close_edit = array('active' => 0);
    $this->pollUpdate($poll_nid, $title, $close_edit);

    // Verify 'Vote' button no longer appears.
    $this->drupalGet('node/' . $poll_nid);
    $elements = $this->xpath('//input[@id="edit-vote"]');
248
    $this->assertTrue(empty($elements), t("Vote button doesn't appear."));
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

    // Verify status on 'poll' page is 'closed'.
    $this->drupalGet('poll');
    $this->assertText($title, 'Poll appears in poll list.');
    $this->assertText('closed', 'Poll is closed.');

    // Edit the poll node and re-activate.
    $open_edit = array('active' => 1);
    $this->pollUpdate($poll_nid, $title, $open_edit);

    // Vote on the poll.
    $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"]');
266
    $this->assertTrue(isset($elements[0]), t("'Cancel your vote' button appears."));
267
268
269
270
271
272
273
274
275
276

    // Edit the poll node and close the poll.
    $this->drupalLogout();
    $this->drupalLogin($content_user);
    $close_edit = array('active' => 0);
    $this->pollUpdate($poll_nid, $title, $close_edit);

    // Verify 'Cancel your vote' button no longer appears.
    $this->drupalGet('node/' . $poll_nid);
    $elements = $this->xpath('//input[@value="Cancel your vote"]');
277
    $this->assertTrue(empty($elements), t("'Cancel your vote' button no longer appears."));
278
279
280
281
  }
}

class PollVoteTestCase extends PollTestCase {
282
  public static function getInfo() {
283
284
285
286
287
    return array(
      'name' => 'Poll vote',
      'description' => 'Vote on a poll',
      'group' => 'Poll'
    );
288
289
290
291
292
293
294
295
296
297
298
  }

  function setUp() {
    parent::setUp('poll');
  }

  function tearDown() {
    parent::tearDown();
  }

  function testPollVote() {
299
300
301
    $title = $this->randomName();
    $choices = $this->_generateChoices(7);
    $poll_nid = $this->pollCreate($title, $choices, FALSE);
302
    $this->drupalLogout();
303

304
305
306
307
    $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);
308

309
    // Record a vote for the first choice.
310
    $edit = array(
311
312
      'choice' => '1',
    );
313
    $this->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
314
    $this->assertText('Your vote was recorded.', 'Your vote was recorded.');
315
316
    $this->assertText('Total votes: 1', 'Vote count updated correctly.');
    $elements = $this->xpath('//input[@value="Cancel your vote"]');
317
    $this->assertTrue(isset($elements[0]), t("'Cancel your vote' button appears."));
318
319

    $this->drupalGet("node/$poll_nid/votes");
320
    $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.');
321
    $this->assertText($choices[0], 'Vote recorded');
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352

    // Ensure poll listing page has correct number of votes.
    $this->drupalGet('poll');
    $this->assertText($title, 'Poll appears in poll list.');
    $this->assertText('1 vote', 'Poll has 1 vote.');

    // Cancel a 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');

    // Ensure poll listing page has correct number of votes.
    $this->drupalGet('poll');
    $this->assertText($title, 'Poll appears in poll list.');
    $this->assertText('0 votes', 'Poll has 0 votes.');

    // Log in as a user who can only vote on polls.
    $this->drupalLogout();
    $this->drupalLogin($restricted_vote_user);

    // Vote on a poll.
    $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"]');
353
    $this->assertTrue(empty($elements), t("'Cancel your vote' button does not appear."));
354
355
  }
}
356

357
class PollBlockTestCase extends PollTestCase {
358
  public static function getInfo() {
359
    return array(
360
361
362
      'name' => 'Block availability',
      'description' => 'Check if the most recent poll block is available.',
      'group' => 'Poll',
363
364
365
366
367
368
369
370
371
372
373
374
    );
  }

  function setUp() {
    parent::setUp('poll');

    // Create and login user
    $admin_user = $this->drupalCreateUser(array('administer blocks'));
    $this->drupalLogin($admin_user);
  }

  function testRecentBlock() {
375
    // Set block title to confirm that the interface is available.
376
    $this->drupalPost('admin/structure/block/manage/poll/recent/configure', array('title' => $this->randomName(8)), t('Save block'));
377
    $this->assertText(t('The block configuration has been saved.'), t('Block configuration set.'));
378

379
    // Set the block to a region to confirm block is available.
380
    $edit = array();
381
    $edit['blocks[poll_recent][region]'] = 'footer';
382
    $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
383
    $this->assertText(t('The block settings have been updated.'), t('Block successfully move to footer region.'));
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420

    // Create a poll which should appear in recent polls block.
    $title = $this->randomName();
    $choices = $this->_generateChoices(7);
    $poll_nid = $this->pollCreate($title, $choices, TRUE);

    // Verify poll appears in a block.
    // View user page so we're not matching the poll node on front page.
    $this->drupalGet('user');
    // If a 'block' view not generated, this title would not appear even though
    // the choices might.
    $this->assertText($title, 'Poll appears in block.');

    // Logout and login back in as a user who can vote.
    $this->drupalLogout();
    $vote_user = $this->drupalCreateUser(array('cancel own vote', 'inspect all votes', 'vote on polls', 'access content'));
    $this->drupalLogin($vote_user);

    // Verify we can vote via the block.
    $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.');

    // Close the poll and verify block doesn't appear.
    $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.');
421
422
  }
}
423
424
425
426
427
428

/**
 * Test adding new choices.
 */
class PollJSAddChoice extends DrupalWebTestCase {

429
  public static function getInfo() {
430
    return array(
431
432
433
      'name' => 'Poll add choice',
      'description' => 'Submits a POST request for an additional poll choice.',
      'group' => 'Poll'
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
    );
  }

  function setUp() {
    parent::setUp('poll');
  }

  /**
   * Test adding a new choice.
   */
  function testAddChoice() {
    $web_user = $this->drupalCreateUser(array('create poll content', 'access content'));
    $this->drupalLogin($web_user);
    $this->drupalGet('node/add/poll');
    $edit = array(
449
      "title" => $this->randomName(),
450
451
452
453
      'choice[new:0][chtext]' => $this->randomName(),
      'choice[new:1][chtext]' => $this->randomName(),
    );

454
    // Press 'add choice' button through Ajax, and place the expected HTML result
455
    // as the tested content.
456
    $commands = $this->drupalPostAJAX(NULL, $edit, array('op' => t('Add another choice')));
457
458
    $this->content = $commands[1]['data'];

459
460
461
    $this->assertFieldByName('choice[chid:0][chtext]', $edit['choice[new:0][chtext]'], t('Field !i found', array('!i' => 0)));
    $this->assertFieldByName('choice[chid:1][chtext]', $edit['choice[new:1][chtext]'], t('Field !i found', array('!i' => 1)));
    $this->assertFieldByName('choice[new:0][chtext]', '', t('Field !i found', array('!i' => 2)));
462
463
  }
}
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484

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');

    // Create and login user.
    $this->admin_user = $this->drupalCreateUser(array('administer permissions', 'create poll content'));
    $this->drupalLogin($this->admin_user);

    // Allow anonymous users to vote on polls.
    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
      'access content' => TRUE,
      'vote on polls' => TRUE,
485
      'cancel own vote' => TRUE,
486
487
    ));

488
489
    // Enable page cache to verify that the result page is not saved in the
    // cache when anonymous voting is allowed.
490
    variable_set('cache', 1);
491

492
    // Create poll.
493
494
495
496
497
498
499
    $title = $this->randomName();
    $choices = $this->_generateChoices(3);
    $this->poll_nid = $this->pollCreate($title, $choices, FALSE);

    $this->drupalLogout();

    // Create web users.
500
    $this->web_user1 = $this->drupalCreateUser(array('access content', 'vote on polls', 'cancel own vote'));
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
    $this->web_user2 = $this->drupalCreateUser(array('access content', 'vote on polls'));
  }

  /**
   * Check that anonymous users with same ip cannot vote on poll more than once
   * unless user is logged in.
   */
  function testHostnamePollVote() {
    // Login User1.
    $this->drupalLogin($this->web_user1);

    $edit = array(
      'choice' => '1',
    );

    // User1 vote on Poll.
    $this->drupalPost('node/' . $this->poll_nid, $edit, t('Vote'));
518
519
    $this->assertText(t('Your vote was recorded.'), t('%user vote was recorded.', array('%user' => $this->web_user1->name)));
    $this->assertText(t('Total votes: @votes', array('@votes' => 1)), t('Vote count updated correctly.'));
520
521
522
523

    // Check to make sure User1 cannot vote again.
    $this->drupalGet('node/' . $this->poll_nid);
    $elements = $this->xpath('//input[@value="Vote"]');
524
    $this->assertTrue(empty($elements), t("%user is not able to vote again.", array('%user' => $this->web_user1->name)));
525
    $elements = $this->xpath('//input[@value="Cancel your vote"]');
526
    $this->assertTrue(!empty($elements), t("'Cancel your vote' button appears."));
527
528
529
530

    // Logout User1.
    $this->drupalLogout();

531
532
    // Fill the page cache by requesting the poll.
    $this->drupalGet('node/' . $this->poll_nid);
533
    $this->assertEqual($this->drupalGetHeader('x-drupal-cache'), 'MISS', t('Page was cacheable but was not in the cache.'));
534
    $this->drupalGet('node/' . $this->poll_nid);
535
    $this->assertEqual($this->drupalGetHeader('x-drupal-cache'), 'HIT', t('Page was cached.'));
536

537
    // Anonymous user vote on Poll.
538
    $this->drupalPost(NULL, $edit, t('Vote'));
539
540
    $this->assertText(t('Your vote was recorded.'), t('Anonymous vote was recorded.'));
    $this->assertText(t('Total votes: @votes', array('@votes' => 2)), t('Vote count updated correctly.'));
541
    $elements = $this->xpath('//input[@value="Cancel your vote"]');
542
    $this->assertTrue(!empty($elements), t("'Cancel your vote' button appears."));
543
544
545

    // Check to make sure Anonymous user cannot vote again.
    $this->drupalGet('node/' . $this->poll_nid);
546
    $this->assertFalse($this->drupalGetHeader('x-drupal-cache'), t('Page was not cacheable.'));
547
    $elements = $this->xpath('//input[@value="Vote"]');
548
    $this->assertTrue(empty($elements), t("Anonymous is not able to vote again."));
549
    $elements = $this->xpath('//input[@value="Cancel your vote"]');
550
    $this->assertTrue(!empty($elements), t("'Cancel your vote' button appears."));
551
552
553
554
555
556

    // Login User2.
    $this->drupalLogin($this->web_user2);

    // User2 vote on poll.
    $this->drupalPost('node/' . $this->poll_nid, $edit, t('Vote'));
557
    $this->assertText(t('Your vote was recorded.'), t('%user vote was recorded.', array('%user' => $this->web_user2->name)));
558
    $this->assertText(t('Total votes: @votes', array('@votes' => 3)), 'Vote count updated correctly.');
559
    $elements = $this->xpath('//input[@value="Cancel your vote"]');
560
    $this->assertTrue(empty($elements), t("'Cancel your vote' button does not appear."));
561
562
563
564
565
566
567

    // Logout User2.
    $this->drupalLogout();

    // Change host name for anonymous users.
    db_update('poll_vote')
      ->fields(array(
568
        'hostname' => '123.456.789.1',
569
      ))
570
      ->condition('hostname', '', '<>')
571
572
      ->execute();

573
574
575
    // Check to make sure Anonymous user can vote again with a new session after
    // a hostname change.
    $this->drupalGet('node/' . $this->poll_nid);
576
    $this->assertEqual($this->drupalGetHeader('x-drupal-cache'), 'MISS', t('Page was cacheable but was not in the cache.'));
577
    $this->drupalPost(NULL, $edit, t('Vote'));
578
    $this->assertText(t('Your vote was recorded.'), t('%user vote was recorded.', array('%user' => $this->web_user2->name)));
579
    $this->assertText(t('Total votes: @votes', array('@votes' => 4)), 'Vote count updated correctly.');
580
    $elements = $this->xpath('//input[@value="Cancel your vote"]');
581
    $this->assertTrue(!empty($elements), t("'Cancel your vote' button appears."));
582

583
584
585
    // Check to make sure Anonymous user cannot vote again with a new session,
    // and that the vote from the previous session cannot be cancelledd.
    $this->curlClose();
586
    $this->drupalGet('node/' . $this->poll_nid);
587
    $this->assertEqual($this->drupalGetHeader('x-drupal-cache'), 'MISS', t('Page was cacheable but was not in the cache.'));
588
    $elements = $this->xpath('//input[@value="Vote"]');
589
    $this->assertTrue(empty($elements), t('Anonymous is not able to vote again.'));
590
    $elements = $this->xpath('//input[@value="Cancel your vote"]');
591
    $this->assertTrue(empty($elements), t("'Cancel your vote' button does not appear."));
592
593
594
595
596
597
598

    // Login User1.
    $this->drupalLogin($this->web_user1);

    // Check to make sure User1 still cannot vote even after hostname changed.
    $this->drupalGet('node/' . $this->poll_nid);
    $elements = $this->xpath('//input[@value="Vote"]');
599
    $this->assertTrue(empty($elements), t("%user is not able to vote again.", array('%user' => $this->web_user1->name)));
600
    $elements = $this->xpath('//input[@value="Cancel your vote"]');
601
    $this->assertTrue(!empty($elements), t("'Cancel your vote' button appears."));
602
603
  }
}
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676

/**
 * Test poll token replacement in strings.
 */
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');
  }

  /**
   * Creates a poll, then tests the tokens generated from it.
   */
  function testPollTokenReplacement() {
    global $language;

    // Craete a poll with three choices.
    $title = $this->randomName();
    $choices = $this->_generateChoices(3);
    $poll_nid = $this->pollCreate($title, $choices, FALSE);
    $this->drupalLogout();

    // Create four users and have each of them vote.
    $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);

    // Generate and test sanitized tokens.
    $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);

    // Test to make sure that we generated something for each token.
677
    $this->assertFalse(in_array(0, array_map('strlen', $tests)), t('No empty tokens generated.'));
678
679
680

    foreach ($tests as $input => $expected) {
      $output = token_replace($input, array('node' => $poll), array('language' => $language));
681
      $this->assertEqual($output, $expected, t('Sanitized poll token %token replaced.', array('%token' => $input)));
682
683
684
685
686
687
688
    }

    // Generate and test unsanitized tokens.
    $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));
689
      $this->assertEqual($output, $expected, t('Unsanitized poll token %token replaced.', array('%token' => $input)));
690
691
692
    }
  }
}
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711

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() {
    // Set up a poll.
    $title = $this->randomName();
    $choices = $this->_generateChoices(2);
    $poll_nid = $this->pollCreate($title, $choices, FALSE);
712
    $this->assertTrue($poll_nid, t('Poll for auto-expire test created.'));
713
714
715
716

    // Visit the poll edit page and verify that by default, expiration
    // is set to unlimited.
    $this->drupalGet("node/$poll_nid/edit");
717
    $this->assertField('runtime', t('Poll expiration setting found.'));
718
    $elements = $this->xpath('//select[@id="edit-runtime"]/option[@selected="selected"]');
719
    $this->assertTrue(isset($elements[0]['value']) && $elements[0]['value'] == 0, t('Poll expiration set to unlimited.'));
720
721
722
723
724
725

    // Set the expiration to one week.
    $edit = array();
    $poll_expiration = 604800; // One week.
    $edit['runtime'] = $poll_expiration;
    $this->drupalPost(NULL, $edit, t('Save'));
726
    $this->assertRaw(t('Poll %title has been updated.', array('%title' => $title)), t('Poll expiration settings saved.'));
727
728
729
730

    // Make sure that the changed expiration settings is kept.
    $this->drupalGet("node/$poll_nid/edit");
    $elements = $this->xpath('//select[@id="edit-runtime"]/option[@selected="selected"]');
731
    $this->assertTrue(isset($elements[0]['value']) && $elements[0]['value'] == $poll_expiration, t('Poll expiration set to unlimited.'));
732
733
734
735
736
737

    // Force a cron run. Since the expiration date has not yet been reached,
    // the poll should remain active.
    drupal_cron_run();
    $this->drupalGet("node/$poll_nid/edit");
    $elements = $this->xpath('//input[@id="edit-active-1"]');
738
    $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), t('Poll is still active.'));
739
740
741
742
743
744
745
746
747
748
749
750
751
752

    // Test expiration. Since REQUEST_TIME is a constant and we don't
    // want to keep SimpleTest waiting until the moment of expiration arrives,
    // we forcibly change the expiration date in the database.
    $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();

    // Run cron and verify that the poll is now marked as "closed".
    drupal_cron_run();
    $this->drupalGet("node/$poll_nid/edit");
    $elements = $this->xpath('//input[@id="edit-active-0"]');
753
    $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), t('Poll has expired.'));
754
  }
755
}
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791

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() {
    // Set up a poll with three choices.
    $title = $this->randomName();
    $choices = array('First choice', 'Second choice', 'Third choice');
    $poll_nid = $this->pollCreate($title, $choices, FALSE);
    $this->assertTrue($poll_nid, t('Poll for choice deletion logic test created.'));

    // Edit the poll, and try to delete first poll choice.
    $this->drupalGet("node/$poll_nid/edit");
    $edit['choice[chid:1][chtext]'] = '';
    $this->drupalPost(NULL, $edit, t('Save'));

    // Click on the poll title to go to node page.
    $this->drupalGet('poll');
    $this->clickLink($title);

    // Check the first poll choice is deleted, while the others remain.
    $this->assertNoText('First choice', t('First choice removed.'));
    $this->assertText('Second choice', t('Second choice remains.'));
    $this->assertText('Third choice', t('Third choice remains.'));
  }
}