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\reportbuilder\audience;
|
|
|
20 |
|
|
|
21 |
use advanced_testcase;
|
|
|
22 |
use context_system;
|
|
|
23 |
use core_reportbuilder_generator;
|
|
|
24 |
use core_user\reportbuilder\datasource\users;
|
|
|
25 |
|
|
|
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Unit tests for manual report audience type
|
|
|
30 |
*
|
|
|
31 |
* @package core_reportbuilder
|
|
|
32 |
* @covers \core_reportbuilder\reportbuilder\audience\manual
|
|
|
33 |
* @copyright 2021 David Matamoros <davidmc@moodle.com>
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
class manual_test extends advanced_testcase {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Test that this audience type description is generated correctly
|
|
|
40 |
*/
|
|
|
41 |
public function test_get_description(): void {
|
|
|
42 |
$this->resetAfterTest();
|
|
|
43 |
|
|
|
44 |
/** @var core_reportbuilder_generator $generator */
|
|
|
45 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
46 |
|
|
|
47 |
$user1 = $this->getDataGenerator()->create_user(['firstname' => 'User', 'lastname' => 'A']);
|
|
|
48 |
$user2 = $this->getDataGenerator()->create_user(['firstname' => 'User', 'lastname' => 'B']);
|
|
|
49 |
|
|
|
50 |
$report = $generator->create_report([
|
|
|
51 |
'name' => 'My report',
|
|
|
52 |
'source' => users::class,
|
|
|
53 |
'default' => false,
|
|
|
54 |
]);
|
|
|
55 |
|
|
|
56 |
$canviewfullnames = has_capability('moodle/site:viewfullnames', context_system::instance());
|
|
|
57 |
$audience = manual::create($report->get('id'), ['users' => [$user1->id, $user2->id]]);
|
|
|
58 |
$this->assertEquals(implode(', ', [fullname($user1, $canviewfullnames), fullname($user2, $canviewfullnames)]),
|
|
|
59 |
$audience->get_description());
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Test if user can add this audience type to the report
|
|
|
64 |
*/
|
|
|
65 |
public function test_user_can_add(): void {
|
|
|
66 |
$this->resetAfterTest();
|
|
|
67 |
|
|
|
68 |
/** @var core_reportbuilder_generator $generator */
|
|
|
69 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
70 |
|
|
|
71 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
72 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
73 |
|
|
|
74 |
$report = $generator->create_report([
|
|
|
75 |
'name' => 'My report',
|
|
|
76 |
'source' => users::class,
|
|
|
77 |
'default' => false,
|
|
|
78 |
]);
|
|
|
79 |
|
|
|
80 |
$audience = manual::create($report->get('id'), ['users' => [$user1->id, $user2->id]]);
|
|
|
81 |
|
|
|
82 |
// Admin user.
|
|
|
83 |
self::setAdminUser();
|
|
|
84 |
$this->assertTrue($audience->user_can_add());
|
|
|
85 |
|
|
|
86 |
// Non-priveleged user.
|
|
|
87 |
$user = self::getDataGenerator()->create_user();
|
|
|
88 |
self::setUser($user);
|
|
|
89 |
$this->assertFalse($audience->user_can_add());
|
|
|
90 |
|
|
|
91 |
// Grant priveleges to user.
|
|
|
92 |
$roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
|
|
|
93 |
assign_capability('moodle/user:viewalldetails', CAP_ALLOW, $roleid, context_system::instance()->id);
|
|
|
94 |
role_assign($roleid, $user->id, context_system::instance()->id);
|
|
|
95 |
$this->assertTrue($audience->user_can_add());
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Test if user can edit this audience type
|
|
|
100 |
*/
|
|
|
101 |
public function test_user_can_edit(): void {
|
|
|
102 |
$this->resetAfterTest();
|
|
|
103 |
|
|
|
104 |
/** @var core_reportbuilder_generator $generator */
|
|
|
105 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
106 |
|
|
|
107 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
108 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
109 |
|
|
|
110 |
$report = $generator->create_report([
|
|
|
111 |
'name' => 'My report',
|
|
|
112 |
'source' => users::class,
|
|
|
113 |
'default' => false,
|
|
|
114 |
]);
|
|
|
115 |
|
|
|
116 |
$audience = manual::create($report->get('id'), ['users' => [$user1->id, $user2->id]]);
|
|
|
117 |
|
|
|
118 |
// Admin user.
|
|
|
119 |
self::setAdminUser();
|
|
|
120 |
$this->assertTrue($audience->user_can_edit());
|
|
|
121 |
|
|
|
122 |
// Non-priveleged user.
|
|
|
123 |
$user = self::getDataGenerator()->create_user();
|
|
|
124 |
self::setUser($user);
|
|
|
125 |
$this->assertFalse($audience->user_can_edit());
|
|
|
126 |
|
|
|
127 |
// Grant priveleges to user.
|
|
|
128 |
$roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
|
|
|
129 |
assign_capability('moodle/user:viewalldetails', CAP_ALLOW, $roleid, context_system::instance()->id);
|
|
|
130 |
role_assign($roleid, $user->id, context_system::instance()->id);
|
|
|
131 |
$this->assertTrue($audience->user_can_edit());
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* Test that sql generated is correct
|
|
|
136 |
*/
|
|
|
137 |
public function test_get_sql(): void {
|
|
|
138 |
global $DB;
|
|
|
139 |
$this->resetAfterTest();
|
|
|
140 |
|
|
|
141 |
/** @var core_reportbuilder_generator $generator */
|
|
|
142 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
143 |
|
|
|
144 |
$report = $generator->create_report([
|
|
|
145 |
'name' => 'My report',
|
|
|
146 |
'source' => users::class,
|
|
|
147 |
'default' => false,
|
|
|
148 |
]);
|
|
|
149 |
|
|
|
150 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
151 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
152 |
$user3 = $this->getDataGenerator()->create_user();
|
|
|
153 |
|
|
|
154 |
$audience = manual::create($report->get('id'), ['users' => [$user1->id, $user3->id]]);
|
|
|
155 |
|
|
|
156 |
[$join, $where, $params] = $audience->get_sql('u');
|
|
|
157 |
$query = 'SELECT u.* FROM {user} u ' . $join . ' WHERE ' . $where;
|
|
|
158 |
$records = $DB->get_records_sql($query, $params);
|
|
|
159 |
|
|
|
160 |
$this->assertEqualsCanonicalizing([$user1->id, $user3->id], array_column($records, 'id'));
|
|
|
161 |
}
|
|
|
162 |
}
|