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   * Base class for unit tests for profilefield_datetime.
  19   *
  20   * @package    profilefield_datetime
  21   * @copyright  2018 Mihail Geshoski <mihail@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace profilefield_datetime\privacy;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use core_privacy\tests\provider_testcase;
  29  use profilefield_datetime\privacy\provider;
  30  use core_privacy\local\request\approved_userlist;
  31  
  32  /**
  33   * Unit tests for user\profile\field\datetime\classes\privacy\provider.php
  34   *
  35   * @copyright  2018 Mihail Geshoski <mihail@moodle.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class provider_test extends provider_testcase {
  39  
  40      /**
  41       * Basic setup for these tests.
  42       */
  43      public function setUp(): void {
  44          $this->resetAfterTest(true);
  45      }
  46  
  47      /**
  48       * Test getting the context for the user ID related to this plugin.
  49       */
  50      public function test_get_contexts_for_userid() {
  51          global $DB;
  52          // Create profile category.
  53          $categoryid = $this->add_profile_category();
  54          // Create profile field.
  55          $profilefieldid = $this->add_profile_field($categoryid, 'datetime');
  56          // Create a user.
  57          $user = $this->getDataGenerator()->create_user();
  58          $this->add_user_info_data($user->id, $profilefieldid, 'test data');
  59          // Get the field that was created.
  60          $userfielddata = $DB->get_records('user_info_data', array('userid' => $user->id));
  61          // Confirm we got the right number of user field data.
  62          $this->assertCount(1, $userfielddata);
  63          $context = \context_user::instance($user->id);
  64          $contextlist = provider::get_contexts_for_userid($user->id);
  65          $this->assertEquals($context, $contextlist->current());
  66      }
  67  
  68      /**
  69       * Test that data is exported correctly for this plugin.
  70       */
  71      public function test_export_user_data() {
  72          // Create profile category.
  73          $categoryid = $this->add_profile_category();
  74          // Create datetime profile field.
  75          $datetimeprofilefieldid = $this->add_profile_field($categoryid, 'datetime');
  76          // Create checkbox profile field.
  77          $checkboxprofilefieldid = $this->add_profile_field($categoryid, 'checkbox');
  78          // Create a user.
  79          $user = $this->getDataGenerator()->create_user();
  80          $context = \context_user::instance($user->id);
  81          // Add datetime user info data.
  82          $this->add_user_info_data($user->id, $datetimeprofilefieldid, '1524067200');
  83          // Add checkbox user info data.
  84          $this->add_user_info_data($user->id, $checkboxprofilefieldid, 'test data');
  85  
  86          $writer = \core_privacy\local\request\writer::with_context($context);
  87          $this->assertFalse($writer->has_any_data());
  88          $this->export_context_data_for_user($user->id, $context, 'profilefield_datetime');
  89          $data = $writer->get_data([get_string('pluginname', 'profilefield_datetime')]);
  90          $this->assertCount(3, (array) $data);
  91          $this->assertEquals('Test field', $data->name);
  92          $this->assertEquals('This is a test.', $data->description);
  93          $this->assertEquals('19 April 2018', $data->data);
  94      }
  95  
  96      /**
  97       * Test that user data is deleted using the context.
  98       */
  99      public function test_delete_data_for_all_users_in_context() {
 100          global $DB;
 101          // Create profile category.
 102          $categoryid = $this->add_profile_category();
 103          // Create datetime profile field.
 104          $datetimeprofilefieldid = $this->add_profile_field($categoryid, 'datetime');
 105          // Create checkbox profile field.
 106          $checkboxprofilefieldid = $this->add_profile_field($categoryid, 'checkbox');
 107          // Create a user.
 108          $user = $this->getDataGenerator()->create_user();
 109          $context = \context_user::instance($user->id);
 110          // Add datetime user info data.
 111          $this->add_user_info_data($user->id, $datetimeprofilefieldid, '1524067200');
 112          // Add checkbox user info data.
 113          $this->add_user_info_data($user->id, $checkboxprofilefieldid, 'test data');
 114          // Check that we have two entries.
 115          $userinfodata = $DB->get_records('user_info_data', ['userid' => $user->id]);
 116          $this->assertCount(2, $userinfodata);
 117          provider::delete_data_for_all_users_in_context($context);
 118          // Check that the correct profile field has been deleted.
 119          $userinfodata = $DB->get_records('user_info_data', ['userid' => $user->id]);
 120          $this->assertCount(1, $userinfodata);
 121          $this->assertNotEquals('1524067200', reset($userinfodata)->data);
 122      }
 123  
 124      /**
 125       * Test that user data is deleted for this user.
 126       */
 127      public function test_delete_data_for_user() {
 128          global $DB;
 129          // Create profile category.
 130          $categoryid = $this->add_profile_category();
 131          // Create datetime profile field.
 132          $datetimeprofilefieldid = $this->add_profile_field($categoryid, 'datetime');
 133          // Create checkbox profile field.
 134          $checkboxprofilefieldid = $this->add_profile_field($categoryid, 'checkbox');
 135          // Create a user.
 136          $user = $this->getDataGenerator()->create_user();
 137          $context = \context_user::instance($user->id);
 138          // Add datetime user info data.
 139          $this->add_user_info_data($user->id, $datetimeprofilefieldid, '1524067200');
 140          // Add checkbox user info data.
 141          $this->add_user_info_data($user->id, $checkboxprofilefieldid, 'test data');
 142          // Check that we have two entries.
 143          $userinfodata = $DB->get_records('user_info_data', ['userid' => $user->id]);
 144          $this->assertCount(2, $userinfodata);
 145          $approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'profilefield_datetime',
 146              [$context->id]);
 147          provider::delete_data_for_user($approvedlist);
 148          // Check that the correct profile field has been deleted.
 149          $userinfodata = $DB->get_records('user_info_data', ['userid' => $user->id]);
 150          $this->assertCount(1, $userinfodata);
 151          $this->assertNotEquals('1524067200', reset($userinfodata)->data);
 152      }
 153  
 154      /**
 155       * Test that only users with a user context are fetched.
 156       */
 157      public function test_get_users_in_context() {
 158          $this->resetAfterTest();
 159  
 160          $component = 'profilefield_datetime';
 161  
 162          // Create profile category.
 163          $categoryid = $this->add_profile_category();
 164          // Create profile field.
 165          $profilefieldid = $this->add_profile_field($categoryid, 'datetime');
 166  
 167          // Create a user.
 168          $user = $this->getDataGenerator()->create_user();
 169          $usercontext = \context_user::instance($user->id);
 170  
 171          // The list of users should not return anything yet (related data still haven't been created).
 172          $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
 173          provider::get_users_in_context($userlist);
 174          $this->assertCount(0, $userlist);
 175  
 176          $this->add_user_info_data($user->id, $profilefieldid, 'test data');
 177  
 178          // The list of users for user context should return the user.
 179          provider::get_users_in_context($userlist);
 180          $this->assertCount(1, $userlist);
 181          $expected = [$user->id];
 182          $actual = $userlist->get_userids();
 183          $this->assertEquals($expected, $actual);
 184  
 185          // The list of users for system context should not return any users.
 186          $systemcontext = \context_system::instance();
 187          $userlist = new \core_privacy\local\request\userlist($systemcontext, $component);
 188          provider::get_users_in_context($userlist);
 189          $this->assertCount(0, $userlist);
 190      }
 191  
 192      /**
 193       * Test that data for users in approved userlist is deleted.
 194       */
 195      public function test_delete_data_for_users() {
 196          $this->resetAfterTest();
 197  
 198          $component = 'profilefield_datetime';
 199          // Create profile category.
 200          $categoryid = $this->add_profile_category();
 201          // Create profile field.
 202          $profilefieldid = $this->add_profile_field($categoryid, 'datetime');
 203  
 204          // Create user1.
 205          $user1 = $this->getDataGenerator()->create_user();
 206          $usercontext1 = \context_user::instance($user1->id);
 207  
 208          // Create user2.
 209          $user2 = $this->getDataGenerator()->create_user();
 210          $usercontext2 = \context_user::instance($user2->id);
 211  
 212          $this->add_user_info_data($user1->id, $profilefieldid, 'test data');
 213          $this->add_user_info_data($user2->id, $profilefieldid, 'test data');
 214  
 215          // The list of users for usercontext1 should return user1.
 216          $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
 217          provider::get_users_in_context($userlist1);
 218          $this->assertCount(1, $userlist1);
 219          $expected = [$user1->id];
 220          $actual = $userlist1->get_userids();
 221          $this->assertEquals($expected, $actual);
 222  
 223          // The list of users for usercontext2 should return user2.
 224          $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
 225          provider::get_users_in_context($userlist2);
 226          $this->assertCount(1, $userlist2);
 227          $expected = [$user2->id];
 228          $actual = $userlist2->get_userids();
 229          $this->assertEquals($expected, $actual);
 230  
 231          // Add userlist1 to the approved user list.
 232          $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
 233  
 234          // Delete user data using delete_data_for_user for usercontext1.
 235          provider::delete_data_for_users($approvedlist);
 236  
 237          // Re-fetch users in usercontext1 - The user list should now be empty.
 238          $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
 239          provider::get_users_in_context($userlist1);
 240          $this->assertCount(0, $userlist1);
 241  
 242          // Re-fetch users in usercontext2 - The user list should not be empty (user2).
 243          $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
 244          provider::get_users_in_context($userlist2);
 245          $this->assertCount(1, $userlist2);
 246  
 247          // User data should be only removed in the user context.
 248          $systemcontext = \context_system::instance();
 249          // Add userlist2 to the approved user list in the system context.
 250          $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
 251          // Delete user1 data using delete_data_for_user.
 252          provider::delete_data_for_users($approvedlist);
 253          // Re-fetch users in usercontext2 - The user list should not be empty (user2).
 254          $userlist1 = new \core_privacy\local\request\userlist($usercontext2, $component);
 255          provider::get_users_in_context($userlist1);
 256          $this->assertCount(1, $userlist1);
 257      }
 258  
 259      /**
 260       * Add dummy user info data.
 261       *
 262       * @param int $userid The ID of the user
 263       * @param int $fieldid The ID of the field
 264       * @param string $data The data
 265       */
 266      private function add_user_info_data($userid, $fieldid, $data) {
 267          global $DB;
 268          $userinfodata = array(
 269              'userid' => $userid,
 270              'fieldid' => $fieldid,
 271              'data' => $data,
 272              'dataformat' => 0
 273          );
 274  
 275          $DB->insert_record('user_info_data', $userinfodata);
 276      }
 277  
 278      /**
 279       * Add dummy profile category.
 280       *
 281       * @return int The ID of the profile category
 282       */
 283      private function add_profile_category() {
 284          $cat = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Test category']);
 285          return $cat->id;
 286      }
 287  
 288      /**
 289       * Add dummy profile field.
 290       *
 291       * @param int $categoryid The ID of the profile category
 292       * @param string $datatype The datatype of the profile field
 293       * @return int The ID of the profile field
 294       */
 295      private function add_profile_field($categoryid, $datatype) {
 296          $data = $this->getDataGenerator()->create_custom_profile_field([
 297              'datatype' => $datatype,
 298              'shortname' => 'tstField',
 299              'name' => 'Test field',
 300              'description' => 'This is a test.',
 301              'categoryid' => $categoryid,
 302          ]);
 303          return $data->id;
 304      }
 305  }