. /** * Unit tests for (some of) mod/quiz/locallib.php. * * @package mod_quiz * @category test * @copyright 2008 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU Public License */ defined('MOODLE_INTERNAL') || die(); /** * @copyright 2008 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU Public License */ class mod_quiz_lib_testcase extends advanced_testcase { public function test_quiz_has_grades() { $quiz = new stdClass(); $quiz->grade = '100.0000'; $quiz->sumgrades = '100.0000'; $this->assertTrue(quiz_has_grades($quiz)); $quiz->sumgrades = '0.0000'; $this->assertFalse(quiz_has_grades($quiz)); $quiz->grade = '0.0000'; $this->assertFalse(quiz_has_grades($quiz)); $quiz->sumgrades = '100.0000'; $this->assertFalse(quiz_has_grades($quiz)); } public function test_quiz_format_grade() { $quiz = new stdClass(); $quiz->decimalpoints = 2; $this->assertEquals(quiz_format_grade($quiz, 0.12345678), format_float(0.12, 2)); $this->assertEquals(quiz_format_grade($quiz, 0), format_float(0, 2)); $this->assertEquals(quiz_format_grade($quiz, 1.000000000000), format_float(1, 2)); $quiz->decimalpoints = 0; $this->assertEquals(quiz_format_grade($quiz, 0.12345678), '0'); } public function test_quiz_get_grade_format() { $quiz = new stdClass(); $quiz->decimalpoints = 2; $this->assertEquals(quiz_get_grade_format($quiz), 2); $this->assertEquals($quiz->questiondecimalpoints, -1); $quiz->questiondecimalpoints = 2; $this->assertEquals(quiz_get_grade_format($quiz), 2); $quiz->decimalpoints = 3; $quiz->questiondecimalpoints = -1; $this->assertEquals(quiz_get_grade_format($quiz), 3); $quiz->questiondecimalpoints = 4; $this->assertEquals(quiz_get_grade_format($quiz), 4); } public function test_quiz_format_question_grade() { $quiz = new stdClass(); $quiz->decimalpoints = 2; $quiz->questiondecimalpoints = 2; $this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.12, 2)); $this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 2)); $this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 2)); $quiz->decimalpoints = 3; $quiz->questiondecimalpoints = -1; $this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.123, 3)); $this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 3)); $this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 3)); $quiz->questiondecimalpoints = 4; $this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.1235, 4)); $this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 4)); $this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 4)); } /** * Test deleting a quiz instance. */ public function test_quiz_delete_instance() { global $SITE, $DB; $this->resetAfterTest(true); $this->setAdminUser(); // Setup a quiz with 1 standard and 1 random question. $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0)); $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $questiongenerator->create_question_category(); $standardq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id)); quiz_add_quiz_question($standardq->id, $quiz); quiz_add_random_questions($quiz, 0, $cat->id, 1, false); // Get the random question. $randomq = $DB->get_record('question', array('qtype' => 'random')); quiz_delete_instance($quiz->id); // Check that the random question was deleted. $count = $DB->count_records('question', array('id' => $randomq->id)); $this->assertEquals(0, $count); // Check that the standard question was not deleted. $count = $DB->count_records('question', array('id' => $standardq->id)); $this->assertEquals(1, $count); // Check that all the slots were removed. $count = $DB->count_records('quiz_slots', array('quizid' => $quiz->id)); $this->assertEquals(0, $count); // Check that the quiz was removed. $count = $DB->count_records('quiz', array('id' => $quiz->id)); $this->assertEquals(0, $count); } }