2014-08-21 19:59:13 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2014-08-21 19:59:13 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2016-05-12 18:14:59 +03:00
|
|
|
namespace OCA\User_LDAP\Tests\Jobs;
|
2014-08-21 19:59:13 +04:00
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
use Exception;
|
2016-09-02 12:02:12 +03:00
|
|
|
use OCA\User_LDAP\Helper;
|
2020-10-22 12:25:33 +03:00
|
|
|
use OCA\User_LDAP\Jobs\CleanUp;
|
|
|
|
use OCA\User_LDAP\User\DeletedUsersIndex;
|
|
|
|
use OCA\User_LDAP\User_Proxy;
|
2016-09-02 12:02:12 +03:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IDBConnection;
|
2020-10-22 12:25:33 +03:00
|
|
|
use Test\TestCase;
|
2016-09-02 12:02:12 +03:00
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
class CleanUpTest extends TestCase {
|
|
|
|
/** @var CleanUp */
|
|
|
|
protected $bgJob;
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
protected $mocks;
|
|
|
|
|
|
|
|
public function setUp(): void {
|
|
|
|
$this->createMocks();
|
2020-10-26 16:44:15 +03:00
|
|
|
$this->bgJob = new CleanUp($this->mocks['userBackend'], $this->mocks['deletedUsersIndex']);
|
2020-10-22 12:25:33 +03:00
|
|
|
$this->bgJob->setArguments($this->mocks);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function createMocks(): void {
|
|
|
|
$this->mocks = [];
|
|
|
|
$this->mocks['userBackend'] = $this->createMock(User_Proxy::class);
|
|
|
|
$this->mocks['deletedUsersIndex'] = $this->createMock(DeletedUsersIndex::class);
|
|
|
|
$this->mocks['ocConfig'] = $this->createMock(IConfig::class);
|
|
|
|
$this->mocks['db'] = $this->createMock(IDBConnection::class);
|
|
|
|
$this->mocks['helper'] = $this->createMock(Helper::class);
|
2014-08-21 19:59:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clean up job must not run when there are disabled configurations
|
|
|
|
*/
|
|
|
|
public function test_runNotAllowedByDisabledConfigurations() {
|
2020-10-22 12:25:33 +03:00
|
|
|
$this->mocks['helper']->expects($this->once())
|
2014-08-21 19:59:13 +04:00
|
|
|
->method('haveDisabledConfigurations')
|
2020-04-09 17:07:47 +03:00
|
|
|
->willReturn(true);
|
2014-08-21 19:59:13 +04:00
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
$this->mocks['ocConfig']->expects($this->never())
|
2014-08-21 19:59:13 +04:00
|
|
|
->method('getSystemValue');
|
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
$result = $this->bgJob->isCleanUpAllowed();
|
2014-08-21 19:59:13 +04:00
|
|
|
$this->assertSame(false, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clean up job must not run when LDAP Helper is broken i.e.
|
|
|
|
* returning unexpected results
|
|
|
|
*/
|
|
|
|
public function test_runNotAllowedByBrokenHelper() {
|
2020-10-22 12:25:33 +03:00
|
|
|
$this->mocks['helper']->expects($this->once())
|
2014-08-21 19:59:13 +04:00
|
|
|
->method('haveDisabledConfigurations')
|
2020-10-22 12:25:33 +03:00
|
|
|
->will($this->throwException(new Exception()));
|
2014-08-21 19:59:13 +04:00
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
$this->mocks['ocConfig']->expects($this->never())
|
2014-08-21 19:59:13 +04:00
|
|
|
->method('getSystemValue');
|
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
$result = $this->bgJob->isCleanUpAllowed();
|
2014-08-21 19:59:13 +04:00
|
|
|
$this->assertSame(false, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clean up job must not run when it is not enabled
|
|
|
|
*/
|
|
|
|
public function test_runNotAllowedBySysConfig() {
|
2020-10-22 12:25:33 +03:00
|
|
|
$this->mocks['helper']->expects($this->once())
|
2014-08-21 19:59:13 +04:00
|
|
|
->method('haveDisabledConfigurations')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2014-08-21 19:59:13 +04:00
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
$this->mocks['ocConfig']->expects($this->once())
|
2014-08-21 19:59:13 +04:00
|
|
|
->method('getSystemValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2014-08-21 19:59:13 +04:00
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
$result = $this->bgJob->isCleanUpAllowed();
|
2014-08-21 19:59:13 +04:00
|
|
|
$this->assertSame(false, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clean up job is allowed to run
|
|
|
|
*/
|
|
|
|
public function test_runIsAllowed() {
|
2020-10-22 12:25:33 +03:00
|
|
|
$this->mocks['helper']->expects($this->once())
|
2014-08-21 19:59:13 +04:00
|
|
|
->method('haveDisabledConfigurations')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2014-08-21 19:59:13 +04:00
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
$this->mocks['ocConfig']->expects($this->once())
|
2014-08-21 19:59:13 +04:00
|
|
|
->method('getSystemValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2014-08-21 19:59:13 +04:00
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
$result = $this->bgJob->isCleanUpAllowed();
|
2014-08-21 19:59:13 +04:00
|
|
|
$this->assertSame(true, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check whether offset will be reset when it needs to
|
|
|
|
*/
|
|
|
|
public function test_OffsetResetIsNecessary() {
|
2020-10-22 12:25:33 +03:00
|
|
|
$result = $this->bgJob->isOffsetResetNecessary($this->bgJob->getChunkSize() - 1);
|
2014-08-21 19:59:13 +04:00
|
|
|
$this->assertSame(true, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* make sure offset is not reset when it is not due
|
|
|
|
*/
|
|
|
|
public function test_OffsetResetIsNotNecessary() {
|
2020-10-22 12:25:33 +03:00
|
|
|
$result = $this->bgJob->isOffsetResetNecessary($this->bgJob->getChunkSize());
|
2014-08-21 19:59:13 +04:00
|
|
|
$this->assertSame(false, $result);
|
|
|
|
}
|
|
|
|
}
|