first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,282 @@
<?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_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");
/**
* Unit tests for course categories datasource
*
* @package core_course
* @covers \core_course\reportbuilder\datasource\categories
* @copyright 2023 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class categories_test extends core_reportbuilder_testcase {
/**
* Test default datasource
*/
public function test_datasource_default(): void {
$this->resetAfterTest();
$category = $this->getDataGenerator()->create_category(['name' => 'Zoo', 'idnumber' => 'Z01']);
$course = $this->getDataGenerator()->create_course(['category' => $category->id]);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => categories::class, 'default' => 1]);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(2, $content);
// Default columns are name, idnumber, coursecount. Sorted by name ascending.
$this->assertEquals([
[get_string('defaultcategoryname'), '', 0],
[$category->get_formatted_name(), $category->idnumber, 1],
], array_map('array_values', $content));
}
/**
* Test datasource columns that aren't added by default
*/
public function test_datasource_non_default_columns(): void {
global $DB;
$this->resetAfterTest();
set_config('allowcategorythemes', true);
$category = $this->getDataGenerator()->create_category([
'name' => 'Zoo',
'idnumber' => 'Z01',
'description' => 'Animals',
'theme' => 'boost',
]);
$course = $this->getDataGenerator()->create_course(['category' => $category->id, 'fullname' => 'Zebra']);
// Add a cohort.
$cohort = $this->getDataGenerator()->create_cohort(['contextid' => $category->get_context()->id, 'name' => 'My cohort']);
// Assign a role.
$managerrole = $DB->get_field('role', 'id', ['shortname' => 'manager']);
$user = $this->getDataGenerator()->create_user();
role_assign($managerrole, $user->id, $category->get_context()->id);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => categories::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course_category:namewithlink',
'sortenabled' => 1]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course_category:path']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course_category:description']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course_category:theme']);
// Add column from each of our entities.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:name']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role:name']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(2, $content);
[$namewithlink, $path, $description, $theme, $coursename, $cohortname, $rolename, $userfullname] =
array_values($content[0]);
$this->assertStringContainsString(get_string('defaultcategoryname'), $namewithlink);
$this->assertEquals(get_string('defaultcategoryname'), $path);
$this->assertEmpty($description);
$this->assertEmpty($theme);
$this->assertEmpty($coursename);
$this->assertEmpty($cohortname);
$this->assertEmpty($rolename);
$this->assertEmpty($userfullname);
[$namewithlink, $path, $description, $theme, $coursename, $cohortname, $rolename, $userfullname] =
array_values($content[1]);
$this->assertStringContainsString($category->get_formatted_name(), $namewithlink);
$this->assertEquals($category->get_nested_name(false), $path);
$this->assertEquals(format_text($category->description, $category->descriptionformat), $description);
$this->assertEquals('Boost', $theme);
$this->assertEquals($course->fullname, $coursename);
$this->assertEquals($cohort->name, $cohortname);
$this->assertEquals('Manager', $rolename);
$this->assertEquals(fullname($user), $userfullname);
}
/**
* Data provider for {@see test_datasource_filters}
*
* @return array[]
*/
public static function datasource_filters_provider(): array {
global $DB;
return [
// Category.
'Filter category' => ['course_category:name', [
'course_category:name_operator' => category::NOT_EQUAL_TO,
'course_category:name_value' => -1,
], true],
'Filter category (no match)' => ['course_category:name', [
'course_category:name_operator' => category::EQUAL_TO,
'course_category:name_value' => -1,
], false],
'Filter category name' => ['course_category:text', [
'course_category:text_operator' => text::IS_EQUAL_TO,
'course_category:text_value' => 'Zoo',
], true],
'Filter category name (no match)' => ['course_category:text', [
'course_category:text_operator' => text::IS_EQUAL_TO,
'course_category:text_value' => 'Plants',
], false],
'Filter category idnumber' => ['course_category:idnumber', [
'course_category:idnumber_operator' => text::IS_EQUAL_TO,
'course_category:idnumber_value' => 'Z01',
], true],
'Filter category idnumber (no match)' => ['course_category:idnumber', [
'course_category:idnumber_operator' => text::IS_EQUAL_TO,
'course_category:idnumber_value' => 'P01',
], false],
'Filter category theme' => ['course_category:theme', [
'course_category:theme_operator' => select::EQUAL_TO,
'course_category:theme_value' => 'boost',
], true],
'Filter category theme (no match)' => ['course_category:theme', [
'course_category:theme_operator' => select::EQUAL_TO,
'course_category:theme_value' => 'classic',
], false],
// Course.
'Filter course fullname' => ['course:fullname', [
'course:fullname_operator' => text::IS_EQUAL_TO,
'course:fullname_value' => 'Zebra',
], true],
'Filter course fullname (no match)' => ['course:fullname', [
'course:fullname_operator' => text::IS_EQUAL_TO,
'course:fullname_value' => 'Python',
], false],
// Cohort.
'Filter cohort name' => ['cohort:name', [
'cohort:name_operator' => text::IS_EQUAL_TO,
'cohort:name_value' => 'My cohort',
], true],
'Filter cohort name (no match)' => ['cohort:name', [
'cohort:name_operator' => text::IS_EQUAL_TO,
'cohort:name_value' => 'Not my cohort',
], false],
// Role.
'Filter role' => ['role:name', [
'role:name_operator' => select::EQUAL_TO,
'role:name_value' => $DB->get_field('role', 'id', ['shortname' => 'manager']),
], true],
'Filter role (no match)' => ['role:name', [
'role:name_operator' => select::EQUAL_TO,
'role:name_value' => -1,
], false],
// User.
'Filter user firstname' => ['user:firstname', [
'user:firstname_operator' => text::IS_EQUAL_TO,
'user:firstname_value' => 'Zoe',
], true],
'Filter user firstname (no match)' => ['user:firstname', [
'user:firstname_operator' => text::IS_EQUAL_TO,
'user:firstname_value' => 'Pedro',
], false],
];
}
/**
* Test datasource filters
*
* @param string $filtername
* @param array $filtervalues
* @param bool $expectmatch
*
* @dataProvider datasource_filters_provider
*/
public function test_datasource_filters(string $filtername, array $filtervalues, bool $expectmatch): void {
global $DB;
$this->resetAfterTest();
set_config('allowcategorythemes', true);
// Get the default category, modify it so we can filter each value.
($category = core_course_category::get_default())->update(['name' => 'Zoo', 'idnumber' => 'Z01', 'theme' => 'boost']);
$course = $this->getDataGenerator()->create_course(['category' => $category->id, 'fullname' => 'Zebra']);
// Add a cohort.
$cohort = $this->getDataGenerator()->create_cohort(['contextid' => $category->get_context()->id, 'name' => 'My cohort']);
// Assign a role.
$managerrole = $DB->get_field('role', 'id', ['shortname' => 'manager']);
$user = $this->getDataGenerator()->create_user(['firstname' => 'Zoe']);
role_assign($managerrole, $user->id, $category->get_context()->id);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Create report containing single idnumber column, and given filter.
$report = $generator->create_report(['name' => 'My report', 'source' => categories::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course_category:idnumber']);
// Add filter, set it's values.
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
$content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
if ($expectmatch) {
$this->assertCount(1, $content);
$this->assertEquals($category->idnumber, reset($content[0]));
} else {
$this->assertEmpty($content);
}
}
/**
* Stress test datasource
*
* In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
*/
public function test_stress_datasource(): void {
if (!PHPUNIT_LONGTEST) {
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
}
$this->resetAfterTest();
$category = $this->getDataGenerator()->create_category(['name' => 'My category']);
$this->datasource_stress_test_columns(categories::class);
$this->datasource_stress_test_columns_aggregation(categories::class);
$this->datasource_stress_test_conditions(categories::class, 'course_category:idnumber');
}
}
@@ -0,0 +1,453 @@
<?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_course\reportbuilder\datasource;
use context_course;
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;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for courses datasources
*
* @package core_course
* @covers \core_course\reportbuilder\datasource\courses
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class courses_test extends core_reportbuilder_testcase {
/**
* Test default datasource
*/
public function test_datasource_default(): void {
$this->resetAfterTest();
// Test subject.
$category = $this->getDataGenerator()->create_category(['name' => 'My cats']);
$courseone = $this->getDataGenerator()->create_course([
'category' => $category->id,
'fullname' => 'Feline fine',
'shortname' => 'C102',
'idnumber' => 'CAT102'
]);
$coursetwo = $this->getDataGenerator()->create_course([
'category' => $category->id,
'fullname' => 'All about cats',
'shortname' => 'C101',
'idnumber' => 'CAT101'
]);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Courses', 'source' => courses::class, 'default' => 1]);
$content = $this->get_custom_report_content($report->get('id'));
// Default columns are category, shortname, fullname, idnumber. Sorted by category, shortname, fullname.
$this->assertEquals([
[$category->name, $coursetwo->shortname, $coursetwo->fullname, $coursetwo->idnumber],
[$category->name, $courseone->shortname, $courseone->fullname, $courseone->idnumber],
], array_map('array_values', $content));
}
/**
* Test datasource columns that aren't added by default
*/
public function test_datasource_non_default_columns(): void {
$this->resetAfterTest();
$category = $this->getDataGenerator()->create_category([
'name' => 'Animals',
'idnumber' => 'CAT101',
'description' => 'Category description',
]);
$course = $this->getDataGenerator()->create_course([
'category' => $category->id,
'fullname' => 'Cats',
'summary' => 'Course description',
'theme' => 'boost',
'tags' => ['Horses'],
]);
// Add a course image.
get_file_storage()->create_file_from_string([
'contextid' => context_course::instance($course->id)->id,
'component' => 'course',
'filearea' => 'overviewfiles',
'itemid' => 0,
'filepath' => '/',
'filename' => 'HelloWorld.jpg',
], 'HelloWorld');
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Courses', 'source' => courses::class, 'default' => 0]);
// Course.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:coursefullnamewithlink']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:courseshortnamewithlink']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:courseidnumberewithlink']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:summary']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:format']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:startdate']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:enddate']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:visible']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:groupmode']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:groupmodeforce']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:lang']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:calendartype']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:theme']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:enablecompletion']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:downloadcontent']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:timecreated']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:timemodified']);
// Tags.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'tag:name']);
// File entity.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:name']);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(1, $content);
[
$coursenamewithlink,
$courseshortnamewithlink,
$courseidnumberwithlink,
$coursesummary,
$courseformat,
$coursestartdate,
$courseenddate,
$coursevisible,
$coursegroupmode,
$coursegroupmodeforce,
$courselang,
$coursecalendar,
$coursetheme,
$coursecompletion,
$coursedownload,
$coursetimecreated,
$coursetimemodified,
$tagname,
$filename,
] = array_values($content[0]);
// Course.
$this->assertStringContainsString($course->fullname, $coursenamewithlink);
$this->assertStringContainsString($course->shortname, $courseshortnamewithlink);
$this->assertStringContainsString($course->idnumber, $courseidnumberwithlink);
$this->assertEquals(format_text($course->summary, $course->summaryformat), $coursesummary);
$this->assertEquals('Custom sections', $courseformat);
$this->assertEquals(userdate($course->startdate), $coursestartdate);
$this->assertEmpty($courseenddate);
$this->assertEquals('Yes', $coursevisible);
$this->assertEquals('No groups', $coursegroupmode);
$this->assertEquals('No', $coursegroupmodeforce);
$this->assertEmpty($courselang);
$this->assertEmpty($coursecalendar);
$this->assertEquals('Boost', $coursetheme);
$this->assertEquals('No', $coursecompletion);
$this->assertEmpty($coursedownload);
$this->assertEquals(userdate($course->timecreated), $coursetimecreated);
$this->assertEquals(userdate($course->timemodified), $coursetimemodified);
// Tags.
$this->assertEquals('Horses', $tagname);
// File.
$this->assertEquals('HelloWorld.jpg', $filename);
}
/**
* Tests courses datasource using multilang filters
*/
public function test_courses_datasource_multilang_filters(): void {
$this->resetAfterTest();
filter_set_global_state('multilang', TEXTFILTER_ON);
filter_set_applies_to_strings('multilang', true);
// Test subject.
$category = $this->getDataGenerator()->create_category([
'name' => '<span class="multilang" lang="en">Cat (en)</span><span class="multilang" lang="es">Cat (es)</span>',
]);
$course = $this->getDataGenerator()->create_course([
'category' => $category->id,
'fullname' => '<span class="multilang" lang="en">Crs (en)</span><span class="multilang" lang="es">Crs (es)</span>',
]);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Create a report containing columns that support multilang content.
$report = $generator->create_report(['name' => 'Courses', 'source' => courses::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course_category:name']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:coursefullnamewithlink']);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(1, $content);
$contentrow = array_values(reset($content));
$this->assertEquals([
'Cat (en)',
'Crs (en)',
'<a href="' . (string) course_get_url($course->id) . '">Crs (en)</a>',
], $contentrow);
}
/**
* Data provider for {@see test_datasource_filters}
*
* @return array[]
*/
public function datasource_filters_provider(): array {
return [
// Category.
'Filter category name' => ['course_category:text', [
'course_category:text_operator' => text::IS_EQUAL_TO,
'course_category:text_value' => 'Animals',
], true],
'Filter category name (no match)' => ['course_category:text', [
'course_category:text_operator' => text::IS_EQUAL_TO,
'course_category:text_value' => 'Fruit',
], false],
// Course.
'Filter course' => ['course:courseselector', [
'course:courseselector_values' => [-1],
], false],
'Filter course fullname' => ['course:fullname', [
'course:fullname_operator' => text::IS_EQUAL_TO,
'course:fullname_value' => 'Equine',
], true],
'Filter course fullname (no match)' => ['course:fullname', [
'course:fullname_operator' => text::IS_EQUAL_TO,
'course:fullname_value' => 'Foxes',
], false],
'Filter course shortname' => ['course:shortname', [
'course:shortname_operator' => text::IS_EQUAL_TO,
'course:shortname_value' => 'EQ101',
], true],
'Filter course shortname (no match)' => ['course:shortname', [
'course:shortname_operator' => text::IS_EQUAL_TO,
'course:shortname_value' => 'FX101',
], false],
'Filter course idnumber' => ['course:idnumber', [
'course:idnumber_operator' => text::IS_EQUAL_TO,
'course:idnumber_value' => 'E-101AB',
], true],
'Filter course idnumber (no match)' => ['course:idnumber', [
'course:idnumber_operator' => text::IS_EQUAL_TO,
'course:idnumber_value' => 'F-101XT',
], false],
'Filter course summary' => ['course:summary', [
'course:summary_operator' => text::CONTAINS,
'course:summary_value' => 'Lorem ipsum',
], true],
'Filter course summary (no match)' => ['course:summary', [
'course:summary_operator' => text::IS_EQUAL_TO,
'course:summary_value' => 'Fiat',
], false],
'Filter course format' => ['course:format', [
'course:format_operator' => select::EQUAL_TO,
'course:format_value' => 'topics',
], true],
'Filter course format (no match)' => ['course:format', [
'course:format_operator' => select::EQUAL_TO,
'course:format_value' => 'weekly',
], false],
'Filter course startdate' => ['course:startdate', [
'course:startdate_operator' => date::DATE_RANGE,
'course:startdate_from' => 1622502000,
], true],
'Filter course startdate (no match)' => ['course:startdate', [
'course:startdate_operator' => date::DATE_RANGE,
'course:startdate_to' => 1622502000,
], false],
'Filter course enddate' => ['course:enddate', [
'course:enddate_operator' => date::DATE_EMPTY,
], true],
'Filter course enddate (no match)' => ['course:enddate', [
'course:enddate_operator' => date::DATE_NOT_EMPTY,
], false],
'Filter course visible' => ['course:visible', [
'course:visible_operator' => boolean_select::CHECKED,
], true],
'Filter course visible (no match)' => ['course:visible', [
'course:visible_operator' => boolean_select::NOT_CHECKED,
], false],
'Filter course groupmode' => ['course:groupmode', [
'course:groupmode_operator' => select::EQUAL_TO,
'course:groupmode_value' => 0, // No groups.
], true],
'Filter course groupmode (no match)' => ['course:groupmode', [
'course:groupmode_operator' => select::EQUAL_TO,
'course:groupmode_value' => 1, // Separate groups.
], false],
'Filter course groupmodeforce' => ['course:groupmodeforce', [
'course:groupmodeforce_operator' => boolean_select::NOT_CHECKED,
], true],
'Filter course groupmodeforce (no match)' => ['course:groupmodeforce', [
'course:groupmodeforce_operator' => boolean_select::CHECKED,
], false],
'Filter course lang' => ['course:lang', [
'course:lang_operator' => select::EQUAL_TO,
'course:lang_value' => 'en',
], true],
'Filter course lang (no match)' => ['course:lang', [
'course:lang_operator' => select::EQUAL_TO,
'course:lang_value' => 'de',
], false],
'Filter course calendartype' => ['course:calendartype', [
'course:calendartype_operator' => select::EQUAL_TO,
'course:calendartype_value' => 'gregorian',
], true],
'Filter course calendartype (no match)' => ['course:calendartype', [
'course:calendartype_operator' => select::EQUAL_TO,
'course:calendartype_value' => 'hijri',
], false],
'Filter course theme' => ['course:theme', [
'course:theme_operator' => select::EQUAL_TO,
'course:theme_value' => 'boost',
], true],
'Filter course theme (no match)' => ['course:theme', [
'course:theme_operator' => select::EQUAL_TO,
'course:theme_value' => 'classic',
], false],
'Filter course enablecompletion' => ['course:enablecompletion', [
'course:enablecompletion_operator' => boolean_select::NOT_CHECKED,
], true],
'Filter course enablecompletion (no match)' => ['course:enablecompletion', [
'course:enablecompletion_operator' => boolean_select::CHECKED,
], false],
'Filter course downloadcontent' => ['course:downloadcontent', [
'course:downloadcontent_operator' => boolean_select::CHECKED,
], true],
'Filter course downloadcontent (no match)' => ['course:downloadcontent', [
'course:downloadcontent_operator' => boolean_select::NOT_CHECKED,
], false],
'Filter course timecreated' => ['course:timecreated', [
'course:timecreated_operator' => date::DATE_RANGE,
'course:timecreated_from' => 1622502000,
], true],
'Filter course timecreated (no match)' => ['course:timecreated', [
'course:timecreated_operator' => date::DATE_RANGE,
'course:timecreated_to' => 1622502000,
], false],
'Filter course timemodified' => ['course:timemodified', [
'course:timemodified_operator' => date::DATE_RANGE,
'course:timemodified_from' => 1622502000,
], true],
'Filter course timemodified (no match)' => ['course:timemodified', [
'course:timemodified_operator' => date::DATE_RANGE,
'course:timemodified_to' => 1622502000,
], false],
// Tags.
'Filter tag name' => ['tag:name', [
'tag:name_operator' => tags::EQUAL_TO,
'tag:name_value' => [-1],
], false],
'Filter tag name not empty' => ['tag:name', [
'tag:name_operator' => tags::NOT_EMPTY,
], true],
// File.
'Filter file name empty' => ['file:name', [
'file:name_operator' => text::IS_EMPTY,
], true],
];
}
/**
* Test datasource filters
*
* @param string $filtername
* @param array $filtervalues
* @param bool $expectmatch
*
* @dataProvider datasource_filters_provider
*/
public function test_datasource_filters(string $filtername, array $filtervalues, bool $expectmatch): void {
$this->resetAfterTest();
$category = $this->getDataGenerator()->create_category(['name' => 'Animals']);
$course = $this->getDataGenerator()->create_course([
'category' => $category->id,
'fullname' => 'Equine',
'shortname' => 'EQ101',
'idnumber' => 'E-101AB',
'lang' => 'en',
'calendartype' => 'gregorian',
'theme' => 'boost',
'downloadcontent' => 1,
'tags' => ['Horses'],
]);
/** @var core_reportbuilder_generator $generator */
$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]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname']);
// Add filter, set it's values.
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
$content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
if ($expectmatch) {
$this->assertCount(1, $content);
$this->assertEquals($course->fullname, reset($content[0]));
} else {
$this->assertEmpty($content);
}
}
/**
* Stress test datasource
*
* In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
*/
public function test_stress_datasource(): void {
if (!PHPUNIT_LONGTEST) {
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
}
$this->resetAfterTest();
$category = $this->getDataGenerator()->create_category();
$course = $this->getDataGenerator()->create_course(['category' => $category->id]);
$this->datasource_stress_test_columns(courses::class);
$this->datasource_stress_test_columns_aggregation(courses::class);
$this->datasource_stress_test_conditions(courses::class, 'course:idnumber');
}
}
@@ -0,0 +1,534 @@
<?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_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 grade_item;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Course participants datasource tests
*
* @package core_course
* @covers \core_course\reportbuilder\datasource\participants
* @copyright 2022 David Matamoros <davidmc@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class participants_test extends core_reportbuilder_testcase {
/**
* Load required test libraries
*/
public static function setUpBeforeClass(): void {
global $CFG;
require_once("{$CFG->libdir}/gradelib.php");
require_once("{$CFG->dirroot}/completion/criteria/completion_criteria_self.php");
}
/**
* Test default datasource
*/
public function test_datasource_default(): void {
global $DB;
$this->resetAfterTest();
// Course one, two manually enrolled users.
$courseone = $this->getDataGenerator()->create_course(['fullname' => 'Zebras']);
$userone = $this->getDataGenerator()->create_and_enrol($courseone, 'student', ['firstname' => 'Zoe']);
$usertwo = $this->getDataGenerator()->create_and_enrol($courseone, 'student', ['firstname' => 'Amy']);
// Course two, two self enrolled users (one inactive).
$coursetwo = $this->getDataGenerator()->create_course(['fullname' => 'Aardvarks']);
$enrol = $DB->get_record('enrol', ['courseid' => $coursetwo->id, 'enrol' => 'self']);
enrol_get_plugin($enrol->enrol)->update_status($enrol, ENROL_INSTANCE_ENABLED);
$this->getDataGenerator()->enrol_user($userone->id, $coursetwo->id, null, 'self');
$this->getDataGenerator()->enrol_user($usertwo->id, $coursetwo->id, null, 'self', 0, 0, ENROL_USER_SUSPENDED);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Participants', 'source' => participants::class, 'default' => 1]);
$content = $this->get_custom_report_content($report->get('id'));
// Default columns are course, user, method. Sorted by each.
$courseoneurl = course_get_url($courseone);
$coursetwourl = course_get_url($coursetwo);
$useroneurl = core_user::get_profile_url($userone);
$usertwourl = core_user::get_profile_url($usertwo);
$this->assertEquals([
["<a href=\"{$coursetwourl}\">{$coursetwo->fullname}</a>",
"<a href=\"{$useroneurl}\">" . fullname($userone) . "</a>", 'Self enrolment (Student)'],
["<a href=\"{$courseoneurl}\">{$courseone->fullname}</a>",
"<a href=\"{$usertwourl}\">" . fullname($usertwo) . "</a>", 'Manual enrolments'],
["<a href=\"{$courseoneurl}\">{$courseone->fullname}</a>",
"<a href=\"{$useroneurl}\">" . fullname($userone) . "</a>", 'Manual enrolments'],
], array_map('array_values', $content));
}
/**
* Test datasource columns that aren't added by default
*/
public function test_datasource_non_default_columns(): void {
global $DB;
$this->resetAfterTest();
$timestart = time() - DAYSECS;
$timeend = $timestart + 3 * DAYSECS;
$timecompleted = $timestart + 2 * DAYSECS;
$timelastaccess = time() + 4 * DAYSECS;
$category = $this->getDataGenerator()->create_category(['name' => 'Music']);
$course = $this->getDataGenerator()->create_course([
'category' => $category->id,
'fullname' => 'All about Lionel at the work place',
'enablecompletion' => true,
'startdate' => $timestart,
'enddate' => $timeend,
]);
$user1 = self::getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student',
'manual', $timestart, $timeend, ENROL_USER_ACTIVE);
// Add them to a group.
$group = self::getDataGenerator()->create_group(['courseid' => $course->id]);
self::getDataGenerator()->create_group_member(['groupid' => $group->id, 'userid' => $user1->id]);
// Create self completion, mark as complete for the user.
$criteriaconfig = (object) ['id' => $course->id, 'criteria_self' => true];
(new completion_criteria_self())->update_config($criteriaconfig);
$ccompletion = new completion_completion(['course' => $course->id, 'userid' => $user1->id]);
$ccompletion->mark_enrolled($timestart);
$ccompletion->mark_complete($timecompleted);
// Update final grade for the user.
$courseitem = grade_item::fetch_course_item($course->id);
$courseitem->update_final_grade($user1->id, 42.5);
// Set some last access value for the user in the course.
$DB->insert_record('user_lastaccess',
['userid' => $user1->id, 'courseid' => $course->id, 'timeaccess' => $timelastaccess]);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Participants', 'source' => participants::class, 'default' => false]);
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'course:fullname']);
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'course_category:name']);
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'user:fullname']);
// Enrol entity (report ordering by enrolment name).
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'enrol:name', 'sortenabled' => 1]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'enrol:plugin']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'enrol:enabled']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'enrol:period']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'enrol:startdate']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'enrol:enddate']);
// Role entity.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role:name']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role:shortname']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role:description']);
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'group:name']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'completion:criteria']);
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'completion:completed']);
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'access:timeaccess']);
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'completion:progresspercent']);
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'completion:timeenrolled']);
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'completion:timestarted']);
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'completion:timecompleted']);
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'completion:reaggregate']);
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'completion:dayscourse']);
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'completion:daysuntilcompletion']);
$generator->create_column(['reportid' => $report->get('id'),
'uniqueidentifier' => 'completion:grade']);
// Add filter to the report.
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'enrol:plugin']);
$content = $this->get_custom_report_content($report->get('id'));
// It should get 3 records (manual enrolment, self and guest).
$this->assertCount(3, $content);
// Filter by Manual enrolment method.
$content = $this->get_custom_report_content($report->get('id'), 30, [
'enrol:plugin_operator' => select::EQUAL_TO,
'enrol:plugin_value' => 'manual',
]);
$this->assertCount(1, $content);
$this->assertEquals([
'All about Lionel at the work place', // Course name.
'Music', // Course category name.
fullname($user1), // User fullname.
'Manual enrolments', // Enrolment method.
'Manual enrolments', // Enrolment plugin.
'Yes', // Enrolment enabled.
'', // Enrolment period.
'', // Enrolment start date.
'', // Enrolment end date.
'Student', // Role name.
'student', // Role shortname.
'Students generally have fewer privileges within a course.', // Role description.
$group->name, // Group name.
"All criteria below are required<ul>\n<li>Self completion: Self completion</li>\n</ul>", // Completion criteria.
'Yes', // Course completed.
userdate($timelastaccess), // Time last access.
'100.0%', // Progress percentage.
userdate($timestart), // Time enrolled.
'', // Time started.
userdate($timecompleted), // Time completed.
'', // Reagreggate.
2, // Days taking course.
2, // Days until completion.
'42.50', // Grade.
], array_values($content[0]));
}
/**
* Test creating participants report, with aggregated last access date (minimum and maximum)
*/
public function test_course_last_access_aggregation(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$userone = $this->getDataGenerator()->create_and_enrol($course);
$useronelastaccess = $this->getDataGenerator()->create_user_course_lastaccess($userone, $course, 1622502000);
$usertwo = $this->getDataGenerator()->create_and_enrol($course);
$usertwolastaccess = $this->getDataGenerator()->create_user_course_lastaccess($usertwo, $course, 1622847600);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Participants', 'source' => participants::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname']);
$column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'access:timeaccess']);
// Course aggregated with "Minimum" last access.
$column->set('aggregation', 'min')->update();
$content = $this->get_custom_report_content($report->get('id'));
$this->assertEquals([
[$course->fullname, userdate($useronelastaccess->timeaccess)],
], array_map('array_values', $content));
// Course aggregated with "Maximum" last access.
$column->set('aggregation', 'max')->update();
$content = $this->get_custom_report_content($report->get('id'));
$this->assertEquals([
[$course->fullname, userdate($usertwolastaccess->timeaccess)],
], array_map('array_values', $content));
}
/**
* Test creating participants report, with aggregated days taking course column
*/
public function test_completion_days_taking_course_aggregation(): void {
$this->resetAfterTest();
$courseone = $this->getDataGenerator()->create_course(['fullname' => 'Course 1', 'startdate' => 1622502000]);
$coursetwo = $this->getDataGenerator()->create_course(['fullname' => 'Course 2']);
// User one completed the course in two days.
$userone = $this->getDataGenerator()->create_and_enrol($courseone);
$completion = new completion_completion(['course' => $courseone->id, 'userid' => $userone->id]);
$completion->mark_complete(1622502000 + (2 * DAYSECS));
// User two completed the course in three days (lazy bum).
$usertwo = $this->getDataGenerator()->create_and_enrol($courseone);
$completion = new completion_completion(['course' => $courseone->id, 'userid' => $usertwo->id]);
$completion->mark_complete(1622502000 + (3 * DAYSECS));
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Participants', 'source' => participants::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname', 'sortenabled' => 1]);
$generator->create_column([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'completion:dayscourse',
'aggregation' => 'avg',
]);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertEquals([
[$courseone->fullname, '2.5'],
[$coursetwo->fullname, ''],
], array_map('array_values', $content));
}
/**
* Data provider for {@see test_datasource_filters}
*
* @return array
*/
public function datasource_filters_provider(): array {
global $DB;
return [
[
'enrolment:status',
[
'enrolment:status_operator' => select::EQUAL_TO,
'enrolment:status_value' => 1,
],
['Luna'],
],
[
'enrolment:timecreated',
[
'enrolment:timecreated_operator' => date::DATE_CURRENT,
'enrolment:timecreated_unit' => date::DATE_UNIT_DAY,
],
['Kira'],
],
[
'enrolment:timestarted',
[
'enrolment:timestarted_operator' => date::DATE_CURRENT,
'enrolment:timecreated_unit' => date::DATE_UNIT_DAY,
],
['Luna'],
],
[
'enrolment:timeended',
[
'enrolment:timeended_operator' => date::DATE_CURRENT,
'enrolment:timeended_unit' => date::DATE_UNIT_DAY,
],
['Luna'],
],
[
'enrol:enabled',
[
'completion:enabled_operator' => boolean_select::CHECKED,
],
['Lionel', 'Kira', 'Luna'],
],
[
'enrol:period',
[
'enrol:period_operator' => duration::DURATION_MAXIMUM,
'enrol:period_unit' => MINSECS,
'enrol:period_value' => 2,
],
['Lionel', 'Kira', 'Luna'],
],
[
'enrol:startdate',
[
'enrol:startdate_operator' => date::DATE_EMPTY,
],
['Lionel', 'Kira', 'Luna'],
],
[
'enrol:enddate',
[
'enrol:enddate_operator' => date::DATE_EMPTY,
],
['Lionel', 'Kira', 'Luna'],
],
[
'enrol:customname',
[
'enrol:customname_operator' => text::IS_EMPTY,
],
['Luna', 'Kira', 'Lionel'],
],
[
'enrol:customname',
[
'enrol:customname_operator' => text::IS_EQUAL_TO,
'enrol:customname_value' => 'All night long'
],
[],
],
[
'role:name',
[
'role:name_operator' => select::EQUAL_TO,
'role:name_value' => $DB->get_field('role', 'id', ['shortname' => 'editingteacher']),
],
['Luna'],
],
[
'completion:completed',
[
'completion:completed_operator' => boolean_select::CHECKED,
],
['Lionel'],
],
[
'completion:timecompleted',
[
'completion:timecompleted_operator' => date::DATE_NOT_EMPTY,
],
['Lionel'],
],
[
'completion:timeenrolled',
[
'completion:timeenrolled_operator' => date::DATE_NOT_EMPTY,
],
['Lionel'],
],
[
'completion:timestarted',
[
'completion:timestarted_operator' => date::DATE_NOT_EMPTY,
],
['Lionel'],
],
[
'completion:reaggregate',
[
'completion:reaggregate_operator' => date::DATE_NOT_EMPTY,
],
['Lionel'],
],
];
}
/**
* Test getting filter SQL
*
* @param string $filter
* @param array $filtervalues
* @param string[] $expected
*
* @dataProvider datasource_filters_provider
*/
public function test_datasource_filters(string $filter, array $filtervalues, array $expected): void {
global $DB;
$this->resetAfterTest();
$timestart = time() - DAYSECS;
$timeend = $timestart + 3 * DAYSECS;
$timecompleted = $timestart + 2 * DAYSECS;
$timelastaccess = time() + 4 * DAYSECS;
$category = $this->getDataGenerator()->create_category(['name' => 'Music']);
$course = $this->getDataGenerator()->create_course([
'category' => $category->id,
'fullname' => 'All about Lionel at the work place',
'enablecompletion' => true,
'startdate' => $timestart,
'enddate' => $timeend,
]);
$user1 = self::getDataGenerator()->create_user(['firstname' => 'Lionel']);
$user2 = self::getDataGenerator()->create_user(['firstname' => 'Kira']);
$user3 = self::getDataGenerator()->create_user(['firstname' => 'Luna']);
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student',
'manual', $timestart - 8 * DAYSECS, $timeend, ENROL_USER_ACTIVE);
$this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student',
'manual', $timestart, $timeend, ENROL_USER_ACTIVE);
$this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher',
'manual', time(), time(), ENROL_USER_SUSPENDED);
// Mark course as completed for the user.
$ccompletion = new completion_completion(array('course' => $course->id, 'userid' => $user1->id));
$ccompletion->mark_enrolled($timestart);
$ccompletion->mark_inprogress($timestart);
$ccompletion->mark_complete($timecompleted);
// Set some last access value for the user in the course.
$DB->insert_record('user_lastaccess',
['userid' => $user1->id, 'courseid' => $course->id, 'timeaccess' => $timelastaccess]);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Participants', 'source' => participants::class, 'default' => false]);
// Add user firstname column to the report.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname']);
$DB->set_field('user_enrolments', 'timecreated', 0, ['userid' => $user1->id]);
$DB->set_field('user_enrolments', 'timecreated', 0, ['userid' => $user3->id]);
// Add filters to the report.
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'enrol:plugin']);
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filter]);
// Apply filters.
$filtermanual = ['enrol:plugin_operator' => select::EQUAL_TO, 'enrol:plugin_value' => 'manual'];
$content = $this->get_custom_report_content($report->get('id'), 30, $filtermanual + $filtervalues);
$this->assertEqualsCanonicalizing($expected, array_column($content, 'c0_firstname'));
}
/**
* Stress test datasource
*
* In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
*/
public function test_stress_datasource(): void {
if (!PHPUNIT_LONGTEST) {
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
}
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$this->getDataGenerator()->create_and_enrol($course);
$this->datasource_stress_test_columns(participants::class);
$this->datasource_stress_test_columns_aggregation(participants::class);
$this->datasource_stress_test_conditions(participants::class, 'course:idnumber');
}
}