diff --git a/.upgradenotes/MDL-84000-2024121209313638.yml b/.upgradenotes/MDL-84000-2024121209313638.yml new file mode 100644 index 00000000000..526c39239ca --- /dev/null +++ b/.upgradenotes/MDL-84000-2024121209313638.yml @@ -0,0 +1,9 @@ +issueNumber: MDL-84000 +notes: + core_reportbuilder: + - message: >- + 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` + type: improved diff --git a/admin/roles/tests/reportbuilder/datasource/roles_test.php b/admin/roles/tests/reportbuilder/datasource/roles_test.php index 6d41deec2a8..d327e4c9762 100644 --- a/admin/roles/tests/reportbuilder/datasource/roles_test.php +++ b/admin/roles/tests/reportbuilder/datasource/roles_test.php @@ -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 diff --git a/admin/tests/reportbuilder/datasource/task_logs_test.php b/admin/tests/reportbuilder/datasource/task_logs_test.php index adc63ea6008..7c5631cb8e4 100644 --- a/admin/tests/reportbuilder/datasource/task_logs_test.php +++ b/admin/tests/reportbuilder/datasource/task_logs_test.php @@ -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 diff --git a/badges/tests/reportbuilder/datasource/badges_test.php b/badges/tests/reportbuilder/datasource/badges_test.php index 3644a16522d..bc10495f2cd 100644 --- a/badges/tests/reportbuilder/datasource/badges_test.php +++ b/badges/tests/reportbuilder/datasource/badges_test.php @@ -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. diff --git a/badges/tests/reportbuilder/datasource/users_test.php b/badges/tests/reportbuilder/datasource/users_test.php index b39f882acaa..a04edd517a1 100644 --- a/badges/tests/reportbuilder/datasource/users_test.php +++ b/badges/tests/reportbuilder/datasource/users_test.php @@ -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. diff --git a/blog/tests/reportbuilder/datasource/blogs_test.php b/blog/tests/reportbuilder/datasource/blogs_test.php index cc0a23df797..ff85996e230 100644 --- a/blog/tests/reportbuilder/datasource/blogs_test.php +++ b/blog/tests/reportbuilder/datasource/blogs_test.php @@ -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, diff --git a/cohort/tests/reportbuilder/datasource/cohorts_test.php b/cohort/tests/reportbuilder/datasource/cohorts_test.php index 55a07692920..d3a8e4e8b17 100644 --- a/cohort/tests/reportbuilder/datasource/cohorts_test.php +++ b/cohort/tests/reportbuilder/datasource/cohorts_test.php @@ -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, diff --git a/comment/tests/reportbuilder/datasource/comments_test.php b/comment/tests/reportbuilder/datasource/comments_test.php index b1674dfad1a..d1ea10fda8f 100644 --- a/comment/tests/reportbuilder/datasource/comments_test.php +++ b/comment/tests/reportbuilder/datasource/comments_test.php @@ -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([ - "{$coursecontext->get_context_name()}", + $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', diff --git a/competency/tests/reportbuilder/datasource/competencies_test.php b/competency/tests/reportbuilder/datasource/competencies_test.php index 26738f5853b..ccd3aa9d9f0 100644 --- a/competency/tests/reportbuilder/datasource/competencies_test.php +++ b/competency/tests/reportbuilder/datasource/competencies_test.php @@ -21,13 +21,8 @@ namespace core_competency\reportbuilder\datasource; use core_competency_generator; use core_competency\user_competency; 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 comptencies datasource diff --git a/course/tests/reportbuilder/datasource/categories_test.php b/course/tests/reportbuilder/datasource/categories_test.php index 8b497e9b0a0..2d57dc84699 100644 --- a/course/tests/reportbuilder/datasource/categories_test.php +++ b/course/tests/reportbuilder/datasource/categories_test.php @@ -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 diff --git a/course/tests/reportbuilder/datasource/courses_test.php b/course/tests/reportbuilder/datasource/courses_test.php index a8aa4000136..1a6ea203313 100644 --- a/course/tests/reportbuilder/datasource/courses_test.php +++ b/course/tests/reportbuilder/datasource/courses_test.php @@ -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. diff --git a/course/tests/reportbuilder/datasource/participants_test.php b/course/tests/reportbuilder/datasource/participants_test.php index 9d63ab0093c..e6fe978b821 100644 --- a/course/tests/reportbuilder/datasource/participants_test.php +++ b/course/tests/reportbuilder/datasource/participants_test.php @@ -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_user; +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 * @@ -88,8 +79,8 @@ final class participants_test extends core_reportbuilder_testcase { $courseoneurl = course_get_url($courseone); $coursetwourl = course_get_url($coursetwo); - $useroneurl = core_user::get_profile_url($userone); - $usertwourl = core_user::get_profile_url($usertwo); + $useroneurl = user::get_profile_url($userone); + $usertwourl = user::get_profile_url($usertwo); $this->assertEquals([ ["{$coursetwo->fullname}", diff --git a/files/tests/reportbuilder/datasource/files_test.php b/files/tests/reportbuilder/datasource/files_test.php index d1eac55236b..a2fd7fc7295 100644 --- a/files/tests/reportbuilder/datasource/files_test.php +++ b/files/tests/reportbuilder/datasource/files_test.php @@ -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 diff --git a/group/tests/reportbuilder/datasource/groups_test.php b/group/tests/reportbuilder/datasource/groups_test.php index 262915a3176..cdf20c3251a 100644 --- a/group/tests/reportbuilder/datasource/groups_test.php +++ b/group/tests/reportbuilder/datasource/groups_test.php @@ -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. diff --git a/notes/tests/reportbuilder/datasource/notes_test.php b/notes/tests/reportbuilder/datasource/notes_test.php index 5f84a406278..2a26e0cf958 100644 --- a/notes/tests/reportbuilder/datasource/notes_test.php +++ b/notes/tests/reportbuilder/datasource/notes_test.php @@ -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 diff --git a/reportbuilder/tests/classes/core_reportbuilder_testcase.php b/reportbuilder/tests/classes/core_reportbuilder_testcase.php new file mode 100644 index 00000000000..b3e8446ef7e --- /dev/null +++ b/reportbuilder/tests/classes/core_reportbuilder_testcase.php @@ -0,0 +1,204 @@ +. + +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 + * @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')); + } + } +} diff --git a/reportbuilder/tests/helpers.php b/reportbuilder/tests/helpers.php index 056e4f5acf4..cd03e988960 100644 --- a/reportbuilder/tests/helpers.php +++ b/reportbuilder/tests/helpers.php @@ -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 * @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); diff --git a/reportbuilder/tests/local/aggregation/avg_test.php b/reportbuilder/tests/local/aggregation/avg_test.php index 58b3cfdb0e1..aa59e19ea75 100644 --- a/reportbuilder/tests/local/aggregation/avg_test.php +++ b/reportbuilder/tests/local/aggregation/avg_test.php @@ -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 * diff --git a/reportbuilder/tests/local/aggregation/count_test.php b/reportbuilder/tests/local/aggregation/count_test.php index 872c05e74ac..37a218ddf73 100644 --- a/reportbuilder/tests/local/aggregation/count_test.php +++ b/reportbuilder/tests/local/aggregation/count_test.php @@ -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 * diff --git a/reportbuilder/tests/local/aggregation/countdistinct_test.php b/reportbuilder/tests/local/aggregation/countdistinct_test.php index 93ad2aef7dc..86f4452bb5d 100644 --- a/reportbuilder/tests/local/aggregation/countdistinct_test.php +++ b/reportbuilder/tests/local/aggregation/countdistinct_test.php @@ -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 * diff --git a/reportbuilder/tests/local/aggregation/groupconcat_test.php b/reportbuilder/tests/local/aggregation/groupconcat_test.php index 66e849dd596..09b51b9913f 100644 --- a/reportbuilder/tests/local/aggregation/groupconcat_test.php +++ b/reportbuilder/tests/local/aggregation/groupconcat_test.php @@ -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 * diff --git a/reportbuilder/tests/local/aggregation/groupconcatdistinct_test.php b/reportbuilder/tests/local/aggregation/groupconcatdistinct_test.php index ed9badaa57c..ca802ad931d 100644 --- a/reportbuilder/tests/local/aggregation/groupconcatdistinct_test.php +++ b/reportbuilder/tests/local/aggregation/groupconcatdistinct_test.php @@ -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 * diff --git a/reportbuilder/tests/local/aggregation/max_test.php b/reportbuilder/tests/local/aggregation/max_test.php index 16b5b41ac42..1ce7d838346 100644 --- a/reportbuilder/tests/local/aggregation/max_test.php +++ b/reportbuilder/tests/local/aggregation/max_test.php @@ -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 * diff --git a/reportbuilder/tests/local/aggregation/min_test.php b/reportbuilder/tests/local/aggregation/min_test.php index f2171faa372..9c10203c5dc 100644 --- a/reportbuilder/tests/local/aggregation/min_test.php +++ b/reportbuilder/tests/local/aggregation/min_test.php @@ -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 * diff --git a/reportbuilder/tests/local/aggregation/percent_test.php b/reportbuilder/tests/local/aggregation/percent_test.php index f6a2b221d29..b1cecbe350c 100644 --- a/reportbuilder/tests/local/aggregation/percent_test.php +++ b/reportbuilder/tests/local/aggregation/percent_test.php @@ -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 * diff --git a/reportbuilder/tests/local/aggregation/sum_test.php b/reportbuilder/tests/local/aggregation/sum_test.php index c6428386d57..8a25112a35f 100644 --- a/reportbuilder/tests/local/aggregation/sum_test.php +++ b/reportbuilder/tests/local/aggregation/sum_test.php @@ -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 * diff --git a/reportbuilder/tests/local/helpers/custom_fields_test.php b/reportbuilder/tests/local/helpers/custom_fields_test.php index 538e7f288d1..2e2d7c4c2a5 100644 --- a/reportbuilder/tests/local/helpers/custom_fields_test.php +++ b/reportbuilder/tests/local/helpers/custom_fields_test.php @@ -20,17 +20,12 @@ 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, date, select, text}; use core_reportbuilder\local\report\{column, 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 * diff --git a/reportbuilder/tests/local/helpers/user_profile_fields_test.php b/reportbuilder/tests/local/helpers/user_profile_fields_test.php index f9cff63bda1..dc018ea3641 100644 --- a/reportbuilder/tests/local/helpers/user_profile_fields_test.php +++ b/reportbuilder/tests/local/helpers/user_profile_fields_test.php @@ -19,17 +19,12 @@ 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, date, select, text}; use core_reportbuilder\local\report\{column, 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 * diff --git a/reportbuilder/tests/manager_test.php b/reportbuilder/tests/manager_test.php index c0100b037da..04f3d428a40 100644 --- a/reportbuilder/tests/manager_test.php +++ b/reportbuilder/tests/manager_test.php @@ -18,19 +18,14 @@ 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; use core_reportbuilder\exception\{source_invalid_exception, source_unavailable_exception}; - -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 @@ -173,7 +168,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()); } /** diff --git a/tag/tests/reportbuilder/datasource/tags_test.php b/tag/tests/reportbuilder/datasource/tags_test.php index 0350ed6cf04..074593a06f5 100644 --- a/tag/tests/reportbuilder/datasource/tags_test.php +++ b/tag/tests/reportbuilder/datasource/tags_test.php @@ -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('' . $coursecontext->get_context_name() . '', - $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. diff --git a/user/tests/reportbuilder/datasource/users_test.php b/user/tests/reportbuilder/datasource/users_test.php index 46bcb06f9e8..48501ae6bba 100644 --- a/user/tests/reportbuilder/datasource/users_test.php +++ b/user/tests/reportbuilder/datasource/users_test.php @@ -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 @@ -527,7 +517,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.