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.

Differences Between: [Versions 311 and 400]

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