Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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