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
+105
View File
@@ -0,0 +1,105 @@
<?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\external\audiences;
use context_system;
use core_reportbuilder\local\models\audience;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for deleting report audiences
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\audiences\delete
* @copyright 2021 David Matamoros <davidmc@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report([
'name' => 'My report',
'source' => users::class,
'default' => false,
]);
$audience1 = $generator->create_audience([
'reportid' => $report->get('id'),
'configdata' => [],
]);
$audience2 = $generator->create_audience([
'reportid' => $report->get('id'),
'configdata' => [],
]);
$audiences = audience::get_records(['reportid' => $report->get('id')]);
$this->assertCount(2, $audiences);
// Delete the first audience.
$result = delete::execute($report->get('id'), $audience1->get_persistent()->get('id'));
$result = external_api::clean_returnvalue(delete::execute_returns(), $result);
$this->assertTrue($result);
$audiences = audience::get_records(['reportid' => $report->get('id')]);
$this->assertCount(1, $audiences);
$audience = reset($audiences);
$this->assertEquals($audience2->get_persistent()->get('id'), $audience->get('id'));
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$audience1 = $generator->create_audience([
'reportid' => $report->get('id'),
'configdata' => [],
]);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
delete::execute($report->get('id'), $audience1->get_persistent()->get('id'));
}
}
+98
View File
@@ -0,0 +1,98 @@
<?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\external\columns;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\local\models\column;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for adding report columns
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\columns\add
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class add_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report([
'name' => 'My report',
'source' => users::class,
'default' => false,
]);
// Add column.
$result = add::execute($report->get('id'), 'user:fullname');
$result = external_api::clean_returnvalue(add::execute_returns(), $result);
$this->assertTrue($result['hassortablecolumns']);
$this->assertCount(1, $result['sortablecolumns']);
$sortablecolumn = reset($result['sortablecolumns']);
$this->assertEquals('Full name', $sortablecolumn['title']);
$this->assertEquals(SORT_ASC, $sortablecolumn['sortdirection']);
$this->assertEquals(0, $sortablecolumn['sortenabled']);
$this->assertEquals(1, $sortablecolumn['sortorder']);
$this->assertEquals('t/uplong', $sortablecolumn['sorticon']['key']);
$this->assertEquals('moodle', $sortablecolumn['sorticon']['component']);
$str = get_string('columnsortdirectiondesc', 'core_reportbuilder', 'Full name');
$this->assertEquals($str, $sortablecolumn['sorticon']['title']);
// Assert report columns.
$columns = column::get_records(['reportid' => $report->get('id')]);
$this->assertCount(1, $columns);
$this->assertEquals('user:fullname', reset($columns)->get('uniqueidentifier'));
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
add::execute($report->get('id'), 'user:fullname');
}
}
+104
View File
@@ -0,0 +1,104 @@
<?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\external\columns;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\local\models\column;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for deleting report columns
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\columns\delete
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report([
'name' => 'My report',
'source' => users::class,
'default' => false,
]);
// Add two columns.
$columnfullname = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
// Delete the first column.
$result = delete::execute($report->get('id'), $columnfullname->get('id'));
$result = external_api::clean_returnvalue(delete::execute_returns(), $result);
$this->assertTrue($result['hassortablecolumns']);
$this->assertCount(1, $result['sortablecolumns']);
$sortablecolumn = reset($result['sortablecolumns']);
$this->assertEquals('Email address', $sortablecolumn['title']);
$this->assertEquals(SORT_ASC, $sortablecolumn['sortdirection']);
$this->assertEquals(0, $sortablecolumn['sortenabled']);
$this->assertEquals(2, $sortablecolumn['sortorder']);
$this->assertEquals('t/uplong', $sortablecolumn['sorticon']['key']);
$this->assertEquals('moodle', $sortablecolumn['sorticon']['component']);
$str = get_string('columnsortdirectiondesc', 'core_reportbuilder', 'Email address');
$this->assertEquals($str, $sortablecolumn['sorticon']['title']);
// Assert report columns.
$columns = column::get_records(['reportid' => $report->get('id')]);
$this->assertCount(1, $columns);
$this->assertEquals('user:email', reset($columns)->get('uniqueidentifier'));
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
delete::execute($report->get('id'), $column->get('id'));
}
}
+104
View File
@@ -0,0 +1,104 @@
<?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\external\columns;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\local\models\column;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for re-ordering report columns
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\columns\reorder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class reorder_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report([
'name' => 'My report',
'source' => users::class,
'default' => false,
]);
// Add four columns.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
$columncity = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
// Move the city column to second position.
$result = reorder::execute($report->get('id'), $columncity->get('id'), 2);
$result = external_api::clean_returnvalue(reorder::execute_returns(), $result);
$this->assertTrue($result);
// Assert report columns order.
$columns = column::get_records(['reportid' => $report->get('id')], 'columnorder');
$columnidentifiers = array_map(static function(column $column): string {
return $column->get('uniqueidentifier');
}, $columns);
$this->assertEquals([
'user:fullname',
'user:city',
'user:email',
'user:country',
], $columnidentifiers);
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
reorder::execute($report->get('id'), $column->get('id'), 1);
}
}
+87
View File
@@ -0,0 +1,87 @@
<?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\external\columns\sort;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for retrieving report column sorting
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\columns\sort\get
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
// Add some columns (note 'user:picture' is not a sortable column).
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:picture']);
$result = get::execute($report->get('id'));
$result = external_api::clean_returnvalue(get::execute_returns(), $result);
$this->assertTrue($result['hassortablecolumns']);
$sortablecolumntitles = array_column($result['sortablecolumns'], 'title');
$this->assertEquals([
'Full name',
'Email address',
], $sortablecolumntitles);
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
get::execute($report->get('id'));
}
}
@@ -0,0 +1,114 @@
<?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\external\columns\sort;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\local\models\column;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for re-ordering report column sorting
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\columns\sort\reorder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class reorder_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
// Add four columns.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
$columncity = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
// Move the city column sort order to second position.
$result = reorder::execute($report->get('id'), $columncity->get('id'), 2);
$result = external_api::clean_returnvalue(reorder::execute_returns(), $result);
$this->assertTrue($result['hassortablecolumns']);
$this->assertCount(4, $result['sortablecolumns']);
$columnid = $columncity->get('id');
$sortablecolumn = array_filter($result['sortablecolumns'], function(array $column) use($columnid) {
return $column['id'] == $columnid;
});
$sortablecolumn = reset($sortablecolumn);
$this->assertEquals($columnid, $sortablecolumn['id']);
$this->assertEquals('City/town', $sortablecolumn['title']);
$this->assertEquals(SORT_ASC, $sortablecolumn['sortdirection']);
$this->assertEquals(0, $sortablecolumn['sortenabled']);
$this->assertEquals(2, $sortablecolumn['sortorder']);
$this->assertEquals('t/uplong', $sortablecolumn['sorticon']['key']);
$this->assertEquals('moodle', $sortablecolumn['sorticon']['component']);
$str = get_string('columnsortdirectiondesc', 'core_reportbuilder', 'City/town');
$this->assertEquals($str, $sortablecolumn['sorticon']['title']);
// Assert report column sort order.
$columns = column::get_records(['reportid' => $report->get('id')], 'sortorder');
$columnidentifiers = array_map(static function(column $column): string {
return $column->get('uniqueidentifier');
}, $columns);
$this->assertEquals([
'user:fullname',
'user:city',
'user:email',
'user:country',
], $columnidentifiers);
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
reorder::execute($report->get('id'), $column->get('id'), 2);
}
}
@@ -0,0 +1,100 @@
<?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\external\columns\sort;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\local\models\column;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for toggling report column sorting
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\columns\sort\toggle
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class toggle_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
// Toggle sort descending.
$result = toggle::execute($report->get('id'), $column->get('id'), true, SORT_DESC);
$result = external_api::clean_returnvalue(toggle::execute_returns(), $result);
$this->assertTrue($result['hassortablecolumns']);
$this->assertCount(4, $result['sortablecolumns']);
$columnid = $column->get('id');
$sortablecolumn = array_filter($result['sortablecolumns'], function(array $column) use($columnid) {
return $column['id'] == $columnid;
});
$sortablecolumn = reset($sortablecolumn);
$this->assertEquals($columnid, $sortablecolumn['id']);
$this->assertEquals('Email address', $sortablecolumn['title']);
$this->assertEquals(SORT_DESC, $sortablecolumn['sortdirection']);
$this->assertEquals(1, $sortablecolumn['sortenabled']);
$this->assertEquals(4, $sortablecolumn['sortorder']);
$this->assertEquals('t/downlong', $sortablecolumn['sorticon']['key']);
$this->assertEquals('moodle', $sortablecolumn['sorticon']['component']);
$str = get_string('columnsortdirectionasc', 'core_reportbuilder', 'Email address');
$this->assertEquals($str, $sortablecolumn['sorticon']['title']);
// Confirm column was updated.
$columnupdated = new column($column->get('id'));
$this->assertTrue($columnupdated->get('sortenabled'));
$this->assertEquals(SORT_DESC, $columnupdated->get('sortdirection'));
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
toggle::execute($report->get('id'), $column->get('id'), true, SORT_DESC);
}
}
+88
View File
@@ -0,0 +1,88 @@
<?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\external\conditions;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\local\models\filter;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for adding report conditions
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\conditions\add
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class add_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report([
'name' => 'My report',
'source' => users::class,
'default' => false,
]);
// Add condition.
$result = add::execute($report->get('id'), 'user:fullname');
$result = external_api::clean_returnvalue(add::execute_returns(), $result);
$this->assertTrue($result['hasactiveconditions']);
$this->assertNotEmpty($result['activeconditionsform']);
// Assert report conditions.
$conditions = filter::get_condition_records($report->get('id'));
$this->assertCount(1, $conditions);
$this->assertEquals('user:fullname', reset($conditions)->get('uniqueidentifier'));
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
add::execute($report->get('id'), 'user:fullname');
}
}
+97
View File
@@ -0,0 +1,97 @@
<?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\external\conditions;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\local\models\filter;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for deleting report conditions
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\conditions\delete
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report([
'name' => 'My report',
'source' => users::class,
'default' => false,
]);
// Add two conditions.
$conditionfullname = $generator->create_condition([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'user:fullname',
]);
$generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
// Delete the first condition.
$result = delete::execute($report->get('id'), $conditionfullname->get('id'));
$result = external_api::clean_returnvalue(delete::execute_returns(), $result);
$this->assertTrue($result['hasactiveconditions']);
$this->assertNotEmpty($result['activeconditionsform']);
// Assert report conditions.
$conditions = filter::get_condition_records($report->get('id'));
$this->assertCount(1, $conditions);
$this->assertEquals('user:email', reset($conditions)->get('uniqueidentifier'));
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
$condition = $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
delete::execute($report->get('id'), $condition->get('id'));
}
}
+105
View File
@@ -0,0 +1,105 @@
<?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\external\conditions;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\local\models\filter;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for re-ordering report conditions
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\conditions\reorder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class reorder_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report([
'name' => 'My report',
'source' => users::class,
'default' => false,
]);
// Add four conditions.
$generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
$generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
$conditioncity = $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
// Move the city condition to second position.
$result = reorder::execute($report->get('id'), $conditioncity->get('id'), 2);
$result = external_api::clean_returnvalue(reorder::execute_returns(), $result);
$this->assertTrue($result['hasactiveconditions']);
$this->assertNotEmpty($result['activeconditionsform']);
// Assert report conditions order.
$conditions = filter::get_condition_records($report->get('id'), 'filterorder');
$conditionidentifiers = array_map(static function(filter $condition): string {
return $condition->get('uniqueidentifier');
}, $conditions);
$this->assertEquals([
'user:fullname',
'user:city',
'user:email',
'user:country',
], $conditionidentifiers);
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
$condition = $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
reorder::execute($report->get('id'), $condition->get('id'), 1);
}
}
+85
View File
@@ -0,0 +1,85 @@
<?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\external\conditions;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\manager;
use core_reportbuilder\report_access_exception;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for resetting report conditions
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\conditions\reset
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class reset_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
$generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
$instance = manager::get_report_from_persistent($report);
$instance->set_condition_values(['foo' => 'bar']);
$result = reset::execute($report->get('id'));
$result = external_api::clean_returnvalue(reset::execute_returns(), $result);
$this->assertTrue($result['hasactiveconditions']);
$this->assertNotEmpty($result['activeconditionsform']);
// Re-create the report instance, we should get an empty array back.
$secondinstance = manager::get_report_from_persistent($report->read());
$this->assertEquals([], $secondinstance->get_condition_values());
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
reset::execute($report->get('id'));
}
}
@@ -0,0 +1,121 @@
<?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\external;
use advanced_testcase;
use context_system;
/**
* Unit tests for custom report audience cards exporter
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\custom_report_audience_cards_exporter
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class custom_report_audience_cards_exporter_test extends advanced_testcase {
/**
* Test exported data/structure
*/
public function test_export(): void {
global $PAGE;
$this->resetAfterTest();
$this->setAdminUser();
$exporter = new custom_report_audience_cards_exporter(null);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
$this->assertNotEmpty($export->menucards);
// Test only the site audiences, so tests are unaffected by audiences within components.
$menucardsite = array_filter($export->menucards, static function(array $menucard): bool {
return $menucard['name'] === get_string('site');
});
$this->assertCount(1, $menucardsite);
$menucardsite = reset($menucardsite);
$this->assertNotEmpty($menucardsite['key']);
$this->assertGreaterThanOrEqual(4, count($menucardsite['items']));
// Test the structure of the first menu card item.
$menucarditem = reset($menucardsite['items']);
$this->assertEquals([
'name' => 'All users',
'identifier' => \core_reportbuilder\reportbuilder\audience\allusers::class,
'title' => 'Add audience \'All users\'',
'action' => 'add-audience',
'disabled' => false,
], $menucarditem);
}
/**
* Test exported data when user cannot add some audience types
*/
public function test_export_audience_user_can_add(): void {
global $DB, $PAGE;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
// This capability controls access to the all/manual users audiences.
$userrole = $DB->get_field('role', 'id', ['shortname' => 'user']);
assign_capability('moodle/user:viewalldetails', CAP_ALLOW, $userrole, context_system::instance());
$exporter = new custom_report_audience_cards_exporter(null);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
$this->assertCount(1, $export->menucards);
$this->assertEquals('Site', $export->menucards[0]['name']);
$this->assertEquals([
'All users',
'Manually added users',
], array_column($export->menucards[0]['items'], 'name'));
}
/**
* Test exported data when user can add an audience type, but it isn't available
*/
public function test_export_audience_is_available(): void {
global $DB, $PAGE;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$userrole = $DB->get_field('role', 'id', ['shortname' => 'user']);
assign_capability('moodle/cohort:view', CAP_ALLOW, $userrole, context_system::instance());
$exporter = new custom_report_audience_cards_exporter(null);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
$this->assertCount(1, $export->menucards);
$this->assertEquals('Site', $export->menucards[0]['name']);
// Cohort audience should be present, but disabled.
$this->assertCount(1, $export->menucards[0]['items']);
$this->assertEquals('Member of cohort', $export->menucards[0]['items'][0]['name']);
$this->assertTrue($export->menucards[0]['items'][0]['disabled']);
}
}
@@ -0,0 +1,58 @@
<?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\external;
use advanced_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\manager;
use core_user\reportbuilder\datasource\users;
/**
* Unit tests for custom report card view exporter
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\custom_report_card_view_exporter
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class custom_report_card_view_exporter_test extends advanced_testcase {
/**
* Test exported data structure
*/
public function test_export(): void {
global $PAGE;
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$reportinstance = manager::get_report_from_persistent($report);
$exporter = new custom_report_card_view_exporter(null, ['report' => $reportinstance]);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
// The exporter just returns two large pieces of HTML, assert they are non empty.
$this->assertNotEmpty($export->form);
$this->assertNotEmpty($export->helpicon);
}
}
@@ -0,0 +1,113 @@
<?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\external;
use advanced_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\manager;
use core_course\reportbuilder\datasource\courses;
/**
* Unit tests for custom report column cards exporter
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\custom_report_column_cards_exporter
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class custom_report_column_cards_exporter_test extends advanced_testcase {
/**
* Test exported data structure
*/
public function test_export(): void {
global $PAGE;
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => courses::class]);
$reportinstance = manager::get_report_from_persistent($report);
$exporter = new custom_report_column_cards_exporter(null, ['report' => $reportinstance]);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
// The root of the menu cards property should contain each entity.
$this->assertCount(4, $export->menucards);
[$menucardcategory, $menucardcourse, $menucardtag, $menucardfile] = $export->menucards;
// Course category entity menu card.
$this->assertEquals('Course category', $menucardcategory['name']);
$this->assertEquals('course_category', $menucardcategory['key']);
$this->assertNotEmpty($menucardcategory['items']);
// Test the structure of the first menu card item.
$menucarditem = reset($menucardcategory['items']);
$this->assertEquals([
'name' => 'Category name',
'identifier' => 'course_category:name',
'title' => 'Add column \'Category name\'',
'action' => 'report-add-column',
], $menucarditem);
// Course entity menu card.
$this->assertEquals('Course', $menucardcourse['name']);
$this->assertEquals('course', $menucardcourse['key']);
$this->assertNotEmpty($menucardcourse['items']);
// Test the structure of the first menu card item.
$menucarditem = reset($menucardcourse['items']);
$this->assertEquals([
'name' => 'Course full name with link',
'identifier' => 'course:coursefullnamewithlink',
'title' => 'Add column \'Course full name with link\'',
'action' => 'report-add-column',
], $menucarditem);
// Tag entity menu card.
$this->assertEquals('Tag', $menucardtag['name']);
$this->assertEquals('tag', $menucardtag['key']);
$this->assertNotEmpty($menucardtag['items']);
// Test the structure of the first menu card item.
$menucarditem = reset($menucardtag['items']);
$this->assertEquals([
'name' => 'Tag name',
'identifier' => 'tag:name',
'title' => 'Add column \'Tag name\'',
'action' => 'report-add-column',
], $menucarditem);
// File entity menu card.
$this->assertEquals('Course image', $menucardfile['name']);
$this->assertEquals('file', $menucardfile['key']);
$this->assertNotEmpty($menucardfile['items']);
// Test the structure of the first menu card item.
$menucarditem = reset($menucardfile['items']);
$this->assertEquals([
'name' => 'Filename',
'identifier' => 'file:name',
'title' => 'Add column \'Filename\'',
'action' => 'report-add-column',
], $menucarditem);
}
}
@@ -0,0 +1,129 @@
<?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\external;
use advanced_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\manager;
use core_reportbuilder\local\helpers\report;
use core_user\reportbuilder\datasource\users;
/**
* Unit tests for custom report column sorting exporter
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\custom_report_columns_sorting_exporter
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class custom_report_columns_sorting_exporter_test extends advanced_testcase {
/**
* Test exported data structure
*/
public function test_export(): void {
global $PAGE;
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
// Add a couple of columns.
$columnfullname = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
$columnemail = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
// Enable sorting on the email column, move it to first place.
report::toggle_report_column_sorting($report->get('id'), $columnemail->get('id'), true, SORT_DESC);
report::reorder_report_column_sorting($report->get('id'), $columnemail->get('id'), 1);
$reportinstance = manager::get_report_from_persistent($report);
$exporter = new custom_report_columns_sorting_exporter(null, ['report' => $reportinstance]);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
$this->assertTrue($export->hassortablecolumns);
$this->assertCount(2, $export->sortablecolumns);
[$sortcolumnemail, $sortcolumnfullname] = $export->sortablecolumns;
// Email column.
$this->assertEquals($columnemail->get('id'), $sortcolumnemail['id']);
$this->assertEquals('Email address', $sortcolumnemail['heading']);
$this->assertTrue($sortcolumnemail['sortenabled']);
$this->assertEquals(1, $sortcolumnemail['sortorder']);
$this->assertEquals(SORT_DESC, $sortcolumnemail['sortdirection']);
$this->assertEquals('Disable initial sorting for column \'Email address\'', $sortcolumnemail['sortenabledtitle']);
$this->assertEquals('Sort column \'Email address\' ascending', $sortcolumnemail['sorticon']['title']);
// Fullname column.
$this->assertEquals($columnfullname->get('id'), $sortcolumnfullname['id']);
$this->assertEquals('Full name', $sortcolumnfullname['heading']);
$this->assertFalse($sortcolumnfullname['sortenabled']);
$this->assertEquals(2, $sortcolumnfullname['sortorder']);
$this->assertEquals(SORT_ASC, $sortcolumnfullname['sortdirection']);
$this->assertEquals('Enable initial sorting for column \'Full name\'', $sortcolumnfullname['sortenabledtitle']);
$this->assertEquals('Sort column \'Full name\' descending', $sortcolumnfullname['sorticon']['title']);
$this->assertNotEmpty($export->helpicon);
}
/**
* Test exported data structure for report with columns, but they aren't sortable
*/
public function test_export_no_sortable_columns(): void {
global $PAGE;
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:picture']);
$reportinstance = manager::get_report_from_persistent($report);
$exporter = new custom_report_columns_sorting_exporter(null, ['report' => $reportinstance]);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
$this->assertFalse($export->hassortablecolumns);
$this->assertEmpty($export->sortablecolumns);
}
/**
* Test exported data structure for report with no columns
*/
public function test_export_no_columns(): void {
global $PAGE;
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
$reportinstance = manager::get_report_from_persistent($report);
$exporter = new custom_report_columns_sorting_exporter(null, ['report' => $reportinstance]);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
$this->assertFalse($export->hassortablecolumns);
$this->assertEmpty($export->sortablecolumns);
}
}
@@ -0,0 +1,121 @@
<?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\external;
use advanced_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\manager;
use core_course\reportbuilder\datasource\courses;
/**
* Unit tests for custom report conditions exporter
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\custom_report_conditions_exporter
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class custom_report_conditions_exporter_test extends advanced_testcase {
/**
* Test exported data structure
*/
public function test_export(): void {
global $PAGE;
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => courses::class, 'default' => false]);
$generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:shortname']);
$reportinstance = manager::get_report_from_persistent($report);
$exporter = new custom_report_conditions_exporter(null, ['report' => $reportinstance]);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
$this->assertTrue($export->hasavailableconditions);
// The root of the available conditions property should contain each entity.
$this->assertCount(4, $export->availableconditions);
[$conditionscategory, $conditionscourse, $conditionstag, $conditionsfile] = $export->availableconditions;
// Course category conditions, assert structure of first item.
$this->assertEquals('Course category', $conditionscategory['optiongroup']['text']);
$this->assertGreaterThanOrEqual(1, count($conditionscategory['optiongroup']['values']));
$this->assertEquals([
'value' => 'course_category:name',
'visiblename' => 'Select category',
], $conditionscategory['optiongroup']['values'][0]);
// Course conditions, assert structure of first item.
$this->assertEquals('Course', $conditionscourse['optiongroup']['text']);
$this->assertGreaterThanOrEqual(1, count($conditionscourse['optiongroup']['values']));
$this->assertEquals([
'value' => 'course:fullname',
'visiblename' => 'Course full name',
], $conditionscourse['optiongroup']['values'][0]);
// Make sure the active condition we added, isn't present in available conditions.
$this->assertNotContains('course:shortname', array_column($conditionscourse['optiongroup']['values'], 'value'));
// Tag conditions, assert structure of first item.
$this->assertEquals('Tag', $conditionstag['optiongroup']['text']);
$this->assertGreaterThanOrEqual(1, count($conditionstag['optiongroup']['values']));
$this->assertEquals([
'value' => 'tag:name',
'visiblename' => 'Tag name',
], $conditionstag['optiongroup']['values'][0]);
// File conditions, assert structure of first item.
$this->assertEquals('Course image', $conditionsfile['optiongroup']['text']);
$this->assertGreaterThanOrEqual(1, count($conditionsfile['optiongroup']['values']));
$this->assertEquals([
'value' => 'file:name',
'visiblename' => 'Filename',
], $conditionsfile['optiongroup']['values'][0]);
// The active conditions are contained inside form HTML, just assert there's something present.
$this->assertTrue($export->hasactiveconditions);
$this->assertNotEmpty($export->activeconditionsform);
$this->assertNotEmpty($export->helpicon);
}
/**
* Test exported data structure for report with no conditions
*/
public function test_export_no_conditions(): void {
global $PAGE;
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => courses::class, 'default' => false]);
$reportinstance = manager::get_report_from_persistent($report);
$exporter = new custom_report_conditions_exporter(null, ['report' => $reportinstance]);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
$this->assertFalse($export->hasactiveconditions);
$this->assertEmpty($export->activeconditionsform);
}
}
@@ -0,0 +1,76 @@
<?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\external;
use advanced_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\manager;
use core_user\reportbuilder\datasource\users;
/**
* Unit tests for custom report data exporter
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\custom_report_data_exporter
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class custom_report_data_exporter_test extends advanced_testcase {
/**
* Test exported data structure
*/
public function test_export(): void {
global $PAGE;
$this->resetAfterTest();
$this->getDataGenerator()->create_user(['firstname' => 'Zoe', 'lastname' => 'Zebra', 'email' => 'u1@example.com']);
$this->getDataGenerator()->create_user(['firstname' => 'Charlie', 'lastname' => 'Carrot', 'email' => 'u2@example.com']);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
$generator->create_column([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'user:fullname',
'heading' => 'Lovely user',
'sortenabled' => 1,
]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$reportinstance = manager::get_report_from_persistent($report);
$exporter = new custom_report_data_exporter(null, ['report' => $reportinstance, 'page' => 0, 'perpage' => 2]);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
// There are three users (admin plus the two previouly created), but we're paging the first two only.
$this->assertEquals(['Lovely user', 'Email address'], $export->headers);
$this->assertEquals([
[
'columns' => ['Admin User', 'admin@example.com'],
],
[
'columns' => ['Charlie Carrot', 'u2@example.com'],
],
], $export->rows);
$this->assertEquals(3, $export->totalrowcount);
}
}
@@ -0,0 +1,64 @@
<?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\external;
use advanced_testcase;
use core_reportbuilder_generator;
use core_user\reportbuilder\datasource\users;
/**
* Unit tests for custom report details exporter
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\custom_report_details_exporter
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class custom_report_details_exporter_test extends advanced_testcase {
/**
* Test exported data structure
*/
public function test_export(): void {
global $PAGE;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$exporter = new custom_report_details_exporter($report);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
// The exporter outputs the persistent details, plus two other properties.
$this->assertEquals($report->get('name'), $export->name);
$this->assertEquals($report->get('source'), $export->source);
// Source name should be the name of the source.
$this->assertEquals(users::get_name(), $export->sourcename);
// We use the user exporter for the modifier of the report.
$this->assertObjectHasProperty('modifiedby', $export);
$this->assertEquals(fullname($user), $export->modifiedby->fullname);
}
}
@@ -0,0 +1,160 @@
<?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\external;
use advanced_testcase;
use core_reportbuilder\manager;
use core_reportbuilder_generator;
use moodle_url;
use core_reportbuilder\local\helpers\user_filter_manager;
use core_reportbuilder\local\filters\text;
use core_user\reportbuilder\datasource\users;
/**
* Unit tests for custom report exporter
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\custom_report_exporter
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class custom_report_exporter_test extends advanced_testcase {
/**
* Test exported data structure when editing a report
*/
public function test_export_editing(): void {
global $PAGE;
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
manager::get_report_from_persistent($report)->add_attributes(['data-foo' => 'bar', 'data-another' => '1']);
$PAGE->set_url(new moodle_url('/'));
$exporter = new custom_report_exporter($report, [], true);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
$this->assertNotEmpty($export->table);
$this->assertEquals(0, $export->filtersapplied);
$this->assertFalse($export->filterspresent);
$this->assertEmpty($export->filtersform);
$this->assertTrue($export->editmode);
$this->assertEmpty($export->attributes);
// The following are all generated by additional exporters.
$this->assertNotEmpty($export->sidebarmenucards);
$this->assertNotEmpty($export->conditions);
$this->assertNotEmpty($export->filters);
$this->assertNotEmpty($export->sorting);
$this->assertNotEmpty($export->cardview);
}
/**
* Test exported data structure when viewing a report
*/
public function test_export_viewing(): void {
global $PAGE;
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
manager::get_report_from_persistent($report)->add_attributes(['data-foo' => 'bar', 'data-another' => '1']);
$PAGE->set_url(new moodle_url('/'));
$exporter = new custom_report_exporter($report, ['pagesize' => 10], false);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
$this->assertNotEmpty($export->table);
$this->assertEquals(0, $export->filtersapplied);
$this->assertFalse($export->filterspresent);
$this->assertEmpty($export->filtersform);
$this->assertFalse($export->editmode);
$this->assertEquals([
['name' => 'data-foo', 'value' => 'bar'],
['name' => 'data-another', 'value' => '1']
], $export->attributes);
// The following are all generated by additional exporters, and should not be present when not editing.
$this->assertObjectNotHasProperty('sidebarmenucards', $export);
$this->assertObjectNotHasProperty('conditions', $export);
$this->assertObjectNotHasProperty('filters', $export);
$this->assertObjectNotHasProperty('sorting', $export);
$this->assertObjectNotHasProperty('cardview', $export);
}
/**
* Test exported data structure when filters are present
*/
public function test_export_filters_present(): void {
global $PAGE;
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$PAGE->set_url(new moodle_url('/'));
$exporter = new custom_report_exporter($report, ['pagesize' => 10], false);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
$this->assertTrue($export->filterspresent);
$this->assertNotEmpty($export->filtersform);
$this->assertEquals(0, $export->filtersapplied);
}
/**
* Test exported data structure when filters are applied
*/
public function test_export_filters_applied(): void {
global $PAGE;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
// Apply filter.
user_filter_manager::set($report->get('id'), ['user:email_operator' => text::IS_NOT_EMPTY]);
$PAGE->set_url(new moodle_url('/'));
$exporter = new custom_report_exporter($report, ['pagesize' => 10], false);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
$this->assertTrue($export->filterspresent);
$this->assertNotEmpty($export->filtersform);
$this->assertEquals(1, $export->filtersapplied);
}
}
@@ -0,0 +1,149 @@
<?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\external;
use advanced_testcase;
use core_reportbuilder_generator;
use core_reportbuilder\manager;
use core_reportbuilder\local\helpers\report;
use core_course\reportbuilder\datasource\courses;
/**
* Unit tests for custom report filters exporter
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\custom_report_filters_exporter
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class custom_report_filters_exporter_test extends advanced_testcase {
/**
* Test exported data structure
*/
public function test_export(): void {
global $PAGE;
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => courses::class, 'default' => false]);
// Add a couple of filters.
$filtercategoryname = $generator->create_filter([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'course_category:name',
]);
$filtercourseidnumber = $generator->create_filter([
'reportid' => $report->get('id'),
'uniqueidentifier' => 'course:idnumber',
]);
// Move course ID number filter to first place.
report::reorder_report_filter($report->get('id'), $filtercourseidnumber->get('id'), 1);
$reportinstance = manager::get_report_from_persistent($report);
$exporter = new custom_report_filters_exporter(null, ['report' => $reportinstance]);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
$this->assertTrue($export->hasavailablefilters);
// The root of the available filters property should contain each entity.
$this->assertCount(4, $export->availablefilters);
[$filterscategory, $filterscourse, $filterstag, $filtersfile] = $export->availablefilters;
// Course category filters, assert structure of first item.
$this->assertEquals('Course category', $filterscategory['optiongroup']['text']);
$this->assertGreaterThanOrEqual(1, count($filterscategory['optiongroup']['values']));
$this->assertEquals([
'value' => 'course_category:text',
'visiblename' => 'Category name',
], $filterscategory['optiongroup']['values'][0]);
// Course filters, assert structure of first item.
$this->assertEquals('Course', $filterscourse['optiongroup']['text']);
$this->assertGreaterThanOrEqual(1, count($filterscourse['optiongroup']['values']));
$this->assertEquals([
'value' => 'course:fullname',
'visiblename' => 'Course full name',
], $filterscourse['optiongroup']['values'][0]);
// Make sure the active filters we added, aren't present in available filters.
$filterscourseavailable = array_column($filterscourse['optiongroup']['values'], 'value');
$this->assertNotContains('course_category:name', $filterscourseavailable);
$this->assertNotContains('course:idnumber', $filterscourseavailable);
// Tag filters, assert structure of first item.
$this->assertEquals('Tag', $filterstag['optiongroup']['text']);
$this->assertGreaterThanOrEqual(1, count($filterstag['optiongroup']['values']));
$this->assertEquals([
'value' => 'tag:name',
'visiblename' => 'Tag name',
], $filterstag['optiongroup']['values'][0]);
// File filters, assert structure of first item.
$this->assertEquals('Course image', $filtersfile['optiongroup']['text']);
$this->assertGreaterThanOrEqual(1, count($filtersfile['optiongroup']['values']));
$this->assertEquals([
'value' => 'file:name',
'visiblename' => 'Filename',
], $filtersfile['optiongroup']['values'][0]);
$this->assertTrue($export->hasactivefilters);
$this->assertCount(2, $export->activefilters);
[$activefiltercourseidnumber, $activefiltercategoryname] = $export->activefilters;
// Course ID number filter.
$this->assertEquals($filtercourseidnumber->get('id'), $activefiltercourseidnumber['id']);
$this->assertEquals('Course', $activefiltercourseidnumber['entityname']);
$this->assertEquals('Course ID number', $activefiltercourseidnumber['heading']);
$this->assertEquals(1, $activefiltercourseidnumber['sortorder']);
// Course category filter.
$this->assertEquals($filtercategoryname->get('id'), $activefiltercategoryname['id']);
$this->assertEquals('Course category', $activefiltercategoryname['entityname']);
$this->assertEquals('Select category', $activefiltercategoryname['heading']);
$this->assertEquals(2, $activefiltercategoryname['sortorder']);
$this->assertNotEmpty($export->helpicon);
}
/**
* Test exported data structure for report with no filters
*/
public function test_export_no_filters(): void {
global $PAGE;
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => courses::class, 'default' => false]);
$reportinstance = manager::get_report_from_persistent($report);
$exporter = new custom_report_filters_exporter(null, ['report' => $reportinstance]);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
$this->assertFalse($export->hasactivefilters);
$this->assertEmpty($export->activefilters);
}
}
+93
View File
@@ -0,0 +1,93 @@
<?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\external\filters;
use core_reportbuilder\manager;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\local\models\filter;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for adding report filters
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\filters\add
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class add_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report([
'name' => 'My report',
'source' => users::class,
'default' => false,
]);
// Add filter.
$result = add::execute($report->get('id'), 'user:fullname');
$result = external_api::clean_returnvalue(add::execute_returns(), $result);
$this->assertTrue($result['hasavailablefilters']);
$this->assertEquals('User', $result['availablefilters'][0]['optiongroup']['text']);
$this->assertNotEmpty($result['availablefilters'][0]['optiongroup']['values']);
$this->assertTrue($result['hasactivefilters']);
$this->assertCount(1, $result['activefilters']);
$this->assertEquals('Full name', $result['activefilters'][0]['heading']);
// Assert report filters.
$filters = filter::get_filter_records($report->get('id'));
$this->assertCount(1, $filters);
$this->assertEquals('user:fullname', reset($filters)->get('uniqueidentifier'));
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
add::execute($report->get('id'), 'user:fullname');
}
}
+98
View File
@@ -0,0 +1,98 @@
<?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\external\filters;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\local\models\filter;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for deleting report filters
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\filters\delete
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report([
'name' => 'My report',
'source' => users::class,
'default' => false,
]);
// Add two filters.
$filterfullname = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
// Delete the first filter.
$result = delete::execute($report->get('id'), $filterfullname->get('id'));
$result = external_api::clean_returnvalue(delete::execute_returns(), $result);
$this->assertTrue($result['hasavailablefilters']);
$this->assertEquals('User', $result['availablefilters'][0]['optiongroup']['text']);
$this->assertNotEmpty($result['availablefilters'][0]['optiongroup']['values']);
$this->assertTrue($result['hasactivefilters']);
$this->assertCount(1, $result['activefilters']);
$this->assertEquals('Email address', $result['activefilters'][0]['heading']);
// Assert report filters.
$filters = filter::get_filter_records($report->get('id'));
$this->assertCount(1, $filters);
$this->assertEquals('user:email', reset($filters)->get('uniqueidentifier'));
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
$filter = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
delete::execute($report->get('id'), $filter->get('id'));
}
}
+108
View File
@@ -0,0 +1,108 @@
<?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\external\filters;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\local\models\filter;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for re-ordering report filters
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\filters\reorder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class reorder_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report([
'name' => 'My report',
'source' => users::class,
'default' => false,
]);
// Add four filters.
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
$filtercity = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
// Move the city filter to second position.
$result = reorder::execute($report->get('id'), $filtercity->get('id'), 2);
$result = external_api::clean_returnvalue(reorder::execute_returns(), $result);
$this->assertTrue($result['hasavailablefilters']);
$this->assertEquals('User', $result['availablefilters'][0]['optiongroup']['text']);
$this->assertNotEmpty($result['availablefilters'][0]['optiongroup']['values']);
$this->assertTrue($result['hasactivefilters']);
$this->assertCount(4, $result['activefilters']);
// Assert report filters order.
$filters = filter::get_filter_records($report->get('id'), 'filterorder');
$filteridentifiers = array_map(static function(filter $filter): string {
return $filter->get('uniqueidentifier');
}, $filters);
$this->assertEquals([
'user:fullname',
'user:city',
'user:email',
'user:country',
], $filteridentifiers);
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
$filter = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
reorder::execute($report->get('id'), $filter->get('id'), 1);
}
}
+86
View File
@@ -0,0 +1,86 @@
<?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\external\filters;
use core_reportbuilder_generator;
use core_reportbuilder\manager;
use core_reportbuilder\report_access_exception;
use core_external\external_api;
use externallib_advanced_testcase;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests external filters reset class
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\filters\reset
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class reset_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$instance = manager::get_report_from_persistent($report);
$instance->set_filter_values([
'entity:filter_operator' => 'something',
'entity:filter_value' => 42,
]);
$result = reset::execute($report->get('id'));
$result = external_api::clean_returnvalue(reset::execute_returns(), $result);
$this->assertTrue($result);
// We should get an empty array back.
$this->assertEquals([], $instance->get_filter_values());
}
/**
* Test execute method for a user without permission to view the report
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot view this report');
reset::execute($report->get('id'));
}
}
+86
View File
@@ -0,0 +1,86 @@
<?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\external\filters;
use core_reportbuilder_generator;
use core_reportbuilder\manager;
use core_reportbuilder\report_access_exception;
use core_external\external_api;
use externallib_advanced_testcase;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests external filters set class
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\filters\set
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class set_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$values = [
'entity:filter_operator' => 'something',
'entity:filter_value' => 42,
];
$result = set::execute($report->get('id'), '', json_encode($values));
$result = external_api::clean_returnvalue(set::execute_returns(), $result);
$this->assertTrue($result);
// We should get our original filter values back.
$instance = manager::get_report_from_persistent($report);
$this->assertEquals($values, $instance->get_filter_values());
}
/**
* Test execute method for a user without permission to view the report
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot view this report');
set::execute($report->get('id'), '', json_encode(['foo' => 1]));
}
}
+87
View File
@@ -0,0 +1,87 @@
<?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\external\reports;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\local\models\report;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for deleting reports
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\reports\delete
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report([
'name' => 'My report',
'source' => users::class,
'default' => false,
]);
// Sanity test.
$this->assertCount(1, report::get_records());
$result = delete::execute($report->get('id'));
$result = external_api::clean_returnvalue(delete::execute_returns(), $result);
$this->assertTrue($result);
// Retrieve reports.
$this->assertEmpty(report::get_records());
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
delete::execute($report->get('id'));
}
}
+168
View File
@@ -0,0 +1,168 @@
<?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\external\reports;
use context_system;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for getting reports
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\reports\get
* @copyright 2021 David Matamoros <davidmc@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_test extends externallib_advanced_testcase {
/**
* Text execute method for edit mode
*/
public function test_execute_editmode(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report([
'name' => 'My report',
'source' => users::class,
'default' => false,
]);
// Add two filters.
$filterfullname = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
$filteremail = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$result = get::execute($report->get('id'), true);
$result = external_api::clean_returnvalue(get::execute_returns(), $result);
$this->assertEquals($result['id'], $report->get('id'));
$this->assertEquals($result['name'], 'My report');
$this->assertEquals($result['source'], users::class);
$this->assertNotEmpty($result['table']);
$this->assertNotEmpty($result['javascript']);
$this->assertFalse($result['filterspresent']);
$this->assertEmpty($result['filtersform']);
$this->assertTrue($result['editmode']);
// Confirm editor-specific data is returned.
$this->assertNotEmpty($result['sidebarmenucards']);
$this->assertNotEmpty($result['conditions']);
$this->assertNotEmpty($result['filters']);
$this->assertTrue($result['filters']['hasavailablefilters']);
$this->assertNotEmpty($result['filters']['availablefilters']);
$this->assertTrue($result['filters']['hasactivefilters']);
$this->assertEquals($filterfullname->get('id'), $result['filters']['activefilters'][0]['id']);
$this->assertEquals($filteremail->get('id'), $result['filters']['activefilters'][1]['id']);
$this->assertNotEmpty($result['sorting']);
$this->assertNotEmpty($result['cardview']);
}
/**
* Text execute method for preview mode
*/
public function test_execute_previewmode(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report([
'name' => 'My report',
'source' => users::class,
'default' => false,
]);
// Add two filters.
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
$result = get::execute($report->get('id'), false);
$result = external_api::clean_returnvalue(get::execute_returns(), $result);
$this->assertEquals($result['id'], $report->get('id'));
$this->assertEquals($result['name'], 'My report');
$this->assertEquals($result['source'], users::class);
$this->assertNotEmpty($result['table']);
$this->assertNotEmpty($result['javascript']);
$this->assertTrue($result['filterspresent']);
$this->assertNotEmpty($result['filtersform']);
$this->assertFalse($result['editmode']);
// Confirm editor-specific data is not returned.
$this->assertArrayNotHasKey('sidebarmenucards', $result);
$this->assertArrayNotHasKey('conditions', $result);
$this->assertArrayNotHasKey('filters', $result);
$this->assertArrayNotHasKey('sorting', $result);
$this->assertArrayNotHasKey('cardview', $result);
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
get::execute($report->get('id'), true);
}
/**
* Test execute method for a user without permission to view reports
*/
public function test_execute_view_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$user = $this->getDataGenerator()->create_user();
$contextid = context_system::instance()->id;
$roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
assign_capability('moodle/reportbuilder:view', CAP_PROHIBIT, $roleid, $contextid);
role_assign($roleid, $user->id, $contextid);
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot view this report');
get::execute($report->get('id'), false);
}
}
+92
View File
@@ -0,0 +1,92 @@
<?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\external\reports;
use context_system;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\local\models\report;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for listing reports
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\reports\listing
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class listing_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Create three reports.
$reportone = $generator->create_report(['name' => 'Report one', 'source' => users::class]);
$reporttwo = $generator->create_report(['name' => 'Report two', 'source' => users::class]);
$reportthree = $generator->create_report(['name' => 'Report three', 'source' => users::class]);
// Create second user, with audience of both report one and two.
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$generator->create_audience(['reportid' => $reportone->get('id'), 'configdata' => []]);
$generator->create_audience(['reportid' => $reporttwo->get('id'), 'configdata' => []]);
// Switch to second user, get their report listing.
$result = listing::execute();
$result = external_api::clean_returnvalue(listing::execute_returns(), $result);
$this->assertEquals(['Report one', 'Report two'], array_column($result['reports'], 'name'));
$this->assertEmpty($result['warnings']);
}
/**
* Test execute method for a user without permission to view reports
*/
public function test_execute_access_exception(): void {
global $DB;
$this->resetAfterTest();
$userrole = $DB->get_field('role', 'id', ['shortname' => 'user'], MUST_EXIST);
assign_capability('moodle/reportbuilder:view', CAP_PROHIBIT, $userrole, context_system::instance(), true);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot view this report');
listing::execute();
}
}
+115
View File
@@ -0,0 +1,115 @@
<?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\external\reports;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for retrieving custom report content
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\reports\retrieve
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class retrieve_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
$this->getDataGenerator()->create_user(['firstname' => 'Zoe', 'lastname' => 'Zebra', 'email' => 'u1@example.com']);
$this->getDataGenerator()->create_user(['firstname' => 'Charlie', 'lastname' => 'Carrot', 'email' => 'u2@example.com']);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname', 'sortenabled' => 1]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
// There are three users (admin plus the two previouly created), but we're paging the first two only.
$result = retrieve::execute($report->get('id'), 0, 2);
$result = external_api::clean_returnvalue(retrieve::execute_returns(), $result);
// All data is generated by exporters, just assert relevant sample of each.
$this->assertArrayHasKey('details', $result);
$this->assertEquals('My report', $result['details']['name']);
$this->assertArrayHasKey('data', $result);
$this->assertEquals(['Full name', 'Email address'], $result['data']['headers']);
$this->assertEquals([
[
'columns' => ['Admin User', 'admin@example.com'],
],
[
'columns' => ['Charlie Carrot', 'u2@example.com'],
],
], $result['data']['rows']);
$this->assertEquals(3, $result['data']['totalrowcount']);
$this->assertEmpty($result['warnings']);
// Retrieve the second set of pages results.
$result = retrieve::execute($report->get('id'), 1, 2);
$result = external_api::clean_returnvalue(retrieve::execute_returns(), $result);
$this->assertArrayHasKey('details', $result);
$this->assertEquals('My report', $result['details']['name']);
$this->assertArrayHasKey('data', $result);
$this->assertEquals(['Full name', 'Email address'], $result['data']['headers']);
$this->assertEquals([
[
'columns' => ['Zoe Zebra', 'u1@example.com'],
],
], $result['data']['rows']);
$this->assertEquals(3, $result['data']['totalrowcount']);
$this->assertEmpty($result['warnings']);
}
/**
* Test execute method for a user without permission to view report
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot view this report');
retrieve::execute($report->get('id'));
}
}
+98
View File
@@ -0,0 +1,98 @@
<?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\external\reports;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\event\report_viewed;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\local\models\report;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for viewing reports
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\reports\view
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class view_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
// Catch the events.
$sink = $this->redirectEvents();
$result = view::execute($report->get('id'));
$result = external_api::clean_returnvalue(view::execute_returns(), $result);
$events = $sink->get_events();
$this->assertCount(1, $events);
$sink->close();
$this->assertValidKeys($result, ['status', 'warnings']);
$this->assertTrue($result['status']);
$this->assertEmpty($result['warnings']);
// Validate the event.
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf(report_viewed::class, $event);
$this->assertEquals(report::TABLE, $event->objecttable);
$this->assertEquals($report->get('id'), $event->objectid);
$this->assertEquals($report->get('name'), $event->other['name']);
$this->assertEquals($report->get('source'), $event->other['source']);
$this->assertEquals($report->get_context()->id, $event->contextid);
}
/**
* Test execute method for a user without permission to view reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot view this report');
view::execute($report->get('id'));
}
}
+83
View File
@@ -0,0 +1,83 @@
<?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\external\schedules;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for deleting report schedules
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\schedules\delete
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
$scheduleid = $schedule->get('id');
$result = delete::execute($report->get('id'), $scheduleid);
$result = external_api::clean_returnvalue(delete::execute_returns(), $result);
$this->assertTrue($result);
$this->assertFalse($schedule::record_exists($scheduleid));
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
delete::execute($report->get('id'), $schedule->get('id'));
}
}
+89
View File
@@ -0,0 +1,89 @@
<?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\external\schedules;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\task\send_schedule;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for sending report schedules
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\schedules\send
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class send_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
$result = send::execute($report->get('id'), $schedule->get('id'));
$result = external_api::clean_returnvalue(send::execute_returns(), $result);
$this->assertTrue($result);
// Assert our adhoc-task was created.
$tasks = \core\task\manager::get_adhoc_tasks(send_schedule::class);
$this->assertCount(1, $tasks);
$this->assertEquals((object) [
'reportid' => $report->get('id'),
'scheduleid' => $schedule->get('id'),
], reset($tasks)->get_custom_data());
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
send::execute($report->get('id'), $schedule->get('id'));
}
}
+86
View File
@@ -0,0 +1,86 @@
<?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\external\schedules;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for toggling report schedules
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\schedules\toggle
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class toggle_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule', 'enabled' => true]);
// Confirm initial state.
$this->assertTrue($schedule->get('enabled'));
$result = toggle::execute($report->get('id'), $schedule->get('id'), false);
$result = external_api::clean_returnvalue(toggle::execute_returns(), $result);
$this->assertTrue($result);
// Check enabled state was toggled.
$schedule = $schedule->read();
$this->assertFalse($schedule->get('enabled'));
}
/**
* Test execute method for a user without permission to edit reports
*/
public function test_execute_access_exception(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
toggle::execute($report->get('id'), $schedule->get('id'), false);
}
}
@@ -0,0 +1,81 @@
<?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\external;
use advanced_testcase;
use core\context\system;
use core_reportbuilder_generator;
use core_reportbuilder\system_report_factory;
use core_reportbuilder\local\systemreports\reports_list;
use core_user\reportbuilder\datasource\users;
/**
* Unit tests for system report data exporter
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\system_report_data_exporter
* @copyright 2023 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class system_report_data_exporter_test extends advanced_testcase {
/**
* Test exported data structure
*/
public function test_export(): void {
global $PAGE;
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Two reports, created one second apart to ensure consistent ordering by time created.
$generator->create_report(['name' => 'My first report', 'source' => users::class]);
$this->waitForSecond();
$generator->create_report(['name' => 'My second report', 'source' => users::class, 'tags' => ['cat', 'dog']]);
$reportinstance = system_report_factory::create(reports_list::class, system::instance());
$exporter = new system_report_data_exporter(null, ['report' => $reportinstance, 'page' => 0, 'perpage' => 1]);
$export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
$this->assertEquals([
'Name',
'Report source',
'Tags',
'Time created',
'Time modified',
'Modified by',
], $export->headers);
$this->assertCount(1, $export->rows);
[$name, $source, $tags, $timecreated, $timemodified, $modifiedby] = $export->rows[0]['columns'];
$this->assertStringContainsString('My second report', $name);
$this->assertEquals(users::get_name(), $source);
$this->assertEquals('cat, dog', $tags);
$this->assertNotEmpty($timecreated);
$this->assertNotEmpty($timemodified);
$this->assertEquals('Admin User', $modifiedby);
$this->assertEquals(2, $export->totalrowcount);
}
}
@@ -0,0 +1,99 @@
<?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\external;
use advanced_testcase;
use context_system;
use moodle_url;
use core_reportbuilder\system_report_available;
use core_reportbuilder\system_report_factory;
/**
* Unit tests for system report exporter
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\system_report_exporter
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class system_report_exporter_test extends advanced_testcase {
/**
* Load test fixture
*/
public static function setUpBeforeClass(): void {
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/fixtures/system_report_available.php");
}
/**
* Data provider for {@see test_export}
*
* @return array[]
*/
public function export_provider(): array {
return [
['With filters' => true],
['Without filters' => false],
];
}
/**
* Text execute method
*
* @param bool $withfilters
*
* @dataProvider export_provider
*/
public function test_export(bool $withfilters): void {
global $PAGE;
$this->resetAfterTest();
// Prevent debug warnings from flexible_table.
$PAGE->set_url(new moodle_url('/'));
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance(), '', '', 0,
['withfilters' => $withfilters])->add_attributes(['data-foo' => 'bar', 'data-another' => '1']);
$exporter = new system_report_exporter($systemreport->get_report_persistent(), [
'source' => $systemreport,
'parameters' => json_encode($systemreport->get_parameters()),
]);
$data = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
$this->assertNotEmpty($data->table);
if ($withfilters) {
$this->assertEquals('{"withfilters":true}', $data->parameters);
$this->assertTrue($data->filterspresent);
$this->assertNotEmpty($data->filtersform);
} else {
$this->assertEquals('{"withfilters":false}', $data->parameters);
$this->assertFalse($data->filterspresent);
$this->assertEmpty($data->filtersform);
}
$this->assertEquals([
['name' => 'data-foo', 'value' => 'bar'],
['name' => 'data-another', 'value' => '1']
], $data->attributes);
}
}
@@ -0,0 +1,73 @@
<?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\external\systemreports;
use core\context\system;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\local\systemreports\reports_list;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for validating access to a system report
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\systemreports\can_view
* @copyright 2023 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class can_view_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
$result = can_view::execute(reports_list::class, ['contextid' => system::instance()->id], '', '', 0, []);
$result = external_api::clean_returnvalue(can_view::execute_returns(), $result);
$this->assertTrue($result);
}
/**
* Test execute method for a user without permission to view report
*/
public function test_execute_access_none(): void {
global $DB;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$userrole = $DB->get_field('role', 'id', ['shortname' => 'user']);
unassign_capability('moodle/reportbuilder:view', $userrole, system::instance());
$result = can_view::execute(reports_list::class, ['contextid' => system::instance()->id], '', '', 0, []);
$result = external_api::clean_returnvalue(can_view::execute_returns(), $result);
$this->assertFalse($result);
}
}
@@ -0,0 +1,106 @@
<?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\external\systemreports;
use core\context\system;
use core_reportbuilder_generator;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\local\systemreports\reports_list;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for retrieving system report content
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\systemreports\retrieve
* @copyright 2023 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class retrieve_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Two reports, created one second apart to ensure consistent ordering by time created.
$generator->create_report(['name' => 'My first report', 'source' => users::class]);
$this->waitForSecond();
$generator->create_report(['name' => 'My second report', 'source' => users::class, 'tags' => ['cat', 'dog']]);
// Retrieve paged results.
$result = retrieve::execute(reports_list::class, ['contextid' => system::instance()->id], '', '', 0, [], 0, 1);
$result = external_api::clean_returnvalue(retrieve::execute_returns(), $result);
$this->assertArrayHasKey('data', $result);
$this->assertEquals([
'Name',
'Report source',
'Tags',
'Time created',
'Time modified',
'Modified by',
], $result['data']['headers']);
$this->assertCount(1, $result['data']['rows']);
[$name, $source, $tags, $timecreated, $timemodified, $modifiedby] = $result['data']['rows'][0]['columns'];
$this->assertStringContainsString('My second report', $name);
$this->assertEquals(users::get_name(), $source);
$this->assertEquals('cat, dog', $tags);
$this->assertNotEmpty($timecreated);
$this->assertNotEmpty($timemodified);
$this->assertEquals('Admin User', $modifiedby);
$this->assertEquals(2, $result['data']['totalrowcount']);
$this->assertEmpty($result['warnings']);
}
/**
* Test execute method for a user without permission to view report
*/
public function test_execute_access_exception(): void {
global $DB;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$userrole = $DB->get_field('role', 'id', ['shortname' => 'user']);
unassign_capability('moodle/reportbuilder:view', $userrole, system::instance());
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot view this report');
retrieve::execute(reports_list::class, ['contextid' => system::instance()->id]);
}
}