From 1c46c0b10f4cc135aec57fc9e85490ef91368c2b Mon Sep 17 00:00:00 2001
From: Justus Dieckmann <justusdieckmann@wwu.de>
Date: Mon, 16 Sep 2019 22:40:16 +0200
Subject: [PATCH] Privacy: Implement tests for delete_data_for_user,
delete_data_for_users, get_users_in_context
---
classes/privacy/provider.php | 2 +-
tests/privacy_test.php | 77 ++++++++++++++++++++++++++++++++++++
2 files changed, 78 insertions(+), 1 deletion(-)
diff --git a/classes/privacy/provider.php b/classes/privacy/provider.php
index 91b7df9..d2905a6 100644
--- a/classes/privacy/provider.php
+++ b/classes/privacy/provider.php
@@ -166,7 +166,7 @@ class provider implements
list($insql, $params) = $DB->get_in_or_equal($userlist->get_userids());
$sql = "UPDATE {tool_lifecycle_action_log}
SET userid = -1
- WHERE $insql";
+ WHERE userid " . $insql;
$DB->execute($sql, $params);
}
diff --git a/tests/privacy_test.php b/tests/privacy_test.php
index dee922e..cce99ec 100644
--- a/tests/privacy_test.php
+++ b/tests/privacy_test.php
@@ -26,7 +26,9 @@
defined('MOODLE_INTERNAL') || die();
global $CFG;
+use core_privacy\local\request\approved_userlist;
use core_privacy\local\request\content_writer;
+use core_privacy\local\request\userlist;
use core_privacy\local\request\writer;
use core_privacy\tests\provider_testcase;
use core_privacy\tests\request\approved_contextlist;
@@ -165,4 +167,79 @@ class tool_lifecycle_privacy_test extends provider_testcase {
$this->assertFalse($DB->record_exists_select('tool_lifecycle_action_log', 'userid != -1'));
}
+ public function test_delete_data_for_user() {
+ global $DB;
+ $c1 = $this->getDataGenerator()->create_course();
+ $c2 = $this->getDataGenerator()->create_course();
+ $u1 = $this->getDataGenerator()->create_user();
+ $u2 = $this->getDataGenerator()->create_user();
+
+
+ $p1 = $this->generator->create_process($c1->id, $this->workflow->id);
+ $p2 = $this->generator->create_process($c2->id, $this->workflow->id);
+
+ $processor = new processor();
+ $processor->process_courses();
+
+ $this->setUser($u1);
+ interaction_manager::handle_interaction($this->emailstep->id, $p1->id, self::ACTION_KEEP);
+
+ $this->setUser($u2);
+ interaction_manager::handle_interaction($this->emailstep->id, $p2->id, self::ACTION_KEEP);
+
+
+ $contextlist = new approved_contextlist($u1, 'tool_lifecycle', [1]);
+ provider::delete_data_for_user($contextlist);
+ $this->assertEquals(0, $DB->count_records_select('tool_lifecycle_action_log', "userid = $u1->id"));
+ $this->assertEquals(1, $DB->count_records_select('tool_lifecycle_action_log', "userid = $u2->id"));
+ $this->assertEquals(1, $DB->count_records_select('tool_lifecycle_action_log', "userid = -1"));
+ }
+
+ public function test_get_users_in_context() {
+ $c1 = $this->getDataGenerator()->create_course();
+ $c2 = $this->getDataGenerator()->create_course();
+ $u1 = $this->getDataGenerator()->create_user();
+
+ $p1 = $this->generator->create_process($c1->id, $this->workflow->id);
+ $p2 = $this->generator->create_process($c2->id, $this->workflow->id);
+
+ $processor = new processor();
+ $processor->process_courses();
+
+ $this->setUser($u1);
+ interaction_manager::handle_interaction($this->emailstep->id, $p1->id, self::ACTION_KEEP);
+ interaction_manager::handle_interaction($this->emailstep->id, $p2->id, self::ACTION_KEEP);
+
+ $userlist = new userlist(context_system::instance(), 'tool_lifecycle');
+ provider::get_users_in_context($userlist);
+ $this->assertEquals(1, $userlist->count());
+ $this->assertEquals($u1->id, $userlist->current()->id);
+ }
+
+ public function test_delete_data_for_users() {
+ global $DB;
+ $c1 = $this->getDataGenerator()->create_course();
+ $c2 = $this->getDataGenerator()->create_course();
+ $u1 = $this->getDataGenerator()->create_user();
+ $u2 = $this->getDataGenerator()->create_user();
+
+ $proc1 = $this->generator->create_process($c1->id, $this->workflow->id);
+ $proc2 = $this->generator->create_process($c2->id, $this->workflow->id);
+ $this->setUser($u1);
+
+ $processor = new processor();
+ $processor->process_courses();
+
+ interaction_manager::handle_interaction($this->emailstep->id, $proc1->id, self::ACTION_KEEP);
+
+ $this->setUser($u2);
+ interaction_manager::handle_interaction($this->emailstep->id, $proc2->id, self::ACTION_KEEP);
+
+ $userlist = new approved_userlist(context_system::instance(), 'tool_lifecycle', [$u1->id]);
+ provider::delete_data_for_users($userlist);
+ $this->assertEquals(0, $DB->count_records_select('tool_lifecycle_action_log', "userid = $u1->id"));
+ $this->assertEquals(1, $DB->count_records_select('tool_lifecycle_action_log', "userid = $u2->id"));
+ $this->assertEquals(1, $DB->count_records_select('tool_lifecycle_action_log', "userid = -1"));
+ }
+
}
\ No newline at end of file
--
GitLab