1441 |
ariadna |
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 |
/**
|
|
|
18 |
* Mailpit mail handling implementation.
|
|
|
19 |
*
|
|
|
20 |
* @package core
|
|
|
21 |
* @category test
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
* @copyright Simey Lameze <simey@moodle.com>
|
|
|
24 |
*/
|
|
|
25 |
namespace core\test;
|
|
|
26 |
|
|
|
27 |
use core\http_client;
|
|
|
28 |
use stdClass;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Mailpit email handling class.
|
|
|
32 |
*
|
|
|
33 |
* @package core
|
|
|
34 |
* @category test
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*/
|
|
|
37 |
class mailpit_email_catcher implements email_catcher {
|
|
|
38 |
|
|
|
39 |
/** @var http_client The http client object. */
|
|
|
40 |
protected http_client $httpclient;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Constructor.
|
|
|
44 |
*
|
|
|
45 |
* @param string $baseuri The base uri for the mailpit server.
|
|
|
46 |
*/
|
|
|
47 |
public function __construct(string $baseuri) {
|
|
|
48 |
$this->httpclient = new http_client(['base_uri' => $baseuri]);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Reset the mailpit server after a test.
|
|
|
53 |
*/
|
|
|
54 |
public function reset_after_test(): void {
|
|
|
55 |
$this->httpclient->delete_all();
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Delete all messages from the mailpit server.
|
|
|
60 |
*/
|
|
|
61 |
public function delete_all() {
|
|
|
62 |
$this->httpclient->delete('api/v1/messages');
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Get a list of messages from the mailpit server.
|
|
|
67 |
*
|
|
|
68 |
* @param bool $showdetails Optional. Whether to include detailed information in the messages. Default is false.
|
|
|
69 |
* @return iterable
|
|
|
70 |
*/
|
|
|
71 |
public function get_messages(bool $showdetails = false): iterable {
|
|
|
72 |
$uri = 'api/v1/messages';
|
|
|
73 |
$options = [
|
|
|
74 |
'query' => [
|
|
|
75 |
'start' => 0,
|
|
|
76 |
],
|
|
|
77 |
];
|
|
|
78 |
|
|
|
79 |
do {
|
|
|
80 |
$response = $this->httpclient->get(
|
|
|
81 |
uri: $uri,
|
|
|
82 |
options: $options,
|
|
|
83 |
);
|
|
|
84 |
|
|
|
85 |
$data = json_decode($response->getBody());
|
|
|
86 |
foreach ($data->messages as $messagedata) {
|
|
|
87 |
yield mailpit_message::create_from_api_response($this, $messagedata, $showdetails);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
$options['query']['start'] = $data->start + $data->count;
|
|
|
91 |
} while ($data->total > ($options['query']['start']));
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Get the message summary for a specific message.
|
|
|
96 |
*
|
|
|
97 |
* @param string $id The message id.
|
|
|
98 |
* @return stdClass
|
|
|
99 |
*/
|
|
|
100 |
public function get_message_data(string $id): stdClass {
|
|
|
101 |
$response = $this->httpclient->get("api/v1/message/{$id}");
|
|
|
102 |
|
|
|
103 |
return json_decode($response->getBody());
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Search for a message in the mailpit server.
|
|
|
108 |
*
|
|
|
109 |
* @param string $query The search query.
|
|
|
110 |
* @return mixed
|
|
|
111 |
*/
|
|
|
112 |
public function search(string $query): iterable {
|
|
|
113 |
$uri = "api/v1/search?query={$query}";
|
|
|
114 |
|
|
|
115 |
$response = $this->httpclient->get($uri);
|
|
|
116 |
$data = json_decode($response->getBody());
|
|
|
117 |
|
|
|
118 |
foreach ($data->messages as $messagedata) {
|
|
|
119 |
yield mailpit_message::create_from_api_response($this, $messagedata);
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
}
|