Proyectos de Subversion Moodle

Rev

| 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\task;
20
 
21
use advanced_testcase;
22
use core_collator;
23
use core_reportbuilder_generator;
24
use core_reportbuilder\manager;
25
use core_reportbuilder\local\filters\user;
26
use core_reportbuilder\local\models\schedule;
27
use core_reportbuilder\local\filters\text;
28
use core_reportbuilder\reportbuilder\audience\manual;
29
use core_user;
30
use core_user\reportbuilder\datasource\users;
31
 
32
/**
33
 * Unit tests for ad-hoc task for sending report schedule
34
 *
35
 * @package     core_reportbuilder
36
 * @covers      \core_reportbuilder\task\send_schedule
37
 * @copyright   2021 Paul Holden <paulh@moodle.com>
38
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class send_schedule_test extends advanced_testcase {
41
 
42
    /**
43
     * Data provider for {@see test_execute_viewas_user}
44
     *
45
     * @return array[]
46
     */
47
    public function execute_report_viewas_user_provider(): array {
48
        return [
49
            'View report as schedule creator' => [schedule::REPORT_VIEWAS_CREATOR, null, 'admin', 'admin'],
50
            'View report as schedule recipient' => [schedule::REPORT_VIEWAS_RECIPIENT, null, 'userone', 'usertwo'],
51
            'View report as specific user' => [null, 'userone', 'userone', 'userone'],
52
        ];
53
    }
54
 
55
    /**
56
     * Test executing task for a schedule with differing "View as user" configuration
57
     *
58
     * @param int|null $viewasuser
59
     * @param string|null $viewasusername
60
     * @param string $useronesees
61
     * @param string $usertwosees
62
     *
63
     * @dataProvider execute_report_viewas_user_provider
64
     */
65
    public function test_execute_report_viewas_user(
66
        ?int $viewasuser,
67
        ?string $viewasusername,
68
        string $useronesees,
69
        string $usertwosees
70
    ): void {
71
        $this->preventResetByRollback();
72
        $this->resetAfterTest();
73
        $this->setAdminUser();
74
 
75
        $userone = $this->getDataGenerator()->create_user([
76
            'username' => 'userone',
77
            'email' => 'user1@example.com',
78
            'firstname' => 'Zoe',
79
            'lastname' => 'Zebra',
80
        ]);
81
        $usertwo = $this->getDataGenerator()->create_user([
82
            'username' => 'usertwo',
83
            'email' => 'user2@example.com',
84
            'firstname' => 'Henrietta',
85
            'lastname' => 'Hamster',
86
        ]);
87
 
88
        /** @var core_reportbuilder_generator $generator */
89
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
90
 
91
        // Create a report, with a single column and condition that the current user only sees themselves.
92
        $report = $generator->create_report(['name' => 'Myself', 'source' => users::class, 'default' => false]);
93
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']);
94
 
95
        $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:userselect']);
96
        manager::get_report_from_persistent($report)
97
            ->set_condition_values(['user:userselect_operator' => user::USER_CURRENT]);
98
 
99
        // Add audience/schedule for our two test users.
100
        $audience = $generator->create_audience([
101
            'reportid' => $report->get('id'),
102
            'classname' => manual::class,
103
            'configdata' => [
104
                'users' => [$userone->id, $usertwo->id],
105
            ],
106
        ]);
107
 
108
        // If "View as user" isn't specified, it should be the ID of the given "View as username".
109
        if ($viewasuser === null) {
110
            $viewasuser = core_user::get_user_by_username($viewasusername, '*', null, MUST_EXIST)->id;
111
        }
112
        $schedule = $generator->create_schedule([
113
            'reportid' => $report->get('id'),
114
            'name' => 'My schedule',
115
            'userviewas' => $viewasuser,
116
            'audiences' => json_encode([$audience->get_persistent()->get('id')]),
117
        ]);
118
 
119
        // Send the schedule, catch emails in sink (noting the users are sorted alphabetically).
120
        $sink = $this->redirectEmails();
121
 
122
        $this->expectOutputRegex("/^Sending schedule: My schedule\n" .
123
            "  Sending to: " . fullname($usertwo) . "\n" .
124
            "  Sending to: " . fullname($userone) . "\n" .
125
            "Sending schedule complete\n/"
126
        );
127
        $sendschedule = new send_schedule();
128
        $sendschedule->set_custom_data(['reportid' => $report->get('id'), 'scheduleid' => $schedule->get('id')]);
129
        $sendschedule->execute();
130
 
131
        $messages = $sink->get_messages();
132
        $this->assertCount(2, $messages);
133
 
134
        $sink->close();
135
 
136
        // Ensure caught messages are consistently ordered by recipient email prior to assertions.
137
        core_collator::asort_objects_by_property($messages, 'to');
138
        $messages = array_values($messages);
139
 
140
        $messageoneattachment = self::extract_message_attachment($messages[0]->body);
141
        $this->assertEquals($userone->email, $messages[0]->to);
142
        $this->assertStringEndsWith("Username\n{$useronesees}\n", $messageoneattachment);
143
 
144
        $messagetwoattachment = self::extract_message_attachment($messages[1]->body);
145
        $this->assertEquals($usertwo->email, $messages[1]->to);
146
        $this->assertStringEndsWith("Username\n{$usertwosees}\n", $messagetwoattachment);
147
    }
148
 
149
    /**
150
     * Test executing task where the schedule "View as user" is an inactive account
151
     */
152
    public function test_execute_report_viewas_user_invalid(): void {
153
        $this->resetAfterTest();
154
        $this->setAdminUser();
155
 
156
        /** @var core_reportbuilder_generator $generator */
157
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
158
 
159
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
160
        $audience = $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => []]);
161
 
162
        $schedule = $generator->create_schedule([
163
            'reportid' => $report->get('id'),
164
            'name' => 'My schedule',
165
            'userviewas' => 42,
166
            'audiences' => json_encode([$audience->get_persistent()->get('id')]),
167
        ]);
168
 
169
        $this->expectOutputRegex("/^Sending schedule: My schedule\nInvalid schedule view as user: Invalid user/");
170
        $sendschedule = new send_schedule();
171
        $sendschedule->set_custom_data(['reportid' => $report->get('id'), 'scheduleid' => $schedule->get('id')]);
172
        $sendschedule->execute();
173
    }
174
 
175
    /**
176
     * Test executing task for a schedule that is configured to not send empty reports
177
     */
178
    public function test_execute_report_empty(): void {
179
        $this->resetAfterTest();
180
        $this->setAdminUser();
181
 
182
        /** @var core_reportbuilder_generator $generator */
183
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
184
 
185
        // Create a report that won't return any data.
186
        $report = $generator->create_report(['name' => 'Myself', 'source' => users::class, 'default' => false]);
187
 
188
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']);
189
        $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']);
190
 
191
        manager::get_report_from_persistent($report)->set_condition_values([
192
            'user:username_operator' => text::IS_EQUAL_TO,
193
            'user:username_value' => 'baconlettucetomato',
194
        ]);
195
 
196
        $audience = $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => []]);
197
        $schedule = $generator->create_schedule([
198
            'reportid' => $report->get('id'),
199
            'name' => 'My schedule',
200
            'audiences' => json_encode([$audience->get_persistent()->get('id')]),
201
            'reportempty' => schedule::REPORT_EMPTY_DONT_SEND,
202
        ]);
203
 
204
        $this->expectOutputString("Sending schedule: My schedule\n" .
205
            "  Empty report, skipping\n" .
206
            "Sending schedule complete\n");
207
        $sendschedule = new send_schedule();
208
        $sendschedule->set_custom_data(['reportid' => $report->get('id'), 'scheduleid' => $schedule->get('id')]);
209
        $sendschedule->execute();
210
    }
211
 
212
    /**
213
     * Test executing task for a schedule that contains no recipients
214
     */
215
    public function test_execute_schedule_no_recipients(): void {
216
        $this->resetAfterTest();
217
        $this->setAdminUser();
218
 
219
        /** @var core_reportbuilder_generator $generator */
220
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
221
 
222
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
223
        $schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
224
 
225
        $this->expectOutputString("Sending schedule: My schedule\n" .
226
            "Sending schedule complete\n");
227
        $sendschedule = new send_schedule();
228
        $sendschedule->set_custom_data(['reportid' => $report->get('id'), 'scheduleid' => $schedule->get('id')]);
229
        $sendschedule->execute();
230
    }
231
 
232
    /**
233
     * Test executing task where the schedule creator is an inactive account
234
     */
235
    public function test_execute_schedule_creator_invalid(): void {
236
        $this->resetAfterTest();
237
 
238
        /** @var core_reportbuilder_generator $generator */
239
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
240
 
241
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
242
        $schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule', 'usercreated' => 42]);
243
 
244
        $this->expectOutputRegex("/^Sending schedule: My schedule\nInvalid schedule creator: Invalid user/");
245
        $sendschedule = new send_schedule();
246
        $sendschedule->set_custom_data(['reportid' => $report->get('id'), 'scheduleid' => $schedule->get('id')]);
247
        $sendschedule->execute();
248
    }
249
 
250
    /**
251
     * Test executing task given invalid schedule data
252
     */
253
    public function test_execute_schedule_invalid(): void {
254
        $this->resetAfterTest();
255
        $this->setAdminUser();
256
 
257
        /** @var core_reportbuilder_generator $generator */
258
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
259
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
260
 
261
        $this->expectOutputString("Invalid schedule\n");
262
        $sendschedule = new send_schedule();
263
        $sendschedule->set_custom_data(['reportid' => $report->get('id'), 'scheduleid' => 42]);
264
        $sendschedule->execute();
265
    }
266
 
267
    /**
268
     * Given a multi-part message in MIME format, return the base64 encoded attachment contained within
269
     *
270
     * @param string $messagebody
271
     * @return string
272
     */
273
    private static function extract_message_attachment(string $messagebody): string {
274
        $mimepart = preg_split('/Content-Disposition: attachment; filename="My schedule.csv"\s+/m', $messagebody);
275
 
276
        // Extract the base64 encoded content after the "Content-Disposition" header.
277
        preg_match_all('/^([A-Z0-9\/\+=]+)\s/im', $mimepart[1], $matches);
278
 
279
        return base64_decode(implode($matches[0]));
280
    }
281
}