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 21 defined('MOODLE_INTERNAL') || die(); 22 23 require_once (__DIR__ . '/../provider/matrix/tests/matrix_test_helper_trait.php'); 24 require_once (__DIR__ . '/communication_test_helper_trait.php'); 25 26 /** 27 * Class api_test to test the communication public api and its associated methods. 28 * 29 * @package core_communication 30 * @category test 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\api 34 */ 35 class api_test extends \advanced_testcase { 36 use matrix_test_helper_trait; 37 use communication_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 set data to the instance. 48 */ 49 public function test_set_data(): void { 50 $course = $this->get_course(); 51 52 $communication = \core_communication\api::load_by_instance( 53 context: \core\context\course::instance($course->id), 54 component: 'core_course', 55 instancetype: 'coursecommunication', 56 instanceid: $course->id, 57 ); 58 59 // Sample data. 60 $roomname = 'Sampleroom'; 61 $provider = 'communication_matrix'; 62 63 // Set the data. 64 $communication->set_data($course); 65 66 // Test the set data. 67 $this->assertEquals($roomname, $course->communicationroomname); 68 $this->assertEquals($provider, $course->selectedcommunication); 69 } 70 71 /** 72 * Test get_current_communication_provider method. 73 */ 74 public function test_get_provider(): void { 75 $course = $this->get_course(); 76 77 $communication = \core_communication\api::load_by_instance( 78 context: \core\context\course::instance($course->id), 79 component: 'core_course', 80 instancetype: 'coursecommunication', 81 instanceid: $course->id, 82 ); 83 84 $this->assertEquals('communication_matrix', $communication->get_provider()); 85 } 86 87 /** 88 * Test set_avatar method. 89 */ 90 public function test_set_avatar(): void { 91 global $CFG; 92 $this->setAdminUser(); 93 $course = $this->get_course('Sampleroom', 'none'); 94 95 // Sample data. 96 $communicationroomname = 'Sampleroom'; 97 $selectedcommunication = 'communication_matrix'; 98 99 $avatar = $this->create_communication_file( 100 'moodle_logo.jpg', 101 'moodle_logo.jpg', 102 ); 103 104 // Create the room, settingthe avatar. 105 $communication = \core_communication\api::load_by_instance( 106 context: \core\context\course::instance($course->id), 107 component: 'core_course', 108 instancetype: 'coursecommunication', 109 instanceid: $course->id, 110 provider: $selectedcommunication, 111 ); 112 113 $communication->create_and_configure_room($communicationroomname, $avatar); 114 115 // Reload the communication processor. 116 $communicationprocessor = processor::load_by_instance( 117 context: \core\context\course::instance($course->id), 118 component: 'core_course', 119 instancetype: 'coursecommunication', 120 instanceid: $course->id, 121 ); 122 123 // Compare result. 124 $this->assertEquals( 125 $avatar->get_contenthash(), 126 $communicationprocessor->get_avatar()->get_contenthash(), 127 ); 128 } 129 130 /** 131 * Test the create_and_configure_room method to add/create tasks. 132 */ 133 public function test_create_and_configure_room(): void { 134 // Get the course by disabling communication so that we can create it manually calling the api. 135 $course = $this->get_course('Sampleroom', 'none'); 136 137 // Sample data. 138 $communicationroomname = 'Sampleroom'; 139 $selectedcommunication = 'communication_matrix'; 140 141 $communication = \core_communication\api::load_by_instance( 142 context: \core\context\course::instance($course->id), 143 component: 'core_course', 144 instancetype: 'coursecommunication', 145 instanceid: $course->id, 146 provider: $selectedcommunication, 147 ); 148 $communication->create_and_configure_room($communicationroomname); 149 150 // Test the tasks added. 151 $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\create_and_configure_room_task'); 152 $this->assertCount(1, $adhoctask); 153 154 $adhoctask = reset($adhoctask); 155 $this->assertInstanceOf('\\core_communication\\task\\create_and_configure_room_task', $adhoctask); 156 157 // Test the communication record exists. 158 $communicationprocessor = processor::load_by_instance( 159 context: \core\context\course::instance($course->id), 160 component: 'core_course', 161 instancetype: 'coursecommunication', 162 instanceid: $course->id, 163 ); 164 165 $this->assertEquals($communicationroomname, $communicationprocessor->get_room_name()); 166 $this->assertEquals($selectedcommunication, $communicationprocessor->get_provider()); 167 } 168 169 /** 170 * Test the create_and_configure_room method to add/create tasks when no communication provider selected. 171 */ 172 public function test_create_and_configure_room_without_communication_provider_selected(): void { 173 // Get the course by disabling communication so that we can create it manually calling the api. 174 $course = $this->get_course('Sampleroom', 'none'); 175 176 // Test the tasks added. 177 $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\create_and_configure_room_task'); 178 $this->assertCount(0, $adhoctask); 179 180 // Test the communication record exists. 181 $communicationprocessor = processor::load_by_instance( 182 context: \core\context\course::instance($course->id), 183 component: 'core_course', 184 instancetype: 'coursecommunication', 185 instanceid: $course->id, 186 ); 187 188 $this->assertNull($communicationprocessor); 189 } 190 191 /** 192 * Test update operation. 193 */ 194 public function test_update_room(): void { 195 $course = $this->get_course(); 196 197 // Sample data. 198 $communicationroomname = 'Sampleroomupdated'; 199 $selectedcommunication = 'communication_matrix'; 200 201 $communication = \core_communication\api::load_by_instance( 202 context: \core\context\course::instance($course->id), 203 component: 'core_course', 204 instancetype: 'coursecommunication', 205 instanceid: $course->id, 206 ); 207 $communication->update_room(processor::PROVIDER_ACTIVE, $communicationroomname); 208 209 // Test the communication record exists. 210 $communicationprocessor = processor::load_by_instance( 211 context: \core\context\course::instance($course->id), 212 component: 'core_course', 213 instancetype: 'coursecommunication', 214 instanceid: $course->id, 215 ); 216 217 $this->assertEquals($communicationroomname, $communicationprocessor->get_room_name()); 218 $this->assertEquals($selectedcommunication, $communicationprocessor->get_provider()); 219 $this->assertTrue($communicationprocessor->is_instance_active()); 220 221 $communication->update_room(processor::PROVIDER_INACTIVE, $communicationroomname); 222 223 // Test updating active state. 224 $communicationprocessor = processor::load_by_instance( 225 context: \core\context\course::instance($course->id), 226 component: 'core_course', 227 instancetype: 'coursecommunication', 228 instanceid: $course->id, 229 provider: $selectedcommunication, 230 ); 231 232 $this->assertEquals($communicationroomname, $communicationprocessor->get_room_name()); 233 $this->assertEquals($selectedcommunication, $communicationprocessor->get_provider()); 234 $this->assertFalse($communicationprocessor->is_instance_active()); 235 } 236 237 /** 238 * Test delete operation. 239 */ 240 public function test_delete_room(): void { 241 $course = $this->get_course(); 242 243 // Sample data. 244 $communicationroomname = 'Sampleroom'; 245 $selectedcommunication = 'communication_matrix'; 246 247 // Test the communication record exists. 248 $communicationprocessor = processor::load_by_instance( 249 context: \core\context\course::instance($course->id), 250 component: 'core_course', 251 instancetype: 'coursecommunication', 252 instanceid: $course->id, 253 ); 254 255 $this->assertEquals($communicationroomname, $communicationprocessor->get_room_name()); 256 $this->assertEquals($selectedcommunication, $communicationprocessor->get_provider()); 257 258 $communication = \core_communication\api::load_by_instance( 259 context: \core\context\course::instance($course->id), 260 component: 'core_course', 261 instancetype: 'coursecommunication', 262 instanceid: $course->id, 263 ); 264 $communication->delete_room(); 265 266 // Test the tasks added. 267 $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\delete_room_task'); 268 // Should be 2 as one for create, another for update. 269 $this->assertCount(1, $adhoctask); 270 271 $adhoctask = reset($adhoctask); 272 $this->assertInstanceOf('\\core_communication\\task\\delete_room_task', $adhoctask); 273 } 274 275 /** 276 * Test the adding and removing of members from room. 277 * 278 * @covers ::add_members_to_room 279 * @covers ::remove_members_from_room 280 */ 281 public function test_adding_and_removing_of_room_membership(): void { 282 $course = $this->get_course(); 283 $userid = $this->get_user()->id; 284 285 // First test the adding members to a room. 286 $communication = \core_communication\api::load_by_instance( 287 context: \core\context\course::instance($course->id), 288 component: 'core_course', 289 instancetype: 'coursecommunication', 290 instanceid: $course->id, 291 ); 292 $communication->add_members_to_room([$userid]); 293 294 // Test the tasks added. 295 $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\add_members_to_room_task'); 296 $this->assertCount(1, $adhoctask); 297 298 // Now test the removing members from a room. 299 $communication->remove_members_from_room([$userid]); 300 301 // Test the tasks added. 302 $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\remove_members_from_room'); 303 $this->assertCount(1, $adhoctask); 304 } 305 306 /** 307 * Test the update of room membership with the change user role. 308 * 309 * @covers ::update_room_membership 310 */ 311 public function test_update_room_membership_on_user_role_change(): void { 312 global $DB; 313 314 // Generate the data. 315 $user = $this->getDataGenerator()->create_user(); 316 $course = $this->get_course(); 317 $coursecontext = \context_course::instance($course->id); 318 $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']); 319 $this->getDataGenerator()->enrol_user($user->id, $course->id); 320 321 $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\add_members_to_room_task'); 322 $this->assertCount(1, $adhoctask); 323 324 $adhoctask = reset($adhoctask); 325 $this->assertInstanceOf('\\core_communication\\task\\add_members_to_room_task', $adhoctask); 326 327 // Test the tasks added as the role is a teacher. 328 $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\update_room_membership_task'); 329 $this->assertCount(1, $adhoctask); 330 331 $adhoctask = reset($adhoctask); 332 $this->assertInstanceOf('\\core_communication\\task\\update_room_membership_task', $adhoctask); 333 } 334 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body