Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

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