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