Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]
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 namespace tool_policy; 18 19 use externallib_advanced_testcase; 20 use tool_mobile\external as external_mobile; 21 22 defined('MOODLE_INTERNAL') || die(); 23 24 global $CFG; 25 26 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 27 require_once($CFG->dirroot . '/user/externallib.php'); 28 29 /** 30 * External policy webservice API tests. 31 * 32 * @package tool_policy 33 * @copyright 2018 Sara Arjona <sara@moodle.com> 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class externallib_test extends externallib_advanced_testcase { 37 38 /** @var \tool_policy\policy_version $policy1 Policy document 1. */ 39 protected $policy1; 40 41 /** @var \tool_policy\policy_version $policy2 Policy document 2. */ 42 protected $policy2; 43 44 /** @var \tool_policy\policy_version $policy3 Policy document 3. */ 45 protected $policy3; 46 47 /** @var \stdClass $child user record. */ 48 protected $child; 49 50 /** @var \stdClass $parent user record. */ 51 protected $parent; 52 53 /** @var \stdClass $adult user record. */ 54 protected $adult; 55 56 /** 57 * Setup function- we will create some policy docs. 58 */ 59 public function setUp(): void { 60 $this->resetAfterTest(true); 61 $this->setAdminUser(); 62 63 // Prepare a policy document with some versions. 64 $formdata = api::form_policydoc_data(new \tool_policy\policy_version(0)); 65 $formdata->name = 'Test policy'; 66 $formdata->revision = 'v1'; 67 $formdata->summary_editor = ['text' => 'summary', 'format' => FORMAT_HTML, 'itemid' => 0]; 68 $formdata->content_editor = ['text' => 'content', 'format' => FORMAT_HTML, 'itemid' => 0]; 69 $this->policy1 = api::form_policydoc_add($formdata); 70 71 $formdata = api::form_policydoc_data($this->policy1); 72 $formdata->revision = 'v2'; 73 $this->policy2 = api::form_policydoc_update_new($formdata); 74 75 $formdata = api::form_policydoc_data($this->policy1); 76 $formdata->revision = 'v3'; 77 $this->policy3 = api::form_policydoc_update_new($formdata); 78 79 api::make_current($this->policy2->get('id')); 80 81 // Create users. 82 $this->child = $this->getDataGenerator()->create_user(); 83 $this->parent = $this->getDataGenerator()->create_user(); 84 $this->adult = $this->getDataGenerator()->create_user(); 85 86 $syscontext = \context_system::instance(); 87 $childcontext = \context_user::instance($this->child->id); 88 89 $roleminorid = create_role('Digital minor', 'digiminor', 'Not old enough to accept site policies themselves'); 90 $roleparentid = create_role('Parent', 'parent', 'Can accept policies on behalf of their child'); 91 92 assign_capability('tool/policy:accept', CAP_PROHIBIT, $roleminorid, $syscontext->id); 93 assign_capability('tool/policy:acceptbehalf', CAP_ALLOW, $roleparentid, $syscontext->id); 94 95 role_assign($roleminorid, $this->child->id, $syscontext->id); 96 role_assign($roleparentid, $this->parent->id, $childcontext->id); 97 } 98 99 /** 100 * Test for the get_policy_version() function. 101 */ 102 public function test_get_policy_version() { 103 $this->setUser($this->adult); 104 105 // View current policy version. 106 $result = external::get_policy_version($this->policy2->get('id')); 107 $result = \core_external\external_api::clean_returnvalue(external::get_policy_version_returns(), $result); 108 $this->assertCount(1, $result['result']); 109 $this->assertEquals($this->policy1->get('name'), $result['result']['policy']['name']); 110 $this->assertEquals($this->policy1->get('content'), $result['result']['policy']['content']); 111 112 // View draft policy version. 113 $result = external::get_policy_version($this->policy3->get('id')); 114 $result = \core_external\external_api::clean_returnvalue(external::get_policy_version_returns(), $result); 115 $this->assertCount(0, $result['result']); 116 $this->assertCount(1, $result['warnings']); 117 $this->assertEquals(array_pop($result['warnings'])['warningcode'], 'errorusercantviewpolicyversion'); 118 119 // Add test for non existing versionid. 120 $result = external::get_policy_version(999); 121 $result = \core_external\external_api::clean_returnvalue(external::get_policy_version_returns(), $result); 122 $this->assertCount(0, $result['result']); 123 $this->assertCount(1, $result['warnings']); 124 $this->assertEquals(array_pop($result['warnings'])['warningcode'], 'errorpolicyversionnotfound'); 125 126 // View previous non-accepted version in behalf of a child. 127 $this->setUser($this->parent); 128 $result = external::get_policy_version($this->policy1->get('id'), $this->child->id); 129 $result = \core_external\external_api::clean_returnvalue(external::get_policy_version_returns(), $result); 130 $this->assertCount(0, $result['result']); 131 $this->assertCount(1, $result['warnings']); 132 $this->assertEquals(array_pop($result['warnings'])['warningcode'], 'errorusercantviewpolicyversion'); 133 134 // Let the parent accept the policy on behalf of her child and view it again. 135 api::accept_policies($this->policy1->get('id'), $this->child->id); 136 $result = external::get_policy_version($this->policy1->get('id'), $this->child->id); 137 $result = \core_external\external_api::clean_returnvalue(external::get_policy_version_returns(), $result); 138 $this->assertCount(1, $result['result']); 139 $this->assertEquals($this->policy1->get('name'), $result['result']['policy']['name']); 140 $this->assertEquals($this->policy1->get('content'), $result['result']['policy']['content']); 141 142 // Only parent is able to view the child policy version accepted by her child. 143 $this->setUser($this->adult); 144 $result = external::get_policy_version($this->policy1->get('id'), $this->child->id); 145 $result = \core_external\external_api::clean_returnvalue(external::get_policy_version_returns(), $result); 146 $this->assertCount(0, $result['result']); 147 $this->assertCount(1, $result['warnings']); 148 $this->assertEquals(array_pop($result['warnings'])['warningcode'], 'errorusercantviewpolicyversion'); 149 } 150 151 /** 152 * Test tool_mobile\external callback to site_policy_handler. 153 */ 154 public function test_get_config_with_site_policy_handler() { 155 global $CFG; 156 157 $this->resetAfterTest(true); 158 159 // Set the handler for the site policy, make sure it substitutes link to the sitepolicy. 160 $CFG->sitepolicyhandler = 'tool_policy'; 161 $sitepolicymanager = new \core_privacy\local\sitepolicy\manager(); 162 $result = external_mobile::get_config(); 163 $result = \core_external\external_api::clean_returnvalue(external_mobile::get_config_returns(), $result); 164 $toolsitepolicy = $sitepolicymanager->get_embed_url(); 165 foreach (array_values($result['settings']) as $r) { 166 if ($r['name'] == 'sitepolicy') { 167 $configsitepolicy = $r['value']; 168 } 169 } 170 $this->assertEquals($toolsitepolicy, $configsitepolicy); 171 } 172 173 /** 174 * Test for core_privacy\sitepolicy\manager::accept() when site policy handler is set. 175 */ 176 public function test_agree_site_policy_with_handler() { 177 global $CFG, $DB, $USER; 178 179 $this->resetAfterTest(true); 180 $user = self::getDataGenerator()->create_user(); 181 $this->setUser($user); 182 183 // Set mock site policy handler. See function tool_phpunit_site_policy_handler() below. 184 $CFG->sitepolicyhandler = 'tool_policy'; 185 $this->assertEquals(0, $USER->policyagreed); 186 $sitepolicymanager = new \core_privacy\local\sitepolicy\manager(); 187 188 // Make sure user can not login. 189 $toolconsentpage = $sitepolicymanager->get_redirect_url(); 190 $this->expectException(\moodle_exception::class); 191 $this->expectExceptionMessage(get_string('sitepolicynotagreed', 'error', $toolconsentpage->out())); 192 \core_user_external::validate_context(\context_system::instance()); 193 194 // Call WS to agree to the site policy. It will call tool_policy handler. 195 $result = \core_user_external::agree_site_policy(); 196 $result = \core_external\external_api::clean_returnvalue(\core_user_external::agree_site_policy_returns(), $result); 197 $this->assertTrue($result['status']); 198 $this->assertCount(0, $result['warnings']); 199 $this->assertEquals(1, $USER->policyagreed); 200 $this->assertEquals(1, $DB->get_field('user', 'policyagreed', array('id' => $USER->id))); 201 202 // Try again, we should get a warning. 203 $result = \core_user_external::agree_site_policy(); 204 $result = \core_external\external_api::clean_returnvalue(\core_user_external::agree_site_policy_returns(), $result); 205 $this->assertFalse($result['status']); 206 $this->assertCount(1, $result['warnings']); 207 $this->assertEquals('alreadyagreed', $result['warnings'][0]['warningcode']); 208 } 209 210 /** 211 * Test for core_privacy\sitepolicy\manager::accept() when site policy handler is set. 212 */ 213 public function test_checkcanaccept_with_handler() { 214 global $CFG; 215 216 $this->resetAfterTest(true); 217 $CFG->sitepolicyhandler = 'tool_policy'; 218 $syscontext = \context_system::instance(); 219 $sitepolicymanager = new \core_privacy\local\sitepolicy\manager(); 220 221 $adult = $this->getDataGenerator()->create_user(); 222 223 $child = $this->getDataGenerator()->create_user(); 224 $rolechildid = create_role('Child', 'child', 'Not old enough to accept site policies themselves'); 225 assign_capability('tool/policy:accept', CAP_PROHIBIT, $rolechildid, $syscontext->id); 226 role_assign($rolechildid, $child->id, $syscontext->id); 227 228 // Default user can accept policies. 229 $this->setUser($adult); 230 $result = external_mobile::get_config(); 231 $result = \core_external\external_api::clean_returnvalue(external_mobile::get_config_returns(), $result); 232 $toolsitepolicy = $sitepolicymanager->accept(); 233 $this->assertTrue($toolsitepolicy); 234 235 // Child user can not accept policies. 236 $this->setUser($child); 237 $result = external_mobile::get_config(); 238 $result = \core_external\external_api::clean_returnvalue(external_mobile::get_config_returns(), $result); 239 $this->expectException(\required_capability_exception::class); 240 $sitepolicymanager->accept(); 241 } 242 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body