You are here

public function QuizResultTest::testPassRateSummary in Quiz 8.5

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

Test the various result summaries and pass rate.

File

tests/src/Functional/QuizResultTest.php, line 25

Class

QuizResultTest
Test quiz results behavior.

Namespace

Drupal\Tests\quiz\Functional

Code

public function testPassRateSummary() {

  // Set up some alternatives.
  $a = Paragraph::create([
    'type' => 'quiz_result_feedback',
    'quiz_feedback' => 'You got 90 or more on the quiz',
    'quiz_feedback_range' => [
      'from' => 90,
      'to' => 100,
    ],
  ]);
  $a
    ->save();
  $b = Paragraph::create([
    'type' => 'quiz_result_feedback',
    'quiz_feedback' => 'You got between 50 and 89',
    'quiz_feedback_range' => [
      'from' => 50,
      'to' => 89,
    ],
  ]);
  $b
    ->save();
  $c = Paragraph::create([
    'type' => 'quiz_result_feedback',
    'quiz_feedback' => 'You failed bro',
    'quiz_feedback_range' => [
      'from' => 0,
      'to' => 49,
    ],
  ]);
  $c
    ->save();

  // By default, the feedback is after the quiz.
  $quiz = $this
    ->createQuiz(array(
    'pass_rate' => 75,
    'summary_pass' => 'This is the summary if passed',
    'summary_default' => 'This is the default summary text',
  ));
  $quiz
    ->get('result_options')
    ->appendItem($a);
  $quiz
    ->get('result_options')
    ->appendItem($b);
  $quiz
    ->get('result_options')
    ->appendItem($c);
  $quiz
    ->save();

  // 3 questions.
  $question1 = $this
    ->createQuestion(array(
    'type' => 'truefalse',
    'truefalse_correct' => 1,
    'feedback' => 'Q1Feedback',
  ));
  $this
    ->linkQuestionToQuiz($question1, $quiz);
  $question2 = $this
    ->createQuestion(array(
    'type' => 'truefalse',
    'truefalse_correct' => 1,
    'feedback' => 'Q2Feedback',
  ));
  $this
    ->linkQuestionToQuiz($question2, $quiz);
  $question3 = $this
    ->createQuestion(array(
    'type' => 'truefalse',
    'truefalse_correct' => 1,
    'feedback' => 'Q3Feedback',
  ));
  $this
    ->linkQuestionToQuiz($question3, $quiz);

  // Test 100%.
  $this
    ->drupalLogin($this->user);
  $this
    ->drupalGet("quiz/{$quiz->id()}/take");
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question1->id()}][answer]" => 1,
  ), t('Next'));
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question2->id()}][answer]" => 1,
  ), t('Next'));
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question3->id()}][answer]" => 1,
  ), t('Finish'));
  $this
    ->assertText('You got 90 or more on the quiz');
  $this
    ->assertText('This is the summary if passed');
  $this
    ->assertNoText('This is the default summary text');

  // Test 66%.
  $this
    ->drupalGet("quiz/{$quiz->id()}/take");
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question1->id()}][answer]" => 1,
  ), t('Next'));
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question2->id()}][answer]" => 1,
  ), t('Next'));
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question3->id()}][answer]" => 0,
  ), t('Finish'));
  $this
    ->assertText('You got between 50 and 89');
  $this
    ->assertNoText('This is the summary if passed');
  $this
    ->assertText('This is the default summary text');

  // Test 33%.
  $this
    ->drupalGet("quiz/{$quiz->id()}/take");
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question1->id()}][answer]" => 1,
  ), t('Next'));
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question2->id()}][answer]" => 0,
  ), t('Next'));
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question3->id()}][answer]" => 0,
  ), t('Finish'));
  $this
    ->assertText('You failed bro');
  $this
    ->assertNoText('This is the summary if passed');
  $this
    ->assertText('This is the default summary text');
}