You are here

public function QuizTakingTest::testQuizMaxAttempts in Quiz 8.5

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

File

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

Class

QuizTakingTest
Test quiz taking behavior.

Namespace

Drupal\Tests\quiz\Functional

Code

public function testQuizMaxAttempts() {
  $quiz_node = $this
    ->createQuiz(array(
    'takes' => 2,
  ));
  $question1 = $this
    ->createQuestion(array(
    'type' => 'truefalse',
    'truefalse_correct' => 1,
  ));
  $this
    ->linkQuestionToQuiz($question1, $quiz_node);
  $question2 = $this
    ->createQuestion(array(
    'type' => 'truefalse',
    'truefalse_correct' => 1,
  ));
  $this
    ->linkQuestionToQuiz($question2, $quiz_node);
  $this
    ->drupalLogin($this->user);
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take");
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take/1");
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question1->id()}][answer]" => 0,
  ), t('Next'));
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question2->id()}][answer]" => 0,
  ), t('Finish'));
  $this
    ->drupalLogin($this->user);
  $this
    ->drupalGet("quiz/{$quiz_node->id()}");
  $this
    ->assertText('You can only take this Quiz 2 times. You have taken it 1 time.');
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take");
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question1->id()}][answer]" => 0,
  ), t('Next'));

  // Make sure we can get back.
  $this
    ->drupalGet("quiz/{$quiz_node->id()}");
  $this
    ->assertNoText('You can only take this Quiz 2 times. You have taken it 1 time.');
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take/2");
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question2->id()}][answer]" => 0,
  ), t('Finish'));

  // No more attempts.
  $this
    ->drupalGet("quiz/{$quiz_node->id()}");
  $this
    ->assertText('You have already taken this Quiz 2 times. You may not take it again.');
}