diff --git a/lib/Cron/DeleteCron.php b/lib/Cron/DeleteCron.php index 95ad042646..0496db6e87 100644 --- a/lib/Cron/DeleteCron.php +++ b/lib/Cron/DeleteCron.php @@ -13,6 +13,7 @@ use OCA\Deck\Db\StackMapper; use OCA\Deck\InvalidAttachmentType; use OCA\Deck\Service\AttachmentService; +use OCA\Deck\Service\ConfigService; use OCA\Deck\Sharing\DeckShareProvider; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\IJob; @@ -35,6 +36,7 @@ class DeleteCron extends TimedJob { public function __construct( ITimeFactory $time, + private readonly ConfigService $configService, BoardMapper $boardMapper, CardMapper $cardMapper, AttachmentService $attachmentService, @@ -50,7 +52,7 @@ public function __construct( $this->stackMapper = $stackMapper; $this->deckShareProvider = $deckShareProvider; - $this->setInterval(60 * 60 * 24); + $this->setInterval(60 * 60); // Run once every hour $this->setTimeSensitivity(IJob::TIME_INSENSITIVE); } @@ -59,18 +61,19 @@ public function __construct( * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ protected function run($argument) { - $boards = $this->boardMapper->findToDelete(); + $timeLimit = time() - $this->configService->getTrashRetention(); + + $boards = $this->boardMapper->findToDelete($timeLimit); foreach ($boards as $board) { $this->boardMapper->delete($board); } - $timeLimit = time() - (60 * 5); // 5 min buffer $cards = $this->cardMapper->findToDelete($timeLimit, 500); foreach ($cards as $card) { $this->cardMapper->delete($card); } - $attachments = $this->attachmentMapper->findToDelete(); + $attachments = $this->attachmentMapper->findToDelete($timeLimit); foreach ($attachments as $attachment) { try { $service = $this->attachmentService->getService($attachment->getType()); @@ -87,7 +90,7 @@ protected function run($argument) { $this->deckShareProvider->delete($share); } - $stacks = $this->stackMapper->findToDelete(); + $stacks = $this->stackMapper->findToDelete($timeLimit); foreach ($stacks as $stack) { $this->stackMapper->delete($stack); } diff --git a/lib/Db/AttachmentMapper.php b/lib/Db/AttachmentMapper.php index 0895453a1d..d96a4f93af 100644 --- a/lib/Db/AttachmentMapper.php +++ b/lib/Db/AttachmentMapper.php @@ -108,9 +108,7 @@ public function findAll(int $cardId): array { /** * @return Attachment[] */ - public function findToDelete(?int $cardId = null, bool $withOffset = true): array { - // add buffer of 5 min - $timeLimit = time() - (60 * 5); + public function findToDelete(int $timeLimit, ?int $cardId = null, bool $withOffset = true): array { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) diff --git a/lib/Db/BoardMapper.php b/lib/Db/BoardMapper.php index 5c8baa5ce4..9c734ac46e 100644 --- a/lib/Db/BoardMapper.php +++ b/lib/Db/BoardMapper.php @@ -455,9 +455,7 @@ public function findAll(): array { return $this->findEntities($qb); } - public function findToDelete() { - // add buffer of 5 min - $timeLimit = time() - (60 * 5); + public function findToDelete(int $timeLimit) { $qb = $this->db->getQueryBuilder(); $qb->select('id', 'title', 'owner', 'color', 'archived', 'deleted_at', 'last_modified', 'external_id', 'share_token') ->from('deck_boards') diff --git a/lib/Db/StackMapper.php b/lib/Db/StackMapper.php index baa8d6186d..bbbd6e94bc 100644 --- a/lib/Db/StackMapper.php +++ b/lib/Db/StackMapper.php @@ -219,9 +219,7 @@ public function findBoardId(int $id): ?int { * @return array * @throws \OCP\DB\Exception */ - public function findToDelete(): array { - // add buffer of 5 min - $timeLimit = time() - (60 * 5); + public function findToDelete(int $timeLimit): array { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) diff --git a/lib/Service/AttachmentService.php b/lib/Service/AttachmentService.php index bcf0bc8eae..a871119f22 100644 --- a/lib/Service/AttachmentService.php +++ b/lib/Service/AttachmentService.php @@ -32,6 +32,7 @@ class AttachmentService { private array $services = []; public function __construct( + private readonly ConfigService $configService, private readonly AttachmentMapper $attachmentMapper, private readonly CardMapper $cardMapper, private readonly IUserManager $userManager, @@ -77,7 +78,8 @@ public function findAll(int $cardId, bool $withDeleted = false): array { $attachments = $this->attachmentMapper->findAll($cardId); if ($withDeleted) { - $attachments = array_merge($attachments, $this->attachmentMapper->findToDelete($cardId, false)); + $timeLimit = time() - $this->configService->getTrashRetention(); + $attachments = array_merge($attachments, $this->attachmentMapper->findToDelete($timeLimit, $cardId, false)); } foreach (array_keys($this->services) as $attachmentType) { diff --git a/lib/Service/ConfigService.php b/lib/Service/ConfigService.php index b4afb8fc45..1dfa60d4ef 100644 --- a/lib/Service/ConfigService.php +++ b/lib/Service/ConfigService.php @@ -251,4 +251,10 @@ public function setAttachmentFolder(?string $userId, string $path): void { $this->config->setUserValue($userId ?? $this->getUserId(), 'deck', 'attachment_folder', $path); } + + public function getTrashRetention(): int { + $value = $this->config->getAppValue(Application::APP_ID, 'trashRetentionHours', '5'); + $hours = (int)$value > 0 ? (int)$value : 5; + return 60 * 60 * $hours; + } } diff --git a/tests/unit/Cron/DeleteCronTest.php b/tests/unit/Cron/DeleteCronTest.php index 73ab3a416a..9834798fc6 100644 --- a/tests/unit/Cron/DeleteCronTest.php +++ b/tests/unit/Cron/DeleteCronTest.php @@ -34,6 +34,7 @@ use OCA\Deck\Db\StackMapper; use OCA\Deck\InvalidAttachmentType; use OCA\Deck\Service\AttachmentService; +use OCA\Deck\Service\ConfigService; use OCA\Deck\Service\IAttachmentService; use OCA\Deck\Sharing\DeckShareProvider; use OCP\AppFramework\Utility\ITimeFactory; @@ -44,6 +45,8 @@ class DeleteCronTest extends TestCase { /** @var ITimeFactory|MockObject */ private $timeFactory; + /** @var ConfigService|MockObject */ + private $configService; /** @var BoardMapper|MockObject */ protected $boardMapper; /** @var CardMapper|\PHPUnit\Framework\MockObject\MockObject */ @@ -62,6 +65,7 @@ class DeleteCronTest extends TestCase { public function setUp(): void { parent::setUp(); $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->configService = $this->createMock(ConfigService::class); $this->boardMapper = $this->createMock(BoardMapper::class); $this->cardMapper = $this->createMock(CardMapper::class); $this->attachmentService = $this->createMock(AttachmentService::class); @@ -70,6 +74,7 @@ public function setUp(): void { $this->deckShareProvider = $this->createMock(DeckShareProvider::class); $this->deleteCron = new DeleteCron( $this->timeFactory, + $this->configService, $this->boardMapper, $this->cardMapper, $this->attachmentService, diff --git a/tests/unit/Db/AttachmentMapperTest.php b/tests/unit/Db/AttachmentMapperTest.php index 017aefb5d3..a7d50e30b4 100644 --- a/tests/unit/Db/AttachmentMapperTest.php +++ b/tests/unit/Db/AttachmentMapperTest.php @@ -110,8 +110,9 @@ public function testFindToDelete() { $attachment->resetUpdatedFields(); } - $this->assertEquals([$attachmentsToDelete[0]], $this->attachmentMapper->findToDelete(1)); - $this->assertEquals([$attachmentsToDelete[2]], $this->attachmentMapper->findToDelete(2)); + $timeLimit = time() - (60 * 60 * 5); + $this->assertEquals([$attachmentsToDelete[0]], $this->attachmentMapper->findToDelete($timeLimit, 1)); + $this->assertEquals([$attachmentsToDelete[2]], $this->attachmentMapper->findToDelete($timeLimit, 2)); } public function testIsOwner() { diff --git a/tests/unit/Db/BoardMapperTest.php b/tests/unit/Db/BoardMapperTest.php index afb5aff936..6b272a4c59 100644 --- a/tests/unit/Db/BoardMapperTest.php +++ b/tests/unit/Db/BoardMapperTest.php @@ -154,7 +154,8 @@ public function testFindAllToDelete() { $this->boards[0]->setDeletedAt(1); $this->boards[0] = $this->boardMapper->update($this->boards[0]); - $actual = $this->boardMapper->findToDelete(); + $timeLimit = time() - (60 * 60 * 5); + $actual = $this->boardMapper->findToDelete($timeLimit); $this->boards[0]->resetUpdatedFields(); $filteredActual = array_values(array_filter($actual, function ($board) { diff --git a/tests/unit/Service/AttachmentServiceTest.php b/tests/unit/Service/AttachmentServiceTest.php index 89fef6d5f4..c7c940c9bf 100644 --- a/tests/unit/Service/AttachmentServiceTest.php +++ b/tests/unit/Service/AttachmentServiceTest.php @@ -67,6 +67,8 @@ class AttachmentServiceTest extends TestCase { /** @var IUserManager|MockObject */ private $userManager; + /** @var ConfigService */ + private $configService; /** @var AttachmentMapper|MockObject */ private $attachmentMapper; /** @var CardMapper|MockObject */ @@ -106,6 +108,8 @@ public function setUp(): void { $this->appContainer = $this->createMock(ContainerInterface::class); + $this->configService = $this->createMock(ConfigService::class); + $this->userManager = $this->createMock(IUserManager::class); $this->attachmentMapper = $this->createMock(AttachmentMapper::class); $this->cardMapper = $this->createMock(CardMapper::class); @@ -130,6 +134,7 @@ public function setUp(): void { $this->attachmentServiceValidator = $this->createMock(AttachmentServiceValidator::class); $this->attachmentService = new AttachmentService( + $this->configService, $this->attachmentMapper, $this->cardMapper, $this->userManager, @@ -166,7 +171,7 @@ public function testRegisterAttachmentService() { $application->expects($this->any()) ->method('getContainer') ->willReturn($appContainer); - $attachmentService = new AttachmentService($this->attachmentMapper, $this->cardMapper, $this->userManager, $this->changeHelper, $this->permissionService, $application, $this->attachmentCacheHelper, $this->userId, $this->l10n, $this->activityManager, $this->attachmentServiceValidator); + $attachmentService = new AttachmentService($this->configService, $this->attachmentMapper, $this->cardMapper, $this->userManager, $this->changeHelper, $this->permissionService, $application, $this->attachmentCacheHelper, $this->userId, $this->l10n, $this->activityManager, $this->attachmentServiceValidator); $attachmentService->registerAttachmentService('custom', MyAttachmentService::class); $this->assertEquals($fileServiceMock, $attachmentService->getService('deck_file')); $this->assertEquals(MyAttachmentService::class, get_class($attachmentService->getService('custom'))); @@ -191,7 +196,7 @@ public function testRegisterAttachmentServiceNotExisting() { ->method('getContainer') ->willReturn($appContainer); - $attachmentService = new AttachmentService($this->attachmentMapper, $this->cardMapper, $this->userManager, $this->changeHelper, $this->permissionService, $application, $this->attachmentCacheHelper, $this->userId, $this->l10n, $this->activityManager, $this->attachmentServiceValidator); + $attachmentService = new AttachmentService($this->configService, $this->attachmentMapper, $this->cardMapper, $this->userManager, $this->changeHelper, $this->permissionService, $application, $this->attachmentCacheHelper, $this->userId, $this->l10n, $this->activityManager, $this->attachmentServiceValidator); $attachmentService->registerAttachmentService('custom', MyAttachmentService::class); $attachmentService->getService('deck_file_invalid'); } @@ -252,9 +257,12 @@ public function testFindAllWithDeleted() { ->method('findAll') ->with(123) ->willReturn($attachments); + $this->configService->expects($this->once()) + ->method('getTrashRetention') + ->willReturn(3600); $this->attachmentMapper->expects($this->once()) ->method('findToDelete') - ->with(123, false) + ->with($this->anything(), 123, false) ->willReturn($attachmentsDeleted); $this->attachmentServiceImpl->expects($this->exactly(4)) diff --git a/tests/unit/Service/BatchQueryPerformanceTest.php b/tests/unit/Service/BatchQueryPerformanceTest.php index d35656aab3..08239a9bc3 100644 --- a/tests/unit/Service/BatchQueryPerformanceTest.php +++ b/tests/unit/Service/BatchQueryPerformanceTest.php @@ -332,6 +332,7 @@ private function setUpAttachmentService(): void { $application->method('getContainer')->willReturn($appContainer); $this->attachmentService = new AttachmentService( + $this->createMock(ConfigService::class), $this->attachmentMapper, $this->createMock(CardMapper::class), $this->createMock(IUserManager::class), diff --git a/tests/unit/Service/Importer/BoardImportServiceTest.php b/tests/unit/Service/Importer/BoardImportServiceTest.php index 6d13d59f59..5941a855bc 100644 --- a/tests/unit/Service/Importer/BoardImportServiceTest.php +++ b/tests/unit/Service/Importer/BoardImportServiceTest.php @@ -30,6 +30,7 @@ use OCA\Deck\Db\Assignment; use OCA\Deck\Db\AssignmentMapper; use OCA\Deck\Db\AttachmentMapper; +use OCA\Deck\Db\Board; use OCA\Deck\Db\BoardMapper; use OCA\Deck\Db\Card; use OCA\Deck\Db\CardMapper; @@ -157,6 +158,12 @@ public function testImportSuccess() { $this->userManager->method('userExists') ->willReturn(true); + $board = new Board(); + $board->setOwner('admin'); + $this->trelloJsonService + ->method('getBoard') + ->willReturn($board); + $this->boardMapper ->expects($this->once()) ->method('insert'); diff --git a/tests/unit/Service/PermissionServiceTest.php b/tests/unit/Service/PermissionServiceTest.php index 3099b9edd2..e324e1d3e4 100644 --- a/tests/unit/Service/PermissionServiceTest.php +++ b/tests/unit/Service/PermissionServiceTest.php @@ -363,7 +363,7 @@ public function testFindUsers() { $this->userManager->expects($this->any()) ->method('userExists') ->withConsecutive(['user1'], ['user2']) - ->willReturnOnConsecutiveCalls($user1, $user2); + ->willReturnOnConsecutiveCalls(true, true); $group = $this->createMock(IGroup::class); $group->expects($this->once())