Moodle release 2.9beta
[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 }
109
110 $expecteddatabases = array();
111 $expecteddatabases[] = $expected2;
112 $expecteddatabases[] = $expected1;
113
114 // Call the external function passing course ids.
115 $result = mod_data_external::get_databases_by_courses(array($course2->id, $course1->id));
116 external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
117 $this->assertEquals($expecteddatabases, $result['databases']);
118
119 // Call the external function without passing course id.
120 $result = mod_data_external::get_databases_by_courses();
121 external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
122 $this->assertEquals($expecteddatabases, $result['databases']);
123
124 // Unenrol user from second course and alter expected databases.
125 $enrol->unenrol_user($instance2, $student->id);
126 array_shift($expecteddatabases);
127
128 // Call the external function without passing course id.
129 $result = mod_data_external::get_databases_by_courses();
130 external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
131 $this->assertEquals($expecteddatabases, $result['databases']);
132
133 // Call for the second course we unenrolled the user from, expected warning.
134 $result = mod_data_external::get_databases_by_courses(array($course2->id));
135 $this->assertCount(1, $result['warnings']);
136 $this->assertEquals('2', $result['warnings'][0]['warningcode']);
137 $this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
138
139 // Now, try as a teacher for getting all the additional fields.
140 self::setUser($teacher);
141
142 $additionalfields = array('maxentries', 'rssarticles', 'singletemplate', 'listtemplate',
143 'listtemplateheader', 'listtemplatefooter', 'addtemplate', 'rsstemplate', 'rsstitletemplate',
144 'csstemplate', 'jstemplate', 'asearchtemplate', 'approval', 'scale', 'assessed', 'assesstimestart',
145 'assesstimefinish', 'defaultsort', 'defaultsortdir', 'editany', 'notification');
146
147 foreach ($additionalfields as $field) {
148 $expecteddatabases[0][$field] = $database1->{$field};
149 }
150 $result = mod_data_external::get_databases_by_courses();
151 external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
152 $this->assertEquals($expecteddatabases, $result['databases']);
153 }
154}