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