View source
<?php
use Drupal\Tests\quiz\Functional\QuizQuestionTestBase;
class QuizPageTestCase extends QuizQuestionTestBase {
public function getQuestionType() {
return 'page';
}
public static function getInfo() {
return array(
'name' => t('Quiz page'),
'description' => t('Unit test for quiz pages'),
'group' => t('Quiz'),
);
}
public function setUp($modules = array(), $admin_permissions = array(), $user_permissions = array()) {
$modules[] = 'quiz_page';
$modules[] = 'truefalse';
parent::setUp($modules, $admin_permissions, $user_permissions);
}
public function testQuizPageParentage() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->createQuiz();
$question_node1 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'TF 1 body text',
),
),
),
));
$this
->linkQuestionToQuiz($question_node1, $quiz_node);
$question_node2 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'TF 2 body text',
),
),
),
));
$this
->linkQuestionToQuiz($question_node2, $quiz_node);
$question_node3 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'TF 3 body text',
),
),
),
));
$this
->linkQuestionToQuiz($question_node3, $quiz_node);
$page_node1 = $this
->drupalCreateNode(array(
'type' => 'quiz_page',
));
$this
->linkQuestionToQuiz($page_node1, $quiz_node);
$page_node2 = $this
->drupalCreateNode(array(
'type' => 'quiz_page',
));
$this
->linkQuestionToQuiz($page_node2, $quiz_node);
$this
->drupalGet("quiz/{$quiz_node->nid}/quiz/questions");
$post = array(
"qqr_pids[{$question_node1->nid}-{$question_node1->vid}]" => 4,
"qqr_pids[{$question_node2->nid}-{$question_node2->vid}]" => 4,
"qqr_pids[{$question_node3->nid}-{$question_node3->vid}]" => 5,
"weights[{$page_node1->nid}-{$page_node1->vid}]" => 2,
"weights[{$question_node1->nid}-{$question_node1->vid}]" => 3,
"weights[{$question_node2->nid}-{$question_node2->vid}]" => 4,
"weights[{$page_node2->nid}-{$page_node2->vid}]" => 3,
"weights[{$question_node3->nid}-{$question_node3->vid}]" => 4,
);
$this
->drupalPostForm(NULL, $post, t('Submit'));
$sql = "SELECT * FROM {quiz_node_relationship}";
$data = db_query($sql)
->fetchAllAssoc('qnr_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->nid}/take");
$this
->assertFieldByName("question[{$question_node1->nid}][answer]");
$this
->assertFieldByName("question[{$question_node2->nid}][answer]");
$this
->assertNoFieldByName("question[{$question_node3->nid}][answer]");
$this
->drupalPostForm(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
"question[{$question_node2->nid}][answer]" => 1,
), t('Next'));
$this
->assertNoFieldByName("question[{$question_node1->nid}][answer]");
$this
->assertNoFieldByName("question[{$question_node2->nid}][answer]");
$this
->assertFieldByName("question[{$question_node3->nid}][answer]");
$this
->drupalPostForm(NULL, array(
"question[{$question_node3->nid}][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');
$this
->drupalLogin($this->admin);
$this
->drupalGet("quiz/{$quiz_node->nid}/quiz/questions");
$post = array(
"qqr_pids[{$question_node1->nid}-{$question_node1->vid}]" => 9,
"qqr_pids[{$question_node2->nid}-{$question_node2->vid}]" => 9,
"qqr_pids[{$question_node3->nid}-{$question_node3->vid}]" => 10,
"weights[{$page_node1->nid}-{$page_node1->vid}]" => 2,
"weights[{$question_node1->nid}-{$question_node1->vid}]" => 3,
"weights[{$question_node2->nid}-{$question_node2->vid}]" => 4,
"weights[{$page_node2->nid}-{$page_node2->vid}]" => 3,
"weights[{$question_node3->nid}-{$question_node3->vid}]" => 4,
);
$this
->drupalPostForm(NULL, $post, t('Submit'));
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->nid}/take");
$this
->assertText("Page 1 of 2");
$this
->assertFieldByName("question[{$question_node1->nid}][answer]");
$this
->assertFieldByName("question[{$question_node2->nid}][answer]");
$this
->assertNoFieldByName("question[{$question_node3->nid}][answer]");
$this
->drupalPostForm(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
"question[{$question_node2->nid}][answer]" => 1,
), t('Next'));
$this
->assertText("Page 2 of 2");
$this
->assertNoFieldByName("question[{$question_node1->nid}][answer]");
$this
->assertNoFieldByName("question[{$question_node2->nid}][answer]");
$this
->assertFieldByName("question[{$question_node3->nid}][answer]");
$this
->drupalPostForm(NULL, array(
"question[{$question_node3->nid}][answer]" => 1,
), t('Finish'));
}
public function testCreateQuizQuestion($settings = array()) {
$this
->drupalLogin($this->admin);
$question_node = $this
->drupalCreateNode(array(
'type' => $this->question_node_type,
'title' => 'PG 1 title',
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'PG 1 body text.',
),
),
),
) + $settings);
return $question_node;
}
public function testEditQuestionResponse() {
}
public function testPageFeedback() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->createQuiz(array(
'review_options' => array(
'question' => array(
'question_feedback' => 'question_feedback',
),
),
));
$question_node1 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'TF 1 body text',
),
),
),
));
$this
->linkQuestionToQuiz($question_node1, $quiz_node);
$question_node2 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'TF 2 body text',
),
),
),
));
$entities = entity_load('quiz_question', FALSE, array(
'nid' => $question_node1->nid,
'vid' => $question_node1->vid,
));
$quiz_question = reset($entities);
$quiz_question->feedback = 'This is the feedback for question 1.';
$quiz_question->feedback_format = filter_default_format();
entity_save('quiz_question', $quiz_question);
$entities = entity_load('quiz_question', FALSE, array(
'nid' => $question_node2->nid,
'vid' => $question_node2->vid,
));
$quiz_question = reset($entities);
$quiz_question->feedback = 'This is the feedback for question 2.';
$quiz_question->feedback_format = filter_default_format();
entity_save('quiz_question', $quiz_question);
$this
->linkQuestionToQuiz($question_node2, $quiz_node);
$page_node1 = $this
->drupalCreateNode(array(
'type' => 'quiz_page',
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'PG 1 body text',
),
),
),
));
$this
->linkQuestionToQuiz($page_node1, $quiz_node);
$this
->drupalGet("quiz/{$quiz_node->nid}/quiz/questions");
$post = array(
"qqr_pids[{$question_node1->nid}-{$question_node1->vid}]" => 3,
"qqr_pids[{$question_node2->nid}-{$question_node2->vid}]" => 3,
"weights[{$page_node1->nid}-{$page_node1->vid}]" => 1,
"weights[{$question_node1->nid}-{$question_node1->vid}]" => 2,
"weights[{$question_node2->nid}-{$question_node2->vid}]" => 3,
);
$this
->drupalPostForm(NULL, $post, t('Submit'));
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->nid}/take");
$this
->drupalPostForm(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
"question[{$question_node2->nid}][answer]" => 1,
), t('Finish'));
$this
->assertText('This is the feedback for question 1');
$this
->assertText('This is the feedback for question 2');
}
}