Skip to content

Commit 5aa946d

Browse files
author
David Monllao
committed
Merge branch 'MDL-62516-34' of git://github.com/andrewnicols/moodle into MOODLE_34_STABLE
2 parents 88d0b05 + 841b8c5 commit 5aa946d

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

mod/forum/classes/privacy/provider.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ public static function delete_data_for_all_users_in_context(\context $context) {
795795
// Delete all files from the posts.
796796
$fs = get_file_storage();
797797
$fs->delete_area_files($context->id, 'mod_forum', 'post');
798+
$fs->delete_area_files($context->id, 'mod_forum', 'attachment');
798799

799800
// Delete all ratings in the context.
800801
\core_rating\privacy\provider::delete_ratings($context, 'mod_forum', 'post');
@@ -879,13 +880,14 @@ public static function delete_data_for_user(approved_contextlist $contextlist) {
879880
// Delete all Tags.
880881
\core_tag\privacy\provider::delete_item_tags_select($context, 'mod_forum', 'forum_posts',
881882
"IN ($postidsql)", $postparams);
883+
884+
// Delete all files from the posts.
885+
$fs = get_file_storage();
886+
$fs->delete_area_files_select($context->id, 'mod_forum', 'post', "IN ($postidsql)", $postparams);
887+
$fs->delete_area_files_select($context->id, 'mod_forum', 'attachment', "IN ($postidsql)", $postparams);
882888
}
883889

884890
$uniquediscussions->close();
885-
886-
// Delete all files from the posts.
887-
$fs = get_file_storage();
888-
$fs->delete_area_files($context->id, 'mod_forum', 'post');
889891
}
890892
}
891893
}

mod/forum/tests/privacy_provider_test.php

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,15 @@ public function test_all_users_deleted_from_context() {
887887
'filepath' => '/',
888888
'filename' => 'example.jpg',
889889
], 'image contents (not really)');
890+
// And an attachment.
891+
$fs->create_file_from_string([
892+
'contextid' => $context->id,
893+
'component' => 'mod_forum',
894+
'filearea' => 'attachment',
895+
'itemid' => $post->id,
896+
'filepath' => '/',
897+
'filename' => 'example.jpg',
898+
], 'image contents (not really)');
890899
}
891900
}
892901

@@ -962,6 +971,7 @@ public function test_all_users_deleted_from_context() {
962971
// All uploaded files relating to this context should have been deleted (post content).
963972
foreach ($postsinforum as $post) {
964973
$this->assertEmpty($fs->get_area_files($context->id, 'mod_forum', 'post', $post->id));
974+
$this->assertEmpty($fs->get_area_files($context->id, 'mod_forum', 'attachment', $post->id));
965975
}
966976

967977
// All ratings should have been deleted.
@@ -1091,6 +1101,15 @@ public function test_delete_data_for_user() {
10911101
'filepath' => '/',
10921102
'filename' => 'example.jpg',
10931103
], 'image contents (not really)');
1104+
// And a fake attachment.
1105+
$fs->create_file_from_string([
1106+
'contextid' => $context->id,
1107+
'component' => 'mod_forum',
1108+
'filearea' => 'attachment',
1109+
'itemid' => $post->id,
1110+
'filepath' => '/',
1111+
'filename' => 'example.jpg',
1112+
], 'image contents (not really)');
10941113
}
10951114
}
10961115

@@ -1137,12 +1156,26 @@ public function test_delete_data_for_user() {
11371156

11381157
// Delete for one of the forums for the first user.
11391158
$firstcontext = reset($contexts);
1140-
list($postinsql, $postinparams) = $DB->get_in_or_equal(
1141-
array_keys($postsbyforum[$user1->id][$firstcontext->id]), SQL_PARAMS_NAMED);
11421159

1143-
$othercontext = next($contexts);
1144-
list($otherpostinsql, $otherpostinparams) = $DB->get_in_or_equal(
1145-
array_keys($postsbyforum[$user1->id][$othercontext->id]), SQL_PARAMS_NAMED);
1160+
$deletedpostids = [];
1161+
$otherpostids = [];
1162+
foreach ($postsbyforum as $user => $contexts) {
1163+
foreach ($contexts as $thiscontextid => $theseposts) {
1164+
$thesepostids = array_map(function($post) {
1165+
return $post->id;
1166+
}, $theseposts);
1167+
1168+
if ($user == $user1->id && $thiscontextid == $firstcontext->id) {
1169+
// This post is in the deleted context and by the target user.
1170+
$deletedpostids = array_merge($deletedpostids, $thesepostids);
1171+
} else {
1172+
// This post is by another user, or in a non-target context.
1173+
$otherpostids = array_merge($otherpostids, $thesepostids);
1174+
}
1175+
}
1176+
}
1177+
list($postinsql, $postinparams) = $DB->get_in_or_equal($deletedpostids, SQL_PARAMS_NAMED);
1178+
list($otherpostinsql, $otherpostinparams) = $DB->get_in_or_equal($otherpostids, SQL_PARAMS_NAMED);
11461179

11471180
$approvedcontextlist = new \core_privacy\tests\request\approved_contextlist(
11481181
\core_user::get_user($user1->id),
@@ -1195,28 +1228,25 @@ public function test_delete_data_for_user() {
11951228
// Ratings should have been removed from the affected posts.
11961229
$this->assertCount(0, $DB->get_records_select('rating', "itemid {$postinsql}", $postinparams));
11971230

1198-
// Ratings should remain on posts in the other context.
1199-
$this->assertCount(16, $DB->get_records_select('rating', "itemid {$otherpostinsql}", $otherpostinparams));
1231+
// Ratings should remain on posts in the other context, and posts not belonging to the affected user.
1232+
$this->assertCount(144, $DB->get_records_select('rating', "itemid {$otherpostinsql}", $otherpostinparams));
12001233

12011234
// Ratings should remain where the user has rated another person's post.
12021235
$this->assertCount(32, $DB->get_records('rating', ['userid' => $user1->id]));
12031236

12041237
// Tags for the affected posts should be removed.
1205-
$this->assertCount(8, $DB->get_records_select('tag_instance', "itemid {$otherpostinsql}", $otherpostinparams));
1206-
1207-
// Tags should remain for the other posts by this user.
12081238
$this->assertCount(0, $DB->get_records_select('tag_instance', "itemid {$postinsql}", $postinparams));
12091239

1210-
// Tags should remain for others.
1211-
// Original total: 5 users * 2 forums * 4 posts * 2 tags
1212-
// Deleted posts: 8
1213-
// New total: 72.
1214-
$this->assertCount(72, $DB->get_records('tag_instance'));
1240+
// Tags should remain for the other posts by this user, and all posts by other users.
1241+
$this->assertCount(72, $DB->get_records_select('tag_instance', "itemid {$otherpostinsql}", $otherpostinparams));
12151242

12161243
// Files for the affected posts should be removed.
1244+
// 5 users * 2 forums * 1 file in each forum
1245+
// Original total: 10
1246+
// One post with file removed.
12171247
$this->assertCount(0, $DB->get_records_select('files', "itemid {$postinsql}", $postinparams));
12181248

12191249
// Files for the other posts should remain.
1220-
$this->assertCount(2, $DB->get_records_select('files', "itemid {$otherpostinsql}", $otherpostinparams));
1250+
$this->assertCount(18, $DB->get_records_select('files', "filename <> '.' AND itemid {$otherpostinsql}", $otherpostinparams));
12211251
}
12221252
}

0 commit comments

Comments
 (0)