Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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\reports;
20
 
21
use core_reportbuilder_generator;
22
use core_external\external_api;
23
use externallib_advanced_testcase;
24
use core_reportbuilder\report_access_exception;
25
use core_user\reportbuilder\datasource\users;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
global $CFG;
30
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
31
 
32
/**
33
 * Unit tests of external class for retrieving custom report content
34
 *
35
 * @package     core_reportbuilder
36
 * @covers      \core_reportbuilder\external\reports\retrieve
37
 * @copyright   2022 Paul Holden <paulh@moodle.com>
38
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class retrieve_test extends externallib_advanced_testcase {
41
 
42
    /**
43
     * Text execute method
44
     */
45
    public function test_execute(): void {
46
        $this->resetAfterTest();
47
        $this->setAdminUser();
48
 
49
        $this->getDataGenerator()->create_user(['firstname' => 'Zoe', 'lastname' => 'Zebra', 'email' => 'u1@example.com']);
50
        $this->getDataGenerator()->create_user(['firstname' => 'Charlie', 'lastname' => 'Carrot', 'email' => 'u2@example.com']);
51
 
52
        /** @var core_reportbuilder_generator $generator */
53
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
54
 
55
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
56
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname', 'sortenabled' => 1]);
57
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
58
 
59
        // There are three users (admin plus the two previouly created), but we're paging the first two only.
60
        $result = retrieve::execute($report->get('id'), 0, 2);
61
        $result = external_api::clean_returnvalue(retrieve::execute_returns(), $result);
62
 
63
        // All data is generated by exporters, just assert relevant sample of each.
64
        $this->assertArrayHasKey('details', $result);
65
        $this->assertEquals('My report', $result['details']['name']);
66
 
67
        $this->assertArrayHasKey('data', $result);
68
        $this->assertEquals(['Full name', 'Email address'], $result['data']['headers']);
69
        $this->assertEquals([
70
            [
71
                'columns' => ['Admin User', 'admin@example.com'],
72
            ],
73
            [
74
                'columns' => ['Charlie Carrot', 'u2@example.com'],
75
            ],
76
        ], $result['data']['rows']);
77
        $this->assertEquals(3, $result['data']['totalrowcount']);
78
        $this->assertEmpty($result['warnings']);
79
 
80
        // Retrieve the second set of pages results.
81
        $result = retrieve::execute($report->get('id'), 1, 2);
82
        $result = external_api::clean_returnvalue(retrieve::execute_returns(), $result);
83
 
84
        $this->assertArrayHasKey('details', $result);
85
        $this->assertEquals('My report', $result['details']['name']);
86
 
87
        $this->assertArrayHasKey('data', $result);
88
        $this->assertEquals(['Full name', 'Email address'], $result['data']['headers']);
89
        $this->assertEquals([
90
            [
91
                'columns' => ['Zoe Zebra', 'u1@example.com'],
92
            ],
93
        ], $result['data']['rows']);
94
        $this->assertEquals(3, $result['data']['totalrowcount']);
95
        $this->assertEmpty($result['warnings']);
96
    }
97
 
98
    /**
99
     * Test execute method for a user without permission to view report
100
     */
101
    public function test_execute_access_exception(): void {
102
        $this->resetAfterTest();
103
 
104
        /** @var core_reportbuilder_generator $generator */
105
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
106
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
107
 
108
        $user = $this->getDataGenerator()->create_user();
109
        $this->setUser($user);
110
 
111
        $this->expectException(report_access_exception::class);
112
        $this->expectExceptionMessage('You cannot view this report');
113
        retrieve::execute($report->get('id'));
114
    }
115
}