You are here

public function QuizTakingTest::testChangeAnswer in Quiz 8.5

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

Test allow/restrict changing of answers.

File

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

Class

QuizTakingTest
Test quiz taking behavior.

Namespace

Drupal\Tests\quiz\Functional

Code

public function testChangeAnswer() {
  $quiz_node = $this
    ->createQuiz(array(
    'review_options' => array(
      'question' => array(
        'score' => 'score',
      ),
    ),
  ));
  $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);
  $question3 = $this
    ->createQuestion(array(
    'type' => 'truefalse',
    'truefalse_correct' => 1,
  ));
  $this
    ->linkQuestionToQuiz($question3, $quiz_node);

  // Answer incorrectly.
  $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
    ->assertText('Score: 0 of 1');

  // Go back and correct the answer.
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take/1");
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question1->id()}][answer]" => 1,
  ), t('Next'));
  $this
    ->assertText('Score: 1 of 1');

  // Go back and incorrect the answer.
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take/1");
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question1->id()}][answer]" => 0,
  ), t('Next'));
  $this
    ->assertText('Score: 0 of 1');
  $quiz_node
    ->set('allow_change', 0);
  $quiz_node
    ->save();

  // Check that the answer cannot be changed.
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take/1");
  $this
    ->assertSession()
    ->fieldDisabled('edit-question-1-answer-1');
  $this
    ->drupalPostForm(NULL, array(), t('Next'));
  $this
    ->assertText('Score: 0 of 1');

  // Check allow change/blank behavior.
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take/2");
  $this
    ->drupalPostForm(NULL, array(), t('Leave blank'));
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take/2");
  $this
    ->assertSession()
    ->fieldDisabled('edit-question-2-answer-1');
  $quiz_node
    ->set('allow_change_blank', 1);
  $quiz_node
    ->save();
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take/2");
  $this
    ->assertSession()
    ->fieldEnabled('edit-question-2-answer-1');
}