MDL-77564 Quiz display options: Hide or show the grade information
[moodle.git] / mod / quiz / tests / privacy_legacy_quizaccess_polyfill_test.php
blobf34b01a04583dccad66ea3d7dd3ba4471af7ff3c
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 the privacy legacy polyfill for quiz access rules.
20 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace mod_quiz;
26 /**
27 * Unit tests for the privacy legacy polyfill for quiz access rules.
29 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 class privacy_legacy_quizaccess_polyfill_test extends \advanced_testcase {
33 /**
34 * Test that the core_quizaccess\privacy\legacy_polyfill works and that the static _export_quizaccess_user_data can
35 * be called.
37 public function test_export_quizaccess_user_data() {
38 $quiz = $this->createMock(quiz_settings::class);
39 $user = (object) [];
40 $returnvalue = (object) [];
42 $mock = $this->createMock(test_privacy_legacy_quizaccess_polyfill_mock_wrapper::class);
43 $mock->expects($this->once())
44 ->method('get_return_value')
45 ->with('_export_quizaccess_user_data', [$quiz, $user])
46 ->willReturn($returnvalue);
48 test_privacy_legacy_quizaccess_polyfill_provider::$mock = $mock;
49 $result = test_privacy_legacy_quizaccess_polyfill_provider::export_quizaccess_user_data($quiz, $user);
50 $this->assertSame($returnvalue, $result);
53 /**
54 * Test the _delete_quizaccess_for_context shim.
56 public function test_delete_quizaccess_for_context() {
57 $context = \context_system::instance();
59 $quiz = $this->createMock(quiz_settings::class);
61 $mock = $this->createMock(test_privacy_legacy_quizaccess_polyfill_mock_wrapper::class);
62 $mock->expects($this->once())
63 ->method('get_return_value')
64 ->with('_delete_quizaccess_data_for_all_users_in_context', [$quiz]);
66 test_privacy_legacy_quizaccess_polyfill_provider::$mock = $mock;
67 test_privacy_legacy_quizaccess_polyfill_provider::delete_quizaccess_data_for_all_users_in_context($quiz);
70 /**
71 * Test the _delete_quizaccess_for_user shim.
73 public function test_delete_quizaccess_for_user() {
74 $context = \context_system::instance();
76 $quiz = $this->createMock(quiz_settings::class);
77 $user = (object) [];
79 $mock = $this->createMock(test_privacy_legacy_quizaccess_polyfill_mock_wrapper::class);
80 $mock->expects($this->once())
81 ->method('get_return_value')
82 ->with('_delete_quizaccess_data_for_user', [$quiz, $user]);
84 test_privacy_legacy_quizaccess_polyfill_provider::$mock = $mock;
85 test_privacy_legacy_quizaccess_polyfill_provider::delete_quizaccess_data_for_user($quiz, $user);
88 /**
89 * Test the _delete_quizaccess_for_users shim.
91 public function test_delete_quizaccess_for_users() {
92 $context = $this->createMock(\context_module::class);
93 $user = (object) [];
94 $approveduserlist = new \core_privacy\local\request\approved_userlist($context, 'mod_quiz', [$user]);
96 $mock = $this->createMock(test_privacy_legacy_quizaccess_polyfill_mock_wrapper::class);
97 $mock->expects($this->once())
98 ->method('get_return_value')
99 ->with('_delete_quizaccess_data_for_users', [$approveduserlist]);
101 test_privacy_legacy_quizaccess_polyfill_provider::$mock = $mock;
102 test_privacy_legacy_quizaccess_polyfill_provider::delete_quizaccess_data_for_users($approveduserlist);
107 * Legacy polyfill test class for the quizaccess_provider.
109 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
110 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
112 class test_privacy_legacy_quizaccess_polyfill_provider implements
113 \core_privacy\local\metadata\provider,
114 \mod_quiz\privacy\quizaccess_provider,
115 \mod_quiz\privacy\quizaccess_user_provider {
117 use \mod_quiz\privacy\legacy_quizaccess_polyfill;
118 use \core_privacy\local\legacy_polyfill;
121 * @var test_privacy_legacy_quizaccess_polyfill_provider $mock.
123 public static $mock = null;
126 * Export all user data for the quizaccess plugin.
128 * @param \mod_quiz\quiz_settings $quiz
129 * @param \stdClass $user
131 protected static function _export_quizaccess_user_data($quiz, $user) {
132 return static::$mock->get_return_value(__FUNCTION__, func_get_args());
136 * Deletes all user data for the given context.
138 * @param \mod_quiz\quiz_settings $quiz
140 protected static function _delete_quizaccess_data_for_all_users_in_context($quiz) {
141 static::$mock->get_return_value(__FUNCTION__, func_get_args());
145 * Delete personal data for the given user and context.
147 * @param \mod_quiz\quiz_settings $quiz The quiz being deleted
148 * @param \stdClass $user The user to export data for
150 protected static function _delete_quizaccess_data_for_user($quiz, $user) {
151 static::$mock->get_return_value(__FUNCTION__, func_get_args());
155 * Delete all user data for the specified users, in the specified context.
157 * @param \core_privacy\local\request\approved_userlist $userlist
159 protected static function _delete_quizaccess_data_for_users($userlist) {
160 static::$mock->get_return_value(__FUNCTION__, func_get_args());
164 * Returns metadata about this plugin.
166 * @param \core_privacy\local\metadata\collection $collection The initialised collection to add items to.
167 * @return \core_privacy\local\metadata\collection A listing of user data stored through this system.
169 protected static function _get_metadata(\core_privacy\local\metadata\collection $collection) {
170 return $collection;
175 * Called inside the polyfill methods in the test polyfill provider, allowing us to ensure these are called with correct params.
177 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
178 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
180 class test_privacy_legacy_quizaccess_polyfill_mock_wrapper {
182 * Get the return value for the specified item.
184 public function get_return_value() {