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 */
24defined('MOODLE_INTERNAL') || die();
25
26global $CFG;
27require_once($CFG->dirroot . '/mod/assign/submissionplugin.php');
28require_once($CFG->dirroot . '/mod/assign/submission/comments/locallib.php');
29
30/**
31 * Unit tests for the assignment submission subplugins API's privacy legacy_polyfill.
32 *
33 * @copyright   2018 Adrian Greeve <adriangreeve.com>
34 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class mod_assignsubmission_privacy_legacy_polyfill_test extends advanced_testcase {
37
38    /**
39     * Convenience function to create an instance of an assignment.
40     *
41     * @param array $params Array of parameters to pass to the generator
42     * @return assign The assign class.
43     */
44    protected function create_instance($params = array()) {
45        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
46        $instance = $generator->create_instance($params);
47        $cm = get_coursemodule_from_instance('assign', $instance->id);
48        $context = \context_module::instance($cm->id);
49        return new \assign($context, $cm, $params['course']);
50    }
51
52    /**
53     * Test the get_context_for_userid_within_submission shim.
54     */
55    public function test_get_context_for_userid_within_submission() {
56        $userid = 21;
57        $contextlist = new \core_privacy\local\request\contextlist();
58        $mock = $this->createMock(test_assignsubmission_legacy_polyfill_mock_wrapper::class);
59        $mock->expects($this->once())
60            ->method('get_return_value')
61            ->with('_get_context_for_userid_within_submission', [$userid, $contextlist]);
62        test_legacy_polyfill_submission_provider::$mock = $mock;
63        test_legacy_polyfill_submission_provider::get_context_for_userid_within_submission($userid, $contextlist);
64    }
65
66    /**
67     * Test the get_student_user_ids shim.
68     */
69    public function test_get_student_user_ids() {
70        $teacherid = 107;
71        $assignid = 15;
72        $useridlist = new \mod_assign\privacy\useridlist($teacherid, $assignid);
73        $mock = $this->createMock(test_assignsubmission_legacy_polyfill_mock_wrapper::class);
74        $mock->expects($this->once())
75            ->method('get_return_value')
76            ->with('_get_student_user_ids', [$useridlist]);
77        test_legacy_polyfill_submission_provider::$mock = $mock;
78        test_legacy_polyfill_submission_provider::get_student_user_ids($useridlist);
79    }
80
81    /**
82     * Test the export_submission_user_data shim.
83     */
84    public function test_export_submission_user_data() {
85        $this->resetAfterTest();
86        $course = $this->getDataGenerator()->create_course();
87        $assign = $this->create_instance(['course' => $course]);
88        $context = context_system::instance();
89        $subplugin = new assign_submission_comments($assign, 'comment');
90        $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
91        $mock = $this->createMock(test_assignsubmission_legacy_polyfill_mock_wrapper::class);
92        $mock->expects($this->once())
93            ->method('get_return_value')
94            ->with('_export_submission_user_data', [$requestdata]);
95        test_legacy_polyfill_submission_provider::$mock = $mock;
96        test_legacy_polyfill_submission_provider::export_submission_user_data($requestdata);
97    }
98
99    /**
100     * Test the delete_submission_for_context shim.
101     */
102    public function test_delete_submission_for_context() {
103        $this->resetAfterTest();
104        $course = $this->getDataGenerator()->create_course();
105        $assign = $this->create_instance(['course' => $course]);
106        $context = context_system::instance();
107        $subplugin = new assign_submission_comments($assign, 'comment');
108        $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
109        $mock = $this->createMock(test_assignsubmission_legacy_polyfill_mock_wrapper::class);
110        $mock->expects($this->once())
111            ->method('get_return_value')
112            ->with('_delete_submission_for_context', [$requestdata]);
113        test_legacy_polyfill_submission_provider::$mock = $mock;
114        test_legacy_polyfill_submission_provider::delete_submission_for_context($requestdata);
115    }
116
117    /**
118     * Test the delete submission for grade shim.
119     */
120    public function test_delete_submission_for_userid() {
121        $this->resetAfterTest();
122        $course = $this->getDataGenerator()->create_course();
123        $assign = $this->create_instance(['course' => $course]);
124        $context = context_system::instance();
125        $subplugin = new assign_submission_comments($assign, 'comment');
126        $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
127        $mock = $this->createMock(test_assignsubmission_legacy_polyfill_mock_wrapper::class);
128        $mock->expects($this->once())
129            ->method('get_return_value')
130            ->with('_delete_submission_for_userid', [$requestdata]);
131        test_legacy_polyfill_submission_provider::$mock = $mock;
132        test_legacy_polyfill_submission_provider::delete_submission_for_userid($requestdata);
133    }
134}
135/**
136 * Legacy polyfill test class for the assignsubmission_provider.
137 *
138 * @copyright   2018 Adrian Greeve <adriangreeve.com>
139 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
140 */
141class test_legacy_polyfill_submission_provider implements \mod_assign\privacy\assignsubmission_provider {
142    use \mod_assign\privacy\submission_legacy_polyfill;
143    /**
144     * @var test_legacy_polyfill_submission_provider $mock.
145     */
146    public static $mock = null;
147
148    /**
149     * Retrieves the contextids associated with the provided userid for this subplugin.
150     * NOTE if your subplugin must have an entry in the assign_grade table to work, then this
151     * method can be empty.
152     *
153     * @param  int $userid The user ID to get context IDs for.
154     * @param  contextlist $contextlist Use add_from_sql with this object to add your context IDs.
155     */
156    public static function _get_context_for_userid_within_submission(int $userid,
157            \core_privacy\local\request\contextlist $contextlist) {
158        static::$mock->get_return_value(__FUNCTION__, func_get_args());
159    }
160
161    /**
162     * Returns student user ids related to the provided teacher ID. If it is possible that a student ID will not be returned by
163     * the sql query in \mod_assign\privacy\provider::find_grader_info() Then you need to provide some sql to retrive those
164     * student IDs. This is highly likely if you had to fill in get_context_for_userid_within_submission above.
165     *
166     * @param  useridlist $useridlist A list of user IDs of students graded by this user.
167     */
168    public static function _get_student_user_ids(\mod_assign\privacy\useridlist $useridlist) {
169        static::$mock->get_return_value(__FUNCTION__, func_get_args());
170    }
171
172    /**
173     * This method is used to export any user data this sub-plugin has using the assign_plugin_request_data object to get the
174     * context and userid.
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_submission_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_submission_for_context(\mod_assign\privacy\assign_plugin_request_data $requestdata) {
196        static::$mock->get_return_value(__FUNCTION__, func_get_args());
197    }
198
199    /**
200     * A call to this method should delete user data (where practicle) from the userid and context.
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_submission_for_userid(\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_assignsubmission_legacy_polyfill_mock_wrapper {
220    /**
221     * Get the return value for the specified item.
222     */
223    public function get_return_value() {
224    }
225}
226