Skip to content

Commit

Permalink
MDL-74720 reportbuilder: Limiting the number of custom reports
Browse files Browse the repository at this point in the history
  • Loading branch information
lostrogit committed Jun 6, 2022
1 parent 5500d14 commit 04f0152
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 1 deletion.
10 changes: 10 additions & 0 deletions admin/settings/reportbuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,13 @@ static function(accesscallback $accesscallback): bool {
empty($CFG->enablecustomreports)
)
);

$settings = new admin_settingpage('reportbuildersettings', get_string('customreportssettings', 'core_reportbuilder'),
'moodle/site:config', empty($CFG->enablecustomreports));

$settings->add(new admin_setting_configtext(
'customreportslimit',
new lang_string('customreportslimit', 'core_reportbuilder'),
new lang_string('customreportslimit_desc', 'core_reportbuilder'), 0, PARAM_INT));

$ADMIN->add('reportbuilder', $settings);
3 changes: 3 additions & 0 deletions lang/en/reportbuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
$string['courseselect'] = 'Select course';
$string['customfieldcolumn'] = '{$a}';
$string['customreports'] = 'Custom reports';
$string['customreportslimit'] = 'Custom reports limit';
$string['customreportslimit_desc'] = 'The number of custom reports may be limited for performance reasons. If set to zero, then there is no limit.';
$string['customreportssettings'] = 'Custom reports settings';
$string['deleteaudience'] = 'Delete audience \'{$a}\'';
$string['deleteaudienceconfirm'] = 'Are you sure you want to delete the audience \'{$a}\'?';
$string['deletecolumn'] = 'Delete column \'{$a}\'';
Expand Down
12 changes: 12 additions & 0 deletions reportbuilder/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,16 @@ public static function get_report_datasources(): array {

return $sources;
}

/**
* Configured site limit for number of custom reports threshold has been reached
*
* @return bool
*/
public static function report_limit_reached(): bool {
global $CFG;

return (!empty($CFG->customreportslimit) &&
(int) $CFG->customreportslimit <= report::count_records(['type' => base::TYPE_CUSTOM_REPORT]));
}
}
2 changes: 1 addition & 1 deletion reportbuilder/classes/permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static function can_create_report(?int $userid = null): bool {
return !empty($CFG->enablecustomreports) && has_any_capability([
'moodle/reportbuilder:edit',
'moodle/reportbuilder:editall',
], context_system::instance(), $userid);
], context_system::instance(), $userid) && !manager::report_limit_reached();
}

/**
Expand Down
15 changes: 15 additions & 0 deletions reportbuilder/tests/behat/customreports.feature
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,18 @@ Feature: Manage custom reports
And I navigate to "Reports > Report builder > Custom reports" in site administration
And I press "Edit report content" action in the "My fish & chips report" report row
Then I should see "My fish & chips report" in the "#region-main .navbar" "css_element"

Scenario: Site report limit is observed when creating new reports
Given the following config values are set as admin:
| customreportslimit | 0 |
And the following "core_reportbuilder > Reports" exist:
| name | source |
| Report users | core_user\reportbuilder\datasource\users |
| Report courses | core_course\reportbuilder\datasource\courses |
When I log in as "admin"
And I navigate to "Reports > Report builder > Custom reports" in site administration
Then "New report" "button" should exist
And the following config values are set as admin:
| customreportslimit | 2 |
And I reload the page
And "New report" "button" should not exist
40 changes: 40 additions & 0 deletions reportbuilder/tests/manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

use advanced_testcase;
use context_system;
use core_reportbuilder_generator;
use core_user\reportbuilder\datasource\users;
use stdClass;
use core_reportbuilder\local\models\report;
use core_reportbuilder\local\report\base;
Expand Down Expand Up @@ -129,4 +131,42 @@ public function test_create_report_persistent(): void {
$this->assertEquals(system_report_available::class, $report->get('source'));
$this->assertInstanceOf(context_system::class, $report->get_context());
}

/**
* Data provider for {@see test_report_limit_reached}
*
* @return array
*/
public function report_limit_reached_provider(): array {
return [
[0, 1, false],
[1, 1, true],
[2, 1, false],
[1, 2, true],
];
}

/**
* Test test_report_limit_reached method to check site custom reports limit
*
* @param int $customreportslimit
* @param int $existingreports
* @param bool $expected
* @dataProvider report_limit_reached_provider
*/
public function test_report_limit_reached(int $customreportslimit, int $existingreports, bool $expected): void {
global $CFG;

$this->resetAfterTest();

/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
for ($i = 1; $i <= $existingreports; $i++) {
$generator->create_report(['name' => 'Limited report '.$i, 'source' => users::class]);
}

// Set current custom report limit, and check whether the limit has been reached.
$CFG->customreportslimit = $customreportslimit;
$this->assertEquals($expected, manager::report_limit_reached());
}
}
38 changes: 38 additions & 0 deletions reportbuilder/tests/permission_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,42 @@ public function test_require_can_create_report_disabled(): void {
$this->expectExceptionMessage('You cannot create a new report');
permission::require_can_create_report();
}

/**
* Data provider for {@see test_can_create_report_limit_reached}
*
* @return array
*/
public function can_create_report_limit_reached_provider(): array {
return [
[0, 1, true],
[1, 1, false],
[2, 1, true],
[1, 2, false],
];
}

/**
* Test whether user can create report when limit report are reache
* @param int $customreportslimit
* @param int $existingreports
* @param bool $expected
* @dataProvider can_create_report_limit_reached_provider
*/
public function test_can_create_report_limit_reached(int $customreportslimit, int $existingreports, bool $expected): void {
global $CFG;

$this->resetAfterTest();
$this->setAdminUser();

/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
for ($i = 1; $i <= $existingreports; $i++) {
$generator->create_report(['name' => 'Report limited '.$i, 'source' => users::class]);
}

// Set current custom report limit, and check whether user can create reports.
$CFG->customreportslimit = $customreportslimit;
$this->assertEquals($expected, permission::can_create_report());
}
}

0 comments on commit 04f0152

Please sign in to comment.