Skip to content

Commit 85522ff

Browse files
authored
Merge pull request #1558 from nextcloud/remove-notifications-upon-user-deletion
Remove notifications upon user deletion
2 parents 938b0d5 + 4d1acfd commit 85522ff

File tree

2 files changed

+83
-15
lines changed

2 files changed

+83
-15
lines changed

lib/private/User/User.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,14 @@ public function delete() {
214214

215215
\OC::$server->getCommentsManager()->deleteReferencesOfActor('users', $this->uid);
216216
\OC::$server->getCommentsManager()->deleteReadMarksFromUser($this);
217-
}
218217

219-
if ($this->emitter) {
220-
$this->emitter->emit('\OC\User', 'postDelete', array($this));
218+
$notification = \OC::$server->getNotificationManager()->createNotification();
219+
$notification->setUser($this->uid);
220+
\OC::$server->getNotificationManager()->markProcessed($notification);
221+
222+
if ($this->emitter) {
223+
$this->emitter->emit('\OC\User', 'postDelete', array($this));
224+
}
221225
}
222226
return !($result === false);
223227
}

tests/lib/User/UserTest.php

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
use OC\Hooks\PublicEmitter;
1313
use OC\User\Database;
14+
use OCP\Comments\ICommentsManager;
15+
use OCP\IConfig;
16+
use OCP\Notification\IManager as INotificationManager;
17+
use OCP\Notification\INotification;
1418

1519
/**
1620
* Class UserTest
@@ -175,9 +179,7 @@ public function testChangeAvatarNotSupported() {
175179

176180
$backend->expects($this->any())
177181
->method('implementsActions')
178-
->will($this->returnCallback(function ($actions) {
179-
return false;
180-
}));
182+
->willReturn(false);
181183

182184
$user = new \OC\User\User('foo', $backend);
183185
$this->assertTrue($user->canChangeAvatar());
@@ -380,9 +382,7 @@ public function testSetDisplayNameNotSupported() {
380382

381383
$backend->expects($this->any())
382384
->method('implementsActions')
383-
->will($this->returnCallback(function ($actions) {
384-
return false;
385-
}));
385+
->willReturn(false);
386386

387387
$backend->expects($this->never())
388388
->method('setDisplayName');
@@ -433,7 +433,19 @@ public function testSetPasswordHooks() {
433433
$this->assertEquals(2, $hooksCalled);
434434
}
435435

436-
public function testDeleteHooks() {
436+
public function dataDeleteHooks() {
437+
return [
438+
[true, 2],
439+
[false, 1],
440+
];
441+
}
442+
443+
/**
444+
* @dataProvider dataDeleteHooks
445+
* @param bool $result
446+
* @param int $expectedHooks
447+
*/
448+
public function testDeleteHooks($result, $expectedHooks) {
437449
$hooksCalled = 0;
438450
$test = $this;
439451

@@ -442,7 +454,10 @@ public function testDeleteHooks() {
442454
*/
443455
$backend = $this->createMock(\Test\Util\User\Dummy::class);
444456
$backend->expects($this->once())
445-
->method('deleteUser');
457+
->method('deleteUser')
458+
->willReturn($result);
459+
$emitter = new PublicEmitter();
460+
$user = new \OC\User\User('foo', $backend, $emitter);
446461

447462
/**
448463
* @param \OC\User\User $user
@@ -452,13 +467,62 @@ public function testDeleteHooks() {
452467
$test->assertEquals('foo', $user->getUID());
453468
};
454469

455-
$emitter = new PublicEmitter();
456470
$emitter->listen('\OC\User', 'preDelete', $hook);
457471
$emitter->listen('\OC\User', 'postDelete', $hook);
458472

459-
$user = new \OC\User\User('foo', $backend, $emitter);
460-
$this->assertTrue($user->delete());
461-
$this->assertEquals(2, $hooksCalled);
473+
$config = $this->createMock(IConfig::class);
474+
$commentsManager = $this->createMock(ICommentsManager::class);
475+
$notificationManager = $this->createMock(INotificationManager::class);
476+
477+
if ($result) {
478+
$config->expects($this->once())
479+
->method('deleteAllUserValues')
480+
->with('foo');
481+
482+
$commentsManager->expects($this->once())
483+
->method('deleteReferencesOfActor')
484+
->with('users', 'foo');
485+
$commentsManager->expects($this->once())
486+
->method('deleteReadMarksFromUser')
487+
->with($user);
488+
489+
$notification = $this->createMock(INotification::class);
490+
$notification->expects($this->once())
491+
->method('setUser')
492+
->with('foo');
493+
494+
$notificationManager->expects($this->once())
495+
->method('createNotification')
496+
->willReturn($notification);
497+
$notificationManager->expects($this->once())
498+
->method('markProcessed')
499+
->with($notification);
500+
} else {
501+
$config->expects($this->never())
502+
->method('deleteAllUserValues');
503+
504+
$commentsManager->expects($this->never())
505+
->method('deleteReferencesOfActor');
506+
$commentsManager->expects($this->never())
507+
->method('deleteReadMarksFromUser');
508+
509+
$notificationManager->expects($this->never())
510+
->method('createNotification');
511+
$notificationManager->expects($this->never())
512+
->method('markProcessed');
513+
}
514+
515+
$this->overwriteService('NotificationManager', $notificationManager);
516+
$this->overwriteService('CommentsManager', $commentsManager);
517+
$this->overwriteService('AllConfig', $config);
518+
519+
$this->assertSame($result, $user->delete());
520+
521+
$this->restoreService('AllConfig');
522+
$this->restoreService('CommentsManager');
523+
$this->restoreService('NotificationManager');
524+
525+
$this->assertEquals($expectedHooks, $hooksCalled);
462526
}
463527

464528
public function testGetCloudId() {

0 commit comments

Comments
 (0)