View source
<?php
namespace Drupal\Tests\quiz\Functional;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\quiz\Entity\Quiz;
use Drupal\quiz\Entity\QuizQuestion;
use Drupal\quiz\Entity\QuizQuestionRelationship;
use Drupal\quiz\Entity\QuizResult;
use Drupal\quiz\Entity\QuizResultAnswer;
use function node_load;
class QuizResultTest extends QuizTestBase {
public static $modules = array(
'quiz_truefalse',
'quiz_multichoice',
);
public function testPassRateSummary() {
$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();
$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();
$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);
$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');
$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');
$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');
}
public function testQuizResultCrud() {
$this
->drupalLogin($this->admin);
$question1 = $this
->createQuestion(array(
'type' => 'truefalse',
'truefalse_correct' => 1,
));
$quiz = $this
->linkQuestionToQuiz($question1);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, array(
"question[{$question1->id()}][answer]" => 1,
), t('Finish'));
$q = Quiz::load(1);
$qq = QuizQuestion::load(1);
$qqr = QuizQuestionRelationship::load(1);
$qr = QuizResult::load(1);
$qra = QuizResultAnswer::load(1);
$this
->assertEquals($qq
->id(), $qqr
->getQuiz()
->id(), 'Question belongs to the relationship.');
$this
->assertEquals($qqr
->getQuiz()
->id(), $q
->id(), 'Relationship belongs to the quiz.');
$this
->assertEquals($qr
->id(), $qra
->getQuizResult()
->id(), 'Answer belongs to the result.');
$q
->delete();
$q = Quiz::load(1);
$qq = QuizQuestion::load(1);
$qqr = QuizQuestionRelationship::load(1);
$qr = QuizResult::load(1);
$qra = QuizResultAnswer::load(1);
$this
->assertEquals($q, NULL);
$this
->assertEquals($qqr, NULL);
$this
->assertNotEquals($qq, NULL);
$this
->assertEquals($qr, NULL);
$this
->assertEquals($qra, NULL);
}
public function testQuizResultAccess() {
$this
->drupalLogin($this->admin);
$question1 = $this
->createQuestion(array(
'type' => 'truefalse',
'truefalse_correct' => 1,
));
$quiz_node = $this
->linkQuestionToQuiz($question1);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalPostForm(NULL, array(
"question[{$question1->id()}][answer]" => 1,
), t('Finish'));
$resultsUrl = $this
->getUrl();
$this
->drupalGet($resultsUrl);
$this
->assertResponse(200, t('User can view own result'));
$this
->drupalLogout();
$this
->drupalGet($resultsUrl);
$this
->assertSession()
->statusCodeEquals(403);
}
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);
$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'));
$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();
$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');
}
public function testBrokenResults() {
$this
->drupalLogin($this->admin);
$question1 = $this
->createQuestion(array(
'type' => 'truefalse',
'truefalse_correct' => 1,
));
$quiz_node = $this
->linkQuestionToQuiz($question1);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalPostForm(NULL, array(
"question[{$question1->id()}][answer]" => 1,
), t('Finish'));
$question1
->delete();
$this
->drupalGet("quiz/{$quiz_node->id()}/result/1");
$this
->assertResponse(200, 'Saw the results page.');
}
}