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