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 core_communication;
  18  
  19  /**
  20   * Trait communication_test_helper_trait to generate initial setup for communication providers.
  21   *
  22   * @package    core_communication
  23   * @category   test
  24   * @copyright  2023 Safat Shahin <safat.shahin@moodle.com>
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  trait communication_test_helper_trait {
  28      /**
  29       * Setup necessary configs for communication subsystem.
  30       *
  31       * @return void
  32       */
  33      protected function setup_communication_configs(): void {
  34          set_config('enablecommunicationsubsystem', 1);
  35      }
  36  
  37      /**
  38       * Disable configs for communication subsystem.
  39       *
  40       * @return void
  41       */
  42      protected function disable_communication_configs(): void {
  43          set_config('enablecommunicationsubsystem', 0);
  44      }
  45  
  46      /**
  47       * Get or create course if it does not exist
  48       *
  49       * @param string $roomname The room name for the communication api
  50       * @param string $provider The selected provider
  51       * @return \stdClass
  52       */
  53      protected function get_course(
  54          string $roomname = 'Sampleroom',
  55          string $provider = 'communication_matrix'
  56      ): \stdClass {
  57  
  58          $this->setup_communication_configs();
  59          $records = [
  60              'selectedcommunication' => $provider,
  61              'communicationroomname' => $roomname,
  62          ];
  63  
  64          return $this->getDataGenerator()->create_course($records);
  65      }
  66  
  67      /**
  68       * Get or create user if it does not exist.
  69       *
  70       * @param string $firstname The user's firstname for the communication api
  71       * @param string $lastname The user's lastname for the communication api
  72       * @param string $username The user's username for the communication api
  73       * @return \stdClass
  74       */
  75      protected function get_user(
  76          string $firstname = 'Samplefn',
  77          string $lastname = 'Sampleln',
  78          string $username = 'sampleun'
  79      ): \stdClass {
  80  
  81          $this->setup_communication_configs();
  82          $records = [
  83              'firstname' => $firstname,
  84              'lastname' => $lastname,
  85              'username' => $username,
  86          ];
  87  
  88          return $this->getDataGenerator()->create_user($records);
  89      }
  90  
  91  
  92      /**
  93       * Create a stored_file in a draft file area from a fixture file.
  94       *
  95       * @param string $filename The file name within the communication/tests/fixtures folder.
  96       * @param string $storedname The name to use in the database.
  97       * @return \stored_file
  98       */
  99      protected function create_communication_file(
 100          string $filename,
 101          string $storedname,
 102      ): \stored_file {
 103          global $CFG;
 104  
 105          $fs = get_file_storage();
 106  
 107          $itemid = file_get_unused_draft_itemid();
 108          return $fs->create_file_from_pathname((object) [
 109              'contextid' => \context_system::instance()->id,
 110              'component' => 'user',
 111              'filearea' => 'draftfile',
 112              'itemid' => $itemid,
 113              'filepath' => '/',
 114              'filename' => $storedname,
 115          ], "{$CFG->dirroot}/communication/tests/fixtures/{$filename}");
 116      }
 117  }