Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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 communication_customlink;
  18  
  19  use core_communication\processor;
  20  use core_communication\communication_test_helper_trait;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  require_once (__DIR__ . '/../../../tests/communication_test_helper_trait.php');
  25  
  26  /**
  27   * Class communication_feature_test to test the custom link features implemented using the core interfaces.
  28   *
  29   * @package    communication_customlink
  30   * @category   test
  31   * @copyright  2023 Michael Hawkins <michaelh@moodle.com>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   * @coversDefaultClass \communication_customlink\communication_feature
  34   */
  35  class communication_feature_test extends \advanced_testcase {
  36      use communication_test_helper_trait;
  37  
  38      public function setUp(): void {
  39          parent::setUp();
  40          $this->resetAfterTest();
  41          $this->setup_communication_configs();
  42      }
  43  
  44      /**
  45       * Test create, update and delete chat room.
  46       *
  47       * @covers ::load_for_instance
  48       */
  49      public function test_load_for_instance(): void {
  50          $communicationprocessor = $this->get_test_communication_processor();
  51  
  52          $instance = communication_feature::load_for_instance($communicationprocessor);
  53          $this->assertInstanceOf('communication_customlink\communication_feature', $instance);
  54      }
  55  
  56      /**
  57       * Test create, update and delete chat room.
  58       *
  59       * @covers ::create_chat_room
  60       * @covers ::update_chat_room
  61       * @covers ::delete_chat_room
  62       */
  63      public function test_create_update_delete_chat_room(): void {
  64          $communicationprocessor = $this->get_test_communication_processor();
  65  
  66          // Create, update and delete room should always return true because this provider contains
  67          // a link to a room, but does not manage the existence of the room.
  68          $createroomresult = $communicationprocessor->get_room_provider()->create_chat_room();
  69          $updateroomresult = $communicationprocessor->get_room_provider()->update_chat_room();
  70          $deleteroomresult = $communicationprocessor->get_room_provider()->delete_chat_room();
  71          $this->assertTrue($createroomresult);
  72          $this->assertTrue($updateroomresult);
  73          $this->assertTrue($deleteroomresult);
  74      }
  75  
  76      /**
  77       * Test save form data with provider's custom field and fetching with get_chat_room_url().
  78       *
  79       * @covers ::save_form_data
  80       * @covers ::get_chat_room_url
  81       */
  82      public function test_save_form_data(): void {
  83          $communicationprocessor = $this->get_test_communication_processor();
  84          $customlinkurl = 'https://moodle.org/message/index.php';
  85          $formdatainstance = (object) ['customlinkurl' => $customlinkurl];
  86  
  87          // Test the custom link URL is saved and can be retrieved as expected.
  88          $communicationprocessor->get_form_provider()->save_form_data($formdatainstance);
  89          $fetchedurl = $communicationprocessor->get_room_provider()->get_chat_room_url();
  90          $this->assertEquals($customlinkurl, $fetchedurl);
  91      }
  92  
  93      /**
  94       * Create a test custom link communication processor object.
  95       *
  96       * @return processor
  97       */
  98      protected function get_test_communication_processor(): processor {
  99          $course = $this->getDataGenerator()->create_course();
 100          $instanceid = $course->id;
 101          $context = \core\context\system::instance();
 102          $component = 'core_course';
 103          $instancetype = 'coursecommunication';
 104          $selectedcommunication = 'communication_customlink';
 105          $communicationroomname = 'communicationroom';
 106  
 107          $communicationprocessor = processor::create_instance(
 108              $context,
 109              $selectedcommunication,
 110              $instanceid,
 111              $component,
 112              $instancetype,
 113              $communicationroomname,
 114          );
 115  
 116          return $communicationprocessor;
 117      }
 118  
 119      /**
 120       * Test if the selected provider is configured.
 121       *
 122       * @covers ::is_configured
 123       */
 124      public function test_is_configured(): void {
 125          $communicationprocessor = $this->get_test_communication_processor();
 126          $this->assertTrue($communicationprocessor->get_form_provider()->is_configured());
 127      }
 128  }