MDL-73794 theme_boost: set footer background white
[moodle.git] / auth / tests / digital_consent_test.php
blobc9b2b90e10a54bec844c1720092b6d5d3e0ce339
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 /**
18 * Unit tests for core_auth\digital_consent.
20 * @package core_auth
21 * @copyright 2018 Mihail Geshoski
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Digital consent helper testcase.
30 * @package core_auth
31 * @copyright 2018 Mihail Geshoski
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class core_auth_digital_consent_testcase extends advanced_testcase {
36 public function test_is_age_digital_consent_verification_enabled() {
37 global $CFG;
38 $this->resetAfterTest();
40 // Age of digital consent verification is enabled.
41 $CFG->agedigitalconsentverification = 0;
43 $isenabled = \core_auth\digital_consent::is_age_digital_consent_verification_enabled();
44 $this->assertFalse($isenabled);
47 public function test_is_minor() {
48 global $CFG;
49 $this->resetAfterTest();
51 $agedigitalconsentmap = implode(PHP_EOL, [
52 '*, 16',
53 'AT, 14',
54 'CZ, 13',
55 'DE, 14',
56 'DK, 13',
57 ]);
58 $CFG->agedigitalconsentmap = $agedigitalconsentmap;
60 $usercountry1 = 'DK';
61 $usercountry2 = 'AU';
62 $userage1 = 12;
63 $userage2 = 14;
64 $userage3 = 16;
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);
80 public function test_parse_age_digital_consent_map_valid_format() {
82 // Value of agedigitalconsentmap has a valid format.
83 $agedigitalconsentmap = implode(PHP_EOL, [
84 '*, 16',
85 'AT, 14',
86 'BE, 13'
87 ]);
89 $ageconsentmapparsed = \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
91 $this->assertEquals([
92 '*' => 16,
93 'AT' => 14,
94 'BE' => 13
95 ], $ageconsentmapparsed
99 public function test_parse_age_digital_consent_map_invalid_format_missing_spaces() {
101 // Value of agedigitalconsentmap has an invalid format (missing space separator between values).
102 $agedigitalconsentmap = implode(PHP_EOL, [
103 '*, 16',
104 'AT14',
107 $this->expectException('moodle_exception');
108 $this->expectExceptionMessage(get_string('agedigitalconsentmapinvalidcomma', 'error', 'AT14'));
110 \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
113 public function test_parse_age_digital_consent_map_invalid_format_missing_default_value() {
115 // Value of agedigitalconsentmap has an invalid format (missing default value).
116 $agedigitalconsentmap = implode(PHP_EOL, [
117 'BE, 16',
118 'AT, 14'
121 $this->expectException('moodle_exception');
122 $this->expectExceptionMessage(get_string('agedigitalconsentmapinvaliddefault', 'error'));
124 \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
127 public function test_parse_age_digital_consent_map_invalid_format_invalid_country() {
129 // Value of agedigitalconsentmap has an invalid format (invalid value for country).
130 $agedigitalconsentmap = implode(PHP_EOL, [
131 '*, 16',
132 'TEST, 14'
135 $this->expectException('moodle_exception');
136 $this->expectExceptionMessage(get_string('agedigitalconsentmapinvalidcountry', 'error', 'TEST'));
138 \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
141 public function test_parse_age_digital_consent_map_invalid_format_invalid_age_string() {
143 // Value of agedigitalconsentmap has an invalid format (string value for age).
144 $agedigitalconsentmap = implode(PHP_EOL, [
145 '*, 16',
146 'AT, ten'
149 $this->expectException('moodle_exception');
150 $this->expectExceptionMessage(get_string('agedigitalconsentmapinvalidage', 'error', 'ten'));
152 \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
155 public function test_parse_age_digital_consent_map_invalid_format_missing_age() {
157 // Value of agedigitalconsentmap has an invalid format (missing value for age).
158 $agedigitalconsentmap = implode(PHP_EOL, [
159 '*, 16',
160 'AT, '
163 $this->expectException('moodle_exception');
164 $this->expectExceptionMessage(get_string('agedigitalconsentmapinvalidage', 'error', ''));
166 \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);
169 public function test_parse_age_digital_consent_map_invalid_format_missing_country() {
171 // Value of agedigitalconsentmap has an invalid format (missing value for country).
172 $agedigitalconsentmap = implode(PHP_EOL, [
173 '*, 16',
174 ', 12'
177 $this->expectException('moodle_exception');
178 $this->expectExceptionMessage(get_string('agedigitalconsentmapinvalidcountry', 'error', ''));
180 \core_auth\digital_consent::parse_age_digital_consent_map($agedigitalconsentmap);