Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   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\task;
  20  
  21  use advanced_testcase;
  22  use core_collator;
  23  use core_reportbuilder_generator;
  24  use core_reportbuilder\manager;
  25  use core_reportbuilder\local\filters\user;
  26  use core_reportbuilder\local\models\schedule;
  27  use core_reportbuilder\local\filters\text;
  28  use core_reportbuilder\reportbuilder\audience\manual;
  29  use core_user;
  30  use core_user\reportbuilder\datasource\users;
  31  
  32  /**
  33   * Unit tests for ad-hoc task for sending report schedule
  34   *
  35   * @package     core_reportbuilder
  36   * @covers      \core_reportbuilder\task\send_schedule
  37   * @copyright   2021 Paul Holden <paulh@moodle.com>
  38   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class send_schedule_test extends advanced_testcase {
  41  
  42      /**
  43       * Data provider for {@see test_execute_viewas_user}
  44       *
  45       * @return array[]
  46       */
  47      public function execute_report_viewas_user_provider(): array {
  48          return [
  49              'View report as schedule creator' => [schedule::REPORT_VIEWAS_CREATOR, null, 'admin', 'admin'],
  50              'View report as schedule recipient' => [schedule::REPORT_VIEWAS_RECIPIENT, null, 'userone', 'usertwo'],
  51              'View report as specific user' => [null, 'userone', 'userone', 'userone'],
  52          ];
  53      }
  54  
  55      /**
  56       * Test executing task for a schedule with differing "View as user" configuration
  57       *
  58       * @param int|null $viewasuser
  59       * @param string|null $viewasusername
  60       * @param string $useronesees
  61       * @param string $usertwosees
  62       *
  63       * @dataProvider execute_report_viewas_user_provider
  64       */
  65      public function test_execute_report_viewas_user(
  66          ?int $viewasuser,
  67          ?string $viewasusername,
  68          string $useronesees,
  69          string $usertwosees
  70      ): void {
  71          $this->preventResetByRollback();
  72          $this->resetAfterTest();
  73          $this->setAdminUser();
  74  
  75          $userone = $this->getDataGenerator()->create_user([
  76              'username' => 'userone',
  77              'email' => 'user1@example.com',
  78              'firstname' => 'Zoe',
  79              'lastname' => 'Zebra',
  80          ]);
  81          $usertwo = $this->getDataGenerator()->create_user([
  82              'username' => 'usertwo',
  83              'email' => 'user2@example.com',
  84              'firstname' => 'Henrietta',
  85              'lastname' => 'Hamster',
  86          ]);
  87  
  88          /** @var core_reportbuilder_generator $generator */
  89          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  90  
  91          // Create a report, with a single column and condition that the current user only sees themselves.
  92          $report = $generator->create_report(['name' => 'Myself', 'source' => users::class, 'default' => false]);
  93          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']);
  94  
  95          $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:userselect']);
  96          manager::get_report_from_persistent($report)
  97              ->set_condition_values(['user:userselect_operator' => user::USER_CURRENT]);
  98  
  99          // Add audience/schedule for our two test users.
 100          $audience = $generator->create_audience([
 101              'reportid' => $report->get('id'),
 102              'classname' => manual::class,
 103              'configdata' => [
 104                  'users' => [$userone->id, $usertwo->id],
 105              ],
 106          ]);
 107  
 108          // If "View as user" isn't specified, it should be the ID of the given "View as username".
 109          if ($viewasuser === null) {
 110              $viewasuser = core_user::get_user_by_username($viewasusername, '*', null, MUST_EXIST)->id;
 111          }
 112          $schedule = $generator->create_schedule([
 113              'reportid' => $report->get('id'),
 114              'name' => 'My schedule',
 115              'userviewas' => $viewasuser,
 116              'audiences' => json_encode([$audience->get_persistent()->get('id')]),
 117          ]);
 118  
 119          // Send the schedule, catch emails in sink (noting the users are sorted alphabetically).
 120          $sink = $this->redirectEmails();
 121  
 122          $this->expectOutputRegex("/^Sending schedule: My schedule\n" .
 123              "  Sending to: " . fullname($usertwo) . "\n" .
 124              "  Sending to: " . fullname($userone) . "\n" .
 125              "Sending schedule complete\n/"
 126          );
 127          $sendschedule = new send_schedule();
 128          $sendschedule->set_custom_data(['reportid' => $report->get('id'), 'scheduleid' => $schedule->get('id')]);
 129          $sendschedule->execute();
 130  
 131          $messages = $sink->get_messages();
 132          $this->assertCount(2, $messages);
 133  
 134          $sink->close();
 135  
 136          // Ensure caught messages are consistently ordered by recipient email prior to assertions.
 137          core_collator::asort_objects_by_property($messages, 'to');
 138          $messages = array_values($messages);
 139  
 140          $messageoneattachment = self::extract_message_attachment($messages[0]->body);
 141          $this->assertEquals($userone->email, $messages[0]->to);
 142          $this->assertStringEndsWith("Username\n{$useronesees}\n", $messageoneattachment);
 143  
 144          $messagetwoattachment = self::extract_message_attachment($messages[1]->body);
 145          $this->assertEquals($usertwo->email, $messages[1]->to);
 146          $this->assertStringEndsWith("Username\n{$usertwosees}\n", $messagetwoattachment);
 147      }
 148  
 149      /**
 150       * Test executing task for a schedule that is configured to not send empty reports
 151       */
 152      public function test_execute_report_empty(): void {
 153          $this->resetAfterTest();
 154          $this->setAdminUser();
 155  
 156          /** @var core_reportbuilder_generator $generator */
 157          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 158  
 159          // Create a report that won't return any data.
 160          $report = $generator->create_report(['name' => 'Myself', 'source' => users::class]);
 161  
 162          $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']);
 163          manager::get_report_from_persistent($report)->set_condition_values([
 164              'user:username_operator' => text::IS_EQUAL_TO,
 165              'user:username_value' => 'baconlettucetomato',
 166          ]);
 167  
 168          $audience = $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => []]);
 169          $schedule = $generator->create_schedule([
 170              'reportid' => $report->get('id'),
 171              'name' => 'My schedule',
 172              'audiences' => json_encode([$audience->get_persistent()->get('id')]),
 173              'reportempty' => schedule::REPORT_EMPTY_DONT_SEND,
 174          ]);
 175  
 176          $this->expectOutputString("Sending schedule: My schedule\n" .
 177              "  Empty report, skipping\n" .
 178              "Sending schedule complete\n");
 179          $sendschedule = new send_schedule();
 180          $sendschedule->set_custom_data(['reportid' => $report->get('id'), 'scheduleid' => $schedule->get('id')]);
 181          $sendschedule->execute();
 182      }
 183  
 184      /**
 185       * Test executing task for a schedule that contains no recipients
 186       */
 187      public function test_execute_schedule_no_recipients(): void {
 188          $this->resetAfterTest();
 189          $this->setAdminUser();
 190  
 191          /** @var core_reportbuilder_generator $generator */
 192          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 193  
 194          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
 195          $schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
 196  
 197          $this->expectOutputString("Sending schedule: My schedule\n" .
 198              "Sending schedule complete\n");
 199          $sendschedule = new send_schedule();
 200          $sendschedule->set_custom_data(['reportid' => $report->get('id'), 'scheduleid' => $schedule->get('id')]);
 201          $sendschedule->execute();
 202      }
 203  
 204      /**
 205       * Test executing task given invalid schedule data
 206       */
 207      public function test_execute_schedule_invalid(): void {
 208          $this->resetAfterTest();
 209          $this->setAdminUser();
 210  
 211          /** @var core_reportbuilder_generator $generator */
 212          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 213          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
 214  
 215          $this->expectOutputString("Invalid schedule\n");
 216          $sendschedule = new send_schedule();
 217          $sendschedule->set_custom_data(['reportid' => $report->get('id'), 'scheduleid' => 42]);
 218          $sendschedule->execute();
 219      }
 220  
 221      /**
 222       * Given a multi-part message in MIME format, return the base64 encoded attachment contained within
 223       *
 224       * @param string $messagebody
 225       * @return string
 226       */
 227      private static function extract_message_attachment(string $messagebody): string {
 228          $mimepart = preg_split('/Content-Disposition: attachment; filename="My schedule.csv"\s+/m', $messagebody);
 229  
 230          // Extract the base64 encoded content after the "Content-Disposition" header.
 231          preg_match_all('/^([A-Z0-9\/\+=]+)\s/im', $mimepart[1], $matches);
 232  
 233          return base64_decode(implode($matches[0]));
 234      }
 235  }