1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
declare(strict_types=1);
|
|
|
18 |
|
|
|
19 |
namespace core_reportbuilder\external\systemreports;
|
|
|
20 |
|
|
|
21 |
use core\context\system;
|
|
|
22 |
use core_reportbuilder_generator;
|
|
|
23 |
use core_external\external_api;
|
|
|
24 |
use externallib_advanced_testcase;
|
|
|
25 |
use core_reportbuilder\report_access_exception;
|
|
|
26 |
use core_reportbuilder\local\systemreports\reports_list;
|
|
|
27 |
use core_user\reportbuilder\datasource\users;
|
|
|
28 |
|
|
|
29 |
defined('MOODLE_INTERNAL') || die();
|
|
|
30 |
|
|
|
31 |
global $CFG;
|
|
|
32 |
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Unit tests of external class for retrieving system report content
|
|
|
36 |
*
|
|
|
37 |
* @package core_reportbuilder
|
|
|
38 |
* @covers \core_reportbuilder\external\systemreports\retrieve
|
|
|
39 |
* @copyright 2023 Paul Holden <paulh@moodle.com>
|
|
|
40 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
41 |
*/
|
|
|
42 |
class retrieve_test extends externallib_advanced_testcase {
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Text execute method
|
|
|
46 |
*/
|
|
|
47 |
public function test_execute(): void {
|
|
|
48 |
$this->resetAfterTest();
|
|
|
49 |
$this->setAdminUser();
|
|
|
50 |
|
|
|
51 |
/** @var core_reportbuilder_generator $generator */
|
|
|
52 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
53 |
|
|
|
54 |
// Two reports, created one second apart to ensure consistent ordering by time created.
|
|
|
55 |
$generator->create_report(['name' => 'My first report', 'source' => users::class]);
|
|
|
56 |
$this->waitForSecond();
|
|
|
57 |
$generator->create_report(['name' => 'My second report', 'source' => users::class, 'tags' => ['cat', 'dog']]);
|
|
|
58 |
|
|
|
59 |
// Retrieve paged results.
|
|
|
60 |
$result = retrieve::execute(reports_list::class, ['contextid' => system::instance()->id], '', '', 0, [], 0, 1);
|
|
|
61 |
$result = external_api::clean_returnvalue(retrieve::execute_returns(), $result);
|
|
|
62 |
|
|
|
63 |
$this->assertArrayHasKey('data', $result);
|
|
|
64 |
$this->assertEquals([
|
|
|
65 |
'Name',
|
|
|
66 |
'Report source',
|
|
|
67 |
'Tags',
|
|
|
68 |
'Time created',
|
|
|
69 |
'Time modified',
|
|
|
70 |
'Modified by',
|
|
|
71 |
], $result['data']['headers']);
|
|
|
72 |
|
|
|
73 |
$this->assertCount(1, $result['data']['rows']);
|
|
|
74 |
[$name, $source, $tags, $timecreated, $timemodified, $modifiedby] = $result['data']['rows'][0]['columns'];
|
|
|
75 |
|
|
|
76 |
$this->assertStringContainsString('My second report', $name);
|
|
|
77 |
$this->assertEquals(users::get_name(), $source);
|
|
|
78 |
$this->assertEquals('cat, dog', $tags);
|
|
|
79 |
$this->assertNotEmpty($timecreated);
|
|
|
80 |
$this->assertNotEmpty($timemodified);
|
|
|
81 |
$this->assertEquals('Admin User', $modifiedby);
|
|
|
82 |
|
|
|
83 |
$this->assertEquals(2, $result['data']['totalrowcount']);
|
|
|
84 |
|
|
|
85 |
$this->assertEmpty($result['warnings']);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Test execute method for a user without permission to view report
|
|
|
90 |
*/
|
|
|
91 |
public function test_execute_access_exception(): void {
|
|
|
92 |
global $DB;
|
|
|
93 |
|
|
|
94 |
$this->resetAfterTest();
|
|
|
95 |
|
|
|
96 |
$user = $this->getDataGenerator()->create_user();
|
|
|
97 |
$this->setUser($user);
|
|
|
98 |
|
|
|
99 |
$userrole = $DB->get_field('role', 'id', ['shortname' => 'user']);
|
|
|
100 |
unassign_capability('moodle/reportbuilder:view', $userrole, system::instance());
|
|
|
101 |
|
|
|
102 |
$this->expectException(report_access_exception::class);
|
|
|
103 |
$this->expectExceptionMessage('You cannot view this report');
|
|
|
104 |
retrieve::execute(reports_list::class, ['contextid' => system::instance()->id]);
|
|
|
105 |
}
|
|
|
106 |
}
|