Merge branch 'MDL-61460-master-fix' of https://github.com/andrewnicols/moodle
[moodle.git] / message / tests / helper_test.php
CommitLineData
9e6734a7
JD
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 * Contains a test class for the message helper.
19 *
20 * @package core_message
21 * @category test
22 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
31
32/**
33 * Tests for the message helper class.
34 *
35 * @package core_message
36 * @category test
37 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class core_message_helper_testcase extends advanced_testcase {
41
f6711bb3 42 public function setUp(): void {
9e6734a7
JD
43 $this->resetAfterTest(true);
44 }
45
46 public function test_get_member_info_ordering() {
47 // Create a conversation with several users.
48 $user1 = self::getDataGenerator()->create_user();
49 $user2 = self::getDataGenerator()->create_user();
50 $user3 = self::getDataGenerator()->create_user();
51 $user4 = self::getDataGenerator()->create_user();
52
53 \core_message\api::create_conversation(
54 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
55 [
56 $user1->id,
57 $user2->id,
58 $user3->id,
59 $user4->id,
60 ],
61 'Group conversation'
62 );
63
64 // Verify that the member information comes back in the same order that we specified in the input array.
65 $memberinfo = \core_message\helper::get_member_info($user1->id, [$user3->id, $user4->id, $user2->id]);
66 $this->assertEquals($user3->id, array_shift($memberinfo)->id);
67 $this->assertEquals($user4->id, array_shift($memberinfo)->id);
68 $this->assertEquals($user2->id, array_shift($memberinfo)->id);
69 }
3edac090
JD
70
71 /**
72 * Test search_get_user_details returns the correct profile data when $CFG->messagingallusers is disabled.
73 */
74 public function test_search_get_user_details_sitewide_disabled() {
75 global $DB;
76 set_config('messagingallusers', false);
77
78 // Two students sharing course 1, visible profile within course (no groups).
79 $user1 = $this->getDataGenerator()->create_user();
80 $user2 = $this->getDataGenerator()->create_user();
81 $course1 = $this->getDataGenerator()->create_course((object) ['groupmode' => 0]);
82 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
83 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
84
85 // A teacher in course 1.
86 $user3 = $this->getDataGenerator()->create_user();
87 $this->getDataGenerator()->enrol_user($user3->id, $course1->id, 'editingteacher');
88
89 // Two students sharing course 2, separate groups (profiles not visible to one another).
90 // Note: no groups are created here, but separate groups mode alone is enough to restrict profile access.
91 $user4 = $this->getDataGenerator()->create_user();
92 $user5 = $this->getDataGenerator()->create_user();
93 $course2 = $this->getDataGenerator()->create_course((object) ['groupmode' => 1]);
94 $this->getDataGenerator()->enrol_user($user4->id, $course2->id);
95 $this->getDataGenerator()->enrol_user($user5->id, $course2->id);
96
97 // A teacher in course 2.
98 $user6 = $this->getDataGenerator()->create_user();
99 $this->getDataGenerator()->enrol_user($user6->id, $course2->id, 'editingteacher');
100
101 // Teacher and course contact in course 3.
102 $user7 = $this->getDataGenerator()->create_user();
103 $course3 = $this->getDataGenerator()->create_course();
104 $this->getDataGenerator()->enrol_user($user7->id, $course3->id, 'editingteacher');
105 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
106
107 // Make teachers course contacts.
108 set_config('coursecontact', $teacherrole->id);
109
110 // User 1 should be able to see users within their course, but not course contacts or students in other courses.
111 $this->setUser($user1);
112 $this->assertNotEmpty(\core_message\helper::search_get_user_details($user2)); // Student in same course.
113 $this->assertEmpty(\core_message\helper::search_get_user_details($user4)); // Student in another course.
114 $this->assertNotEmpty(\core_message\helper::search_get_user_details($user3)); // Teacher in same course.
115 $this->assertEmpty(\core_message\helper::search_get_user_details($user7)); // Teacher (course contact) in another course.
116
117 // User 3 should be able to see the teacher in their own course, but not other students in that course nor course contacts
118 // or students in other courses.
119 $this->setUser($user4);
120 $this->assertEmpty(\core_message\helper::search_get_user_details($user5)); // Student in same course.
121 $this->assertEmpty(\core_message\helper::search_get_user_details($user1)); // Student in another course.
122 $this->assertNotEmpty(\core_message\helper::search_get_user_details($user6)); // Teacher in same course.
123 $this->assertEmpty(\core_message\helper::search_get_user_details($user7)); // Teacher (course contact) in another course.
124 }
125
126 /**
127 * Test search_get_user_details returns the correct profile data when $CFG->messagingallusers is enabled.
128 */
129 public function test_search_get_user_details_sitewide_enabled() {
130 global $DB;
131 set_config('messagingallusers', true);
132
133 // Two students sharing course 1, visible profile within course (no groups).
134 $user1 = $this->getDataGenerator()->create_user();
135 $user2 = $this->getDataGenerator()->create_user();
136 $course1 = $this->getDataGenerator()->create_course((object) ['groupmode' => 0]);
137 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
138 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
139
140 // A teacher in course 1.
141 $user3 = $this->getDataGenerator()->create_user();
142 $this->getDataGenerator()->enrol_user($user3->id, $course1->id, 'editingteacher');
143
144 // Two students sharing course 2, separate groups (profiles not visible to one another).
145 // Note: no groups are created here, but separate groups mode alone is enough to restrict profile access.
146 $user4 = $this->getDataGenerator()->create_user();
147 $user5 = $this->getDataGenerator()->create_user();
148 $course2 = $this->getDataGenerator()->create_course((object) ['groupmode' => 1]);
149 $this->getDataGenerator()->enrol_user($user4->id, $course2->id);
150 $this->getDataGenerator()->enrol_user($user5->id, $course2->id);
151
152 // A teacher in course 2.
153 $user6 = $this->getDataGenerator()->create_user();
154 $this->getDataGenerator()->enrol_user($user6->id, $course2->id, 'editingteacher');
155
156 // Teacher and course contact in course 3.
157 $user7 = $this->getDataGenerator()->create_user();
158 $course3 = $this->getDataGenerator()->create_course();
159 $this->getDataGenerator()->enrol_user($user7->id, $course3->id, 'editingteacher');
160 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
161
162 // Make teachers course contacts.
163 set_config('coursecontact', $teacherrole->id);
164
165 // User 1 should be able to see users within their course and course contacts, but not students in other courses.
166 $this->setUser($user1);
167 $this->assertNotEmpty(\core_message\helper::search_get_user_details($user2)); // Student in same course.
168 $this->assertEmpty(\core_message\helper::search_get_user_details($user4)); // Student in another course.
169 $this->assertNotEmpty(\core_message\helper::search_get_user_details($user3)); // Teacher in same course.
170 $this->assertNotEmpty(\core_message\helper::search_get_user_details($user7)); // Teacher (course contact) in another course.
171
172 // User 3 should be able to see the teacher in their own course, but not other students in that course nor course contacts
173 // or students in other courses.
174 $this->setUser($user4);
175 $this->assertEmpty(\core_message\helper::search_get_user_details($user5)); // Student in same course.
176 $this->assertEmpty(\core_message\helper::search_get_user_details($user1)); // Student in another course.
177 $this->assertNotEmpty(\core_message\helper::search_get_user_details($user6)); // Teacher in same course.
178 $this->assertNotEmpty(\core_message\helper::search_get_user_details($user7)); // Teacher (course contact) in another course.
179 }
9e6734a7 180}