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'),
);
}
public function setUp($modules = array(), $admin_permissions = array(), $user_permissions = array()) {
$modules[] = 'truefalse';
$modules[] = 'multichoice';
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');
}
public 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_answer_id', $quiz_result_answer->result_answer_id)
->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_answer_id' => $quiz_truefalse_result_answer->result_answer_id,
), 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_answer_id', $quiz_truefalse_result_answer->result_answer_id)
->execute();
$quiz_truefalse_result_answer = $result
->fetch();
$this
->assertTrue(empty($quiz_truefalse_result_answer), 'Did not find quiz result question answer');
}
public 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(
'is_invalid' => 1,
), TRUE);
$this
->assertEqual(count($results), 2, 'Found 2 invalid quiz results');
$results = entity_load('quiz_result', FALSE, array(
'is_invalid' => 0,
), 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(
'is_invalid' => 1,
), TRUE);
$this
->assertEqual(count($results), 3, 'Found 3 invalid quiz results');
$results = entity_load('quiz_result', FALSE, array(
'is_invalid' => 0,
), 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(
'is_invalid' => 1,
), TRUE);
$this
->assertEqual(count($results), 4, 'Found 4 invalid quiz results');
$results = entity_load('quiz_result', FALSE, array(
'is_invalid' => 0,
), 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%');
variable_set('quiz_remove_invalid_quiz_record', 86400);
drupal_cron_run();
$results = entity_load('quiz_result', FALSE, array(
'is_invalid' => 1,
), TRUE);
$this
->assertEqual(count($results), 4, 'Found 4 invalid quiz results');
$results = entity_load('quiz_result', FALSE, array(
'is_invalid' => 0,
), 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%');
variable_set('quiz_remove_invalid_quiz_record', -86400);
drupal_cron_run();
$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%');
$this
->assertEqual($quiz_result->is_invalid, 0, 'Quiz score was valid');
}
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('Leave blank'));
$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
->assertNoFieldChecked("edit-question-{$question_node1->nid}-answer-1");
$this
->assertNoFieldChecked("edit-question-{$question_node1->nid}-answer-0");
$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'));
}
public 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_show_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
->assertText('Question 1');
}
public function testQuizResultAnswerExport() {
$settings = array(
'title' => 'MCQ 1 Title',
'type' => 'multichoice',
'choice_multi' => 0,
'choice_random' => 0,
'choice_boolean' => 0,
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => '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
->drupalCreateNode($settings);
$question_node = node_load($question_node->nid);
$quiz_node = $this
->linkQuestionToQuiz($question_node);
$this
->drupalLogin($this->admin);
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(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("node/{$quiz_node->nid}/quiz/results");
$this
->assertText('1. MCQ 1 Title');
$this
->assertText('This is the A answer');
$this
->assertNoText('This is the B answer');
}
public function testQuizResultBundles() {
$result_type = entity_create('quiz_result_type', array(
'type' => 'type_A',
'label' => t('Bundle type A'),
));
$result_type
->save();
$result_type = entity_create('quiz_result_type', array(
'type' => 'type_B',
'label' => t('Bundle type B'),
));
$result_type
->save();
cache_clear_all();
$field = array(
'field_name' => 'result_field_a',
'type' => 'text',
);
$instance = array(
'field_name' => 'result_field_a',
'entity_type' => 'quiz_result',
'bundle' => 'type_A',
'label' => 'Result field A',
'widget' => array(
'active' => 1,
'module' => 'text',
'settings' => array(
'size' => 60,
),
'type' => 'text_textfield',
'weight' => 1,
),
'settings' => array(
'quiz_result_show_field' => 1,
),
'required' => 1,
);
field_create_field($field);
$instance_a = field_create_instance($instance);
$field = array(
'field_name' => 'result_field_b',
'type' => 'text',
);
$instance = array(
'field_name' => 'result_field_b',
'entity_type' => 'quiz_result',
'bundle' => 'type_B',
'label' => 'Result field B',
'widget' => array(
'active' => 1,
'module' => 'text',
'settings' => array(
'size' => 60,
),
'type' => 'text_textfield',
'weight' => 1,
),
'settings' => array(
'quiz_result_show_field' => 1,
),
'required' => 1,
);
field_create_field($field);
$instance_b = field_create_instance($instance);
$quizNodeA = $this
->drupalCreateQuiz(array(
'result_type' => 'type_A',
));
$question_node1 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
));
$this
->linkQuestionToQuiz($question_node1, $quizNodeA);
$quizNodeB = $this
->drupalCreateQuiz(array(
'result_type' => 'type_B',
));
$question_node2 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
));
$this
->linkQuestionToQuiz($question_node2, $quizNodeB);
$this
->drupalLogin($this->user);
$this
->drupalGet("node/{$quizNodeA->nid}/take");
$this
->assertFieldById('edit-result-field-a-und-0-value');
$this
->assertNoFieldById('edit-result-field-b-und-0-value');
$result = entity_load('quiz_result', FALSE, array(
'nid' => $quizNodeA->nid,
'uid' => $this->user->uid,
));
$this
->assertFalse($result);
$this
->drupalPost(NULL, array(), t('Start Quiz'));
$this
->assertText('field is required');
$this
->drupalPost(NULL, array(
'result_field_a[und][0][value]' => 'test 123',
), t('Start Quiz'));
$this
->drupalGet("node/{$quizNodeB->nid}/take");
$this
->assertFieldById('edit-result-field-b-und-0-value');
$this
->assertNoFieldById('edit-result-field-a-und-0-value');
$instance_b['settings']['quiz_result_show_field'] = 0;
field_update_instance($instance_b);
$this
->drupalGet("node/{$quizNodeB->nid}/take");
$this
->assertNoFieldById('edit-result-field-a-und-0-value');
$this
->assertNoFieldById('edit-result-field-b-und-0-value');
}
public function testBrokenResults() {
$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'));
node_delete($question_node1->nid);
$this
->drupalGet("node/{$quiz_node->nid}/quiz-results/1");
$this
->assertResponse(200, 'Saw the results page.');
}
}