Skip to content

Commit 559edd2

Browse files
authored
Merge pull request #29125 from nextcloud/bug/noid/disable-trash-expire-background-job
Add configuration flag to disable the background job for files_trashbin
2 parents fadeae8 + b0d1ad5 commit 559edd2

File tree

2 files changed

+65
-18
lines changed

2 files changed

+65
-18
lines changed

apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,35 @@
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
*/
4650
private $userManager;
4751

48-
/**
49-
* @param IUserManager|null $userManager
50-
* @param Expiration|null $expiration
51-
*/
52-
public function __construct(IUserManager $userManager = null,
52+
public function __construct(IConfig $config = null,
53+
IUserManager $userManager = null,
5354
Expiration $expiration = null) {
5455
// Run once per 30 minutes
5556
$this->setInterval(60 * 30);
5657

57-
if (is_null($expiration) || is_null($userManager)) {
58+
if ($config === null || $expiration === null || $userManager === null) {
5859
$this->fixDIForJobs();
5960
} else {
61+
$this->config = $config;
6062
$this->userManager = $userManager;
6163
$this->expiration = $expiration;
6264
}
@@ -65,6 +67,7 @@ public function __construct(IUserManager $userManager = null,
6567
protected function fixDIForJobs() {
6668
/** @var Application $application */
6769
$application = \OC::$server->query(Application::class);
70+
$this->config = $application->getContainer()->get(IConfig::class);
6871
$this->userManager = \OC::$server->getUserManager();
6972
$this->expiration = $application->getContainer()->query('Expiration');
7073
}
@@ -74,6 +77,11 @@ protected function fixDIForJobs() {
7477
* @throws \Exception
7578
*/
7679
protected function run($argument) {
80+
$backgroundJob = $this->config->getAppValue('files_trashbin', 'background_job_expire_trash', 'yes');
81+
if ($backgroundJob === 'no') {
82+
return;
83+
}
84+
7785
$maxAge = $this->expiration->getMaxAgeAsTimestamp();
7886
if (!$maxAge) {
7987
return;
@@ -87,7 +95,7 @@ protected function run($argument) {
8795
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
8896
Trashbin::deleteExpiredFiles($dirContent, $uid);
8997
});
90-
98+
9199
\OC_Util::tearDownFS();
92100
}
93101

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)