View source
<?php
namespace Drupal\Tests\quiz_page\Functional;
use Drupal\quiz\Entity\QuizResult;
use Drupal\Tests\quiz\Functional\QuizQuestionTestBase;
use function db_query;
class QuizPageTestCase extends QuizQuestionTestBase {
protected $profile = 'standard';
public static $modules = [
'quiz_page',
'quiz_truefalse',
];
public function getQuestionType() {
return 'page';
}
public function testQuizPageParentage() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->createQuiz();
$question_node1 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
'body' => 'TF 1 body text',
]);
$this
->linkQuestionToQuiz($question_node1, $quiz_node);
$question_node2 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
'body' => 'TF 2 body text',
]);
$this
->linkQuestionToQuiz($question_node2, $quiz_node);
$question_node3 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
'body' => 'TF 3 body text',
]);
$this
->linkQuestionToQuiz($question_node3, $quiz_node);
$page_node1 = $this
->createQuestion([
'type' => 'page',
]);
$this
->linkQuestionToQuiz($page_node1, $quiz_node);
$page_node2 = $this
->createQuestion([
'type' => 'page',
]);
$this
->linkQuestionToQuiz($page_node2, $quiz_node);
$this
->drupalGet("quiz/{$quiz_node->id()}/questions");
$post = [
"question_list[{$question_node1->getRevisionId()}][qqr_pid]" => 4,
"question_list[{$question_node2->getRevisionId()}][qqr_pid]" => 4,
"question_list[{$question_node3->getRevisionId()}][qqr_pid]" => 5,
"question_list[{$page_node1->getRevisionId()}][weight]" => 2,
"question_list[{$question_node1->getRevisionId()}][weight]" => 3,
"question_list[{$question_node2->getRevisionId()}][weight]" => 4,
"question_list[{$page_node2->getRevisionId()}][weight]" => 3,
"question_list[{$question_node3->getRevisionId()}][weight]" => 4,
];
$this
->drupalPostForm(NULL, $post, t('Submit'));
$sql = "SELECT * FROM {quiz_question_relationship}";
$data = \Drupal::database()
->query($sql)
->fetchAllAssoc('qqr_id');
foreach ($data as $qnr_id => $rel) {
switch ($qnr_id) {
case 1:
case 2:
$this
->assertEqual($rel->qqr_pid, 4);
break;
case 3:
$this
->assertEqual($rel->qqr_pid, 5);
break;
case 4:
case 5:
$this
->assertNull($rel->qqr_pid);
break;
}
}
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->assertFieldByName("question[{$question_node1->id()}][answer]");
$this
->assertFieldByName("question[{$question_node2->id()}][answer]");
$this
->assertNoFieldByName("question[{$question_node3->id()}][answer]");
$this
->drupalPostForm(NULL, [
"question[{$question_node1->id()}][answer]" => 1,
"question[{$question_node2->id()}][answer]" => 1,
], t('Next'));
$this
->assertNoFieldByName("question[{$question_node1->id()}][answer]");
$this
->assertNoFieldByName("question[{$question_node2->id()}][answer]");
$this
->assertFieldByName("question[{$question_node3->id()}][answer]");
$this
->drupalPostForm(NULL, [
"question[{$question_node3->id()}][answer]" => 1,
], t('Finish'));
$this
->assertText('You got 3 of 3 possible points.');
$this
->assertText('TF 1 body text');
$this
->assertText('TF 2 body text');
$this
->assertText('TF 3 body text');
foreach (QuizResult::loadMultiple() as $quiz_result) {
$quiz_result
->delete();
}
$this
->drupalLogin($this->admin);
$this
->drupalGet("quiz/{$quiz_node->id()}/edit");
$this
->drupalPostForm(NULL, [
'revision' => 1,
], t('Save'));
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->assertText("Page 1 of 2");
$this
->assertFieldByName("question[{$question_node1->id()}][answer]");
$this
->assertFieldByName("question[{$question_node2->id()}][answer]");
$this
->assertNoFieldByName("question[{$question_node3->id()}][answer]");
$this
->drupalPostForm(NULL, [
"question[{$question_node1->id()}][answer]" => 1,
"question[{$question_node2->id()}][answer]" => 1,
], t('Next'));
$this
->assertText("Page 2 of 2");
$this
->assertNoFieldByName("question[{$question_node1->id()}][answer]");
$this
->assertNoFieldByName("question[{$question_node2->id()}][answer]");
$this
->assertFieldByName("question[{$question_node3->id()}][answer]");
$this
->drupalPostForm(NULL, [], t('Back'));
$this
->assertText("Page 1 of 2");
$this
->drupalPostForm(NULL, [], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question_node3->id()}][answer]" => 1,
], t('Finish'));
}
public function testCreateQuizQuestion($settings = []) {
$this
->drupalLogin($this->admin);
$question_node = $this
->createQuestion([
'type' => $this
->getQuestionType(),
'title' => 'PG 1 title',
'body' => 'PG 1 body text.',
] + $settings);
return $question_node;
}
public function testPageFeedback() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->createQuiz([
'review_options' => [
'question' => [
'question_feedback' => 'question_feedback',
],
],
]);
$question_node1 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
'body' => 'TF 1 body text',
'feedback' => 'This is the feedback for question 1.',
]);
$this
->linkQuestionToQuiz($question_node1, $quiz_node);
$question_node2 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
'body' => 'TF 2 body text',
'feedback' => 'This is the feedback for question 2.',
]);
$this
->linkQuestionToQuiz($question_node2, $quiz_node);
$question_node3 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
'body' => 'TF 3 body text',
'feedback' => 'This is the feedback for question 3.',
]);
$this
->linkQuestionToQuiz($question_node3, $quiz_node);
$page_node1 = $this
->createQuestion([
'type' => 'page',
'body' => 'PG 1 body text',
]);
$this
->linkQuestionToQuiz($page_node1, $quiz_node);
$this
->drupalGet("quiz/{$quiz_node->id()}/questions");
$post = [
"question_list[{$question_node1->getRevisionId()}][qqr_pid]" => 4,
"question_list[{$question_node2->getRevisionId()}][qqr_pid]" => 4,
"question_list[{$page_node1->getRevisionId()}][weight]" => 1,
"question_list[{$question_node1->getRevisionId()}][weight]" => 2,
"question_list[{$question_node2->getRevisionId()}][weight]" => 3,
"question_list[{$question_node3->getRevisionId()}][weight]" => 4,
];
$this
->drupalPostForm(NULL, $post, t('Submit'));
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question_node1->id()}][answer]" => 1,
"question[{$question_node2->id()}][answer]" => 1,
], t('Next'));
$this
->assertText('This is the feedback for question 1.');
$this
->assertText('This is the feedback for question 2.');
$this
->assertNoText('This is the feedback for question 3.');
}
}