Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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  declare(strict_types=1);
  18  
  19  namespace core_reportbuilder\privacy;
  20  
  21  use context_system;
  22  use core_privacy\local\metadata\collection;
  23  use core_privacy\local\metadata\types\database_table;
  24  use core_privacy\local\metadata\types\user_preference;
  25  use core_privacy\local\request\userlist;
  26  use core_privacy\local\request\writer;
  27  use core_privacy\tests\provider_testcase;
  28  use core_reportbuilder_generator;
  29  use core_reportbuilder\manager;
  30  use core_reportbuilder\local\helpers\user_filter_manager;
  31  use core_reportbuilder\local\models\audience;
  32  use core_reportbuilder\local\models\column;
  33  use core_reportbuilder\local\models\filter;
  34  use core_reportbuilder\local\models\report;
  35  use core_reportbuilder\local\models\schedule;
  36  use core_user\reportbuilder\datasource\users;
  37  
  38  /**
  39   * Unit tests for privacy provider
  40   *
  41   * @package     core_reportbuilder
  42   * @covers      \core_reportbuilder\privacy\provider
  43   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  44   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  class provider_test extends provider_testcase {
  47  
  48      /**
  49       * Test provider metadata
  50       */
  51      public function test_get_metadata(): void {
  52          $collection = new collection('core_reportbuilder');
  53          $metadata = provider::get_metadata($collection)->get_collection();
  54  
  55          $this->assertCount(6, $metadata);
  56  
  57          $this->assertInstanceOf(database_table::class, $metadata[0]);
  58          $this->assertEquals(report::TABLE, $metadata[0]->get_name());
  59  
  60          $this->assertInstanceOf(database_table::class, $metadata[1]);
  61          $this->assertEquals(column::TABLE, $metadata[1]->get_name());
  62  
  63          $this->assertInstanceOf(database_table::class, $metadata[2]);
  64          $this->assertEquals(filter::TABLE, $metadata[2]->get_name());
  65  
  66          $this->assertInstanceOf(database_table::class, $metadata[3]);
  67          $this->assertEquals(audience::TABLE, $metadata[3]->get_name());
  68  
  69          $this->assertInstanceOf(database_table::class, $metadata[4]);
  70          $this->assertEquals(schedule::TABLE, $metadata[4]->get_name());
  71  
  72          $this->assertInstanceOf(user_preference::class, $metadata[5]);
  73      }
  74  
  75      /**
  76       * Test getting contexts for user who created a report
  77       */
  78      public function test_get_contexts_for_userid_report(): void {
  79          $this->resetAfterTest();
  80  
  81          $user = $this->getDataGenerator()->create_user();
  82          $this->setUser($user);
  83  
  84          /** @var core_reportbuilder_generator $generator */
  85          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  86          $generator->create_report(['name' => 'Users', 'source' => users::class]);
  87  
  88          $contextlist = $this->get_contexts_for_userid((int) $user->id, 'core_reportbuilder');
  89          $this->assertCount(1, $contextlist);
  90          $this->assertInstanceOf(context_system::class, $contextlist->current());
  91      }
  92  
  93      /**
  94       * Test getting contexts for user who created an audience for a report by another user
  95       */
  96      public function test_get_contexts_for_userid_audience(): void {
  97          $this->resetAfterTest();
  98          $this->setAdminUser();
  99  
 100          /** @var core_reportbuilder_generator $generator */
 101          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 102          $report = $generator->create_report(['name' => 'Users', 'source' => users::class]);
 103  
 104          // Switch user, create a report audience.
 105          $user = $this->getDataGenerator()->create_user();
 106          $this->setUser($user);
 107  
 108          $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => []]);
 109  
 110          $contextlist = $this->get_contexts_for_userid((int) $user->id, 'core_reportbuilder');
 111          $this->assertCount(1, $contextlist);
 112          $this->assertInstanceOf(context_system::class, $contextlist->current());
 113      }
 114  
 115      /**
 116       * Test getting contexts for user who created a schedule for a report by another user
 117       */
 118      public function test_get_contexts_for_userid_schedule(): void {
 119          $this->resetAfterTest();
 120          $this->setAdminUser();
 121  
 122          /** @var core_reportbuilder_generator $generator */
 123          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 124          $report = $generator->create_report(['name' => 'Users', 'source' => users::class]);
 125  
 126          // Switch user, create a report schedule.
 127          $user = $this->getDataGenerator()->create_user();
 128          $this->setUser($user);
 129  
 130          $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
 131  
 132          $contextlist = $this->get_contexts_for_userid((int) $user->id, 'core_reportbuilder');
 133          $this->assertCount(1, $contextlist);
 134          $this->assertInstanceOf(context_system::class, $contextlist->current());
 135      }
 136  
 137      /**
 138       * Test getting users in given context
 139       */
 140      public function test_get_users_in_context(): void {
 141          $this->resetAfterTest();
 142  
 143          /** @var core_reportbuilder_generator $generator */
 144          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 145  
 146          // Switch user, create a report.
 147          $reportuser = $this->getDataGenerator()->create_user();
 148          $this->setUser($reportuser);
 149  
 150          $report = $generator->create_report(['name' => 'Users', 'source' => users::class]);
 151  
 152          // Switch user, create a report audience.
 153          $audienceuser = $this->getDataGenerator()->create_user();
 154          $this->setUser($audienceuser);
 155  
 156          $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => []]);
 157  
 158          // Switch user, create a report schedule.
 159          $scheduleuser = $this->getDataGenerator()->create_user();
 160          $this->setUser($scheduleuser);
 161  
 162          $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
 163  
 164          $userlist = new userlist(context_system::instance(), 'core_reportbuilder');
 165          provider::get_users_in_context($userlist);
 166  
 167          $this->assertEqualsCanonicalizing([
 168              $reportuser->id,
 169              $audienceuser->id,
 170              $scheduleuser->id,
 171          ], $userlist->get_userids());
 172      }
 173  
 174      /**
 175       * Test export of user data
 176       */
 177      public function test_export_user_data(): void {
 178          $this->resetAfterTest();
 179  
 180          $user = $this->getDataGenerator()->create_user();
 181          $this->setUser($user);
 182  
 183          /** @var core_reportbuilder_generator $generator */
 184          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 185  
 186          // Create some report elements for the user.
 187          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
 188          $audience = $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => [], 'heading' => 'Beans']);
 189          $schedule = $generator->create_schedule([
 190              'reportid' => $report->get('id'),
 191              'audiences' => json_encode([$audience->get_persistent()->get('id')]),
 192              'name' => 'My schedule',
 193          ]);
 194  
 195          $context = context_system::instance();
 196          $this->export_context_data_for_user((int) $user->id, $context, 'core_reportbuilder');
 197  
 198          /** @var \core_privacy\tests\request\content_writer $writer */
 199          $writer = writer::with_context($context);
 200          $this->assertTrue($writer->has_any_data());
 201  
 202          $subcontext = provider::get_export_subcontext($report);
 203  
 204          // Exported report data.
 205          $reportdata = $writer->get_data($subcontext);
 206          $this->assertEquals($report->get_formatted_name(), $reportdata->name);
 207          $this->assertEquals(users::get_name(), $reportdata->source);
 208          $this->assertEquals($user->id, $reportdata->usercreated);
 209          $this->assertEquals($user->id, $reportdata->usermodified);
 210          $this->assertNotEmpty($reportdata->timecreated);
 211          $this->assertNotEmpty($reportdata->timemodified);
 212  
 213          // Exported audience data.
 214          $audiencedata = $writer->get_related_data($subcontext, 'audiences')->data;
 215  
 216          $this->assertCount(1, $audiencedata);
 217          $audiencedata = reset($audiencedata);
 218  
 219          $audiencepersistent = $audience->get_persistent();
 220          $audienceclassname = $audiencepersistent->get('classname');
 221  
 222          $this->assertEquals($audienceclassname::instance()->get_name(), $audiencedata->classname);
 223          $this->assertEquals($audiencepersistent->get('configdata'), $audiencedata->configdata);
 224          $this->assertEquals($audiencepersistent->get_formatted_heading(), $audiencedata->heading);
 225          $this->assertEquals($user->id, $audiencedata->usercreated);
 226          $this->assertEquals($user->id, $audiencedata->usermodified);
 227          $this->assertNotEmpty($audiencedata->timecreated);
 228          $this->assertNotEmpty($audiencedata->timemodified);
 229  
 230          // Exported schedule data.
 231          $scheduledata = $writer->get_related_data($subcontext, 'schedules')->data;
 232  
 233          $this->assertCount(1, $scheduledata);
 234          $scheduledata = reset($scheduledata);
 235  
 236          $this->assertEquals($schedule->get_formatted_name(), $scheduledata->name);
 237          $this->assertEquals('Yes', $scheduledata->enabled);
 238          $this->assertEquals('Comma separated values (.csv)', $scheduledata->format);
 239          $this->assertNotEmpty($scheduledata->timescheduled);
 240          $this->assertEquals('None', $scheduledata->recurrence);
 241          $this->assertEquals('Schedule creator', $scheduledata->userviewas);
 242          $this->assertEquals(json_encode([$audiencepersistent->get('id')]), $scheduledata->audiences);
 243          $this->assertEquals($schedule->get('subject'), $scheduledata->subject);
 244          $this->assertEquals(format_text($schedule->get('message'), $schedule->get('messageformat')), $scheduledata->message);
 245          $this->assertEquals('Send message with empty report', $scheduledata->reportempty);
 246          $this->assertEquals($user->id, $scheduledata->usercreated);
 247          $this->assertEquals($user->id, $scheduledata->usermodified);
 248          $this->assertNotEmpty($scheduledata->timecreated);
 249          $this->assertNotEmpty($scheduledata->timemodified);
 250      }
 251  
 252      /**
 253       * Test export of user data where there is nothing to export
 254       */
 255      public function test_export_user_data_empty(): void {
 256          $this->resetAfterTest();
 257  
 258          $user = $this->getDataGenerator()->create_user();
 259  
 260          $context = context_system::instance();
 261          $this->export_context_data_for_user((int) $user->id, $context, 'core_reportbuilder');
 262  
 263          $this->assertFalse(writer::with_context($context)->has_any_data());
 264      }
 265  
 266      /**
 267       * Test to check export_user_preferences.
 268       */
 269      public function test_export_user_preferences(): void {
 270          $this->resetAfterTest();
 271  
 272          $user1 = $this->getDataGenerator()->create_user();
 273          $user2 = $this->getDataGenerator()->create_user();
 274          $this->setUser($user1);
 275  
 276          // Create report and set some filters for the user.
 277          $report1 = manager::create_report_persistent((object) [
 278              'type' => 1,
 279              'source' => 'class',
 280          ]);
 281          $filtervalues1 = [
 282              'task_log:name_operator' => 0,
 283              'task_log:name_value' => 'My task logs',
 284          ];
 285          user_filter_manager::set($report1->get('id'), $filtervalues1);
 286  
 287          // Add a filter for user2.
 288          $filtervalues1user2 = [
 289              'task_log:name_operator' => 0,
 290              'task_log:name_value' => 'My task logs user2',
 291          ];
 292          user_filter_manager::set($report1->get('id'), $filtervalues1user2, (int)$user2->id);
 293  
 294          // Create a second report and set some filters for the user.
 295          $report2 = manager::create_report_persistent((object) [
 296              'type' => 1,
 297              'source' => 'class',
 298          ]);
 299          $filtervalues2 = [
 300              'config_change:setting_operator' => 0,
 301              'config_change:setting_value' => str_repeat('A', 3000),
 302          ];
 303          user_filter_manager::set($report2->get('id'), $filtervalues2);
 304  
 305          // Switch to admin user (so we can validate preferences of our test user are still exported).
 306          $this->setAdminUser();
 307  
 308          // Export user preferences.
 309          provider::export_user_preferences((int)$user1->id);
 310          $writer = writer::with_context(context_system::instance());
 311          $prefs = $writer->get_user_preferences('core_reportbuilder');
 312  
 313          // Check that user preferences only contain the 2 preferences from user1.
 314          $this->assertCount(2, (array)$prefs);
 315  
 316          // Check that exported user preferences for report1 are correct.
 317          $report1key = 'reportbuilder-report-' . $report1->get('id');
 318          $this->assertEquals(json_encode($filtervalues1, JSON_PRETTY_PRINT), $prefs->$report1key->value);
 319  
 320          // Check that exported user preferences for report2 are correct.
 321          $report2key = 'reportbuilder-report-' . $report2->get('id');
 322          $this->assertEquals(json_encode($filtervalues2, JSON_PRETTY_PRINT), $prefs->$report2key->value);
 323      }
 324  }