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
|
<?php
declare(strict_types=1);
/**
* @author Anna Larch <anna.larch@nextcloud.com>
*
* 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\IMAP;
use ChristophWurst\Nextcloud\Testing\TestCase;
use Horde_Imap_Client;
use Horde_Imap_Client_Exception;
use OC;
use OCA\Mail\Account;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\IMAP\MessageMapper as ImapMessageMapper;
use OCA\Mail\Db\MessageMapper;
use OCA\Mail\Service\Sync\SyncService;
use OCA\Mail\Tests\Integration\Framework\ImapTest;
use OCA\Mail\Tests\Integration\Framework\ImapTestAccount;
class MessageMapperTest extends TestCase {
use ImapTest,
ImapTestAccount;
public function setUp():void {
parent::setUp();
}
public function tearDown(): void {
$this->resetImapAccount();
}
public function testTagging(): void {
// First, set up account and retrieve sync token
$this->resetImapAccount();
$account = $this->createTestAccount();
/** @var SyncService $syncService */
$syncService = OC::$server->get(SyncService::class);
/** @var ImapMessageMapper $messageMapper */
$imapMessageMapper = OC::$server->get(ImapMessageMapper::class);
/** @var MessageMapper $messageMapper */
$messageMapper = OC::$server->get(MessageMapper::class);
/** @var IMailManager $mailManager */
$mailManager = OC::$server->get(IMailManager::class);
$mailBoxes = $mailManager->getMailboxes(new Account($account));
$inbox = null;
foreach ($mailBoxes as $mailBox) {
if ($mailBox->getName() === 'INBOX') {
$inbox = $mailBox;
break;
}
}
// Second, put a new message into the mailbox
$message = $this->getMessageBuilder()
->from('buffington@domain.tld')
->to('user@domain.tld')
->finish();
$newUid = $this->saveMessage($inbox->getName(), $message, $account);
// now we tag this message!
try {
$imapMessageMapper->addFlag($this->getClient($account), $mailBox, [$newUid], '$label1');
} catch (Horde_Imap_Client_Exception $e) {
self::fail('Could not tag message');
}
// sync
$syncService->syncMailbox(
new Account($account),
$inbox,
Horde_Imap_Client::SYNC_NEWMSGSUIDS | Horde_Imap_Client::SYNC_FLAGSUIDS | Horde_Imap_Client::SYNC_VANISHEDUIDS,
null,
false
);
// Let's retrieve the DB to see if we have this tag!
$messages = $messageMapper->findByUids($mailBox, [$newUid]);
$related = $messageMapper->findRelatedData($messages, $account->getUserId());
foreach ($related as $message) {
$tags = $message->getTags();
self::assertCount(1, $tags);
self::assertEquals('$label1', $tags[0]->getImapLabel());
}
// now we untag this message!
try {
$imapMessageMapper->removeFlag($this->getClient($account), $mailBox, [$newUid], '$label1');
} catch (Horde_Imap_Client_Exception $e) {
self::fail('Could not untag message');
}
// sync again
$syncService->syncMailbox(
new Account($account),
$inbox,
Horde_Imap_Client::SYNC_NEWMSGSUIDS | Horde_Imap_Client::SYNC_FLAGSUIDS | Horde_Imap_Client::SYNC_VANISHEDUIDS,
null,
true
);
$messages = $messageMapper->findByUids($mailBox, [$newUid]);
$related = $messageMapper->findRelatedData($messages, $account->getUserId());
foreach ($related as $message) {
$tags = $message->getTags();
self::assertEmpty($tags);
}
}
public function testGetFlagged(): void {
// First, set up account and retrieve sync token
$this->resetImapAccount();
$account = $this->createTestAccount();
/** @var ImapMessageMapper $messageMapper */
$imapMessageMapper = OC::$server->get(ImapMessageMapper::class);
/** @var IMailManager $mailManager */
$mailManager = OC::$server->get(IMailManager::class);
$mailBoxes = $mailManager->getMailboxes(new Account($account));
$inbox = null;
foreach ($mailBoxes as $mailBox) {
if ($mailBox->getName() === 'INBOX') {
$inbox = $mailBox;
break;
}
}
// Put a second new message into the mailbox
$message = $this->getMessageBuilder()
->from('buffington@domain.tld')
->to('user@domain.tld')
->finish();
$newUid = $this->saveMessage($inbox->getName(), $message, $account);
// Put another new message into the mailbox
$message = $this->getMessageBuilder()
->from('fluffington@domain.tld')
->to('user@domain.tld')
->finish();
$newUid2 = $this->saveMessage($inbox->getName(), $message, $account);
// Thirdly, create a message that will not be tagged
$message = $this->getMessageBuilder()
->from('scruffington@domain.tld')
->to('user@domain.tld')
->finish();
$this->saveMessage($inbox->getName(), $message, $account);
// now we tag this message with $label1
try {
$imapMessageMapper->addFlag($this->getClient($account), $mailBox, [$newUid], '$label1');
} catch (Horde_Imap_Client_Exception $e) {
self::fail('Could not tag message');
}
// now we tag this and the previous message with $label2
try {
$imapMessageMapper->addFlag($this->getClient($account), $mailBox, [$newUid, $newUid2], '$label2');
} catch (Horde_Imap_Client_Exception $e) {
self::fail('Could not tag message');
}
// test for labels
$tagged = $imapMessageMapper->getFlagged($this->getClient($account), $mailBox, '$label1');
self::assertNotEmpty($tagged);
// are the counts correct?
self::assertCount(1, $tagged);
$tagged = $imapMessageMapper->getFlagged($this->getClient($account), $mailBox, '$label2');
self::assertNotEmpty($tagged);
self::assertCount(2, $tagged);
// test for labels that wasn't set
$tagged = $imapMessageMapper->getFlagged($this->getClient($account), $mailBox, '$notAvailable');
self::assertEmpty($tagged);
// test for regular flag - recent
$tagged = $imapMessageMapper->getFlagged($this->getClient($account), $mailBox, Horde_Imap_Client::FLAG_RECENT);
self::assertNotEmpty($tagged);
// should return all messages
self::assertCount(3, $tagged);
}
}
|