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   * Privacy provider tests.
  19   *
  20   * @package    tool_policy
  21   * @category   test
  22   * @copyright  2018 Sara Arjona <sara@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  use core_privacy\local\metadata\collection;
  27  use tool_policy\privacy\provider;
  28  use tool_policy\api;
  29  use tool_policy\policy_version;
  30  use core_privacy\local\request\approved_contextlist;
  31  use core_privacy\local\request\writer;
  32  
  33  defined('MOODLE_INTERNAL') || die();
  34  
  35  /**
  36   * Privacy provider tests class.
  37   *
  38   * @copyright  2018 Sara Arjona <sara@moodle.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class tool_policy_privacy_provider_testcase extends \core_privacy\tests\provider_testcase {
  42      /** @var stdClass The user object. */
  43      protected $user;
  44  
  45      /** @var stdClass The manager user object. */
  46      protected $manager;
  47  
  48      /** @var context_system The system context instance. */
  49      protected $syscontext;
  50  
  51      /**
  52       * Setup function. Will create a user.
  53       */
  54      protected function setUp(): void {
  55          $this->resetAfterTest();
  56  
  57          $generator = $this->getDataGenerator();
  58          $this->user = $generator->create_user();
  59  
  60          // Create manager user.
  61          $this->manager = $generator->create_user();
  62          $this->syscontext = context_system::instance();
  63          $rolemanagerid = create_role('Policy manager', 'policymanager', 'Can manage policy documents');
  64          assign_capability('tool/policy:managedocs', CAP_ALLOW, $rolemanagerid, $this->syscontext->id);
  65          assign_capability('tool/policy:acceptbehalf', CAP_ALLOW, $rolemanagerid, $this->syscontext->id);
  66          role_assign($rolemanagerid, $this->manager->id, $this->syscontext->id);
  67          accesslib_clear_all_caches_for_unit_testing();
  68      }
  69  
  70      /**
  71       * Test getting the context for the user ID related to this plugin.
  72       */
  73      public function test_get_contexts_for_userid() {
  74          global $CFG;
  75  
  76          // When there are no policies or agreements context list is empty.
  77          $contextlist = \tool_policy\privacy\provider::get_contexts_for_userid($this->manager->id);
  78          $this->assertEmpty($contextlist);
  79          $contextlist = \tool_policy\privacy\provider::get_contexts_for_userid($this->user->id);
  80          $this->assertEmpty($contextlist);
  81  
  82          // Create a policy.
  83          $this->setUser($this->manager);
  84          $CFG->sitepolicyhandler = 'tool_policy';
  85          $policy = $this->add_policy();
  86          api::make_current($policy->get('id'));
  87  
  88          // After creating a policy, there should be manager context.
  89          $contextlist = \tool_policy\privacy\provider::get_contexts_for_userid($this->manager->id);
  90          $this->assertEquals(1, $contextlist->count());
  91  
  92          // But when there are no agreements, user context list is empty.
  93          $contextlist = \tool_policy\privacy\provider::get_contexts_for_userid($this->user->id);
  94          $this->assertEmpty($contextlist);
  95  
  96          // Agree to the policy.
  97          $this->setUser($this->user);
  98          api::accept_policies([$policy->get('id')]);
  99  
 100          // There should be user context.
 101          $contextlist = \tool_policy\privacy\provider::get_contexts_for_userid($this->user->id);
 102          $this->assertEquals(1, $contextlist->count());
 103      }
 104  
 105      /**
 106       * Test getting the user IDs within the context related to this plugin.
 107       */
 108      public function test_get_users_in_context() {
 109          global $CFG;
 110          $component = 'tool_policy';
 111  
 112          // System context should have nothing before a policy is added.
 113          $userlist = new \core_privacy\local\request\userlist($this->syscontext, $component);
 114          provider::get_users_in_context($userlist);
 115          $this->assertEmpty($userlist);
 116  
 117          // Create parent and child users.
 118          $generator = $this->getDataGenerator();
 119          $parentuser = $generator->create_user();
 120          $childuser = $generator->create_user();
 121  
 122          // Fetch relevant contexts.
 123          $managercontext = \context_user::instance($this->manager->id);
 124          $usercontext = $managercontext = \context_user::instance($this->user->id);
 125          $parentcontext = $managercontext = \context_user::instance($parentuser->id);
 126          $childcontext = $managercontext = \context_user::instance($childuser->id);
 127  
 128          // Assign parent to accept on behalf of the child.
 129          $roleparentid = create_role('Parent', 'parent', 'Can accept policies on behalf of their child');
 130          assign_capability('tool/policy:acceptbehalf', CAP_ALLOW, $roleparentid, $this->syscontext->id);
 131          role_assign($roleparentid, $parentuser->id, $childcontext->id);
 132  
 133          // Create a policy.
 134          $this->setUser($this->manager);
 135          $CFG->sitepolicyhandler = 'tool_policy';
 136          $policy = $this->add_policy();
 137          api::make_current($policy->get('id'));
 138  
 139          // Manager should exist in system context now they have created a policy.
 140          $userlist = new \core_privacy\local\request\userlist($this->syscontext, $component);
 141          provider::get_users_in_context($userlist);
 142          $this->assertCount(1, $userlist);
 143          $this->assertEquals([$this->manager->id], $userlist->get_userids());
 144  
 145          // User contexts should be empty before policy acceptances.
 146          $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
 147          provider::get_users_in_context($userlist);
 148          $this->assertEmpty($userlist);
 149  
 150          $userlist = new \core_privacy\local\request\userlist($parentcontext, $component);
 151          provider::get_users_in_context($userlist);
 152          $this->assertEmpty($userlist);
 153  
 154          $userlist = new \core_privacy\local\request\userlist($childcontext, $component);
 155          provider::get_users_in_context($userlist);
 156          $this->assertEmpty($userlist);
 157  
 158          // User accepts policy, parent accepts on behalf of child only.
 159          $this->setUser($this->user);
 160          api::accept_policies([$policy->get('id')]);
 161  
 162          $this->setUser($parentuser);
 163          api::accept_policies([$policy->get('id')], $childuser->id);
 164  
 165          // Ensure user is fetched within its user context.
 166          $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
 167          provider::get_users_in_context($userlist);
 168          $this->assertCount(1, $userlist);
 169          $this->assertEquals([$this->user->id], $userlist->get_userids());
 170  
 171          // Ensure parent and child are both found within child's user context.
 172          $userlist = new \core_privacy\local\request\userlist($childcontext, $component);
 173          provider::get_users_in_context($userlist);
 174          $this->assertCount(2, $userlist);
 175          $expected = [$parentuser->id, $childuser->id];
 176          $actual = $userlist->get_userids();
 177          sort($expected);
 178          sort($actual);
 179          $this->assertEquals($expected, $actual);
 180  
 181          // Parent has not accepted for itself, so should not be found within its user context.
 182          $userlist = new \core_privacy\local\request\userlist($parentcontext, $component);
 183          provider::get_users_in_context($userlist);
 184          $this->assertCount(0, $userlist);
 185      }
 186  
 187      public function test_export_agreements() {
 188          global $CFG;
 189  
 190          $otheruser = $this->getDataGenerator()->create_user();
 191          $otherusercontext = \context_user::instance($otheruser->id);
 192  
 193          // Create policies and agree to them as manager.
 194          $this->setUser($this->manager);
 195          $managercontext = \context_user::instance($this->manager->id);
 196          $systemcontext = \context_system::instance();
 197          $agreementsubcontext = [
 198              get_string('privacyandpolicies', 'admin'),
 199              get_string('useracceptances', 'tool_policy')
 200          ];
 201          $versionsubcontext = [
 202              get_string('policydocuments', 'tool_policy')
 203          ];
 204          $CFG->sitepolicyhandler = 'tool_policy';
 205          $policy1 = $this->add_policy();
 206          api::make_current($policy1->get('id'));
 207          $policy2 = $this->add_policy();
 208          api::make_current($policy2->get('id'));
 209          api::accept_policies([$policy1->get('id'), $policy2->get('id')]);
 210  
 211          // Agree to the policies for oneself.
 212          $this->setUser($this->user);
 213          $usercontext = \context_user::instance($this->user->id);
 214          api::accept_policies([$policy1->get('id'), $policy2->get('id')]);
 215  
 216          // Request export for this user.
 217          $contextlist = provider::get_contexts_for_userid($this->user->id);
 218          $this->assertCount(1, $contextlist);
 219          $this->assertEquals([$usercontext->id], $contextlist->get_contextids());
 220  
 221          $approvedcontextlist = new approved_contextlist($this->user, 'tool_policy', [$usercontext->id]);
 222          provider::export_user_data($approvedcontextlist);
 223  
 224          // User can not see manager's agreements but can see his own.
 225          $writer = writer::with_context($managercontext);
 226          $this->assertFalse($writer->has_any_data());
 227  
 228          $writer = writer::with_context($usercontext);
 229          $this->assertTrue($writer->has_any_data());
 230  
 231          // Test policy 1.
 232          $subcontext = array_merge($agreementsubcontext, [get_string('policynamedversion', 'tool_policy', $policy1->to_record())]);
 233          $datauser = $writer->get_data($subcontext);
 234          $this->assertEquals($policy1->get('name'), $datauser->name);
 235          $this->assertEquals($this->user->id, $datauser->agreedby);
 236          $this->assertEquals(strip_tags($policy1->get('summary')), strip_tags($datauser->summary));
 237          $this->assertEquals(strip_tags($policy1->get('content')), strip_tags($datauser->content));
 238  
 239          // Test policy 2.
 240          $subcontext = array_merge($agreementsubcontext, [get_string('policynamedversion', 'tool_policy', $policy2->to_record())]);
 241          $datauser = $writer->get_data($subcontext);
 242          $this->assertEquals($policy2->get('name'), $datauser->name);
 243          $this->assertEquals($this->user->id, $datauser->agreedby);
 244          $this->assertEquals(strip_tags($policy2->get('summary')), strip_tags($datauser->summary));
 245          $this->assertEquals(strip_tags($policy2->get('content')), strip_tags($datauser->content));
 246      }
 247  
 248      public function test_export_agreements_for_other() {
 249          global $CFG;
 250  
 251          $managercontext = \context_user::instance($this->manager->id);
 252          $systemcontext = \context_system::instance();
 253          $usercontext = \context_user::instance($this->user->id);
 254  
 255          // Create policies and agree to them as manager.
 256          $this->setUser($this->manager);
 257          $agreementsubcontext = [
 258              get_string('privacyandpolicies', 'admin'),
 259              get_string('useracceptances', 'tool_policy')
 260          ];
 261          $versionsubcontext = [
 262              get_string('policydocuments', 'tool_policy')
 263          ];
 264          $CFG->sitepolicyhandler = 'tool_policy';
 265          $policy1 = $this->add_policy();
 266          api::make_current($policy1->get('id'));
 267          $policy2 = $this->add_policy();
 268          api::make_current($policy2->get('id'));
 269          api::accept_policies([$policy1->get('id'), $policy2->get('id')]);
 270  
 271          // Agree to the other user's policies.
 272          api::accept_policies([$policy1->get('id'), $policy2->get('id')], $this->user->id, 'My note');
 273  
 274          // Request export for the manager.
 275          $contextlist = provider::get_contexts_for_userid($this->manager->id);
 276          $this->assertCount(3, $contextlist);
 277          $this->assertEqualsCanonicalizing(
 278              [$managercontext->id, $usercontext->id, $systemcontext->id],
 279              $contextlist->get_contextids()
 280          );
 281  
 282          $approvedcontextlist = new approved_contextlist($this->user, 'tool_policy', [$usercontext->id]);
 283          provider::export_user_data($approvedcontextlist);
 284  
 285          // The user context has data.
 286          $writer = writer::with_context($usercontext);
 287          $this->assertTrue($writer->has_any_data());
 288  
 289          // Test policy 1.
 290          $writer = writer::with_context($usercontext);
 291          $subcontext = array_merge($agreementsubcontext, [get_string('policynamedversion', 'tool_policy', $policy1->to_record())]);
 292          $datauser = $writer->get_data($subcontext);
 293          $this->assertEquals($policy1->get('name'), $datauser->name);
 294          $this->assertEquals($this->manager->id, $datauser->agreedby);
 295          $this->assertEquals(strip_tags($policy1->get('summary')), strip_tags($datauser->summary));
 296          $this->assertEquals(strip_tags($policy1->get('content')), strip_tags($datauser->content));
 297  
 298          // Test policy 2.
 299          $subcontext = array_merge($agreementsubcontext, [get_string('policynamedversion', 'tool_policy', $policy2->to_record())]);
 300          $datauser = $writer->get_data($subcontext);
 301          $this->assertEquals($policy2->get('name'), $datauser->name);
 302          $this->assertEquals($this->manager->id, $datauser->agreedby);
 303          $this->assertEquals(strip_tags($policy2->get('summary')), strip_tags($datauser->summary));
 304          $this->assertEquals(strip_tags($policy2->get('content')), strip_tags($datauser->content));
 305      }
 306  
 307      public function test_export_created_policies() {
 308          global $CFG;
 309  
 310          // Create policies and agree to them as manager.
 311          $this->setUser($this->manager);
 312          $managercontext = \context_user::instance($this->manager->id);
 313          $systemcontext = \context_system::instance();
 314          $agreementsubcontext = [
 315              get_string('privacyandpolicies', 'admin'),
 316              get_string('useracceptances', 'tool_policy')
 317          ];
 318          $versionsubcontext = [
 319              get_string('policydocuments', 'tool_policy')
 320          ];
 321          $CFG->sitepolicyhandler = 'tool_policy';
 322          $policy1 = $this->add_policy();
 323          api::make_current($policy1->get('id'));
 324          $policy2 = $this->add_policy();
 325          api::make_current($policy2->get('id'));
 326          api::accept_policies([$policy1->get('id'), $policy2->get('id')]);
 327  
 328          // Agree to the policies for oneself.
 329          $contextlist = provider::get_contexts_for_userid($this->manager->id);
 330          $this->assertCount(2, $contextlist);
 331          $this->assertEqualsCanonicalizing([$managercontext->id, $systemcontext->id], $contextlist->get_contextids());
 332  
 333          $approvedcontextlist = new approved_contextlist($this->manager, 'tool_policy', $contextlist->get_contextids());
 334          provider::export_user_data($approvedcontextlist);
 335  
 336          // User has agreed to policies.
 337          $writer = writer::with_context($managercontext);
 338          $this->assertTrue($writer->has_any_data());
 339  
 340          // Test policy 1.
 341          $subcontext = array_merge($agreementsubcontext, [get_string('policynamedversion', 'tool_policy', $policy1->to_record())]);
 342          $datauser = $writer->get_data($subcontext);
 343          $this->assertEquals($policy1->get('name'), $datauser->name);
 344          $this->assertEquals($this->manager->id, $datauser->agreedby);
 345          $this->assertEquals(strip_tags($policy1->get('summary')), strip_tags($datauser->summary));
 346          $this->assertEquals(strip_tags($policy1->get('content')), strip_tags($datauser->content));
 347  
 348          // Test policy 2.
 349          $subcontext = array_merge($agreementsubcontext, [get_string('policynamedversion', 'tool_policy', $policy2->to_record())]);
 350          $datauser = $writer->get_data($subcontext);
 351          $this->assertEquals($policy2->get('name'), $datauser->name);
 352          $this->assertEquals($this->manager->id, $datauser->agreedby);
 353          $this->assertEquals(strip_tags($policy2->get('summary')), strip_tags($datauser->summary));
 354          $this->assertEquals(strip_tags($policy2->get('content')), strip_tags($datauser->content));
 355  
 356          // User can see policy documents.
 357          $writer = writer::with_context($systemcontext);
 358          $this->assertTrue($writer->has_any_data());
 359  
 360          $subcontext = array_merge($versionsubcontext, [get_string('policynamedversion', 'tool_policy', $policy1->to_record())]);
 361          $dataversion = $writer->get_data($subcontext);
 362          $this->assertEquals($policy1->get('name'), $dataversion->name);
 363          $this->assertEquals(get_string('yes'), $dataversion->createdbyme);
 364  
 365          $subcontext = array_merge($versionsubcontext, [get_string('policynamedversion', 'tool_policy', $policy2->to_record())]);
 366          $dataversion = $writer->get_data($subcontext);
 367          $this->assertEquals($policy2->get('name'), $dataversion->name);
 368          $this->assertEquals(get_string('yes'), $dataversion->createdbyme);
 369      }
 370  
 371      /**
 372       * Helper method that creates a new policy for testing
 373       *
 374       * @param array $params
 375       * @return policy_version
 376       */
 377      protected function add_policy($params = []) {
 378          static $counter = 0;
 379          $counter++;
 380  
 381          $defaults = [
 382              'name' => 'Policy '.$counter,
 383              'summary_editor' => ['text' => "P$counter summary", 'format' => FORMAT_HTML, 'itemid' => 0],
 384              'content_editor' => ['text' => "P$counter content", 'format' => FORMAT_HTML, 'itemid' => 0],
 385          ];
 386  
 387          $params = (array)$params + $defaults;
 388          $formdata = \tool_policy\api::form_policydoc_data(new policy_version(0));
 389          foreach ($params as $key => $value) {
 390              $formdata->$key = $value;
 391          }
 392          return api::form_policydoc_add($formdata);
 393      }
 394  }