. namespace core_communication; defined('MOODLE_INTERNAL') || die(); require_once(__DIR__ . '/communication_test_helper_trait.php'); /** * Class api_test to test the communication public api and its associated methods. * * @package core_communication * @category test * @copyright 2023 Safat Shahin * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @covers \core_communication\api */ class api_test extends \advanced_testcase { use communication_test_helper_trait; public function setUp(): void { parent::setUp(); $this->resetAfterTest(); $this->setup_communication_configs(); } /** * Test the communication plugin list for the form element returns the correct number of plugins. */ public function test_get_communication_plugin_list_for_form(): void { $communicationplugins = \core_communication\api::get_communication_plugin_list_for_form(); // Get the communication plugins. $plugins = \core_component::get_plugin_list('communication'); // Check the number of plugins matches plus 1 as we have none in the selection. $this->assertCount(count($plugins) + 1, $communicationplugins); } /** * Test set data to the instance. */ public function test_set_data(): void { $course = $this->get_course(); $communication = \core_communication\api::load_by_instance( 'core_course', 'coursecommunication', $course->id ); // Sample data. $roomname = 'Sampleroom'; $provider = 'communication_matrix'; // Set the data. $communication->set_data($course); // Test the set data. $this->assertEquals($roomname, $course->communicationroomname); $this->assertEquals($provider, $course->selectedcommunication); } /** * Test get_current_communication_provider method. */ public function test_get_provider(): void { $course = $this->get_course(); $communication = \core_communication\api::load_by_instance( 'core_course', 'coursecommunication', $course->id ); $this->assertEquals('communication_matrix', $communication->get_provider()); } /** * Test set_avatar method. */ public function test_set_avatar(): void { global $CFG; $this->setAdminUser(); $course = $this->get_course('Sampleroom', 'none'); // Sample data. $communicationroomname = 'Sampleroom'; $selectedcommunication = 'communication_matrix'; $avatar = $this->create_communication_file( 'moodle_logo.jpg', 'moodle_logo.jpg', ); // Create the room, settingthe avatar. $communication = \core_communication\api::load_by_instance( 'core_course', 'coursecommunication', $course->id, ); $communication->create_and_configure_room($selectedcommunication, $communicationroomname, $avatar); // Reload the communication processor. $communicationprocessor = processor::load_by_instance( 'core_course', 'coursecommunication', $course->id, ); // Compare result. $this->assertEquals( $avatar->get_contenthash(), $communicationprocessor->get_avatar()->get_contenthash(), ); } /** * Test the create_and_configure_room method to add/create tasks. */ public function test_create_and_configure_room(): void { // Get the course by disabling communication so that we can create it manually calling the api. $course = $this->get_course('Sampleroom', 'none'); // Sample data. $communicationroomname = 'Sampleroom'; $selectedcommunication = 'communication_matrix'; $communication = \core_communication\api::load_by_instance( 'core_course', 'coursecommunication', $course->id ); $communication->create_and_configure_room($selectedcommunication, $communicationroomname); // Test the tasks added. $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\create_and_configure_room_task'); $this->assertCount(1, $adhoctask); $adhoctask = reset($adhoctask); $this->assertInstanceOf('\\core_communication\\task\\create_and_configure_room_task', $adhoctask); // Test the communication record exists. $communicationprocessor = processor::load_by_instance( 'core_course', 'coursecommunication', $course->id ); $this->assertEquals($communicationroomname, $communicationprocessor->get_room_name()); $this->assertEquals($selectedcommunication, $communicationprocessor->get_provider()); } /** * Test the create_and_configure_room method to add/create tasks when no communication provider selected. */ public function test_create_and_configure_room_without_communication_provider_selected(): void { // Get the course by disabling communication so that we can create it manually calling the api. $course = $this->get_course('Sampleroom', 'none'); // Test the tasks added. $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\create_and_configure_room_task'); $this->assertCount(0, $adhoctask); // Test the communication record exists. $communicationprocessor = processor::load_by_instance( 'core_course', 'coursecommunication', $course->id ); $this->assertNull($communicationprocessor); } /** * Test update operation. */ public function test_update_room(): void { $course = $this->get_course(); // Sample data. $communicationroomname = 'Sampleroomupdated'; $selectedcommunication = 'communication_matrix'; $communication = \core_communication\api::load_by_instance( 'core_course', 'coursecommunication', $course->id ); $communication->update_room($selectedcommunication, $communicationroomname); // Test the communication record exists. $communicationprocessor = processor::load_by_instance( 'core_course', 'coursecommunication', $course->id ); $this->assertEquals($communicationroomname, $communicationprocessor->get_room_name()); $this->assertEquals($selectedcommunication, $communicationprocessor->get_provider()); } /** * Test delete operation. */ public function test_delete_room(): void { $course = $this->get_course(); // Sample data. $communicationroomname = 'Sampleroom'; $selectedcommunication = 'communication_matrix'; // Test the communication record exists. $communicationprocessor = processor::load_by_instance( 'core_course', 'coursecommunication', $course->id ); $this->assertEquals($communicationroomname, $communicationprocessor->get_room_name()); $this->assertEquals($selectedcommunication, $communicationprocessor->get_provider()); $communication = \core_communication\api::load_by_instance( 'core_course', 'coursecommunication', $course->id ); $communication->delete_room(); // Test the tasks added. $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\delete_room_task'); // Should be 2 as one for create, another for update. $this->assertCount(1, $adhoctask); $adhoctask = reset($adhoctask); $this->assertInstanceOf('\\core_communication\\task\\delete_room_task', $adhoctask); } /** * Test the update_room_membership for adding adn removing members. */ public function test_update_room_membership(): void { $course = $this->get_course(); $userid = $this->get_user()->id; // First test the adding members to a room. $communication = \core_communication\api::load_by_instance( 'core_course', 'coursecommunication', $course->id ); $communication->add_members_to_room([$userid]); // Test the tasks added. $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\add_members_to_room_task'); $this->assertCount(1, $adhoctask); // Now test the removing members from a room. $communication->remove_members_from_room([$userid]); // Test the tasks added. $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\remove_members_from_room'); $this->assertCount(1, $adhoctask); } /** * Test the enabled communication plugin list and default. */ public function test_get_enabled_providers_and_default(): void { list($communicationproviders, $defaulprovider) = \core_communication\api::get_enabled_providers_and_default(); // Get the communication plugins. $plugins = \core_component::get_plugin_list('communication'); // Check the number of plugins matches plus 1 as we have none in the selection. $this->assertCount(count($plugins) + 1, $communicationproviders); $this->assertEquals(processor::PROVIDER_NONE, $defaulprovider); } }