2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
20 * Digital consent helper testcase.
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() {
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() {
41 $this->resetAfterTest();
43 $agedigitalconsentmap = implode(PHP_EOL
, [
50 $CFG->agedigitalconsentmap
= $agedigitalconsentmap;
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
, [
81 $ageconsentmapparsed = \core_auth\digital_consent
::parse_age_digital_consent_map($agedigitalconsentmap);
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
, [
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
, [
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
, [
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
, [
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
, [
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
, [
169 $this->expectException('moodle_exception');
170 $this->expectExceptionMessage(get_string('agedigitalconsentmapinvalidcountry', 'error', ''));
172 \core_auth\digital_consent
::parse_age_digital_consent_map($agedigitalconsentmap);