View source
<?php
class TrueFalseUnitTest extends DrupalWebTestCase {
var $question_node_type = 'true_false';
var $title = '';
var $body = '';
var $node_id = NULL;
var $rand_min = 1;
var $rand_max = 64;
public static function getInfo() {
return array(
'name' => t('TrueFalse unit test'),
'description' => t('Unit test for true or false question type.'),
'group' => t('Quiz'),
);
}
function setUp() {
parent::setUp('taxonomy', 'quiz', 'views', 'autoload', 'multichoice', 'quiz_directions', 'quiz_question', 'querypath', 'questions_import', 'short_answer', 'truefalse', 'long_answer', 'matching', 'questions_export');
$permission = array(
'administer site configuration',
'access administration pages',
'administer quiz',
'access quiz',
'administer blocks',
'import questions',
'create quiz',
'administer quiz configuration',
'use PHP for block visibility',
'administer blocks',
'create multichoice',
'edit any multichoice',
'administer taxonomy',
'allow multiple correct answers',
'allow any number of answers',
'export questions',
);
$user = $this
->drupalCreateUser($permission);
$this
->drupalLogin($user);
$quiz_settings = array();
$quiz_settings['title'] = $this
->randomName($this
->getRandSize());
$quiz_settings['comment'] = $this
->randomName($this
->getRandSize());
$quiz_settings['type'] = 'quiz';
}
public function getRandSize() {
return mt_rand($this->min, $this->max);
}
public function testTrueFalseQuizQuestionInfo() {
$info = truefalse_quiz_question_info();
$this
->assertEqual(count($info), 1, t('Check that info was returned.'));
$this
->assertTrue(isset($info['true_false']), t('Check that true/false question type exists.'));
}
public function createTrueFalseQuestion() {
$this->title = $this
->randomName($this
->getRandSize());
$this->body = $this
->randomName($this
->getRandSize());
$this->correct_answer = array_rand(array(
0,
1,
));
$settings = array(
'type' => $this->question_node_type,
'title' => $this->title,
'body' => $this->body,
'correct_answer' => $this->correct_answer,
'revisions' => TRUE,
);
return $this
->drupalCreateNode($settings);
}
public function unitTestCreateTrueFalseQuestionNode() {
$node = $this
->createTrueFalseQuestion();
if (!$node) {
throw new Exception('Expected to have a node to work with.');
}
$this->node_id = $node->nid;
$this
->assertEqual($node->title, $this->title, t('Title of stored node should equal the original title.'));
$this
->assertEqual($node->body, $this->body, t('Body of stored node should be equal to original body.'));
$this
->assertEqual($node->type, $this->question_node_type, t('Stored node type should be true_false'));
}
public function unitTestUpdateTrueFalseQuestionNode() {
$node = node_load($this->node_id);
$new_correct_answer = !$this->correct_answer;
$node->title = $this
->randomName(100);
$node->correct_answer = $new_correct_answer;
node_save($node);
$nodeCopy = node_load($node->nid, $node->vid);
$this
->assertEqual($nodeCopy->correct_answer, $new_correct_answer, t('Check that stored score is the same as newly assigned score.'));
}
public function unitTestDeleteTrueFalseQuestionNode() {
}
public function testTrueFalseNodeOperations() {
$this
->createTrueFalseQuestion();
$this
->unitTestDeleteTrueFalseQuestionNode();
$this
->unitTestCreateTrueFalseQuestionNode();
$this
->unitTestCreateTrueFalseQuestionRevision();
$this
->unitTestUpdateTrueFalseQuestionNode();
}
public function unitTestCreateTrueFalseQuestionRevision() {
$node = node_load($this->node_id);
$oldVid = $node->vid;
$node->revision = 1;
node_save($node);
$nodeCopy = node_load($node->nid, $node->vid);
$this
->assertNotEqual($nodeCopy->vid, $oldVid, t('Check that VID of new version is not the same as old VID.'));
$this
->assertEqual($oldMax, $nodeCopy->maximum_score, t('Check that new VID has an entry in node properties.'));
}
}