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+ namespace mod_pdfannotator \privacy ;
18+
19+ use mod_pdfannotator \privacy \provider ;
20+ use core_privacy \local \request \approved_contextlist ;
21+ use core_privacy \local \request \approved_userlist ;
22+ use core_privacy \tests \provider_testcase ;
23+ use stdClass ;
24+
25+ defined ('MOODLE_INTERNAL ' ) || die ();
26+
27+ require_once (__DIR__ . '/../fixtures/test_indicator_max.php ' );
28+ require_once (__DIR__ . '/../fixtures/test_indicator_min.php ' );
29+ require_once (__DIR__ . '/../fixtures/test_target_site_users.php ' );
30+ require_once (__DIR__ . '/../fixtures/test_target_course_users.php ' );
31+
32+ /**
33+ * Unit tests for privacy.
34+ *
35+ * @package mod_pdfannotator
36+ * @copyright IT Center RWTH Aachen University
37+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38+ */
39+ class provider_test extends provider_testcase {
40+
41+ protected $ course ;
42+ protected $ cmcontext ;
43+ protected $ user ;
44+ protected $ pdffile ;
45+ protected $ annotations ;
46+ protected $ questions ;
47+ protected $ answers ;
48+
49+ public function setUp (): void {
50+ global $ DB ;
51+
52+ $ this ->resetAfterTest ();
53+
54+ // Create a course.
55+ $ generator = $ this ->getDataGenerator ();
56+ $ this ->course = $ generator ->create_course ();
57+
58+ // Create a pdfannotator module.
59+ $ cm = $ generator ->create_module ('pdfannotator ' , ['course ' => $ this ->course ->id ]);
60+ $ this ->cmcontext = \context_module::instance ($ cm ->cmid );
61+
62+ // Create a user.
63+ $ this ->user = $ generator ->create_user ();
64+
65+ // Create a pdf object.
66+ $ pdfobject = new stdClass ();
67+ $ pdfobject ->course = $ this ->course ->id ;
68+ $ pdfobject ->name = "PDF_Provider_Test " ;
69+ $ pdfobject ->intro = "" ;
70+ $ pdfobject ->introformat = 1 ;
71+ $ pdfobject ->usevotes = 1 ;
72+ $ pdfobject ->useprint = 1 ;
73+ $ pdfobject ->useprintcomments = 1 ;
74+ $ pdfobject ->use_studenttextbox = 0 ;
75+ $ pdfobject ->use_studentdrawing = 0 ;
76+ $ pdfobject ->useprivatecomments = 0 ;
77+ $ pdfobject ->useprotectedcomments = 0 ;
78+ $ pdfobject ->timecreated = time ();
79+ $ pdfobject ->timemodified = time ();
80+ $ pdfobject ->id = $ DB ->insert_record ('pdfannotator ' , $ pdfobject );
81+ $ this ->pdffile = $ pdfobject ;
82+
83+ // Create an (pin as a test) annotation to the test pdf.
84+ $ annotationobj = new stdClass ();
85+ $ annotationobj ->pdfannotatorid = $ this ->pdffile ->id ;
86+ $ annotationobj ->page = 1 ;
87+ $ annotationobj ->userid = $ this ->user ->id ;
88+ $ pinannotation = $ DB ->get_record ('pdfannotator_annotationtypes ' , ['name ' => 'pin ' ]);
89+ $ annotationobj ->annotationtypeid = $ pinannotation ->id ;
90+ $ annotationobj ->data = json_encode (['x ' => 365 , 'y ' => 166 ]);
91+ $ annotationobj ->timecreated = time ();
92+ $ annotationobj ->timemodified = time ();
93+ $ annotationobj ->id = $ DB ->insert_record ('pdfannotator_annotations ' , $ annotationobj );
94+ $ this ->annotations [] = $ annotationobj ;
95+
96+ // Create a question for the pin annotation.
97+ $ question = new stdClass ();
98+ $ question ->pdfannotatorid = $ this ->pdffile ->id ;
99+ $ question ->annotationid = $ this ->annotations [0 ]->id ;
100+ $ question ->userid = $ this ->user ->id ;
101+ $ question ->timecreated = time ();
102+ $ question ->timemodified = time ();
103+ $ question ->isquestion = 1 ;
104+ $ question ->isdeleted = 0 ;
105+ $ question ->ishidden = 0 ;
106+ $ question ->solved = 0 ;
107+ $ question ->id = $ DB ->insert_record ('pdfannotator_comments ' , $ question );
108+ $ this ->questions [] = $ question ;
109+
110+ // Create a comment for the question above.
111+ $ answer = new stdClass ();
112+ $ answer ->pdfannotatorid = $ this ->pdffile ->id ;
113+ $ answer ->annotationid = $ this ->annotations [0 ]->id ;
114+ $ answer ->userid = $ this ->user ->id ;
115+ $ answer ->timecreated = time ();
116+ $ answer ->timemodified = time ();
117+ $ answer ->isquestion = 0 ;
118+ $ answer ->isdeleted = 0 ;
119+ $ answer ->ishidden = 0 ;
120+ $ answer ->solved = 0 ;
121+ $ answer ->id = $ DB ->insert_record ('pdfannotator_comments ' , $ answer );
122+ $ this ->answers [] = $ answer ;
123+ }
124+
125+ public function test_delete_data_for_users () {
126+ global $ DB ;
127+
128+ $ this ->resetAfterTest ();
129+
130+ $ component = 'mod_pdfannotator ' ;
131+
132+ $ usercontext1 = \context_user::instance ($ this ->user ->id );
133+ $ userlist1 = new \core_privacy \local \request \userlist ($ usercontext1 , $ component );
134+ provider::get_users_in_context ($ userlist1 );
135+ $ this ->assertCount (1 , $ userlist1 );
136+
137+ // Create a comment for the question above.
138+ $ answer = new stdClass ();
139+ $ answer ->pdfannotatorid = $ this ->pdffile ->id ;
140+ $ answer ->annotationid = $ this ->annotations [0 ]->id ;
141+ $ answer ->userid = $ this ->user ->id ;
142+ $ answer ->timecreated = time ();
143+ $ answer ->timemodified = time ();
144+ $ answer ->isquestion = 0 ;
145+ $ answer ->isdeleted = 0 ;
146+ $ answer ->ishidden = 0 ;
147+ $ answer ->solved = 0 ;
148+ $ answer ->id = $ DB ->insert_record ('pdfannotator_comments ' , $ answer );
149+ $ this ->answers [] = $ answer ;
150+
151+ // Report the first comment from the setUp().
152+ $ reportobj = new stdClass ();
153+ $ reportobj ->commentid = $ this ->answers [0 ]->id ;
154+ $ reportobj ->courseid = $ this ->course ->id ;
155+ $ reportobj ->pdfannotatorid = $ this ->pdffile ->id ;
156+ $ reportobj ->userid = $ this ->user ->id ;
157+ $ reportobj ->timecreated = time ();
158+ $ reportobj ->seen = 0 ;
159+ $ reportobj ->id = $ DB ->insert_record ('pdfannotator_reports ' , $ reportobj );
160+
161+ // Vote the second comment in the quetions[0].
162+ $ voteobj = new stdClass ();
163+ $ voteobj ->commentid = $ this ->answers [1 ]->id ;
164+ $ voteobj ->userid = $ this ->user ->id ;
165+ $ voteobj ->vote = 1 ;
166+ $ voteobj ->id = $ DB ->insert_record ('pdfannotator_votes ' , $ voteobj );
167+
168+ // Subscribe the questions[0].
169+ $ subscriptionsobj = new stdClass ();
170+ $ subscriptionsobj ->annotationid = $ this ->questions [0 ]->id ;
171+ $ subscriptionsobj ->userid = $ this ->user ->id ;
172+ $ subscriptionsobj ->id = $ DB ->insert_record ('pdfannotator_subscriptions ' , $ voteobj );
173+
174+ // Perform delete_data_for_users.
175+ $ systemcontext = \context_system::instance ();
176+ $ component = 'mod_pdfannotator ' ;
177+ $ userlist = new approved_userlist ($ systemcontext , $ component , $ this ->user ->id );
178+ // Delete using delete_data_for_user.
179+ provider::delete_data_for_users ($ userlist );
180+
181+ list ($ userinsql , $ userinparams ) = $ DB ->get_in_or_equal ($ userlist ->get_userids (), SQL_PARAMS_NAMED );
182+
183+ $ annotatorid = $ this ->pdffile ->id ;
184+
185+ // Combine instance + user sql.
186+ $ params = array_merge (['pdfannotatorid ' => $ annotatorid ], $ userinparams );
187+ $ sql = "pdfannotatorid = :pdfannotatorid AND userid {$ userinsql }" ;
188+
189+ // Count subscriptions.
190+ $ annotations = $ DB ->get_records ('pdfannotator_annotations ' , ['pdfannotatorid ' => $ annotatorid ]);
191+ $ annotationids = array_column ($ annotations , 'id ' );
192+ list ($ subinsql , $ subinparams ) = $ DB ->get_in_or_equal ($ annotationids , SQL_PARAMS_NAMED );
193+
194+ $ count_subs = $ DB ->count_records_sql ("SELECT *
195+ FROM {pdfannotator_subscriptions} sub
196+ WHERE sub.userid {$ userinsql }
197+ AND sub.annotationid {$ subinsql }" ,
198+ array_merge ($ userinparams , $ subinparams ));
199+ $ this ->assertCount (0 , $ count_subs );
200+
201+ // Count votes.
202+ $ comments = $ DB ->get_records ('pdfannotator_comments ' , ['pdfannotatorid ' => $ annotatorid ]);
203+ $ commentsids = array_column ($ comments , 'id ' );
204+ list ($ commentinsql , $ commentinparams ) = $ DB ->get_in_or_equal ($ commentsids , SQL_PARAMS_NAMED );
205+
206+ $ count_votes = $ DB ->count_records_sql ("SELECT *
207+ FORM {pdfannotator_votes} votes
208+ WHERE vote.userid {$ userinsql }
209+ AND vote.commentid {$ commentinsql }" ,
210+ array_merge ($ userinparams , $ commentinparams ));
211+ $ this ->assertCount (0 , $ count_votes );
212+
213+ // Count annotations, reports, and comments.
214+ $ count_annotations = count ($ DB ->get_records_select ('pdfannotator_annotations ' , $ sql , $ params ));
215+ $ this ->assertCount (0 , $ count_annotations );
216+ $ count_reports = count ($ DB ->get_records_select ('pdfannotator_reports ' , $ sql , $ params ));
217+ $ this ->assertCount (0 , $ count_reports );
218+ $ count_comments = count ($ DB ->get_records_select ('pdfannotator_comments ' , $ sql , $ params ));
219+ $ this ->assertCount (0 , $ count_comments );
220+
221+ // Count pictures in comments.
222+ $ count_pics = $ DB ->count_records_sql ("SELECT *
223+ FORM {files} imgs
224+ WHERE imgs.component = 'mod_pdfannotator'
225+ AND imgs.filearea = 'post'
226+ AND imgs.userid {$ userinsql }
227+ AND imgs.itemid {$ commentinsql }" ,
228+ array_merge ($ userinparams , $ commentinparams ));
229+ $ this ->assertCount (0 , $ count_pics );
230+ }
231+ }
0 commit comments