You are here

public function QuizFeedbackTest::testAnswerFeedback in Quiz 6.x

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

Test question feedback.

Note that we are only testing if any feedback displays, each question type has its own tests for testing feedback returned from that question type.

File

tests/src/Functional/QuizFeedbackTest.php, line 22

Class

QuizFeedbackTest
Test quiz feedback.

Namespace

Drupal\Tests\quiz\Functional

Code

public function testAnswerFeedback() {
  $this
    ->drupalLogin($this->admin);
  $quiz = $this
    ->createQuiz();

  // 2 questions.
  $question1 = $this
    ->createQuestion([
    'type' => 'truefalse',
    'truefalse_correct' => 1,
  ]);
  $this
    ->linkQuestionToQuiz($question1, $quiz);
  $question2 = $this
    ->createQuestion([
    'type' => 'truefalse',
    'truefalse_correct' => 1,
  ]);
  $this
    ->linkQuestionToQuiz($question2, $quiz);
  $review_options = [
    'attempt' => t('Your answer'),
    'correct' => t('Correct?'),
    'score' => t('Score'),
    'answer_feedback' => t('Feedback'),
    'solution' => t('Correct answer'),
  ];
  $this
    ->drupalLogin($this->user);

  // Answer the first question.
  $this
    ->drupalGet("quiz/{$quiz->id()}/take");
  $this
    ->drupalPostForm(NULL, [
    "question[{$question1->id()}][answer]" => 1,
  ], t('Next'));

  // Check feedback after the Question.
  foreach ($review_options as $option => $text) {

    // Loop through each review option, set it, then reload the quiz page to
    // see if it had an effect.
    $quiz->review_options = [
      'question' => [
        $option => $option,
      ],
    ];
    $quiz
      ->save();

    // Refresh feedback page.
    $this
      ->drupalGet("quiz/{$quiz->id()}/take/1/feedback");

    // As long as there is some feedback there should be a question title
    // header.
    $this
      ->assertText('Question 1');
    $this
      ->assertNoText('Question 2');
    $this
      ->assertRaw('<th>' . $text . '</th>');
    foreach ($review_options as $option2 => $text2) {
      if ($option != $option2) {
        $this
          ->assertNoRaw('<th>' . $text2 . '</th>');
      }
    }
  }

  // Answer the last question
  $this
    ->clickLink(t('Next question'));
  $this
    ->drupalPostForm(NULL, [
    "question[{$question2->id()}][answer]" => 1,
  ], t('Finish'));

  // Check that we can access the feedback for the final question before quiz
  // feedback is shown. Verify the first question feedback is not shown.
  $this
    ->assertNoText('Question 1');
  $this
    ->assertText('Question 2');

  // Press the finish button on the last question's feedback page.
  $this
    ->drupalPostForm(NULL, [], t('Finish'));

  // Check feedback after the Quiz.
  foreach ($review_options as $option => $text) {

    // Loop through each review option, set it, then reload the quiz page to
    // see if it had an effect.
    $quiz->review_options = [
      'end' => [
        $option => $option,
      ],
    ];
    $quiz
      ->save();

    // Refresh feedback page.
    $this
      ->drupalGet("quiz/{$quiz->id()}/result/1");

    // Verify both questions appear. As long as there is some feedback there

    //should be a question title header.
    $this
      ->assertText('Question 1');
    $this
      ->assertText('Question 2');
    $this
      ->assertRaw('<th>' . $text . '</th>');
    foreach ($review_options as $option2 => $text2) {
      if ($option != $option2) {
        $this
          ->assertNoRaw('<th>' . $text2 . '</th>');
      }
    }
  }
}