MDL-55742 mod_lti: Remove support for get_magic_quotes_gpc
[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 test_quiz_attempt_state_in_progress() {
59         $attempt = new stdClass();
60         $attempt->state = quiz_attempt::IN_PROGRESS;
61         $attempt->timefinish = 0;
63         $quiz = new stdClass();
64         $quiz->timeclose = 0;
66         $this->assertEquals(mod_quiz_display_options::DURING, quiz_attempt_state($quiz, $attempt));
67     }
69     public function test_quiz_attempt_state_recently_submitted() {
70         $attempt = new stdClass();
71         $attempt->state = quiz_attempt::FINISHED;
72         $attempt->timefinish = time() - 10;
74         $quiz = new stdClass();
75         $quiz->timeclose = 0;
77         $this->assertEquals(mod_quiz_display_options::IMMEDIATELY_AFTER, quiz_attempt_state($quiz, $attempt));
78     }
80     public function test_quiz_attempt_state_sumitted_quiz_never_closes() {
81         $attempt = new stdClass();
82         $attempt->state = quiz_attempt::FINISHED;
83         $attempt->timefinish = time() - 7200;
85         $quiz = new stdClass();
86         $quiz->timeclose = 0;
88         $this->assertEquals(mod_quiz_display_options::LATER_WHILE_OPEN, quiz_attempt_state($quiz, $attempt));
89     }
91     public function test_quiz_attempt_state_sumitted_quiz_closes_later() {
92         $attempt = new stdClass();
93         $attempt->state = quiz_attempt::FINISHED;
94         $attempt->timefinish = time() - 7200;
96         $quiz = new stdClass();
97         $quiz->timeclose = time() + 3600;
99         $this->assertEquals(mod_quiz_display_options::LATER_WHILE_OPEN, quiz_attempt_state($quiz, $attempt));
100     }
102     public function test_quiz_attempt_state_sumitted_quiz_closed() {
103         $attempt = new stdClass();
104         $attempt->state = quiz_attempt::FINISHED;
105         $attempt->timefinish = time() - 7200;
107         $quiz = new stdClass();
108         $quiz->timeclose = time() - 3600;
110         $this->assertEquals(mod_quiz_display_options::AFTER_CLOSE, quiz_attempt_state($quiz, $attempt));
111     }
113     public function test_quiz_attempt_state_never_sumitted_quiz_never_closes() {
114         $attempt = new stdClass();
115         $attempt->state = quiz_attempt::ABANDONED;
116         $attempt->timefinish = 1000; // A very long time ago!
118         $quiz = new stdClass();
119         $quiz->timeclose = 0;
121         $this->assertEquals(mod_quiz_display_options::LATER_WHILE_OPEN, quiz_attempt_state($quiz, $attempt));
122     }
124     public function test_quiz_attempt_state_never_sumitted_quiz_closes_later() {
125         $attempt = new stdClass();
126         $attempt->state = quiz_attempt::ABANDONED;
127         $attempt->timefinish = time() - 7200;
129         $quiz = new stdClass();
130         $quiz->timeclose = time() + 3600;
132         $this->assertEquals(mod_quiz_display_options::LATER_WHILE_OPEN, quiz_attempt_state($quiz, $attempt));
133     }
135     public function test_quiz_attempt_state_never_sumitted_quiz_closed() {
136         $attempt = new stdClass();
137         $attempt->state = quiz_attempt::ABANDONED;
138         $attempt->timefinish = time() - 7200;
140         $quiz = new stdClass();
141         $quiz->timeclose = time() - 3600;
143         $this->assertEquals(mod_quiz_display_options::AFTER_CLOSE, quiz_attempt_state($quiz, $attempt));
144     }
146     public function test_quiz_question_tostring() {
147         $question = new stdClass();
148         $question->qtype = 'multichoice';
149         $question->name = 'The question name';
150         $question->questiontext = '<p>What sort of <b>inequality</b> is x &lt; y<img alt="?" src="..."></p>';
151         $question->questiontextformat = FORMAT_HTML;
153         $summary = quiz_question_tostring($question);
154         $this->assertEquals('<span class="questionname">The question name</span> ' .
155                 '<span class="questiontext">What sort of INEQUALITY is x &lt; y[?]' . "\n" . '</span>', $summary);
156     }
158     /**
159      * Test quiz_view
160      * @return void
161      */
162     public function test_quiz_view() {
163         global $CFG;
165         $CFG->enablecompletion = 1;
166         $this->resetAfterTest();
168         $this->setAdminUser();
169         // Setup test data.
170         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
171         $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
172                                                             array('completion' => 2, 'completionview' => 1));
173         $context = context_module::instance($quiz->cmid);
174         $cm = get_coursemodule_from_instance('quiz', $quiz->id);
176         // Trigger and capture the event.
177         $sink = $this->redirectEvents();
179         quiz_view($quiz, $course, $cm, $context);
181         $events = $sink->get_events();
182         // 2 additional events thanks to completion.
183         $this->assertCount(3, $events);
184         $event = array_shift($events);
186         // Checking that the event contains the expected values.
187         $this->assertInstanceOf('\mod_quiz\event\course_module_viewed', $event);
188         $this->assertEquals($context, $event->get_context());
189         $moodleurl = new \moodle_url('/mod/quiz/view.php', array('id' => $cm->id));
190         $this->assertEquals($moodleurl, $event->get_url());
191         $this->assertEventContextNotUsed($event);
192         $this->assertNotEmpty($event->get_name());
193         // Check completion status.
194         $completion = new completion_info($course);
195         $completiondata = $completion->get_data($cm);
196         $this->assertEquals(1, $completiondata->completionstate);
197     }