Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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   * BBB Library tests class.
  19   *
  20   * @package   mod_bigbluebuttonbn
  21   * @copyright 2018 - present, Blindside Networks Inc
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @author    Laurent David (laurent@call-learning.fr)
  24   */
  25  
  26  namespace mod_bigbluebuttonbn\local\helpers;
  27  
  28  use mod_bigbluebuttonbn\instance;
  29  use mod_bigbluebuttonbn\test\testcase_helper_trait;
  30  
  31  /**
  32   * BBB Library tests class.
  33   *
  34   * @package   mod_bigbluebuttonbn
  35   * @copyright 2018 - 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\local\helpers\mod_helper
  39   * @coversDefaultClass \mod_bigbluebuttonbn\local\helpers\mod_helper
  40   */
  41  class mod_helper_trait_test extends \advanced_testcase {
  42      use testcase_helper_trait;
  43  
  44      /**
  45       * Presave test
  46       */
  47      public function test_process_pre_save() {
  48          $this->resetAfterTest();
  49          list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
  50          $bbformdata = $this->get_form_data_from_instance($bbactivity);
  51          $bbformdata->participants = '<p>this -&gt; &quot;</p>\n';
  52          $bbformdata->timemodified = time();
  53          mod_helper::process_pre_save($bbformdata);
  54          $this->assertTrue($bbformdata->timemodified != 0);
  55          $this->assertEquals('<p>this -> "</p>\n', $bbformdata->participants);
  56      }
  57  
  58      /**
  59       * Presave instance
  60       */
  61      public function test_process_pre_save_instance() {
  62          $this->resetAfterTest();
  63          list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
  64          $bbformdata = $this->get_form_data_from_instance($bbactivity);
  65          $bbformdata->instance = 0;
  66          $bbformdata->timemodified = time();
  67          mod_helper::process_pre_save($bbformdata);
  68          $this->assertTrue($bbformdata->timemodified == 0);
  69      }
  70  
  71      /**
  72       * Presave checkboxes
  73       */
  74      public function test_process_pre_save_checkboxes() {
  75          $this->resetAfterTest();
  76          list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
  77          $bbformdata = $this->get_form_data_from_instance($bbactivity);
  78          unset($bbformdata->wait);
  79          unset($bbformdata->recordallfromstart);
  80          mod_helper::process_pre_save($bbformdata);
  81          $this->assertTrue(isset($bbformdata->wait));
  82          $this->assertTrue(isset($bbformdata->recordallfromstart));
  83      }
  84  
  85      /**
  86       * Presave common
  87       */
  88      public function test_process_pre_save_common() {
  89          global $CFG;
  90          $this->resetAfterTest();
  91  
  92          list($bbactivitycontext, $bbactivitycm, $bbactivity) =
  93              $this->create_instance(null, ['type' => instance::TYPE_RECORDING_ONLY]);
  94          $bbformdata = $this->get_form_data_from_instance($bbactivity);
  95  
  96          $bbformdata->groupmode = '1';
  97          mod_helper::process_pre_save($bbformdata);
  98          $this->assertEquals(0, $bbformdata->groupmode);
  99      }
 100  
 101      /**
 102       * Post save
 103       */
 104      public function test_process_post_save() {
 105          $this->resetAfterTest();
 106  
 107          $generator = $this->getDataGenerator();
 108          list($bbactivitycontext, $bbactivitycm, $bbactivity) =
 109              $this->create_instance(null, ['type' => instance::TYPE_RECORDING_ONLY]);
 110          // Reset some static caches used by this test after enabling the plugin.
 111          get_module_types_names(false, true);
 112  
 113          $bbformdata = $this->get_form_data_from_instance($bbactivity);
 114  
 115          // Enrol users in a course so he will receive the message.
 116          $teacher = $generator->create_user(['role' => 'editingteacher']);
 117          $generator->enrol_user($teacher->id, $this->get_course()->id);
 118  
 119          // Mark the form to trigger notification.
 120          $bbformdata->coursecontentnotification = true;
 121          $bbformdata->update = false;
 122          $messagesink = $this->redirectMessages();
 123          mod_helper::process_post_save($bbformdata);
 124          edit_module_post_actions($bbformdata, $this->course);
 125          // Now run cron.
 126          ob_start();
 127          $this->runAdhocTasks();
 128          ob_get_clean(); // Suppress output as it can fail the test.
 129          $this->assertEquals(1, $messagesink->count());
 130          $firstmessage = $messagesink->get_messages()[0];
 131          $this->assertStringContainsString('is new in', $firstmessage->smallmessage);
 132      }
 133  
 134      /**
 135       * Post save notification
 136       */
 137      public function test_process_post_save_with_add() {
 138          $this->resetAfterTest();
 139  
 140          $generator = $this->getDataGenerator();
 141          list($bbactivitycontext, $bbactivitycm, $bbactivity) =
 142              $this->create_instance(null, ['type' => instance::TYPE_RECORDING_ONLY]);
 143          // Reset some static caches used by this test after enabling the plugin.
 144          get_module_types_names(false, true);
 145  
 146          $bbformdata = $this->get_form_data_from_instance($bbactivity);
 147  
 148          $bbformdata->update = false;
 149          $messagesink = $this->redirectMessages();
 150          // Enrol users in a course so he will receive the message.
 151          $teacher = $generator->create_user(['role' => 'editingteacher']);
 152          $generator->enrol_user($teacher->id, $this->get_course()->id);
 153          $bbformdata->coursecontentnotification = true;
 154          mod_helper::process_post_save($bbformdata);
 155          edit_module_post_actions($bbformdata, $this->course);
 156          // Now run cron.
 157          ob_start();
 158          $this->runAdhocTasks();
 159          ob_get_clean(); // Suppress output as it can fail the test.
 160          $this->assertEquals(1, $messagesink->count());
 161          $firstmessage = $messagesink->get_messages()[0];
 162          $this->assertStringContainsString('is new in', $firstmessage->smallmessage);
 163      }
 164  
 165      /**
 166       * Post save
 167       *
 168       * There was an issue when both the opening time and completion were set
 169       * and the form was saved twice.
 170       */
 171      public function test_process_post_save_twice_with_completion() {
 172          $this->resetAfterTest();
 173  
 174          $generator = $this->getDataGenerator();
 175          list($bbactivitycontext, $bbactivitycm, $bbactivity) =
 176              $this->create_instance(null, ['type' => instance::TYPE_RECORDING_ONLY]);
 177          // Reset some static caches used by this test after enabling the plugin.
 178          get_module_types_names(false, true);
 179  
 180          $bbformdata = $this->get_form_data_from_instance($bbactivity);
 181          $bbformdata->completionunlocked = 0;
 182          $bbformdata->completion = COMPLETION_AGGREGATION_ANY;
 183          $bbformdata->completionview = COMPLETION_VIEWED;
 184          $bbformdata->completionexpected = time();
 185          $bbformdata->openingtime = time() - 1000;
 186          $bbformdata->closing = time() + 1000;
 187          // Enrol users in a course so he will receive the message.
 188          $teacher = $generator->create_user();
 189          $generator->enrol_user($teacher->id, $this->get_course()->id, 'editingteacher');
 190          $this->setUser($teacher);
 191          // Mark the form to trigger notification.
 192          $bbformdata->coursecontentnotification = true;
 193          $bbformdata->update = false;
 194          $messagesink = $this->redirectMessages();
 195          mod_helper::process_post_save($bbformdata);
 196          edit_module_post_actions($bbformdata, $this->course);
 197          // Now run cron.
 198          ob_start();
 199          $this->runAdhocTasks();
 200          ob_get_clean(); // Suppress output as it can fail the test.
 201          $this->assertEquals(1, $messagesink->count());
 202          $firstmessage = $messagesink->get_messages()[0];
 203          $this->assertStringContainsString('is new in', $firstmessage->smallmessage);
 204          $messagesink->clear();
 205          // Do it a again, so we check we still have one event.
 206          mod_helper::process_post_save($bbformdata);
 207          // Mark the form to trigger notification.
 208          $bbformdata->update = true;
 209          edit_module_post_actions($bbformdata, $this->course);
 210          // Now run cron.
 211          ob_start();
 212          $this->runAdhocTasks();
 213          ob_get_clean(); // Suppress output as it can fail the test.
 214          $this->assertEquals(1, $messagesink->count());
 215          $firstmessage = $messagesink->get_messages()[0];
 216          $this->assertStringContainsString('has been changed', $firstmessage->smallmessage);
 217      }
 218  }