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.

Differences Between: [Versions 400 and 403] [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 core_external\external_api;
  20  use mod_bigbluebuttonbn\instance;
  21  use mod_bigbluebuttonbn\meeting;
  22  use mod_bigbluebuttonbn\test\testcase_helper_trait;
  23  use moodle_exception;
  24  use require_login_exception;
  25  use core_external\restricted_context_exception;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  31  
  32  /**
  33   * Tests for the update_course class.
  34   *
  35   * @package   mod_bigbluebuttonbn
  36   * @copyright  2021 Andrew Lyons <andrew@nicols.co.uk>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   * @covers \mod_bigbluebuttonbn\external\end_meeting
  39   */
  40  class end_meeting_test extends \externallib_advanced_testcase {
  41      use testcase_helper_trait;
  42      /**
  43       * Setup for test
  44       */
  45      public function setUp(): void {
  46          parent::setUp();
  47          $this->initialise_mock_server();
  48      }
  49  
  50      /**
  51       * Helper
  52       *
  53       * @param mixed ...$params
  54       * @return array|bool|mixed
  55       */
  56      protected function end_meeting(...$params) {
  57          $returnvalue = end_meeting::execute(...$params);
  58  
  59          return external_api::clean_returnvalue(end_meeting::execute_returns(), $returnvalue);
  60      }
  61  
  62      /**
  63       * Test execute API CALL with no instance
  64       */
  65      public function test_execute_no_instance() {
  66          $this->expectException(moodle_exception::class);
  67          $endmeeting = $this->end_meeting(1234, 5678);
  68      }
  69  
  70      /**
  71       * Test execute API CALL without login
  72       */
  73      public function test_execute_without_login() {
  74          $this->resetAfterTest();
  75  
  76          $course = $this->getDataGenerator()->create_course();
  77          $record = $this->getDataGenerator()->create_module('bigbluebuttonbn', ['course' => $course->id]);
  78          $instance = instance::get_from_instanceid($record->id);
  79  
  80          $this->expectException(require_login_exception::class);
  81          $this->end_meeting($instance->get_instance_id(), 0);
  82      }
  83  
  84      /**
  85       * Test execute API CALL with invalid login
  86       */
  87      public function test_execute_with_invalid_login() {
  88          $this->resetAfterTest();
  89  
  90          $generator = $this->getDataGenerator();
  91          $course = $generator->create_course();
  92          $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
  93          $instance = instance::get_from_instanceid($record->id);
  94  
  95          $user = $generator->create_user();
  96          $this->setUser($user);
  97  
  98          $this->expectException(require_login_exception::class);
  99          $this->end_meeting($instance->get_instance_id(), 0);
 100      }
 101  
 102      /**
 103       * When login as a student
 104       */
 105      public function test_execute_with_student_login() {
 106          $this->resetAfterTest();
 107  
 108          $generator = $this->getDataGenerator();
 109          $course = $generator->create_course();
 110          $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
 111          $instance = instance::get_from_instanceid($record->id);
 112  
 113          $user = $generator->create_and_enrol($course, 'student');
 114          $this->setUser($user);
 115  
 116          $this->expectException(restricted_context_exception::class);
 117          $this->end_meeting($instance->get_instance_id(), 0);
 118      }
 119  
 120      /**
 121       * Test execute admin logic
 122       */
 123      public function test_execute_with_admin_login() {
 124          $this->resetAfterTest();
 125  
 126          $generator = $this->getDataGenerator();
 127          $course = $generator->create_course();
 128          $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
 129          $instance = instance::get_from_instanceid($record->id);
 130  
 131          $plugingenerator = $generator->get_plugin_generator('mod_bigbluebuttonbn');
 132          $plugingenerator->create_meeting([
 133              'instanceid' => $instance->get_instance_id(),
 134          ]);
 135  
 136          $this->setAdminUser();
 137  
 138          $result = $this->end_meeting($instance->get_instance_id(), 0);
 139          $this->assertIsArray($result);
 140  
 141          // TODO Check that the meeting was ended on the remote.
 142      }
 143      /**
 144       * Test execute admin logic
 145       */
 146      public function test_execute_end_meeting_already_ended() {
 147          $this->resetAfterTest();
 148  
 149          $generator = $this->getDataGenerator();
 150          $course = $generator->create_course();
 151          $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
 152          $instance = instance::get_from_instanceid($record->id);
 153  
 154          $plugingenerator = $generator->get_plugin_generator('mod_bigbluebuttonbn');
 155          $plugingenerator->create_meeting([
 156              'instanceid' => $instance->get_instance_id(),
 157          ]);
 158  
 159          // Then end the meeting.
 160          // Execute the end command.
 161          $meeting = new meeting($instance);
 162          $meeting->end_meeting();
 163  
 164          $this->setAdminUser();
 165  
 166          $result = $this->end_meeting($instance->get_instance_id(), 0);
 167          $this->assertIsArray($result);
 168  
 169          // TODO Check that the meeting was ended on the remote.
 170      }
 171  }