-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathNotifierTest.php
More file actions
141 lines (120 loc) Β· 4.16 KB
/
Copy pathNotifierTest.php
File metadata and controls
141 lines (120 loc) Β· 4.16 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Notifications\Tests\Unit\Notifier;
use OCA\Notifications\Notifier\AdminNotifications;
use OCP\Files\IRootFolder;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Notification\INotification;
use OCP\Notification\UnknownNotificationException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class NotifierTest extends TestCase {
protected IFactory&MockObject $factory;
protected IURLGenerator&MockObject $urlGenerator;
protected IUserManager&MockObject $userManager;
protected IRootFolder&MockObject $rootFolder;
protected IL10N&MockObject $l;
protected AdminNotifications $notifier;
protected function setUp(): void {
parent::setUp();
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->l = $this->createMock(IL10N::class);
$this->l->expects($this->any())
->method('t')
->willReturnCallback(fn ($string, $args) => vsprintf($string, $args));
$this->factory = $this->createMock(IFactory::class);
$this->factory->expects($this->any())
->method('get')
->willReturn($this->l);
$this->userManager = $this->createMock(IUserManager::class);
$this->rootFolder = $this->createMock(IRootFolder::class);
$this->notifier = new AdminNotifications(
$this->factory,
$this->urlGenerator,
$this->userManager,
$this->rootFolder
);
}
public function testPrepareWrongApp(): void {
/** @var INotification&MockObject $notification */
$notification = $this->createMock(INotification::class);
$notification->expects($this->exactly(2))
->method('getApp')
->willReturn('notifications');
$notification->expects($this->never())
->method('getSubject');
$this->expectException(UnknownNotificationException::class);
$this->expectExceptionMessage('app');
$this->notifier->prepare($notification, 'en');
}
public function testPrepareWrongSubject(): void {
/** @var INotification&MockObject $notification */
$notification = $this->createMock(INotification::class);
$notification->expects($this->once())
->method('getApp')
->willReturn('admin_notifications');
$notification->expects($this->once())
->method('getSubject')
->willReturn('wrong subject');
$this->expectException(UnknownNotificationException::class);
$this->expectExceptionMessage('subject');
$this->notifier->prepare($notification, 'en');
}
public static function dataPrepare(): array {
return [
['ocs', ['subject'], ['message'], true],
];
}
#[DataProvider(methodName: 'dataPrepare')]
public function testPrepare(string $subject, array $subjectParams, array $messageParams, bool $setMessage): void {
/** @var INotification&MockObject $notification */
$notification = $this->createMock(INotification::class);
$notification->expects($this->once())
->method('getApp')
->willReturn('admin_notifications');
$notification->expects($this->once())
->method('getSubject')
->willReturn($subject);
$notification->expects($this->once())
->method('getSubjectParameters')
->willReturn($subjectParams);
$notification->expects($this->once())
->method('getMessageParameters')
->willReturn($messageParams);
$notification->expects($this->once())
->method('setParsedSubject')
->with($subjectParams[0])
->willReturnSelf();
if ($setMessage) {
$notification->expects($this->once())
->method('setParsedMessage')
->with($messageParams[0])
->willReturnSelf();
} else {
$notification->expects($this->never())
->method('setParsedMessage');
}
$this->urlGenerator->expects($this->once())
->method('imagePath')
->with('notifications', 'notifications-dark.svg')
->willReturn('icon-url');
$this->urlGenerator->expects($this->once())
->method('getAbsoluteURL')
->with('icon-url')
->willReturn('absolute-icon-url');
$notification->expects($this->once())
->method('setIcon')
->with('absolute-icon-url')
->willReturnSelf();
$return = $this->notifier->prepare($notification, 'en');
$this->assertEquals($notification, $return);
}
}