Skip to content
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
7 changes: 7 additions & 0 deletions lib/Db/TextBlockMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,11 @@ public function findSharedWithMe(string $userId, array $groups) {
return $this->findEntities($qb);
}

public function deleteByUserId(string $userId) {
$qb = $this->db->getQueryBuilder();
$delete = $qb->delete($this->getTableName())
->where($qb->expr()->eq('owner', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)));
$delete->executeStatement();
}

}
11 changes: 9 additions & 2 deletions lib/Listener/UserDeletedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use OCA\Mail\Exception\ClientException;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\TextBlockService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\User\Events\UserDeletedEvent;
Expand All @@ -26,8 +27,11 @@ class UserDeletedListener implements IEventListener {
/** @var LoggerInterface */
private $logger;

public function __construct(AccountService $accountService,
LoggerInterface $logger) {
public function __construct(
AccountService $accountService,
private TextBlockService $textBlockService,
LoggerInterface $logger,
) {
$this->accountService = $accountService;
$this->logger = $logger;
}
Expand All @@ -52,5 +56,8 @@ public function handle(Event $event): void {
]);
}
}
$this->textBlockService->deleteByUserId(
$user->getUID()
);
}
}
4 changes: 4 additions & 0 deletions lib/Service/TextBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,8 @@ public function unshare(int $textBlockId, string $shareWith): void {
$share = $this->textBlockShareMapper->find($textBlockId, $shareWith);
$this->textBlockShareMapper->delete($share);
}

public function deleteByUserId(string $userId): void {
$this->textBlockMapper->deleteByUserId($userId);
}
}
28 changes: 28 additions & 0 deletions tests/Unit/Listener/UserDeletedListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Listener\UserDeletedListener;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\TextBlockService;
use OCP\EventDispatcher\Event;
use OCP\IUser;
use OCP\User\Events\UserDeletedEvent;
Expand All @@ -23,6 +24,8 @@

class UserDeletedListenerTest extends TestCase {
private AccountService&MockObject $accountService;

private TextBlockService&MockObject $textBlockService;
private LoggerInterface&MockObject $logger;
private UserDeletedListener $listener;

Expand All @@ -31,9 +34,11 @@ protected function setUp(): void {

$this->accountService = $this->createMock(AccountService::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->textBlockService = $this->createMock(TextBlockService::class);

$this->listener = new UserDeletedListener(
$this->accountService,
$this->textBlockService,
$this->logger
);
}
Expand All @@ -60,6 +65,9 @@ public function testHandleUnrelated(): void {
$this->accountService->expects($this->never())
->method('findByUserId');

$this->textBlockService->expects($this->never())
->method('deleteByUserId');

$this->listener->handle($event);

$this->addToAssertionCount(1);
Expand All @@ -80,6 +88,10 @@ public function testHandleUserDeletedWithNoAccounts(): void {
$this->logger->expects($this->never())
->method('error');

$this->textBlockService->expects($this->once())
->method('deleteByUserId')
->with('test-user');

$this->listener->handle($event);
}

Expand All @@ -97,9 +109,14 @@ public function testHandleUserDeletedWithSingleAccount(): void {
->method('delete')
->with('test-user', 42);


$this->logger->expects($this->never())
->method('error');

$this->textBlockService->expects($this->once())
->method('deleteByUserId')
->with('test-user');

$this->listener->handle($event);
}

Expand All @@ -125,6 +142,10 @@ public function testHandleUserDeletedWithMultipleAccounts(): void {
$this->logger->expects($this->never())
->method('error');

$this->textBlockService->expects($this->once())
->method('deleteByUserId')
->with('test-user');

$this->listener->handle($event);
}

Expand Down Expand Up @@ -152,6 +173,10 @@ public function testHandleUserDeletedWithClientException(): void {
['exception' => $exception]
);

$this->textBlockService->expects($this->once())
->method('deleteByUserId')
->with('test-user');

$this->listener->handle($event);
}

Expand Down Expand Up @@ -184,6 +209,9 @@ public function testHandleUserDeletedWithPartialFailure(): void {
'Could not delete user\'s Mail account: Failed to delete account 2',
['exception' => $exception]
);
$this->textBlockService->expects($this->once())
->method('deleteByUserId')
->with('test-user');

$this->listener->handle($event);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/Service/TextBlockServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,5 +338,12 @@ public function testGetShares(): void {
$this->assertSame($shares, $result);
}

public function testDeleteByUserId(): void {
$userId = 'toBeDeleted';
$this->textBlockMapper->expects($this->once())
->method('deleteByUserId')
->with($userId);
$this->textBlockService->deleteByUserId($userId);
}

}