2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Unit tests for (some of) mod/quiz/locallib.php.
22 * @copyright 2008 Tim Hunt
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
34 * Unit tests for (some of) mod/quiz/locallib.php.
36 * @copyright 2008 Tim Hunt
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class mod_quiz_locallib_testcase extends advanced_testcase {
41 public function test_quiz_rescale_grade() {
42 $quiz = new stdClass();
43 $quiz->decimalpoints = 2;
44 $quiz->questiondecimalpoints = 3;
46 $quiz->sumgrades = 10;
47 $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, false), 0.12345678);
48 $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, true), format_float(0.12, 2));
49 $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, 'question'),
50 format_float(0.123, 3));
52 $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, false), 0.24691356);
53 $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, true), format_float(0.25, 2));
54 $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, 'question'),
55 format_float(0.247, 3));
58 public function quiz_attempt_state_data_provider() {
60 [quiz_attempt::IN_PROGRESS, null, null, mod_quiz_display_options::DURING],
61 [quiz_attempt::FINISHED, -90, null, mod_quiz_display_options::IMMEDIATELY_AFTER],
62 [quiz_attempt::FINISHED, -7200, null, mod_quiz_display_options::LATER_WHILE_OPEN],
63 [quiz_attempt::FINISHED, -7200, 3600, mod_quiz_display_options::LATER_WHILE_OPEN],
64 [quiz_attempt::FINISHED, -30, 30, mod_quiz_display_options::IMMEDIATELY_AFTER],
65 [quiz_attempt::FINISHED, -90, -30, mod_quiz_display_options::AFTER_CLOSE],
66 [quiz_attempt::FINISHED, -7200, -3600, mod_quiz_display_options::AFTER_CLOSE],
67 [quiz_attempt::FINISHED, -90, -3600, mod_quiz_display_options::AFTER_CLOSE],
68 [quiz_attempt::ABANDONED, -10000000, null, mod_quiz_display_options::LATER_WHILE_OPEN],
69 [quiz_attempt::ABANDONED, -7200, 3600, mod_quiz_display_options::LATER_WHILE_OPEN],
70 [quiz_attempt::ABANDONED, -7200, -3600, mod_quiz_display_options::AFTER_CLOSE],
75 * @dataProvider quiz_attempt_state_data_provider
77 * @param unknown $attemptstate as in the quiz_attempts.state DB column.
78 * @param unknown $relativetimefinish time relative to now when the attempt finished, or null for 0.
79 * @param unknown $relativetimeclose time relative to now when the quiz closes, or null for 0.
80 * @param unknown $expectedstate expected result. One of the mod_quiz_display_options constants/
82 public function test_quiz_attempt_state($attemptstate,
83 $relativetimefinish, $relativetimeclose, $expectedstate) {
85 $attempt = new stdClass();
86 $attempt->state = $attemptstate;
87 if ($relativetimefinish === null) {
88 $attempt->timefinish = 0;
90 $attempt->timefinish = time() + $relativetimefinish;
93 $quiz = new stdClass();
94 if ($relativetimeclose === null) {
97 $quiz->timeclose = time() + $relativetimeclose;
100 $this->assertEquals($expectedstate, quiz_attempt_state($quiz, $attempt));
103 public function test_quiz_question_tostring() {
104 $question = new stdClass();
105 $question->qtype = 'multichoice';
106 $question->name = 'The question name';
107 $question->questiontext = '<p>What sort of <b>inequality</b> is x < y<img alt="?" src="..."></p>';
108 $question->questiontextformat = FORMAT_HTML;
110 $summary = quiz_question_tostring($question);
111 $this->assertEquals('<span class="questionname">The question name</span> ' .
112 '<span class="questiontext">What sort of INEQUALITY is x < y[?]' . "\n" . '</span>', $summary);
119 public function test_quiz_view() {
122 $CFG->enablecompletion = 1;
123 $this->resetAfterTest();
125 $this->setAdminUser();
127 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
128 $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
129 array('completion' => 2, 'completionview' => 1));
130 $context = context_module::instance($quiz->cmid);
131 $cm = get_coursemodule_from_instance('quiz', $quiz->id);
133 // Trigger and capture the event.
134 $sink = $this->redirectEvents();
136 quiz_view($quiz, $course, $cm, $context);
138 $events = $sink->get_events();
139 // 2 additional events thanks to completion.
140 $this->assertCount(3, $events);
141 $event = array_shift($events);
143 // Checking that the event contains the expected values.
144 $this->assertInstanceOf('\mod_quiz\event\course_module_viewed', $event);
145 $this->assertEquals($context, $event->get_context());
146 $moodleurl = new \moodle_url('/mod/quiz/view.php', array('id' => $cm->id));
147 $this->assertEquals($moodleurl, $event->get_url());
148 $this->assertEventContextNotUsed($event);
149 $this->assertNotEmpty($event->get_name());
150 // Check completion status.
151 $completion = new completion_info($course);
152 $completiondata = $completion->get_data($cm);
153 $this->assertEquals(1, $completiondata->completionstate);