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   * Unit tests for core_auth\digital_consent.
  19   *
  20   * @package    core_auth
  21   * @copyright  2018 Mihail Geshoski
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Digital consent helper testcase.
  29   *
  30   * @package    core_auth
  31   * @copyright  2018 Mihail Geshoski
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class core_auth_digital_consent_testcase extends advanced_testcase {
  35  
  36      public function test_is_age_digital_consent_verification_enabled() {
  37          global $CFG;
  38          $this->resetAfterTest();
  39  
  40          // Age of digital consent verification is enabled.
  41          $CFG->agedigitalconsentverification = 0;
  42  
  43          $isenabled = \core_auth\digital_consent::is_age_digital_consent_verification_enabled();
  44          $this->assertFalse($isenabled);
  45      }
  46  
  47      public function test_is_minor() {
  48          global $CFG;
  49          $this->resetAfterTest();
  50  
  51          $agedigitalconsentmap = implode(PHP_EOL, [
  52              '*, 16',
  53              'AT, 14',
  54              'CZ, 13',
  55              'DE, 14',
  56              'DK, 13',
  57          ]);
  58          $CFG->agedigitalconsentmap = $agedigitalconsentmap;
  59  
  60          $usercountry1 = 'DK';
  61          $usercountry2 = 'AU';
  62          $userage1 = 12;
  63          $userage2 = 14;
  64          $userage3 = 16;
  65  
  66          // Test country exists in agedigitalconsentmap and user age is below the particular digital minor age.
  67          $isminor = \core_auth\digital_consent::is_minor($userage1, $usercountry1);
  68          $this->assertTrue($isminor);
  69          // Test country exists in agedigitalconsentmap and user age is above the particular digital minor age.
  70          $isminor = \core_auth\digital_consent::is_minor($userage2, $usercountry1);
  71          $this->assertFalse($isminor);
  72          // Test country does not exists in agedigitalconsentmap and user age is below the particular digital minor age.
  73          $isminor = \core_auth\digital_consent::is_minor($userage2, $usercountry2);
  74          $this->assertTrue($isminor);
  75          // Test country does not exists in agedigitalconsentmap and user age is above the particular digital minor age.
  76          $isminor = \core_auth\digital_consent::is_minor($userage3, $usercountry2);
  77          $this->assertFalse($isminor);
  78      }
  79  
  80      public function test_parse_age_digital_consent_map_valid_format() {
  81  
  82          // Value of agedigitalconsentmap has a valid format.
  83          $agedigitalconsentmap = implode(PHP_EOL, [
  84              '*, 16',
  85              'AT, 14',
  86              'BE, 13'
  87          ]);
  88  
  89          $ageconsentmapparsed = \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
  90  
  91          $this->assertEquals([
  92              '*' => 16,
  93              'AT' => 14,
  94              'BE' => 13
  95          ], $ageconsentmapparsed
  96          );
  97      }
  98  
  99      public function test_parse_age_digital_consent_map_invalid_format_missing_spaces() {
 100  
 101          // Value of agedigitalconsentmap has an invalid format (missing space separator between values).
 102          $agedigitalconsentmap = implode(PHP_EOL, [
 103              '*, 16',
 104              'AT14',
 105          ]);
 106  
 107          $this->expectException('moodle_exception');
 108          $this->expectExceptionMessage(get_string('agedigitalconsentmapinvalidcomma', 'error', 'AT14'));
 109  
 110          \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
 111      }
 112  
 113      public function test_parse_age_digital_consent_map_invalid_format_missing_default_value() {
 114  
 115          // Value of agedigitalconsentmap has an invalid format (missing default value).
 116          $agedigitalconsentmap = implode(PHP_EOL, [
 117              'BE, 16',
 118              'AT, 14'
 119          ]);
 120  
 121          $this->expectException('moodle_exception');
 122          $this->expectExceptionMessage(get_string('agedigitalconsentmapinvaliddefault', 'error'));
 123  
 124          \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
 125      }
 126  
 127      public function test_parse_age_digital_consent_map_invalid_format_invalid_country() {
 128  
 129          // Value of agedigitalconsentmap has an invalid format (invalid value for country).
 130          $agedigitalconsentmap = implode(PHP_EOL, [
 131              '*, 16',
 132              'TEST, 14'
 133          ]);
 134  
 135          $this->expectException('moodle_exception');
 136          $this->expectExceptionMessage(get_string('agedigitalconsentmapinvalidcountry', 'error', 'TEST'));
 137  
 138          \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
 139      }
 140  
 141      public function test_parse_age_digital_consent_map_invalid_format_invalid_age_string() {
 142  
 143          // Value of agedigitalconsentmap has an invalid format (string value for age).
 144          $agedigitalconsentmap = implode(PHP_EOL, [
 145              '*, 16',
 146              'AT, ten'
 147          ]);
 148  
 149          $this->expectException('moodle_exception');
 150          $this->expectExceptionMessage(get_string('agedigitalconsentmapinvalidage', 'error', 'ten'));
 151  
 152          \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
 153      }
 154  
 155      public function test_parse_age_digital_consent_map_invalid_format_missing_age() {
 156  
 157          // Value of agedigitalconsentmap has an invalid format (missing value for age).
 158          $agedigitalconsentmap = implode(PHP_EOL, [
 159              '*, 16',
 160              'AT, '
 161          ]);
 162  
 163          $this->expectException('moodle_exception');
 164          $this->expectExceptionMessage(get_string('agedigitalconsentmapinvalidage', 'error', ''));
 165  
 166          \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
 167      }
 168  
 169      public function test_parse_age_digital_consent_map_invalid_format_missing_country() {
 170  
 171          // Value of agedigitalconsentmap has an invalid format (missing value for country).
 172          $agedigitalconsentmap = implode(PHP_EOL, [
 173              '*, 16',
 174              ', 12'
 175          ]);
 176  
 177          $this->expectException('moodle_exception');
 178          $this->expectExceptionMessage(get_string('agedigitalconsentmapinvalidcountry', 'error', ''));
 179  
 180          \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
 181      }
 182  }