Moodle release 4.4beta
[moodle.git] / auth / tests / digital_consent_test.php
blob8ad2d411b1591e6a4037ad890f02d00c35057492
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.
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/>.
17 namespace core_auth;
19 /**
20 * Digital consent helper testcase.
22 * @package core_auth
23 * @copyright 2018 Mihail Geshoski
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 class digital_consent_test extends \advanced_testcase {
28 public function test_is_age_digital_consent_verification_enabled() {
29 global $CFG;
30 $this->resetAfterTest();
32 // Age of digital consent verification is enabled.
33 $CFG->agedigitalconsentverification = 0;
35 $isenabled = \core_auth\digital_consent::is_age_digital_consent_verification_enabled();
36 $this->assertFalse($isenabled);
39 public function test_is_minor() {
40 global $CFG;
41 $this->resetAfterTest();
43 $agedigitalconsentmap = implode(PHP_EOL, [
44 '*, 16',
45 'AT, 14',
46 'CZ, 13',
47 'DE, 14',
48 'DK, 13',
49 ]);
50 $CFG->agedigitalconsentmap = $agedigitalconsentmap;
52 $usercountry1 = 'DK';
53 $usercountry2 = 'AU';
54 $userage1 = 12;
55 $userage2 = 14;
56 $userage3 = 16;
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);
72 public function test_parse_age_digital_consent_map_valid_format() {
74 // Value of agedigitalconsentmap has a valid format.
75 $agedigitalconsentmap = implode(PHP_EOL, [
76 '*, 16',
77 'AT, 14',
78 'BE, 13'
79 ]);
81 $ageconsentmapparsed = \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
83 $this->assertEquals([
84 '*' => 16,
85 'AT' => 14,
86 'BE' => 13
87 ], $ageconsentmapparsed
91 public function test_parse_age_digital_consent_map_invalid_format_missing_spaces() {
93 // Value of agedigitalconsentmap has an invalid format (missing space separator between values).
94 $agedigitalconsentmap = implode(PHP_EOL, [
95 '*, 16',
96 'AT14',
97 ]);
99 $this->expectException('moodle_exception');
100 $this->expectExceptionMessage(get_string('agedigitalconsentmapinvalidcomma', 'error', 'AT14'));
102 \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
105 public function test_parse_age_digital_consent_map_invalid_format_missing_default_value() {
107 // Value of agedigitalconsentmap has an invalid format (missing default value).
108 $agedigitalconsentmap = implode(PHP_EOL, [
109 'BE, 16',
110 'AT, 14'
113 $this->expectException('moodle_exception');
114 $this->expectExceptionMessage(get_string('agedigitalconsentmapinvaliddefault', 'error'));
116 \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
119 public function test_parse_age_digital_consent_map_invalid_format_invalid_country() {
121 // Value of agedigitalconsentmap has an invalid format (invalid value for country).
122 $agedigitalconsentmap = implode(PHP_EOL, [
123 '*, 16',
124 'TEST, 14'
127 $this->expectException('moodle_exception');
128 $this->expectExceptionMessage(get_string('agedigitalconsentmapinvalidcountry', 'error', 'TEST'));
130 \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
133 public function test_parse_age_digital_consent_map_invalid_format_invalid_age_string() {
135 // Value of agedigitalconsentmap has an invalid format (string value for age).
136 $agedigitalconsentmap = implode(PHP_EOL, [
137 '*, 16',
138 'AT, ten'
141 $this->expectException('moodle_exception');
142 $this->expectExceptionMessage(get_string('agedigitalconsentmapinvalidage', 'error', 'ten'));
144 \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
147 public function test_parse_age_digital_consent_map_invalid_format_missing_age() {
149 // Value of agedigitalconsentmap has an invalid format (missing value for age).
150 $agedigitalconsentmap = implode(PHP_EOL, [
151 '*, 16',
152 'AT, '
155 $this->expectException('moodle_exception');
156 $this->expectExceptionMessage(get_string('agedigitalconsentmapinvalidage', 'error', ''));
158 \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
161 public function test_parse_age_digital_consent_map_invalid_format_missing_country() {
163 // Value of agedigitalconsentmap has an invalid format (missing value for country).
164 $agedigitalconsentmap = implode(PHP_EOL, [
165 '*, 16',
166 ', 12'
169 $this->expectException('moodle_exception');
170 $this->expectExceptionMessage(get_string('agedigitalconsentmapinvalidcountry', 'error', ''));
172 \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);