MDL-53149 mod_data: update backup and webservices for timemodified
[moodle.git] / mod / data / tests / externallib_test.php
CommitLineData
067b3fda
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 * Database module external functions tests
19 *
20 * @package mod_data
21 * @category external
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 2.9
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32
33/**
34 * Database module external functions tests
35 *
36 * @package mod_data
37 * @category external
38 * @copyright 2015 Juan Leyva <juan@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 * @since Moodle 2.9
41 */
42class mod_data_external_testcase extends externallib_advanced_testcase {
43
44 /**
45 * Test get databases by courses
46 */
47 public function test_mod_data_get_databases_by_courses() {
48 global $DB;
49
50 $this->resetAfterTest(true);
51
52 // Create users.
53 $student = self::getDataGenerator()->create_user();
54 $teacher = self::getDataGenerator()->create_user();
55
56 // Set to the student user.
57 self::setUser($student);
58
59 // Create courses to add the modules.
60 $course1 = self::getDataGenerator()->create_course();
61 $course2 = self::getDataGenerator()->create_course();
62
63 // First database.
64 $record = new stdClass();
65 $record->introformat = FORMAT_HTML;
66 $record->course = $course1->id;
67 $database1 = self::getDataGenerator()->create_module('data', $record);
68
69 // Second database.
70 $record = new stdClass();
71 $record->introformat = FORMAT_HTML;
72 $record->course = $course2->id;
73 $database2 = self::getDataGenerator()->create_module('data', $record);
74
75 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
76 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
77
78 // Users enrolments.
79 $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id, 'manual');
80 $this->getDataGenerator()->enrol_user($teacher->id, $course1->id, $teacherrole->id, 'manual');
81
82 // Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
83 $enrol = enrol_get_plugin('manual');
84 $enrolinstances = enrol_get_instances($course2->id, true);
85 foreach ($enrolinstances as $courseenrolinstance) {
86 if ($courseenrolinstance->enrol == "manual") {
87 $instance2 = $courseenrolinstance;
88 break;
89 }
90 }
91 $enrol->enrol_user($instance2, $student->id, $studentrole->id);
92
93 // Create what we expect to be returned when querying the two courses.
94 // First for the student user.
95 $expectedfields = array('id', 'coursemodule', 'course', 'name', 'comments', 'timeavailablefrom',
96 'timeavailableto', 'timeviewfrom', 'timeviewto', 'requiredentries', 'requiredentriestoview',
97 'intro', 'introformat');
98
99 // Add expected coursemodule.
100 $database1->coursemodule = $database1->cmid;
101 $database2->coursemodule = $database2->cmid;
102
103 $expected1 = array();
104 $expected2 = array();
105 foreach ($expectedfields as $field) {
106 $expected1[$field] = $database1->{$field};
107 $expected2[$field] = $database2->{$field};
108 }
8352aa50
JL
109 $expected1['comments'] = (bool) $expected1['comments'];
110 $expected2['comments'] = (bool) $expected2['comments'];
067b3fda
JL
111
112 $expecteddatabases = array();
113 $expecteddatabases[] = $expected2;
114 $expecteddatabases[] = $expected1;
115
116 // Call the external function passing course ids.
117 $result = mod_data_external::get_databases_by_courses(array($course2->id, $course1->id));
8352aa50 118 $result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
067b3fda
JL
119 $this->assertEquals($expecteddatabases, $result['databases']);
120
121 // Call the external function without passing course id.
122 $result = mod_data_external::get_databases_by_courses();
8352aa50 123 $result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
067b3fda
JL
124 $this->assertEquals($expecteddatabases, $result['databases']);
125
126 // Unenrol user from second course and alter expected databases.
127 $enrol->unenrol_user($instance2, $student->id);
128 array_shift($expecteddatabases);
129
130 // Call the external function without passing course id.
131 $result = mod_data_external::get_databases_by_courses();
8352aa50 132 $result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
067b3fda
JL
133 $this->assertEquals($expecteddatabases, $result['databases']);
134
135 // Call for the second course we unenrolled the user from, expected warning.
136 $result = mod_data_external::get_databases_by_courses(array($course2->id));
137 $this->assertCount(1, $result['warnings']);
8352aa50 138 $this->assertEquals('1', $result['warnings'][0]['warningcode']);
067b3fda
JL
139 $this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
140
141 // Now, try as a teacher for getting all the additional fields.
142 self::setUser($teacher);
143
4539892c 144 $additionalfields = array('maxentries', 'rssarticles', 'singletemplate', 'listtemplate', 'timemodified',
067b3fda
JL
145 'listtemplateheader', 'listtemplatefooter', 'addtemplate', 'rsstemplate', 'rsstitletemplate',
146 'csstemplate', 'jstemplate', 'asearchtemplate', 'approval', 'scale', 'assessed', 'assesstimestart',
ec9e1bec 147 'assesstimefinish', 'defaultsort', 'defaultsortdir', 'editany', 'notification', 'manageapproved');
067b3fda
JL
148
149 foreach ($additionalfields as $field) {
8352aa50
JL
150 if ($field == 'approval' or $field == 'editany') {
151 $expecteddatabases[0][$field] = (bool) $database1->{$field};
152 } else {
153 $expecteddatabases[0][$field] = $database1->{$field};
154 }
067b3fda
JL
155 }
156 $result = mod_data_external::get_databases_by_courses();
8352aa50
JL
157 $result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
158 $this->assertEquals($expecteddatabases, $result['databases']);
159
160 // Admin should get all the information.
161 self::setAdminUser();
162
163 $result = mod_data_external::get_databases_by_courses(array($course1->id));
164 $result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
067b3fda
JL
165 $this->assertEquals($expecteddatabases, $result['databases']);
166 }
167}