1 |
efrain |
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 |
namespace core_communication;
|
|
|
18 |
|
|
|
19 |
use communication_matrix\matrix_test_helper_trait;
|
|
|
20 |
use core_communication\processor as communication_processor;
|
|
|
21 |
|
|
|
22 |
defined('MOODLE_INTERNAL') || die();
|
|
|
23 |
|
|
|
24 |
require_once(__DIR__ . '/../provider/matrix/tests/matrix_test_helper_trait.php');
|
|
|
25 |
require_once(__DIR__ . '/communication_test_helper_trait.php');
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Test communication helper methods.
|
|
|
29 |
*
|
|
|
30 |
* @package core_communication
|
|
|
31 |
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
|
|
|
32 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
33 |
* @covers \core_communication\helper
|
|
|
34 |
*/
|
|
|
35 |
class helper_test extends \advanced_testcase {
|
|
|
36 |
use communication_test_helper_trait;
|
|
|
37 |
use matrix_test_helper_trait;
|
|
|
38 |
|
|
|
39 |
public function setUp(): void {
|
|
|
40 |
parent::setUp();
|
|
|
41 |
$this->resetAfterTest();
|
|
|
42 |
$this->setup_communication_configs();
|
|
|
43 |
$this->initialise_mock_server();
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Test load_by_group.
|
|
|
48 |
*/
|
|
|
49 |
public function test_load_by_group(): void {
|
|
|
50 |
|
|
|
51 |
// As communication is created by default.
|
|
|
52 |
$course = $this->get_course(
|
|
|
53 |
extrafields: ['groupmode' => SEPARATEGROUPS],
|
|
|
54 |
);
|
|
|
55 |
$group = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
|
|
|
56 |
$context = \context_course::instance(courseid: $course->id);
|
|
|
57 |
|
|
|
58 |
$groupcommunication = helper::load_by_group(
|
|
|
59 |
groupid: $group->id,
|
|
|
60 |
context: $context,
|
|
|
61 |
);
|
|
|
62 |
$this->assertInstanceOf(
|
|
|
63 |
expected: communication_processor::class,
|
|
|
64 |
actual: $groupcommunication->get_processor(),
|
|
|
65 |
);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Test load_by_course.
|
|
|
70 |
*/
|
|
|
71 |
public function test_load_by_course(): void {
|
|
|
72 |
// As communication is created by default.
|
|
|
73 |
$course = $this->get_course();
|
|
|
74 |
$coursecontext = \context_course::instance(courseid: $course->id);
|
|
|
75 |
$coursecommunication = helper::load_by_course(
|
|
|
76 |
courseid: $course->id,
|
|
|
77 |
context: $coursecontext,
|
|
|
78 |
);
|
|
|
79 |
$this->assertInstanceOf(
|
|
|
80 |
expected: communication_processor::class,
|
|
|
81 |
actual: $coursecommunication->get_processor(),
|
|
|
82 |
);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Test get_access_to_all_group_cap_users.
|
|
|
87 |
*/
|
|
|
88 |
public function test_get_users_has_access_to_all_groups(): void {
|
|
|
89 |
global $DB;
|
|
|
90 |
// Set up the data with course, group, user etc.
|
|
|
91 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
92 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
93 |
$course = $this->get_course();
|
|
|
94 |
$coursecontext = \context_course::instance(courseid: $course->id);
|
|
|
95 |
|
|
|
96 |
// Enrol user1 as teacher.
|
|
|
97 |
$teacherrole = $DB->get_record(
|
|
|
98 |
table: 'role',
|
|
|
99 |
conditions: ['shortname' => 'manager'],
|
|
|
100 |
);
|
|
|
101 |
$this->getDataGenerator()->enrol_user(
|
|
|
102 |
userid: $user1->id,
|
|
|
103 |
courseid: $course->id,
|
|
|
104 |
);
|
|
|
105 |
role_assign(
|
|
|
106 |
roleid: $teacherrole->id,
|
|
|
107 |
userid: $user1->id,
|
|
|
108 |
contextid: $coursecontext->id,
|
|
|
109 |
);
|
|
|
110 |
|
|
|
111 |
// Enrol user2 as student.
|
|
|
112 |
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
|
|
|
113 |
$this->getDataGenerator()->enrol_user(
|
|
|
114 |
userid: $user2->id,
|
|
|
115 |
courseid: $course->id,
|
|
|
116 |
);
|
|
|
117 |
role_assign(
|
|
|
118 |
roleid: $studentrole->id,
|
|
|
119 |
userid: $user2->id,
|
|
|
120 |
contextid: $coursecontext->id,
|
|
|
121 |
);
|
|
|
122 |
|
|
|
123 |
$allgroupaccessusers = helper::get_users_has_access_to_all_groups(
|
|
|
124 |
userids: [$user1->id, $user2->id],
|
|
|
125 |
courseid: $course->id,
|
|
|
126 |
);
|
|
|
127 |
$this->assertContains(
|
|
|
128 |
needle: $user1->id,
|
|
|
129 |
haystack: $allgroupaccessusers,
|
|
|
130 |
);
|
|
|
131 |
$this->assertNotContains(
|
|
|
132 |
needle: $user2->id,
|
|
|
133 |
haystack: $allgroupaccessusers,
|
|
|
134 |
);
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* Test update_communication_room_membership.
|
|
|
139 |
*/
|
|
|
140 |
public function test_update_communication_room_membership(): void {
|
|
|
141 |
global $DB;
|
|
|
142 |
|
|
|
143 |
// Set up the data with course, group, user etc.
|
|
|
144 |
$user = $this->getDataGenerator()->create_user();
|
|
|
145 |
$course = $this->get_course();
|
|
|
146 |
$coursecontext = \context_course::instance(courseid: $course->id);
|
|
|
147 |
$teacherrole = $DB->get_record(
|
|
|
148 |
table: 'role',
|
|
|
149 |
conditions: ['shortname' => 'manager'],
|
|
|
150 |
);
|
|
|
151 |
$this->getDataGenerator()->enrol_user(
|
|
|
152 |
userid: $user->id,
|
|
|
153 |
courseid: $course->id,
|
|
|
154 |
);
|
|
|
155 |
role_assign(
|
|
|
156 |
roleid: $teacherrole->id,
|
|
|
157 |
userid:$user->id,
|
|
|
158 |
contextid: $coursecontext->id,
|
|
|
159 |
);
|
|
|
160 |
|
|
|
161 |
// Now remove members from room.
|
|
|
162 |
helper::update_course_communication_room_membership(
|
|
|
163 |
course: $course,
|
|
|
164 |
userids: [$user->id],
|
|
|
165 |
memberaction: 'remove_members_from_room',
|
|
|
166 |
);
|
|
|
167 |
|
|
|
168 |
// Now test that there is communication instances for the course and the user removed from that instance.
|
|
|
169 |
$coursecommunication = helper::load_by_course(
|
|
|
170 |
courseid: $course->id,
|
|
|
171 |
context: $coursecontext,
|
|
|
172 |
);
|
|
|
173 |
|
|
|
174 |
// Check the user is added for course communication instance.
|
|
|
175 |
$courseusers = $coursecommunication->get_processor()->get_all_delete_flagged_userids();
|
|
|
176 |
$courseusers = reset($courseusers);
|
|
|
177 |
$this->assertEquals(
|
|
|
178 |
expected: $user->id,
|
|
|
179 |
actual: $courseusers,
|
|
|
180 |
);
|
|
|
181 |
|
|
|
182 |
// Now add members to room.
|
|
|
183 |
helper::update_course_communication_room_membership(
|
|
|
184 |
course: $course,
|
|
|
185 |
userids: [$user->id],
|
|
|
186 |
memberaction: 'add_members_to_room',
|
|
|
187 |
);
|
|
|
188 |
|
|
|
189 |
$coursecommunication->reload();
|
|
|
190 |
// Check the user is added for course communication instance.
|
|
|
191 |
$courseusers = $coursecommunication->get_processor()->get_instance_userids();
|
|
|
192 |
$courseusers = reset($courseusers);
|
|
|
193 |
$this->assertEquals(
|
|
|
194 |
expected: $user->id,
|
|
|
195 |
actual: $courseusers,
|
|
|
196 |
);
|
|
|
197 |
|
|
|
198 |
// Now update membership.
|
|
|
199 |
helper::update_course_communication_room_membership(
|
|
|
200 |
course: $course,
|
|
|
201 |
userids: [$user->id],
|
|
|
202 |
memberaction: 'update_room_membership',
|
|
|
203 |
);
|
|
|
204 |
|
|
|
205 |
$coursecommunication->reload();
|
|
|
206 |
// Check the user is added for course communication instance.
|
|
|
207 |
$courseusers = $coursecommunication->get_processor()->get_instance_userids();
|
|
|
208 |
$courseusers = reset($courseusers);
|
|
|
209 |
$this->assertEquals(
|
|
|
210 |
expected: $user->id,
|
|
|
211 |
actual: $courseusers,
|
|
|
212 |
);
|
|
|
213 |
|
|
|
214 |
// Now try using invalid action.
|
|
|
215 |
$this->expectException('coding_exception');
|
|
|
216 |
$this->expectExceptionMessage('Invalid action provided.');
|
|
|
217 |
helper::update_course_communication_room_membership(
|
|
|
218 |
course: $course,
|
|
|
219 |
userids: [$user->id],
|
|
|
220 |
memberaction: 'a_funny_action',
|
|
|
221 |
);
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
/**
|
|
|
225 |
* Test format_group_room_name.
|
|
|
226 |
*/
|
|
|
227 |
public function test_format_group_room_name(): void {
|
|
|
228 |
$baseroomname = 'Course A';
|
|
|
229 |
$groupname = 'Group 1';
|
|
|
230 |
$formattedroomname = helper::format_group_room_name($baseroomname, $groupname);
|
|
|
231 |
// Check the room name is formatted as expected.
|
|
|
232 |
$this->assertEquals('Group 1 (Course A)', $formattedroomname);
|
|
|
233 |
}
|
|
|
234 |
}
|