on-demand release 4.0dev+
[moodle.git] / question / type / ddimageortext / 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_ddimageortext
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_ddimageortext\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/ddimageortext/classes/privacy/provider.php');
35
36/**
37 * Privacy provider tests class.
38 *
39 * @package qtype_ddimageortext
40 * @copyright 2021 The Open university
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
43class qtype_ddimageortext_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_ddimageortext');
48 $actual = \qtype_ddimageortext\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_ddimageortext_$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_ddimageortext');
77 foreach ($preferences as $key => $pref) {
78 $preference = get_user_preferences("qtype_ddimageortext_{$key}", null, $user->id);
79 if ($preference === null) {
80 continue;
81 }
82 $desc = get_string("privacy:preference:{$key}", 'qtype_ddimageortext');
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 1' => ['defaultmark', 1, 1],
96 'penalty 33.33333%' => ['penalty', 0.3333333, '33.33333%'],
97 'shuffle yes' => ['shuffleanswers', 1, 'Yes'],
98 'shuffle no' => ['shuffleanswers', 0, 'No']
99 ];
100 }
101}