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 core_reportbuilder_generator;
|
|
|
23 |
use core_user\reportbuilder\datasource\users;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Unit tests for the administrators audience type
|
|
|
27 |
*
|
|
|
28 |
* @package core_reportbuilder
|
|
|
29 |
* @covers \core_reportbuilder\reportbuilder\audience\admins
|
|
|
30 |
* @copyright 2022 Paul Holden <paulh@moodle.com>
|
|
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
32 |
*/
|
|
|
33 |
class admins_test extends advanced_testcase {
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Test whether user can add this audience
|
|
|
37 |
*/
|
|
|
38 |
public function test_user_can_add(): void {
|
|
|
39 |
$this->resetAfterTest();
|
|
|
40 |
|
|
|
41 |
/** @var core_reportbuilder_generator $generator */
|
|
|
42 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
43 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
|
|
|
44 |
|
|
|
45 |
$audience = admins::create($report->get('id'), []);
|
|
|
46 |
$this->assertFalse($audience->user_can_add());
|
|
|
47 |
|
|
|
48 |
// Switch to privileged user.
|
|
|
49 |
$this->setAdminUser();
|
|
|
50 |
$this->assertTrue($audience->user_can_add());
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Test whether user can edit this audience
|
|
|
55 |
*/
|
|
|
56 |
public function test_user_can_edit(): void {
|
|
|
57 |
$this->resetAfterTest();
|
|
|
58 |
|
|
|
59 |
/** @var core_reportbuilder_generator $generator */
|
|
|
60 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
61 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
|
|
|
62 |
|
|
|
63 |
$audience = admins::create($report->get('id'), []);
|
|
|
64 |
$this->assertFalse($audience->user_can_edit());
|
|
|
65 |
|
|
|
66 |
// Switch to privileged user.
|
|
|
67 |
$this->setAdminUser();
|
|
|
68 |
$this->assertTrue($audience->user_can_edit());
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Test retrieving audience SQL for matching users
|
|
|
73 |
*/
|
|
|
74 |
public function test_get_sql(): void {
|
|
|
75 |
global $CFG, $DB;
|
|
|
76 |
|
|
|
77 |
$this->resetAfterTest();
|
|
|
78 |
|
|
|
79 |
/** @var core_reportbuilder_generator $generator */
|
|
|
80 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
81 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
|
|
|
82 |
|
|
|
83 |
// Create three users, set only the initial two as admins.
|
|
|
84 |
$userone = $this->getDataGenerator()->create_user();
|
|
|
85 |
$usertwo = $this->getDataGenerator()->create_user();
|
|
|
86 |
$userthree = $this->getDataGenerator()->create_user();
|
|
|
87 |
|
|
|
88 |
$CFG->siteadmins = "{$userone->id},{$usertwo->id}";
|
|
|
89 |
|
|
|
90 |
$audience = admins::create($report->get('id'), []);
|
|
|
91 |
[$join, $select, $params] = $audience->get_sql('u');
|
|
|
92 |
|
|
|
93 |
$users = $DB->get_fieldset_sql("SELECT u.id FROM {user} u {$join} WHERE {$select}", $params);
|
|
|
94 |
$this->assertEqualsCanonicalizing([$userone->id, $usertwo->id], $users);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Test showing audience description
|
|
|
99 |
*/
|
|
|
100 |
public function test_get_description(): void {
|
|
|
101 |
global $CFG;
|
|
|
102 |
|
|
|
103 |
$this->resetAfterTest();
|
|
|
104 |
|
|
|
105 |
/** @var core_reportbuilder_generator $generator */
|
|
|
106 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
107 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
|
|
|
108 |
|
|
|
109 |
// Create three users, set only the initial two as admins.
|
|
|
110 |
$userone = $this->getDataGenerator()->create_user();
|
|
|
111 |
$usertwo = $this->getDataGenerator()->create_user();
|
|
|
112 |
$userthree = $this->getDataGenerator()->create_user();
|
|
|
113 |
|
|
|
114 |
$CFG->siteadmins = "{$userone->id},{$usertwo->id}";
|
|
|
115 |
|
|
|
116 |
$audience = admins::create($report->get('id'), []);
|
|
|
117 |
$this->assertEquals(fullname($userone) . ', ' . fullname($usertwo), $audience->get_description());
|
|
|
118 |
}
|
|
|
119 |
}
|