View source
<?php
class QuizResultTestCase extends QuizTestCase {
public static function getInfo() {
return array(
'name' => t('Quiz results'),
'description' => t('Unit test for Quiz results.'),
'group' => t('Quiz'),
);
}
function setUp($modules = array(), $admin_permissions = array(), $user_permissions = array()) {
$modules[] = 'truefalse';
parent::setUp($modules);
}
public function testPassRateSummary() {
$quiz_node = $this
->drupalCreateQuiz(array(
'pass_rate' => 75,
'summary_pass' => 'This is the summary if passed',
'summary_pass_format' => 'plain_text',
'summary_default' => 'This is the default summary text',
'summary_default_format' => 'plain_text',
'resultoptions' => array(
array(
'option_name' => '90 and higher',
'option_summary' => 'You got 90 or more on the quiz',
'option_summary_format' => 'filtered_html',
'option_start' => 90,
'option_end' => 100,
),
array(
'option_name' => '50 and higher',
'option_summary' => 'You got between 50 and 89',
'option_summary_format' => 'filtered_html',
'option_start' => 50,
'option_end' => 89,
),
array(
'option_name' => 'Below 50',
'option_summary' => 'You failed bro',
'option_summary_format' => 'filtered_html',
'option_start' => 0,
'option_end' => 49,
),
),
));
$question_node1 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
'feedback' => 'Q1Feedback',
));
$this
->linkQuestionToQuiz($question_node1, $quiz_node);
$question_node2 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
'feedback' => 'Q2Feedback',
));
$this
->linkQuestionToQuiz($question_node2, $quiz_node);
$question_node3 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
'feedback' => 'Q3Feedback',
));
$this
->linkQuestionToQuiz($question_node3, $quiz_node);
$this
->drupalLogin($this->user);
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node2->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node3->nid}][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("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node2->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node3->nid}][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("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node2->nid}][answer]" => 0,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node3->nid}][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');
}
function testQuizResultCrud() {
$this
->drupalLogin($this->admin);
$question_node1 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
));
$quiz_node = $this
->linkQuestionToQuiz($question_node1);
$this
->drupalLogin($this->user);
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
), t('Finish'));
$quiz_result = entity_load('quiz_result', FALSE, array(
'nid' => $quiz_node->nid,
'vid' => $quiz_node->vid,
'uid' => $this->user->uid,
));
$this
->assertFalse(empty($quiz_result), 'Found quiz result');
$quiz_result = reset($quiz_result);
$quiz_result_answer = entity_load('quiz_result_answer', FALSE, array(
'result_id' => $quiz_result->result_id,
'question_nid' => $question_node1->nid,
'question_vid' => $question_node1->vid,
));
$this
->assertFalse(empty($quiz_result_answer), 'Found quiz result answer');
$quiz_result_answer = reset($quiz_result_answer);
$result = db_select('quiz_truefalse_user_answers', 'qtua')
->fields('qtua')
->condition('result_id', $quiz_result->result_id)
->condition('question_nid', $question_node1->nid)
->condition('question_vid', $question_node1->vid)
->execute();
$quiz_truefalse_result_answer = $result
->fetch();
$this
->assertFalse(empty($quiz_truefalse_result_answer), 'Found quiz result question answer');
entity_delete('quiz_result', $quiz_result->result_id);
$quiz_result2 = entity_load('quiz_result', FALSE, array(
'nid' => $quiz_node->nid,
'vid' => $quiz_node->vid,
'uid' => $this->user->uid,
), TRUE);
$this
->assertTrue(empty($quiz_result2), 'Did not find quiz result');
$quiz_result_answer = entity_load('quiz_result_answer', FALSE, array(
'result_id' => $quiz_result->result_id,
'question_nid' => $question_node1->nid,
'question_vid' => $question_node1->vid,
), TRUE);
$this
->assertTrue(empty($quiz_result_answer), 'Did not find quiz result answer');
$result = db_select('quiz_truefalse_user_answers', 'qtua')
->fields('qtua')
->condition('result_id', $quiz_result->result_id)
->condition('question_nid', $question_node1->nid)
->condition('question_vid', $question_node1->vid)
->execute();
$quiz_truefalse_result_answer = $result
->fetch();
$this
->assertTrue(empty($quiz_truefalse_result_answer), 'Did not find quiz result question answer');
}
function testQuizResultAccess() {
$this
->drupalLogin($this->admin);
$question_node1 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
));
$quiz_node = $this
->linkQuestionToQuiz($question_node1);
$this
->drupalLogin($this->user);
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][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
->assertNoResponse(200, t('Anonymous user cannot view result'));
}
public function testResultPruning() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->drupalCreateQuiz(array(
'keep_results' => QUIZ_KEEP_ALL,
));
$question_node1 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
));
$this
->linkQuestionToQuiz($question_node1, $quiz_node);
$question_node2 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
));
$this
->linkQuestionToQuiz($question_node2, $quiz_node);
$question_node3 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
));
$this
->linkQuestionToQuiz($question_node3, $quiz_node);
$question_node4 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
));
$this
->linkQuestionToQuiz($question_node4, $quiz_node);
$this
->drupalLogin($this->user);
for ($i = 1; $i <= 2; $i++) {
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node2->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node3->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node4->nid}][answer]" => 1,
), t('Finish'));
}
$results = entity_load('quiz_result');
$this
->assertEqual(count($results), 2, 'Found 2 quiz results.');
$quiz_node->keep_results = QUIZ_KEEP_LATEST;
node_save($quiz_node);
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node2->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node3->nid}][answer]" => 0,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node4->nid}][answer]" => 0,
), t('Finish'));
$results = entity_load('quiz_result', FALSE, array(), TRUE);
$this
->assertEqual(count($results), 1, 'Found only one quiz result');
$quiz_result = reset($results);
$this
->assertEqual($quiz_result->score, 50, 'Quiz result was 50%');
$quiz_node->keep_results = QUIZ_KEEP_BEST;
node_save($quiz_node);
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node2->nid}][answer]" => 0,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node3->nid}][answer]" => 0,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node4->nid}][answer]" => 0,
), t('Finish'));
$results = entity_load('quiz_result', FALSE, array(), TRUE);
$this
->assertTrue(count($results) == 1, 'Found only one quiz result');
$quiz_result = reset($results);
$this
->assertEqual($quiz_result->score, 50, 'Quiz score was 50%');
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node2->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node3->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node4->nid}][answer]" => 0,
), t('Finish'));
$results = entity_load('quiz_result', FALSE, array(), TRUE);
$this
->assertEqual(count($results), 1, 'Found only one quiz result');
$quiz_result = reset($results);
$this
->assertEqual($quiz_result->score, 75, 'Quiz score was 75%');
}
public function testBuildOnLastAttempt() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->drupalCreateQuiz(array(
'build_on_last' => 'correct',
));
$question_node1 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
));
$this
->linkQuestionToQuiz($question_node1, $quiz_node);
$question_node2 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
));
$this
->linkQuestionToQuiz($question_node2, $quiz_node);
$question_node3 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
));
$this
->linkQuestionToQuiz($question_node3, $quiz_node);
$question_node4 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
));
$this
->linkQuestionToQuiz($question_node4, $quiz_node);
$this
->drupalLogin($this->user);
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node2->nid}][answer]" => 0,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node3->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node4->nid}][answer]" => 0,
), t('Finish'));
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
'build_on_last' => 'correct',
), t('Start @quiz', array(
'@quiz' => QUIZ_NAME,
)));
$this
->assertFieldChecked("edit-question-{$question_node1->nid}-answer-1");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
), t('Next'));
$this
->assertNoFieldChecked("edit-question-{$question_node2->nid}-answer-1");
$this
->drupalPost(NULL, array(
"question[{$question_node2->nid}][answer]" => 0,
), t('Next'));
$this
->assertFieldChecked("edit-question-{$question_node3->nid}-answer-1");
$this
->drupalPost(NULL, array(
"question[{$question_node3->nid}][answer]" => 1,
), t('Next'));
$this
->assertNoFieldChecked("edit-question-{$question_node4->nid}-answer-1");
$this
->drupalPost(NULL, array(
"question[{$question_node4->nid}][answer]" => 0,
), t('Finish'));
$quiz_node->build_on_last = 'all';
node_save($quiz_node);
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node2->nid}][answer]" => 0,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node3->nid}][answer]" => 1,
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node4->nid}][answer]" => 0,
), t('Finish'));
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
'build_on_last' => 'all',
), t('Start @quiz', array(
'@quiz' => QUIZ_NAME,
)));
$this
->assertFieldChecked("edit-question-{$question_node1->nid}-answer-1");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
), t('Next'));
$this
->assertFieldChecked("edit-question-{$question_node2->nid}-answer-0");
$this
->drupalPost(NULL, array(
"question[{$question_node2->nid}][answer]" => 0,
), t('Next'));
$this
->assertFieldChecked("edit-question-{$question_node3->nid}-answer-1");
$this
->drupalPost(NULL, array(
"question[{$question_node3->nid}][answer]" => 1,
), t('Next'));
$this
->assertFieldChecked("edit-question-{$question_node4->nid}-answer-0");
$this
->drupalPost(NULL, array(
"question[{$question_node4->nid}][answer]" => 0,
), t('Finish'));
}
function testFieldableResults() {
$field = array(
'field_name' => 'quiz_result_field_a',
'type' => 'text',
);
$instance = array(
'field_name' => 'quiz_result_field_a',
'entity_type' => 'quiz_result',
'bundle' => 'quiz_result',
'label' => 'Result field A',
'widget' => array(
'active' => 1,
'module' => 'text',
'settings' => array(
'size' => 60,
),
'type' => 'text_textfield',
'weight' => 1,
),
'settings' => array(
'quiz_result_user_field' => 1,
),
'required' => 1,
);
field_create_field($field);
field_create_instance($instance);
$quizNodeA = $this
->drupalCreateQuiz();
$question_node1 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
'feedback' => 'Q1Feedback',
));
$this
->linkQuestionToQuiz($question_node1, $quizNodeA);
$this
->drupalLogin($this->user);
$this
->drupalGet("node/{$quizNodeA->nid}/take");
$this
->assertFieldById('edit-quiz-result-field-a-und-0-value');
$quiz_result = _quiz_active_result_id($this->user->uid, $quizNodeA->nid, $quizNodeA->vid);
$this
->assertFalse($quiz_result, 'Quiz result does not yet exist.');
$this
->drupalPost(NULL, array(), t('Start Quiz'));
$this
->assertText('field is required');
$this
->drupalPost(NULL, array(
'quiz_result_field_a[und][0][value]' => 'test 123',
), t('Start Quiz'));
$quiz_result = _quiz_active_result_id($this->user->uid, $quizNodeA->nid, $quizNodeA->vid);
$this
->assertTrue($quiz_result, 'Quiz result exists.');
}
}