Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
<?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_cohort\reportbuilder\datasource;

> use context_coursecat; use core_reportbuilder_generator; > use context_system; use core_reportbuilder_testcase; > use core_customfield_generator;
< use core_reportbuilder\manager; < use core_reportbuilder\local\filters\user; < use core_user;
> use core_reportbuilder\local\filters\{date, select, text};
defined('MOODLE_INTERNAL') || die(); global $CFG; require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php"); /** * Unit tests for cohorts datasource * * @package core_cohort * @covers \core_cohort\reportbuilder\datasource\cohorts * @copyright 2021 Paul Holden <paulh@moodle.com> * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class cohorts_test extends core_reportbuilder_testcase { /**
< * Test cohorts datasource
> * Test default datasource
*/
< public function test_cohorts_datasource(): void {
> public function test_datasource_default(): void {
$this->resetAfterTest(); // Test subject.
> $contextsystem = context_system::instance(); $cohort = $this->getDataGenerator()->create_cohort([ > $cohortone = $this->getDataGenerator()->create_cohort([ 'name' => 'Legends', > 'contextid' => $contextsystem->id, 'idnumber' => 'C101', > 'name' => 'Legends', 'description' => 'Cohort for the legends', > 'idnumber' => 'C101', ]); > 'description' => 'Cohort for the legends', > ]); $user = $this->getDataGenerator()->create_user(['firstname' => 'Lionel', 'lastname' => 'Richards']); > cohort_add_member($cohort->id, $user->id); > $category = $this->getDataGenerator()->create_category(); > $contextcategory = context_coursecat::instance($category->id); /** @var core_reportbuilder_generator $generator */ > $cohorttwo = $this->getDataGenerator()->create_cohort([ $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); > 'contextid' => $contextcategory->id, $report = $generator->create_report(['name' => 'Cohorts', 'source' => cohorts::class]); > 'name' => 'Category cohort', > 'description' => 'This is my category cohort', // Add user fullname column to the report. > ]); $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']); > > /** @var core_reportbuilder_generator $generator */ $content = $this->get_custom_report_content($report->get('id')); > $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); $this->assertCount(1, $content); > $report = $generator->create_report(['name' => 'Cohorts', 'source' => cohorts::class, 'default' => 1]); > $contentrow = array_values(reset($content)); > $content = $this->get_custom_report_content($report->get('id')); $this->assertEquals([ > 'System', // Context. > // Default columns are name, context, idnumber, description. Sorted by name. 'Legends', // Name. > $this->assertEquals([ 'C101', // ID number. > [$cohorttwo->name, $contextcategory->get_context_name(false), $cohorttwo->idnumber, '<div class="text_to_html">Cohort for the legends</div>', // Description. > format_text($cohorttwo->description)], 'Lionel Richards', // User. > [$cohortone->name, $contextsystem->get_context_name(false), $cohortone->idnumber, ], $contentrow); > format_text($cohortone->description)], } > ], array_map('array_values', $content)); > } /** > * Data provider for {@see test_cohorts_datasource_user_select} > /** * > * Test datasource columns that aren't added by default * @return array[] > */ */ > public function test_datasource_non_default_columns(): void { public function cohorts_datasource_user_select_provider(): array { > $this->resetAfterTest(); return [ > $this->setAdminUser(); ['user01', 'Cohort01'], > ['user02', 'Cohort02'], > /** @var core_customfield_generator $generator */ ]; > $generator = $this->getDataGenerator()->get_plugin_generator('core_customfield'); } > > $fieldcategory = $generator->create_category(['component' => 'core_cohort', 'area' => 'cohort']); /** > $field = $generator->create_field(['categoryid' => $fieldcategory->get('id'), 'shortname' => 'hi']); * Test cohorts datasource, while adding the user select condition > * > // Test subject.
* @param string $username
> 'customfield_hi' => 'Hello',
< $report = $generator->create_report(['name' => 'Cohorts', 'source' => cohorts::class]);
> $report = $generator->create_report(['name' => 'Cohorts', 'source' => cohorts::class, 'default' => 0]);
< // Add user fullname column to the report.
> $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:visible']); > $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:timecreated']); > $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:timemodified']); > $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:component']); > $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:theme']); > $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:customfield_hi']); > $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort_member:timeadded']);
< $contentrow = array_values(reset($content)); < $this->assertEquals([ < 'System', // Context. < 'Legends', // Name. < 'C101', // ID number. < '<div class="text_to_html">Cohort for the legends</div>', // Description. < 'Lionel Richards', // User. < ], $contentrow);
> [$visible, $timecreated, $timemodified, $component, $theme, $custom, $timeadded, $fullname] = array_values($content[0]); > > $this->assertEquals('Yes', $visible); > $this->assertNotEmpty($timecreated); > $this->assertNotEmpty($timemodified); > $this->assertEquals('Created manually', $component); > $this->assertEmpty($theme); > $this->assertEquals('Hello', $custom); > $this->assertNotEmpty($timeadded); > $this->assertEquals(fullname($user), $fullname);
< * Data provider for {@see test_cohorts_datasource_user_select}
> * Data provider for {@see test_datasource_filters}
< public function cohorts_datasource_user_select_provider(): array {
> public function datasource_filters_provider(): array {
< ['user01', 'Cohort01'], < ['user02', 'Cohort02'],
> // Cohort. > 'Filter context' => ['cohort:context', [ > 'cohort:context_operator' => select::EQUAL_TO, > 'cohort:context_value' => context_system::instance()->id, > ], true], > 'Filter content (no match)' => ['cohort:context', [ > 'cohort:context_operator' => select::EQUAL_TO, > 'cohort:context_value' => -1, > ], false], > 'Filter name' => ['cohort:name', [ > 'cohort:name_operator' => text::IS_EQUAL_TO, > 'cohort:name_value' => 'Legends', > ], true], > 'Filter name (no match)' => ['cohort:name', [ > 'cohort:name_operator' => text::IS_EQUAL_TO, > 'cohort:name_value' => 'Dancing', > ], false], > 'Filter idnumber' => ['cohort:idnumber', [ > 'cohort:idnumber_operator' => text::IS_EQUAL_TO, > 'cohort:idnumber_value' => 'C101', > ], true], > 'Filter idnumber (no match)' => ['cohort:idnumber', [ > 'cohort:idnumber_operator' => text::IS_EQUAL_TO, > 'cohort:idnumber_value' => 'C102', > ], false], > 'Filter time created' => ['cohort:timecreated', [ > 'cohort:timecreated_operator' => date::DATE_RANGE, > 'cohort:timecreated_from' => 1622502000, > ], true], > 'Filter time created (no match)' => ['cohort:timecreated', [ > 'cohort:timecreated_operator' => date::DATE_RANGE, > 'cohort:timecreated_to' => 1622502000, > ], false], > 'Filter description' => ['cohort:description', [ > 'cohort:description_operator' => text::CONTAINS, > 'cohort:description_value' => 'legends', > ], true], > 'Filter description (no match)' => ['cohort:description', [ > 'cohort:description_operator' => text::IS_EMPTY, > ], false], > > // Cohort member. > 'Filter time added' => ['cohort_member:timeadded', [ > 'cohort_member:timeadded_operator' => date::DATE_RANGE, > 'cohort_member:timeadded_from' => 1622502000, > ], true], > 'Filter time added (no match)' => ['cohort_member:timeadded', [ > 'cohort_member:timeadded_operator' => date::DATE_RANGE, > 'cohort_member:timeadded_to' => 1622502000, > ], false], > > // User. > 'Filter user' => ['user:username', [ > 'user:username_operator' => text::IS_EQUAL_TO, > 'user:username_value' => 'lionel', > ], true], > 'Filter user (no match)' => ['user:username', [ > 'user:username_operator' => text::IS_EQUAL_TO, > 'user:username_value' => 'rick', > ], false],
< * Test cohorts datasource, while adding the user select condition
> * Test datasource filters
< * @param string $username < * @param string $expectedcohort
> * @param string $filtername > * @param array $filtervalues > * @param bool $expectmatch
< * @dataProvider cohorts_datasource_user_select_provider
> * @dataProvider datasource_filters_provider
< public function test_cohorts_datasource_user_select(string $username, string $expectedcohort): void {
> public function test_datasource_filters(string $filtername, array $filtervalues, bool $expectmatch): void {
< // First cohort/user member. < $cohort01 = $this->getDataGenerator()->create_cohort(['name' => 'Cohort01']); < $user01 = $this->getDataGenerator()->create_user(['username' => 'user01']); < cohort_add_member($cohort01->id, $user01->id); < < // Second cohort/user member. < $cohort02 = $this->getDataGenerator()->create_cohort(['name' => 'Cohort02']); < $user02 = $this->getDataGenerator()->create_user(['username' => 'user02']); < cohort_add_member($cohort02->id, $user02->id);
> // Test subject. > $cohort = $this->getDataGenerator()->create_cohort([ > 'name' => 'Legends', > 'idnumber' => 'C101', > 'description' => 'Cohort for the legends', > ]); > > $user = $this->getDataGenerator()->create_user(['username' => 'lionel']); > cohort_add_member($cohort->id, $user->id);
< $report = $generator->create_report(['name' => 'User cohorts', 'source' => cohorts::class, 'default' => 0]);
< // Add cohort name and user fullname columns.
> // Create report containing single column, and given filter. > $report = $generator->create_report(['name' => 'Cohorts', 'source' => cohorts::class, 'default' => 0]);
< $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']);
< // Add condition to limit report data to current user. < $condition = $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:userselect']); < manager::get_report_from_persistent($report)->set_condition_values([ < $condition->get('uniqueidentifier') . '_operator' => user::USER_CURRENT, < ]);
> // 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);
< // Switch user, request report. < $currentuser = core_user::get_user_by_username($username); < $this->setUser($currentuser); < < $content = $this->get_custom_report_content($report->get('id'));
> if ($expectmatch) {
< < $contentrow = array_values(reset($content)); < $this->assertEquals([$expectedcohort, $username], $contentrow);
> $this->assertEquals('Legends', reset($content[0])); > } else { > $this->assertEmpty($content); > }
* * 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(); $cohort = $this->getDataGenerator()->create_cohort(); $user = $this->getDataGenerator()->create_user(); cohort_add_member($cohort->id, $user->id); $this->datasource_stress_test_columns(cohorts::class); $this->datasource_stress_test_columns_aggregation(cohorts::class); $this->datasource_stress_test_conditions(cohorts::class, 'cohort:name'); } }