1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
<?php
/**
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
*
* Mail
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Mail\Tests\Integration\Framework;
use Horde_Imap_Client;
use Horde_Imap_Client_Data_Fetch;
use Horde_Imap_Client_Fetch_Query;
use Horde_Imap_Client_Ids;
use Horde_Imap_Client_Socket;
use Horde_Mail_Rfc822_Address;
use Horde_Mime_Mail;
use Horde_Mime_Part;
use OCA\Mail\Account;
use OCA\Mail\Db\MailAccount;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCP\AppFramework\QueryException;
trait ImapTest {
/** @var Horde_Imap_Client_Socket */
private $client;
/** @var array<string> */
private $defaultMailboxes = [
'INBOX',
'Junk',
'Sent',
'Trash',
];
/**
* @return Horde_Imap_Client_Socket
*/
private function getTestClient() {
if ($this->client === null) {
$this->client = new Horde_Imap_Client_Socket([
'username' => 'user@domain.tld',
'password' => 'mypassword',
'hostspec' => 'localhost',
'port' => 993,
'secure' => 'ssl',
]);
}
return $this->client;
}
/**
* Reset the testing account to empty default mailboxes and delete any
* other mailboxes that have been created
*/
public function resetImapAccount() {
$client = $this->getTestClient();
$mailboxes = $this->listMailboxes($client);
foreach ($mailboxes as $mailbox) {
if (in_array($mailbox, $this->defaultMailboxes)) {
$this->emptyMailbox($client, $mailbox);
} else {
$this->deleteMailbox($client, $mailbox);
}
}
}
/**
* @return array<string>
*/
public function getMailboxes() {
$client = $this->getTestClient();
return $this->listMailboxes($client);
}
/**
* @param Horde_Imap_Client_Socket $client
* @return array<string>
*/
private function listMailboxes(Horde_Imap_Client_Socket $client) {
return array_map(function ($mailbox) {
return $mailbox['mailbox'];
}, $client->listMailboxes('*'));
}
/**
* @param string $mailbox
*/
public function createImapMailbox($mailbox) {
$client = $this->getTestClient();
$client->createMailbox($mailbox);
}
/**
* @return MessageBuilder
*/
public function getMessageBuilder() {
return MessageBuilder::create();
}
/**
* @param string $mailbox
* @param SimpleMessage $message
* @param MailAccount|null $account
*
* @return int id of the new message
*/
public function saveMessage(string $mailbox, SimpleMessage $message, MailAccount $account = null) {
$client = $this->getClient($account);
$headers = [
'From' => new Horde_Mail_Rfc822_Address($message->getFrom()),
'To' => new Horde_Mail_Rfc822_Address($message->getTo()),
'Cc' => new Horde_Mail_Rfc822_Address($message->getCc()),
'Bcc' => new Horde_Mail_Rfc822_Address($message->getBcc()),
'Subject' => $message->getSubject(),
];
$mail = new Horde_Mime_Mail();
$mail->addHeaders($headers);
$body = new Horde_Mime_Part();
$body->setType('text/plain');
$body->setContents($message->getBody());
$mail->setBasePart($body);
$raw = $mail->getRaw();
$data = stream_get_contents($raw);
return $client->append($mailbox, [
[
'data' => $data,
]
])->ids[0];
}
public function flagMessage($mailbox, $id, MailAccount $account = null) {
$client = $this->getClient($account);
$client->store($mailbox, [
'ids' => new Horde_Imap_Client_Ids([$id]),
'add' => [
Horde_Imap_Client::FLAG_FLAGGED,
],
]);
}
public function deleteMessage($mailbox, $id, MailAccount $account = null) {
$client = $this->getClient($account);
$ids = new Horde_Imap_Client_Ids([$id]);
$client->expunge($mailbox, [
'ids' => $ids,
'delete' => true,
]);
}
/**
* @param Horde_Imap_Client_Socket $client
* @param string $mailbox
*/
private function emptyMailbox(Horde_Imap_Client_Socket $client, $mailbox) {
$query = new Horde_Imap_Client_Fetch_Query();
$query->uid();
$ids = new Horde_Imap_Client_Ids($client->fetch($mailbox, $query)->ids());
$client->expunge($mailbox, [
'ids' => $ids,
'delete' => true,
]);
}
/**
* @param Horde_Imap_Client_Socket $client
* @param string $mailbox
*/
private function deleteMailbox(Horde_Imap_Client_Socket $client, $mailbox) {
$client->deleteMailbox($mailbox);
}
/**
* Assert that a mailbox has a certain number of messages in it
*
* @param int $number
* @param string $mailbox
*/
public function assertMessageCount(int $number, string $mailbox) {
$client = $this->getTestClient();
$query = new Horde_Imap_Client_Fetch_Query();
$query->uid();
$this->assertSame($number, $client->fetch($mailbox, $query)->count(), "wrong number of messages in mailbox <$mailbox>");
}
/**
* Assert that the test account has a certain mailbox
*
* @param string $mailbox
*/
public function assertMailboxExists($mailbox) {
$mailboxes = $this->getMailboxes();
$this->assertArrayHasKey($mailbox, $mailboxes);
}
public function assertMessageContent($mailbox, $uid, $content) {
$client = $this->getTestClient();
$query = new Horde_Imap_Client_Fetch_Query();
$query->bodyText();
$result = $client->fetch($mailbox, $query, [
'ids' => new Horde_Imap_Client_Ids([$uid]),
]);
$messages = iterator_to_array($result);
$this->assertCount(1, $messages);
/* @var $message Horde_Imap_Client_Data_Fetch */
$message = reset($messages);
$actualContent = $message->getBodyText();
$this->assertSame($content, $actualContent, 'message content does not match');
}
/**
* @param MailAccount|null $account
*
* @return Horde_Imap_Client_Socket
* @throws QueryException
*/
protected function getClient(?MailAccount $account): Horde_Imap_Client_Socket {
if ($account !== null) {
/** @var IMAPClientFactory $clientFactory */
$clientFactory = \OC::$server->query(IMAPClientFactory::class);
$client = $clientFactory->getClient(new Account($account));
} else {
$client = $this->getTestClient();
}
return $client;
}
}
|