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.

Differences Between: [Versions 311 and 403] [Versions 400 and 403] [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   * Base class for unit tests for tool_mobile.
  18   *
  19   * @package    tool_mobile
  20   * @category   test
  21   * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_mobile\privacy;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use core_privacy\local\request\writer;
  29  use core_privacy\local\request\transform;
  30  use core_privacy\local\request\approved_contextlist;
  31  use core_privacy\local\request\approved_userlist;
  32  use tool_mobile\privacy\provider;
  33  
  34  /**
  35   * Unit tests for the tool_mobile implementation of the privacy API.
  36   *
  37   * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class provider_test extends \core_privacy\tests\provider_testcase {
  41  
  42      /**
  43       * Basic setup for these tests.
  44       */
  45      public function setUp(): void {
  46          $this->resetAfterTest(true);
  47      }
  48  
  49      /**
  50       * Test to check export_user_preferences.
  51       * returns user preferences data.
  52       */
  53      public function test_export_user_preferences() {
  54          $user = $this->getDataGenerator()->create_user();
  55          $expectedtime = time();
  56          set_user_preference('tool_mobile_autologin_request_last', time(), $user);
  57          provider::export_user_preferences($user->id);
  58          $writer = writer::with_context(\context_system::instance());
  59          $prefs = $writer->get_user_preferences('tool_mobile');
  60          $time = transform::datetime($expectedtime);
  61          $this->assertEquals($time, $prefs->tool_mobile_autologin_request_last->value);
  62          $this->assertEquals(get_string('privacy:metadata:preference:tool_mobile_autologin_request_last', 'tool_mobile'),
  63              $prefs->tool_mobile_autologin_request_last->description);
  64      }
  65  
  66      /**
  67       * Test getting the context for the user ID related to this plugin.
  68       */
  69      public function test_get_contexts_for_userid() {
  70          // Create user and Mobile user keys.
  71          $user = $this->getDataGenerator()->create_user();
  72          $context = \context_user::instance($user->id);
  73          $key = get_user_key('tool_mobile', $user->id);
  74          $contextlist = provider::get_contexts_for_userid($user->id);
  75          $this->assertEquals($context->id, $contextlist->current()->id);
  76      }
  77  
  78      /**
  79       * Test getting the users for a context related to this plugin.
  80       */
  81      public function test_get_users_in_context() {
  82          $component = 'tool_mobile';
  83  
  84          // Create users and Mobile user keys.
  85          $user1 = $this->getDataGenerator()->create_user();
  86          $user2 = $this->getDataGenerator()->create_user();
  87          $context1 = \context_user::instance($user1->id);
  88          $context2 = \context_user::instance($user2->id);
  89          $key1 = get_user_key('tool_mobile', $user1->id);
  90          $key2 = get_user_key('tool_mobile', $user2->id);
  91  
  92          // Ensure only user1 is found in context1.
  93          $userlist = new \core_privacy\local\request\userlist($context1, $component);
  94          provider::get_users_in_context($userlist);
  95          $userids = $userlist->get_userids();
  96          $userid = reset($userids);
  97  
  98          $this->assertCount(1, $userids);
  99          $this->assertEquals($user1->id, $userid);
 100      }
 101  
 102      /**
 103       * Test that data is exported correctly for this plugin.
 104       */
 105      public function test_export_user_data() {
 106          global $DB;
 107          // Create user and Mobile user keys.
 108          $user = $this->getDataGenerator()->create_user();
 109          $context = \context_user::instance($user->id);
 110          $keyvalue = get_user_key('tool_mobile', $user->id);
 111          $key = $DB->get_record('user_private_key', ['value' => $keyvalue]);
 112          // Validate exported data.
 113          $this->setUser($user);
 114          /** @var \core_privacy\tests\request\content_writer $writer */
 115          $writer = writer::with_context($context);
 116          $this->assertFalse($writer->has_any_data());
 117          $this->export_context_data_for_user($user->id, $context, 'tool_mobile');
 118          $userkeydata = $writer->get_related_data([], 'userkeys');
 119          $this->assertCount(1, $userkeydata->keys);
 120          $this->assertEquals($key->script, reset($userkeydata->keys)->script);
 121      }
 122  
 123      /**
 124       * Test for provider::delete_data_for_all_users_in_context().
 125       */
 126      public function test_delete_data_for_all_users_in_context() {
 127          global $DB;
 128          // Create user and Mobile user keys.
 129          $user = $this->getDataGenerator()->create_user();
 130          $context = \context_user::instance($user->id);
 131          $keyvalue = get_user_key('tool_mobile', $user->id);
 132          $key = $DB->get_record('user_private_key', ['value' => $keyvalue]);
 133          // Before deletion, we should have 1 user_private_key.
 134          $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']);
 135          $this->assertEquals(1, $count);
 136          // Delete data.
 137          provider::delete_data_for_all_users_in_context($context);
 138          // After deletion, the user_private_key entries should have been deleted.
 139          $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']);
 140          $this->assertEquals(0, $count);
 141      }
 142  
 143      /**
 144       * Test for provider::delete_data_for_user().
 145       */
 146      public function test_delete_data_for_user() {
 147          global $DB;
 148          // Create user and Mobile user keys.
 149          $user = $this->getDataGenerator()->create_user();
 150          $context = \context_user::instance($user->id);
 151          $keyvalue = get_user_key('tool_mobile', $user->id);
 152          $key = $DB->get_record('user_private_key', ['value' => $keyvalue]);
 153          // Before deletion, we should have 1 user_private_key.
 154          $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']);
 155          $this->assertEquals(1, $count);
 156          // Delete data.
 157          $contextlist = provider::get_contexts_for_userid($user->id);
 158          $approvedcontextlist = new approved_contextlist($user, 'tool_mobile', $contextlist->get_contextids());
 159          provider::delete_data_for_user($approvedcontextlist);
 160          // After deletion, the user_private_key entries should have been deleted.
 161          $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']);
 162          $this->assertEquals(0, $count);
 163      }
 164  
 165      /**
 166       * Test for provider::test_delete_data_for_users().
 167       */
 168      public function test_delete_data_for_users() {
 169          global $DB;
 170          $component = 'tool_mobile';
 171  
 172          // Create users and Mobile user keys.
 173          $user1 = $this->getDataGenerator()->create_user();
 174          $user2 = $this->getDataGenerator()->create_user();
 175          $context1 = \context_user::instance($user1->id);
 176          $context2 = \context_user::instance($user2->id);
 177          $keyvalue1 = get_user_key('tool_mobile', $user1->id);
 178          $keyvalue2 = get_user_key('tool_mobile', $user2->id);
 179          $key1 = $DB->get_record('user_private_key', ['value' => $keyvalue1]);
 180  
 181          // Before deletion, we should have 2 user_private_keys.
 182          $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']);
 183          $this->assertEquals(2, $count);
 184  
 185          // Ensure deleting wrong user in the user context does nothing.
 186          $approveduserids = [$user2->id];
 187          $approvedlist = new approved_userlist($context1, $component, $approveduserids);
 188          provider::delete_data_for_users($approvedlist);
 189  
 190          $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']);
 191          $this->assertEquals(2, $count);
 192  
 193          // Delete for user1 in context1.
 194          $approveduserids = [$user1->id];
 195          $approvedlist = new approved_userlist($context1, $component, $approveduserids);
 196          provider::delete_data_for_users($approvedlist);
 197  
 198          // Ensure only user1's data is deleted, user2's remains.
 199          $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']);
 200          $this->assertEquals(1, $count);
 201  
 202          $params = ['script' => $component];
 203          $userid = $DB->get_field_select('user_private_key', 'userid', 'script = :script', $params);
 204          $this->assertEquals($user2->id, $userid);
 205      }
 206  }