You are here

public function QuizResultTest::testQuizResultAnswerExport in Quiz 8.6

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

Test our wildcard answer exporter.

File

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

Class

QuizResultTest
Test quiz results behavior.

Namespace

Drupal\Tests\quiz\Functional

Code

public function testQuizResultAnswerExport() {
  $settings = array(
    'title' => 'MCQ 1 Title',
    'type' => 'multichoice',
    'choice_multi' => 0,
    'choice_random' => 0,
    'choice_boolean' => 0,
    'body' => 'MCQ 1 body text',
  );
  $settings['alternatives'][0]['answer']['value'] = 'This is the A answer';
  $settings['alternatives'][0]['answer']['format'] = 'filtered_html';
  $settings['alternatives'][0]['score_if_chosen'] = 1;
  $settings['alternatives'][0]['feedback_if_chosen']['format'] = 'filtered_html';
  $settings['alternatives'][0]['feedback_if_not_chosen']['format'] = 'filtered_html';
  $settings['alternatives'][1]['answer']['value'] = 'This is the B answer';
  $settings['alternatives'][1]['answer']['format'] = 'filtered_html';
  $settings['alternatives'][1]['score_if_chosen'] = 0;
  $settings['alternatives'][1]['feedback_if_chosen']['format'] = 'filtered_html';
  $settings['alternatives'][1]['feedback_if_not_chosen']['format'] = 'filtered_html';
  $question_node = $this
    ->createQuestion($settings);

  // Reload to get the answer IDs.
  $question_node = node_load($question_node->nid);
  $quiz_node = $this
    ->linkQuestionToQuiz($question_node);
  $this
    ->drupalLogin($this->admin);
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take");
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question_node->nid}][answer][user_answer]" => $question_node->alternatives[0]['id'],
  ), t('Finish'));

  // Make a modification to the default view.
  $view = views_get_view('quiz_results');
  $view
    ->set_arguments(array(
    $quiz_node->nid,
  ));
  $view
    ->set_display('page_1');
  $fields = $view->display_handler
    ->get_option('fields');
  $newfield = array();
  $newfield['id'] = 'answers';
  $newfield['table'] = 'quiz_node_results';
  $newfield['field'] = 'answers';
  $fields['answers'] = $newfield;
  $view->display_handler
    ->set_option('fields', $fields);
  $view
    ->save();

  // Verify the user's answer appears on our modified report.
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/quiz/results");
  $this
    ->assertText('1. MCQ 1 Title');
  $this
    ->assertText('This is the A answer');
  $this
    ->assertNoText('This is the B answer');
}