Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 401 and 403] [Versions 402 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_notes\reportbuilder\datasource;
  20  
  21  use core_notes_generator;
  22  use core_reportbuilder_generator;
  23  use core_reportbuilder_testcase;
  24  use core_reportbuilder\local\filters\{date, select, text};
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
  30  
  31  /**
  32   * Unit tests for notes datasource
  33   *
  34   * @package     core_notes
  35   * @covers      \core_notes\reportbuilder\datasource\notes
  36   * @copyright   2022 Paul Holden <paulh@moodle.com>
  37   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class notes_test extends core_reportbuilder_testcase {
  40  
  41      /**
  42       * Load required test libraries
  43       */
  44      public static function setUpBeforeClass(): void {
  45          global $CFG;
  46          require_once("{$CFG->dirroot}/notes/lib.php");
  47      }
  48  
  49      /**
  50       * Test default datasource
  51       */
  52      public function test_datasource_default(): void {
  53          $this->resetAfterTest();
  54  
  55          /** @var core_notes_generator $notesgenerator */
  56          $notesgenerator = $this->getDataGenerator()->get_plugin_generator('core_notes');
  57  
  58          // Our first user will create a course note.
  59          $course = $this->getDataGenerator()->create_course();
  60          $userone = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe']);
  61          $coursenote = $notesgenerator->create_instance(['courseid' => $course->id, 'userid' => $userone->id, 'content' => 'Course',
  62              'publishstate' => NOTES_STATE_PUBLIC]);
  63  
  64          // Our second user will create a personal and site note.
  65          $usertwo = $this->getDataGenerator()->create_user(['firstname' => 'Amy']);
  66          $personalnote = $notesgenerator->create_instance(['courseid' => SITEID, 'userid' => $usertwo->id, 'content' => 'Personal',
  67              'publishstate' => NOTES_STATE_DRAFT]);
  68  
  69          $this->waitForSecond(); // For consistent ordering we need distinct time for second user notes.
  70          $sitenote = $notesgenerator->create_instance(['courseid' => SITEID, 'userid' => $usertwo->id, 'content' => 'Site',
  71              'publishstate' => NOTES_STATE_SITE]);
  72  
  73          /** @var core_reportbuilder_generator $generator */
  74          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  75          $report = $generator->create_report(['name' => 'Notes', 'source' => notes::class, 'default' => 1]);
  76  
  77          $content = $this->get_custom_report_content($report->get('id'));
  78  
  79          // Default columns are recipient, publishstate, course, note, time created. Sorted by recipient and time created.
  80          $this->assertEquals([
  81              [fullname($usertwo), 'Personal notes', '', 'Personal', userdate($personalnote->created)],
  82              [fullname($usertwo), 'Site notes', '', 'Site', userdate($sitenote->created)],
  83              [fullname($userone), 'Course notes', $course->fullname, 'Course', userdate($coursenote->created)],
  84          ], array_map('array_values', $content));
  85      }
  86  
  87      /**
  88       * Test datasource columns that aren't added by default
  89       */
  90      public function test_datasource_non_default_columns(): void {
  91          global $DB;
  92  
  93          $this->resetAfterTest();
  94  
  95          $recipient = $this->getDataGenerator()->create_user();
  96          $author = $this->getDataGenerator()->create_user();
  97          $this->setUser($author);
  98  
  99          /** @var core_notes_generator $notesgenerator */
 100          $notesgenerator = $this->getDataGenerator()->get_plugin_generator('core_notes');
 101          $note = $notesgenerator->create_instance(['courseid' => SITEID, 'publishstate' => NOTES_STATE_SITE, 'content' => 'Cool',
 102              'userid' => $recipient->id,
 103          ]);
 104  
 105          // Manually update the created/modified date of the note.
 106          $note->created = 1654038000;
 107          $note->lastmodified = $note->created + HOURSECS;
 108          $DB->update_record('post', $note);
 109  
 110          /** @var core_reportbuilder_generator $generator */
 111          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 112          $report = $generator->create_report(['name' => 'Notes', 'source' => notes::class, 'default' => 0]);
 113  
 114          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'note:content']);
 115          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'note:timecreated']);
 116          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'note:timemodified']);
 117  
 118          // Ensure we can add data from both user entities.
 119          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'recipient:fullname']);
 120          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'author:fullname']);
 121  
 122          $content = $this->get_custom_report_content($report->get('id'));
 123          $this->assertCount(1, $content);
 124  
 125          $this->assertEquals([
 126              'Cool',
 127              userdate($note->created),
 128              userdate($note->lastmodified),
 129              fullname($recipient),
 130              fullname($author),
 131          ], array_values($content[0]));
 132      }
 133  
 134      /**
 135       * Data provider for {@see test_datasource_filters}
 136       *
 137       * @return array[]
 138       */
 139      public function datasource_filters_provider(): array {
 140          return [
 141              'Filter content' => ['content', 'Cool', 'note:content', [
 142                  'note:content_operator' => text::IS_EQUAL_TO,
 143                  'note:content_value' => 'Cool',
 144              ], true],
 145              'Filter content (no match)' => ['content', 'Cool', 'note:content', [
 146                  'note:content_operator' => text::DOES_NOT_CONTAIN,
 147                  'note:content_value' => 'Cool',
 148              ], false],
 149              'Filter publish state' => ['publishstate', 'site', 'note:publishstate', [
 150                  'note:publishstate_operator' => select::EQUAL_TO,
 151                  'note:publishstate_value' => 'site',
 152              ], true],
 153              'Filter publish state (no match)' => ['publishstate', 'site', 'note:publishstate', [
 154                  'note:publishstate_operator' => select::EQUAL_TO,
 155                  'note:publishstate_value' => 'public',
 156              ], false],
 157              'Filter time created' => ['created', 1654038000, 'note:timecreated', [
 158                  'note:timecreated_operator' => date::DATE_RANGE,
 159                  'note:timecreated_from' => 1622502000,
 160              ], true],
 161              'Filter time created (no match)' => ['created', 1654038000, 'note:timecreated', [
 162                  'note:timecreated_operator' => date::DATE_RANGE,
 163                  'note:timecreated_to' => 1622502000,
 164              ], false],
 165              'Filter time modified' => ['lastmodified', 1654038000, 'note:timemodified', [
 166                  'note:timemodified_operator' => date::DATE_RANGE,
 167                  'note:timemodified_from' => 1622502000,
 168              ], true],
 169              'Filter time modified (no match)' => ['lastmodified', 1654038000, 'note:timemodified', [
 170                  'note:timemodified_operator' => date::DATE_RANGE,
 171                  'note:timemodified_to' => 1622502000,
 172              ], false],
 173          ];
 174      }
 175  
 176      /**
 177       * Test datasource filters
 178       *
 179       * @param string $field
 180       * @param mixed $value
 181       * @param string $filtername
 182       * @param array $filtervalues
 183       * @param bool $expectmatch
 184       *
 185       * @dataProvider datasource_filters_provider
 186       */
 187      public function test_datasource_filters(
 188          string $field,
 189          $value,
 190          string $filtername,
 191          array $filtervalues,
 192          bool $expectmatch
 193      ): void {
 194          global $DB;
 195  
 196          $this->resetAfterTest();
 197  
 198          $recipient = $this->getDataGenerator()->create_user();
 199  
 200          /** @var core_notes_generator $notesgenerator */
 201          $notesgenerator = $this->getDataGenerator()->get_plugin_generator('core_notes');
 202  
 203          // Create default note, then manually override one of it's properties to use for filtering.
 204          $note = $notesgenerator->create_instance(['courseid' => SITEID, 'userid' => $recipient->id]);
 205          $DB->set_field('post', $field, $value, ['id' => $note->id]);
 206  
 207          /** @var core_reportbuilder_generator $generator */
 208          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 209  
 210          // Create report containing single recipient column, and given filter.
 211          $report = $generator->create_report(['name' => 'Notes', 'source' => notes::class, 'default' => 0]);
 212          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'recipient:fullname']);
 213  
 214          // Add filter, set it's values.
 215          $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
 216          $content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
 217  
 218          if ($expectmatch) {
 219              $this->assertCount(1, $content);
 220              $this->assertEquals(fullname($recipient), reset($content[0]));
 221          } else {
 222              $this->assertEmpty($content);
 223          }
 224      }
 225  
 226      /**
 227       * Stress test datasource
 228       *
 229       * In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
 230       */
 231      public function test_stress_datasource(): void {
 232          if (!PHPUNIT_LONGTEST) {
 233              $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
 234          }
 235  
 236          $this->resetAfterTest();
 237  
 238          $recipient = $this->getDataGenerator()->create_user();
 239  
 240          /** @var core_notes_generator $notesgenerator */
 241          $notesgenerator = $this->getDataGenerator()->get_plugin_generator('core_notes');
 242          $notesgenerator->create_instance(['courseid' => SITEID, 'userid' => $recipient->id]);
 243  
 244          $this->datasource_stress_test_columns(notes::class);
 245          $this->datasource_stress_test_columns_aggregation(notes::class);
 246          $this->datasource_stress_test_conditions(notes::class, 'note:content');
 247      }
 248  }