|
| 1 | +<?php |
| 2 | + |
| 3 | +use DirectoryTree\ImapEngine\Connection\ImapConnection; |
| 4 | +use DirectoryTree\ImapEngine\Enums\ImapFlag; |
| 5 | +use DirectoryTree\ImapEngine\Exceptions\ImapCapabilityException; |
| 6 | +use DirectoryTree\ImapEngine\Folder; |
| 7 | +use DirectoryTree\ImapEngine\Mailbox; |
| 8 | +use DirectoryTree\ImapEngine\Message; |
| 9 | + |
| 10 | +test('it moves message using MOVE when capable', function () { |
| 11 | + $mailbox = Mailbox::make([ |
| 12 | + 'username' => 'foo', |
| 13 | + 'password' => 'bar', |
| 14 | + ]); |
| 15 | + |
| 16 | + $mailbox->connect(ImapConnection::fake([ |
| 17 | + '* OK Welcome to IMAP', |
| 18 | + 'TAG1 OK Logged in', |
| 19 | + '* CAPABILITY IMAP4rev1 STARTTLS MOVE AUTH=PLAIN', |
| 20 | + 'TAG2 OK CAPABILITY completed', |
| 21 | + 'TAG3 OK MOVE completed', |
| 22 | + ])); |
| 23 | + |
| 24 | + $folder = new Folder($mailbox, 'INBOX', [], '/'); |
| 25 | + |
| 26 | + $message = new Message($folder, 1, [], 'header', 'body'); |
| 27 | + |
| 28 | + $message->move('INBOX.Sent'); |
| 29 | +})->throwsNoExceptions(); |
| 30 | + |
| 31 | +test('it copies and then deletes message using UIDPLUS when incapable of MOVE', function () { |
| 32 | + $mailbox = Mailbox::make([ |
| 33 | + 'username' => 'foo', |
| 34 | + 'password' => 'bar', |
| 35 | + ]); |
| 36 | + |
| 37 | + $mailbox->connect(ImapConnection::fake([ |
| 38 | + '* OK Welcome to IMAP', |
| 39 | + 'TAG1 OK Logged in', |
| 40 | + '* CAPABILITY IMAP4rev1 STARTTLS UIDPLUS AUTH=PLAIN', |
| 41 | + 'TAG2 OK CAPABILITY completed', |
| 42 | + 'TAG3 OK UID MOVE completed', |
| 43 | + 'TAG4 OK COPY completed', |
| 44 | + ])); |
| 45 | + |
| 46 | + $folder = new Folder($mailbox, 'INBOX', [], '/'); |
| 47 | + |
| 48 | + $message = new Message($folder, 1, [], 'header', 'body'); |
| 49 | + |
| 50 | + $message->move('INBOX.Sent'); |
| 51 | +})->throwsNoExceptions(); |
| 52 | + |
| 53 | +test('it throws exception when server does not support MOVE or UIDPLUS capabilities', function () { |
| 54 | + $mailbox = Mailbox::make([ |
| 55 | + 'username' => 'foo', |
| 56 | + 'password' => 'bar', |
| 57 | + ]); |
| 58 | + |
| 59 | + $mailbox->connect(ImapConnection::fake([ |
| 60 | + '* OK Welcome to IMAP', |
| 61 | + 'TAG1 OK Logged in', |
| 62 | + '* CAPABILITY IMAP4rev1 STARTTLS AUTH=PLAIN', |
| 63 | + 'TAG2 OK CAPABILITY completed', |
| 64 | + ])); |
| 65 | + |
| 66 | + $folder = new Folder($mailbox, 'INBOX', [], '/'); |
| 67 | + |
| 68 | + $message = new Message($folder, 1, [], 'header', 'body'); |
| 69 | + |
| 70 | + $message->move('INBOX.Sent'); |
| 71 | +})->throws(ImapCapabilityException::class); |
| 72 | + |
| 73 | +test('it can mark and unmark a message as flagged', function () { |
| 74 | + $mailbox = Mailbox::make([ |
| 75 | + 'username' => 'foo', |
| 76 | + 'password' => 'bar', |
| 77 | + ]); |
| 78 | + |
| 79 | + $mailbox->connect(ImapConnection::fake([ |
| 80 | + '* OK Welcome to IMAP', |
| 81 | + 'TAG1 OK Logged in', |
| 82 | + '* CAPABILITY IMAP4rev1 STARTTLS AUTH=PLAIN', |
| 83 | + 'TAG2 OK CAPABILITY completed', |
| 84 | + 'TAG3 OK STORE completed', |
| 85 | + ])); |
| 86 | + |
| 87 | + $folder = new Folder($mailbox, 'INBOX', [], '/'); |
| 88 | + |
| 89 | + $message = new Message($folder, 1, [], 'header', 'body'); |
| 90 | + |
| 91 | + expect($message->isFlagged())->toBeFalse(); |
| 92 | + expect($message->flags())->not->toContain('\\Flagged'); |
| 93 | + |
| 94 | + $message->markFlagged(); |
| 95 | + |
| 96 | + expect($message->isFlagged())->toBeTrue(); |
| 97 | + expect($message->flags())->toContain('\\Flagged'); |
| 98 | + expect($message->hasFlag(ImapFlag::Flagged))->toBeTrue(); |
| 99 | + |
| 100 | + $message->unmarkFlagged(); |
| 101 | + |
| 102 | + expect($message->isFlagged())->toBeFalse(); |
| 103 | + expect($message->flags())->not->toContain('\\Flagged'); |
| 104 | + expect($message->hasFlag(ImapFlag::Flagged))->toBeFalse(); |
| 105 | +}); |
0 commit comments