add Sync test
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
parent
fc6b3902af
commit
8113f26eed
|
@ -34,11 +34,12 @@ use OCA\User_LDAP\LDAP;
|
||||||
use OCA\User_LDAP\LogWrapper;
|
use OCA\User_LDAP\LogWrapper;
|
||||||
use OCA\User_LDAP\Mapping\UserMapping;
|
use OCA\User_LDAP\Mapping\UserMapping;
|
||||||
use OCA\User_LDAP\User\Manager;
|
use OCA\User_LDAP\User\Manager;
|
||||||
use OCA\User_LDAP\User_LDAP;
|
|
||||||
use OCP\Image;
|
use OCP\Image;
|
||||||
use OCP\IServerContainer;
|
use OCP\IServerContainer;
|
||||||
|
|
||||||
class Sync extends TimedJob {
|
class Sync extends TimedJob {
|
||||||
|
const MAX_INTERVAL = 12 * 60 * 60; // 12h
|
||||||
|
const MIN_INTERVAL = 30 * 60; // 30min
|
||||||
/** @var IServerContainer */
|
/** @var IServerContainer */
|
||||||
protected $c;
|
protected $c;
|
||||||
/** @var Helper */
|
/** @var Helper */
|
||||||
|
@ -49,17 +50,13 @@ class Sync extends TimedJob {
|
||||||
protected $userManager;
|
protected $userManager;
|
||||||
/** @var UserMapping */
|
/** @var UserMapping */
|
||||||
protected $mapper;
|
protected $mapper;
|
||||||
/** @var int */
|
|
||||||
protected $maxInterval = 12 * 60 * 60; // 12h
|
|
||||||
/** @var int */
|
|
||||||
protected $minInterval = 30 * 60; // 30min
|
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->setInterval(
|
$this->setInterval(
|
||||||
\OC::$server->getConfig()->getAppValue(
|
\OC::$server->getConfig()->getAppValue(
|
||||||
'user_ldap',
|
'user_ldap',
|
||||||
'background_sync_interval',
|
'background_sync_interval',
|
||||||
$this->minInterval
|
self::MIN_INTERVAL
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -75,9 +72,10 @@ class Sync extends TimedJob {
|
||||||
$minPagingSize = $this->getMinPagingSize();
|
$minPagingSize = $this->getMinPagingSize();
|
||||||
$mappedUsers = $this->mapper->count();
|
$mappedUsers = $this->mapper->count();
|
||||||
|
|
||||||
$runsPerDay = ($minPagingSize === 0) ? $this->maxInterval : $mappedUsers / $minPagingSize;
|
$runsPerDay = ($minPagingSize === 0 || $mappedUsers === 0) ? self::MAX_INTERVAL
|
||||||
|
: $mappedUsers / $minPagingSize;
|
||||||
$interval = floor(24 * 60 * 60 / $runsPerDay);
|
$interval = floor(24 * 60 * 60 / $runsPerDay);
|
||||||
$interval = min(max($interval, $this->minInterval), $this->maxInterval);
|
$interval = min(max($interval, self::MIN_INTERVAL), self::MAX_INTERVAL);
|
||||||
|
|
||||||
$this->c->getConfig()->setAppValue('user_ldap', 'background_sync_interval', $interval);
|
$this->c->getConfig()->setAppValue('user_ldap', 'background_sync_interval', $interval);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,130 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||||
|
*
|
||||||
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||||
|
*
|
||||||
|
* @license GNU AGPL version 3 or any later version
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace OCA\User_LDAP\Tests\Jobs;
|
||||||
|
|
||||||
|
use OCA\User_LDAP\Helper;
|
||||||
|
use OCA\User_LDAP\Jobs\Sync;
|
||||||
|
use OCA\User_LDAP\LDAP;
|
||||||
|
use OCA\User_LDAP\Mapping\UserMapping;
|
||||||
|
use OCA\User_LDAP\User\Manager;
|
||||||
|
use OCP\IConfig;
|
||||||
|
use OCP\IServerContainer;
|
||||||
|
use Test\TestCase;
|
||||||
|
|
||||||
|
class SyncTest extends TestCase {
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
protected $arguments;
|
||||||
|
/** @var IServerContainer|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
|
protected $c;
|
||||||
|
/** @var Helper|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
|
protected $helper;
|
||||||
|
/** @var LDAP|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
|
protected $ldapWrapper;
|
||||||
|
/** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
|
protected $userManager;
|
||||||
|
/** @var UserMapping|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
|
protected $mapper;
|
||||||
|
/** @var Sync */
|
||||||
|
protected $sync;
|
||||||
|
|
||||||
|
public function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->c = $this->createMock(IServerContainer::class);
|
||||||
|
$this->helper = $this->createMock(Helper::class);
|
||||||
|
$this->ldapWrapper = $this->createMock(LDAP::class);
|
||||||
|
$this->userManager = $this->createMock(Manager::class);
|
||||||
|
$this->mapper = $this->createMock(UserMapping::class);
|
||||||
|
|
||||||
|
$this->arguments = [
|
||||||
|
'c' => $this->c,
|
||||||
|
'helper' => $this->helper,
|
||||||
|
'ldapWrapper' => $this->ldapWrapper,
|
||||||
|
'userManager' => $this->userManager,
|
||||||
|
'mapper' => $this->mapper,
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->sync = new Sync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function intervalDataProvider() {
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
0, 1000, 750
|
||||||
|
],
|
||||||
|
[
|
||||||
|
22, 0, 50
|
||||||
|
],
|
||||||
|
[
|
||||||
|
500, 500, 500
|
||||||
|
],
|
||||||
|
[
|
||||||
|
1357, 0, 0
|
||||||
|
],
|
||||||
|
[
|
||||||
|
421337, 2000, 3000
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider intervalDataProvider
|
||||||
|
*/
|
||||||
|
public function testUpdateInterval($userCount, $pagingSize1, $pagingSize2) {
|
||||||
|
$config = $this->createMock(IConfig::class);
|
||||||
|
$config->expects($this->once())
|
||||||
|
->method('setAppValue')
|
||||||
|
->with('user_ldap', 'background_sync_interval', $this->anything())
|
||||||
|
->willReturnCallback(function($a, $k, $interval) {
|
||||||
|
$this->assertTrue($interval >= SYNC::MIN_INTERVAL);
|
||||||
|
$this->assertTrue($interval <= SYNC::MAX_INTERVAL);
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
$config->expects($this->atLeastOnce())
|
||||||
|
->method('getAppKeys')
|
||||||
|
->willReturn([
|
||||||
|
'blabla',
|
||||||
|
'ldap_paging_size',
|
||||||
|
's07blabla',
|
||||||
|
'installed',
|
||||||
|
's07ldap_paging_size'
|
||||||
|
]);
|
||||||
|
$config->expects($this->exactly(2))
|
||||||
|
->method('getAppValue')
|
||||||
|
->willReturnOnConsecutiveCalls($pagingSize1, $pagingSize2);
|
||||||
|
|
||||||
|
$this->c->expects($this->any())
|
||||||
|
->method('getConfig')
|
||||||
|
->willReturn($config);
|
||||||
|
|
||||||
|
$this->mapper->expects($this->atLeastOnce())
|
||||||
|
->method('count')
|
||||||
|
->willReturn($userCount);
|
||||||
|
|
||||||
|
$this->sync->setArgument($this->arguments);
|
||||||
|
$this->sync->updateInterval();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue