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