Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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  namespace tool_dataprivacy;
  18  
  19  use data_privacy_testcase;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  require_once ('data_privacy_testcase.php');
  23  
  24  /**
  25   * Tests for the manager observer.
  26   *
  27   * @package    tool_dataprivacy
  28   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class manager_observer_test extends data_privacy_testcase {
  32      /**
  33       * Ensure that when users are configured as DPO, they are sent an message upon failure.
  34       */
  35      public function test_handle_component_failure() {
  36          $this->resetAfterTest();
  37  
  38          // Create another user who is not a DPO.
  39          $this->getDataGenerator()->create_user();
  40  
  41          // Create two DPOs.
  42          $dpo1 = $this->getDataGenerator()->create_user();
  43          $dpo2 = $this->getDataGenerator()->create_user();
  44          $this->assign_site_dpo(array($dpo1, $dpo2));
  45          $dpos = \tool_dataprivacy\api::get_site_dpos();
  46  
  47          $observer = new \tool_dataprivacy\manager_observer();
  48  
  49          // Handle the failure, catching messages.
  50          $mailsink = $this->redirectMessages();
  51          $mailsink->clear();
  52          $observer->handle_component_failure(new \Exception('error'), 'foo', 'bar', 'baz', ['foobarbaz', 'bum']);
  53  
  54          // Messages should be sent to both DPOs only.
  55          $this->assertEquals(2, $mailsink->count());
  56  
  57          $messages = $mailsink->get_messages();
  58          $messageusers = array_map(function($message) {
  59              return $message->useridto;
  60          }, $messages);
  61  
  62          $this->assertEqualsCanonicalizing(array_keys($dpos), $messageusers);
  63      }
  64  
  65      /**
  66       * Ensure that when no user is configured as DPO, the message is sent to admin instead.
  67       */
  68      public function test_handle_component_failure_no_dpo() {
  69          $this->resetAfterTest();
  70  
  71          // Create another user who is not a DPO or admin.
  72          $this->getDataGenerator()->create_user();
  73  
  74          $observer = new \tool_dataprivacy\manager_observer();
  75  
  76          $mailsink = $this->redirectMessages();
  77          $mailsink->clear();
  78          $observer->handle_component_failure(new \Exception('error'), 'foo', 'bar', 'baz', ['foobarbaz', 'bum']);
  79  
  80          // Messages should have been sent only to the admin.
  81          $this->assertEquals(1, $mailsink->count());
  82  
  83          $messages = $mailsink->get_messages();
  84          $message = reset($messages);
  85  
  86          $admin = \core_user::get_user_by_username('admin');
  87          $this->assertEquals($admin->id, $message->useridto);
  88      }
  89  }