Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

   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   * Data provider tests.
  19   *
  20   * @package    tool_messageinbound
  21   * @category   test
  22   * @copyright  2018 Frédéric Massart
  23   * @author     Frédéric Massart <fred@branchup.tech>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  global $CFG;
  29  
  30  use core_privacy\tests\provider_testcase;
  31  use core_privacy\local\request\approved_contextlist;
  32  use core_privacy\local\request\approved_userlist;
  33  use core_privacy\local\request\transform;
  34  use core_privacy\local\request\userlist;
  35  use core_privacy\local\request\writer;
  36  use tool_messageinbound\privacy\provider;
  37  
  38  /**
  39   * Data provider testcase class.
  40   *
  41   * @package    tool_messageinbound
  42   * @category   test
  43   * @copyright  2018 Frédéric Massart
  44   * @author     Frédéric Massart <fred@branchup.tech>
  45   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   */
  47  class tool_messageinbound_privacy_testcase extends provider_testcase {
  48  
  49      public function setUp(): void {
  50          global $CFG;
  51          $this->resetAfterTest();
  52  
  53          // Pretend the system is enabled.
  54          $CFG->messageinbound_enabled = true;
  55          $CFG->messageinbound_mailbox = 'mailbox';
  56          $CFG->messageinbound_domain = 'example.com';
  57      }
  58  
  59      public function test_get_contexts_for_userid() {
  60          $dg = $this->getDataGenerator();
  61          $u1 = $dg->create_user();
  62          $u2 = $dg->create_user();
  63          $u1ctx = context_user::instance($u1->id);
  64          $u2ctx = context_user::instance($u2->id);
  65  
  66          $contexts = provider::get_contexts_for_userid($u1->id)->get_contexts();
  67          $this->assertCount(1, $contexts);
  68          $this->assertEquals($u1ctx->id, $contexts[0]->id);
  69  
  70          $contexts = provider::get_contexts_for_userid($u2->id)->get_contexts();
  71          $this->assertCount(1, $contexts);
  72          $this->assertEquals($u2ctx->id, $contexts[0]->id);
  73      }
  74  
  75      /**
  76       * Test for provider::test_get_users_in_context().
  77       */
  78      public function test_get_users_in_context() {
  79          $component = 'tool_messageinbound';
  80          $dg = $this->getDataGenerator();
  81          $u1 = $dg->create_user();
  82          $u2 = $dg->create_user();
  83          $u3 = $dg->create_user();
  84          $u1ctx = context_user::instance($u1->id);
  85          $u2ctx = context_user::instance($u2->id);
  86          $u3ctx = context_user::instance($u3->id);
  87  
  88          $addressmanager = new \core\message\inbound\address_manager();
  89          $addressmanager->set_handler('\tool_messageinbound\message\inbound\invalid_recipient_handler');
  90          $addressmanager->set_data(123);
  91  
  92          // Create a user key for user 1.
  93          $addressmanager->generate($u1->id);
  94  
  95          // Create a messagelist for user 2.
  96          $this->create_messagelist(['userid' => $u2->id, 'address' => 'u2@example1.com']);
  97  
  98          $userlist1 = new userlist($u1ctx, $component);
  99          provider::get_users_in_context($userlist1);
 100          $userlist2 = new userlist($u2ctx, $component);
 101          provider::get_users_in_context($userlist2);
 102          $userlist3 = new userlist($u3ctx, $component);
 103          provider::get_users_in_context($userlist3);
 104  
 105          // Ensure user 1 is found from userkey.
 106          $userids = $userlist1->get_userids();
 107          $this->assertCount(1, $userids);
 108          $this->assertEquals($u1->id, $userids[0]);
 109  
 110          // Ensure user 2 is found from messagelist.
 111          $userids = $userlist2->get_userids();
 112          $this->assertCount(1, $userids);
 113          $this->assertEquals($u2->id, $userids[0]);
 114  
 115          // User 3 has neither, so should not be found.
 116          $userids = $userlist3->get_userids();
 117          $this->assertCount(0, $userids);
 118      }
 119  
 120      public function test_delete_data_for_user() {
 121          global $DB;
 122          $dg = $this->getDataGenerator();
 123          $u1 = $dg->create_user();
 124          $u2 = $dg->create_user();
 125          $u1ctx = context_user::instance($u1->id);
 126          $u2ctx = context_user::instance($u2->id);
 127  
 128          $addressmanager = new \core\message\inbound\address_manager();
 129          $addressmanager->set_handler('\tool_messageinbound\message\inbound\invalid_recipient_handler');
 130          $addressmanager->set_data(123);
 131  
 132          // Create a user key for both users.
 133          $addressmanager->generate($u1->id);
 134          $addressmanager->generate($u2->id);
 135  
 136          // Create a messagelist for both users.
 137          $this->create_messagelist(['userid' => $u1->id]);
 138          $this->create_messagelist(['userid' => $u2->id]);
 139  
 140          $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u1->id, 'script' => 'messageinbound_handler']));
 141          $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u2->id, 'script' => 'messageinbound_handler']));
 142          $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u1->id]));
 143          $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u2->id]));
 144  
 145          // Passing another user's context does not do anything.
 146          provider::delete_data_for_user(new approved_contextlist($u1, 'tool_messageinbound', [$u2ctx->id]));
 147          $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u1->id, 'script' => 'messageinbound_handler']));
 148          $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u2->id, 'script' => 'messageinbound_handler']));
 149          $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u1->id]));
 150          $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u2->id]));
 151  
 152          // Deleting user 1.
 153          provider::delete_data_for_user(new approved_contextlist($u1, 'tool_messageinbound', [$u1ctx->id]));
 154          $this->assertFalse($DB->record_exists('user_private_key', ['userid' => $u1->id, 'script' => 'messageinbound_handler']));
 155          $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u2->id, 'script' => 'messageinbound_handler']));
 156          $this->assertFalse($DB->record_exists('messageinbound_messagelist', ['userid' => $u1->id]));
 157          $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u2->id]));
 158      }
 159  
 160      /**
 161       * Test for provider::test_delete_data_for_users().
 162       */
 163      public function test_delete_data_for_users() {
 164          global $DB;
 165          $component = 'tool_messageinbound';
 166          $dg = $this->getDataGenerator();
 167          $u1 = $dg->create_user();
 168          $u2 = $dg->create_user();
 169          $u1ctx = context_user::instance($u1->id);
 170          $u2ctx = context_user::instance($u2->id);
 171  
 172          $addressmanager = new \core\message\inbound\address_manager();
 173          $addressmanager->set_handler('\tool_messageinbound\message\inbound\invalid_recipient_handler');
 174          $addressmanager->set_data(123);
 175  
 176          // Create a user key for both users.
 177          $addressmanager->generate($u1->id);
 178          $addressmanager->generate($u2->id);
 179  
 180          // Create a messagelist for both users.
 181          $this->create_messagelist(['userid' => $u1->id]);
 182          $this->create_messagelist(['userid' => $u2->id]);
 183  
 184          // Ensure data exists for both users.
 185          $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u1->id, 'script' => 'messageinbound_handler']));
 186          $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u2->id, 'script' => 'messageinbound_handler']));
 187          $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u1->id]));
 188          $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u2->id]));
 189  
 190          // Ensure passing another user's ID does not do anything.
 191          $approveduserids = [$u2->id];
 192          $approvedlist = new approved_userlist($u1ctx, $component, $approveduserids);
 193          provider::delete_data_for_users($approvedlist);
 194  
 195          $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u1->id, 'script' => 'messageinbound_handler']));
 196          $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u2->id, 'script' => 'messageinbound_handler']));
 197          $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u1->id]));
 198          $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u2->id]));
 199  
 200          // Delete u1's data.
 201          $approveduserids = [$u1->id];
 202          $approvedlist = new approved_userlist($u1ctx, $component, $approveduserids);
 203          provider::delete_data_for_users($approvedlist);
 204  
 205          // Confirm only u1's data is deleted.
 206          $this->assertFalse($DB->record_exists('user_private_key', ['userid' => $u1->id, 'script' => 'messageinbound_handler']));
 207          $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u2->id, 'script' => 'messageinbound_handler']));
 208          $this->assertFalse($DB->record_exists('messageinbound_messagelist', ['userid' => $u1->id]));
 209          $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u2->id]));
 210      }
 211  
 212      public function test_delete_data_for_all_users_in_context() {
 213          global $DB;
 214          $dg = $this->getDataGenerator();
 215          $u1 = $dg->create_user();
 216          $u2 = $dg->create_user();
 217          $u1ctx = context_user::instance($u1->id);
 218          $u2ctx = context_user::instance($u2->id);
 219  
 220          $addressmanager = new \core\message\inbound\address_manager();
 221          $addressmanager->set_handler('\tool_messageinbound\message\inbound\invalid_recipient_handler');
 222          $addressmanager->set_data(123);
 223  
 224          // Create a user key for both users.
 225          $addressmanager->generate($u1->id);
 226          $addressmanager->generate($u2->id);
 227  
 228          // Create a messagelist for both users.
 229          $this->create_messagelist(['userid' => $u1->id]);
 230          $this->create_messagelist(['userid' => $u2->id]);
 231  
 232          $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u1->id, 'script' => 'messageinbound_handler']));
 233          $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u2->id, 'script' => 'messageinbound_handler']));
 234          $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u1->id]));
 235          $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u2->id]));
 236  
 237          // Deleting user 1.
 238          provider::delete_data_for_all_users_in_context($u1ctx);
 239          $this->assertFalse($DB->record_exists('user_private_key', ['userid' => $u1->id, 'script' => 'messageinbound_handler']));
 240          $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u2->id, 'script' => 'messageinbound_handler']));
 241          $this->assertFalse($DB->record_exists('messageinbound_messagelist', ['userid' => $u1->id]));
 242          $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u2->id]));
 243      }
 244  
 245      public function test_export_data_for_user() {
 246          $dg = $this->getDataGenerator();
 247          $u1 = $dg->create_user();
 248          $u2 = $dg->create_user();
 249          $u1ctx = context_user::instance($u1->id);
 250          $u2ctx = context_user::instance($u2->id);
 251  
 252          $addressmanager = new \core\message\inbound\address_manager();
 253          $addressmanager->set_handler('\tool_messageinbound\message\inbound\invalid_recipient_handler');
 254          $addressmanager->set_data(123);
 255  
 256          // Create a user key for both users.
 257          $addressmanager->generate($u1->id);
 258          $addressmanager->generate($u2->id);
 259  
 260          // Create a messagelist for both users.
 261          $this->create_messagelist(['userid' => $u1->id, 'address' => 'u1@example1.com']);
 262          $this->create_messagelist(['userid' => $u1->id, 'address' => 'u1@example2.com']);
 263          $this->create_messagelist(['userid' => $u2->id, 'address' => 'u2@example1.com']);
 264  
 265          // Export for user.
 266          $this->setUser($u1);
 267          provider::export_user_data(new approved_contextlist($u1, 'tool_messageinbound', [$u1ctx->id, $u2ctx->id]));
 268          $data = writer::with_context($u2ctx)->get_data([get_string('messageinbound', 'tool_messageinbound')]);
 269          $this->assertEmpty($data);
 270          $data = writer::with_context($u1ctx)->get_data([get_string('messageinbound', 'tool_messageinbound')]);
 271          $this->assertCount(2, $data->messages_pending_validation);
 272          $this->assertEquals('u1@example1.com', $data->messages_pending_validation[0]['received_at']);
 273          $this->assertEquals('u1@example2.com', $data->messages_pending_validation[1]['received_at']);
 274  
 275          $data = writer::with_context($u2ctx)->get_related_data([get_string('messageinbound', 'tool_messageinbound')], 'userkeys');
 276          $this->assertEmpty($data);
 277          $data = writer::with_context($u1ctx)->get_related_data([get_string('messageinbound', 'tool_messageinbound')], 'userkeys');
 278          $this->assertCount(1, $data->keys);
 279          $this->assertEquals('messageinbound_handler', $data->keys[0]->script);
 280      }
 281  
 282      /**
 283       * Create a message to validate.
 284       *
 285       * @param array $params The params.
 286       * @return stdClass
 287       */
 288      protected function create_messagelist(array $params) {
 289          global $DB, $USER;
 290          $record = (object) array_merge([
 291              'messageid' => 'abc',
 292              'userid' => $USER->id,
 293              'address' => 'text@example.com',
 294              'timecreated' => time(),
 295          ], $params);
 296          $record->id = $DB->insert_record('messageinbound_messagelist', $record);
 297          return $record;
 298      }
 299  
 300  }