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 |
/**
|
|
|
18 |
* Privacy provider tests.
|
|
|
19 |
*
|
|
|
20 |
* @package qtype_essay
|
|
|
21 |
* @copyright 2021 The Open university
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
namespace qtype_essay\privacy;
|
|
|
25 |
|
|
|
26 |
use core_privacy\local\metadata\collection;
|
|
|
27 |
use core_privacy\local\request\user_preference_provider;
|
|
|
28 |
use qtype_essay\privacy\provider;
|
|
|
29 |
use core_privacy\local\request\writer;
|
|
|
30 |
use core_privacy\local\request\transform;
|
|
|
31 |
|
|
|
32 |
defined('MOODLE_INTERNAL') || die();
|
|
|
33 |
|
|
|
34 |
global $CFG;
|
|
|
35 |
require_once($CFG->dirroot . '/question/type/essay/classes/privacy/provider.php');
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Privacy provider tests class.
|
|
|
39 |
*
|
|
|
40 |
* @package qtype_essay
|
|
|
41 |
* @copyright 2021 The Open university
|
|
|
42 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
43 |
*/
|
1441 |
ariadna |
44 |
final class provider_test extends \core_privacy\tests\provider_testcase {
|
1 |
efrain |
45 |
// Include the privacy helper which has assertions on it.
|
|
|
46 |
|
11 |
efrain |
47 |
public function test_get_metadata(): void {
|
1 |
efrain |
48 |
$collection = new \core_privacy\local\metadata\collection('qtype_essay');
|
|
|
49 |
$actual = \qtype_essay\privacy\provider::get_metadata($collection);
|
|
|
50 |
$this->assertEquals($collection, $actual);
|
|
|
51 |
}
|
|
|
52 |
|
11 |
efrain |
53 |
public function test_export_user_preferences_no_pref(): void {
|
1 |
efrain |
54 |
$this->resetAfterTest();
|
|
|
55 |
|
|
|
56 |
$user = $this->getDataGenerator()->create_user();
|
|
|
57 |
provider::export_user_preferences($user->id);
|
|
|
58 |
$writer = writer::with_context(\context_system::instance());
|
|
|
59 |
$this->assertFalse($writer->has_any_data());
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Test the export_user_preferences given different inputs
|
|
|
64 |
* @dataProvider user_preference_provider
|
|
|
65 |
|
|
|
66 |
* @param string $name The name of the user preference to get/set
|
|
|
67 |
* @param string $value The value stored in the database
|
|
|
68 |
* @param string $expected The expected transformed value
|
|
|
69 |
*/
|
11 |
efrain |
70 |
public function test_export_user_preferences($name, $value, $expected): void {
|
1 |
efrain |
71 |
$this->resetAfterTest();
|
|
|
72 |
$user = $this->getDataGenerator()->create_user();
|
|
|
73 |
set_user_preference("qtype_essay_$name", $value, $user);
|
|
|
74 |
provider::export_user_preferences($user->id);
|
|
|
75 |
$writer = writer::with_context(\context_system::instance());
|
|
|
76 |
$this->assertTrue($writer->has_any_data());
|
|
|
77 |
$preferences = $writer->get_user_preferences('qtype_essay');
|
|
|
78 |
foreach ($preferences as $key => $pref) {
|
|
|
79 |
$preference = get_user_preferences("qtype_essay_{$key}", null, $user->id);
|
|
|
80 |
if ($preference === null) {
|
|
|
81 |
continue;
|
|
|
82 |
}
|
|
|
83 |
$desc = get_string("privacy:preference:{$key}", 'qtype_essay');
|
|
|
84 |
$this->assertEquals($expected, $pref->value);
|
|
|
85 |
$this->assertEquals($desc, $pref->description);
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Create an array of valid user preferences for the multiple choice question type.
|
|
|
91 |
*
|
|
|
92 |
* @return array Array of valid user preferences.
|
|
|
93 |
*/
|
1441 |
ariadna |
94 |
public static function user_preference_provider(): array {
|
1 |
efrain |
95 |
return [
|
|
|
96 |
'default mark 2' => ['defaultmark', 2, 2],
|
|
|
97 |
'responseformat editror ' => ['responseformat', 'editor', get_string('formateditor', 'qtype_essay')],
|
|
|
98 |
'responseformat editor and filepicker ' =>
|
|
|
99 |
['responseformat', 'editorfilepicker', get_string('formateditorfilepicker', 'qtype_essay')],
|
|
|
100 |
'responseformat plain ' => ['responseformat', 'plain', get_string('formatplain', 'qtype_essay')],
|
|
|
101 |
'responseformat monospaced ' => ['responseformat', 'monospaced', get_string('formatmonospaced', 'qtype_essay')],
|
|
|
102 |
'responseformat noinline ' => ['responseformat', 'noinline', get_string('formatnoinline', 'qtype_essay')],
|
|
|
103 |
'responserequired yes' => ['responserequired', 1, get_string('responseisrequired', 'qtype_essay')],
|
|
|
104 |
'responserequired no ' => ['responserequired', 0, get_string('responsenotrequired', 'qtype_essay')],
|
|
|
105 |
'responsefieldlines 10' => ['responsefieldlines', 10, '10 lines'],
|
|
|
106 |
'attachments none' => ['attachments', 0, get_string('no')],
|
|
|
107 |
'attachments 3' => ['attachments', 3, '3'],
|
|
|
108 |
'attachments unlimited' => ['attachments', -1, get_string('unlimited')],
|
|
|
109 |
'attachmentsrequired optional' => ['attachmentsrequired', 0, get_string('attachmentsoptional', 'qtype_essay')],
|
|
|
110 |
'attachmentsrequired 1' => ['attachmentsrequired', 1, '1'],
|
|
|
111 |
'maxbytes 50KB' => ['maxbytes', 51200, '50KB']
|
|
|
112 |
];
|
|
|
113 |
}
|
|
|
114 |
}
|