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