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 aa55f38820
commit fab65d14c9
No known key found for this signature in database
GPG Key ID: A81A96D6045F6164
31 changed files with 290 additions and 406 deletions

View File

@ -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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -20,21 +20,12 @@ namespace core_course\reportbuilder\datasource;
use completion_completion; use completion_completion;
use completion_criteria_self; 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_generator;
use core_reportbuilder_testcase; use core_reportbuilder\local\filters\{boolean_select, date, duration, select, text};
use core_user; use core_reportbuilder\tests\core_reportbuilder_testcase;
use core\user;
use grade_item; use grade_item;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/** /**
* Course participants datasource tests * Course participants datasource tests
* *
@ -88,8 +79,8 @@ final class participants_test extends core_reportbuilder_testcase {
$courseoneurl = course_get_url($courseone); $courseoneurl = course_get_url($courseone);
$coursetwourl = course_get_url($coursetwo); $coursetwourl = course_get_url($coursetwo);
$useroneurl = core_user::get_profile_url($userone); $useroneurl = user::get_profile_url($userone);
$usertwourl = core_user::get_profile_url($usertwo); $usertwourl = user::get_profile_url($usertwo);
$this->assertEquals([ $this->assertEquals([
["<a href=\"{$coursetwourl}\">{$coursetwo->fullname}</a>", ["<a href=\"{$coursetwourl}\">{$coursetwo->fullname}</a>",

View File

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

View File

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

View File

@ -20,13 +20,8 @@ namespace core_notes\reportbuilder\datasource;
use core_notes_generator; use core_notes_generator;
use core_reportbuilder_generator; use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{date, select, text}; use core_reportbuilder\local\filters\{date, select, text};
use core_reportbuilder\tests\core_reportbuilder_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/** /**
* Unit tests for notes datasource * 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); 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 * 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> * @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
abstract class core_reportbuilder_testcase extends advanced_testcase {
/** class_alias(\core_reportbuilder\tests\core_reportbuilder_testcase::class, \core_reportbuilder_testcase::class);
* 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

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

View File

@ -18,15 +18,10 @@ declare(strict_types=1);
namespace core_reportbuilder\local\aggregation; namespace core_reportbuilder\local\aggregation;
use core_reportbuilder_testcase;
use core_reportbuilder_generator; use core_reportbuilder_generator;
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users; 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 * Unit tests for count aggregation
* *

View File

@ -18,15 +18,10 @@ declare(strict_types=1);
namespace core_reportbuilder\local\aggregation; namespace core_reportbuilder\local\aggregation;
use core_reportbuilder_testcase;
use core_reportbuilder_generator; use core_reportbuilder_generator;
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users; 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 * Unit tests for count distinct aggregation
* *

View File

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

View File

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

View File

@ -18,15 +18,10 @@ declare(strict_types=1);
namespace core_reportbuilder\local\aggregation; namespace core_reportbuilder\local\aggregation;
use core_reportbuilder_testcase;
use core_reportbuilder_generator; use core_reportbuilder_generator;
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users; 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 * Unit tests for max aggregation
* *

View File

@ -18,15 +18,10 @@ declare(strict_types=1);
namespace core_reportbuilder\local\aggregation; namespace core_reportbuilder\local\aggregation;
use core_reportbuilder_testcase;
use core_reportbuilder_generator; use core_reportbuilder_generator;
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users; 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 * Unit tests for min aggregation
* *

View File

@ -18,15 +18,10 @@ declare(strict_types=1);
namespace core_reportbuilder\local\aggregation; namespace core_reportbuilder\local\aggregation;
use core_reportbuilder_testcase;
use core_reportbuilder_generator; use core_reportbuilder_generator;
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users; 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 * Unit tests for sum aggregation
* *

View File

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

View File

@ -20,17 +20,12 @@ namespace core_reportbuilder\local\helpers;
use core_customfield_generator; use core_customfield_generator;
use core_reportbuilder_generator; use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\entities\course; use core_reportbuilder\local\entities\course;
use core_reportbuilder\local\filters\{boolean_select, date, select, text}; use core_reportbuilder\local\filters\{boolean_select, date, select, text};
use core_reportbuilder\local\report\{column, filter}; use core_reportbuilder\local\report\{column, filter};
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_course\reportbuilder\datasource\{categories, courses}; 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 * Unit tests for custom fields helper
* *

View File

@ -19,17 +19,12 @@ declare(strict_types=1);
namespace core_reportbuilder\local\helpers; namespace core_reportbuilder\local\helpers;
use core_reportbuilder_generator; use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\entities\user; use core_reportbuilder\local\entities\user;
use core_reportbuilder\local\filters\{boolean_select, date, select, text}; use core_reportbuilder\local\filters\{boolean_select, date, select, text};
use core_reportbuilder\local\report\{column, filter}; use core_reportbuilder\local\report\{column, filter};
use core_reportbuilder\tests\core_reportbuilder_testcase;
use core_user\reportbuilder\datasource\users; 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 * Unit tests for user profile fields helper
* *

View File

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

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

View File

@ -18,19 +18,9 @@ declare(strict_types=1);
namespace core_user\reportbuilder\datasource; namespace core_user\reportbuilder\datasource;
use core_reportbuilder_testcase;
use core_reportbuilder_generator; use core_reportbuilder_generator;
use core_reportbuilder\local\filters\boolean_select; use core_reportbuilder\local\filters\{boolean_select, date, select, tags, text, user as user_filter};
use core_reportbuilder\local\filters\date; use core_reportbuilder\tests\core_reportbuilder_testcase;
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");
/** /**
* Unit tests for users datasource * 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'); $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Create report containing single column, and given filter. // 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']); $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']);
// Add filter, set it's values. // Add filter, set it's values.