| 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_comment\reportbuilder\datasource;
|
|
|
20 |
|
|
|
21 |
use core_comment_generator;
|
| 1441 |
ariadna |
22 |
use core\context\{block, course};
|
| 1 |
efrain |
23 |
use core_reportbuilder_generator;
|
|
|
24 |
use core_reportbuilder\local\filters\{date, select, text};
|
| 1441 |
ariadna |
25 |
use core_reportbuilder\tests\core_reportbuilder_testcase;
|
| 1 |
efrain |
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Unit tests for comments datasource
|
|
|
29 |
*
|
|
|
30 |
* @package core_comment
|
|
|
31 |
* @covers \core_comment\reportbuilder\datasource\comments
|
|
|
32 |
* @copyright 2022 Paul Holden <paulh@moodle.com>
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
| 1441 |
ariadna |
35 |
final class comments_test extends core_reportbuilder_testcase {
|
| 1 |
efrain |
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Test default datasource
|
|
|
39 |
*/
|
|
|
40 |
public function test_datasource_default(): void {
|
|
|
41 |
$this->resetAfterTest();
|
|
|
42 |
|
|
|
43 |
$course = $this->getDataGenerator()->create_course();
|
| 1441 |
ariadna |
44 |
$coursecontext = course::instance($course->id);
|
| 1 |
efrain |
45 |
|
|
|
46 |
/** @var core_comment_generator $generator */
|
|
|
47 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
|
|
|
48 |
|
|
|
49 |
// Our first user will create a single comment.
|
|
|
50 |
$userone = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe']);
|
|
|
51 |
$this->setUser($userone);
|
|
|
52 |
$useronecomment = $generator->create_comment([
|
|
|
53 |
'context' => $coursecontext,
|
|
|
54 |
'component' => 'block_comments',
|
|
|
55 |
'area' => 'page_comments',
|
|
|
56 |
])->add('Cool');
|
|
|
57 |
|
|
|
58 |
// Our second user will create a couple of comments.
|
|
|
59 |
$usertwo = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Amy']);
|
|
|
60 |
$this->setUser($usertwo);
|
|
|
61 |
$usertwocommentfirst = $generator->create_comment([
|
|
|
62 |
'context' => $coursecontext,
|
|
|
63 |
'component' => 'block_comments',
|
|
|
64 |
'area' => 'page_comments',
|
|
|
65 |
])->add('Super');
|
|
|
66 |
|
|
|
67 |
$this->waitForSecond(); // For consistent ordering we need distinct time for second user comments.
|
|
|
68 |
$usertwocommentsecond = $generator->create_comment([
|
|
|
69 |
'context' => $coursecontext,
|
|
|
70 |
'component' => 'block_comments',
|
|
|
71 |
'area' => 'page_comments',
|
|
|
72 |
])->add('Awesome');
|
|
|
73 |
|
|
|
74 |
/** @var core_reportbuilder_generator $generator */
|
|
|
75 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
| 1441 |
ariadna |
76 |
$report = $generator->create_report(['name' => 'Comments', 'source' => comments::class, 'default' => 1]);
|
| 1 |
efrain |
77 |
|
|
|
78 |
$content = $this->get_custom_report_content($report->get('id'));
|
|
|
79 |
|
|
|
80 |
// Default columns are user, context, content, time created. Sorted by user and time created.
|
|
|
81 |
$contextname = $coursecontext->get_context_name();
|
|
|
82 |
$this->assertEquals([
|
|
|
83 |
[fullname($usertwo), $contextname, format_text('Super'), userdate($usertwocommentfirst->timecreated)],
|
|
|
84 |
[fullname($usertwo), $contextname, format_text('Awesome'), userdate($usertwocommentsecond->timecreated)],
|
|
|
85 |
[fullname($userone), $contextname, format_text('Cool'), userdate($useronecomment->timecreated)],
|
|
|
86 |
], array_map('array_values', $content));
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Test datasource columns that aren't added by default
|
|
|
91 |
*/
|
|
|
92 |
public function test_datasource_non_default_columns(): void {
|
|
|
93 |
$this->resetAfterTest();
|
|
|
94 |
$this->setAdminUser();
|
|
|
95 |
|
|
|
96 |
$course = $this->getDataGenerator()->create_course();
|
| 1441 |
ariadna |
97 |
$coursecontext = course::instance($course->id);
|
| 1 |
efrain |
98 |
|
|
|
99 |
/** @var core_comment_generator $generator */
|
|
|
100 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
|
|
|
101 |
$generator->create_comment([
|
|
|
102 |
'context' => $coursecontext,
|
|
|
103 |
'component' => 'block_comments',
|
|
|
104 |
'area' => 'page_comments',
|
|
|
105 |
'content' => 'Cool',
|
|
|
106 |
]);
|
|
|
107 |
|
|
|
108 |
/** @var core_reportbuilder_generator $generator */
|
|
|
109 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
| 1441 |
ariadna |
110 |
$report = $generator->create_report(['name' => 'Comments', 'source' => comments::class, 'default' => 0]);
|
| 1 |
efrain |
111 |
|
| 1441 |
ariadna |
112 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'context:name']);
|
| 1 |
efrain |
113 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:component']);
|
|
|
114 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:area']);
|
|
|
115 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:itemid']);
|
|
|
116 |
|
|
|
117 |
$content = $this->get_custom_report_content($report->get('id'));
|
|
|
118 |
$this->assertCount(1, $content);
|
|
|
119 |
|
|
|
120 |
$this->assertEquals([
|
| 1441 |
ariadna |
121 |
$coursecontext->get_context_name(),
|
| 1 |
efrain |
122 |
'block_comments',
|
|
|
123 |
'page_comments',
|
|
|
124 |
0,
|
|
|
125 |
], array_values($content[0]));
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Data provider for {@see test_datasource_filters}
|
|
|
130 |
*
|
|
|
131 |
* @return array[]
|
|
|
132 |
*/
|
| 1441 |
ariadna |
133 |
public static function datasource_filters_provider(): array {
|
| 1 |
efrain |
134 |
return [
|
|
|
135 |
// Comment.
|
|
|
136 |
'Filter content' => ['comment:content', [
|
|
|
137 |
'comment:content_operator' => text::CONTAINS,
|
|
|
138 |
'comment:content_value' => 'Cool',
|
|
|
139 |
], true],
|
|
|
140 |
'Filter content (no match)' => ['comment:content', [
|
|
|
141 |
'comment:content_operator' => text::IS_EQUAL_TO,
|
|
|
142 |
'comment:content_value' => 'Beans',
|
|
|
143 |
], false],
|
|
|
144 |
'Filter time created' => ['comment:timecreated', [
|
|
|
145 |
'comment:timecreated_operator' => date::DATE_RANGE,
|
|
|
146 |
'comment:timecreated_from' => 1622502000,
|
|
|
147 |
], true],
|
|
|
148 |
'Filter time created (no match)' => ['comment:timecreated', [
|
|
|
149 |
'comment:timecreated_operator' => date::DATE_RANGE,
|
|
|
150 |
'comment:timecreated_to' => 1622502000,
|
|
|
151 |
], false],
|
|
|
152 |
|
|
|
153 |
// Context.
|
|
|
154 |
'Context level' => ['context:level', [
|
|
|
155 |
'context:level_operator' => select::EQUAL_TO,
|
| 1441 |
ariadna |
156 |
'context:level_value' => course::LEVEL,
|
| 1 |
efrain |
157 |
], true],
|
|
|
158 |
'Context level (no match)' => ['context:level', [
|
|
|
159 |
'context:level_operator' => select::EQUAL_TO,
|
| 1441 |
ariadna |
160 |
'context:level_value' => block::LEVEL,
|
| 1 |
efrain |
161 |
], false],
|
|
|
162 |
|
|
|
163 |
// User.
|
|
|
164 |
'Filter user' => ['user:username', [
|
|
|
165 |
'user:username_operator' => text::IS_EQUAL_TO,
|
|
|
166 |
'user:username_value' => 'admin',
|
|
|
167 |
], true],
|
|
|
168 |
'Filter user (no match)' => ['user:username', [
|
|
|
169 |
'user:username_operator' => text::IS_EQUAL_TO,
|
|
|
170 |
'user:username_value' => 'lionel',
|
|
|
171 |
], false],
|
|
|
172 |
];
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
/**
|
|
|
176 |
* Test datasource filters
|
|
|
177 |
*
|
|
|
178 |
* @param string $filtername
|
|
|
179 |
* @param array $filtervalues
|
|
|
180 |
* @param bool $expectmatch
|
|
|
181 |
*
|
|
|
182 |
* @dataProvider datasource_filters_provider
|
|
|
183 |
*/
|
|
|
184 |
public function test_datasource_filters(
|
|
|
185 |
string $filtername,
|
|
|
186 |
array $filtervalues,
|
|
|
187 |
bool $expectmatch
|
|
|
188 |
): void {
|
|
|
189 |
$this->resetAfterTest();
|
|
|
190 |
$this->setAdminUser();
|
|
|
191 |
|
|
|
192 |
$course = $this->getDataGenerator()->create_course();
|
|
|
193 |
|
|
|
194 |
/** @var core_comment_generator $generator */
|
|
|
195 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
|
|
|
196 |
$generator->create_comment([
|
| 1441 |
ariadna |
197 |
'context' => course::instance($course->id),
|
| 1 |
efrain |
198 |
'component' => 'block_comments',
|
|
|
199 |
'area' => 'page_comments',
|
|
|
200 |
'content' => 'Cool',
|
|
|
201 |
]);
|
|
|
202 |
|
|
|
203 |
/** @var core_reportbuilder_generator $generator */
|
|
|
204 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
205 |
|
|
|
206 |
// Create report containing single column, and given filter.
|
| 1441 |
ariadna |
207 |
$report = $generator->create_report(['name' => 'Comments', 'source' => comments::class, 'default' => 0]);
|
| 1 |
efrain |
208 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:component']);
|
|
|
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('block_comments', 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 |
$this->setAdminUser();
|
|
|
234 |
|
|
|
235 |
$course = $this->getDataGenerator()->create_course();
|
|
|
236 |
|
|
|
237 |
/** @var core_comment_generator $generator */
|
|
|
238 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
|
|
|
239 |
$generator->create_comment([
|
| 1441 |
ariadna |
240 |
'context' => course::instance($course->id),
|
| 1 |
efrain |
241 |
'component' => 'block_comments',
|
|
|
242 |
'area' => 'page_comments',
|
|
|
243 |
'content' => 'Cool',
|
|
|
244 |
]);
|
|
|
245 |
|
|
|
246 |
$this->datasource_stress_test_columns(comments::class);
|
|
|
247 |
$this->datasource_stress_test_columns_aggregation(comments::class);
|
|
|
248 |
$this->datasource_stress_test_conditions(comments::class, 'comment:component');
|
|
|
249 |
}
|
|
|
250 |
}
|