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