You are here

public function QuizTimerTest::testQuizTimer in Quiz 8.5

Same name and namespace in other branches
  1. 8.6 tests/src/Functional/QuizTimerTest.php \Drupal\Tests\quiz\Functional\QuizTimerTest::testQuizTimer()
  2. 6.x tests/src/Functional/QuizTimerTest.php \Drupal\Tests\quiz\Functional\QuizTimerTest::testQuizTimer()

Test quiz timer expiration.

File

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

Class

QuizTimerTest
Test the quiz timer.

Namespace

Drupal\Tests\quiz\Functional

Code

public function testQuizTimer() {

  // Set up a quiz to show us feedback, 30 second expiration.
  $quiz = $this
    ->createQuiz(array(
    'review_options' => array(
      'end' => array(
        'score' => 'score',
      ),
    ),
    'time_limit' => 30,
  ));
  $question1 = $this
    ->createQuestion(array(
    'type' => 'truefalse',
    'truefalse_correct' => 1,
  ));
  $this
    ->linkQuestionToQuiz($question1, $quiz);
  $question2 = $this
    ->createQuestion(array(
    'type' => 'truefalse',
    'truefalse_correct' => 1,
  ));
  $this
    ->linkQuestionToQuiz($question2, $quiz);
  $question3 = $this
    ->createQuestion(array(
    'type' => 'truefalse',
    'truefalse_correct' => 1,
  ));
  $this
    ->linkQuestionToQuiz($question3, $quiz);

  // Record 2 answers before expiration.
  $this
    ->drupalLogin($this->user);
  $this
    ->drupalGet("quiz/{$quiz->id()}/take");
  $this
    ->drupalGet("quiz/{$quiz->id()}/take/1");
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question1->id()}][answer]" => 1,
  ), t('Next'));
  $this
    ->assertNoText(t('The last answer was not submitted, as the time ran out.'));
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question2->id()}][answer]" => 1,
  ), t('Next'));
  $this
    ->assertNoText(t('The last answer was not submitted, as the time ran out.'));

  // Set the quiz result to have started 31 seconds ago.
  db_query('UPDATE {quiz_result} SET time_start = :time', array(
    ':time' => \Drupal::time()
      ->getRequestTime() - 31,
  ));
  \Drupal::entityTypeManager()
    ->getStorage('quiz_result')
    ->resetCache();

  // Submit the last question past the expiration.
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question3->id()}][answer]" => 1,
  ), t('Finish'));
  $this
    ->assertText(t('The last answer was not submitted, as the time ran out.'));
  $this
    ->assertText('You got 2 of 3 possible points.');
}