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 400 and 401] [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  /**
  18   * Behat custom steps and configuration for mod_bigbluebuttonbn.
  19   *
  20   * @package   mod_bigbluebuttonbn
  21   * @category  test
  22   * @copyright 2021 Andrew Lyons <andrew@nicols.co.uk>
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once (__DIR__ . '/../../../../lib/behat/behat_base.php');
  27  
  28  use Behat\Behat\Hook\Scope\BeforeScenarioScope;
  29  use Behat\Gherkin\Node\TableNode;
  30  use mod_bigbluebuttonbn\instance;
  31  use Moodle\BehatExtension\Exception\SkippedException;
  32  
  33  /**
  34   * Behat custom steps and configuration for mod_bigbluebuttonbn.
  35   *
  36   * @package   mod_bigbluebuttonbn
  37   * @copyright 2021 Andrew Lyons <andrew@nicols.co.uk>
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class behat_mod_bigbluebuttonbn extends behat_base {
  41  
  42      /**
  43       * BeforeScenario hook to reset the remote testpoint.
  44       *
  45       * @BeforeScenario @mod_bigbluebuttonbn
  46       *
  47       * @param BeforeScenarioScope $scope
  48       */
  49      public function before_scenario(BeforeScenarioScope $scope) {
  50          if (defined('TEST_MOD_BIGBLUEBUTTONBN_MOCK_SERVER')) {
  51              $this->send_mock_request('backoffice/reset');
  52          }
  53      }
  54  
  55      /**
  56       * Accept dpa and enable bigbluebuttonbn plugin.
  57       *
  58       * @When /^I accept dpa and enable bigbluebuttonbn plugin$/
  59       */
  60      public function i_accept_dpa_and_enable_bigbluebuttonbn_plugin(): void {
  61          set_config('bigbluebuttonbn_default_dpa_accepted', true);
  62          $this->execute('behat_general::i_enable_plugin', ['bigbluebuttonbn', 'mod']);
  63      }
  64  
  65      /**
  66       * Check that the TEST_MOD_BIGBLUEBUTTONBN_MOCK_SERVER is defined, so we can connect to the mock server.
  67       *
  68       * @Given /^a BigBlueButton mock server is configured$/
  69       */
  70      public function mock_is_configured(): void {
  71          if (!defined('TEST_MOD_BIGBLUEBUTTONBN_MOCK_SERVER')) {
  72              throw new SkippedException(
  73                  'The TEST_MOD_BIGBLUEBUTTONBN_MOCK_SERVER constant must be defined to run mod_bigbluebuttonbn tests'
  74              );
  75          }
  76  
  77          set_config('bigbluebuttonbn_server_url', TEST_MOD_BIGBLUEBUTTONBN_MOCK_SERVER);
  78      }
  79  
  80      /**
  81       * Return the list of exact named selectors.
  82       *
  83       * @return array
  84       */
  85      public static function get_exact_named_selectors(): array {
  86          return [
  87              new behat_component_named_selector('Meeting field', [
  88                  <<<XPATH
  89      .//*[@data-identifier=%locator%]
  90  XPATH
  91              ], false),
  92          ];
  93      }
  94  
  95      /**
  96       * Retrieve the mock server URL from the TEST_MOD_BIGBLUEBUTTONBN_MOCK_SERVER definition
  97       *
  98       * @param string $endpoint
  99       * @param array $params
 100       * @return moodle_url
 101       */
 102      public static function get_mocked_server_url(string $endpoint = '', array $params = []): moodle_url {
 103          return new moodle_url(TEST_MOD_BIGBLUEBUTTONBN_MOCK_SERVER . '/' . $endpoint, $params);
 104      }
 105  
 106      /**
 107       * Send a query to the mock server
 108       *
 109       * @param string $endpoint
 110       * @param array $params
 111       */
 112      protected function send_mock_request(string $endpoint, array $params = []): void {
 113          $url = $this->get_mocked_server_url($endpoint, $params);
 114  
 115          $curl = new \curl();
 116          $curl->get($url->out_omit_querystring(), $url->params());
 117      }
 118  
 119      /**
 120       * Convert page names to URLs for steps like 'When I am on the "[page name]" page'.
 121       *
 122       * Recognised page names are:
 123       * | None so far!      |                                                              |
 124       *
 125       * @param string $page name of the page, with the component name removed e.g. 'Admin notification'.
 126       * @return moodle_url the corresponding URL.
 127       * @throws Exception with a meaningful error message if the specified page cannot be found.
 128       */
 129      protected function resolve_page_url(string $page): moodle_url {
 130          throw new Exception("Unrecognised page type '{$page}'.");
 131      }
 132  
 133      /**
 134       * Convert page names to URLs for steps like 'When I am on the "[identifier]" "[page type]" page'.
 135       *
 136       * Recognised page names are:
 137       * | pagetype | name meaning     | description                    |
 138       * | Index    | BBB Course Index | The bbb index page (index.php) |
 139       *
 140       * @param string $type identifies which type of page this is, e.g. 'Indez'.
 141       * @param string $identifier identifies the particular page, e.g. 'Mathematics 101'.
 142       * @return moodle_url the corresponding URL.
 143       * @throws Exception with a meaningful error message if the specified page cannot be found.
 144       */
 145      protected function resolve_page_instance_url(string $type, string $identifier): moodle_url {
 146          switch ($type) {
 147              case 'Index':
 148                  $this->get_course_id($identifier);
 149                  return new moodle_url('/mod/bigbluebuttonbn/index.php', [
 150                      'id' => $this->get_course_id($identifier),
 151                  ]);
 152              case 'BigblueButtonBN Guest':
 153                  $cm = $this->get_cm_by_activity_name('bigbluebuttonbn', $identifier);
 154                  $instance = instance::get_from_cmid($cm->id);
 155                  $url = $instance->get_guest_access_url();
 156                  // We have to make sure we set the password. It makes it then easy to submit the form with the right password.
 157                  $url->param('password', $instance->get_guest_access_password());
 158                  return $url;
 159              default:
 160                  throw new Exception("Unrecognised page type '{$type}'.");
 161          }
 162      }
 163  
 164      /**
 165       * Get course id from its identifier (shortname or fullname or idnumber)
 166       *
 167       * @param string $identifier
 168       * @return int
 169       */
 170      protected function get_course_id(string $identifier): int {
 171          global $DB;
 172  
 173          return $DB->get_field_select(
 174              'course',
 175              'id',
 176              "shortname = :shortname OR fullname = :fullname OR idnumber = :idnumber",
 177              [
 178                  'shortname' => $identifier,
 179                  'fullname' => $identifier,
 180                  'idnumber' => $identifier,
 181              ],
 182              MUST_EXIST
 183          );
 184      }
 185  
 186      /**
 187       * Trigger a recording ready notification on BBB side
 188       *
 189       * @Given the BigBlueButtonBN server has sent recording ready notifications
 190       */
 191      public function trigger_recording_ready_notification(): void {
 192          $this->send_mock_request('backoffice/sendRecordingReadyNotifications', [
 193                  'secret' => \mod_bigbluebuttonbn\local\config::DEFAULT_SHARED_SECRET,
 194              ]
 195          );
 196      }
 197  
 198      /**
 199       * Trigger a meeting event on BBB side
 200       *
 201       * @Given /^the BigBlueButtonBN server has received the following events from user "(?P<element_string>(?:[^"]|\\")*)":$/
 202       * @param string $username
 203       * @param TableNode $data
 204       */
 205      public function trigger_meeting_event(string $username, TableNode $data): void {
 206          global $DB;
 207          $user = core_user::get_user_by_username($username);
 208          $rows = $data->getHash();
 209          foreach ($rows as $elementdata) {
 210              $instanceid = $DB->get_field('bigbluebuttonbn', 'id', [
 211                  'name' => $elementdata['instancename'],
 212              ]);
 213              $instance = \mod_bigbluebuttonbn\instance::get_from_instanceid($instanceid);
 214              $this->send_mock_request('backoffice/addMeetingEvent', [
 215                      'secret' => \mod_bigbluebuttonbn\local\config::DEFAULT_SHARED_SECRET,
 216                      'meetingID' => $instance->get_meeting_id(),
 217                      'attendeeID' => $user->id,
 218                      'attendeeName' => fullname($user),
 219                      'eventType' => $elementdata['eventtype'],
 220                      'eventData' => $elementdata['eventdata'] ?? '',
 221                  ]
 222              );
 223          }
 224      }
 225      /**
 226       * Send all events received for this meeting back to moodle
 227       *
 228       * @Given /^the BigBlueButtonBN activity "(?P<element_string>(?:[^"]|\\")*)" has sent recording all its events$/
 229       * @param string $instancename
 230       */
 231      public function trigger_all_events(string $instancename): void {
 232          global $DB;
 233  
 234          $instanceid = $DB->get_field('bigbluebuttonbn', 'id', [
 235              'name' => $instancename,
 236          ]);
 237          $instance = \mod_bigbluebuttonbn\instance::get_from_instanceid($instanceid);
 238          $this->send_mock_request('backoffice/sendAllEvents', [
 239                  'meetingID' => $instance->get_meeting_id(),
 240                  'sendQuery' => true
 241              ]
 242          );
 243      }
 244  }