mirror of
https://github.com/nextcloud/server.git
synced 2026-04-21 06:08:46 -04:00
Merge pull request #29125 from nextcloud/bug/noid/disable-trash-expire-background-job
Add configuration flag to disable the background job for files_trashbin
This commit is contained in:
commit
559edd2dc9
2 changed files with 65 additions and 18 deletions
|
|
@ -30,33 +30,35 @@ use OCA\Files_Trashbin\AppInfo\Application;
|
|||
use OCA\Files_Trashbin\Expiration;
|
||||
use OCA\Files_Trashbin\Helper;
|
||||
use OCA\Files_Trashbin\Trashbin;
|
||||
use OCP\IConfig;
|
||||
use OCP\IUser;
|
||||
use OCP\IUserManager;
|
||||
|
||||
class ExpireTrash extends \OC\BackgroundJob\TimedJob {
|
||||
|
||||
/** @var IConfig */
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var Expiration
|
||||
*/
|
||||
private $expiration;
|
||||
|
||||
|
||||
/**
|
||||
* @var IUserManager
|
||||
*/
|
||||
private $userManager;
|
||||
|
||||
/**
|
||||
* @param IUserManager|null $userManager
|
||||
* @param Expiration|null $expiration
|
||||
*/
|
||||
public function __construct(IUserManager $userManager = null,
|
||||
public function __construct(IConfig $config = null,
|
||||
IUserManager $userManager = null,
|
||||
Expiration $expiration = null) {
|
||||
// Run once per 30 minutes
|
||||
$this->setInterval(60 * 30);
|
||||
|
||||
if (is_null($expiration) || is_null($userManager)) {
|
||||
if ($config === null || $expiration === null || $userManager === null) {
|
||||
$this->fixDIForJobs();
|
||||
} else {
|
||||
$this->config = $config;
|
||||
$this->userManager = $userManager;
|
||||
$this->expiration = $expiration;
|
||||
}
|
||||
|
|
@ -65,6 +67,7 @@ class ExpireTrash extends \OC\BackgroundJob\TimedJob {
|
|||
protected function fixDIForJobs() {
|
||||
/** @var Application $application */
|
||||
$application = \OC::$server->query(Application::class);
|
||||
$this->config = $application->getContainer()->get(IConfig::class);
|
||||
$this->userManager = \OC::$server->getUserManager();
|
||||
$this->expiration = $application->getContainer()->query('Expiration');
|
||||
}
|
||||
|
|
@ -74,6 +77,11 @@ class ExpireTrash extends \OC\BackgroundJob\TimedJob {
|
|||
* @throws \Exception
|
||||
*/
|
||||
protected function run($argument) {
|
||||
$backgroundJob = $this->config->getAppValue('files_trashbin', 'background_job_expire_trash', 'yes');
|
||||
if ($backgroundJob === 'no') {
|
||||
return;
|
||||
}
|
||||
|
||||
$maxAge = $this->expiration->getMaxAgeAsTimestamp();
|
||||
if (!$maxAge) {
|
||||
return;
|
||||
|
|
@ -87,7 +95,7 @@ class ExpireTrash extends \OC\BackgroundJob\TimedJob {
|
|||
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
|
||||
Trashbin::deleteExpiredFiles($dirContent, $uid);
|
||||
});
|
||||
|
||||
|
||||
\OC_Util::tearDownFS();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,23 +22,62 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Files_Trashbin\Tests\BackgroundJob;
|
||||
|
||||
use OCA\Files_Trashbin\BackgroundJob\ExpireTrash;
|
||||
use OCA\Files_Trashbin\Expiration;
|
||||
use OCP\BackgroundJob\IJobList;
|
||||
use OCP\IConfig;
|
||||
use OCP\ILogger;
|
||||
use OCP\IUserManager;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Test\TestCase;
|
||||
|
||||
class ExpireTrashTest extends \Test\TestCase {
|
||||
public function testConstructAndRun() {
|
||||
$backgroundJob = new ExpireTrash(
|
||||
$this->createMock(IUserManager::class),
|
||||
$this->getMockBuilder('OCA\Files_Trashbin\Expiration')->disableOriginalConstructor()->getMock()
|
||||
);
|
||||
class ExpireTrashTest extends TestCase {
|
||||
/** @var IConfig|MockObject */
|
||||
private $config;
|
||||
|
||||
$jobList = $this->createMock(IJobList::class);
|
||||
/** @var IUserManager|MockObject */
|
||||
private $userManager;
|
||||
|
||||
/** @var \OC\BackgroundJob\JobList $jobList */
|
||||
$backgroundJob->execute($jobList);
|
||||
$this->addToAssertionCount(1);
|
||||
/** @var Expiration|MockObject */
|
||||
private $expiration;
|
||||
|
||||
/** @var IJobList|MockObject */
|
||||
private $jobList;
|
||||
|
||||
/** @var ILogger|MockObject */
|
||||
private $logger;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->config = $this->createMock(IConfig::class);
|
||||
$this->userManager = $this->createMock(IUserManager::class);
|
||||
$this->expiration = $this->createMock(Expiration::class);
|
||||
$this->jobList = $this->createMock(IJobList::class);
|
||||
$this->logger = $this->createMock(ILogger::class);
|
||||
|
||||
$this->jobList->expects($this->once())
|
||||
->method('setLastRun');
|
||||
$this->jobList->expects($this->once())
|
||||
->method('setExecutionTime');
|
||||
}
|
||||
|
||||
public function testConstructAndRun(): void {
|
||||
$job = new ExpireTrash($this->config, $this->userManager, $this->expiration);
|
||||
$job->execute($this->jobList, $this->logger);
|
||||
}
|
||||
|
||||
public function testBackgroundJobDeactivated(): void {
|
||||
$this->config->method('getAppValue')
|
||||
->with('files_trashbin', 'background_job_expire_trash', 'yes')
|
||||
->willReturn('no');
|
||||
$this->expiration->expects($this->never())
|
||||
->method('getMaxAgeAsTimestamp');
|
||||
|
||||
$job = new ExpireTrash($this->config, $this->userManager, $this->expiration);
|
||||
$job->execute($this->jobList, $this->logger);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue