MDL-51569 mod_choice: Check choice availability prior to do any action
[moodle.git] / mod / choice / tests / lib_test.php
CommitLineData
dffb87ed
JL
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/>.
16
17/**
18 * Choice module library functions tests
19 *
20 * @package mod_choice
21 * @category test
22 * @copyright 2015 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since Moodle 3.0
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32require_once($CFG->dirroot . '/mod/choice/lib.php');
33
34/**
35 * Choice module library functions tests
36 *
37 * @package mod_choice
38 * @category test
39 * @copyright 2015 Juan Leyva <juan@moodle.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 * @since Moodle 3.0
42 */
43class mod_choice_lib_testcase extends externallib_advanced_testcase {
44
45 /**
46 * Test choice_view
47 * @return void
48 */
49 public function test_choice_view() {
50 global $CFG;
51
52 $this->resetAfterTest();
53
54 $this->setAdminUser();
55 // Setup test data.
56 $course = $this->getDataGenerator()->create_course();
57 $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id));
58 $context = context_module::instance($choice->cmid);
59 $cm = get_coursemodule_from_instance('choice', $choice->id);
60
61 // Trigger and capture the event.
62 $sink = $this->redirectEvents();
63
64 choice_view($choice, $course, $cm, $context);
65
66 $events = $sink->get_events();
67 $this->assertCount(1, $events);
68 $event = array_shift($events);
69
70 // Checking that the event contains the expected values.
71 $this->assertInstanceOf('\mod_choice\event\course_module_viewed', $event);
72 $this->assertEquals($context, $event->get_context());
73 $url = new \moodle_url('/mod/choice/view.php', array('id' => $cm->id));
74 $this->assertEquals($url, $event->get_url());
75 $this->assertEventContextNotUsed($event);
76 $this->assertNotEmpty($event->get_name());
77 }
78
79 /**
80 * Test choice_can_view_results
81 * @return void
82 */
83 public function test_choice_can_view_results() {
84 global $DB, $USER;
85
86 $this->resetAfterTest();
87
88 $this->setAdminUser();
89 // Setup test data.
90 $course = $this->getDataGenerator()->create_course();
91 $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id));
92 $context = context_module::instance($choice->cmid);
93 $cm = get_coursemodule_from_instance('choice', $choice->id);
94
95 // Default values are false, user cannot view results.
96 $canview = choice_can_view_results($choice);
97 $this->assertFalse($canview);
98
99 // Show results forced.
100 $choice->showresults = CHOICE_SHOWRESULTS_ALWAYS;
101 $DB->update_record('choice', $choice);
102 $canview = choice_can_view_results($choice);
103 $this->assertTrue($canview);
104
105 // Show results after closing.
106 $choice->showresults = CHOICE_SHOWRESULTS_AFTER_CLOSE;
107 $DB->update_record('choice', $choice);
108 $canview = choice_can_view_results($choice);
109 $this->assertFalse($canview);
110
111 $choice->timeclose = time() - HOURSECS;
112 $DB->update_record('choice', $choice);
113 $canview = choice_can_view_results($choice);
114 $this->assertTrue($canview);
115
116 // Show results after answering.
117 $choice->timeclose = 0;
118 $choice->showresults = CHOICE_SHOWRESULTS_AFTER_ANSWER;
119 $DB->update_record('choice', $choice);
120 $canview = choice_can_view_results($choice);
121 $this->assertFalse($canview);
122
123 // Get the first option.
124 $choicewithoptions = choice_get_choice($choice->id);
125 $optionids = array_keys($choicewithoptions->option);
126
127 choice_user_submit_response($optionids[0], $choice, $USER->id, $course, $cm);
128
129 $canview = choice_can_view_results($choice);
130 $this->assertTrue($canview);
131
132 }
133
134 /**
135 * Test choice_get_my_response
136 * @return void
137 */
138 public function test_choice_get_my_response() {
139 global $USER;
140
141 $this->resetAfterTest();
142
143 $this->setAdminUser();
144 // Setup test data.
145 $course = $this->getDataGenerator()->create_course();
146 $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id));
147 $context = context_module::instance($choice->cmid);
148 $cm = get_coursemodule_from_instance('choice', $choice->id);
149
150 $choicewithoptions = choice_get_choice($choice->id);
151 $optionids = array_keys($choicewithoptions->option);
152
153 choice_user_submit_response($optionids[0], $choice, $USER->id, $course, $cm);
154 $responses = choice_get_my_response($choice, $course, $cm, $context);
155 $this->assertCount(1, $responses);
156 $response = array_shift($responses);
157 $this->assertEquals($optionids[0], $response->optionid);
158
159 // Multiple responses.
160 $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id, 'allowmultiple' => 1));
161 $context = context_module::instance($choice->cmid);
162 $cm = get_coursemodule_from_instance('choice', $choice->id);
163
164 $choicewithoptions = choice_get_choice($choice->id);
165 $optionids = array_keys($choicewithoptions->option);
166
167 choice_user_submit_response($optionids, $choice, $USER->id, $course, $cm);
168 $responses = choice_get_my_response($choice, $course, $cm, $context);
169 $this->assertCount(count($optionids), $responses);
170 foreach ($responses as $resp) {
171 $this->assertContains($resp->optionid, $optionids);
172 }
173 }
174
3dd0ac55
JL
175 /**
176 * Test choice_get_availability_status
177 * @return void
178 */
179 public function choice_get_availability_status() {
180 $this->resetAfterTest();
181
182 $this->setAdminUser();
183 // Setup test data.
184 $course = $this->getDataGenerator()->create_course();
185 $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id));
186
187 // No time restrictions.
188 list($status, $warnings) = choice_get_availability_status($choice, false);
189 $this->assertEquals(true, $status);
190 $this->assertCount(0, $warnings);
191
192 // With time restrictions, still open.
193 $choice->timeopen = time() - DAYSECS;
194 $choice->timeclose = time() + DAYSECS;
195 list($status, $warnings) = choice_get_availability_status($choice, false);
196 $this->assertEquals(true, $status);
197 $this->assertCount(0, $warnings);
198
199 // Choice not open yet.
200 $choice->timeopen = time() + DAYSECS;
201 $choice->timeclose = $choice->timeopen + DAYSECS;
202 list($status, $warnings) = choice_get_availability_status($choice, false);
203 $this->assertEquals(false, $status);
204 $this->assertCount(1, $warnings);
205 $this->assertEquals('notopenyet', $warnings[0]);
206
207 // Choice closed.
208 $choice->timeopen = time() - DAYSECS;
209 $choice->timeclose = time() - 1;
210 list($status, $warnings) = choice_get_availability_status($choice, false);
211 $this->assertEquals(false, $status);
212 $this->assertCount(1, $warnings);
213 $this->assertEquals('expired', $warnings[0]);
214
215 }
216
dffb87ed 217}