-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathAPIControllerTest.php
More file actions
159 lines (143 loc) Β· 5.29 KB
/
Copy pathAPIControllerTest.php
File metadata and controls
159 lines (143 loc) Β· 5.29 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
<?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\Controller;
use OCA\Notifications\App;
use OCA\Notifications\Controller\APIController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Notification\IManager;
use OCP\Notification\IncompleteNotificationException;
use OCP\Notification\INotification;
use OCP\RichObjectStrings\IValidator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
#[Group(name: 'DB')]
class APIControllerTest extends TestCase {
protected ITimeFactory&MockObject $timeFactory;
protected IUserManager&MockObject $userManager;
protected IUserSession&MockObject $userSession;
protected IManager&MockObject $notificationManager;
protected App&MockObject $notificationApp;
protected IValidator&MockObject $richValidator;
protected IL10N&MockObject $l;
protected LoggerInterface&MockObject $logger;
protected APIController $controller;
protected function setUp(): void {
parent::setUp();
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject $request */
$request = $this->createMock(IRequest::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->notificationManager = $this->createMock(IManager::class);
$this->notificationApp = $this->createMock(App::class);
$this->richValidator = $this->createMock(IValidator::class);
$this->l = $this->createMock(IL10N::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->controller = new APIController(
'notifications',
$request,
$this->timeFactory,
$this->userManager,
$this->userSession,
$this->notificationManager,
$this->notificationApp,
$this->richValidator,
$this->l,
$this->logger,
);
}
public static function dataGenerateNotification(): array {
return [
['user', '', '', false, null, false, null, 123, null, Http::STATUS_NOT_FOUND],
['user', '', '', false, null, false, 'user', 123, null, Http::STATUS_BAD_REQUEST],
['user', str_repeat('a', 256), '', false, null, false, 'user', 123, null, Http::STATUS_BAD_REQUEST],
['user', 'short', '', true, false, false, 'user', 123, '7b', Http::STATUS_OK],
['user', 'short', str_repeat('a', 4001), false, null, false, 'user', 123, null, Http::STATUS_BAD_REQUEST],
['user', 'short', str_repeat('a', 4000), true, false, true, 'user', 123, '7b', Http::STATUS_OK],
['user', 'short', 'long', true, true, true, 'user', 123, '7b', Http::STATUS_INTERNAL_SERVER_ERROR],
];
}
#[DataProvider(methodName: 'dataGenerateNotification')]
public function testGenerateNotification(string $userId, string $short, string $long, bool $createNotification, ?bool $notifyThrows, bool $validLong, ?string $user, int $time, ?string $hexTime, int $statusCode): void {
if ($user !== null) {
$u = $this->createMock(IUser::class);
$u->expects($createNotification ? $this->once() : $this->never())
->method('getUID')
->willReturn($user);
} else {
$u = null;
}
$this->userManager->expects($this->any())
->method('get')
->with($userId)
->willReturn($u);
$dateTime = new \DateTime();
$dateTime->setTimestamp($time);
$this->timeFactory->expects($hexTime === null ? $this->never() : $this->once())
->method('getDateTime')
->willReturn($dateTime);
if ($createNotification) {
$n = $this->createMock(INotification::class);
$n->expects($this->once())
->method('setApp')
->with('admin_notifications')
->willReturnSelf();
$n->expects($this->once())
->method('setUser')
->with($user)
->willReturnSelf();
$n->expects($this->once())
->method('setDateTime')
->willReturnSelf();
$n->expects($this->once())
->method('setObject')
->with('admin_notifications', $hexTime)
->willReturnSelf();
$n->expects($this->once())
->method('setSubject')
->with('ocs', ['subject' => $short, 'parameters' => []])
->willReturnSelf();
if ($validLong) {
$n->expects($this->once())
->method('setMessage')
->with('ocs', ['message' => $long, 'parameters' => []])
->willReturnSelf();
} else {
$n->expects($this->never())
->method('setMessage');
}
$this->notificationManager->expects($this->once())
->method('createNotification')
->willReturn($n);
if ($notifyThrows === null) {
$this->notificationManager->expects($this->never())
->method('notify');
} elseif ($notifyThrows === false) {
$this->notificationManager->expects($this->once())
->method('notify')
->with($n);
} elseif ($notifyThrows === true) {
$this->notificationManager->expects($this->once())
->method('notify')
->willThrowException(new IncompleteNotificationException());
}
}
$response = $this->controller->generateNotification($userId, $short, $long);
$this->assertInstanceOf(DataResponse::class, $response);
$this->assertSame($statusCode, $response->getStatus());
}
}