From 1fcd7848c6066085893423d3069bc831757d6e41 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 28 Sep 2016 13:59:50 +0200 Subject: [PATCH 1/3] Remove notifications upon user deletion Signed-off-by: Joas Schilling --- lib/private/User/User.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/private/User/User.php b/lib/private/User/User.php index 4d403535bf306..7859db23b6b81 100644 --- a/lib/private/User/User.php +++ b/lib/private/User/User.php @@ -212,6 +212,10 @@ public function delete() { \OC::$server->getCommentsManager()->deleteReferencesOfActor('users', $this->uid); \OC::$server->getCommentsManager()->deleteReadMarksFromUser($this); + + $notification = \OC::$server->getNotificationManager()->createNotification(); + $notification->setUser($this->uid); + \OC::$server->getNotificationManager()->markProcessed($notification); } if ($this->emitter) { From 9107573020a85c23b0650cef4001a0c39b129715 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 29 Sep 2016 15:01:38 +0200 Subject: [PATCH 2/3] Make sure that comments, notifications and preferences are deleted Signed-off-by: Joas Schilling --- tests/lib/User/UserTest.php | 81 ++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 11 deletions(-) diff --git a/tests/lib/User/UserTest.php b/tests/lib/User/UserTest.php index a49bddde9eb70..69bab0b659b6f 100644 --- a/tests/lib/User/UserTest.php +++ b/tests/lib/User/UserTest.php @@ -174,9 +174,7 @@ public function testChangeAvatarNotSupported() { $backend->expects($this->any()) ->method('implementsActions') - ->will($this->returnCallback(function ($actions) { - return false; - })); + ->willReturn(false); $user = new \OC\User\User('foo', $backend); $this->assertTrue($user->canChangeAvatar()); @@ -379,9 +377,7 @@ public function testSetDisplayNameNotSupported() { $backend->expects($this->any()) ->method('implementsActions') - ->will($this->returnCallback(function ($actions) { - return false; - })); + ->willReturn(false); $backend->expects($this->never()) ->method('setDisplayName'); @@ -432,7 +428,18 @@ public function testSetPasswordHooks() { $this->assertEquals(2, $hooksCalled); } - public function testDeleteHooks() { + public function dataDeleteHooks() { + return [ + [true], + [false], + ]; + } + + /** + * @dataProvider dataDeleteHooks + * @param bool $result + */ + public function testDeleteHooks($result) { $hooksCalled = 0; $test = $this; @@ -441,7 +448,10 @@ public function testDeleteHooks() { */ $backend = $this->getMock('\Test\Util\User\Dummy'); $backend->expects($this->once()) - ->method('deleteUser'); + ->method('deleteUser') + ->willReturn($result); + $emitter = new PublicEmitter(); + $user = new \OC\User\User('foo', $backend, $emitter); /** * @param \OC\User\User $user @@ -451,12 +461,61 @@ public function testDeleteHooks() { $test->assertEquals('foo', $user->getUID()); }; - $emitter = new PublicEmitter(); $emitter->listen('\OC\User', 'preDelete', $hook); $emitter->listen('\OC\User', 'postDelete', $hook); - $user = new \OC\User\User('foo', $backend, $emitter); - $this->assertTrue($user->delete()); + $config = $this->getMockBuilder('OCP\IConfig')->getMock(); + $commentsManager = $this->getMockBuilder('OCP\Comments\ICommentsManager')->getMock(); + $notificationManager = $this->getMockBuilder('OCP\Notification\IManager')->getMock(); + + if ($result) { + $config->expects($this->once()) + ->method('deleteAllUserValues') + ->with('foo'); + + $commentsManager->expects($this->once()) + ->method('deleteReferencesOfActor') + ->with('users', 'foo'); + $commentsManager->expects($this->once()) + ->method('deleteReadMarksFromUser') + ->with($user); + + $notification = $this->getMockBuilder('OCP\Notification\INotification')->getMock(); + $notification->expects($this->once()) + ->method('setUser') + ->with('foo'); + + $notificationManager->expects($this->once()) + ->method('createNotification') + ->willReturn($notification); + $notificationManager->expects($this->once()) + ->method('markProcessed') + ->with($notification); + } else { + $config->expects($this->never()) + ->method('deleteAllUserValues'); + + $commentsManager->expects($this->never()) + ->method('deleteReferencesOfActor'); + $commentsManager->expects($this->never()) + ->method('deleteReadMarksFromUser'); + + $notificationManager->expects($this->never()) + ->method('createNotification'); + $notificationManager->expects($this->never()) + ->method('markProcessed'); + } + + $this->overwriteService('NotificationManager', $notificationManager); + $this->overwriteService('CommentsManager', $commentsManager); + $this->overwriteService('AllConfig', $config); + + $this->assertSame($result, $user->delete()); + + $this->restoreService('AllConfig'); + $this->restoreService('CommentsManager'); + $this->restoreService('NotificationManager'); + $this->assertEquals(2, $hooksCalled); } From 6b703e97ce331c939c9718230358c63d74218502 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 29 Sep 2016 15:05:47 +0200 Subject: [PATCH 3/3] Only trigger postDelete hooks when the user was deleted... Signed-off-by: Joas Schilling --- lib/private/User/User.php | 6 +++--- tests/lib/User/UserTest.php | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/private/User/User.php b/lib/private/User/User.php index 7859db23b6b81..decd32ac82e00 100644 --- a/lib/private/User/User.php +++ b/lib/private/User/User.php @@ -216,10 +216,10 @@ public function delete() { $notification = \OC::$server->getNotificationManager()->createNotification(); $notification->setUser($this->uid); \OC::$server->getNotificationManager()->markProcessed($notification); - } - if ($this->emitter) { - $this->emitter->emit('\OC\User', 'postDelete', array($this)); + if ($this->emitter) { + $this->emitter->emit('\OC\User', 'postDelete', array($this)); + } } return !($result === false); } diff --git a/tests/lib/User/UserTest.php b/tests/lib/User/UserTest.php index 69bab0b659b6f..d72d7af7c3a80 100644 --- a/tests/lib/User/UserTest.php +++ b/tests/lib/User/UserTest.php @@ -430,16 +430,17 @@ public function testSetPasswordHooks() { public function dataDeleteHooks() { return [ - [true], - [false], + [true, 2], + [false, 1], ]; } /** * @dataProvider dataDeleteHooks * @param bool $result + * @param int $expectedHooks */ - public function testDeleteHooks($result) { + public function testDeleteHooks($result, $expectedHooks) { $hooksCalled = 0; $test = $this; @@ -516,7 +517,7 @@ public function testDeleteHooks($result) { $this->restoreService('CommentsManager'); $this->restoreService('NotificationManager'); - $this->assertEquals(2, $hooksCalled); + $this->assertEquals($expectedHooks, $hooksCalled); } public function testGetCloudId() {