You are here

public function QuizTakingTest::testQuizAvailability in Quiz 6.x

Same name and namespace in other branches
  1. 8.6 tests/src/Functional/QuizTakingTest.php \Drupal\Tests\quiz\Functional\QuizTakingTest::testQuizAvailability()
  2. 8.5 tests/src/Functional/QuizTakingTest.php \Drupal\Tests\quiz\Functional\QuizTakingTest::testQuizAvailability()

Test the quiz availability tests.

File

tests/src/Functional/QuizTakingTest.php, line 23

Class

QuizTakingTest
Test quiz taking behavior.

Namespace

Drupal\Tests\quiz\Functional

Code

public function testQuizAvailability() {

  // Anonymous doesn't have 'access quiz' permissions, so login a user that
  // has that permission.
  $this
    ->drupalLogin($this->user);
  $future = \Drupal::time()
    ->getRequestTime() + 86400;
  $past = \Drupal::time()
    ->getRequestTime() - 86400;

  // Within range.
  $quiz_node_open = $this
    ->createQuiz([
    'quiz_date' => [
      'value' => date('Y-m-d\\TH:i:s', $past),
      'end_value' => date('Y-m-d\\TH:i:s', $future),
    ],
  ]);
  $this
    ->drupalGet("quiz/{$quiz_node_open->id()}");
  $this
    ->assertNoText(t('This @quiz is closed.', [
    '@quiz' => QuizUtil::getQuizName(),
  ]));
  $this
    ->assertNoText(t('You are not allowed to take this @quiz.', [
    '@quiz' => QuizUtil::getQuizName(),
  ]));

  // Starts in the future.
  $quiz_node_future = $this
    ->createQuiz([
    'quiz_date' => [
      'value' => date('Y-m-d\\TH:i:s', $future),
      'end_value' => date('Y-m-d\\TH:i:s', $future + 1),
    ],
  ]);
  $this
    ->drupalGet("quiz/{$quiz_node_future->id()}");
  $this
    ->assertText(t('This @quiz is not yet open.', [
    '@quiz' => QuizUtil::getQuizName(),
  ]));
  $this
    ->assertNoText(t('You are not allowed to take this @quiz.', [
    '@quiz' => QuizUtil::getQuizName(),
  ]));
  $this
    ->drupalGet("quiz/{$quiz_node_future->id()}/take");
  $this
    ->assertText(t('This @quiz is not yet open.', [
    '@quiz' => QuizUtil::getQuizName(),
  ]));

  // Ends in the past.
  $quiz_node_past = $this
    ->createQuiz([
    'quiz_date' => [
      'value' => date('Y-m-d\\TH:i:s', $past),
      'end_value' => date('Y-m-d\\TH:i:s', $past + 1),
    ],
  ]);
  $this
    ->drupalGet("quiz/{$quiz_node_past->id()}");
  $this
    ->assertText(t('This @quiz is closed.', [
    '@quiz' => QuizUtil::getQuizName(),
  ]));
  $this
    ->assertNoText(t('You are not allowed to take this @quiz.', [
    '@quiz' => QuizUtil::getQuizName(),
  ]));
  $this
    ->drupalGet("quiz/{$quiz_node_past->id()}/take");
  $this
    ->assertText(t('This @quiz is closed.', [
    '@quiz' => QuizUtil::getQuizName(),
  ]));

  // Always available.
  $quiz = $this
    ->createQuiz([]);
  $this
    ->drupalGet("quiz/{$quiz->id()}");
  $this
    ->assertNoText(t('This @quiz is closed.', [
    '@quiz' => QuizUtil::getQuizName(),
  ]));
  $this
    ->assertNoText(t('You are not allowed to take this @quiz.', [
    '@quiz' => QuizUtil::getQuizName(),
  ]));
}