MDL-76221 reportbuilder: improve report test generator methods.

Test generators for creating report columns, filters and conditions
now allow for setting all persistent properties.
This commit is contained in:
Paul Holden 2022-11-17 21:54:37 +00:00
parent 121f5b6300
commit 45818da292
17 changed files with 182 additions and 113 deletions

View File

@ -72,16 +72,13 @@ class badges_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Badges', 'source' => badges::class, 'default' => 0]);
// Badge course.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname'])
->set_many(['sortenabled' => true, 'sortdirection' => SORT_ASC])->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname', 'sortenabled' => 1]);
// Badge name.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:name'])
->set_many(['sortenabled' => true, 'sortdirection' => SORT_ASC])->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:name', 'sortenabled' => 1]);
// User fullname.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname'])
->set_many(['sortenabled' => true, 'sortdirection' => SORT_ASC])->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname', 'sortenabled' => 1]);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(3, $content);

View File

@ -97,10 +97,7 @@ class participants_test extends core_reportbuilder_testcase {
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'user:fullname']);
// Order by enrolment method.
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'enrolment:method'])
->set('sortenabled', true)
->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'enrolment:method', 'sortenabled' => 1]);
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'group:name']);
$generator->create_column(['reportid' => $report->get('id'),

View File

@ -86,8 +86,7 @@ class groups_test extends core_reportbuilder_testcase {
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'grouping:name']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'group:name'])
->set('sortenabled', true)->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'group:name', 'sortenabled' => 1]);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(2, $content);

View File

@ -48,8 +48,12 @@ class custom_report_data_exporter_test extends advanced_testcase {
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname'])
->set_many(['heading' => 'Lovely user', 'sortenabled' => true])->update();
$generator->create_column([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'user:fullname',
'heading' => 'Lovely user',
'sortenabled' => 1,
]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$reportinstance = manager::get_report_from_persistent($report);

View File

@ -53,8 +53,7 @@ class retrieve_test extends externallib_advanced_testcase {
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname'])
->set('sortenabled', true)->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname', 'sortenabled' => 1]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
// There are three users (admin plus the two previouly created), but we're paging the first two only.

View File

@ -80,7 +80,15 @@ class core_reportbuilder_generator extends component_generator_base {
throw new coding_exception('Record must contain \'uniqueidentifier\' property');
}
return helper::add_report_column($record['reportid'], $record['uniqueidentifier']);
$column = helper::add_report_column($record['reportid'], $record['uniqueidentifier']);
// Update additional record properties.
unset($record['reportid'], $record['uniqueidentifier']);
if ($properties = column::properties_filter((object) $record)) {
$column->set_many($properties)->update();
}
return $column;
}
/**
@ -100,7 +108,15 @@ class core_reportbuilder_generator extends component_generator_base {
throw new coding_exception('Record must contain \'uniqueidentifier\' property');
}
return helper::add_report_filter($record['reportid'], $record['uniqueidentifier']);
$filter = helper::add_report_filter($record['reportid'], $record['uniqueidentifier']);
// Update additional record properties.
unset($record['reportid'], $record['uniqueidentifier']);
if ($properties = filter::properties_filter((object) $record)) {
$filter->set_many($properties)->update();
}
return $filter;
}
/**
@ -120,7 +136,15 @@ class core_reportbuilder_generator extends component_generator_base {
throw new coding_exception('Record must contain \'uniqueidentifier\' property');
}
return helper::add_report_condition($record['reportid'], $record['uniqueidentifier']);
$condition = helper::add_report_condition($record['reportid'], $record['uniqueidentifier']);
// Update additional record properties.
unset($record['reportid'], $record['uniqueidentifier']);
if ($properties = filter::properties_filter((object) $record)) {
$condition->set_many($properties)->update();
}
return $condition;
}
/**

View File

@ -64,6 +64,27 @@ class generator_test extends advanced_testcase {
$this->assertTrue(column::record_exists($column->get('id')));
}
/**
* Test creating a column, specifying additional properties
*/
public function test_create_column_additional_properties(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => 0]);
$column = $generator->create_column([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'user:lastname',
'heading' => 'My pants',
'sortenabled' => 1,
]);
$this->assertEquals('My pants', $column->get('heading'));
$this->assertTrue($column->get('sortenabled'));
}
/**
* Test creating a filter
*/
@ -79,6 +100,25 @@ class generator_test extends advanced_testcase {
$this->assertTrue(filter::record_exists($filter->get('id')));
}
/**
* Test creating a filter, specifying additional properties
*/
public function test_create_filter_additional_properties(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => 0]);
$filter = $generator->create_filter([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'user:lastname',
'heading' => 'My pants',
]);
$this->assertEquals('My pants', $filter->get('heading'));
}
/**
* Test creating a condition
*/
@ -94,6 +134,25 @@ class generator_test extends advanced_testcase {
$this->assertTrue(filter::record_exists($condition->get('id')));
}
/**
* Test creating a condition, specifying additional properties
*/
public function test_create_condition_additional_properties(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => 0]);
$condition = $generator->create_condition([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'user:lastname',
'heading' => 'My pants',
]);
$this->assertEquals('My pants', $condition->get('heading'));
}
/**
* Test creating an audience
*/

View File

@ -54,14 +54,12 @@ class avg_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// First column, sorted.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname'])
->set('sortenabled', true)
->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended'])
->set('aggregation', avg::get_class_name())
->update();
$generator->create_column(
['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => avg::get_class_name()]
);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertEquals([
@ -91,14 +89,12 @@ class avg_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// First column, sorted.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname'])
->set('sortenabled', true)
->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended'])
->set('aggregation', avg::get_class_name())
->update();
$generator->create_column(
['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => avg::get_class_name()]
);
// Set callback to format the column (hack column definition to ensure callbacks are executed).
$instance = manager::get_report_from_persistent($report);

View File

@ -54,14 +54,12 @@ class count_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// First column, sorted.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname'])
->set('sortenabled', true)
->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname'])
->set('aggregation', count::get_class_name())
->update();
$generator->create_column([
'reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname', 'aggregation' => count::get_class_name()]
);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertEquals([

View File

@ -54,14 +54,14 @@ class countdistinct_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// First column, sorted.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname'])
->set('sortenabled', true)
->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname'])
->set('aggregation', countdistinct::get_class_name())
->update();
$generator->create_column([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'user:lastname',
'aggregation' => countdistinct::get_class_name(),
]);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertEquals([
@ -90,9 +90,11 @@ class countdistinct_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname'])
->set('aggregation', countdistinct::get_class_name())
->update();
$generator->create_column([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'user:fullname',
'aggregation' => countdistinct::get_class_name(),
]);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(1, $content);

View File

@ -56,14 +56,14 @@ class groupconcat_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// First column, sorted.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname'])
->set('sortenabled', true)
->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname'])
->set('aggregation', groupconcat::get_class_name())
->update();
$generator->create_column([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'user:lastname',
'aggregation' => groupconcat::get_class_name(),
]);
// Assert lastname column was aggregated, and sorted predictably.
$content = $this->get_custom_report_content($report->get('id'));
@ -92,9 +92,11 @@ class groupconcat_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullnamewithlink'])
->set('aggregation', groupconcat::get_class_name())
->update();
$generator->create_column([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'user:fullnamewithlink',
'aggregation' => groupconcat::get_class_name(),
]);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(1, $content);
@ -121,14 +123,14 @@ class groupconcat_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// First column, sorted.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname'])
->set('sortenabled', true)
->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:confirmed'])
->set('aggregation', groupconcat::get_class_name())
->update();
$generator->create_column([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'user:confirmed',
'aggregation' => groupconcat::get_class_name(),
]);
// Assert confirmed column was aggregated, and sorted predictably with callback applied.
$content = $this->get_custom_report_content($report->get('id'));
@ -170,14 +172,14 @@ class groupconcat_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Badges', 'source' => badges::class, 'default' => 0]);
// First column, sorted.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:name'])
->set('sortenabled', true)
->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:name', 'sortenabled' => 1]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:description'])
->set('aggregation', groupconcat::get_class_name())
->update();
$generator->create_column([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'user:description',
'aggregation' => groupconcat::get_class_name(),
]);
// Assert description column was aggregated, with callbacks accounting for null values.
$content = $this->get_custom_report_content($report->get('id'));

View File

@ -66,14 +66,14 @@ class groupconcatdistinct_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// First column, sorted.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname'])
->set('sortenabled', true)
->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname'])
->set('aggregation', groupconcatdistinct::get_class_name())
->update();
$generator->create_column([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'user:lastname',
'aggregation' => groupconcatdistinct::get_class_name(),
]);
// Assert lastname column was aggregated, and sorted predictably.
$content = $this->get_custom_report_content($report->get('id'));
@ -102,9 +102,11 @@ class groupconcatdistinct_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullnamewithlink'])
->set('aggregation', groupconcatdistinct::get_class_name())
->update();
$generator->create_column([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'user:fullnamewithlink',
'aggregation' => groupconcatdistinct::get_class_name(),
]);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(1, $content);
@ -131,14 +133,14 @@ class groupconcatdistinct_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// First column, sorted.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname'])
->set('sortenabled', true)
->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:confirmed'])
->set('aggregation', groupconcatdistinct::get_class_name())
->update();
$generator->create_column([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'user:confirmed',
'aggregation' => groupconcatdistinct::get_class_name(),
]);
// Assert confirmed column was aggregated, and sorted predictably with callback applied.
$content = $this->get_custom_report_content($report->get('id'));

View File

@ -53,14 +53,12 @@ class max_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// First column, sorted.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname'])
->set('sortenabled', true)
->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended'])
->set('aggregation', max::get_class_name())
->update();
$generator->create_column(
['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => max::get_class_name()]
);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertEquals([

View File

@ -53,14 +53,12 @@ class min_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// First column, sorted.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname'])
->set('sortenabled', true)
->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended'])
->set('aggregation', min::get_class_name())
->update();
$generator->create_column([
'reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => min::get_class_name()]
);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertEquals([

View File

@ -53,14 +53,12 @@ class percent_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// First column, sorted.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname'])
->set('sortenabled', true)
->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended'])
->set('aggregation', percent::get_class_name())
->update();
$generator->create_column([
'reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => percent::get_class_name()]
);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertEquals([

View File

@ -55,14 +55,12 @@ class sum_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// First column, sorted.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname'])
->set('sortenabled', true)
->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended'])
->set('aggregation', sum::get_class_name())
->update();
$generator->create_column([
'reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => sum::get_class_name()
]);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertEquals([
@ -92,14 +90,12 @@ class sum_test extends core_reportbuilder_testcase {
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// First column, sorted.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname'])
->set('sortenabled', true)
->update();
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended'])
->set('aggregation', sum::get_class_name())
->update();
$generator->create_column(
['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => sum::get_class_name()]
);
// Set callback to format the column (hack column definition to ensure callbacks are executed).
$instance = manager::get_report_from_persistent($report);

View File

@ -10,7 +10,7 @@ Information provided here is intended especially for developers.
- `data-force-table` to force table view
* New optional parameter `pagesize` in external method `core_reportbuilder_reports_get` to set the displayed rows per page.
* Javascript reports repository module method `getReport` updated to accept new pagesize parameter.
* The `create_[column|filter|condition]` test generator methods now allow for setting all persistent properties
=== 4.1 ===