Merge branch 'MDL-55815' of git://github.com/timhunt/moodle
[moodle.git] / mod / quiz / tests / locallib_test.php
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.
13 //
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.
19  *
20  * @package    mod_quiz
21  * @category   test
22  * @copyright  2008 Tim Hunt
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
33 /**
34  * Unit tests for (some of) mod/quiz/locallib.php.
35  *
36  * @copyright  2008 Tim Hunt
37  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  */
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;
45         $quiz->grade = 10;
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));
51         $quiz->sumgrades = 5;
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));
56     }
58     public function quiz_attempt_state_data_provider() {
59         return [
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],
71         ];
72     }
74     /**
75      * @dataProvider quiz_attempt_state_data_provider
76      *
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/
81      */
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;
89         } else {
90             $attempt->timefinish = time() + $relativetimefinish;
91         }
93         $quiz = new stdClass();
94         if ($relativetimeclose === null) {
95             $quiz->timeclose = 0;
96         } else {
97             $quiz->timeclose = time() + $relativetimeclose;
98         }
100         $this->assertEquals($expectedstate, quiz_attempt_state($quiz, $attempt));
101     }
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 &lt; 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 &lt; y[?]' . "\n" . '</span>', $summary);
113     }
115     /**
116      * Test quiz_view
117      * @return void
118      */
119     public function test_quiz_view() {
120         global $CFG;
122         $CFG->enablecompletion = 1;
123         $this->resetAfterTest();
125         $this->setAdminUser();
126         // Setup test data.
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);
154     }