Skip to content

Commit 134f213

Browse files
committed
Add configuration flag to disable the background job for files_trashbin
Signed-off-by: Daniel Kesselberg <[email protected]>
1 parent 339c6ed commit 134f213

File tree

2 files changed

+65
-14
lines changed

2 files changed

+65
-14
lines changed

apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,20 @@
3030
use OCA\Files_Trashbin\Expiration;
3131
use OCA\Files_Trashbin\Helper;
3232
use OCA\Files_Trashbin\Trashbin;
33+
use OCP\IConfig;
3334
use OCP\IUser;
3435
use OCP\IUserManager;
3536

3637
class ExpireTrash extends \OC\BackgroundJob\TimedJob {
3738

39+
/** @var IConfig */
40+
private $config;
41+
3842
/**
3943
* @var Expiration
4044
*/
4145
private $expiration;
42-
46+
4347
/**
4448
* @var IUserManager
4549
*/
@@ -49,14 +53,16 @@ class ExpireTrash extends \OC\BackgroundJob\TimedJob {
4953
* @param IUserManager|null $userManager
5054
* @param Expiration|null $expiration
5155
*/
52-
public function __construct(IUserManager $userManager = null,
56+
public function __construct(IConfig $config = null,
57+
IUserManager $userManager = null,
5358
Expiration $expiration = null) {
5459
// Run once per 30 minutes
5560
$this->setInterval(60 * 30);
5661

57-
if (is_null($expiration) || is_null($userManager)) {
62+
if ($config === null || is_null($expiration) || is_null($userManager)) {
5863
$this->fixDIForJobs();
5964
} else {
65+
$this->config = $config;
6066
$this->userManager = $userManager;
6167
$this->expiration = $expiration;
6268
}
@@ -65,6 +71,7 @@ public function __construct(IUserManager $userManager = null,
6571
protected function fixDIForJobs() {
6672
/** @var Application $application */
6773
$application = \OC::$server->query(Application::class);
74+
$this->config = $application->getContainer()->get(IConfig::class);
6875
$this->userManager = \OC::$server->getUserManager();
6976
$this->expiration = $application->getContainer()->query('Expiration');
7077
}
@@ -74,6 +81,11 @@ protected function fixDIForJobs() {
7481
* @throws \Exception
7582
*/
7683
protected function run($argument) {
84+
$backgroundJob = $this->config->getAppValue('files_trashbin', 'background_job_expire_trash', 'yes');
85+
if ($backgroundJob === 'no') {
86+
return;
87+
}
88+
7789
$maxAge = $this->expiration->getMaxAgeAsTimestamp();
7890
if (!$maxAge) {
7991
return;
@@ -87,7 +99,7 @@ protected function run($argument) {
8799
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
88100
Trashbin::deleteExpiredFiles($dirContent, $uid);
89101
});
90-
102+
91103
\OC_Util::tearDownFS();
92104
}
93105

apps/files_trashbin/tests/BackgroundJob/ExpireTrashTest.php

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,62 @@
2222
* along with this program. If not, see <http://www.gnu.org/licenses/>
2323
*
2424
*/
25+
2526
namespace OCA\Files_Trashbin\Tests\BackgroundJob;
2627

2728
use OCA\Files_Trashbin\BackgroundJob\ExpireTrash;
29+
use OCA\Files_Trashbin\Expiration;
2830
use OCP\BackgroundJob\IJobList;
31+
use OCP\IConfig;
32+
use OCP\ILogger;
2933
use OCP\IUserManager;
34+
use PHPUnit\Framework\MockObject\MockObject;
35+
use Test\TestCase;
36+
37+
class ExpireTrashTest extends TestCase {
38+
/** @var IConfig|MockObject */
39+
private $config;
40+
41+
/** @var IUserManager|MockObject */
42+
private $userManager;
43+
44+
/** @var Expiration|MockObject */
45+
private $expiration;
46+
47+
/** @var IJobList|MockObject */
48+
private $jobList;
49+
50+
/** @var ILogger|MockObject */
51+
private $logger;
3052

31-
class ExpireTrashTest extends \Test\TestCase {
32-
public function testConstructAndRun() {
33-
$backgroundJob = new ExpireTrash(
34-
$this->createMock(IUserManager::class),
35-
$this->getMockBuilder('OCA\Files_Trashbin\Expiration')->disableOriginalConstructor()->getMock()
36-
);
53+
protected function setUp(): void {
54+
parent::setUp();
55+
56+
$this->config = $this->createMock(IConfig::class);
57+
$this->userManager = $this->createMock(IUserManager::class);
58+
$this->expiration = $this->createMock(Expiration::class);
59+
$this->jobList = $this->createMock(IJobList::class);
60+
$this->logger = $this->createMock(ILogger::class);
61+
62+
$this->jobList->expects($this->once())
63+
->method('setLastRun');
64+
$this->jobList->expects($this->once())
65+
->method('setExecutionTime');
66+
}
67+
68+
public function testConstructAndRun(): void {
69+
$job = new ExpireTrash($this->config, $this->userManager, $this->expiration);
70+
$job->execute($this->jobList, $this->logger);
71+
}
3772

38-
$jobList = $this->createMock(IJobList::class);
73+
public function testBackgroundJobDeactivated(): void {
74+
$this->config->method('getAppValue')
75+
->with('files_trashbin', 'background_job_expire_trash', 'yes')
76+
->willReturn('no');
77+
$this->expiration->expects($this->never())
78+
->method('getMaxAgeAsTimestamp');
3979

40-
/** @var \OC\BackgroundJob\JobList $jobList */
41-
$backgroundJob->execute($jobList);
42-
$this->addToAssertionCount(1);
80+
$job = new ExpireTrash($this->config, $this->userManager, $this->expiration);
81+
$job->execute($this->jobList, $this->logger);
4382
}
4483
}

0 commit comments

Comments
 (0)