-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathMailServiceTest.php
More file actions
175 lines (158 loc) · 4.53 KB
/
MailServiceTest.php
File metadata and controls
175 lines (158 loc) · 4.53 KB
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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Tests\Unit\Service;
use OCA\Libresign\Db\File;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\SignRequest;
use OCA\Libresign\Service\MailService;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\Mail\IEMailTemplate;
use OCP\Mail\IMailer;
use OCP\Mail\IMessage;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
/**
* @internal
*/
final class MailServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
private LoggerInterface&MockObject $logger;
private IMailer&MockObject $mailer;
private FileMapper&MockObject $fileMapper;
private IL10N&MockObject $l10n;
private IURLGenerator&MockObject $urlGenerator;
private IAppConfig&MockObject $appConfig;
private MailService $service;
public function setUp(): void {
parent::setUp();
$this->logger = $this->createMock(LoggerInterface::class);
$this->mailer = $this->createMock(IMailer::class);
$this->fileMapper = $this->createMock(FileMapper::class);
$this->l10n = $this->createMock(IL10N::class);
$this->l10n
->method('t')
->willReturnArgument(0);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->appConfig = $this->createMock(IAppConfig::class);
$this->service = new MailService(
$this->logger,
$this->mailer,
$this->fileMapper,
$this->l10n,
$this->urlGenerator,
$this->appConfig
);
}
public function testSuccessNotifyUnsignedUser():void {
$signRequest = $this->createMock(SignRequest::class);
$signRequest
->method('__call')
->willReturnCallback(fn (string $method)
=> match ($method) {
'getUuid' => 'asdfg',
'getFileId' => 1,
'getDisplayName' => 'John Doe'
}
);
$file = $this->createMock(File::class);
$file
->method('__call')
->with($this->equalTo('getName'), $this->anything())
->willReturn('Filename');
$this->fileMapper
->method('getById')
->willReturn($file);
$this->appConfig
->method('getValueBool')
->willReturn(true);
$actual = $this->service->notifyUnsignedUser($signRequest, 'a@b.coop');
$this->assertNull($actual);
}
public function testFailToSendMailToUnsignedUser():void {
$this->expectExceptionMessage('Notify unsigned notification mail could not be sent');
$signRequest = $this->createMock(SignRequest::class);
$signRequest
->method('__call')
->willReturnCallback(fn (string $method)
=> match ($method) {
'getUuid' => 'asdfg',
'getFileId' => 1,
'getDisplayName' => 'John doe',
}
);
$file = $this->createMock(File::class);
$file
->method('__call')
->with($this->equalTo('getName'), $this->anything())
->willReturn('Filename');
$this->fileMapper
->method('getById')
->willReturn($file);
$this->mailer
->method('send')
->willReturnCallback(function ():void {
throw new \Exception('Error Processing Request', 1);
});
$this->appConfig
->method('getValueBool')
->willReturn(true);
$actual = $this->service->notifyUnsignedUser($signRequest, 'a@b.coop');
$this->assertNull($actual);
}
public function testSendCodeToSignUsesLocalizedSubjectWithoutMutation(): void {
$l10n = $this->createMock(IL10N::class);
$l10n
->method('t')
->willReturnCallback(static fn (string $text): string
=> $text === 'LibreSign: Code to sign file'
? 'LibreSign : Code nécessaire à la signature du fichier'
: $text
);
$service = new MailService(
$this->logger,
$this->mailer,
$this->fileMapper,
$l10n,
$this->urlGenerator,
$this->appConfig
);
$emailTemplate = $this->createMock(IEMailTemplate::class);
$emailTemplate
->expects($this->once())
->method('setSubject')
->with('LibreSign : Code nécessaire à la signature du fichier');
$emailTemplate
->expects($this->once())
->method('addHeader');
$emailTemplate
->expects($this->exactly(2))
->method('addBodyText');
$message = $this->createMock(IMessage::class);
$message
->method('setTo')
->willReturnSelf();
$message
->method('useTemplate')
->willReturnSelf();
$this->mailer
->expects($this->once())
->method('createEMailTemplate')
->with('settings.TestEmail')
->willReturn($emailTemplate);
$this->mailer
->expects($this->once())
->method('createMessage')
->willReturn($message);
$this->mailer
->expects($this->once())
->method('send')
->with($message)
->willReturn([]);
$service->sendCodeToSign('a@b.coop', 'John Doe', '123456');
}
}