View source
<?php
class QuizTakingTestCase extends QuizTestCase {
public static function getInfo() {
return array(
'name' => t('Quiz taking'),
'description' => t('Unit test for Quiz take behaviors.'),
'group' => t('Quiz'),
);
}
public function setUp($modules = array(), $admin_permissions = array(), $user_permissions = array()) {
$modules[] = 'truefalse';
parent::setUp($modules);
}
public function testQuestionRepeatUntilCorrect() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->drupalCreateQuiz(array(
'repeat_until_correct' => 1,
'review_options' => array(
'question' => array(
'answer_feedback' => 'answer_feedback',
),
),
));
$question_node = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
));
$this
->linkQuestionToQuiz($question_node, $quiz_node);
$this
->drupalLogin($this->user);
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost(NULL, array(
"question[{$question_node->nid}][answer]" => 0,
), t('Finish'));
$this
->assertText('The answer was incorrect. Please try again.');
$this
->assertUrl("node/{$quiz_node->nid}/take/1");
$this
->drupalPost(NULL, array(
"question[{$question_node->nid}][answer]" => 1,
), t('Finish'));
$this
->assertNoText('The answer was incorrect. Please try again.');
}
public function testQuizResuming() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->drupalCreateQuiz(array(
'allow_resume' => 1,
));
$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);
$this
->drupalLogin($this->user);
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalGet("node/{$quiz_node->nid}/take/2");
$this
->assertResponse(403);
$this
->drupalGet("node/{$quiz_node->nid}/take/1");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
), t('Next'));
$this
->drupalLogin($this->user);
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->assertText('Resuming');
$this
->drupalGet("node/{$quiz_node->nid}/take/2");
$this
->assertResponse(200);
}
public function testQuizNoResuming() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->drupalCreateQuiz(array(
'allow_resume' => 0,
));
$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);
$this
->drupalLogin($this->user);
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalGet("node/{$quiz_node->nid}/take/2");
$this
->assertResponse(403);
$this
->drupalGet("node/{$quiz_node->nid}/take/1");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
), t('Next'));
$this
->drupalLogin($this->user);
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->assertNoText('Resuming');
$this
->drupalGet("node/{$quiz_node->nid}/take/2");
$this
->assertResponse(403);
}
public function testQuizAvailability() {
return;
$future = REQUEST_TIME + 86400;
$past = REQUEST_TIME - 86400;
$quiz_node = $this
->drupalCreateQuiz(array(
'quiz_always' => 0,
'quiz_open' => $past,
'quiz_close' => $future,
));
$this
->drupalGet("node/{$quiz_node->nid}");
$this
->assertNoText(t('This @quiz is closed.', array(
'@quiz' => QUIZ_NAME,
)));
$quiz_node = $this
->drupalCreateQuiz(array(
'quiz_always' => 0,
'quiz_open' => $future,
'quiz_close' => $future + 1,
));
$this
->drupalGet("node/{$quiz_node->nid}");
$this
->assertText(t('This @quiz is closed.', array(
'@quiz' => QUIZ_NAME,
)));
$quiz_node = $this
->drupalCreateQuiz(array(
'quiz_always' => 0,
'quiz_open' => $past,
'quiz_close' => $past + 1,
));
$this
->drupalGet("node/{$quiz_node->nid}");
$this
->assertText(t('This @quiz is closed.', array(
'@quiz' => QUIZ_NAME,
)));
$quiz_node = $this
->drupalCreateQuiz(array(
'quiz_always' => 1,
'quiz_open' => $future,
'quiz_close' => $past,
));
$this
->drupalGet("node/{$quiz_node->nid}");
$this
->assertNoText(t('This @quiz is closed.', array(
'@quiz' => QUIZ_NAME,
)));
}
function testViewQuestionsOutsideQuiz() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->drupalCreateQuiz();
$question_node1 = $this
->drupalCreateNode(array(
'type' => 'truefalse',
'correct_answer' => 1,
));
$this
->linkQuestionToQuiz($question_node1, $quiz_node);
node_access_rebuild(FALSE);
$this
->drupalLogin($this->user);
$this
->drupalGet("node/{$question_node1->nid}");
$this
->assertResponse(403);
$user_with_privs = $this
->drupalCreateUser(array(
'view quiz question outside of a quiz',
'access quiz',
));
$this
->drupalLogin($user_with_privs);
$this
->drupalGet("node/{$question_node1->nid}");
$this
->assertResponse(200);
}
function testChangeAnswer() {
$quiz_node = $this
->drupalCreateQuiz(array(
'review_options' => array(
'question' => array(
'score' => 'score',
),
),
));
$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);
$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_node1->nid}][answer]" => 0,
), t('Next'));
$this
->assertText('Score: 0 of 1');
$this
->drupalGet("node/{$quiz_node->nid}/take/1");
$this
->drupalPost(NULL, array(
"question[{$question_node1->nid}][answer]" => 1,
), t('Next'));
$this
->assertText('Score: 1 of 1');
variable_set('quiz_auto_revisioning', 0);
$quiz_node->allow_change = 0;
node_save($quiz_node);
$this
->drupalGet("node/{$quiz_node->nid}/take/1");
$disabled_field = $this
->xpath('//input[@id=:id and @disabled="disabled"]', array(
':id' => 'edit-question-2-answer-1',
));
$this
->assertTrue($disabled_field, t('The answer cannot be changed.'));
$this
->drupalPost(NULL, array(), t('Next'));
$this
->assertText('Score: 1 of 1');
}
}