Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove notifications upon user deletion #1558

Merged
merged 3 commits into from
Sep 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,14 @@ public function delete() {

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

if ($this->emitter) {
$this->emitter->emit('\OC\User', 'postDelete', array($this));
$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));
}
}
return !($result === false);
}
Expand Down
88 changes: 76 additions & 12 deletions tests/lib/User/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

use OC\Hooks\PublicEmitter;
use OC\User\Database;
use OCP\Comments\ICommentsManager;
use OCP\IConfig;
use OCP\Notification\IManager as INotificationManager;
use OCP\Notification\INotification;

/**
* Class UserTest
Expand Down Expand Up @@ -175,9 +179,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());
Expand Down Expand Up @@ -380,9 +382,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');
Expand Down Expand Up @@ -433,7 +433,19 @@ public function testSetPasswordHooks() {
$this->assertEquals(2, $hooksCalled);
}

public function testDeleteHooks() {
public function dataDeleteHooks() {
return [
[true, 2],
[false, 1],
];
}

/**
* @dataProvider dataDeleteHooks
* @param bool $result
* @param int $expectedHooks
*/
public function testDeleteHooks($result, $expectedHooks) {
$hooksCalled = 0;
$test = $this;

Expand All @@ -442,7 +454,10 @@ public function testDeleteHooks() {
*/
$backend = $this->createMock(\Test\Util\User\Dummy::class);
$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
Expand All @@ -452,13 +467,62 @@ 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());
$this->assertEquals(2, $hooksCalled);
$config = $this->createMock(IConfig::class);
$commentsManager = $this->createMock(ICommentsManager::class);
$notificationManager = $this->createMock(INotificationManager::class);

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->createMock(INotification::class);
$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($expectedHooks, $hooksCalled);
}

public function testGetCloudId() {
Expand Down