MDL-63692 core_group: Add group conversation messages to Privacy API
authorSara Arjona <sara@moodle.com>
Mon, 12 Nov 2018 18:24:23 +0000 (19:24 +0100)
committerSara Arjona <sara@moodle.com>
Mon, 19 Nov 2018 21:08:28 +0000 (22:08 +0100)
group/classes/privacy/provider.php
group/tests/privacy_provider_test.php
lang/en/group.php

index 86e441c..33f172f 100644 (file)
@@ -68,6 +68,8 @@ class provider implements
             'timeadded' => 'privacy:metadata:groups:timeadded',
         ], 'privacy:metadata:groups');
 
+        $collection->link_subsystem('core_message', 'privacy:metadata:core_message');
+
         return $collection;
     }
 
@@ -88,7 +90,7 @@ class provider implements
 
         $subcontext[] = get_string('groups', 'core_group');
 
-        $sql = "SELECT gm.id, gm.timeadded, gm.userid, g.name
+        $sql = "SELECT gm.id, gm.timeadded, gm.userid, g.name, gm.groupid
                   FROM {groups_members} gm
                   JOIN {groups} g ON gm.groupid = g.id
                  WHERE g.courseid = :courseid
@@ -107,7 +109,7 @@ class provider implements
 
         $groups = $DB->get_records_sql($sql, $params);
 
-        $groups = array_map(function($group) {
+        $groupstoexport = array_map(function($group) {
             return (object) [
                 'name' => format_string($group->name),
                 'timeadded' => transform::datetime($group->timeadded),
@@ -117,8 +119,14 @@ class provider implements
         if (!empty($groups)) {
             \core_privacy\local\request\writer::with_context($context)
                     ->export_data($subcontext, (object) [
-                        'groups' => $groups,
+                        'groups' => $groupstoexport,
                     ]);
+
+            foreach ($groups as $group) {
+                // Export associated conversations to this group.
+                \core_message\privacy\provider::export_conversations($USER->id, 'core_group', 'groups',
+                    $context, [], $group->groupid);
+            }
         }
     }
 
@@ -148,6 +156,13 @@ class provider implements
             $params['itemid'] = $itemid;
         }
 
+        // Delete the group conversations.
+        $groups = $DB->get_records_select('groups_members', $select, $params);
+        foreach ($groups as $group) {
+            \core_message\privacy\provider::delete_conversations_for_all_users($context, 'core_group', 'groups', $group->groupid);
+        }
+
+        // Remove members from the group.
         $DB->delete_records_select('groups_members', $select, $params);
 
         // Purge the group and grouping cache for users.
@@ -191,6 +206,13 @@ class provider implements
             $params['itemid'] = $itemid;
         }
 
+        // Delete the group conversations.
+        $groups = $DB->get_records_select('groups_members', $select, $params);
+        foreach ($groups as $group) {
+            \core_message\privacy\provider::delete_conversations_for_user($contextlist, 'core_group', 'groups', $group->groupid);
+        }
+
+        // Remove members from the group.
         $DB->delete_records_select('groups_members', $select, $params);
 
         // Invalidate the group and grouping cache for the user.
@@ -227,6 +249,9 @@ class provider implements
         }
 
         $userlist->add_from_sql('userid', $sql, $params);
+
+        // Get the users with some group conversation in this context.
+        \core_message\privacy\provider::add_conversations_in_context($userlist, 'core_group', 'groups', $itemid);
     }
 
     /**
@@ -258,6 +283,12 @@ class provider implements
             $params['itemid'] = $itemid;
         }
 
+        // Delete the group conversations for these users.
+        $groups = $DB->get_records_select('groups_members', $select, $params);
+        foreach ($groups as $group) {
+            \core_message\privacy\provider::delete_conversations_for_users($userlist, 'core_group', 'groups', $group->groupid);
+        }
+
         $DB->delete_records_select('groups_members', $select, $params);
 
         // Invalidate the group and grouping cache for the user.
@@ -294,6 +325,9 @@ class provider implements
 
         $contextlist->add_from_sql($sql, $params);
 
+        // Get the contexts where the userid has group conversations.
+        \core_message\privacy\provider::add_contexts_for_conversations($contextlist, $userid, 'core_group', 'groups', $itemid);
+
         return $contextlist;
     }
 
index 1959029..6fd01fa 100644 (file)
@@ -45,10 +45,9 @@ class core_group_privacy_provider_testcase extends provider_testcase {
         $collection = new collection('core_group');
         $newcollection = provider::get_metadata($collection);
         $itemcollection = $newcollection->get_collection();
-        $this->assertCount(1, $itemcollection);
-
-        $table = reset($itemcollection);
+        $this->assertCount(2, $itemcollection);
 
+        $table = array_shift($itemcollection);
         $this->assertEquals('groups_members', $table->get_name());
         $this->assertEquals('privacy:metadata:groups', $table->get_summary());
 
@@ -56,6 +55,10 @@ class core_group_privacy_provider_testcase extends provider_testcase {
         $this->assertArrayHasKey('groupid', $privacyfields);
         $this->assertArrayHasKey('userid', $privacyfields);
         $this->assertArrayHasKey('timeadded', $privacyfields);
+
+        $table = array_shift($itemcollection);
+        $this->assertEquals('core_message', $table->get_name());
+        $this->assertEquals('privacy:metadata:core_message', $table->get_summary());
     }
 
     /**
@@ -508,6 +511,145 @@ class core_group_privacy_provider_testcase extends provider_testcase {
         );
     }
 
+    /**
+     * Test for provider::delete_groups_for_users() to delete group memberships of a component.
+     */
+    public function test_delete_groups_for_users_for_component() {
+        global $DB;
+
+        $this->resetAfterTest();
+
+        $course1 = $this->getDataGenerator()->create_course();
+        $course2 = $this->getDataGenerator()->create_course();
+        $course3 = $this->getDataGenerator()->create_course();
+
+        $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
+        $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
+        $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
+        $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
+        $group3a = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
+        $group3b = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
+
+        $user1 = $this->getDataGenerator()->create_user();
+        $user2 = $this->getDataGenerator()->create_user();
+
+        $this->getDataGenerator()->enrol_user($user1->id, $course1->id, null, 'self');
+        $this->getDataGenerator()->enrol_user($user1->id, $course2->id, null, 'self');
+        $this->getDataGenerator()->enrol_user($user1->id, $course3->id, null, 'self');
+        $this->getDataGenerator()->enrol_user($user2->id, $course1->id, null, 'self');
+        $this->getDataGenerator()->enrol_user($user2->id, $course2->id, null, 'self');
+        $this->getDataGenerator()->enrol_user($user2->id, $course3->id, null, 'self');
+
+        $this->getDataGenerator()->create_group_member(
+                array('groupid' => $group1a->id, 'userid' => $user1->id, 'component' => 'enrol_self'));
+        $this->getDataGenerator()->create_group_member(
+                array('groupid' => $group1b->id, 'userid' => $user2->id, 'component' => 'enrol_self'));
+        $this->getDataGenerator()->create_group_member(
+                array('groupid' => $group2a->id, 'userid' => $user1->id, 'component' => 'enrol_self'));
+        $this->getDataGenerator()->create_group_member(
+                array('groupid' => $group2b->id, 'userid' => $user2->id, 'component' => 'enrol_self'));
+        $this->getDataGenerator()->create_group_member(array('groupid' => $group3a->id, 'userid' => $user1->id));
+        $this->getDataGenerator()->create_group_member(array('groupid' => $group3b->id, 'userid' => $user2->id));
+
+        $this->assertEquals(
+                2,
+                $DB->count_records_sql("SELECT COUNT(gm.id)
+                                          FROM {groups_members} gm
+                                          JOIN {groups} g ON gm.groupid = g.id
+                                         WHERE g.courseid = ?", [$course1->id])
+        );
+        $this->assertEquals(
+                2,
+                $DB->count_records_sql("SELECT COUNT(gm.id)
+                                          FROM {groups_members} gm
+                                          JOIN {groups} g ON gm.groupid = g.id
+                                         WHERE g.courseid = ?", [$course2->id])
+        );
+        $this->assertEquals(
+                2,
+                $DB->count_records_sql("SELECT COUNT(gm.id)
+                                          FROM {groups_members} gm
+                                          JOIN {groups} g ON gm.groupid = g.id
+                                         WHERE g.courseid = ?", [$course2->id])
+        );
+        $this->assertEquals(
+                3,
+                $DB->count_records_sql("SELECT COUNT(gm.id)
+                                          FROM {groups_members} gm
+                                          JOIN {groups} g ON gm.groupid = g.id
+                                         WHERE gm.userid = ?", [$user1->id])
+        );
+
+        // Delete user1 and user2 from groups in course1.
+        $coursecontext1 = context_course::instance($course1->id);
+        $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'core_group',
+                [$user1->id, $user2->id]);
+        provider::delete_groups_for_users($approveduserlist, 'enrol_self');
+
+        $this->assertEquals(
+                0,
+                $DB->count_records_sql("SELECT COUNT(gm.id)
+                                          FROM {groups_members} gm
+                                          JOIN {groups} g ON gm.groupid = g.id
+                                         WHERE g.courseid = ?", [$course1->id])
+        );
+        $this->assertEquals(
+                2,
+                $DB->count_records_sql("SELECT COUNT(gm.id)
+                                          FROM {groups_members} gm
+                                          JOIN {groups} g ON gm.groupid = g.id
+                                         WHERE g.courseid = ?", [$course2->id])
+        );
+        $this->assertEquals(
+                2,
+                $DB->count_records_sql("SELECT COUNT(gm.id)
+                                          FROM {groups_members} gm
+                                          JOIN {groups} g ON gm.groupid = g.id
+                                         WHERE g.courseid = ?", [$course3->id])
+        );
+        $this->assertEquals(
+                2,
+                $DB->count_records_sql("SELECT COUNT(gm.id)
+                                          FROM {groups_members} gm
+                                          JOIN {groups} g ON gm.groupid = g.id
+                                         WHERE gm.userid = ?", [$user1->id])
+        );
+
+        // Delete user1 and user2 from course3.
+        $coursecontext3 = context_course::instance($course3->id);
+        $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext3, 'core_group',
+                [$user1->id, $user2->id]);
+        provider::delete_groups_for_users($approveduserlist, 'enrol_self');
+        $this->assertEquals(
+                0,
+                $DB->count_records_sql("SELECT COUNT(gm.id)
+                                          FROM {groups_members} gm
+                                          JOIN {groups} g ON gm.groupid = g.id
+                                         WHERE g.courseid = ?", [$course1->id])
+        );
+        $this->assertEquals(
+                2,
+                $DB->count_records_sql("SELECT COUNT(gm.id)
+                                          FROM {groups_members} gm
+                                          JOIN {groups} g ON gm.groupid = g.id
+                                         WHERE g.courseid = ?", [$course2->id])
+        );
+        $this->assertEquals(
+                2,
+                $DB->count_records_sql("SELECT COUNT(gm.id)
+                                          FROM {groups_members} gm
+                                          JOIN {groups} g ON gm.groupid = g.id
+                                         WHERE g.courseid = ?", [$course3->id])
+        );
+        $this->assertEquals(
+                2,
+                $DB->count_records_sql("SELECT COUNT(gm.id)
+                                          FROM {groups_members} gm
+                                          JOIN {groups} g ON gm.groupid = g.id
+                                         WHERE gm.userid = ?", [$user1->id])
+        );
+    }
+
     /**
      * Test for provider::delete_groups_for_user() to check deleting from cache.
      */
index cabf387..aed3fcb 100644 (file)
@@ -175,6 +175,7 @@ $string['overview'] = 'Overview';
 $string['potentialmembers'] = 'Potential members: {$a}';
 $string['potentialmembs'] = 'Potential members';
 $string['printerfriendly'] = 'Printer-friendly display';
+$string['privacy:metadata:core_message'] = 'The group conversations';
 $string['privacy:metadata:groups'] = 'A record of group membership.';
 $string['privacy:metadata:groups:groupid'] = 'The ID of the group.';
 $string['privacy:metadata:groups:timeadded'] = 'The timestamp indicating when the user was added to the group.';