View source
<?php
namespace Drupal\Tests\quiz_matching\Functional;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\Tests\quiz\Functional\QuizQuestionTestBase;
class MatchingTestCase extends QuizQuestionTestBase {
public static $modules = [
'quiz_matching',
];
public function testCreateQuizQuestion($settings = []) {
$this
->drupalLogin($this->admin);
$question_node = $this
->createQuestion($settings + [
'type' => 'matching',
'title' => 'MA 1 title',
'body' => 'MA 1 body text',
'choice_penalty' => 0,
]);
for ($i = 0; $i <= 2; $i++) {
$a = Paragraph::create([
'type' => 'quiz_matching',
'matching_question' => "MAQ " . ($i + 1),
'matching_answer' => "MAA " . ($i + 1),
]);
$a
->save();
$question_node
->get('quiz_matching')
->appendItem($a);
}
$question_node
->save();
return $question_node;
}
public function testTakeQuestion() {
$quiz_node = $this
->createQuiz([
'review_options' => [
'end' => array_combine([
'answer_feedback',
'score',
], [
'answer_feedback',
'score',
]),
],
]);
$question_node = $this
->testCreateQuizQuestion();
$this
->linkQuestionToQuiz($question_node, $quiz_node);
$this
->drupalGet("quiz/{$quiz_node->id()}/questions");
$this
->assertText('MA 1 title');
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->assertNoText('MA 1 title');
$this
->assertText('MA 1 body text');
$this
->assertText('MAQ 1');
$this
->assertText('MAQ 2');
$this
->assertText('MAQ 3');
$this
->assertText('MAA 1');
$this
->assertText('MAA 2');
$this
->assertText('MAA 3');
$this
->drupalPostForm(NULL, [], t('Finish'));
$this
->assertText('You need to match at least one of the items.');
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalPostForm(NULL, [
"question[1][answer][user_answer][1]" => 1,
"question[1][answer][user_answer][2]" => 2,
"question[1][answer][user_answer][3]" => 3,
], t('Finish'));
$this
->assertText('You got 3 of 3 possible points.');
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalPostForm(NULL, [
"question[1][answer][user_answer][1]" => 1,
"question[1][answer][user_answer][2]" => 2,
"question[1][answer][user_answer][3]" => 2,
], t('Finish'));
$this
->assertText('You got 2 of 3 possible points.');
}
public function testChoicePenalty() {
$quiz_node = $this
->createQuiz(array(
'review_options' => array(
'end' => array_combine([
'answer_feedback',
'score',
], [
'answer_feedback',
'score',
]),
),
));
$question_node = $this
->testCreateQuizQuestion([
'choice_penalty' => 1,
]);
$this
->linkQuestionToQuiz($question_node, $quiz_node);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question_node->id()}][answer][user_answer][1]" => 1,
"question[{$question_node->id()}][answer][user_answer][2]" => 1,
"question[{$question_node->id()}][answer][user_answer][3]" => 3,
], t('Finish'));
$this
->assertText('You got 1 of 3 possible points.');
}
public function testEditQuestionResponse() {
$question_node = $this
->testCreateQuizQuestion();
$quiz_node = $this
->linkQuestionToQuiz($question_node);
$question_node2 = $this
->testCreateQuizQuestion();
$this
->linkQuestionToQuiz($question_node2, $quiz_node);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->drupalPostForm(NULL, [
"question[{$question_node->id()}][answer][user_answer][1]" => 1,
], t('Next'));
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->drupalPostForm(NULL, [
"question[{$question_node->id()}][answer][user_answer][1]" => 0,
"question[{$question_node->id()}][answer][user_answer][2]" => 2,
], t('Next'));
}
public function getQuestionType() {
return 'matching';
}
public function testMatchingShuffle() {
$config = \Drupal::configFactory()
->getEditable('quiz_matching.settings');
$config
->set('shuffle', 1)
->save();
$quiz = $this
->createQuiz();
$question = $this
->testCreateQuizQuestion();
$quiz
->addQuestion($question);
$this
->drupalLogin($this->user);
for ($i = 1; $i <= 5; $i++) {
$this
->drupalGet("quiz/{$quiz->id()}/take");
$content = $this
->getSession()
->getPage()
->getContent();
$one = strpos($content, 'MAQ 1');
$two = strpos($content, 'MAQ 2');
$three = strpos($content, 'MAQ 3');
$result[] = $one < $two && $two < $three;
}
$this
->assertNotEquals(count($result), count(array_filter($result)), 'Matching questions were shuffled.');
}
}