MDL-50173 ratings: Use proper checks to ensure ratings are viewable.
[moodle.git] / mod / data / tests / lib_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 lib.php
19  *
20  * @package    mod_data
21  * @category   phpunit
22  * @copyright  2013 Adrian Greeve
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/mod/data/lib.php');
31 /**
32  * Unit tests for lib.php
33  *
34  * @package    mod_data
35  * @copyright  2013 Adrian Greeve
36  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37  */
38 class mod_data_lib_testcase extends advanced_testcase {
40     public function test_data_delete_record() {
41         global $DB;
43         $this->resetAfterTest();
45         // Create a record for deleting.
46         $this->setAdminUser();
47         $course = $this->getDataGenerator()->create_course();
48         $record = new stdClass();
49         $record->course = $course->id;
50         $record->name = "Mod data delete test";
51         $record->intro = "Some intro of some sort";
53         $module = $this->getDataGenerator()->create_module('data', $record);
55         $field = data_get_field_new('text', $module);
57         $fielddetail = new stdClass();
58         $fielddetail->d = $module->id;
59         $fielddetail->mode = 'add';
60         $fielddetail->type = 'text';
61         $fielddetail->sesskey = sesskey();
62         $fielddetail->name = 'Name';
63         $fielddetail->description = 'Some name';
65         $field->define_field($fielddetail);
66         $field->insert_field();
67         $recordid = data_add_record($module);
69         $datacontent = array();
70         $datacontent['fieldid'] = $field->field->id;
71         $datacontent['recordid'] = $recordid;
72         $datacontent['content'] = 'Asterix';
74         $contentid = $DB->insert_record('data_content', $datacontent);
75         $cm = get_coursemodule_from_instance('data', $module->id, $course->id);
77         // Check to make sure that we have a database record.
78         $data = $DB->get_records('data', array('id' => $module->id));
79         $this->assertEquals(1, count($data));
81         $datacontent = $DB->get_records('data_content', array('id' => $contentid));
82         $this->assertEquals(1, count($datacontent));
84         $datafields = $DB->get_records('data_fields', array('id' => $field->field->id));
85         $this->assertEquals(1, count($datafields));
87         $datarecords = $DB->get_records('data_records', array('id' => $recordid));
88         $this->assertEquals(1, count($datarecords));
90         // Test to see if a failed delete returns false.
91         $result = data_delete_record(8798, $module, $course->id, $cm->id);
92         $this->assertFalse($result);
94         // Delete the record.
95         $result = data_delete_record($recordid, $module, $course->id, $cm->id);
97         // Check that all of the record is gone.
98         $datacontent = $DB->get_records('data_content', array('id' => $contentid));
99         $this->assertEquals(0, count($datacontent));
101         $datarecords = $DB->get_records('data_records', array('id' => $recordid));
102         $this->assertEquals(0, count($datarecords));
104         // Make sure the function returns true on a successful deletion.
105         $this->assertTrue($result);
106     }
108     /**
109      * Test comment_created event.
110      */
111     public function test_data_comment_created_event() {
112         global $CFG, $DB;
113         require_once($CFG->dirroot . '/comment/lib.php');
115         $this->resetAfterTest();
117         // Create a record for deleting.
118         $this->setAdminUser();
119         $course = $this->getDataGenerator()->create_course();
120         $record = new stdClass();
121         $record->course = $course->id;
122         $record->name = "Mod data delete test";
123         $record->intro = "Some intro of some sort";
124         $record->comments = 1;
126         $module = $this->getDataGenerator()->create_module('data', $record);
127         $field = data_get_field_new('text', $module);
129         $fielddetail = new stdClass();
130         $fielddetail->name = 'Name';
131         $fielddetail->description = 'Some name';
133         $field->define_field($fielddetail);
134         $field->insert_field();
135         $recordid = data_add_record($module);
137         $datacontent = array();
138         $datacontent['fieldid'] = $field->field->id;
139         $datacontent['recordid'] = $recordid;
140         $datacontent['content'] = 'Asterix';
142         $contentid = $DB->insert_record('data_content', $datacontent);
143         $cm = get_coursemodule_from_instance('data', $module->id, $course->id);
145         $context = context_module::instance($module->cmid);
146         $cmt = new stdClass();
147         $cmt->context = $context;
148         $cmt->course = $course;
149         $cmt->cm = $cm;
150         $cmt->area = 'database_entry';
151         $cmt->itemid = $recordid;
152         $cmt->showcount = true;
153         $cmt->component = 'mod_data';
154         $comment = new comment($cmt);
156         // Triggering and capturing the event.
157         $sink = $this->redirectEvents();
158         $comment->add('New comment');
159         $events = $sink->get_events();
160         $this->assertCount(1, $events);
161         $event = reset($events);
163         // Checking that the event contains the expected values.
164         $this->assertInstanceOf('\mod_data\event\comment_created', $event);
165         $this->assertEquals($context, $event->get_context());
166         $url = new moodle_url('/mod/data/view.php', array('id' => $cm->id));
167         $this->assertEquals($url, $event->get_url());
168         $this->assertEventContextNotUsed($event);
169     }
171     /**
172      * Test comment_deleted event.
173      */
174     public function test_data_comment_deleted_event() {
175         global $CFG, $DB;
176         require_once($CFG->dirroot . '/comment/lib.php');
178         $this->resetAfterTest();
180         // Create a record for deleting.
181         $this->setAdminUser();
182         $course = $this->getDataGenerator()->create_course();
183         $record = new stdClass();
184         $record->course = $course->id;
185         $record->name = "Mod data delete test";
186         $record->intro = "Some intro of some sort";
187         $record->comments = 1;
189         $module = $this->getDataGenerator()->create_module('data', $record);
190         $field = data_get_field_new('text', $module);
192         $fielddetail = new stdClass();
193         $fielddetail->name = 'Name';
194         $fielddetail->description = 'Some name';
196         $field->define_field($fielddetail);
197         $field->insert_field();
198         $recordid = data_add_record($module);
200         $datacontent = array();
201         $datacontent['fieldid'] = $field->field->id;
202         $datacontent['recordid'] = $recordid;
203         $datacontent['content'] = 'Asterix';
205         $contentid = $DB->insert_record('data_content', $datacontent);
206         $cm = get_coursemodule_from_instance('data', $module->id, $course->id);
208         $context = context_module::instance($module->cmid);
209         $cmt = new stdClass();
210         $cmt->context = $context;
211         $cmt->course = $course;
212         $cmt->cm = $cm;
213         $cmt->area = 'database_entry';
214         $cmt->itemid = $recordid;
215         $cmt->showcount = true;
216         $cmt->component = 'mod_data';
217         $comment = new comment($cmt);
218         $newcomment = $comment->add('New comment 1');
220         // Triggering and capturing the event.
221         $sink = $this->redirectEvents();
222         $comment->delete($newcomment->id);
223         $events = $sink->get_events();
224         $this->assertCount(1, $events);
225         $event = reset($events);
227         // Checking that the event contains the expected values.
228         $this->assertInstanceOf('\mod_data\event\comment_deleted', $event);
229         $this->assertEquals($context, $event->get_context());
230         $url = new moodle_url('/mod/data/view.php', array('id' => $module->cmid));
231         $this->assertEquals($url, $event->get_url());
232         $this->assertEventContextNotUsed($event);
233     }
235     /**
236      * Tests for mod_data_rating_can_see_item_ratings().
237      *
238      * @throws coding_exception
239      * @throws rating_exception
240      */
241     public function test_mod_data_rating_can_see_item_ratings() {
242         global $DB;
244         $this->resetAfterTest();
246         // Setup test data.
247         $course = new stdClass();
248         $course->groupmode = SEPARATEGROUPS;
249         $course->groupmodeforce = true;
250         $course = $this->getDataGenerator()->create_course($course);
251         $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id));
252         $cm = get_coursemodule_from_instance('data', $data->id);
253         $context = context_module::instance($cm->id);
255         // Create users.
256         $user1 = $this->getDataGenerator()->create_user();
257         $user2 = $this->getDataGenerator()->create_user();
258         $user3 = $this->getDataGenerator()->create_user();
259         $user4 = $this->getDataGenerator()->create_user();
261         // Groups and stuff.
262         $role = $DB->get_record('role', array('shortname' => 'teacher'), '*', MUST_EXIST);
263         $this->getDataGenerator()->enrol_user($user1->id, $course->id, $role->id);
264         $this->getDataGenerator()->enrol_user($user2->id, $course->id, $role->id);
265         $this->getDataGenerator()->enrol_user($user3->id, $course->id, $role->id);
266         $this->getDataGenerator()->enrol_user($user4->id, $course->id, $role->id);
268         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
269         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
270         groups_add_member($group1, $user1);
271         groups_add_member($group1, $user2);
272         groups_add_member($group2, $user3);
273         groups_add_member($group2, $user4);
275         // Add data.
276         $field = data_get_field_new('text', $data);
278         $fielddetail = new stdClass();
279         $fielddetail->name = 'Name';
280         $fielddetail->description = 'Some name';
282         $field->define_field($fielddetail);
283         $field->insert_field();
284         $recordid = data_add_record($data, $group1->id);
286         $datacontent = array();
287         $datacontent['fieldid'] = $field->field->id;
288         $datacontent['recordid'] = $recordid;
289         $datacontent['content'] = 'Asterix';
290         $DB->insert_record('data_content', $datacontent);
292         // Now try to access it as various users.
293         unassign_capability('moodle/site:accessallgroups', $role->id);
294         $params = array('contextid' => 2,
295                         'component' => 'mod_data',
296                         'ratingarea' => 'entry',
297                         'itemid' => $recordid,
298                         'scaleid' => 2);
299         $this->setUser($user1);
300         $this->assertTrue(mod_data_rating_can_see_item_ratings($params));
301         $this->setUser($user2);
302         $this->assertTrue(mod_data_rating_can_see_item_ratings($params));
303         $this->setUser($user3);
304         $this->assertFalse(mod_data_rating_can_see_item_ratings($params));
305         $this->setUser($user4);
306         $this->assertFalse(mod_data_rating_can_see_item_ratings($params));
308         // Now try with accessallgroups cap and make sure everything is visible.
309         assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $role->id, $context->id);
310         $this->setUser($user1);
311         $this->assertTrue(mod_data_rating_can_see_item_ratings($params));
312         $this->setUser($user2);
313         $this->assertTrue(mod_data_rating_can_see_item_ratings($params));
314         $this->setUser($user3);
315         $this->assertTrue(mod_data_rating_can_see_item_ratings($params));
316         $this->setUser($user4);
317         $this->assertTrue(mod_data_rating_can_see_item_ratings($params));
319         // Change group mode and verify visibility.
320         $course->groupmode = VISIBLEGROUPS;
321         $DB->update_record('course', $course);
322         unassign_capability('moodle/site:accessallgroups', $role->id);
323         $this->setUser($user1);
324         $this->assertTrue(mod_data_rating_can_see_item_ratings($params));
325         $this->setUser($user2);
326         $this->assertTrue(mod_data_rating_can_see_item_ratings($params));
327         $this->setUser($user3);
328         $this->assertTrue(mod_data_rating_can_see_item_ratings($params));
329         $this->setUser($user4);
330         $this->assertTrue(mod_data_rating_can_see_item_ratings($params));
332     }