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 * Unit tests for the privacy legacy polyfill for mod_assign.
18 *
19 * @package     mod_assign
20 * @category    test
21 * @copyright   2018 Adrian Greeve <adriangreeve.com>
22 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
28require_once($CFG->dirroot . '/mod/assign/feedbackplugin.php');
29require_once($CFG->dirroot . '/mod/assign/feedback/comments/locallib.php');
30
31/**
32 * Unit tests for the assignment feedback subplugins API's privacy legacy_polyfill.
33 *
34 * @copyright   2018 Adrian Greeve <adriangreeve.com>
35 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class mod_assignfeedback_privacy_legacy_polyfill_test extends advanced_testcase {
38
39    /**
40     * Convenience function to create an instance of an assignment.
41     *
42     * @param array $params Array of parameters to pass to the generator
43     * @return assign The assign class.
44     */
45    protected function create_instance($params = array()) {
46        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
47        $instance = $generator->create_instance($params);
48        $cm = get_coursemodule_from_instance('assign', $instance->id);
49        $context = \context_module::instance($cm->id);
50        return new \assign($context, $cm, $params['course']);
51    }
52
53    /**
54     * Test the get_context_for_userid_within_feedback shim.
55     */
56    public function test_get_context_for_userid_within_feedback() {
57        $userid = 21;
58        $contextlist = new \core_privacy\local\request\contextlist();
59        $mock = $this->createMock(test_assignfeedback_legacy_polyfill_mock_wrapper::class);
60        $mock->expects($this->once())
61            ->method('get_return_value')
62            ->with('_get_context_for_userid_within_feedback', [$userid, $contextlist]);
63        test_legacy_polyfill_feedback_provider::$mock = $mock;
64        test_legacy_polyfill_feedback_provider::get_context_for_userid_within_feedback($userid, $contextlist);
65    }
66
67    /**
68     * Test the get_student_user_ids shim.
69     */
70    public function test_get_student_user_ids() {
71        $teacherid = 107;
72        $assignid = 15;
73        $useridlist = new \mod_assign\privacy\useridlist($teacherid, $assignid);
74        $mock = $this->createMock(test_assignfeedback_legacy_polyfill_mock_wrapper::class);
75        $mock->expects($this->once())
76            ->method('get_return_value')
77            ->with('_get_student_user_ids', [$useridlist]);
78        test_legacy_polyfill_feedback_provider::$mock = $mock;
79        test_legacy_polyfill_feedback_provider::get_student_user_ids($useridlist);
80    }
81
82    /**
83     * Test the export_feedback_user_data shim.
84     */
85    public function test_export_feedback_user_data() {
86        $this->resetAfterTest();
87        $course = $this->getDataGenerator()->create_course();
88        $assign = $this->create_instance(['course' => $course]);
89        $context = context_system::instance();
90        $subplugin = new assign_feedback_comments($assign, 'comments');
91        $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context,$assign);
92        $mock = $this->createMock(test_assignfeedback_legacy_polyfill_mock_wrapper::class);
93        $mock->expects($this->once())
94            ->method('get_return_value')
95            ->with('_export_feedback_user_data', [$requestdata]);
96        test_legacy_polyfill_feedback_provider::$mock = $mock;
97        test_legacy_polyfill_feedback_provider::export_feedback_user_data($requestdata);
98    }
99
100    /**
101     * Test the delete_feedback_for_context shim.
102     */
103    public function test_delete_feedback_for_context() {
104        $this->resetAfterTest();
105        $course = $this->getDataGenerator()->create_course();
106        $assign = $this->create_instance(['course' => $course]);
107        $context = context_system::instance();
108        $subplugin = new assign_feedback_comments($assign, 'comments');
109        $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context,$assign);
110        $mock = $this->createMock(test_assignfeedback_legacy_polyfill_mock_wrapper::class);
111        $mock->expects($this->once())
112            ->method('get_return_value')
113            ->with('_delete_feedback_for_context', [$requestdata]);
114        test_legacy_polyfill_feedback_provider::$mock = $mock;
115        test_legacy_polyfill_feedback_provider::delete_feedback_for_context($requestdata);
116    }
117
118    /**
119     * Test the delete feedback for grade shim.
120     */
121    public function test_delete_feedback_for_grade() {
122        $this->resetAfterTest();
123        $course = $this->getDataGenerator()->create_course();
124        $assign = $this->create_instance(['course' => $course]);
125        $context = context_system::instance();
126        $subplugin = new assign_feedback_comments($assign, 'comments');
127        $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context,$assign);
128        $mock = $this->createMock(test_assignfeedback_legacy_polyfill_mock_wrapper::class);
129        $mock->expects($this->once())
130            ->method('get_return_value')
131            ->with('_delete_feedback_for_grade', [$requestdata]);
132        test_legacy_polyfill_feedback_provider::$mock = $mock;
133        test_legacy_polyfill_feedback_provider::delete_feedback_for_grade($requestdata);
134    }
135}
136/**
137 * Legacy polyfill test class for the assignfeedback_provider.
138 *
139 * @copyright   2018 Adrian Greeve <adriangreeve.com>
140 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
141 */
142class test_legacy_polyfill_feedback_provider implements \mod_assign\privacy\assignfeedback_provider {
143    use \mod_assign\privacy\feedback_legacy_polyfill;
144    /**
145     * @var test_legacy_polyfill_feedback_provider $mock.
146     */
147    public static $mock = null;
148
149    /**
150     * Retrieves the contextids associated with the provided userid for this subplugin.
151     * NOTE if your subplugin must have an entry in the assign_grade table to work, then this
152     * method can be empty.
153     *
154     * @param  int $userid The user ID to get context IDs for.
155     * @param  contextlist $contextlist Use add_from_sql with this object to add your context IDs.
156     */
157    public static function _get_context_for_userid_within_feedback(int $userid,
158            \core_privacy\local\request\contextlist $contextlist) {
159        static::$mock->get_return_value(__FUNCTION__, func_get_args());
160    }
161
162    /**
163     * Returns student user ids related to the provided teacher ID. If an entry must be present in the assign_grade table for
164     * your plugin to work then there is no need to fill in this method. If you filled in get_context_for_userid_within_feedback()
165     * then you probably have to fill this in as well.
166     *
167     * @param  useridlist $useridlist A list of user IDs of students graded by this user.
168     */
169    public static function _get_student_user_ids(\mod_assign\privacy\useridlist $useridlist) {
170        static::$mock->get_return_value(__FUNCTION__, func_get_args());
171    }
172
173    /**
174     * Export feedback data with the available grade and userid information provided.
175     * assign_plugin_request_data contains:
176     * - context
177     * - grade object
178     * - current path (subcontext)
179     * - user object
180     *
181     * @param  assign_plugin_request_data $exportdata Contains data to help export the user information.
182     */
183    public static function _export_feedback_user_data(\mod_assign\privacy\assign_plugin_request_data $exportdata) {
184        static::$mock->get_return_value(__FUNCTION__, func_get_args());
185    }
186
187    /**
188     * Any call to this method should delete all user data for the context defined in the deletion_criteria.
189     * assign_plugin_request_data contains:
190     * - context
191     * - assign object
192     *
193     * @param  assign_plugin_request_data $requestdata Data useful for deleting user data from this sub-plugin.
194     */
195    public static function _delete_feedback_for_context(\mod_assign\privacy\assign_plugin_request_data $requestdata) {
196        static::$mock->get_return_value(__FUNCTION__, func_get_args());
197    }
198
199    /**
200     * Calling this function should delete all user data associated with this grade.
201     * assign_plugin_request_data contains:
202     * - context
203     * - grade object
204     * - user object
205     * - assign object
206     *
207     * @param  assign_plugin_request_data $requestdata Data useful for deleting user data.
208     */
209    public static function _delete_feedback_for_grade(\mod_assign\privacy\assign_plugin_request_data $requestdata) {
210        static::$mock->get_return_value(__FUNCTION__, func_get_args());
211    }
212}
213/**
214 * Called inside the polyfill methods in the test polyfill provider, allowing us to ensure these are called with correct params.
215 *
216 * @copyright   2018 Adrian Greeve <adriangreeve.com>
217 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
218 */
219class test_assignfeedback_legacy_polyfill_mock_wrapper {
220    /**
221     * Get the return value for the specified item.
222     */
223    public function get_return_value() {
224    }
225}
226