short_answer.test in Quiz 7.5
Unit tests for the short_answer Module.
File
question_types/short_answer/short_answer.testView source
<?php
/**
* @file
* Unit tests for the short_answer Module.
*/
/**
* Test class for short answer.
*/
class ShortAnswerTestCase extends QuizQuestionTestCase {
var $question_node_type = 'short_answer';
public static function getInfo() {
return array(
'name' => t('Short answer'),
'description' => t('Unit test for Short answers question type.'),
'group' => t('Quiz'),
);
}
public function setUp($modules = array(), $admin_permissions = array(), $user_permissions = array()) {
parent::setUp(array(
'short_answer',
), array(
'score any quiz',
));
}
/**
* Test case insensitive graded questions.
*/
public function testGradeAnswerInsensitive() {
// Login as our privileged user.
$this
->drupalLogin($this->admin);
$question_node = $this
->testCreateQuizQuestion(array(
'correct_answer_evaluation' => ShortAnswerQuestion::ANSWER_INSENSITIVE_MATCH,
'correct_answer' => 'the Zero One Infinity rule',
));
$quiz_node = $this
->linkQuestionToQuiz($question_node);
// Test incorrect.
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node->nid}][answer]" => 'This is an incorrect answer.',
), t('Finish'));
$this
->assertText('Your score: 0%');
// Test correct.
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node->nid}][answer]" => 'the Zero One Infinity rule',
), t('Finish'));
$this
->assertText('Your score: 100%');
// Test correct.
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node->nid}][answer]" => 'the zero one Infinity rule',
), t('Finish'));
$this
->assertText('Your score: 100%');
}
/**
* Test case sensitive graded questions.
*/
public function testGradeAnswerSensitive() {
// Login as our privileged user.
$this
->drupalLogin($this->admin);
$quiz_node = $this
->drupalCreateQuiz(array(
'review_options' => array(
'end' => array(
'score' => 'score',
),
),
));
$question_node = $this
->testCreateQuizQuestion(array(
'correct_answer_evaluation' => ShortAnswerQuestion::ANSWER_MATCH,
'correct_answer' => 'the Zero One Infinity rule',
));
$this
->linkQuestionToQuiz($question_node, $quiz_node);
// Test incorrect.
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node->nid}][answer]" => 'the zero one Infinity rule',
), t('Finish'));
$this
->assertText('Your score: 0%');
// Test correct.
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node->nid}][answer]" => 'the Zero One Infinity rule',
), t('Finish'));
$this
->assertText('Your score: 100%');
}
/**
* Test regex graded questions.
*/
public function testGradeAnswerRegex() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->drupalCreateQuiz();
$question_node = $this
->testCreateQuizQuestion(array(
'correct_answer_evaluation' => ShortAnswerQuestion::ANSWER_REGEX,
'correct_answer' => '/Zero One Infinity/i',
));
$this
->linkQuestionToQuiz($question_node, $quiz_node);
// Test incorrect.
$this
->drupalLogin($this->user);
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node->nid}][answer]" => 'um some rule, I forget',
), t('Finish'));
$this
->assertText('Your score: 0%');
// Test correct.
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node->nid}][answer]" => 'the answer is the zero one infinity rule',
), t('Finish'));
$this
->assertText('Your score: 100%');
}
/**
* Test manually graded questions.
*
* Note: we use two questions here to make sure the grading form is handled
* correctly.
*/
public function testGradeAnswerManualFeedback() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->drupalCreateQuiz();
$question_node1 = $this
->testCreateQuizQuestion(array(
'correct_answer_evaluation' => ShortAnswerQuestion::ANSWER_MANUAL,
'correct_answer' => 'the Zero One Infinity rule',
));
$this
->linkQuestionToQuiz($question_node1, $quiz_node);
$question_node2 = $this
->testCreateQuizQuestion(array(
'correct_answer_evaluation' => ShortAnswerQuestion::ANSWER_MANUAL,
'correct_answer' => 'The number two is ridiculous and cannot exist',
));
$this
->linkQuestionToQuiz($question_node2, $quiz_node);
// Test correct.
$this
->drupalLogin($this->user);
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][answer]" => 'the answer is the zero one infinity rule',
), t('Next'));
$this
->drupalPost(NULL, array(
"question[{$question_node2->nid}][answer]" => 'the number two really is ridiculous',
), t('Finish'));
$this
->assertText('Your score: 0%');
// Strange behavior - extra spaces in HTML.
//$this->assertText('Score ? of 10');
$this
->assertText('This answer has not yet been scored.');
$this
->assertNoFieldByName('question[0][score]');
$this
->assertNoFieldByName('question[1][score]');
$this
->assertNoFieldByName('question[0][answer_feedback][value]');
$this
->assertNoFieldByName('question[1][answer_feedback][value]');
$this
->assertNoRaw(t('Save score'));
$url_of_result = $this
->getUrl();
$admin_url_of_result = preg_replace('#quiz-results/(\\d+)#', 'quiz/results/$1', $this
->getUrl());
// Test grading the question.
$this
->drupalLogin($this->admin);
$this
->drupalGet($admin_url_of_result);
$this
->assertFieldByName('question[0][score]');
$this
->assertFieldByName('question[1][score]');
$this
->drupalPost(NULL, array(
"question[0][score]" => 2,
"question[1][score]" => 3,
"question[0][answer_feedback][value]" => 'Feedback for answer 1.',
"question[1][answer_feedback][value]" => 'Feedback for answer 2.',
"question[0][answer_feedback][format]" => 'filtered_html',
"question[1][answer_feedback][format]" => 'filtered_html',
), t('Save score'));
$this
->assertText('The scoring data you provided has been saved.');
// Test the score is visible to the user.
$this
->drupalLogin($this->user);
$this
->drupalGet($url_of_result);
$this
->assertText('You got 5 of 10 possible points.');
$this
->assertText('Your score: 50%');
// Strange behavior - extra spaces in HTML.
//$this->assertText('Score 2 of 5');
//$this->assertText('Score 3 of 5');
$this
->assertText('Feedback for answer 1.');
$this
->assertText('Feedback for answer 2.');
}
/**
* Test adding and taking a truefalse question.
*/
public function testCreateQuizQuestion($settings = array()) {
if (!$settings) {
$settings = array(
'correct_answer_evaluation' => ShortAnswerQuestion::ANSWER_INSENSITIVE_MATCH,
'correct_answer' => 'SA 1 correct answer.',
);
}
// Login as our privileged user.
$this
->drupalLogin($this->admin);
$question_node = $this
->drupalCreateNode(array(
'type' => $this->question_node_type,
'title' => 'SA 1 title',
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'SA 1 body text.',
),
),
),
) + $settings);
return $question_node;
}
/**
* Test that the question response can be edited.
*/
public function testEditQuestionResponse() {
$this
->drupalLogin($this->admin);
// Create & link a question.
$question_node = $this
->testCreateQuizQuestion();
$quiz_node = $this
->linkQuestionToQuiz($question_node);
$question_node2 = $this
->testCreateQuizQuestion();
$this
->linkQuestionToQuiz($question_node2, $quiz_node);
// Login as non-admin.
$this
->drupalLogin($this->user);
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalGet("node/{$quiz_node->nid}/take/1");
$this
->drupalPost(NULL, array(
"question[{$question_node->nid}][answer]" => 'um some rule, I forget',
), t('Next'));
$this
->drupalGet("node/{$quiz_node->nid}/take/1");
$this
->drupalPost(NULL, array(
"question[{$question_node->nid}][answer]" => 'um some rule, I forget',
), t('Next'));
}
}
Classes
Name![]() |
Description |
---|---|
ShortAnswerTestCase | Test class for short answer. |