Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/tests/live/FolderTest.php

raw symbols SCIP
1 <?php
2 /*
3 * File: FolderTest.php
4 * Category: -
5 * Author: M.Goldenbaum
6 * Created: 04.03.23 03:52
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13 namespace Tests\live;
14
15 use Webklex\PHPIMAP\Client;
16 use Webklex\PHPIMAP\Exceptions\AuthFailedException;
17 use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
18 use Webklex\PHPIMAP\Exceptions\EventNotFoundException;
19 use Webklex\PHPIMAP\Exceptions\FolderFetchingException;
20 use Webklex\PHPIMAP\Exceptions\ImapBadRequestException;
21 use Webklex\PHPIMAP\Exceptions\ImapServerErrorException;
22 use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException;
23 use Webklex\PHPIMAP\Exceptions\MaskNotFoundException;
24 use Webklex\PHPIMAP\Exceptions\MessageContentFetchingException;
25 use Webklex\PHPIMAP\Exceptions\MessageFlagException;
26 use Webklex\PHPIMAP\Exceptions\MessageHeaderFetchingException;
27 use Webklex\PHPIMAP\Exceptions\MessageNotFoundException;
28 use Webklex\PHPIMAP\Exceptions\ResponseException;
29 use Webklex\PHPIMAP\Exceptions\RuntimeException;
30 use Webklex\PHPIMAP\Folder;
31 use Webklex\PHPIMAP\Message;
32 use Webklex\PHPIMAP\Query\WhereQuery;
33 use Webklex\PHPIMAP\Support\FolderCollection;
34
35 /**
36 * Class FolderTest
37 *
38 * @package Tests
39 */
40 class FolderTest extends LiveMailboxTestCase {
41
42 /**
43 * Try to create a new query instance
44 *
45 * @return void
46 * @throws AuthFailedException
47 * @throws ConnectionFailedException
48 * @throws FolderFetchingException
49 * @throws ImapBadRequestException
50 * @throws ImapServerErrorException
51 * @throws MaskNotFoundException
52 * @throws ResponseException
53 * @throws RuntimeException
54 */
55 public function testQuery(): void {
56 $folder = $this->getFolder('INBOX');
57 self::assertInstanceOf(Folder::class, $folder);
58
59 self::assertInstanceOf(WhereQuery::class, $folder->query());
60 self::assertInstanceOf(WhereQuery::class, $folder->search());
61 self::assertInstanceOf(WhereQuery::class, $folder->messages());
62 }
63
64 /**
65 * Test Folder::hasChildren()
66 *
67 * @return void
68 * @throws AuthFailedException
69 * @throws ConnectionFailedException
70 * @throws FolderFetchingException
71 * @throws ImapBadRequestException
72 * @throws ImapServerErrorException
73 * @throws MaskNotFoundException
74 * @throws ResponseException
75 * @throws RuntimeException
76 * @throws EventNotFoundException
77 */
78 public function testHasChildren(): void {
79 $folder = $this->getFolder('INBOX');
80 self::assertInstanceOf(Folder::class, $folder);
81
82 $delimiter = $this->getManager()->getConfig()->get("options.delimiter");
83 $child_path = implode($delimiter, ['INBOX', 'test']);
84 if ($folder->getClient()->getFolder($child_path) === null) {
85 $folder->getClient()->createFolder($child_path, false);
86 $folder = $this->getFolder('INBOX');
87 }
88
89 self::assertTrue($folder->hasChildren());
90 }
91
92 /**
93 * Test Folder::setChildren()
94 *
95 * @return void
96 * @throws AuthFailedException
97 * @throws ConnectionFailedException
98 * @throws EventNotFoundException
99 * @throws FolderFetchingException
100 * @throws ImapBadRequestException
101 * @throws ImapServerErrorException
102 * @throws MaskNotFoundException
103 * @throws ResponseException
104 * @throws RuntimeException
105 */
106 public function testSetChildren(): void {
107 $folder = $this->getFolder('INBOX');
108 self::assertInstanceOf(Folder::class, $folder);
109
110 $delimiter = $this->getManager()->getConfig()->get("options.delimiter");
111 $child_path = implode($delimiter, ['INBOX', 'test']);
112 if ($folder->getClient()->getFolder($child_path) === null) {
113 $folder->getClient()->createFolder($child_path, false);
114 $folder = $this->getFolder('INBOX');
115 }
116 self::assertTrue($folder->hasChildren());
117
118 $folder->setChildren(new FolderCollection());
119 self::assertTrue($folder->getChildren()->isEmpty());
120 }
121
122 /**
123 * Test Folder::getChildren()
124 *
125 * @return void
126 * @throws AuthFailedException
127 * @throws ConnectionFailedException
128 * @throws EventNotFoundException
129 * @throws FolderFetchingException
130 * @throws ImapBadRequestException
131 * @throws ImapServerErrorException
132 * @throws MaskNotFoundException
133 * @throws ResponseException
134 * @throws RuntimeException
135 */
136 public function testGetChildren(): void {
137 $folder = $this->getFolder('INBOX');
138 self::assertInstanceOf(Folder::class, $folder);
139
140 $delimiter = $this->getManager()->getConfig()->get("options.delimiter");
141 $child_path = implode($delimiter, ['INBOX', 'test']);
142 if ($folder->getClient()->getFolder($child_path) === null) {
143 $folder->getClient()->createFolder($child_path, false);
144 }
145
146 $folder = $folder->getClient()->getFolders()->where('name', 'INBOX')->first();
147 self::assertInstanceOf(Folder::class, $folder);
148
149 self::assertTrue($folder->hasChildren());
150 self::assertFalse($folder->getChildren()->isEmpty());
151 }
152
153 /**
154 * Test Folder::move()
155 *
156 * @return void
157 * @throws AuthFailedException
158 * @throws ConnectionFailedException
159 * @throws EventNotFoundException
160 * @throws FolderFetchingException
161 * @throws ImapBadRequestException
162 * @throws ImapServerErrorException
163 * @throws MaskNotFoundException
164 * @throws ResponseException
165 * @throws RuntimeException
166 */
167 public function testMove(): void {
168 $client = $this->getClient();
169
170 $delimiter = $this->getManager()->getConfig()->get("options.delimiter");
171 $folder_path = implode($delimiter, ['INBOX', 'test']);
172
173 $folder = $client->getFolder($folder_path);
174 if ($folder === null) {
175 $folder = $client->createFolder($folder_path, false);
176 }
177 $new_folder_path = implode($delimiter, ['INBOX', 'other']);
178 $new_folder = $client->getFolder($new_folder_path);
179 $new_folder?->delete(false);
180
181 $status = $folder->move($new_folder_path, false);
182 self::assertIsArray($status);
183 self::assertTrue(str_starts_with($status[0], 'OK'));
184
185 $new_folder = $client->getFolder($new_folder_path);
186 self::assertEquals($new_folder_path, $new_folder->path);
187 self::assertEquals('other', $new_folder->name);
188
189 if ($this->deleteFolder($new_folder) === false) {
190 $this->fail("Could not delete folder: " . $new_folder->path);
191 }
192 }
193
194 /**
195 * Test Folder::delete()
196 *
197 * @return void
198 * @throws AuthFailedException
199 * @throws ConnectionFailedException
200 * @throws EventNotFoundException
201 * @throws FolderFetchingException
202 * @throws ImapBadRequestException
203 * @throws ImapServerErrorException
204 * @throws MaskNotFoundException
205 * @throws ResponseException
206 * @throws RuntimeException
207 */
208 public function testDelete(): void {
209 $client = $this->getClient();
210
211 $delimiter = $this->getManager()->getConfig()->get("options.delimiter");
212 $folder_path = implode($delimiter, ['INBOX', 'test']);
213
214 $folder = $client->getFolder($folder_path);
215 if ($folder === null) {
216 $folder = $client->createFolder($folder_path, false);
217 }
218 self::assertInstanceOf(Folder::class, $folder);
219
220 if ($this->deleteFolder($folder) === false) {
221 $this->fail("Could not delete folder: " . $folder->path);
222 }
223 }
224
225 /**
226 * Test Folder::overview()
227 *
228 * @return void
229 * @throws AuthFailedException
230 * @throws ConnectionFailedException
231 * @throws EventNotFoundException
232 * @throws FolderFetchingException
233 * @throws ImapBadRequestException
234 * @throws ImapServerErrorException
235 * @throws MaskNotFoundException
236 * @throws ResponseException
237 * @throws RuntimeException
238 * @throws InvalidMessageDateException
239 * @throws MessageContentFetchingException
240 * @throws MessageFlagException
241 * @throws MessageHeaderFetchingException
242 * @throws MessageNotFoundException
243 */
244 public function testOverview(): void {
245 $folder = $this->getFolder('INBOX');
246 self::assertInstanceOf(Folder::class, $folder);
247
248 $folder->select();
249
250 // Test empty overview
251 $overview = $folder->overview();
252 self::assertIsArray($overview);
253 self::assertCount(0, $overview);
254
255 $message = $this->appendMessageTemplate($folder, "plain.eml");
256
257 $overview = $folder->overview();
258
259 self::assertIsArray($overview);
260 self::assertCount(1, $overview);
261
262 self::assertEquals($message->from->first()->full, end($overview)["from"]->toString());
263
264 self::assertTrue($message->delete());
265 }
266
267 /**
268 * Test Folder::appendMessage()
269 *
270 * @return void
271 * @throws AuthFailedException
272 * @throws ConnectionFailedException
273 * @throws EventNotFoundException
274 * @throws FolderFetchingException
275 * @throws ImapBadRequestException
276 * @throws ImapServerErrorException
277 * @throws InvalidMessageDateException
278 * @throws MaskNotFoundException
279 * @throws MessageContentFetchingException
280 * @throws MessageFlagException
281 * @throws MessageHeaderFetchingException
282 * @throws MessageNotFoundException
283 * @throws ResponseException
284 * @throws RuntimeException
285 */
286 public function testAppendMessage(): void {
287 $folder = $this->getFolder('INBOX');
288 self::assertInstanceOf(Folder::class, $folder);
289
290 $message = $this->appendMessageTemplate($folder, "plain.eml");
291 self::assertInstanceOf(Message::class, $message);
292
293 self::assertEquals("Example", $message->subject);
294 self::assertEquals("[email protected]", $message->to);
295 self::assertEquals("[email protected]", $message->from);
296
297 // Clean up
298 $this->assertTrue($message->delete(true));
299 }
300
301 /**
302 * Test Folder::subscribe()
303 *
304 * @return void
305 * @throws AuthFailedException
306 * @throws ConnectionFailedException
307 * @throws FolderFetchingException
308 * @throws ImapBadRequestException
309 * @throws ImapServerErrorException
310 * @throws MaskNotFoundException
311 * @throws ResponseException
312 * @throws RuntimeException
313 */
314 public function testSubscribe(): void {
315 $folder = $this->getFolder('INBOX');
316 self::assertInstanceOf(Folder::class, $folder);
317
318 $status = $folder->subscribe();
319 self::assertIsArray($status);
320 self::assertTrue(str_starts_with($status[0], 'OK'));
321
322 // Clean up
323 $folder->unsubscribe();
324 }
325
326 /**
327 * Test Folder::unsubscribe()
328 *
329 * @return void
330 * @throws AuthFailedException
331 * @throws ConnectionFailedException
332 * @throws FolderFetchingException
333 * @throws ImapBadRequestException
334 * @throws ImapServerErrorException
335 * @throws MaskNotFoundException
336 * @throws ResponseException
337 * @throws RuntimeException
338 */
339 public function testUnsubscribe(): void {
340 $folder = $this->getFolder('INBOX');
341 self::assertInstanceOf(Folder::class, $folder);
342
343 $folder->subscribe();
344
345 $status = $folder->subscribe();
346 self::assertIsArray($status);
347 self::assertTrue(str_starts_with($status[0], 'OK'));
348 }
349
350 /**
351 * Test Folder::status()
352 *
353 * @return void
354 * @throws AuthFailedException
355 * @throws ConnectionFailedException
356 * @throws FolderFetchingException
357 * @throws ImapBadRequestException
358 * @throws ImapServerErrorException
359 * @throws MaskNotFoundException
360 * @throws ResponseException
361 * @throws RuntimeException
362 */
363 public function testStatus(): void {
364 $folder = $this->getFolder('INBOX');
365 self::assertInstanceOf(Folder::class, $folder);
366
367 $status = $folder->status();
368 self::assertEquals(0, $status['messages']);
369 self::assertEquals(0, $status['recent']);
370 self::assertEquals(0, $status['unseen']);
371 self::assertGreaterThan(0, $status['uidnext']);
372 self::assertGreaterThan(0, $status['uidvalidity']);
373 }
374
375 /**
376 * Test Folder::examine()
377 *
378 * @return void
379 * @throws AuthFailedException
380 * @throws ConnectionFailedException
381 * @throws FolderFetchingException
382 * @throws ImapBadRequestException
383 * @throws ImapServerErrorException
384 * @throws MaskNotFoundException
385 * @throws ResponseException
386 * @throws RuntimeException
387 */
388 public function testExamine(): void {
389 $folder = $this->getFolder('INBOX');
390 self::assertInstanceOf(Folder::class, $folder);
391
392 $status = $folder->examine();
393 self::assertTrue(isset($status["flags"]) && count($status["flags"]) > 0);
394 self::assertTrue(($status["uidnext"] ?? 0) > 0);
395 self::assertTrue(($status["uidvalidity"] ?? 0) > 0);
396 self::assertTrue(($status["recent"] ?? -1) >= 0);
397 self::assertTrue(($status["exists"] ?? -1) >= 0);
398 }
399
400 /**
401 * Test Folder::getClient()
402 *
403 * @return void
404 * @throws AuthFailedException
405 * @throws ConnectionFailedException
406 * @throws FolderFetchingException
407 * @throws ImapBadRequestException
408 * @throws ImapServerErrorException
409 * @throws MaskNotFoundException
410 * @throws ResponseException
411 * @throws RuntimeException
412 */
413 public function testGetClient(): void {
414 $folder = $this->getFolder('INBOX');
415 self::assertInstanceOf(Folder::class, $folder);
416 self::assertInstanceOf(Client::class, $folder->getClient());
417 }
418
419 /**
420 * Test Folder::setDelimiter()
421 *
422 * @return void
423 * @throws AuthFailedException
424 * @throws ConnectionFailedException
425 * @throws FolderFetchingException
426 * @throws ImapBadRequestException
427 * @throws ImapServerErrorException
428 * @throws MaskNotFoundException
429 * @throws ResponseException
430 * @throws RuntimeException
431 */
432 public function testSetDelimiter(): void {
433 $folder = $this->getFolder('INBOX');
434 self::assertInstanceOf(Folder::class, $folder);
435
436 $folder->setDelimiter("/");
437 self::assertEquals("/", $folder->delimiter);
438
439 $folder->setDelimiter(".");
440 self::assertEquals(".", $folder->delimiter);
441
442 $default_delimiter = $this->getManager()->getConfig()->get("options.delimiter", "/");
443 $folder->setDelimiter(null);
444 self::assertEquals($default_delimiter, $folder->delimiter);
445 }
446
447 }