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.
   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   * Privacy provider tests.
  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    Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
  24   */
  25  namespace mod_bigbluebuttonbn\privacy;
  26  
  27  use context_module;
  28  use core_privacy\local\metadata\collection;
  29  use core_privacy\local\request\approved_contextlist;
  30  use core_privacy\local\request\approved_userlist;
  31  use core_privacy\local\request\userlist;
  32  
  33  /**
  34   * Privacy provider tests class.
  35   *
  36   * @package   mod_bigbluebuttonbn
  37   * @copyright 2018 - present, Blindside Networks Inc
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   * @author    Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
  40   * @covers \mod_bigbluebuttonbn\privacy\provider
  41   */
  42  class provider_test extends \core_privacy\tests\provider_testcase {
  43  
  44      /**
  45       * Test for provider::get_metadata().
  46       */
  47      public function test_get_metadata() {
  48          $this->resetAfterTest(true);
  49  
  50          $collection = new collection('mod_bigbluebuttonbn');
  51          $newcollection = provider::get_metadata($collection);
  52          $itemcollection = $newcollection->get_collection();
  53          $this->assertCount(4, $itemcollection);
  54  
  55          $instancetable = array_shift($itemcollection);
  56          $this->assertEquals('bigbluebuttonbn', $instancetable->get_name());
  57  
  58          $instancelogstable = array_shift($itemcollection);
  59          $this->assertEquals('bigbluebuttonbn_logs', $instancelogstable->get_name());
  60  
  61          $recordings = array_shift($itemcollection);
  62          $this->assertEquals('bigbluebuttonbn_recordings', $recordings->get_name());
  63  
  64          $bigbluebuttonserver = array_shift($itemcollection);
  65          $this->assertEquals('bigbluebutton', $bigbluebuttonserver->get_name());
  66  
  67          $privacyfields = $instancetable->get_privacy_fields();
  68          $this->assertArrayHasKey('participants', $privacyfields);
  69          $this->assertEquals('privacy:metadata:bigbluebuttonbn', $instancetable->get_summary());
  70  
  71          $privacyfields = $instancelogstable->get_privacy_fields();
  72          $this->assertArrayHasKey('userid', $privacyfields);
  73          $this->assertArrayHasKey('timecreated', $privacyfields);
  74          $this->assertArrayHasKey('meetingid', $privacyfields);
  75          $this->assertArrayHasKey('log', $privacyfields);
  76          $this->assertArrayHasKey('meta', $privacyfields);
  77          $this->assertEquals('privacy:metadata:bigbluebuttonbn_logs', $instancelogstable->get_summary());
  78  
  79          $privacyfields = $bigbluebuttonserver->get_privacy_fields();
  80          $this->assertArrayHasKey('userid', $privacyfields);
  81          $this->assertArrayHasKey('fullname', $privacyfields);
  82          $this->assertEquals('privacy:metadata:bigbluebutton', $bigbluebuttonserver->get_summary());
  83      }
  84  
  85      /**
  86       * Test for provider::get_contexts_for_userid().
  87       */
  88      public function test_get_contexts_for_userid() {
  89          $this->resetAfterTest();
  90  
  91          $e = $this->get_bigbluebuttonbn_environemnt();
  92          $bigbluebuttonbn = $e['instance'];
  93          $course = $e['course'];
  94  
  95          // Another bigbluebuttonbn activity that has no user activity.
  96          $this->getDataGenerator()->create_module('bigbluebuttonbn', ['course' => $course]);
  97  
  98          // Create a user which will make a submission.
  99          $user = $this->getDataGenerator()->create_user();
 100  
 101          $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn')
 102              ->create_log(['bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user->id]);
 103  
 104          // Check the contexts supplied are correct.
 105          $contextlist = provider::get_contexts_for_userid($user->id);
 106          $this->assertCount(1, $contextlist);
 107  
 108          $contextformodule = $contextlist->current();
 109          $cmcontext = context_module::instance($bigbluebuttonbn->cmid);
 110          $this->assertEquals($cmcontext->id, $contextformodule->id);
 111      }
 112  
 113      /**
 114       * Test for provider::export_user_data().
 115       */
 116      public function test_export_for_context_logs() {
 117          $this->resetAfterTest();
 118  
 119          $e = $this->get_bigbluebuttonbn_environemnt();
 120          $bigbluebuttonbn = $e['instance'];
 121  
 122          // Create users which will make submissions.
 123          $user1 = $this->getDataGenerator()->create_user();
 124          $user2 = $this->getDataGenerator()->create_user();
 125  
 126          $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn')
 127              ->create_log(['bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user1->id]);
 128          $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn')
 129              ->create_log(['bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user1->id]);
 130          $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn')
 131              ->create_log(['bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user2->id]);
 132  
 133          // Export all of the data for the context for user 1.
 134          $cmcontext = context_module::instance($bigbluebuttonbn->cmid);
 135          $this->export_context_data_for_user($user1->id, $cmcontext, 'mod_bigbluebuttonbn');
 136          $writer = \core_privacy\local\request\writer::with_context($cmcontext);
 137  
 138          $this->assertTrue($writer->has_any_data());
 139  
 140          $data = $writer->get_data();
 141          $this->assertCount(2, $data->logs);
 142      }
 143  
 144      /**
 145       * Test that only users with relevant contexts are fetched.
 146       */
 147      public function test_get_users_in_context() {
 148          // For backward compatibility with old versions of Moodle.
 149          if (!class_exists('\core_privacy\local\request\userlist')) {
 150              return;
 151          }
 152  
 153          $this->resetAfterTest();
 154  
 155          $e = $this->get_bigbluebuttonbn_environemnt();
 156          $bigbluebuttonbn = $e['instance'];
 157  
 158          // Users which will make submissions.
 159          $user1 = $e['users'][0];
 160          $user2 = $e['users'][1];
 161  
 162          $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn')
 163              ->create_log(['bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user1->id]);
 164          $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn')
 165              ->create_log(['bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user1->id]);
 166          $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn')
 167              ->create_log(['bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user2->id]);
 168  
 169          // Export all of the data for the context for user 1.
 170          $cmcontext = context_module::instance($bigbluebuttonbn->cmid);
 171  
 172          $userlist = new userlist($cmcontext, 'mod_bigbluebuttonbn');
 173          provider::get_users_in_context($userlist);
 174  
 175          // Ensure correct users are found in relevant contexts.
 176          $this->assertCount(2, $userlist);
 177          $expected = [intval($user1->id), intval($user2->id)];
 178          $actual = $userlist->get_userids();
 179          $this->assertEquals(sort($expected), sort($actual));
 180      }
 181  
 182      /**
 183       * Test for provider::delete_data_for_all_users_in_context().
 184       */
 185      public function test_delete_data_for_all_users_in_context() {
 186          global $DB;
 187  
 188          $this->resetAfterTest();
 189  
 190          $e = $this->get_bigbluebuttonbn_environemnt();
 191  
 192          // Before deletion, we should have 3 responses, 1 Add event and 2 Create events (1 per user).
 193          $count = $DB->count_records('bigbluebuttonbn_logs', ['bigbluebuttonbnid' => $e['instance']->id]);
 194          $this->assertEquals(3, $count);
 195  
 196          // Delete data based on context.
 197          $cmcontext = context_module::instance($e['instance']->cmid);
 198          provider::delete_data_for_all_users_in_context($cmcontext);
 199  
 200          // After deletion, the bigbluebuttonbn logs for that activity should have been deleted.
 201          $count = $DB->count_records('bigbluebuttonbn_logs', ['bigbluebuttonbnid' => $e['instance']->id]);
 202          $this->assertEquals(0, $count);
 203      }
 204  
 205      /**
 206       * Test for provider::delete_data_for_user().
 207       */
 208      public function test_delete_data_for_user() {
 209          global $DB;
 210  
 211          $this->resetAfterTest();
 212  
 213          $e = $this->get_bigbluebuttonbn_environemnt();
 214  
 215          // Delete data for the first user.
 216          $context = \context_module::instance($e['instance']->cmid);
 217          $contextlist = new approved_contextlist($e['users'][0], 'bigbluebuttonbn',
 218              [$context->id]);
 219          provider::delete_data_for_user($contextlist);
 220  
 221          // After deletion the bigbluebuttonbn logs for the first user should have been deleted.
 222          $count = $DB->count_records('bigbluebuttonbn_logs',
 223              ['bigbluebuttonbnid' => $e['instance']->id, 'userid' => $e['users'][0]->id]);
 224          $this->assertEquals(0, $count);
 225  
 226          // Check the logs for the other user is still there.
 227          $count = $DB->count_records('bigbluebuttonbn_logs',
 228              ['bigbluebuttonbnid' => $e['instance']->id, 'userid' => $e['users'][1]->id]);
 229          $this->assertEquals(1, $count);
 230      }
 231  
 232      /**
 233       * Test that data for users in approved userlist is deleted.
 234       */
 235      public function test_delete_data_for_users() {
 236          global $DB;
 237  
 238          // For backward compatibility with old versions of Moodle.
 239          if (!class_exists('\core_privacy\local\request\approved_userlist')) {
 240              return;
 241          }
 242  
 243          $this->resetAfterTest();
 244  
 245          $e = $this->get_bigbluebuttonbn_environemnt();
 246  
 247          // Delete user 1 and 2 data from chat 1 context only.
 248          $context = \context_module::instance($e['instance']->cmid);
 249          $approveduserids = [$e['users'][0]->id];
 250          $approvedlist = new approved_userlist($context, 'mod_bigbluebuttonbn', $approveduserids);
 251          provider::delete_data_for_users($approvedlist);
 252  
 253          // After deletion the bigbluebuttonbn logs for the first user should have been deleted.
 254          $count = $DB->count_records('bigbluebuttonbn_logs',
 255              ['bigbluebuttonbnid' => $e['instance']->id, 'userid' => $e['users'][0]->id]);
 256          $this->assertEquals(0, $count);
 257  
 258          // Check the logs for the other user is still there.
 259          $count = $DB->count_records('bigbluebuttonbn_logs',
 260              ['bigbluebuttonbnid' => $e['instance']->id, 'userid' => $e['users'][1]->id]);
 261          $this->assertEquals(1, $count);
 262      }
 263  
 264      /**
 265       * Prepares the environment for testing.
 266       *
 267       * @return array $e
 268       */
 269      protected function get_bigbluebuttonbn_environemnt() {
 270          $e = [];
 271  
 272          // Create a course.
 273          $e['course'] = $this->getDataGenerator()->create_course();
 274  
 275          // Create a bigbluebuttonbn instance.
 276          $e['instance'] = $this->getDataGenerator()->create_module('bigbluebuttonbn',
 277              ['course' => $e['course']->id]);
 278  
 279          // Create users that will use the bigbluebuttonbn instance.
 280          $e['users'][] = $this->getDataGenerator()->create_user();
 281          $e['users'][] = $this->getDataGenerator()->create_user();
 282  
 283          // Create the bigbluebuttonbn logs.
 284          $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn')
 285              ->create_log(['bigbluebuttonbnid' => $e['instance']->id, 'userid' => $e['users'][0]->id]);
 286          $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn')
 287              ->create_log(['bigbluebuttonbnid' => $e['instance']->id, 'userid' => $e['users'][1]->id]);
 288  
 289          return $e;
 290      }
 291  }