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
|
<?php
declare(strict_types=1);
/**
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OCA\Mail\Tests\Unit\Listener;
use ChristophWurst\Nextcloud\Testing\TestCase;
use OCA\Mail\Account;
use OCA\Mail\Db\Mailbox;
use OCA\Mail\Db\MailboxMapper;
use OCA\Mail\Db\Message;
use OCA\Mail\Events\DraftSavedEvent;
use OCA\Mail\Events\MessageSentEvent;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCA\Mail\IMAP\MailboxSync;
use OCA\Mail\IMAP\MessageMapper;
use OCA\Mail\Listener\DeleteDraftListener;
use OCA\Mail\Model\IMessage;
use OCA\Mail\Model\NewMessageData;
use OCA\Mail\Model\RepliedMessageData;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
class DeleteDraftListenerTest extends TestCase {
/** @var IMAPClientFactory|MockObject */
private $imapClientFactory;
/** @var MailboxMapper|MockObject */
private $mailboxMapper;
/** @var MessageMapper|MockObject */
private $messageMapper;
/** @var MailboxSync|MockObject */
private $mailboxSync;
/** @var LoggerInterface|MockObject */
private $logger;
/** @var IEventListener */
private $listener;
protected function setUp(): void {
parent::setUp();
$this->imapClientFactory = $this->createMock(IMAPClientFactory::class);
$this->mailboxMapper = $this->createMock(MailboxMapper::class);
$this->messageMapper = $this->createMock(MessageMapper::class);
$this->mailboxSync = $this->createMock(MailboxSync::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->listener = new DeleteDraftListener(
$this->imapClientFactory,
$this->mailboxMapper,
$this->messageMapper,
$this->mailboxSync,
$this->logger
);
}
public function testHandleUnrelated(): void {
$event = new Event();
$this->listener->handle($event);
$this->addToAssertionCount(1);
}
public function testHandleDraftSavedEventNoUid(): void {
/** @var Account|MockObject $account */
$account = $this->createMock(Account::class);
/** @var NewMessageData|MockObject $newMessageData */
$newMessageData = $this->createMock(NewMessageData::class);
$event = new DraftSavedEvent(
$account,
$newMessageData,
null
);
$this->messageMapper->expects($this->never())
->method('addFlag');
$this->logger->expects($this->never())
->method('error');
$this->listener->handle($event);
}
public function testHandleDraftSavedEventCreatesDraftsMailbox(): void {
/** @var Account|MockObject $account */
$account = $this->createMock(Account::class);
/** @var NewMessageData|MockObject $newMessageData */
$newMessageData = $this->createMock(NewMessageData::class);
$draft = new Message();
$uid = 123;
$draft->setUid($uid);
$event = new DraftSavedEvent(
$account,
$newMessageData,
$draft
);
/** @var \Horde_Imap_Client_Socket|MockObject $client */
$client = $this->createMock(\Horde_Imap_Client_Socket::class);
$this->imapClientFactory
->method('getClient')
->with($account)
->willReturn($client);
$mailbox = new Mailbox();
$mailbox->setName('Drafts');
$this->mailboxMapper->expects($this->exactly(2))
->method('findSpecial')
->with($account, 'drafts')
->willReturnOnConsecutiveCalls(
$this->throwException(new DoesNotExistException('')),
$mailbox
);
$client->expects($this->once())
->method('createMailbox')
->with(
'Drafts',
[
'special_use' => [
\Horde_Imap_Client::SPECIALUSE_DRAFTS,
],
]
);
$this->mailboxSync->expects($this->once())
->method('sync')
->with($account, $this->logger, true);
$this->messageMapper->expects($this->once())
->method('addFlag')
->with(
$client,
$mailbox,
$uid,
\Horde_Imap_Client::FLAG_DELETED
);
$client->expects($this->once())
->method('expunge')
->with('Drafts');
$this->logger->expects($this->never())
->method('error');
$this->listener->handle($event);
}
public function testHandleMessageSentEventNoUid(): void {
/** @var Account|MockObject $account */
$account = $this->createMock(Account::class);
/** @var NewMessageData|MockObject $newMessageData */
$newMessageData = $this->createMock(NewMessageData::class);
$event = new DraftSavedEvent(
$account,
$newMessageData,
null
);
$this->messageMapper->expects($this->never())
->method('addFlag');
$this->logger->expects($this->never())
->method('error');
$this->listener->handle($event);
}
public function testHandleMessageSentEvent(): void {
/** @var Account|MockObject $account */
$account = $this->createMock(Account::class);
/** @var NewMessageData|MockObject $newMessageData */
$newMessageData = $this->createMock(NewMessageData::class);
/** @var RepliedMessageData|MockObject $repliedMessageData */
$repliedMessageData = $this->createMock(RepliedMessageData::class);
/** @var IMessage|MockObject $message */
$message = $this->createMock(IMessage::class);
/** @var \Horde_Mime_Mail|MockObject $mail */
$mail = $this->createMock(\Horde_Mime_Mail::class);
$draft = new Message();
$uid = 123;
$draft->setUid($uid);
$event = new MessageSentEvent(
$account,
$newMessageData,
$repliedMessageData,
$draft,
$message,
$mail
);
/** @var \Horde_Imap_Client_Socket|MockObject $client */
$client = $this->createMock(\Horde_Imap_Client_Socket::class);
$this->imapClientFactory
->method('getClient')
->with($account)
->willReturn($client);
$mailbox = new Mailbox();
$mailbox->setName('Drafts');
$this->mailboxMapper->expects($this->once())
->method('findSpecial')
->with($account, 'drafts')
->willReturn($mailbox);
$this->messageMapper->expects($this->once())
->method('addFlag')
->with(
$client,
$mailbox,
$uid,
\Horde_Imap_Client::FLAG_DELETED
);
$client->expects($this->once())
->method('expunge')
->with('Drafts');
$this->logger->expects($this->never())
->method('error');
$this->listener->handle($event);
}
}
|