1111
1212use ChristophWurst \Nextcloud \Testing \TestCase ;
1313use OCA \Mail \Account ;
14+ use OCA \Mail \Contracts \IMessageConnector ;
1415use OCA \Mail \Db \MailAccount ;
1516use OCA \Mail \Db \Mailbox ;
1617use OCA \Mail \Db \MailboxMapper ;
1718use OCA \Mail \Db \Message ;
1819use OCA \Mail \Events \DraftSavedEvent ;
19- use OCA \Mail \IMAP \ MessageMapper ;
20+ use OCA \Mail \Events \ MessageDeletedEvent ;
2021use OCA \Mail \Listener \DeleteDraftListener ;
2122use OCA \Mail \Model \NewMessageData ;
2223use OCA \Mail \Protocol \ProtocolFactory ;
@@ -34,9 +35,6 @@ class DeleteDraftListenerTest extends TestCase {
3435 /** @var MailboxMapper|MockObject */
3536 private $ mailboxMapper ;
3637
37- /** @var MessageMapper|MockObject */
38- private $ messageMapper ;
39-
4038 /** @var LoggerInterface|MockObject */
4139 private $ logger ;
4240
@@ -51,14 +49,12 @@ protected function setUp(): void {
5149
5250 $ this ->protocolFactory = $ this ->createMock (ProtocolFactory::class);
5351 $ this ->mailboxMapper = $ this ->createMock (MailboxMapper::class);
54- $ this ->messageMapper = $ this ->createMock (MessageMapper::class);
5552 $ this ->logger = $ this ->createMock (LoggerInterface::class);
5653 $ this ->eventDispatcher = $ this ->createMock (IEventDispatcher::class);
5754
5855 $ this ->listener = new DeleteDraftListener (
5956 $ this ->protocolFactory ,
6057 $ this ->mailboxMapper ,
61- $ this ->messageMapper ,
6258 $ this ->logger ,
6359 $ this ->eventDispatcher
6460 );
@@ -82,8 +78,8 @@ public function testHandleDraftSavedEventNoUid(): void {
8278 $ newMessageData ,
8379 null
8480 );
85- $ this ->messageMapper ->expects ($ this ->never ())
86- ->method ('addFlag ' );
81+ $ this ->protocolFactory ->expects ($ this ->never ())
82+ ->method ('messageConnector ' );
8783 $ this ->logger ->expects ($ this ->never ())
8884 ->method ('error ' );
8985 $ this ->eventDispatcher ->expects ($ this ->never ())
@@ -107,16 +103,10 @@ public function testHandleDraftSavedEventNoDraftMailboxSet(): void {
107103 $ newMessageData ,
108104 $ draft
109105 );
110- /** @var \Horde_Imap_Client_Socket|MockObject $client */
111- $ client = $ this ->createStub (\Horde_Imap_Client_Socket::class);
112- $ this ->protocolFactory
113- ->method ('imapClient ' )
114- ->with ($ account )
115- ->willReturn ($ client );
116- $ mailbox = new Mailbox ();
117- $ mailbox ->setName ('Drafts ' );
118106 $ this ->mailboxMapper ->expects ($ this ->never ())
119107 ->method ('findById ' );
108+ $ this ->protocolFactory ->expects ($ this ->never ())
109+ ->method ('messageConnector ' );
120110 $ this ->logger ->expects ($ this ->once ())->method ('warning ' );
121111
122112 $ this ->listener ->handle ($ event );
@@ -138,23 +128,62 @@ public function testHandleDraftSavedEventDraftMailboxNotFound(): void {
138128 $ newMessageData ,
139129 $ draft
140130 );
141- /** @var \Horde_Imap_Client_Socket|MockObject $client */
142- $ client = $ this ->createStub (\Horde_Imap_Client_Socket::class);
143- $ this ->protocolFactory
144- ->method ('imapClient ' )
145- ->with ($ account )
146- ->willReturn ($ client );
147- $ mailbox = new Mailbox ();
148- $ mailbox ->setName ('Drafts ' );
149131 $ this ->mailboxMapper ->expects ($ this ->once ())
150132 ->method ('findById ' )
151133 ->with (123 )
152134 ->willThrowException (new DoesNotExistException ('' ));
135+ $ this ->protocolFactory ->expects ($ this ->never ())
136+ ->method ('messageConnector ' );
153137 $ this ->logger ->expects ($ this ->once ())->method ('warning ' );
154138
155139 $ this ->listener ->handle ($ event );
156140 }
157141
142+ public function testHandleDraftSavedEventDeletesDraftViaMessageConnector (): void {
143+ /** @var Account|MockObject $account */
144+ $ account = $ this ->createMock (Account::class);
145+ $ mailAccount = new MailAccount ();
146+ $ mailAccount ->setDraftsMailboxId (123 );
147+ $ account ->method ('getMailAccount ' )->willReturn ($ mailAccount );
148+ /** @var NewMessageData|MockObject $newMessageData */
149+ $ newMessageData = $ this ->createStub (NewMessageData::class);
150+ $ draft = new Message ();
151+ $ uid = 123 ;
152+ $ draft ->setUid ($ uid );
153+ $ event = new DraftSavedEvent (
154+ $ account ,
155+ $ newMessageData ,
156+ $ draft
157+ );
158+ $ mailbox = new Mailbox ();
159+ $ mailbox ->setName ('Drafts ' );
160+ $ this ->mailboxMapper ->expects ($ this ->once ())
161+ ->method ('findById ' )
162+ ->with (123 )
163+ ->willReturn ($ mailbox );
164+
165+ /** @var IMessageConnector|MockObject $messageConnector */
166+ $ messageConnector = $ this ->createMock (IMessageConnector::class);
167+ $ this ->protocolFactory ->expects ($ this ->once ())
168+ ->method ('messageConnector ' )
169+ ->with ($ account )
170+ ->willReturn ($ messageConnector );
171+ $ messageConnector ->expects ($ this ->once ())
172+ ->method ('deleteMessages ' )
173+ ->with ($ account , $ mailbox , $ draft )
174+ ->willReturn ([$ draft ]);
175+
176+ $ this ->eventDispatcher ->expects ($ this ->once ())
177+ ->method ('dispatchTyped ' )
178+ ->with ($ this ->callback (static function (MessageDeletedEvent $ deletedEvent ) use ($ account , $ mailbox , $ uid ): bool {
179+ return $ deletedEvent ->getAccount () === $ account
180+ && $ deletedEvent ->getMailbox () === $ mailbox
181+ && $ deletedEvent ->getMessageId () === $ uid ;
182+ }));
183+
184+ $ this ->listener ->handle ($ event );
185+ }
186+
158187 public function testHandleMessageSentEventNoUid (): void {
159188 /** @var Account|MockObject $account */
160189 $ account = $ this ->createStub (Account::class);
@@ -165,8 +194,8 @@ public function testHandleMessageSentEventNoUid(): void {
165194 $ newMessageData ,
166195 null
167196 );
168- $ this ->messageMapper ->expects ($ this ->never ())
169- ->method ('addFlag ' );
197+ $ this ->protocolFactory ->expects ($ this ->never ())
198+ ->method ('messageConnector ' );
170199 $ this ->logger ->expects ($ this ->never ())
171200 ->method ('error ' );
172201 $ this ->eventDispatcher ->expects ($ this ->never ())
0 commit comments