MDL-84000 reportbuilder: move testcase helper to autoloaded namespace.

Clean up some class imports while we're at it.
This commit is contained in:
Paul Holden 2024-12-11 17:19:53 +00:00
parent ce0193fcde
commit a0f26a114f
No known key found for this signature in database
GPG Key ID: A81A96D6045F6164
30 changed files with 282 additions and 397 deletions

View File

@ -20,13 +20,8 @@ namespace core_role\reportbuilder\datasource;
use core\context\course;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{date, select, text};
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
use core_reportbuilder\tests\core_reportbuilder_testcase;
/**
* Unit tests for roles datasource

View File

@ -20,14 +20,9 @@ namespace core_admin\reportbuilder\datasource;
use core\task\database_logger;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{boolean_select, date, duration, number, select, text};
use core_reportbuilder\task\send_schedules;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
use core_reportbuilder\tests\core_reportbuilder_testcase;
/**
* Unit tests for task logs datasource

View File

@ -20,14 +20,13 @@ namespace core_badges\reportbuilder\datasource;
use core_badges_generator;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{boolean_select, date, select, tags, text};
use core_reportbuilder\manager;
use core_reportbuilder\tests\core_reportbuilder_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
require_once("{$CFG->libdir}/badgeslib.php");
/**
@ -377,7 +376,7 @@ final class badges_test extends core_reportbuilder_testcase {
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Create report containing single username column, and given filter.
$report = $generator->create_report(['name' => 'My report', 'source' => badges::class, 'default' => 0]);
$report = $generator->create_report(['name' => 'Badges', 'source' => badges::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:name']);
// Add filter, set it's values.

View File

@ -20,13 +20,12 @@ namespace core_badges\reportbuilder\datasource;
use core_badges_generator;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{boolean_select, date, select, tags, text};
use core_reportbuilder\tests\core_reportbuilder_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
require_once("{$CFG->libdir}/badgeslib.php");
/**
@ -337,7 +336,7 @@ final class users_test extends core_reportbuilder_testcase {
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Create report containing single username column, and given filter.
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => 0]);
$report = $generator->create_report(['name' => 'Badges', 'source' => users::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']);
// Add filter, set it's values.

View File

@ -18,18 +18,12 @@ declare(strict_types=1);
namespace core_blog\reportbuilder\datasource;
use context_system;
use context_user;
use core_blog_generator;
use core_comment_generator;
use core\context\{system, user};
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{boolean_select, date, select, text};
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
use core_reportbuilder\tests\core_reportbuilder_testcase;
/**
* Unit tests for blogs datasource
@ -98,7 +92,7 @@ final class blogs_test extends core_reportbuilder_testcase {
// Add an attachment.
$blog->attachment = 1;
get_file_storage()->create_file_from_string([
'contextid' => context_system::instance()->id,
'contextid' => system::instance()->id,
'component' => 'blog',
'filearea' => 'attachment',
'itemid' => $blog->id,
@ -109,7 +103,7 @@ final class blogs_test extends core_reportbuilder_testcase {
/** @var core_comment_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
$generator->create_comment([
'context' => context_user::instance($user->id),
'context' => user::instance($user->id),
'component' => 'blog',
'area' => 'format_blog',
'itemid' => $blog->id,

View File

@ -18,17 +18,11 @@ declare(strict_types=1);
namespace core_cohort\reportbuilder\datasource;
use context_coursecat;
use context_system;
use core\context\{coursecat, system};
use core_customfield_generator;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{boolean_select, date, select, text};
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
use core_reportbuilder\tests\core_reportbuilder_testcase;
/**
* Unit tests for cohorts datasource
@ -47,7 +41,7 @@ final class cohorts_test extends core_reportbuilder_testcase {
$this->resetAfterTest();
// Test subject.
$contextsystem = context_system::instance();
$contextsystem = system::instance();
$cohortone = $this->getDataGenerator()->create_cohort([
'contextid' => $contextsystem->id,
'name' => 'Legends',
@ -56,7 +50,7 @@ final class cohorts_test extends core_reportbuilder_testcase {
]);
$category = $this->getDataGenerator()->create_category();
$contextcategory = context_coursecat::instance($category->id);
$contextcategory = coursecat::instance($category->id);
$cohorttwo = $this->getDataGenerator()->create_cohort([
'contextid' => $contextcategory->id,
'name' => 'Category cohort',
@ -153,7 +147,7 @@ final class cohorts_test extends core_reportbuilder_testcase {
], false],
'Filter context' => ['cohort:context', [
'cohort:context_operator' => select::EQUAL_TO,
'cohort:context_value' => context_system::instance()->id,
'cohort:context_value' => system::instance()->id,
], true],
'Filter content (no match)' => ['cohort:context', [
'cohort:context_operator' => select::EQUAL_TO,

View File

@ -18,16 +18,11 @@ declare(strict_types=1);
namespace core_comment\reportbuilder\datasource;
use context_course;
use core_comment_generator;
use core\context\{block, course};
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{date, select, text};
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
use core_reportbuilder\tests\core_reportbuilder_testcase;
/**
* Unit tests for comments datasource
@ -46,7 +41,7 @@ final class comments_test extends core_reportbuilder_testcase {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
$coursecontext = course::instance($course->id);
/** @var core_comment_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
@ -78,7 +73,7 @@ final class comments_test extends core_reportbuilder_testcase {
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Blogs', 'source' => comments::class, 'default' => 1]);
$report = $generator->create_report(['name' => 'Comments', 'source' => comments::class, 'default' => 1]);
$content = $this->get_custom_report_content($report->get('id'));
@ -99,8 +94,7 @@ final class comments_test extends core_reportbuilder_testcase {
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$courseurl = course_get_url($course);
$coursecontext = context_course::instance($course->id);
$coursecontext = course::instance($course->id);
/** @var core_comment_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
@ -113,9 +107,9 @@ final class comments_test extends core_reportbuilder_testcase {
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Blogs', 'source' => comments::class, 'default' => 0]);
$report = $generator->create_report(['name' => 'Comments', 'source' => comments::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'context:link']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'context:name']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:component']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:area']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:itemid']);
@ -124,7 +118,7 @@ final class comments_test extends core_reportbuilder_testcase {
$this->assertCount(1, $content);
$this->assertEquals([
"<a href=\"{$courseurl}\">{$coursecontext->get_context_name()}</a>",
$coursecontext->get_context_name(),
'block_comments',
'page_comments',
0,
@ -159,11 +153,11 @@ final class comments_test extends core_reportbuilder_testcase {
// Context.
'Context level' => ['context:level', [
'context:level_operator' => select::EQUAL_TO,
'context:level_value' => CONTEXT_COURSE,
'context:level_value' => course::LEVEL,
], true],
'Context level (no match)' => ['context:level', [
'context:level_operator' => select::EQUAL_TO,
'context:level_value' => CONTEXT_BLOCK,
'context:level_value' => block::LEVEL,
], false],
// User.
@ -196,12 +190,11 @@ final class comments_test extends core_reportbuilder_testcase {
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
/** @var core_comment_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
$generator->create_comment([
'context' => $coursecontext,
'context' => course::instance($course->id),
'component' => 'block_comments',
'area' => 'page_comments',
'content' => 'Cool',
@ -211,7 +204,7 @@ final class comments_test extends core_reportbuilder_testcase {
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Create report containing single column, and given filter.
$report = $generator->create_report(['name' => 'Tasks', 'source' => comments::class, 'default' => 0]);
$report = $generator->create_report(['name' => 'Comments', 'source' => comments::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:component']);
// Add filter, set it's values.
@ -240,12 +233,11 @@ final class comments_test extends core_reportbuilder_testcase {
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
/** @var core_comment_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
$generator->create_comment([
'context' => $coursecontext,
'context' => course::instance($course->id),
'component' => 'block_comments',
'area' => 'page_comments',
'content' => 'Cool',

View File

@ -20,13 +20,8 @@ namespace core_course\reportbuilder\datasource;
use core_course_category;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{category, select, text};
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
use core_reportbuilder\tests\core_reportbuilder_testcase;
/**
* Unit tests for course categories datasource

View File

@ -18,19 +18,10 @@ declare(strict_types=1);
namespace core_course\reportbuilder\datasource;
use context_course;
use core_reportbuilder_testcase;
use core\context\course;
use core_reportbuilder_generator;
use core_reportbuilder\local\filters\boolean_select;
use core_reportbuilder\local\filters\date;
use core_reportbuilder\local\filters\select;
use core_reportbuilder\local\filters\tags;
use core_reportbuilder\local\filters\text;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
use core_reportbuilder\local\filters\{boolean_select, date, select, tags, text};
use core_reportbuilder\tests\core_reportbuilder_testcase;
/**
* Unit tests for courses datasources
@ -97,7 +88,7 @@ final class courses_test extends core_reportbuilder_testcase {
// Add a course image.
get_file_storage()->create_file_from_string([
'contextid' => context_course::instance($course->id)->id,
'contextid' => course::instance($course->id)->id,
'component' => 'course',
'filearea' => 'overviewfiles',
'itemid' => 0,
@ -416,7 +407,7 @@ final class courses_test extends core_reportbuilder_testcase {
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Create report containing single column, and given filter.
$report = $generator->create_report(['name' => 'Tasks', 'source' => courses::class, 'default' => 0]);
$report = $generator->create_report(['name' => 'Courses', 'source' => courses::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname']);
// Add filter, set it's values.

View File

@ -20,21 +20,12 @@ namespace core_course\reportbuilder\datasource;
use completion_completion;
use completion_criteria_self;
use core_reportbuilder\local\filters\boolean_select;
use core_reportbuilder\local\filters\date;
use core_reportbuilder\local\filters\duration;
use core_reportbuilder\local\filters\select;
use core_reportbuilder\local\filters\text;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{boolean_select, date, duration, select, text};
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user;
use grade_item;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Course participants datasource tests
*

View File

@ -20,13 +20,8 @@ namespace core_files\reportbuilder\datasource;
use core\context\{course, coursecat, user};
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{boolean_select, date, filesize, select, text};
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
use core_reportbuilder\tests\core_reportbuilder_testcase;
/**
* Unit tests for files datasource

View File

@ -20,13 +20,8 @@ namespace core_group\reportbuilder\datasource;
use core_customfield_generator;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{boolean_select, date, select, text};
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
use core_reportbuilder\tests\core_reportbuilder_testcase;
/**
* Unit tests for groups datasource
@ -362,7 +357,7 @@ final class groups_test extends core_reportbuilder_testcase {
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Create report containing single column, and given filter.
$report = $generator->create_report(['name' => 'Tasks', 'source' => groups::class, 'default' => 0]);
$report = $generator->create_report(['name' => 'Groups', 'source' => groups::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'group:idnumber']);
// Add filter, set it's values.

View File

@ -20,13 +20,8 @@ namespace core_notes\reportbuilder\datasource;
use core_notes_generator;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{date, select, text};
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
use core_reportbuilder\tests\core_reportbuilder_testcase;
/**
* Unit tests for notes datasource

View File

@ -0,0 +1,204 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_reportbuilder\tests;
use advanced_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\manager;
use core_reportbuilder\local\helpers\{aggregation, report, user_filter_manager};
use core_reportbuilder\table\custom_report_table_view;
use Throwable;
/**
* Helper base class for reportbuilder unit tests
*
* @package core_reportbuilder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class core_reportbuilder_testcase extends advanced_testcase {
/**
* Retrieve content for given report as array of report data
*
* @param int $reportid
* @param int $pagesize
* @param array $filtervalues
* @return array[]
*/
protected function get_custom_report_content(int $reportid, int $pagesize = 30, array $filtervalues = []): array {
$records = [];
// Apply filter values.
user_filter_manager::set($reportid, $filtervalues);
// Create table instance.
$table = custom_report_table_view::create($reportid);
$table->setup();
$table->query_db($pagesize, false);
// Extract raw data.
foreach ($table->rawdata as $record) {
$records[] = $table->format_row($record);
}
$table->close_recordset();
return $records;
}
/**
* Stress test a report source by iterating over all it's columns, enabling sorting where possible and asserting we can
* create a report for each
*
* @param string $source
*/
protected function datasource_stress_test_columns(string $source): void {
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Stress columns', 'source' => $source, 'default' => 0]);
$instance = manager::get_report_from_persistent($report);
// Iterate over each available column, ensure each works correctly independent of any others.
foreach ($instance->get_columns() as $columnidentifier => $columninstance) {
$column = report::add_report_column($report->get('id'), $columnidentifier);
// Enable sorting of the column where possible.
if ($columninstance->get_is_sortable()) {
report::toggle_report_column_sorting($report->get('id'), $column->get('id'), true, SORT_DESC);
}
// We are only asserting the report returns content without errors, not the content itself.
try {
$content = $this->get_custom_report_content($report->get('id'));
$this->assertNotEmpty($content);
// Ensure appropriate debugging was triggered for deprecated column.
if ($columninstance->get_is_deprecated()) {
$this->assertDebuggingCalled(null, DEBUG_DEVELOPER);
}
} catch (Throwable $exception) {
$this->fail("Error for column '{$columnidentifier}': " . $exception->getMessage());
}
report::delete_report_column($report->get('id'), $column->get('id'));
}
}
/**
* Stress test a report source by iterating over all columns and asserting we can create a report while aggregating each
*
* @param string $source
*/
protected function datasource_stress_test_columns_aggregation(string $source): void {
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Stress aggregation', 'source' => $source, 'default' => 0]);
$instance = manager::get_report_from_persistent($report);
// Add every column.
$columndeprecatedcount = 0;
foreach ($instance->get_columns() as $columnidentifier => $column) {
$columndeprecatedcount += (int) $column->get_is_deprecated();
report::add_report_column($report->get('id'), $columnidentifier);
}
// Now iterate over each column, and apply all suitable aggregation types.
$columns = $instance->get_active_columns();
$this->assertDebuggingCalledCount($columndeprecatedcount, null,
array_fill(0, $columndeprecatedcount, DEBUG_DEVELOPER));
foreach ($columns as $column) {
$aggregations = aggregation::get_column_aggregations($column->get_type(), $column->get_disabled_aggregation());
foreach (array_keys($aggregations) as $aggregation) {
$column->get_persistent()->set('aggregation', $aggregation)->update();
// We are only asserting the report returns content without errors, not the content itself.
try {
$content = $this->get_custom_report_content($report->get('id'));
$this->assertNotEmpty($content);
// Ensure appropriate debugging was triggered for deprecated columns.
$this->assertDebuggingCalledCount($columndeprecatedcount, null,
array_fill(0, $columndeprecatedcount, DEBUG_DEVELOPER));
} catch (Throwable $exception) {
$this->fail("Error for column '{$column->get_unique_identifier()}' with aggregation '{$aggregation}': " .
$exception->getMessage());
}
}
// Reset the column aggregation.
$column->get_persistent()->set('aggregation', null)->update();
}
}
/**
* Stress test a report source by iterating over all it's conditions and asserting we can create a report using each
*
* @param string $source
* @param string $columnidentifier Should be a simple column, with as few fields and joins as possible, ideally selected
* from the base table itself
*/
protected function datasource_stress_test_conditions(string $source, string $columnidentifier): void {
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Stress conditions', 'source' => $source, 'default' => 0]);
$instance = manager::get_report_from_persistent($report);
// Add single column only (to ensure no conditions have reliance on any columns).
report::add_report_column($report->get('id'), $columnidentifier);
// Iterate over each available condition, ensure each works correctly independent of any others.
$conditionidentifiers = array_keys($instance->get_conditions());
foreach ($conditionidentifiers as $conditionidentifier) {
$condition = report::add_report_condition($report->get('id'), $conditionidentifier);
$conditioninstance = $instance->get_condition($condition->get('uniqueidentifier'));
/** @var \core_reportbuilder\local\filters\base $conditionclass */
$conditionclass = $conditioninstance->get_filter_class();
// Set report condition values in order to activate it.
$conditionvalues = $conditionclass::create($conditioninstance)->get_sample_values();
if (empty($conditionvalues)) {
debugging("Missing sample values from filter '{$conditionclass}'", DEBUG_DEVELOPER);
}
$instance->set_condition_values($conditionvalues);
// We are only asserting the report returns content without errors, not the content itself.
try {
$content = $this->get_custom_report_content($report->get('id'));
$this->assertIsArray($content);
// Ensure appropriate debugging was triggered for deprecated condition.
if ($conditioninstance->get_is_deprecated()) {
$this->assertDebuggingCalled(null, DEBUG_DEVELOPER);
}
} catch (Throwable $exception) {
$this->fail("Error for condition '{$conditionidentifier}': " . $exception->getMessage());
}
report::delete_report_condition($report->get('id'), $condition->get('id'));
}
}
}

View File

@ -16,12 +16,6 @@
declare(strict_types=1);
use core_reportbuilder\manager;
use core_reportbuilder\local\helpers\aggregation;
use core_reportbuilder\local\helpers\report;
use core_reportbuilder\local\helpers\user_filter_manager;
use core_reportbuilder\table\custom_report_table_view;
/**
* Helper base class for reportbuilder unit tests
*
@ -29,173 +23,5 @@ use core_reportbuilder\table\custom_report_table_view;
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class core_reportbuilder_testcase extends advanced_testcase {
/**
* Retrieve content for given report as array of report data
*
* @param int $reportid
* @param int $pagesize
* @param array $filtervalues
* @return array[]
*/
protected function get_custom_report_content(int $reportid, int $pagesize = 30, array $filtervalues = []): array {
$records = [];
// Apply filter values.
user_filter_manager::set($reportid, $filtervalues);
// Create table instance.
$table = custom_report_table_view::create($reportid);
$table->setup();
$table->query_db($pagesize, false);
// Extract raw data.
foreach ($table->rawdata as $record) {
$records[] = $table->format_row($record);
}
$table->close_recordset();
return $records;
}
/**
* Stress test a report source by iterating over all it's columns, enabling sorting where possible and asserting we can
* create a report for each
*
* @param string $source
*/
protected function datasource_stress_test_columns(string $source): void {
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Stress columns', 'source' => $source, 'default' => 0]);
$instance = manager::get_report_from_persistent($report);
// Iterate over each available column, ensure each works correctly independent of any others.
foreach ($instance->get_columns() as $columnidentifier => $columninstance) {
$column = report::add_report_column($report->get('id'), $columnidentifier);
// Enable sorting of the column where possible.
if ($columninstance->get_is_sortable()) {
report::toggle_report_column_sorting($report->get('id'), $column->get('id'), true, SORT_DESC);
}
// We are only asserting the report returns content without errors, not the content itself.
try {
$content = $this->get_custom_report_content($report->get('id'));
$this->assertNotEmpty($content);
// Ensure appropriate debugging was triggered for deprecated column.
if ($columninstance->get_is_deprecated()) {
$this->assertDebuggingCalled(null, DEBUG_DEVELOPER);
}
} catch (Throwable $exception) {
$this->fail("Error for column '{$columnidentifier}': " . $exception->getMessage());
}
report::delete_report_column($report->get('id'), $column->get('id'));
}
}
/**
* Stress test a report source by iterating over all columns and asserting we can create a report while aggregating each
*
* @param string $source
*/
protected function datasource_stress_test_columns_aggregation(string $source): void {
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Stress aggregation', 'source' => $source, 'default' => 0]);
$instance = manager::get_report_from_persistent($report);
// Add every column.
$columndeprecatedcount = 0;
foreach ($instance->get_columns() as $columnidentifier => $column) {
$columndeprecatedcount += (int) $column->get_is_deprecated();
report::add_report_column($report->get('id'), $columnidentifier);
}
// Now iterate over each column, and apply all suitable aggregation types.
$columns = $instance->get_active_columns();
$this->assertDebuggingCalledCount($columndeprecatedcount, null,
array_fill(0, $columndeprecatedcount, DEBUG_DEVELOPER));
foreach ($columns as $column) {
$aggregations = aggregation::get_column_aggregations($column->get_type(), $column->get_disabled_aggregation());
foreach (array_keys($aggregations) as $aggregation) {
$column->get_persistent()->set('aggregation', $aggregation)->update();
// We are only asserting the report returns content without errors, not the content itself.
try {
$content = $this->get_custom_report_content($report->get('id'));
$this->assertNotEmpty($content);
// Ensure appropriate debugging was triggered for deprecated columns.
$this->assertDebuggingCalledCount($columndeprecatedcount, null,
array_fill(0, $columndeprecatedcount, DEBUG_DEVELOPER));
} catch (Throwable $exception) {
$this->fail("Error for column '{$column->get_unique_identifier()}' with aggregation '{$aggregation}': " .
$exception->getMessage());
}
}
// Reset the column aggregation.
$column->get_persistent()->set('aggregation', null)->update();
}
}
/**
* Stress test a report source by iterating over all it's conditions and asserting we can create a report using each
*
* @param string $source
* @param string $columnidentifier Should be a simple column, with as few fields and joins as possible, ideally selected
* from the base table itself
*/
protected function datasource_stress_test_conditions(string $source, string $columnidentifier): void {
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Stress conditions', 'source' => $source, 'default' => 0]);
$instance = manager::get_report_from_persistent($report);
// Add single column only (to ensure no conditions have reliance on any columns).
report::add_report_column($report->get('id'), $columnidentifier);
// Iterate over each available condition, ensure each works correctly independent of any others.
$conditionidentifiers = array_keys($instance->get_conditions());
foreach ($conditionidentifiers as $conditionidentifier) {
$condition = report::add_report_condition($report->get('id'), $conditionidentifier);
$conditioninstance = $instance->get_condition($condition->get('uniqueidentifier'));
/** @var \core_reportbuilder\local\filters\base $conditionclass */
$conditionclass = $conditioninstance->get_filter_class();
// Set report condition values in order to activate it.
$conditionvalues = $conditionclass::create($conditioninstance)->get_sample_values();
if (empty($conditionvalues)) {
debugging("Missing sample values from filter '{$conditionclass}'", DEBUG_DEVELOPER);
}
$instance->set_condition_values($conditionvalues);
// We are only asserting the report returns content without errors, not the content itself.
try {
$content = $this->get_custom_report_content($report->get('id'));
$this->assertIsArray($content);
// Ensure appropriate debugging was triggered for deprecated condition.
if ($conditioninstance->get_is_deprecated()) {
$this->assertDebuggingCalled(null, DEBUG_DEVELOPER);
}
} catch (Throwable $exception) {
$this->fail("Error for condition '{$conditionidentifier}': " . $exception->getMessage());
}
report::delete_report_condition($report->get('id'), $condition->get('id'));
}
}
}
class_alias(\core_reportbuilder\tests\core_reportbuilder_testcase::class, \core_reportbuilder_testcase::class);

View File

@ -18,18 +18,13 @@ declare(strict_types=1);
namespace core_reportbuilder\local\aggregation;
use core_reportbuilder_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\manager;
use core_reportbuilder\local\report\column;
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users;
use stdClass;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for avg aggregation
*

View File

@ -18,15 +18,10 @@ declare(strict_types=1);
namespace core_reportbuilder\local\aggregation;
use core_reportbuilder_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for count aggregation
*

View File

@ -18,15 +18,10 @@ declare(strict_types=1);
namespace core_reportbuilder\local\aggregation;
use core_reportbuilder_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for count distinct aggregation
*

View File

@ -20,17 +20,12 @@ namespace core_reportbuilder\local\aggregation;
use core_badges_generator;
use core_badges\reportbuilder\datasource\badges;
use core_reportbuilder_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\manager;
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users;
use stdClass;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for group concatenation aggregation
*

View File

@ -18,18 +18,13 @@ declare(strict_types=1);
namespace core_reportbuilder\local\aggregation;
use core_reportbuilder_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\manager;
use core_reportbuilder\local\report\column;
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users;
use stdClass;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for group concatenation distinct aggregation
*

View File

@ -18,15 +18,10 @@ declare(strict_types=1);
namespace core_reportbuilder\local\aggregation;
use core_reportbuilder_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for max aggregation
*

View File

@ -18,15 +18,10 @@ declare(strict_types=1);
namespace core_reportbuilder\local\aggregation;
use core_reportbuilder_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for min aggregation
*

View File

@ -18,15 +18,10 @@ declare(strict_types=1);
namespace core_reportbuilder\local\aggregation;
use core_reportbuilder_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for sum aggregation
*

View File

@ -18,18 +18,13 @@ declare(strict_types=1);
namespace core_reportbuilder\local\aggregation;
use core_reportbuilder_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\manager;
use core_reportbuilder\local\report\column;
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users;
use stdClass;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for sum aggregation
*

View File

@ -20,7 +20,6 @@ namespace core_reportbuilder\local\helpers;
use core_customfield_generator;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\entities\course;
use core_reportbuilder\local\filters\boolean_select;
use core_reportbuilder\local\filters\date;
@ -28,13 +27,9 @@ use core_reportbuilder\local\filters\select;
use core_reportbuilder\local\filters\text;
use core_reportbuilder\local\report\column;
use core_reportbuilder\local\report\filter;
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_course\reportbuilder\datasource\{categories, courses};
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for custom fields helper
*

View File

@ -19,7 +19,6 @@ declare(strict_types=1);
namespace core_reportbuilder\local\helpers;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\entities\user;
use core_reportbuilder\local\filters\boolean_select;
use core_reportbuilder\local\filters\date;
@ -27,13 +26,9 @@ use core_reportbuilder\local\filters\select;
use core_reportbuilder\local\filters\text;
use core_reportbuilder\local\report\column;
use core_reportbuilder\local\report\filter;
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for user profile fields helper
*

View File

@ -18,18 +18,13 @@ declare(strict_types=1);
namespace core_reportbuilder;
use context_system;
use core\context\system;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users;
use stdClass;
use core_reportbuilder\local\models\report;
use core_reportbuilder\local\report\base;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users;
use stdClass;
/**
* Unit tests for the report manager class
@ -172,7 +167,7 @@ final class manager_test extends core_reportbuilder_testcase {
$this->assertInstanceOf(report::class, $report);
$this->assertEquals(base::TYPE_SYSTEM_REPORT, $report->get('type'));
$this->assertEquals(system_report_available::class, $report->get('source'));
$this->assertInstanceOf(context_system::class, $report->get_context());
$this->assertInstanceOf(system::class, $report->get_context());
}
/**

View File

@ -1,6 +1,11 @@
This file describes API changes in /reportbuilder/*
Information provided here is intended especially for developers.
=== 4.4.6 ===
* The `core_reportbuilder_testcase` class has been moved to new autoloaded `core_reportbuilder\tests\core_reportbuilder_testcase`
location, affected tests no longer have to manually require `/reportbuilder/tests/helpers.php`
=== 4.4.5 ===
* The `get_active_conditions` method of the base report class has a new `$checkavailable` parameter to determine whether to check

View File

@ -18,17 +18,10 @@ declare(strict_types=1);
namespace core_tag\reportbuilder\datasource;
use context_course;
use context_user;
use core\context\{course, user};
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{boolean_select, date, number, select};
use core_reportbuilder\local\filters\tags as tags_filter;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
use core_reportbuilder\local\filters\{boolean_select, date, number, select, tags as tags_filter};
use core_reportbuilder\tests\core_reportbuilder_testcase;
/**
* Unit tests for tags datasource
@ -47,14 +40,11 @@ final class tags_test extends core_reportbuilder_testcase {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user(['interests' => ['Pies']]);
$usercontext = context_user::instance($user->id);
$course = $this->getDataGenerator()->create_course(['tags' => ['Horses']]);
$coursecontext = context_course::instance($course->id);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Notes', 'source' => tags::class, 'default' => 1]);
$report = $generator->create_report(['name' => 'Tags', 'source' => tags::class, 'default' => 1]);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(2, $content);
@ -64,13 +54,13 @@ final class tags_test extends core_reportbuilder_testcase {
$this->assertEquals('Default collection', $collection);
$this->assertStringContainsString('Horses', $tag);
$this->assertEquals('No', $standard);
$this->assertEquals($coursecontext->get_context_name(), $context);
$this->assertEquals(course::instance($course->id)->get_context_name(), $context);
[$collection, $tag, $standard, $context] = array_values($content[1]);
$this->assertEquals('Default collection', $collection);
$this->assertStringContainsString('Pies', $tag);
$this->assertEquals('No', $standard);
$this->assertEquals($usercontext->get_context_name(), $context);
$this->assertEquals(user::instance($user->id)->get_context_name(), $context);
}
/**
@ -81,11 +71,10 @@ final class tags_test extends core_reportbuilder_testcase {
$this->getDataGenerator()->create_tag(['name' => 'Horses', 'description' => 'Neigh', 'flag' => 2]);
$course = $this->getDataGenerator()->create_course(['tags' => ['Horses']]);
$coursecontext = context_course::instance($course->id);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Notes', 'source' => tags::class, 'default' => 0]);
$report = $generator->create_report(['name' => 'Tags', 'source' => tags::class, 'default' => 0]);
// Collection.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'collection:default']);
@ -101,7 +90,7 @@ final class tags_test extends core_reportbuilder_testcase {
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'tag:timemodified']);
// Context.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'context:link']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'context:name']);
// Instance.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'instance:area']);
@ -124,7 +113,7 @@ final class tags_test extends core_reportbuilder_testcase {
$tagflagged,
$tagflagcount,
$tagtimemodified,
$contextlink,
$contextname,
$instancearea,
$instancecomponent,
$instanceitemtype,
@ -146,9 +135,10 @@ final class tags_test extends core_reportbuilder_testcase {
$this->assertEquals(2, $tagflagcount);
$this->assertNotEmpty($tagtimemodified);
// Context.
$this->assertEquals(course::instance($course->id)->get_context_name(), $contextname);
// Instance.
$this->assertEquals('<a href="' . $coursecontext->get_url() . '">' . $coursecontext->get_context_name() . '</a>',
$contextlink);
$this->assertEquals('Courses', $instancearea);
$this->assertEquals('core', $instancecomponent);
$this->assertEquals('course', $instanceitemtype);
@ -277,7 +267,7 @@ final class tags_test extends core_reportbuilder_testcase {
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Create report containing single tag name, and given filter.
$report = $generator->create_report(['name' => 'Tasks', 'source' => tags::class, 'default' => 0]);
$report = $generator->create_report(['name' => 'Tags', 'source' => tags::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'tag:name']);
// Add filter, set it's values.

View File

@ -18,19 +18,9 @@ declare(strict_types=1);
namespace core_user\reportbuilder\datasource;
use core_reportbuilder_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\local\filters\boolean_select;
use core_reportbuilder\local\filters\date;
use core_reportbuilder\local\filters\select;
use core_reportbuilder\local\filters\tags;
use core_reportbuilder\local\filters\text;
use core_reportbuilder\local\filters\user as user_filter;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
use core_reportbuilder\local\filters\{boolean_select, date, select, tags, text, user as user_filter};
use core_reportbuilder\tests\core_reportbuilder_testcase;
/**
* Unit tests for users datasource
@ -501,7 +491,7 @@ final class users_test extends core_reportbuilder_testcase {
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Create report containing single column, and given filter.
$report = $generator->create_report(['name' => 'Tasks', 'source' => users::class, 'default' => 0]);
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']);
// Add filter, set it's values.