Skip to content

Commit

Permalink
MDL-76221 reportbuilder: improve report test generator methods.
Browse files Browse the repository at this point in the history
Test generators for creating report columns, filters and conditions
now allow for setting all persistent properties.
  • Loading branch information
paulholden committed Jan 4, 2023
1 parent 121f5b6 commit 45818da
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 113 deletions.
9 changes: 3 additions & 6 deletions badges/tests/reportbuilder/datasource/badges_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,13 @@ public function test_datasource(): void {
$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);
Expand Down
5 changes: 1 addition & 4 deletions course/tests/reportbuilder/datasource/participants_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ public function test_participants_datasource(): void {
$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'),
Expand Down
3 changes: 1 addition & 2 deletions group/tests/reportbuilder/datasource/groups_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public function test_datasource_groupings(): void {

$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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ public function test_export(): void {
$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);
Expand Down
3 changes: 1 addition & 2 deletions reportbuilder/tests/external/reports/retrieve_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public function test_execute(): void {
$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.
Expand Down
30 changes: 27 additions & 3 deletions reportbuilder/tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ public function create_column($record): column {
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;
}

/**
Expand All @@ -100,7 +108,15 @@ public function create_filter($record): filter {
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;
}

/**
Expand All @@ -120,7 +136,15 @@ public function create_condition($record): filter {
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;
}

/**
Expand Down
59 changes: 59 additions & 0 deletions reportbuilder/tests/generator_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ public function test_create_column(): void {
$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
*/
Expand All @@ -79,6 +100,25 @@ public function test_create_filter(): void {
$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
*/
Expand All @@ -94,6 +134,25 @@ public function test_create_condition(): void {
$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
*/
Expand Down
20 changes: 8 additions & 12 deletions reportbuilder/tests/local/aggregation/avg_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ public function test_column_aggregation(): void {
$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([
Expand Down Expand Up @@ -91,14 +89,12 @@ public function test_column_aggregation_with_callback(): void {
$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);
Expand Down
10 changes: 4 additions & 6 deletions reportbuilder/tests/local/aggregation/count_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ public function test_column_aggregation(): void {
$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([
Expand Down
20 changes: 11 additions & 9 deletions reportbuilder/tests/local/aggregation/countdistinct_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public function test_column_aggregation(): void {
$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([
Expand Down Expand Up @@ -90,9 +90,11 @@ public function test_column_aggregation_multiple_fields(): void {
$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);
Expand Down
44 changes: 23 additions & 21 deletions reportbuilder/tests/local/aggregation/groupconcat_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ public function test_column_aggregation(): void {
$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'));
Expand Down Expand Up @@ -92,9 +92,11 @@ public function test_column_aggregation_multiple_fields(): void {
$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);
Expand All @@ -121,14 +123,14 @@ public function test_column_aggregation_with_callback(): void {
$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'));
Expand Down Expand Up @@ -170,14 +172,14 @@ public function test_datasource_aggregate_column_callback_with_null(): void {
$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'));
Expand Down
Loading

0 comments on commit 45818da

Please sign in to comment.