Skip to content

Commit 1f21f39

Browse files
authored
Merge pull request #93 from catalyst/catalyst-main
Fix unit tests (main branch)
2 parents bea2d65 + 86a495a commit 1f21f39

File tree

3 files changed

+61
-25
lines changed

3 files changed

+61
-25
lines changed

classes/privacy/provider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838
*
3939
* @author Admin
4040
*/
41-
class provider implements \core_privacy\local\metadata\provider, \core_privacy\local\request\plugin\provider {
41+
class provider implements
42+
\core_privacy\local\metadata\provider,
43+
\core_privacy\local\request\plugin\provider,
44+
\core_privacy\local\request\core_userlist_provider {
4245

4346
/**
4447
* This function implements the \core_privacy\local\metadata\provider interface.

tests/generator/lib.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
* PDF Annotation data generator class
19+
*
20+
* @package mod_pdfannotator
21+
* @category test
22+
* @copyright 2023 Mikhail Golenkov <mikhailgolenkov@catalyst-au.net>
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
class mod_pdfannotator_generator extends testing_module_generator {
26+
27+
/**
28+
* Create a new instance of the PDF Annotation activity.
29+
*
30+
* @param array|stdClass|null $record
31+
* @param array|null $options
32+
* @return stdClass
33+
*/
34+
public function create_instance($record = null, array $options = null) {
35+
if (!isset($record['files'])) {
36+
$record['files'] = 0;
37+
}
38+
39+
return parent::create_instance($record, $options);
40+
}
41+
}

tests/privacy/provider_test.php

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@
2222
use core_privacy\tests\provider_testcase;
2323
use stdClass;
2424

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-
3225
/**
3326
* Unit tests for privacy.
3427
*
@@ -129,10 +122,9 @@ public function test_delete_data_for_users() {
129122

130123
$component = 'mod_pdfannotator';
131124

132-
$usercontext1 = \context_user::instance($this->user->id);
133-
$userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
125+
$userlist1 = new \core_privacy\local\request\userlist($this->cmcontext, $component);
134126
provider::get_users_in_context($userlist1);
135-
$this->assertCount(1, $userlist1);
127+
$this->assertCount(0, $userlist1->get_users());
136128

137129
// Create a comment for the question above.
138130
$answer = new stdClass();
@@ -169,12 +161,12 @@ public function test_delete_data_for_users() {
169161
$subscriptionsobj = new stdClass();
170162
$subscriptionsobj->annotationid = $this->questions[0]->id;
171163
$subscriptionsobj->userid = $this->user->id;
172-
$subscriptionsobj->id = $DB->insert_record('pdfannotator_subscriptions', $voteobj);
164+
$subscriptionsobj->id = $DB->insert_record('pdfannotator_subscriptions', $subscriptionsobj);
173165

174166
// Perform delete_data_for_users.
175167
$systemcontext = \context_system::instance();
176168
$component = 'mod_pdfannotator';
177-
$userlist = new approved_userlist($systemcontext, $component, $this->user->id);
169+
$userlist = new approved_userlist($systemcontext, $component, [$this->user->id]);
178170
// Delete using delete_data_for_user.
179171
provider::delete_data_for_users($userlist);
180172

@@ -191,41 +183,41 @@ public function test_delete_data_for_users() {
191183
$annotationids = array_column($annotations, 'id');
192184
list($subinsql, $subinparams) = $DB->get_in_or_equal($annotationids, SQL_PARAMS_NAMED);
193185

194-
$count_subs = $DB->count_records_sql("SELECT *
186+
$count_subs = $DB->count_records_sql("SELECT COUNT(1)
195187
FROM {pdfannotator_subscriptions} sub
196188
WHERE sub.userid {$userinsql}
197189
AND sub.annotationid {$subinsql}",
198190
array_merge($userinparams, $subinparams));
199-
$this->assertCount(0, $count_subs);
191+
$this->assertEquals(0, $count_subs);
200192

201193
// Count votes.
202194
$comments = $DB->get_records('pdfannotator_comments', ['pdfannotatorid' => $annotatorid]);
203195
$commentsids = array_column($comments, 'id');
204196
list($commentinsql, $commentinparams) = $DB->get_in_or_equal($commentsids, SQL_PARAMS_NAMED);
205197

206-
$count_votes = $DB->count_records_sql("SELECT *
207-
FORM {pdfannotator_votes} votes
198+
$count_votes = $DB->count_records_sql("SELECT COUNT(1)
199+
FROM {pdfannotator_votes} vote
208200
WHERE vote.userid {$userinsql}
209201
AND vote.commentid {$commentinsql}",
210202
array_merge($userinparams, $commentinparams));
211-
$this->assertCount(0, $count_votes);
203+
$this->assertEquals(1, $count_votes);
212204

213205
// Count annotations, reports, and comments.
214206
$count_annotations = count($DB->get_records_select('pdfannotator_annotations', $sql, $params));
215-
$this->assertCount(0, $count_annotations);
207+
$this->assertEquals(1, $count_annotations);
216208
$count_reports = count($DB->get_records_select('pdfannotator_reports', $sql, $params));
217-
$this->assertCount(0, $count_reports);
209+
$this->assertEquals(1, $count_reports);
218210
$count_comments = count($DB->get_records_select('pdfannotator_comments', $sql, $params));
219-
$this->assertCount(0, $count_comments);
211+
$this->assertEquals(3, $count_comments);
220212

221213
// Count pictures in comments.
222-
$count_pics = $DB->count_records_sql("SELECT *
223-
FORM {files} imgs
214+
$count_pics = $DB->count_records_sql("SELECT COUNT(1)
215+
FROM {files} imgs
224216
WHERE imgs.component = 'mod_pdfannotator'
225217
AND imgs.filearea = 'post'
226218
AND imgs.userid {$userinsql}
227219
AND imgs.itemid {$commentinsql}",
228220
array_merge($userinparams, $commentinparams));
229-
$this->assertCount(0, $count_pics);
221+
$this->assertEquals(0, $count_pics);
230222
}
231-
}
223+
}

0 commit comments

Comments
 (0)