Skip to content

Commit cd282fa

Browse files
authored
Merge pull request #1695 from nextcloud/backport-1558-remove-notifications-upon-user-deletion
[stable10] Remove notifications upon user deletion
2 parents a27633b + 6b703e9 commit cd282fa

File tree

2 files changed

+79
-15
lines changed

2 files changed

+79
-15
lines changed

lib/private/User/User.php

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

213213
\OC::$server->getCommentsManager()->deleteReferencesOfActor('users', $this->uid);
214214
\OC::$server->getCommentsManager()->deleteReadMarksFromUser($this);
215-
}
216215

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

tests/lib/User/UserTest.php

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,7 @@ public function testChangeAvatarNotSupported() {
174174

175175
$backend->expects($this->any())
176176
->method('implementsActions')
177-
->will($this->returnCallback(function ($actions) {
178-
return false;
179-
}));
177+
->willReturn(false);
180178

181179
$user = new \OC\User\User('foo', $backend);
182180
$this->assertTrue($user->canChangeAvatar());
@@ -379,9 +377,7 @@ public function testSetDisplayNameNotSupported() {
379377

380378
$backend->expects($this->any())
381379
->method('implementsActions')
382-
->will($this->returnCallback(function ($actions) {
383-
return false;
384-
}));
380+
->willReturn(false);
385381

386382
$backend->expects($this->never())
387383
->method('setDisplayName');
@@ -432,7 +428,19 @@ public function testSetPasswordHooks() {
432428
$this->assertEquals(2, $hooksCalled);
433429
}
434430

435-
public function testDeleteHooks() {
431+
public function dataDeleteHooks() {
432+
return [
433+
[true, 2],
434+
[false, 1],
435+
];
436+
}
437+
438+
/**
439+
* @dataProvider dataDeleteHooks
440+
* @param bool $result
441+
* @param int $expectedHooks
442+
*/
443+
public function testDeleteHooks($result, $expectedHooks) {
436444
$hooksCalled = 0;
437445
$test = $this;
438446

@@ -441,7 +449,10 @@ public function testDeleteHooks() {
441449
*/
442450
$backend = $this->getMock('\Test\Util\User\Dummy');
443451
$backend->expects($this->once())
444-
->method('deleteUser');
452+
->method('deleteUser')
453+
->willReturn($result);
454+
$emitter = new PublicEmitter();
455+
$user = new \OC\User\User('foo', $backend, $emitter);
445456

446457
/**
447458
* @param \OC\User\User $user
@@ -451,13 +462,62 @@ public function testDeleteHooks() {
451462
$test->assertEquals('foo', $user->getUID());
452463
};
453464

454-
$emitter = new PublicEmitter();
455465
$emitter->listen('\OC\User', 'preDelete', $hook);
456466
$emitter->listen('\OC\User', 'postDelete', $hook);
457467

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

463523
public function testGetCloudId() {

0 commit comments

Comments
 (0)