2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Tests for the manager observer.
20 * @package tool_dataprivacy
21 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
30 * @package tool_dataprivacy
31 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class tool_dataprivacy_manager_observer_testcase
extends advanced_testcase
{
37 * Helper to set andn return two users who are DPOs.
39 protected function setup_site_dpos() {
41 $this->resetAfterTest();
43 $generator = new testing_data_generator();
44 $u1 = $this->getDataGenerator()->create_user();
45 $u2 = $this->getDataGenerator()->create_user();
47 $context = context_system
::instance();
49 // Give the manager role with the capability to manage data requests.
50 $managerroleid = $DB->get_field('role', 'id', array('shortname' => 'manager'));
51 assign_capability('tool/dataprivacy:managedatarequests', CAP_ALLOW
, $managerroleid, $context->id
, true);
53 // Assign both users as manager.
54 role_assign($managerroleid, $u1->id
, $context->id
);
55 role_assign($managerroleid, $u2->id
, $context->id
);
57 // Only map the manager role to the DPO role.
58 set_config('dporoles', $managerroleid, 'tool_dataprivacy');
60 return \tool_dataprivacy\api
::get_site_dpos();
64 * Ensure that when users are configured as DPO, they are sent an message upon failure.
66 public function test_handle_component_failure() {
67 $this->resetAfterTest();
69 // Create another user who is not a DPO.
70 $this->getDataGenerator()->create_user();
73 $dpos = $this->setup_site_dpos();
75 $observer = new \tool_dataprivacy\
manager_observer();
77 // Handle the failure, catching messages.
78 $mailsink = $this->redirectMessages();
80 $observer->handle_component_failure(new \
Exception('error'), 'foo', 'bar', 'baz', ['foobarbaz', 'bum']);
82 // Messages should be sent to both DPOs only.
83 $this->assertEquals(2, $mailsink->count());
85 $messages = $mailsink->get_messages();
86 $messageusers = array_map(function($message) {
87 return $message->useridto
;
90 $this->assertEquals(array_keys($dpos), $messageusers, '', 0.0, 0, true);
94 * Ensure that when no user is configured as DPO, the message is sent to admin instead.
96 public function test_handle_component_failure_no_dpo() {
97 $this->resetAfterTest();
99 // Create another user who is not a DPO or admin.
100 $this->getDataGenerator()->create_user();
102 $observer = new \tool_dataprivacy\
manager_observer();
104 $mailsink = $this->redirectMessages();
106 $observer->handle_component_failure(new \
Exception('error'), 'foo', 'bar', 'baz', ['foobarbaz', 'bum']);
108 // Messages should have been sent only to the admin.
109 $this->assertEquals(1, $mailsink->count());
111 $messages = $mailsink->get_messages();
112 $message = reset($messages);
114 $admin = \core_user
::get_user_by_username('admin');
115 $this->assertEquals($admin->id
, $message->useridto
);