Skip to content

Commit 6715269

Browse files
committed
Add option for a dummy test notification
Signed-off-by: Joas Schilling <coding@schilljs.com>
1 parent eb55c38 commit 6715269

File tree

4 files changed

+165
-15
lines changed

4 files changed

+165
-15
lines changed

lib/Command/Generate.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ protected function configure(): void {
7878
'Long mesage to be sent to the user (max. 4000 characters)',
7979
''
8080
)
81+
->addOption(
82+
'dummy',
83+
'd',
84+
InputOption::VALUE_NONE,
85+
'Create a full-flexed dummy notification for client debugging with actions and parameters (short-message will be casted to integer and is the number of actions (max 3))'
86+
)
8187
;
8288
}
8389

@@ -90,21 +96,29 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9096
$userId = $input->getArgument('user-id');
9197
$subject = $input->getArgument('short-message');
9298
$message = $input->getOption('long-message');
99+
$dummy = $input->getOption('dummy');
93100

94101
$user = $this->userManager->get($userId);
95102
if (!$user instanceof IUser) {
96103
$output->writeln('Unknown user');
97104
return 1;
98105
}
99106

100-
if ($subject === '' || strlen($subject) > 255) {
101-
$output->writeln('Too long or empty short-message');
102-
return 1;
103-
}
107+
if (!$dummy) {
108+
if ($subject === '' || strlen($subject) > 255) {
109+
$output->writeln('Too long or empty short-message');
110+
return 1;
111+
}
104112

105-
if ($message !== '' && strlen($message) > 4000) {
106-
$output->writeln('Too long long-message');
107-
return 1;
113+
if ($message !== '' && strlen($message) > 4000) {
114+
$output->writeln('Too long long-message');
115+
return 1;
116+
}
117+
118+
$subjectTitle = 'cli';
119+
} else {
120+
$subject = (int) $subject;
121+
$subjectTitle = 'dummy';
108122
}
109123

110124
$notification = $this->notificationManager->createNotification();
@@ -115,7 +129,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
115129
->setUser($user->getUID())
116130
->setDateTime($datetime)
117131
->setObject('admin_notifications', dechex($datetime->getTimestamp()))
118-
->setSubject('cli', [$subject]);
132+
->setSubject($subjectTitle, [$subject]);
119133

120134
if ($message !== '') {
121135
$notification->setMessage('cli', [$message]);

lib/Notifier/AdminNotifications.php

Lines changed: 126 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@
2626

2727
namespace OCA\Notifications\Notifier;
2828

29+
use OCP\Files\Folder;
30+
use OCP\Files\IRootFolder;
2931
use OCP\IURLGenerator;
32+
use OCP\IUser;
33+
use OCP\IUserManager;
3034
use OCP\L10N\IFactory;
3135
use OCP\Notification\AlreadyProcessedException;
36+
use OCP\Notification\IAction;
3237
use OCP\Notification\INotification;
3338
use OCP\Notification\INotifier;
3439

@@ -39,11 +44,19 @@ class AdminNotifications implements INotifier {
3944

4045
/** @var IURLGenerator */
4146
protected $urlGenerator;
47+
/** @var IUserManager */
48+
protected $userManager;
49+
/** @var IRootFolder */
50+
protected $rootFolder;
4251

4352
public function __construct(IFactory $l10nFactory,
44-
IURLGenerator $urlGenerator) {
53+
IURLGenerator $urlGenerator,
54+
IUserManager $userManager,
55+
IRootFolder $rootFolder) {
4556
$this->l10nFactory = $l10nFactory;
4657
$this->urlGenerator = $urlGenerator;
58+
$this->userManager = $userManager;
59+
$this->rootFolder = $rootFolder;
4760
}
4861

4962
/**
@@ -57,7 +70,7 @@ public function getID(): string {
5770
}
5871

5972
/**
60-
* Human readable name describing the notifier
73+
* Human-readable name describing the notifier
6174
*
6275
* @return string
6376
* @since 17.0.0
@@ -79,6 +92,117 @@ public function prepare(INotification $notification, string $languageCode): INot
7992
}
8093

8194
switch ($notification->getSubject()) {
95+
case 'dummy':
96+
$subjectParams = $notification->getSubjectParameters();
97+
$numActions = (int) $subjectParams[0];
98+
99+
$user = $this->userManager->get($notification->getUser());
100+
assert($user instanceof IUser);
101+
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
102+
$dirList = $userFolder->getDirectoryListing();
103+
if (empty($dirList)) {
104+
$file1 = $userFolder;
105+
} else {
106+
$file1 = array_pop($dirList);
107+
}
108+
if (empty($dirList)) {
109+
$file2 = $userFolder;
110+
} else {
111+
$file2 = array_shift($dirList);
112+
if ($file2 instanceof Folder) {
113+
$dirList = $file2->getDirectoryListing();
114+
if (!empty($dirList)) {
115+
$file2 = array_shift($dirList);
116+
}
117+
}
118+
}
119+
120+
$path1 = rtrim($file1->getPath(), '/');
121+
if (strpos($path1, '/' . $notification->getUser() . '/files/') === 0) {
122+
// Remove /user/files/...
123+
[,,, $path1] = explode('/', $path1, 4);
124+
}
125+
$path2 = rtrim($file2->getPath(), '/');
126+
if (strpos($path2, '/' . $notification->getUser() . '/files/') === 0) {
127+
// Remove /user/files/...
128+
[,,, $path2] = explode('/', $path2, 4);
129+
}
130+
131+
$loremIpsum = 'User {actor} owns a file {item}';
132+
$loremIpsumLong = 'Lorem {user-2} dolor sit {file-3}, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.' . "\n" . 'Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.';
133+
134+
$notification->setRichSubject($loremIpsum, [
135+
'actor' => [
136+
'type' => 'user',
137+
'id' => $user->getUID(),
138+
'name' => $user->getDisplayName(),
139+
],
140+
'item' => [
141+
'type' => 'file',
142+
'id' => $file1->getId(),
143+
'name' => $file1->getName(),
144+
'size' => $file1->getSize(),
145+
'path' => $path1,
146+
'link' => $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $file1->getId()]),
147+
'mimetype' => $file1->getMimetype(),
148+
],
149+
]);
150+
$notification->setRichMessage($loremIpsumLong, [
151+
'user-2' => [
152+
'type' => 'user',
153+
'id' => $user->getUID(),
154+
'name' => $user->getDisplayName(),
155+
],
156+
'file-3' => [
157+
'type' => 'file',
158+
'id' => $file2->getId(),
159+
'name' => $file2->getName(),
160+
'size' => $file2->getSize(),
161+
'path' => $path2,
162+
'link' => $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $file2->getId()]),
163+
'mimetype' => $file2->getMimetype(),
164+
],
165+
]);
166+
167+
$primary = $notification->createAction();
168+
$primary->setPrimary(true);
169+
$primary->setParsedLabel('3 is prim(e|ary)');
170+
$primary->setLink(
171+
'https://en.wikipedia.org/wiki/3#Mathematics',
172+
IAction::TYPE_WEB
173+
);
174+
175+
$secondary = $notification->createAction();
176+
$secondary->setPrimary(false);
177+
$secondary->setParsedLabel('Get status');
178+
$secondary->setLink(
179+
$this->urlGenerator->getAbsoluteURL('status.php'),
180+
IAction::TYPE_GET
181+
);
182+
183+
$three = $notification->createAction();
184+
$three->setPrimary(false);
185+
$three->setParsedLabel('Delete status.php');
186+
$three->setLink(
187+
$this->urlGenerator->getAbsoluteURL('status.php'),
188+
IAction::TYPE_DELETE
189+
);
190+
191+
$numActions = min(3, $numActions);
192+
switch ($numActions) {
193+
case 3:
194+
$notification->addParsedAction($three);
195+
// no break
196+
case 2:
197+
$notification->addParsedAction($secondary);
198+
// no break
199+
case 1:
200+
$notification->addParsedAction($primary);
201+
}
202+
203+
$notification->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('notifications', 'notifications-dark.svg')));
204+
return $notification;
205+
82206
// Deal with known subjects
83207
case 'cli':
84208
case 'ocs':

tests/Unit/Command/GenerateTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,11 @@ public function testExecute($userId, $short, $long, $createNotification, $notify
162162
['user-id', $userId],
163163
['short-message', $short],
164164
]);
165-
$input->expects($this->exactly(1))
165+
$input->expects($this->exactly(2))
166166
->method('getOption')
167167
->willReturnMap([
168168
['long-message', $long],
169+
['dummy', false],
169170
]);
170171
$output = $this->createMock(OutputInterface::class);
171172

tests/Unit/Notifier/NotifierTest.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,27 @@
2424
namespace OCA\Notifications\Tests\Unit\Notifier;
2525

2626
use OCA\Notifications\Notifier\AdminNotifications;
27+
use OCP\Files\IRootFolder;
2728
use OCP\IL10N;
2829
use OCP\IURLGenerator;
30+
use OCP\IUserManager;
2931
use OCP\L10N\IFactory;
3032
use OCP\Notification\INotification;
33+
use PHPUnit\Framework\MockObject\MockObject;
3134

3235
class NotifierTest extends \Test\TestCase {
3336
/** @var AdminNotifications */
3437
protected $notifier;
3538

36-
/** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
39+
/** @var IFactory|MockObject */
3740
protected $factory;
38-
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
41+
/** @var IURLGenerator|MockObject */
3942
protected $urlGenerator;
40-
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
43+
/** @var IUserManager|MockObject */
44+
protected $userManager;
45+
/** @var IRootFolder|MockObject */
46+
protected $rootFolder;
47+
/** @var IL10N|MockObject */
4148
protected $l;
4249

4350
protected function setUp(): void {
@@ -54,10 +61,14 @@ protected function setUp(): void {
5461
$this->factory->expects($this->any())
5562
->method('get')
5663
->willReturn($this->l);
64+
$this->userManager = $this->createMock(IUserManager::class);
65+
$this->rootFolder = $this->createMock(IRootFolder::class);
5766

5867
$this->notifier = new AdminNotifications(
5968
$this->factory,
60-
$this->urlGenerator
69+
$this->urlGenerator,
70+
$this->userManager,
71+
$this->rootFolder
6172
);
6273
}
6374

0 commit comments

Comments
 (0)