Merge branch 'MDL-49187-27' of https://github.com/spvickers/moodle into MOODLE_27_STABLE
[moodle.git] / mod / quiz / tests / lib_test.php
blob783f694d20f2d2aeae423a5967eb75046efd32f3
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Unit tests for (some of) mod/quiz/locallib.php.
20 * @package mod_quiz
21 * @category phpunit
22 * @copyright 2008 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->dirroot . '/mod/quiz/editlib.php');
33 /**
34 * @copyright 2008 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
37 class mod_quiz_lib_testcase extends advanced_testcase {
38 public function test_quiz_has_grades() {
39 $quiz = new stdClass();
40 $quiz->grade = '100.0000';
41 $quiz->sumgrades = '100.0000';
42 $this->assertTrue(quiz_has_grades($quiz));
43 $quiz->sumgrades = '0.0000';
44 $this->assertFalse(quiz_has_grades($quiz));
45 $quiz->grade = '0.0000';
46 $this->assertFalse(quiz_has_grades($quiz));
47 $quiz->sumgrades = '100.0000';
48 $this->assertFalse(quiz_has_grades($quiz));
51 public function test_quiz_format_grade() {
52 $quiz = new stdClass();
53 $quiz->decimalpoints = 2;
54 $this->assertEquals(quiz_format_grade($quiz, 0.12345678), format_float(0.12, 2));
55 $this->assertEquals(quiz_format_grade($quiz, 0), format_float(0, 2));
56 $this->assertEquals(quiz_format_grade($quiz, 1.000000000000), format_float(1, 2));
57 $quiz->decimalpoints = 0;
58 $this->assertEquals(quiz_format_grade($quiz, 0.12345678), '0');
61 public function test_quiz_format_question_grade() {
62 $quiz = new stdClass();
63 $quiz->decimalpoints = 2;
64 $quiz->questiondecimalpoints = 2;
65 $this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.12, 2));
66 $this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 2));
67 $this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 2));
68 $quiz->decimalpoints = 3;
69 $quiz->questiondecimalpoints = -1;
70 $this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.123, 3));
71 $this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 3));
72 $this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 3));
73 $quiz->questiondecimalpoints = 4;
74 $this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.1235, 4));
75 $this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 4));
76 $this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 4));
79 /**
80 * Test deleting a quiz instance.
82 public function test_quiz_delete_instance() {
83 global $SITE, $DB;
84 $this->resetAfterTest(true);
85 $this->setAdminUser();
87 // Setup a quiz with 1 standard and 1 random question.
88 $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
89 $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0));
91 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
92 $cat = $questiongenerator->create_question_category();
93 $standardq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
95 quiz_add_quiz_question($standardq->id, $quiz);
96 quiz_add_random_questions($quiz, 0, $cat->id, 1, false);
98 // Get the random question.
99 $randomq = $DB->get_record('question', array('qtype' => 'random'));
101 quiz_delete_instance($quiz->id);
103 // Check that the random question was deleted.
104 $count = $DB->count_records('question', array('id' => $randomq->id));
105 $this->assertEquals(0, $count);
106 // Check that the standard question was not deleted.
107 $count = $DB->count_records('question', array('id' => $standardq->id));
108 $this->assertEquals(1, $count);
110 // Check that all the slots were removed.
111 $count = $DB->count_records('quiz_slots', array('quizid' => $quiz->id));
112 $this->assertEquals(0, $count);
114 // Check that the quiz was removed.
115 $count = $DB->count_records('quiz', array('id' => $quiz->id));
116 $this->assertEquals(0, $count);