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;
22
use core_reportbuilder_generator;
23
use core_external\external_api;
24
use externallib_advanced_testcase;
1441 ariadna 25
use core_reportbuilder\exception\report_access_exception;
26
use core_reportbuilder\manager;
27
use core_reportbuilder\output\report_action;
1 efrain 28
use core_user\reportbuilder\datasource\users;
29
 
30
defined('MOODLE_INTERNAL') || die();
31
 
32
global $CFG;
33
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
34
 
35
/**
36
 * Unit tests of external class for getting reports
37
 *
38
 * @package     core_reportbuilder
39
 * @covers      \core_reportbuilder\external\reports\get
40
 * @copyright   2021 David Matamoros <davidmc@moodle.com>
41
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 */
1441 ariadna 43
final class get_test extends externallib_advanced_testcase {
1 efrain 44
 
45
    /**
46
     * Text execute method for edit mode
47
     */
48
    public function test_execute_editmode(): void {
49
        $this->resetAfterTest();
50
        $this->setAdminUser();
51
 
52
        /** @var core_reportbuilder_generator $generator */
53
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
1441 ariadna 54
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
1 efrain 55
 
1441 ariadna 56
        $instance = manager::get_report_from_persistent($report);
57
        $instance->set_report_action(new report_action('Add', ['class' => 'btn', 'data-action' => 'action']));
58
        $instance->set_report_info_container('Hello');
1 efrain 59
 
60
        // Add two filters.
61
        $filterfullname = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
62
        $filteremail = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
63
 
64
        $result = get::execute($report->get('id'), true);
65
        $result = external_api::clean_returnvalue(get::execute_returns(), $result);
66
 
67
        $this->assertEquals($result['id'], $report->get('id'));
68
        $this->assertEquals($result['name'], 'My report');
69
        $this->assertEquals($result['source'], users::class);
70
        $this->assertNotEmpty($result['table']);
71
        $this->assertNotEmpty($result['javascript']);
1441 ariadna 72
        $this->assertEquals('Hello', $result['infocontainer']);
1 efrain 73
        $this->assertFalse($result['filterspresent']);
74
        $this->assertEmpty($result['filtersform']);
75
        $this->assertTrue($result['editmode']);
76
 
77
        // Confirm editor-specific data is returned.
78
        $this->assertNotEmpty($result['sidebarmenucards']);
79
        $this->assertNotEmpty($result['conditions']);
80
        $this->assertNotEmpty($result['filters']);
81
        $this->assertTrue($result['filters']['hasavailablefilters']);
82
        $this->assertNotEmpty($result['filters']['availablefilters']);
83
        $this->assertTrue($result['filters']['hasactivefilters']);
84
        $this->assertEquals($filterfullname->get('id'), $result['filters']['activefilters'][0]['id']);
85
        $this->assertEquals($filteremail->get('id'), $result['filters']['activefilters'][1]['id']);
86
        $this->assertNotEmpty($result['sorting']);
87
        $this->assertNotEmpty($result['cardview']);
1441 ariadna 88
 
89
        // The following should not be present when editing.
90
        $this->assertArrayNotHasKey('button', $result);
1 efrain 91
    }
92
 
93
    /**
94
     * Text execute method for preview mode
95
     */
96
    public function test_execute_previewmode(): void {
97
        $this->resetAfterTest();
98
        $this->setAdminUser();
99
 
100
        /** @var core_reportbuilder_generator $generator */
101
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
1441 ariadna 102
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
1 efrain 103
 
1441 ariadna 104
        $instance = manager::get_report_from_persistent($report);
105
        $instance->set_report_action(new report_action('Add', ['class' => 'btn', 'data-action' => 'action']));
106
        $instance->set_report_info_container('Hello');
1 efrain 107
 
108
        // Add two filters.
109
        $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
110
        $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
111
 
112
        $result = get::execute($report->get('id'), false);
113
        $result = external_api::clean_returnvalue(get::execute_returns(), $result);
114
 
115
        $this->assertEquals($result['id'], $report->get('id'));
116
        $this->assertEquals($result['name'], 'My report');
117
        $this->assertEquals($result['source'], users::class);
118
        $this->assertNotEmpty($result['table']);
119
        $this->assertNotEmpty($result['javascript']);
1441 ariadna 120
        $this->assertEquals([
121
            'tag' => 'button',
122
            'title' => 'Add',
123
            'attributes' => [
124
                ['name' => 'class', 'value' => 'btn'],
125
                ['name' => 'data-action', 'value' => 'action'],
126
            ],
127
        ], $result['button']);
128
        $this->assertEquals('Hello', $result['infocontainer']);
1 efrain 129
        $this->assertTrue($result['filterspresent']);
130
        $this->assertNotEmpty($result['filtersform']);
131
        $this->assertFalse($result['editmode']);
132
 
1441 ariadna 133
        // The following should not be present when viewing.
1 efrain 134
        $this->assertArrayNotHasKey('sidebarmenucards', $result);
135
        $this->assertArrayNotHasKey('conditions', $result);
136
        $this->assertArrayNotHasKey('filters', $result);
137
        $this->assertArrayNotHasKey('sorting', $result);
138
        $this->assertArrayNotHasKey('cardview', $result);
139
    }
140
 
141
    /**
142
     * Test execute method for a user without permission to edit reports
143
     */
144
    public function test_execute_access_exception(): void {
145
        $this->resetAfterTest();
146
 
147
        /** @var core_reportbuilder_generator $generator */
148
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
149
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
150
 
151
        $user = $this->getDataGenerator()->create_user();
152
        $this->setUser($user);
153
 
154
        $this->expectException(report_access_exception::class);
155
        $this->expectExceptionMessage('You cannot edit this report');
156
        get::execute($report->get('id'), true);
157
    }
158
 
159
    /**
160
     * Test execute method for a user without permission to view reports
161
     */
162
    public function test_execute_view_access_exception(): void {
163
        $this->resetAfterTest();
164
 
165
        /** @var core_reportbuilder_generator $generator */
166
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
167
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
168
 
169
        $user = $this->getDataGenerator()->create_user();
170
        $contextid = context_system::instance()->id;
171
        $roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
172
        assign_capability('moodle/reportbuilder:view', CAP_PROHIBIT, $roleid, $contextid);
173
        role_assign($roleid, $user->id, $contextid);
174
 
175
        $this->setUser($user);
176
 
177
        $this->expectException(report_access_exception::class);
178
        $this->expectExceptionMessage('You cannot view this report');
179
        get::execute($report->get('id'), false);
180
    }
181
}