Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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\test\testcase_helper_trait;
  22  use moodle_exception;
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  global $CFG;
  27  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  28  
  29  /**
  30   * Tests for the get_bigbluebuttons_by_courses class.
  31   *
  32   * @package    mod_bigbluebuttonbn
  33   * @category   test
  34   * @copyright  2021 - present, Blindside Networks Inc
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   * @author    Laurent David (laurent@call-learning.fr)
  37   * @covers \mod_bigbluebuttonbn\external\get_bigbluebuttonbns_by_courses
  38   */
  39  class get_bigbluebuttons_by_courses_test extends \externallib_advanced_testcase {
  40      use testcase_helper_trait;
  41  
  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 mixed
  55       */
  56      protected function get_bigbluebuttons_by_courses(...$params) {
  57          $returnvalue = get_bigbluebuttonbns_by_courses::execute(...$params);
  58  
  59          return external_api::clean_returnvalue(get_bigbluebuttonbns_by_courses::execute_returns(), $returnvalue);
  60      }
  61  
  62      /**
  63       * Test execute API CALL with no instance
  64       */
  65      public function test_execute_no_instance() {
  66          $bbbactivities = $this->get_bigbluebuttons_by_courses([1234, 5678]);
  67  
  68          $this->assertIsArray($bbbactivities);
  69          $this->assertArrayHasKey('bigbluebuttonbns', $bbbactivities);
  70          $this->assertArrayHasKey('warnings', $bbbactivities);
  71          $this->assertEmpty($bbbactivities['bigbluebuttonbns']);
  72      }
  73  
  74      /**
  75       * Test execute API CALL without login
  76       */
  77      public function test_execute_without_login() {
  78          $this->resetAfterTest();
  79  
  80          $course = $this->getDataGenerator()->create_course();
  81          $record = $this->getDataGenerator()->create_module('bigbluebuttonbn', ['course' => $course->id]);
  82          $instance = instance::get_from_instanceid($record->id);
  83  
  84          $this->expectException(moodle_exception::class);
  85          $this->get_bigbluebuttons_by_courses($instance->get_cm_id());
  86      }
  87  
  88      /**
  89       * Test execute API CALL with invalid login
  90       */
  91      public function test_execute_with_invalid_login() {
  92          $this->resetAfterTest();
  93  
  94          $generator = $this->getDataGenerator();
  95          $course = $generator->create_course();
  96          $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
  97          $instance = instance::get_from_instanceid($record->id);
  98  
  99          $user = $generator->create_user();
 100          $this->setUser($user);
 101  
 102          $this->expectException(moodle_exception::class);
 103          $this->get_bigbluebuttons_by_courses($instance->get_cm_id());
 104      }
 105  
 106      /**
 107       * Test get bbbactivities
 108       */
 109      public function test_execute_with_valid_login() {
 110          $this->resetAfterTest(true);
 111          $generator = $this->getDataGenerator();
 112  
 113          // Create a user.
 114          $user = $generator->create_user();
 115  
 116          // Create courses to add the modules.
 117          $course1 = $generator->create_course();
 118          $course2 = $generator->create_course();
 119  
 120          $bbbs = [];
 121          // First bbb activity.
 122          $bbbs[] = $generator->create_module('bigbluebuttonbn', ['course' => $course1->id]);
 123  
 124          // Second bbb activity.
 125          $bbbs[] = $generator->create_module('bigbluebuttonbn', ['course' => $course2->id]);
 126  
 127          $generator->enrol_user($user->id, $course1->id, null, 'manual');
 128          $generator->enrol_user($user->id, $course2->id, null, 'manual');
 129          // Set to the user.
 130          self::setUser($user);
 131  
 132          // Call the external function passing course ids.
 133          $bbbactivities = $this->get_bigbluebuttons_by_courses([$course1->id, $course2->id]);
 134          $this->assert_same_bbb_activities($bbbs, $bbbactivities['bigbluebuttonbns']);
 135  
 136          // Call the external function without passing course id.
 137          $bbbactivities = $this->get_bigbluebuttons_by_courses();
 138          $this->assert_same_bbb_activities($bbbs, $bbbactivities['bigbluebuttonbns']);
 139  
 140          // Unenrol user from second course and alter expected bbb activity.
 141          $enrol = enrol_get_plugin('manual');
 142          $enrolinstances = enrol_get_instances($course2->id, true);
 143          foreach ($enrolinstances as $courseenrolinstance) {
 144              if ($courseenrolinstance->enrol == "manual") {
 145                  $instance2 = $courseenrolinstance;
 146                  break;
 147              }
 148          }
 149          $enrol->unenrol_user($instance2, $user->id);
 150  
 151          // Call the external function without passing course id.
 152          $bbbactivities = $this->get_bigbluebuttons_by_courses();
 153          $this->assertCount(1, $bbbactivities['bigbluebuttonbns']);
 154          $this->assertEquals($bbbs[0]->id, $bbbactivities['bigbluebuttonbns'][0]['id']);
 155  
 156          // Call for the second course we unenrolled the user from.
 157          $bbbactivities = $this->get_bigbluebuttons_by_courses([$course2->id]);
 158          $this->assertCount(0, $bbbactivities['bigbluebuttonbns']);
 159      }
 160  
 161      /**
 162       * Check if the two arrays containing the activities are the same.
 163       *
 164       * @param mixed $expected
 165       * @param mixed $actual
 166       */
 167      protected function assert_same_bbb_activities($expected, $actual) {
 168          $this->assertCount(count($expected), $actual);
 169          $getids = function($bbb) {
 170              return is_array($bbb) ? $bbb['id'] : $bbb->id;
 171          };
 172          $expectedids = array_map($getids, $expected);
 173          $actualid = array_map($getids, $actual);
 174          sort($expectedids);
 175          sort($actualid);
 176          $this->assertEquals($expectedids, $actualid);
 177      }
 178  }
 179