resolve DI

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2017-11-08 01:51:14 +01:00
parent 3ba8b337a4
commit 419759e68b
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
8 changed files with 115 additions and 80 deletions

View File

@ -64,9 +64,13 @@ $userManager = new \OCA\User_LDAP\User\Manager(
\OC::$server->getUserManager(),
\OC::$server->getNotificationManager());
$access = new \OCA\User_LDAP\Access($con, $ldapWrapper, $userManager, new \OCA\User_LDAP\Helper(
$access = new \OCA\User_LDAP\Access(
$con,
$ldapWrapper,
$userManager,
new \OCA\User_LDAP\Helper(\OC::$server->getConfig()),
\OC::$server->getConfig()
));
);
$wizard = new \OCA\User_LDAP\Wizard($configuration, $ldapWrapper, $access);

View File

@ -51,7 +51,7 @@ use OCA\User_LDAP\User\OfflineUser;
use OCA\User_LDAP\Mapping\AbstractMapping;
use OC\ServerNotAvailableException;
use OCP\IServerContainer;
use OCP\IConfig;
/**
* Class Access
@ -92,22 +92,22 @@ class Access extends LDAPUtility implements IUserTools {
* @var \OCA\User_LDAP\Helper
*/
private $helper;
/** @var IServerContainer */
private $c;
/** @var IConfig */
private $config;
public function __construct(
Connection $connection,
ILDAPWrapper $ldap,
Manager $userManager,
Helper $helper,
IServerContainer $c
IConfig $config
) {
parent::__construct($ldap);
$this->connection = $connection;
$this->userManager = $userManager;
$this->userManager->setLdapAccess($this);
$this->helper = $helper;
$this->c = $c;
$this->config = $config;
}
/**
@ -522,7 +522,7 @@ class Access extends LDAPUtility implements IUserTools {
* returns an internal Nextcloud name for the given LDAP DN, false on DN outside of search DN
*
* @param string $fdn the dn of the user object
* @param string $ldapName optional, the display name of the object
* @param string|null $ldapName optional, the display name of the object
* @param bool $isUser optional, whether it is a user object (otherwise group assumed)
* @param bool|null $newlyMapped
* @param array|null $record
@ -825,7 +825,7 @@ class Access extends LDAPUtility implements IUserTools {
$ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset);
$recordsToUpdate = $ldapRecords;
if(!$forceApplyAttributes) {
$isBackgroundJobModeAjax = $this->c->getConfig()
$isBackgroundJobModeAjax = $this->config
->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax';
$recordsToUpdate = array_filter($ldapRecords, function($record) use ($isBackgroundJobModeAjax) {
$newlyMapped = false;

View File

@ -34,14 +34,16 @@ use OCA\User_LDAP\LDAP;
use OCA\User_LDAP\LogWrapper;
use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\User\Manager;
use OCP\IAvatarManager;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Image;
use OCP\IServerContainer;
use OCP\IUserManager;
use OCP\Notification\IManager;
class Sync extends TimedJob {
const MAX_INTERVAL = 12 * 60 * 60; // 12h
const MIN_INTERVAL = 30 * 60; // 30min
/** @var IServerContainer */
protected $c;
/** @var Helper */
protected $ldapHelper;
/** @var LDAP */
@ -50,6 +52,16 @@ class Sync extends TimedJob {
protected $userManager;
/** @var UserMapping */
protected $mapper;
/** @var IConfig */
protected $config;
/** @var IAvatarManager */
protected $avatarManager;
/** @var IDBConnection */
protected $dbc;
/** @var IUserManager */
protected $ncUserManager;
/** @var IManager */
protected $notificationManager;
public function __construct() {
$this->setInterval(
@ -77,7 +89,7 @@ class Sync extends TimedJob {
$interval = floor(24 * 60 * 60 / $runsPerDay);
$interval = min(max($interval, self::MIN_INTERVAL), self::MAX_INTERVAL);
$this->c->getConfig()->setAppValue('user_ldap', 'background_sync_interval', $interval);
$this->config->setAppValue('user_ldap', 'background_sync_interval', $interval);
}
/**
@ -85,14 +97,13 @@ class Sync extends TimedJob {
* @return int
*/
protected function getMinPagingSize() {
$config = $this->c->getConfig();
$configKeys = $config->getAppKeys('user_ldap');
$configKeys = $this->config->getAppKeys('user_ldap');
$configKeys = array_filter($configKeys, function($key) {
return strpos($key, 'ldap_paging_size') !== false;
});
$minPagingSize = null;
foreach ($configKeys as $configKey) {
$pagingSize = $config->getAppValue('user_ldap', $configKey, $minPagingSize);
$pagingSize = $this->config->getAppValue('user_ldap', $configKey, $minPagingSize);
$minPagingSize = $minPagingSize === null ? $pagingSize : min($minPagingSize, $pagingSize);
}
return (int)$minPagingSize;
@ -104,7 +115,7 @@ class Sync extends TimedJob {
protected function run($argument) {
$this->setArgument($argument);
$isBackgroundJobModeAjax = $this->c->getConfig()
$isBackgroundJobModeAjax = $this->config
->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax';
if($isBackgroundJobModeAjax) {
return;
@ -143,7 +154,7 @@ class Sync extends TimedJob {
*/
public function runCycle($cycleData) {
$connection = new Connection($this->ldap, $cycleData['prefix']);
$access = new Access($connection, $this->ldap, $this->userManager, $this->ldapHelper, $this->c);
$access = new Access($connection, $this->ldap, $this->userManager, $this->ldapHelper, $this->config);
$access->setUserMapper($this->mapper);
$filter = $access->combineFilterWithAnd(array(
@ -177,10 +188,9 @@ class Sync extends TimedJob {
return null;
}
$config = $this->c->getConfig();
$cycleData = [
'prefix' => $config->getAppValue('user_ldap', 'background_sync_prefix', null),
'offset' => (int)$config->getAppValue('user_ldap', 'background_sync_offset', 0),
'prefix' => $this->config->getAppValue('user_ldap', 'background_sync_prefix', null),
'offset' => (int)$this->config->getAppValue('user_ldap', 'background_sync_offset', 0),
];
if(
@ -199,9 +209,8 @@ class Sync extends TimedJob {
* @param array $cycleData
*/
public function setCycle(array $cycleData) {
$config = $this->c->getConfig();
$config->setAppValue('user_ldap', 'background_sync_prefix', $cycleData['prefix']);
$config->setAppValue('user_ldap', 'background_sync_offset', $cycleData['offset']);
$this->config->setAppValue('user_ldap', 'background_sync_prefix', $cycleData['prefix']);
$this->config->setAppValue('user_ldap', 'background_sync_offset', $cycleData['offset']);
}
/**
@ -238,8 +247,7 @@ class Sync extends TimedJob {
* @return bool
*/
protected function qualifiesToRun($cycleData) {
$config = $this->c->getConfig();
$lastChange = $config->getAppValue('user_ldap', $cycleData['prefix'] . '_lastChange', 0);
$lastChange = $this->config->getAppValue('user_ldap', $cycleData['prefix'] . '_lastChange', 0);
if((time() - $lastChange) > 60 * 30) {
return true;
}
@ -288,16 +296,16 @@ class Sync extends TimedJob {
* @param array $argument
*/
public function setArgument($argument) {
if(isset($argument['c'])) {
$this->c = $argument['c'];
if(isset($argument['config'])) {
$this->config = $argument['config'];
} else {
$this->c = \OC::$server;
$this->config = \OC::$server->getConfig();
}
if(isset($argument['helper'])) {
$this->ldapHelper = $argument['helper'];
} else {
$this->ldapHelper = new Helper($this->c->getConfig());
$this->ldapHelper = new Helper($this->config);
}
if(isset($argument['ldapWrapper'])) {
@ -306,25 +314,49 @@ class Sync extends TimedJob {
$this->ldap = new LDAP();
}
if(isset($argument['avatarManager'])) {
$this->avatarManager = $argument['avatarManager'];
} else {
$this->avatarManager = \OC::$server->getAvatarManager();
}
if(isset($argument['dbc'])) {
$this->dbc = $argument['dbc'];
} else {
$this->dbc = \OC::$server->getDatabaseConnection();
}
if(isset($argument['ncUserManager'])) {
$this->ncUserManager = $argument['ncUserManager'];
} else {
$this->ncUserManager = \OC::$server->getUserManager();
}
if(isset($argument['notificationManager'])) {
$this->notificationManager = $argument['notificationManager'];
} else {
$this->notificationManager = \OC::$server->getNotificationManager();
}
if(isset($argument['userManager'])) {
$this->userManager = $argument['userManager'];
} else {
$this->userManager = new Manager(
$this->c->getConfig(),
$this->config,
new FilesystemHelper(),
new LogWrapper(),
$this->c->getAvatarManager(),
$this->avatarManager,
new Image(),
$this->c->getDatabaseConnection(),
$this->c->getUserManager(),
$this->c->getNotificationManager()
$this->dbc,
$this->ncUserManager,
$this->notificationManager
);
}
if(isset($argument['mapper'])) {
$this->mapper = $argument['mapper'];
} else {
$this->mapper = new UserMapping($this->c->getDatabaseConnection());
$this->mapper = new UserMapping($this->dbc);
}
}
}

View File

@ -192,7 +192,7 @@ class UpdateGroups extends \OC\BackgroundJob\TimedJob {
\OC::$server->getUserManager(),
\OC::$server->getNotificationManager());
$connector = new Connection($ldapWrapper, $configPrefixes[0]);
$ldapAccess = new Access($connector, $ldapWrapper, $userManager, $helper, \OC::$server);
$ldapAccess = new Access($connector, $ldapWrapper, $userManager, $helper, \OC::$server->getConfig());
$groupMapper = new GroupMapping($dbc);
$userMapper = new UserMapping($dbc);
$ldapAccess->setGroupMapper($groupMapper);

View File

@ -82,7 +82,7 @@ abstract class Proxy {
new Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(), $db,
$coreUserManager, $coreNotificationManager);
$connector = new Connection($this->ldap, $configPrefix);
$access = new Access($connector, $this->ldap, $userManager, new Helper(\OC::$server->getConfig()), \OC::$server);
$access = new Access($connector, $this->ldap, $userManager, new Helper($ocConfig), $ocConfig);
$access->setUserMapper($userMap);
$access->setGroupMapper($groupMap);
self::$accesses[$configPrefix] = $access;

View File

@ -48,7 +48,6 @@ use OCP\IAvatarManager;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Image;
use OCP\IServerContainer;
use OCP\IUserManager;
use OCP\Notification\IManager as INotificationManager;
use Test\TestCase;
@ -69,8 +68,8 @@ class AccessTest extends TestCase {
private $userManager;
/** @var Helper|\PHPUnit_Framework_MockObject_MockObject */
private $helper;
/** @var IServerContainer|\PHPUnit_Framework_MockObject_MockObject */
private $c;
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
private $config;
/** @var Access */
private $access;
@ -79,14 +78,14 @@ class AccessTest extends TestCase {
$this->ldap = $this->createMock(LDAP::class);
$this->userManager = $this->createMock(Manager::class);
$this->helper = $this->createMock(Helper::class);
$this->c = $this->createMock(IServerContainer::class);
$this->config = $this->createMock(IConfig::class);
$this->access = new Access(
$this->connection,
$this->ldap,
$this->userManager,
$this->helper,
$this->c
$this->config
);
}
@ -221,9 +220,9 @@ class AccessTest extends TestCase {
*/
public function testStringResemblesDN($case) {
list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock();
/** @var IServerContainer|\PHPUnit_Framework_MockObject_MockObject $c */
$c = $this->createMock(IServerContainer::class);
$access = new Access($con, $lw, $um, $helper, $c);
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject $config */
$config = $this->createMock(IConfig::class);
$access = new Access($con, $lw, $um, $helper, $config);
$lw->expects($this->exactly(1))
->method('explodeDN')
@ -243,10 +242,10 @@ class AccessTest extends TestCase {
*/
public function testStringResemblesDNLDAPmod($case) {
list(, $con, $um, $helper) = $this->getConnectorAndLdapMock();
/** @var IServerContainer|\PHPUnit_Framework_MockObject_MockObject $c */
$c = $this->createMock(IServerContainer::class);
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject $config */
$config = $this->createMock(IConfig::class);
$lw = new LDAP();
$access = new Access($con, $lw, $um, $helper, $c);
$access = new Access($con, $lw, $um, $helper, $config);
if(!function_exists('ldap_explode_dn')) {
$this->markTestSkipped('LDAP Module not available');
@ -312,10 +311,6 @@ class AccessTest extends TestCase {
->method('get')
->will($this->returnValue($userMock));
$this->c->expects($this->any())
->method('getConfig')
->willReturn($this->createMock(IConfig::class));
$this->access->batchApplyUserAttributes($data);
}
@ -357,10 +352,6 @@ class AccessTest extends TestCase {
->method('get')
->willReturn($this->createMock(User::class));
$this->c->expects($this->any())
->method('getConfig')
->willReturn($this->createMock(IConfig::class));
$this->access->batchApplyUserAttributes($data);
}
@ -402,12 +393,6 @@ class AccessTest extends TestCase {
->method('get')
->will($this->returnValue($userMock));
$configMock = $this->createMock(IConfig::class);
$this->c->expects($this->any())
->method('getConfig')
->willReturn($configMock);
$this->access->batchApplyUserAttributes($data);
}
@ -427,8 +412,8 @@ class AccessTest extends TestCase {
*/
public function testSanitizeDN($attribute) {
list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock();
/** @var IServerContainer|\PHPUnit_Framework_MockObject_MockObject $c */
$c = $this->createMock(IServerContainer::class);
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject $config */
$config = $this->createMock(IConfig::class);
$dnFromServer = 'cn=Mixed Cases,ou=Are Sufficient To,ou=Test,dc=example,dc=org';
@ -441,7 +426,7 @@ class AccessTest extends TestCase {
$attribute => array('count' => 1, $dnFromServer)
)));
$access = new Access($con, $lw, $um, $helper, $c);
$access = new Access($con, $lw, $um, $helper, $config);
$values = $access->readAttribute('uid=whoever,dc=example,dc=org', $attribute);
$this->assertSame($values[0], strtolower($dnFromServer));
}

View File

@ -140,7 +140,7 @@ abstract class AbstractIntegrationTest {
* initializes the Access test instance
*/
protected function initAccess() {
$this->access = new Access($this->connection, $this->ldap, $this->userManager, $this->helper, \OC::$server);
$this->access = new Access($this->connection, $this->ldap, $this->userManager, $this->helper, \OC::$server->getConfig());
}
/**

View File

@ -28,16 +28,17 @@ 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\IAvatarManager;
use OCP\IConfig;
use OCP\IServerContainer;
use OCP\IDBConnection;
use OCP\IUserManager;
use OCP\Notification\IManager;
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 */
@ -48,22 +49,40 @@ class SyncTest extends TestCase {
protected $mapper;
/** @var Sync */
protected $sync;
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
protected $config;
/** @var IAvatarManager|\PHPUnit_Framework_MockObject_MockObject */
protected $avatarManager;
/** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */
protected $dbc;
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
protected $ncUserManager;
/** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
protected $notificationManager;
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->config = $this->createMock(IConfig::class);
$this->avatarManager = $this->createMock(IAvatarManager::class);
$this->dbc = $this->createMock(IDBConnection::class);
$this->ncUserManager = $this->createMock(IUserManager::class);
$this->notificationManager = $this->createMock(IManager::class);
$this->arguments = [
'c' => $this->c,
'helper' => $this->helper,
'ldapWrapper' => $this->ldapWrapper,
'userManager' => $this->userManager,
'mapper' => $this->mapper,
'config' => $this->config,
'avatarManager' => $this->avatarManager,
'dbc' => $this->dbc,
'ncUserManager' => $this->ncUserManager,
'notificationManager' => $this->notificationManager,
];
$this->sync = new Sync();
@ -93,8 +112,7 @@ class SyncTest extends TestCase {
* @dataProvider intervalDataProvider
*/
public function testUpdateInterval($userCount, $pagingSize1, $pagingSize2) {
$config = $this->createMock(IConfig::class);
$config->expects($this->once())
$this->config->expects($this->once())
->method('setAppValue')
->with('user_ldap', 'background_sync_interval', $this->anything())
->willReturnCallback(function($a, $k, $interval) {
@ -102,7 +120,7 @@ class SyncTest extends TestCase {
$this->assertTrue($interval <= SYNC::MAX_INTERVAL);
return true;
});
$config->expects($this->atLeastOnce())
$this->config->expects($this->atLeastOnce())
->method('getAppKeys')
->willReturn([
'blabla',
@ -111,14 +129,10 @@ class SyncTest extends TestCase {
'installed',
's07ldap_paging_size'
]);
$config->expects($this->exactly(2))
$this->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);