Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   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 mod_bigbluebuttonbn\external;
  18  
  19  use external_api;
  20  use mod_bigbluebuttonbn\instance;
  21  use mod_bigbluebuttonbn\local\config;
  22  use mod_bigbluebuttonbn\test\testcase_helper_trait;
  23  use moodle_exception;
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  29  
  30  /**
  31   * Tests for the get_join_url class.
  32   *
  33   * @package    mod_bigbluebuttonbn
  34   * @category   test
  35   * @copyright  2021 - present, Blindside Networks Inc
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   * @author    Laurent David (laurent@call-learning.fr)
  38   * @covers \mod_bigbluebuttonbn\external\get_join_url
  39   */
  40  class get_join_url_test extends \externallib_advanced_testcase {
  41      use testcase_helper_trait;
  42  
  43      /**
  44       * Setup for test
  45       */
  46      public function setUp(): void {
  47          parent::setUp();
  48          $this->initialise_mock_server();
  49      }
  50  
  51      /**
  52       * Helper
  53       *
  54       * @param mixed ...$params
  55       * @return mixed
  56       */
  57      protected function get_join_url(...$params) {
  58          $getjoinurl = get_join_url::execute(...$params);
  59  
  60          return external_api::clean_returnvalue(get_join_url::execute_returns(), $getjoinurl);
  61      }
  62  
  63      /**
  64       * Test execute API CALL with no instance
  65       */
  66      public function test_execute_no_instance() {
  67          $this->expectExceptionMessageMatches('/No such instance.*/');
  68          $joinurl = $this->get_join_url(1234, 5678);
  69  
  70          $this->assertIsArray($joinurl);
  71          $this->assertArrayNotHasKey('join_url', $joinurl);
  72          $this->assertEquals(false, $joinurl['join_url']);
  73      }
  74  
  75      /**
  76       * Test execute API CALL without login
  77       */
  78      public function test_execute_without_login() {
  79          $this->resetAfterTest();
  80  
  81          $course = $this->getDataGenerator()->create_course();
  82          $record = $this->getDataGenerator()->create_module('bigbluebuttonbn', ['course' => $course->id]);
  83          $instance = instance::get_from_instanceid($record->id);
  84  
  85          $this->expectException(moodle_exception::class);
  86          $this->get_join_url($instance->get_cm_id());
  87      }
  88  
  89      /**
  90       * Test execute API CALL with invalid login
  91       */
  92      public function test_execute_with_invalid_login() {
  93          $this->resetAfterTest();
  94  
  95          $generator = $this->getDataGenerator();
  96          $course = $generator->create_course();
  97          $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
  98          $instance = instance::get_from_instanceid($record->id);
  99  
 100          $user = $generator->create_user();
 101          $this->setUser($user);
 102  
 103          $this->expectException(moodle_exception::class);
 104          $this->get_join_url($instance->get_cm_id());
 105      }
 106  
 107      /**
 108       * When login as a student
 109       */
 110      public function test_execute_with_valid_login() {
 111          $this->resetAfterTest();
 112  
 113          $generator = $this->getDataGenerator();
 114          $course = $generator->create_course();
 115          $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
 116          $instance = instance::get_from_instanceid($record->id);
 117  
 118          $user = $generator->create_and_enrol($course, 'student');
 119          $bbbgenerator = $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn');
 120          // Make sure the meeting is running (this is not the default with the mock server).
 121          $bbbgenerator->create_meeting([
 122              'instanceid' => $instance->get_instance_id(),
 123              'groupid' => $instance->get_group_id()
 124          ]);
 125  
 126          $this->setUser($user);
 127  
 128          $joinurl = $this->get_join_url($instance->get_cm_id());
 129  
 130          $this->assertIsArray($joinurl);
 131          $this->assertArrayHasKey('join_url', $joinurl);
 132          $this->assertEmpty($joinurl['warnings']);
 133          $this->assertNotNull($joinurl['join_url']);
 134      }
 135  
 136      /**
 137       * Check that URL are different depending on the group.
 138       */
 139      public function test_execute_with_group() {
 140          $this->resetAfterTest();
 141  
 142          $generator = $this->getDataGenerator();
 143          $course = $generator->create_course();
 144          $g1 = $generator->create_group(['courseid' => $course->id]);
 145          $g2 = $generator->create_group(['courseid' => $course->id]);
 146          $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
 147          $instance = instance::get_from_instanceid($record->id);
 148  
 149          $user = $generator->create_and_enrol($course, 'student');
 150          $bbbgenerator = $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn');
 151          // Make sure the meeting is running (this is not the default with the mock server).
 152          $bbbgenerator->create_meeting([
 153              'instanceid' => $instance->get_instance_id(),
 154              'groupid' => $instance->get_group_id()
 155          ]);
 156  
 157          $this->setUser($user);
 158  
 159          $joinurlnogroup = $this->get_join_url($instance->get_cm_id());
 160          $joinurlnogroupv2 = $this->get_join_url($instance->get_cm_id());
 161          $joinurlg1 = $this->get_join_url($instance->get_cm_id(), $g1->id);
 162          $joinurlg2 = $this->get_join_url($instance->get_cm_id(), $g2->id);
 163  
 164          foreach ([$joinurlnogroup, $joinurlnogroupv2, $joinurlg1, $joinurlg2] as $join) {
 165              $this->assertIsArray($join);
 166              $this->assertArrayHasKey('join_url', $join);
 167              $this->assertEmpty($join['warnings']);
 168              $this->assertNotNull($join['join_url']);
 169          }
 170          $this->assertNotEquals($joinurlnogroup['join_url'], $joinurlg1['join_url']);
 171          $this->assertNotEquals($joinurlg2['join_url'], $joinurlg1['join_url']);
 172      }
 173  
 174      /**
 175       * Check that we return the same URL once meeting is started.
 176       */
 177      public function test_execute_with_same_url() {
 178          $this->resetAfterTest();
 179  
 180          $generator = $this->getDataGenerator();
 181          $course = $generator->create_course();
 182          $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
 183          $instance = instance::get_from_instanceid($record->id);
 184  
 185          $user = $generator->create_and_enrol($course, 'student');
 186  
 187          $bbbgenerator = $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn');
 188          // Make sure the meeting is running (this is not the default with the mock server).
 189          $bbbgenerator->create_meeting([
 190              'instanceid' => $instance->get_instance_id(),
 191              'groupid' => $instance->get_group_id()
 192          ]);
 193  
 194          $this->setUser($user);
 195  
 196          $joinurl = $this->get_join_url($instance->get_cm_id());
 197          $joinurlv2 = $this->get_join_url($instance->get_cm_id());
 198  
 199          foreach ([$joinurl, $joinurlv2] as $join) {
 200              $this->assertIsArray($join);
 201              $this->assertArrayHasKey('join_url', $join);
 202              $this->assertEmpty($join['warnings']);
 203              $this->assertNotNull($join['join_url']);
 204          }
 205          $this->assertEquals($joinurl['join_url'], $joinurlv2['join_url']);
 206      }
 207  
 208      /**
 209       * Check that we return the same URL once meeting is started.
 210       */
 211      public function test_user_limit() {
 212          $this->resetAfterTest();
 213          set_config('bigbluebuttonbn_userlimit_editable', true);
 214          $generator = $this->getDataGenerator();
 215          $course = $generator->create_course();
 216          $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id, 'userlimit' => 2]);
 217  
 218          $user1 = $generator->create_and_enrol($course, 'student');
 219          $user2 = $generator->create_and_enrol($course, 'student');
 220          $instance = instance::get_from_instanceid($record->id);
 221  
 222          $bbbgenerator = $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn');
 223          // Make sure the meeting is running (this is not the default with the mock server).
 224          $bbbgenerator->create_meeting([
 225              'instanceid' => $instance->get_instance_id(),
 226              'groupid' => $instance->get_group_id(),
 227              'participants' => 2
 228          ]);
 229          $this->setUser($user1);
 230          $joinurl = $this->get_join_url($instance->get_cm_id());
 231          $this->assertNotNull($joinurl['warnings']);
 232          $this->assertEquals('userlimitreached', $joinurl['warnings'][0]['warningcode']);
 233      }
 234  }
 235