Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   * Base class for unit tests for profilefield_textarea.
  19   *
  20   * @package    profilefield_textarea
  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_textarea\privacy;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use core_privacy\tests\provider_testcase;
  29  use profilefield_textarea\privacy\provider;
  30  use core_privacy\local\request\approved_userlist;
  31  
  32  /**
  33   * Unit tests for user\profile\field\textarea\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, 'textarea');
  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 textarea profile field.
  75          $textareaprofilefieldid = $this->add_profile_field($categoryid, 'textarea');
  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 textarea user info data.
  82          $this->add_user_info_data($user->id, $textareaprofilefieldid, 'test textarea');
  83          // Add checkbox user info data.
  84          $this->add_user_info_data($user->id, $checkboxprofilefieldid, 'test data');
  85          $writer = \core_privacy\local\request\writer::with_context($context);
  86          $this->assertFalse($writer->has_any_data());
  87          $this->export_context_data_for_user($user->id, $context, 'profilefield_textarea');
  88          $data = $writer->get_data([get_string('pluginname', 'profilefield_textarea')]);
  89          $this->assertCount(3, (array) $data);
  90          $this->assertEquals('Test field', $data->name);
  91          $this->assertEquals('This is a test.', $data->description);
  92          $this->assertEquals('test textarea', $data->data);
  93      }
  94  
  95      /**
  96       * Test that user data is deleted using the context.
  97       */
  98      public function test_delete_data_for_all_users_in_context() {
  99          global $DB;
 100          // Create profile category.
 101          $categoryid = $this->add_profile_category();
 102          // Create textarea profile field.
 103          $textareaprofilefieldid = $this->add_profile_field($categoryid, 'textarea');
 104          // Create checkbox profile field.
 105          $checkboxprofilefieldid = $this->add_profile_field($categoryid, 'checkbox');
 106          // Create a user.
 107          $user = $this->getDataGenerator()->create_user();
 108          $context = \context_user::instance($user->id);
 109          // Add textarea user info data.
 110          $this->add_user_info_data($user->id, $textareaprofilefieldid, 'test textarea');
 111          // Add checkbox user info data.
 112          $this->add_user_info_data($user->id, $checkboxprofilefieldid, 'test data');
 113          // Check that we have two entries.
 114          $userinfodata = $DB->get_records('user_info_data', ['userid' => $user->id]);
 115          $this->assertCount(2, $userinfodata);
 116          provider::delete_data_for_all_users_in_context($context);
 117          // Check that the correct profile field has been deleted.
 118          $userinfodata = $DB->get_records('user_info_data', ['userid' => $user->id]);
 119          $this->assertCount(1, $userinfodata);
 120          $this->assertNotEquals('test textarea', reset($userinfodata)->data);
 121      }
 122  
 123      /**
 124       * Test that user data is deleted for this user.
 125       */
 126      public function test_delete_data_for_user() {
 127          global $DB;
 128          // Create profile category.
 129          $categoryid = $this->add_profile_category();
 130          // Create textarea profile field.
 131          $textareaprofilefieldid = $this->add_profile_field($categoryid, 'textarea');
 132          // Create checkbox profile field.
 133          $checkboxprofilefieldid = $this->add_profile_field($categoryid, 'checkbox');
 134          // Create a user.
 135          $user = $this->getDataGenerator()->create_user();
 136          $context = \context_user::instance($user->id);
 137          // Add textarea user info data.
 138          $this->add_user_info_data($user->id, $textareaprofilefieldid, 'test textarea');
 139          // Add checkbox user info data.
 140          $this->add_user_info_data($user->id, $checkboxprofilefieldid, 'test data');
 141          // Check that we have two entries.
 142          $userinfodata = $DB->get_records('user_info_data', ['userid' => $user->id]);
 143          $this->assertCount(2, $userinfodata);
 144          $approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'profilefield_textarea',
 145              [$context->id]);
 146          provider::delete_data_for_user($approvedlist);
 147          // Check that the correct profile field has been deleted.
 148          $userinfodata = $DB->get_records('user_info_data', ['userid' => $user->id]);
 149          $this->assertCount(1, $userinfodata);
 150          $this->assertNotEquals('test textarea', reset($userinfodata)->data);
 151      }
 152  
 153      /**
 154       * Test that only users with a user context are fetched.
 155       */
 156      public function test_get_users_in_context() {
 157          $this->resetAfterTest();
 158  
 159          $component = 'profilefield_textarea';
 160          // Create profile category.
 161          $categoryid = $this->add_profile_category();
 162          // Create profile field.
 163          $profilefieldid = $this->add_profile_field($categoryid, 'textarea');
 164  
 165          // Create a user.
 166          $user = $this->getDataGenerator()->create_user();
 167          $usercontext = \context_user::instance($user->id);
 168          // The list of users should not return anything yet (related data still haven't been created).
 169          $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
 170          provider::get_users_in_context($userlist);
 171          $this->assertCount(0, $userlist);
 172  
 173          $this->add_user_info_data($user->id, $profilefieldid, 'test data');
 174  
 175          // The list of users for user context should return the user.
 176          provider::get_users_in_context($userlist);
 177          $this->assertCount(1, $userlist);
 178          $expected = [$user->id];
 179          $actual = $userlist->get_userids();
 180          $this->assertEquals($expected, $actual);
 181  
 182          // The list of users for system context should not return any users.
 183          $systemcontext = \context_system::instance();
 184          $userlist = new \core_privacy\local\request\userlist($systemcontext, $component);
 185          provider::get_users_in_context($userlist);
 186          $this->assertCount(0, $userlist);
 187      }
 188  
 189      /**
 190       * Test that data for users in approved userlist is deleted.
 191       */
 192      public function test_delete_data_for_users() {
 193          $this->resetAfterTest();
 194  
 195          $component = 'profilefield_textarea';
 196          // Create profile category.
 197          $categoryid = $this->add_profile_category();
 198          // Create profile field.
 199          $profilefieldid = $this->add_profile_field($categoryid, 'textarea');
 200  
 201          // Create user1.
 202          $user1 = $this->getDataGenerator()->create_user();
 203          $usercontext1 = \context_user::instance($user1->id);
 204          // Create user2.
 205          $user2 = $this->getDataGenerator()->create_user();
 206          $usercontext2 = \context_user::instance($user2->id);
 207  
 208          $this->add_user_info_data($user1->id, $profilefieldid, 'test data');
 209          $this->add_user_info_data($user2->id, $profilefieldid, 'test data');
 210  
 211          // The list of users for usercontext1 should return user1.
 212          $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
 213          provider::get_users_in_context($userlist1);
 214          $this->assertCount(1, $userlist1);
 215          $expected = [$user1->id];
 216          $actual = $userlist1->get_userids();
 217          $this->assertEquals($expected, $actual);
 218  
 219          // The list of users for usercontext2 should return user2.
 220          $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
 221          provider::get_users_in_context($userlist2);
 222          $this->assertCount(1, $userlist2);
 223          $expected = [$user2->id];
 224          $actual = $userlist2->get_userids();
 225          $this->assertEquals($expected, $actual);
 226  
 227          // Add userlist1 to the approved user list.
 228          $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
 229  
 230          // Delete user data using delete_data_for_user for usercontext1.
 231          provider::delete_data_for_users($approvedlist);
 232  
 233          // Re-fetch users in usercontext1 - The user list should now be empty.
 234          $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
 235          provider::get_users_in_context($userlist1);
 236          $this->assertCount(0, $userlist1);
 237  
 238          // Re-fetch users in usercontext2 - The user list should not be empty (user2).
 239          $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
 240          provider::get_users_in_context($userlist2);
 241          $this->assertCount(1, $userlist2);
 242  
 243          // User data should be only removed in the user context.
 244          $systemcontext = \context_system::instance();
 245          // Add userlist2 to the approved user list in the system context.
 246          $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
 247          // Delete user1 data using delete_data_for_user.
 248          provider::delete_data_for_users($approvedlist);
 249          // Re-fetch users in usercontext2 - The user list should not be empty (user2).
 250          $userlist1 = new \core_privacy\local\request\userlist($usercontext2, $component);
 251          provider::get_users_in_context($userlist1);
 252          $this->assertCount(1, $userlist1);
 253      }
 254  
 255      /**
 256       * Add dummy user info data.
 257       *
 258       * @param int $userid The ID of the user
 259       * @param int $fieldid The ID of the field
 260       * @param string $data The data
 261       */
 262      private function add_user_info_data($userid, $fieldid, $data) {
 263          global $DB;
 264          $userinfodata = array(
 265              'userid' => $userid,
 266              'fieldid' => $fieldid,
 267              'data' => $data,
 268              'dataformat' => 0
 269          );
 270  
 271          $DB->insert_record('user_info_data', $userinfodata);
 272      }
 273  
 274      /**
 275       * Add dummy profile category.
 276       *
 277       * @return int The ID of the profile category
 278       */
 279      private function add_profile_category() {
 280          $cat = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Test category']);
 281          return $cat->id;
 282      }
 283  
 284      /**
 285       * Add dummy profile field.
 286       *
 287       * @param int $categoryid The ID of the profile category
 288       * @param string $datatype The datatype of the profile field
 289       * @return int The ID of the profile field
 290       */
 291      private function add_profile_field($categoryid, $datatype) {
 292          $data = $this->getDataGenerator()->create_custom_profile_field([
 293              'datatype' => $datatype,
 294              'shortname' => 'tstField',
 295              'name' => 'Test field',
 296              'description' => 'This is a test.',
 297              'categoryid' => $categoryid,
 298          ]);
 299          return $data->id;
 300      }
 301  }