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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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   * Provides the {@link tool_policy_sitepolicy_handler_testcase} class.
  19   *
  20   * @package     tool_policy
  21   * @category    test
  22   * @copyright   2018 David Mudrák <david@moodle.com>
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  use tool_policy\api;
  27  use tool_policy\policy_version;
  28  use tool_policy\privacy\local\sitepolicy\handler;
  29  use tool_policy\test\helper;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  global $CFG;
  34  
  35  /**
  36   * Unit tests for the {@link \tool_policy\privacy\local\sitepolicy\handler} class.
  37   *
  38   * @copyright 2018 David Mudrak <david@moodle.com>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class tool_policy_sitepolicy_handler_testcase extends advanced_testcase {
  42  
  43      /**
  44       * Test behaviour of the {@link \tool_policy\privacy\local\sitepolicy\handler::get_redirect_url()} method.
  45       */
  46      public function test_get_redirect_url() {
  47          $this->resetAfterTest();
  48          $this->setAdminUser();
  49  
  50          // No redirect for guests.
  51          $this->assertNull(handler::get_redirect_url(true));
  52  
  53          // No redirect if there is no policy.
  54          $this->assertNull(handler::get_redirect_url());
  55  
  56          // No redirect if no policy for logged in users.
  57          $policy1 = helper::add_policy(['audience' => policy_version::AUDIENCE_GUESTS])->to_record();
  58          api::make_current($policy1->id);
  59          $this->assertNull(handler::get_redirect_url());
  60  
  61          // URL only when there is actually some policy to show.
  62          $policy2 = helper::add_policy(['audience' => policy_version::AUDIENCE_LOGGEDIN])->to_record();
  63          api::make_current($policy2->id);
  64          $this->assertInstanceOf('moodle_url', handler::get_redirect_url());
  65      }
  66  
  67      /**
  68       * Test behaviour of the {@link \tool_policy\privacy\local\sitepolicy\handler::get_embed_url()} method.
  69       */
  70      public function test_get_embed_url() {
  71          $this->resetAfterTest();
  72          $this->setAdminUser();
  73  
  74          // No embed if there is no policy.
  75          $this->assertNull(handler::get_embed_url());
  76          $this->assertNull(handler::get_embed_url(true));
  77  
  78          $policy1 = helper::add_policy(['audience' => policy_version::AUDIENCE_GUESTS])->to_record();
  79          api::make_current($policy1->id);
  80  
  81          // Policy exists for guests only.
  82          $this->assertNull(handler::get_embed_url());
  83          $this->assertInstanceOf('moodle_url', handler::get_embed_url(true));
  84  
  85          $policy2 = helper::add_policy(['audience' => policy_version::AUDIENCE_LOGGEDIN])->to_record();
  86          api::make_current($policy2->id);
  87  
  88          // Some policy exists for all users.
  89          $this->assertInstanceOf('moodle_url', handler::get_embed_url());
  90          $this->assertInstanceOf('moodle_url', handler::get_embed_url(true));
  91      }
  92  
  93      /**
  94       * Test behaviour of the {@link \tool_policy\privacy\local\sitepolicy\handler::accept()} method.
  95       */
  96      public function test_accept() {
  97          global $DB, $USER;
  98          $this->resetAfterTest();
  99  
 100          // False if not logged in.
 101          $this->setUser(0);
 102          $this->assertFalse(handler::accept());
 103  
 104          // Guests accept policies implicitly by continuing to use the site.
 105          $this->setGuestUser();
 106          $this->assertTrue(handler::accept());
 107  
 108          // Create one compulsory and one optional policy.
 109          $this->setAdminUser();
 110          $policy1 = helper::add_policy(['optional' => policy_version::AGREEMENT_COMPULSORY])->to_record();
 111          api::make_current($policy1->id);
 112          $policy2 = helper::add_policy(['optional' => policy_version::AGREEMENT_OPTIONAL])->to_record();
 113          api::make_current($policy2->id);
 114  
 115          $user1 = $this->getDataGenerator()->create_user();
 116          $this->assertEquals(0, $DB->get_field('user', 'policyagreed', ['id' => $user1->id]));
 117          $this->assertEmpty($DB->get_records('tool_policy_acceptances', ['userid' => $user1->id]));
 118  
 119          $this->setUser($user1->id);
 120          $this->assertEquals(0, $USER->policyagreed);
 121  
 122          // Only the compulsory policy is marked as accepted when accepting via the handler.
 123          $this->assertTrue(handler::accept());
 124          $this->assertEquals(1, $DB->get_field('user', 'policyagreed', ['id' => $user1->id]));
 125          $this->assertEquals(1, $USER->policyagreed);
 126          $this->assertEquals(1, $DB->count_records('tool_policy_acceptances', ['userid' => $user1->id]));
 127          $this->assertTrue($DB->record_exists('tool_policy_acceptances', ['userid' => $user1->id,
 128              'policyversionid' => $policy1->id]));
 129      }
 130  
 131      /**
 132       * Test presence of the {@link \tool_policy\privacy\local\sitepolicy\handler::signup_form()} method.
 133       */
 134      public function test_signup_form() {
 135          $this->assertTrue(method_exists('\tool_policy\privacy\local\sitepolicy\handler', 'signup_form'));
 136      }
 137  }