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 context_course;
|
|
|
22 |
use core_comment_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 comments datasource
|
|
|
34 |
*
|
|
|
35 |
* @package core_comment
|
|
|
36 |
* @covers \core_comment\reportbuilder\datasource\comments
|
|
|
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 comments_test extends core_reportbuilder_testcase {
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Test default datasource
|
|
|
44 |
*/
|
|
|
45 |
public function test_datasource_default(): void {
|
|
|
46 |
$this->resetAfterTest();
|
|
|
47 |
|
|
|
48 |
$course = $this->getDataGenerator()->create_course();
|
|
|
49 |
$coursecontext = context_course::instance($course->id);
|
|
|
50 |
|
|
|
51 |
/** @var core_comment_generator $generator */
|
|
|
52 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
|
|
|
53 |
|
|
|
54 |
// Our first user will create a single comment.
|
|
|
55 |
$userone = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe']);
|
|
|
56 |
$this->setUser($userone);
|
|
|
57 |
$useronecomment = $generator->create_comment([
|
|
|
58 |
'context' => $coursecontext,
|
|
|
59 |
'component' => 'block_comments',
|
|
|
60 |
'area' => 'page_comments',
|
|
|
61 |
])->add('Cool');
|
|
|
62 |
|
|
|
63 |
// Our second user will create a couple of comments.
|
|
|
64 |
$usertwo = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Amy']);
|
|
|
65 |
$this->setUser($usertwo);
|
|
|
66 |
$usertwocommentfirst = $generator->create_comment([
|
|
|
67 |
'context' => $coursecontext,
|
|
|
68 |
'component' => 'block_comments',
|
|
|
69 |
'area' => 'page_comments',
|
|
|
70 |
])->add('Super');
|
|
|
71 |
|
|
|
72 |
$this->waitForSecond(); // For consistent ordering we need distinct time for second user comments.
|
|
|
73 |
$usertwocommentsecond = $generator->create_comment([
|
|
|
74 |
'context' => $coursecontext,
|
|
|
75 |
'component' => 'block_comments',
|
|
|
76 |
'area' => 'page_comments',
|
|
|
77 |
])->add('Awesome');
|
|
|
78 |
|
|
|
79 |
/** @var core_reportbuilder_generator $generator */
|
|
|
80 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
81 |
$report = $generator->create_report(['name' => 'Blogs', 'source' => comments::class, 'default' => 1]);
|
|
|
82 |
|
|
|
83 |
$content = $this->get_custom_report_content($report->get('id'));
|
|
|
84 |
|
|
|
85 |
// Default columns are user, context, content, time created. Sorted by user and time created.
|
|
|
86 |
$contextname = $coursecontext->get_context_name();
|
|
|
87 |
$this->assertEquals([
|
|
|
88 |
[fullname($usertwo), $contextname, format_text('Super'), userdate($usertwocommentfirst->timecreated)],
|
|
|
89 |
[fullname($usertwo), $contextname, format_text('Awesome'), userdate($usertwocommentsecond->timecreated)],
|
|
|
90 |
[fullname($userone), $contextname, format_text('Cool'), userdate($useronecomment->timecreated)],
|
|
|
91 |
], array_map('array_values', $content));
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Test datasource columns that aren't added by default
|
|
|
96 |
*/
|
|
|
97 |
public function test_datasource_non_default_columns(): void {
|
|
|
98 |
$this->resetAfterTest();
|
|
|
99 |
$this->setAdminUser();
|
|
|
100 |
|
|
|
101 |
$course = $this->getDataGenerator()->create_course();
|
|
|
102 |
$courseurl = course_get_url($course);
|
|
|
103 |
$coursecontext = context_course::instance($course->id);
|
|
|
104 |
|
|
|
105 |
/** @var core_comment_generator $generator */
|
|
|
106 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
|
|
|
107 |
$generator->create_comment([
|
|
|
108 |
'context' => $coursecontext,
|
|
|
109 |
'component' => 'block_comments',
|
|
|
110 |
'area' => 'page_comments',
|
|
|
111 |
'content' => 'Cool',
|
|
|
112 |
]);
|
|
|
113 |
|
|
|
114 |
/** @var core_reportbuilder_generator $generator */
|
|
|
115 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
116 |
$report = $generator->create_report(['name' => 'Blogs', 'source' => comments::class, 'default' => 0]);
|
|
|
117 |
|
|
|
118 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'context:link']);
|
|
|
119 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:component']);
|
|
|
120 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:area']);
|
|
|
121 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:itemid']);
|
|
|
122 |
|
|
|
123 |
$content = $this->get_custom_report_content($report->get('id'));
|
|
|
124 |
$this->assertCount(1, $content);
|
|
|
125 |
|
|
|
126 |
$this->assertEquals([
|
|
|
127 |
"<a href=\"{$courseurl}\">{$coursecontext->get_context_name()}</a>",
|
|
|
128 |
'block_comments',
|
|
|
129 |
'page_comments',
|
|
|
130 |
0,
|
|
|
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 |
// Comment.
|
|
|
142 |
'Filter content' => ['comment:content', [
|
|
|
143 |
'comment:content_operator' => text::CONTAINS,
|
|
|
144 |
'comment:content_value' => 'Cool',
|
|
|
145 |
], true],
|
|
|
146 |
'Filter content (no match)' => ['comment:content', [
|
|
|
147 |
'comment:content_operator' => text::IS_EQUAL_TO,
|
|
|
148 |
'comment:content_value' => 'Beans',
|
|
|
149 |
], false],
|
|
|
150 |
'Filter time created' => ['comment:timecreated', [
|
|
|
151 |
'comment:timecreated_operator' => date::DATE_RANGE,
|
|
|
152 |
'comment:timecreated_from' => 1622502000,
|
|
|
153 |
], true],
|
|
|
154 |
'Filter time created (no match)' => ['comment:timecreated', [
|
|
|
155 |
'comment:timecreated_operator' => date::DATE_RANGE,
|
|
|
156 |
'comment:timecreated_to' => 1622502000,
|
|
|
157 |
], false],
|
|
|
158 |
|
|
|
159 |
// Context.
|
|
|
160 |
'Context level' => ['context:level', [
|
|
|
161 |
'context:level_operator' => select::EQUAL_TO,
|
|
|
162 |
'context:level_value' => CONTEXT_COURSE,
|
|
|
163 |
], true],
|
|
|
164 |
'Context level (no match)' => ['context:level', [
|
|
|
165 |
'context:level_operator' => select::EQUAL_TO,
|
|
|
166 |
'context:level_value' => CONTEXT_BLOCK,
|
|
|
167 |
], false],
|
|
|
168 |
|
|
|
169 |
// User.
|
|
|
170 |
'Filter user' => ['user:username', [
|
|
|
171 |
'user:username_operator' => text::IS_EQUAL_TO,
|
|
|
172 |
'user:username_value' => 'admin',
|
|
|
173 |
], true],
|
|
|
174 |
'Filter user (no match)' => ['user:username', [
|
|
|
175 |
'user:username_operator' => text::IS_EQUAL_TO,
|
|
|
176 |
'user:username_value' => 'lionel',
|
|
|
177 |
], false],
|
|
|
178 |
];
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
* Test datasource filters
|
|
|
183 |
*
|
|
|
184 |
* @param string $filtername
|
|
|
185 |
* @param array $filtervalues
|
|
|
186 |
* @param bool $expectmatch
|
|
|
187 |
*
|
|
|
188 |
* @dataProvider datasource_filters_provider
|
|
|
189 |
*/
|
|
|
190 |
public function test_datasource_filters(
|
|
|
191 |
string $filtername,
|
|
|
192 |
array $filtervalues,
|
|
|
193 |
bool $expectmatch
|
|
|
194 |
): void {
|
|
|
195 |
$this->resetAfterTest();
|
|
|
196 |
$this->setAdminUser();
|
|
|
197 |
|
|
|
198 |
$course = $this->getDataGenerator()->create_course();
|
|
|
199 |
$coursecontext = context_course::instance($course->id);
|
|
|
200 |
|
|
|
201 |
/** @var core_comment_generator $generator */
|
|
|
202 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
|
|
|
203 |
$generator->create_comment([
|
|
|
204 |
'context' => $coursecontext,
|
|
|
205 |
'component' => 'block_comments',
|
|
|
206 |
'area' => 'page_comments',
|
|
|
207 |
'content' => 'Cool',
|
|
|
208 |
]);
|
|
|
209 |
|
|
|
210 |
/** @var core_reportbuilder_generator $generator */
|
|
|
211 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
212 |
|
|
|
213 |
// Create report containing single column, and given filter.
|
|
|
214 |
$report = $generator->create_report(['name' => 'Tasks', 'source' => comments::class, 'default' => 0]);
|
|
|
215 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:component']);
|
|
|
216 |
|
|
|
217 |
// Add filter, set it's values.
|
|
|
218 |
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
|
|
|
219 |
$content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
|
|
|
220 |
|
|
|
221 |
if ($expectmatch) {
|
|
|
222 |
$this->assertCount(1, $content);
|
|
|
223 |
$this->assertEquals('block_comments', reset($content[0]));
|
|
|
224 |
} else {
|
|
|
225 |
$this->assertEmpty($content);
|
|
|
226 |
}
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
* Stress test datasource
|
|
|
231 |
*
|
|
|
232 |
* In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
|
|
|
233 |
*/
|
|
|
234 |
public function test_stress_datasource(): void {
|
|
|
235 |
if (!PHPUNIT_LONGTEST) {
|
|
|
236 |
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
$this->resetAfterTest();
|
|
|
240 |
$this->setAdminUser();
|
|
|
241 |
|
|
|
242 |
$course = $this->getDataGenerator()->create_course();
|
|
|
243 |
$coursecontext = context_course::instance($course->id);
|
|
|
244 |
|
|
|
245 |
/** @var core_comment_generator $generator */
|
|
|
246 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
|
|
|
247 |
$generator->create_comment([
|
|
|
248 |
'context' => $coursecontext,
|
|
|
249 |
'component' => 'block_comments',
|
|
|
250 |
'area' => 'page_comments',
|
|
|
251 |
'content' => 'Cool',
|
|
|
252 |
]);
|
|
|
253 |
|
|
|
254 |
$this->datasource_stress_test_columns(comments::class);
|
|
|
255 |
$this->datasource_stress_test_columns_aggregation(comments::class);
|
|
|
256 |
$this->datasource_stress_test_conditions(comments::class, 'comment:component');
|
|
|
257 |
}
|
|
|
258 |
}
|