You are here

public function QuizFeedbackTest::testFeedbackTimes in Quiz 8.5

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

Test custom feedback types.

File

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

Class

QuizFeedbackTest
Test quiz feedback.

Namespace

Drupal\Tests\quiz\Functional

Code

public function testFeedbackTimes() {
  $this
    ->drupalLogin($this->admin);
  $component = [
    'expression' => [
      'id' => 'rules_and',
      'conditions' => array(
        [
          'id' => 'rules_condition',
          'uuid' => 'ca2a6b2f-3b17-449e-b913-d64b52c17203',
          'weight' => 2,
          'context_values' => array(
            'operation' => '==',
            'value' => '2',
          ),
          'context_mapping' => array(
            'data' => 'quiz_result.attempt.value',
          ),
          'condition_id' => 'rules_data_comparison',
          'negate' => 0,
        ],
      ),
    ],
    'context_definitions' => [
      'quiz_result' => [
        'type' => 'entity:quiz_result',
        'label' => 'Quiz result',
        'description' => 'Quiz result to evaluate feedback',
      ],
    ],
  ];
  QuizFeedbackType::create([
    'label' => 'After two attempts',
    'id' => 'after2attempts',
    'component' => $component,
  ])
    ->save();

  // Feedback but, only after second attempt (rule).
  $quiz = $this
    ->createQuiz(array(
    'review_options' => array(
      'after2attempts' => array(
        'solution' => 'solution',
      ),
    ),
  ));

  // Set up a Quiz with one question that has a body and a summary.
  $question1 = $this
    ->createQuestion(array(
    'type' => 'truefalse',
    'truefalse_correct' => 1,
  ));
  $this
    ->linkQuestionToQuiz($question1, $quiz);

  // Test no feedback.
  $this
    ->drupalLogin($this->user);
  $this
    ->drupalGet("quiz/{$quiz->id()}/take");
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question1->id()}][answer]" => 1,
  ), t('Finish'));
  $this
    ->assertNoText('Correct answer');

  // Take again.
  $this
    ->drupalGet("quiz/{$quiz->id()}/take");
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question1->id()}][answer]" => 1,
  ), t('Finish'));
  $this
    ->assertText('Correct answer');
}