Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

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