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