You are here

function PollExpirationTest::testAutoExpire in Poll 8

Tests the expiration of a poll.

File

tests/src/Functional/PollExpirationTest.php, line 17

Class

PollExpirationTest
Tests the expiration of polls.

Namespace

Drupal\Tests\poll\Functional

Code

function testAutoExpire() {

  // Set up a poll.
  $poll = $this->poll;

  // Visit the poll edit page and verify that by default, expiration
  // is set to unlimited.
  $this
    ->drupalLogin($this->admin_user);
  $this
    ->drupalGet('poll/' . $poll
    ->id() . '/edit');
  $this
    ->assertField('runtime');
  $elements = $this
    ->xpath('//select[@id="edit-runtime"]/option[@selected="selected"]');
  $this
    ->assertEquals(0, $elements[0]
    ->getAttribute('value'), 'Poll expiration set to unlimited.');

  // Set the expiration to one week.
  $runtime = 604800;

  // One week.
  $poll
    ->setRuntime($runtime);
  $poll
    ->save();

  // Make sure that the changed expiration settings is kept.
  // here
  $this
    ->drupalGet('poll/' . $poll
    ->id() . '/edit');
  $elements = $this
    ->xpath('//select[@id="edit-runtime"]/option[@selected="selected"]');
  $this
    ->assertEquals($runtime, $elements[0]
    ->getAttribute('value'), 'Poll expiration set to one week.');

  // Force a cron run. Since the expiration date has not yet been reached,
  // the poll should remain open.
  \Drupal::service('cron')
    ->run();
  $this
    ->assertTrue($poll
    ->isOpen(), 'Poll remains open after cron.');
  $created = $poll
    ->getCreated();
  $offset = $created - $runtime * 1.01;
  $poll
    ->setCreated($offset);
  $poll
    ->save();

  // Run cron and verify that the poll is now marked as "closed".
  \Drupal::service('cron')
    ->run();
  $loaded_poll = Poll::load($poll
    ->id());
  $this
    ->assertTrue($loaded_poll
    ->isClosed(), 'Poll has expired.');
}