This commit is contained in:
Ilya Tregubov 2022-08-30 14:59:50 +04:00
commit 96dbcc0840
21 changed files with 1437 additions and 165 deletions

View file

@ -18,15 +18,17 @@ declare(strict_types=1);
namespace core_user\reportbuilder\datasource;
use lang_string;
use core_reportbuilder\datasource;
use core_reportbuilder\local\entities\user;
use core_reportbuilder\local\filters\boolean_select;
use core_reportbuilder\local\helpers\database;
use core_tag\reportbuilder\local\entities\tag;
/**
* Users datasource
*
* @package core_reportbuilder
* @package core_user
* @copyright 2021 David Matamoros <davidmc@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -59,9 +61,20 @@ class users extends datasource {
$this->add_entity($userentity);
// Join the tag entity.
$tagentity = (new tag())
->set_table_alias('tag', $userentity->get_table_alias('tag'))
->set_entity_title(new lang_string('interests'));
$this->add_entity($tagentity
->add_joins($userentity->get_tag_joins()));
// Add all columns/filters/conditions from entities to be available in custom reports.
$userentityname = $userentity->get_entity_name();
$this->add_all_from_entity($userentityname);
$this->add_all_from_entity($userentity->get_entity_name());
// Add specific tag entity elements.
$this->add_columns_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink']);
$this->add_filter($tagentity->get_filter('name'));
$this->add_condition($tagentity->get_condition('name'));
}
/**

View file

@ -0,0 +1,158 @@
<?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_user\reportbuilder\datasource;
use core_collator;
use core_reportbuilder_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\local\filters\tags;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for users datasources
*
* @package core_user
* @covers \core_user\reportbuilder\datasource\users
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class users_test extends core_reportbuilder_testcase {
/**
* Test default datasource
*/
public function test_datasource_default(): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user(['email' => 'test@example.com']);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 1]);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(2, $content);
// Consistent order by email, just in case.
core_collator::asort_array_of_arrays_by_key($content, 'c2_email');
$content = array_values($content);
// Default columns are fullname, username, email.
[$adminrow, $userrow] = array_map('array_values', $content);
$this->assertEquals(['Admin User', 'admin', 'admin@example.com'], $adminrow);
$this->assertEquals([fullname($user), $user->username, $user->email], $userrow);
}
/**
* Test datasource columns that aren't added by default
*/
public function test_datasource_non_default_columns(): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user(['fisrtname' => 'Zoe', 'interests' => ['Horses']]);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// User.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname']);
// Tags.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'tag:name']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'tag:namewithlink']);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(2, $content);
// Consistent order by firstname, just in case.
core_collator::asort_array_of_arrays_by_key($content, 'c0_firstname');
$content = array_values($content);
[$adminrow, $userrow] = array_map('array_values', $content);
$this->assertEquals('Admin', $adminrow[0]);
$this->assertEmpty($adminrow[1]);
$this->assertEmpty($adminrow[2]);
$this->assertEquals($user->firstname, $userrow[0]);
$this->assertEquals('Horses', $userrow[1]);
$this->assertStringContainsString('Horses', $userrow[2]);
}
/**
* Data provider for {@see test_datasource_filters}
*
* @return array[]
*/
public function datasource_filters_provider(): array {
return [
// 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],
];
}
/**
* 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();
$user = $this->getDataGenerator()->create_user(['interests' => ['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' => users::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']);
// 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($user->username, reset($content[0]));
} else {
$this->assertEmpty($content);
}
}
}