MDL-57890 ws: Return cmid in all get_by_courses WebServices
[moodle.git] / mod / label / tests / externallib_test.php
CommitLineData
7440c7b2
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 * External mod_label functions unit tests
19 *
20 * @package mod_label
21 * @category external
22 * @copyright 2017 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since Moodle 3.3
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32
33/**
34 * External mod_label functions unit tests
35 *
36 * @package mod_label
37 * @category external
38 * @copyright 2017 Juan Leyva <juan@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 * @since Moodle 3.3
41 */
42class mod_label_external_testcase extends externallib_advanced_testcase {
43
44 /**
45 * Test test_mod_label_get_labels_by_courses
46 */
47 public function test_mod_label_get_labels_by_courses() {
48 global $DB;
49
50 $this->resetAfterTest(true);
51
52 $course1 = self::getDataGenerator()->create_course();
53 $course2 = self::getDataGenerator()->create_course();
54
55 $student = self::getDataGenerator()->create_user();
56 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
57 $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id);
58
59 // First label.
60 $record = new stdClass();
61 $record->course = $course1->id;
62 $label1 = self::getDataGenerator()->create_module('label', $record);
63
64 // Second label.
65 $record = new stdClass();
66 $record->course = $course2->id;
67 $label2 = self::getDataGenerator()->create_module('label', $record);
68
69 // Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
70 $enrol = enrol_get_plugin('manual');
71 $enrolinstances = enrol_get_instances($course2->id, true);
72 foreach ($enrolinstances as $courseenrolinstance) {
73 if ($courseenrolinstance->enrol == "manual") {
74 $instance2 = $courseenrolinstance;
75 break;
76 }
77 }
78 $enrol->enrol_user($instance2, $student->id, $studentrole->id);
79
80 self::setUser($student);
81
82 $returndescription = mod_label_external::get_labels_by_courses_returns();
83
84 // Create what we expect to be returned when querying the two courses.
16840c71
DP
85 $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'timemodified',
86 'section', 'visible', 'groupmode', 'groupingid');
7440c7b2
JL
87
88 // Add expected coursemodule and data.
89 $label1->coursemodule = $label1->cmid;
90 $label1->introformat = 1;
91 $label1->section = 0;
92 $label1->visible = true;
93 $label1->groupmode = 0;
94 $label1->groupingid = 0;
95 $label1->introfiles = [];
96
97 $label2->coursemodule = $label2->cmid;
98 $label2->introformat = 1;
99 $label2->section = 0;
100 $label2->visible = true;
101 $label2->groupmode = 0;
102 $label2->groupingid = 0;
103 $label2->introfiles = [];
104
105 foreach ($expectedfields as $field) {
106 $expected1[$field] = $label1->{$field};
107 $expected2[$field] = $label2->{$field};
108 }
109
110 $expectedlabels = array($expected2, $expected1);
111
112 // Call the external function passing course ids.
113 $result = mod_label_external::get_labels_by_courses(array($course2->id, $course1->id));
114 $result = external_api::clean_returnvalue($returndescription, $result);
115
116 $this->assertEquals($expectedlabels, $result['labels']);
117 $this->assertCount(0, $result['warnings']);
118
119 // Call the external function without passing course id.
120 $result = mod_label_external::get_labels_by_courses();
121 $result = external_api::clean_returnvalue($returndescription, $result);
122 $this->assertEquals($expectedlabels, $result['labels']);
123 $this->assertCount(0, $result['warnings']);
124
125 // Add a file to the intro.
126 $filename = "file.txt";
127 $filerecordinline = array(
128 'contextid' => context_module::instance($label2->cmid)->id,
129 'component' => 'mod_label',
130 'filearea' => 'intro',
131 'itemid' => 0,
132 'filepath' => '/',
133 'filename' => $filename,
134 );
135 $fs = get_file_storage();
136 $timepost = time();
137 $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
138
139 $result = mod_label_external::get_labels_by_courses(array($course2->id, $course1->id));
140 $result = external_api::clean_returnvalue($returndescription, $result);
141
142 $this->assertCount(1, $result['labels'][0]['introfiles']);
143 $this->assertEquals($filename, $result['labels'][0]['introfiles'][0]['filename']);
144
145 // Unenrol user from second course.
146 $enrol->unenrol_user($instance2, $student->id);
147 array_shift($expectedlabels);
148
149 // Call the external function without passing course id.
150 $result = mod_label_external::get_labels_by_courses();
151 $result = external_api::clean_returnvalue($returndescription, $result);
152 $this->assertEquals($expectedlabels, $result['labels']);
153
154 // Call for the second course we unenrolled the user from, expected warning.
155 $result = mod_label_external::get_labels_by_courses(array($course2->id));
156 $this->assertCount(1, $result['warnings']);
157 $this->assertEquals('1', $result['warnings'][0]['warningcode']);
158 $this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
159 }
160}