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_matrix;
  18  
  19  use moodle_exception;
  20  
  21  /**
  22   * Class matrix_user_manager_test to test the matrix user manager.
  23   *
  24   * @package    communication_matrix
  25   * @category   test
  26   * @copyright  2023 Stevani Andolo <stevani.andolo@moodle.com>
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   * @covers \communication_matrix\matrix_user_manager
  29   */
  30  class matrix_user_manager_test extends \advanced_testcase {
  31      /**
  32       * Test fetcihing a users matrix userid from Moodle.
  33       */
  34      public function test_get_matrixid_from_moodle_without_field(): void {
  35          $user = get_admin();
  36  
  37          $this->assertNull(matrix_user_manager::get_matrixid_from_moodle($user->id));
  38      }
  39  
  40      /**
  41       * Test fetching a user's matrix userid from Moodle.
  42       */
  43      public function test_get_matrixid_from_moodle(): void {
  44          $this->resetAfterTest();
  45  
  46          $user1 = $this->getDataGenerator()->create_user();
  47          $user2 = $this->getDataGenerator()->create_user();
  48  
  49          // Add user ids to both users.
  50          matrix_user_manager::set_matrix_userid_in_moodle(
  51              $user1->id,
  52              '@someexampleuser:matrix.moodle.org',
  53          );
  54  
  55          matrix_user_manager::set_matrix_userid_in_moodle(
  56              $user2->id,
  57              '@someotherexampleuser:matrix.moodle.org',
  58          );
  59  
  60          // And confirm that they're fetched back.
  61          $this->assertEquals(
  62              '@someexampleuser:matrix.moodle.org',
  63              matrix_user_manager::get_matrixid_from_moodle($user1->id),
  64          );
  65          $this->assertEquals(
  66              '@someotherexampleuser:matrix.moodle.org',
  67              matrix_user_manager::get_matrixid_from_moodle($user2->id),
  68          );
  69      }
  70  
  71      /**
  72       * Test fetching a formatted matrix userid from Moodle when no server is set.
  73       */
  74      public function test_get_formatted_matrix_userid_unset(): void {
  75          $this->expectException(moodle_exception::class);
  76  
  77          matrix_user_manager::get_formatted_matrix_userid('No value');
  78      }
  79  
  80      /**
  81       * Test fetch of a formatted matrix userid.
  82       *
  83       * @dataProvider get_formatted_matrix_userid_provider
  84       * @param string $server
  85       * @param string $username The moodle username to turn into a Matrix username
  86       * @param string $expecteduserid The expected matrix user id
  87       */
  88      public function test_get_formatted_matrix_userid(
  89          string $server,
  90          string $username,
  91          string $expecteduserid,
  92      ): void {
  93          $this->resetAfterTest();
  94  
  95          set_config('matrixhomeserverurl', $server, 'communication_matrix');
  96          $this->assertEquals(
  97              $expecteduserid,
  98              matrix_user_manager::get_formatted_matrix_userid($username),
  99          );
 100      }
 101  
 102      /**
 103       * Data provider for get_formatted_matrix_userid.
 104       *
 105       * @return array
 106       */
 107      public static function get_formatted_matrix_userid_provider(): array {
 108          return [
 109              'alphanumeric' => [
 110                  'https://matrix.example.org',
 111                  'alphabet1',
 112                  '@alphabet1:matrix.example.org',
 113              ],
 114              'chara' => [
 115                  'https://matrix.example.org',
 116                  'asdf#$%^&*()+{}|<>?!,asdf',
 117                  '@asdf.................asdf:matrix.example.org',
 118              ],
 119              'local server' => [
 120                  'https://synapse',
 121                  'colin.creavey',
 122                  '@colin.creavey:synapse',
 123              ],
 124              'server with port' => [
 125                  'https://matrix.example.org:8448',
 126                  'colin.creavey',
 127                  '@colin.creavey:matrix.example.org',
 128              ],
 129          ];
 130      }
 131  
 132      /**
 133       * Data provider for set_matrix_userid_in_moodle.
 134       *
 135       * @return array
 136       */
 137      public static function set_matrix_userid_in_moodle_provider(): array {
 138          return array_combine(
 139              array_keys(self::get_formatted_matrix_userid_provider()),
 140              array_map(
 141                  fn($value) => [$value[2]],
 142                  self::get_formatted_matrix_userid_provider(),
 143              ),
 144          );
 145      }
 146  
 147      /**
 148       * Test setting of a user's matrix userid in Moodle.
 149       *
 150       * @dataProvider set_matrix_userid_in_moodle_provider
 151       * @param string $expectedusername
 152       */
 153      public function test_set_matrix_userid_in_moodle(
 154          string $expectedusername,
 155      ): void {
 156          $this->resetAfterTest();
 157  
 158          $user = $this->getDataGenerator()->create_user();
 159          matrix_user_manager::set_matrix_userid_in_moodle($user->id, $expectedusername);
 160  
 161          // Get created matrixuserid from moodle.
 162          $this->assertEquals(
 163              $expectedusername,
 164              matrix_user_manager::get_matrixid_from_moodle($user->id),
 165          );
 166      }
 167  
 168      /**
 169       * Test for getting a formatted matrix home server id.
 170       *
 171       * @dataProvider get_formatted_matrix_home_server_provider
 172       * @param string $input
 173       * @param string $expectedoutput
 174       */
 175      public function test_get_formatted_matrix_home_server(
 176          string $input,
 177          string $expectedoutput
 178      ): void {
 179          $this->resetAfterTest();
 180  
 181          set_config(
 182              'matrixhomeserverurl',
 183              $input,
 184              'communication_matrix',
 185          );
 186  
 187          $this->assertEquals(
 188              $expectedoutput,
 189              matrix_user_manager::get_formatted_matrix_home_server(),
 190          );
 191      }
 192  
 193      /**
 194       * Data provider for get_formatted_matrix_home_server.
 195       *
 196       * @return array
 197       */
 198      public static function get_formatted_matrix_home_server_provider(): array {
 199          return [
 200              'www is removed' => [
 201                  'https://www.example.org',
 202                  'example.org',
 203              ],
 204              'www is not removed if it is not at the beginning' => [
 205                  'https://matrix.www.example.org',
 206                  'matrix.www.example.org',
 207              ],
 208              'others are not removed' => [
 209                  'https://matrix.example.org',
 210                  'matrix.example.org',
 211              ],
 212          ];
 213      }
 214  
 215      /**
 216       * Test creation of matrix user profile fields.
 217       */
 218      public function test_create_matrix_user_profile_fields(): void {
 219          global $CFG;
 220          require_once("{$CFG->dirroot}/user/profile/lib.php");
 221  
 222          $this->resetAfterTest();
 223  
 224          $matrixprofilefield = get_config('communication_matrix', 'matrixuserid_field');
 225          $this->assertFalse($matrixprofilefield);
 226  
 227          $this->assertIsString(matrix_user_manager::create_matrix_user_profile_fields());
 228          $matrixprofilefield = get_config('communication_matrix', 'matrixuserid_field');
 229          $this->assertNotFalse($matrixprofilefield);
 230  
 231          $user = $this->getDataGenerator()->create_user();
 232          $this->assertObjectHasAttribute($matrixprofilefield, profile_user_record($user->id));
 233      }
 234  }