Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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 context_system;
1441 ariadna 22
use core_customfield_generator;
1 efrain 23
use core_reportbuilder_generator;
24
use core_external\external_api;
25
use externallib_advanced_testcase;
1441 ariadna 26
use core_reportbuilder\exception\report_access_exception;
1 efrain 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 listing reports
36
 *
37
 * @package     core_reportbuilder
38
 * @covers      \core_reportbuilder\external\reports\listing
39
 * @copyright   2022 Paul Holden <paulh@moodle.com>
40
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
1441 ariadna 42
final class listing_test extends externallib_advanced_testcase {
1 efrain 43
 
44
    /**
45
     * Text execute method
46
     */
47
    public function test_execute(): void {
48
        $this->resetAfterTest();
49
        $this->setAdminUser();
50
 
1441 ariadna 51
        /** @var core_customfield_generator $generator */
52
        $generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
53
        $category = $generator->create_category(['component' => 'core_reportbuilder', 'area' => 'report']);
54
        $generator->create_field([
55
            'categoryid' => $category->get('id'),
56
            'name' => 'My field',
57
            'shortname' => 'myfield',
58
            'type' => 'number',
59
        ]);
60
 
1 efrain 61
        /** @var core_reportbuilder_generator $generator */
62
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
63
 
64
        // Create three reports.
1441 ariadna 65
        $reportone = $generator->create_report([
66
            'name' => 'Report one',
67
            'source' => users::class,
68
            'tags' => ['cat', 'dog'],
69
            'customfield_myfield' => 42,
70
        ]);
1 efrain 71
        $reporttwo = $generator->create_report(['name' => 'Report two', 'source' => users::class]);
72
        $reportthree = $generator->create_report(['name' => 'Report three', 'source' => users::class]);
73
 
74
        // Create second user, with audience of both report one and two.
75
        $user = $this->getDataGenerator()->create_user();
76
        $this->setUser($user);
77
 
78
        $generator->create_audience(['reportid' => $reportone->get('id'), 'configdata' => []]);
79
        $generator->create_audience(['reportid' => $reporttwo->get('id'), 'configdata' => []]);
80
 
81
        // Switch to second user, get their report listing.
82
        $result = listing::execute();
83
        $result = external_api::clean_returnvalue(listing::execute_returns(), $result);
84
 
1441 ariadna 85
        // All data is generated by exporters, just assert relevant sample of each.
1 efrain 86
        $this->assertEquals(['Report one', 'Report two'], array_column($result['reports'], 'name'));
1441 ariadna 87
 
88
        $tagscolumn = array_column($result['reports'], 'tags');
89
        $this->assertEquals([
90
            ['cat', 'dog'],
91
            [],
92
        ], array_map(fn(array $tags) => array_column($tags, 'name'), $tagscolumn));
93
 
94
        $customfieldscolumn = array_column($result['reports'], 'customfields');
95
        $this->assertEquals([
96
            ['42'],
97
            [null],
98
        ], array_map(fn(array $customfields) => array_column($customfields['data'], 'value'), $customfieldscolumn));
99
 
1 efrain 100
        $this->assertEmpty($result['warnings']);
101
    }
102
 
103
    /**
104
     * Test execute method for a user without permission to view reports
105
     */
106
    public function test_execute_access_exception(): void {
107
        global $DB;
108
 
109
        $this->resetAfterTest();
110
 
111
        $userrole = $DB->get_field('role', 'id', ['shortname' => 'user'], MUST_EXIST);
112
        assign_capability('moodle/reportbuilder:view', CAP_PROHIBIT, $userrole, context_system::instance(), true);
113
 
114
        $user = $this->getDataGenerator()->create_user();
115
        $this->setUser($user);
116
 
117
        $this->expectException(report_access_exception::class);
118
        $this->expectExceptionMessage('You cannot view this report');
119
        listing::execute();
120
    }
121
}