Skip to content

Commit 4d368a7

Browse files
committed
test: add controller tests
Signed-off-by: Jana Peper <[email protected]>
1 parent 3c18a7a commit 4d368a7

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace Unit\Controller;
11+
12+
use ChristophWurst\Nextcloud\Testing\TestCase;
13+
use OCA\Mail\Account;
14+
use OCA\Mail\Contracts\IMailManager;
15+
use OCA\Mail\Contracts\IMailSearch;
16+
use OCA\Mail\Controller\MailboxesApiController;
17+
use OCA\Mail\Db\MailAccount;
18+
use OCA\Mail\Db\Mailbox;
19+
use OCA\Mail\Db\Message as DbMessage;
20+
use OCA\Mail\Folder;
21+
use OCA\Mail\Service\AccountService;
22+
use OCP\AppFramework\Db\DoesNotExistException;
23+
use OCP\AppFramework\Http;
24+
use OCP\IRequest;
25+
use PHPUnit\Framework\MockObject\MockObject;
26+
27+
class MailboxesApiControllerTest extends TestCase {
28+
private const USER_ID = 'user';
29+
30+
private MailboxesApiController $controller;
31+
32+
private IRequest&MockObject $request;
33+
private IMailManager|MockObject $mailManager;
34+
private AccountService&MockObject $accountService;
35+
private MockObject|IMailSearch $mailSearch;
36+
37+
protected function setUp(): void {
38+
parent::setUp();
39+
40+
$this->request = $this->createMock(IRequest::class);
41+
$this->accountService = $this->createMock(AccountService::class);
42+
$this->mailManager = $this->createMock(IMailManager::class);
43+
$this->mailSearch = $this->createMock(IMailSearch::class);
44+
45+
$this->controller = new MailboxesApiController(
46+
'mail',
47+
$this->request,
48+
self::USER_ID,
49+
$this->mailManager,
50+
$this->accountService,
51+
$this->mailSearch,
52+
53+
);
54+
}
55+
56+
public function testListMailboxesWithoutUser() {
57+
$controller = new MailboxesApiController(
58+
'mail',
59+
$this->request,
60+
null,
61+
$this->mailManager,
62+
$this->accountService,
63+
$this->mailSearch,
64+
);
65+
66+
$this->accountService->expects(self::never())
67+
->method('find');
68+
69+
$accountId = 28;
70+
71+
$actual = $controller->list($accountId);
72+
$this->assertEquals(Http::STATUS_NOT_FOUND, $actual->getStatus());
73+
}
74+
75+
76+
public function testListMailboxes() {
77+
$accountId = 42;
78+
$mailAccount = new MailAccount();
79+
$mailAccount->setId($accountId);
80+
$mailAccount->setEmail('[email protected]');
81+
82+
$account = new Account($mailAccount);
83+
$folder = $this->createMock(Folder::class);
84+
# $accountId = 28;
85+
$this->accountService->expects($this->once())
86+
->method('find')
87+
->with($this->equalTo(self::USER_ID), $this->equalTo($accountId))
88+
->willReturn($account);
89+
$this->mailManager->expects($this->once())
90+
->method('getMailboxes')
91+
->with($this->equalTo($account))
92+
->willReturn([
93+
$folder
94+
]);
95+
$folder->expects($this->once())
96+
->method('getDelimiter')
97+
->willReturn('.');
98+
99+
$actual = $this->controller->list($accountId);
100+
print($actual->getData());
101+
102+
$this->assertEquals(Http::STATUS_OK, $actual->getStatus());
103+
$this->assertEquals(json_encode([
104+
[
105+
'id' => 42,
106+
'email' => '[email protected]',
107+
'mailboxes' => [
108+
$folder,
109+
],
110+
'delimiter' => '.',
111+
]
112+
]), $actual->getData());
113+
}
114+
115+
public function testListMessagesWithoutUser() {
116+
$controller = new MailboxesApiController(
117+
'mail',
118+
$this->request,
119+
null,
120+
$this->mailManager,
121+
$this->accountService,
122+
$this->mailSearch,
123+
);
124+
125+
$this->accountService->expects(self::never())
126+
->method('find');
127+
128+
$accountId = 28;
129+
130+
$actual = $controller->listMessages($accountId);
131+
$this->assertEquals(Http::STATUS_NOT_FOUND, $actual->getStatus());
132+
}
133+
134+
135+
public function testListMessages(): void {
136+
$accountId = 100;
137+
$mailboxId = 101;
138+
$mailbox = new Mailbox();
139+
$mailbox->setAccountId($accountId);
140+
$this->mailManager->expects(self::once())
141+
->method('getMailbox')
142+
->with(SELF::USER_ID, $mailboxId)
143+
->willReturn($mailbox);
144+
$mailAccount = new MailAccount();
145+
$account = new Account($mailAccount);
146+
$this->accountService->expects(self::once())
147+
->method('find')
148+
->with(SELF::USER_ID, $accountId)
149+
->willReturn($account);
150+
151+
$messages = [
152+
new DbMessage(),
153+
new DbMessage(),
154+
];
155+
$this->mailSearch->expects(self::once())
156+
->method('findMessages')
157+
->with(
158+
$account,
159+
$mailbox,
160+
'DESC',
161+
null,
162+
null,
163+
null,
164+
SELF::USER_ID,
165+
'threaded',
166+
)->willReturn($messages);
167+
168+
$actual = $this->controller->listMessages($mailboxId);
169+
170+
$this->assertEquals(Http::STATUS_OK, $actual->getStatus());
171+
$this->assertEquals(json_encode($messages), $actual->getData());
172+
}
173+
174+
public function testListMessagesInvalidMailbox(): void {
175+
$accountId = 100;
176+
$mailboxId = 101;
177+
$mailbox = new Mailbox();
178+
$mailbox->setAccountId($accountId);
179+
$this->mailManager->expects(self::once())
180+
->method('getMailbox')
181+
->with(SELF::USER_ID, $mailboxId)
182+
->willThrowException(new DoesNotExistException(''));
183+
$this->accountService->expects(self::never())
184+
->method('find');
185+
186+
187+
$actual = $this->controller->listMessages($mailboxId);
188+
189+
$this->assertEquals(Http::STATUS_FORBIDDEN, $actual->getStatus());
190+
}
191+
192+
193+
194+
}

0 commit comments

Comments
 (0)