Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix unit tests
Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc committed Apr 4, 2022
commit 4db523c3faffe2e0503fd72e18cb7a5faba31926
8 changes: 3 additions & 5 deletions apps/user_ldap/lib/Access.php
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ private function combineFilter($filters, $operator) {
* @param string $search the search term
* @return string the final filter part to use in LDAP searches
*/
public function getFilterPartForUserSearch($search) {
public function getFilterPartForUserSearch($search): string {
return $this->getFilterPartForSearch($search,
$this->connection->ldapAttributesForUserSearch,
$this->connection->ldapUserDisplayName);
Expand All @@ -1538,7 +1538,7 @@ public function getFilterPartForUserSearch($search) {
* @param string $search the search term
* @return string the final filter part to use in LDAP searches
*/
public function getFilterPartForGroupSearch($search) {
public function getFilterPartForGroupSearch($search): string {
return $this->getFilterPartForSearch($search,
$this->connection->ldapAttributesForGroupSearch,
$this->connection->ldapGroupDisplayName);
Expand Down Expand Up @@ -1632,10 +1632,8 @@ private function prepareSearchTerm($term) {

/**
* returns the filter used for counting users
*
* @return string
*/
public function getFilterForUserCount() {
public function getFilterForUserCount(): string {
$filter = $this->combineFilterWithAnd([
$this->connection->ldapUserFilter,
$this->connection->ldapUserDisplayName . '=*'
Expand Down
38 changes: 31 additions & 7 deletions apps/user_ldap/lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,25 @@
*/
class Connection extends LDAPUtility {
private $ldapConnectionRes = null;

/**
* @var string
*/
private $configPrefix;

/**
* @var ?string
*/
private $configID;

/**
* @var bool
*/
private $configured = false;
//whether connection should be kept on __destruct

/**
* @var bool whether connection should be kept on __destruct
*/
private $dontDestruct = false;

/**
Expand All @@ -91,33 +106,42 @@ class Connection extends LDAPUtility {
*/
public $hasGidNumber = true;

//cache handler
protected $cache;
/**
* @var \OCP\ICache|null
*/
protected $cache = null;

/** @var Configuration settings handler **/
protected $configuration;

/**
* @var bool
*/
protected $doNotValidate = false;

/**
* @var bool
*/
protected $ignoreValidation = false;

/**
* @var array{dn?: mixed, hash?: string, result?: bool}
*/
protected $bindResult = [];

/** @var LoggerInterface */
protected $logger;

/**
* Constructor
* @param ILDAPWrapper $ldap
* @param string $configPrefix a string with the prefix for the configkey column (appconfig table)
* @param string|null $configID a string with the value for the appid column (appconfig table) or null for on-the-fly connections
*/
public function __construct(ILDAPWrapper $ldap, $configPrefix = '', $configID = 'user_ldap') {
public function __construct(ILDAPWrapper $ldap, string $configPrefix = '', ?string $configID = 'user_ldap') {
parent::__construct($ldap);
$this->configPrefix = $configPrefix;
$this->configID = $configID;
$this->configuration = new Configuration($configPrefix,
!is_null($configID));
$this->configuration = new Configuration($configPrefix, !is_null($configID));
$memcache = \OC::$server->getMemCacheFactory();
if ($memcache->isAvailable()) {
$this->cache = $memcache->createDistributed();
Expand Down
2 changes: 1 addition & 1 deletion apps/user_ldap/lib/Group_LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,7 @@ public function getDisplayName(string $gid): string {
$this->access->groupname2dn($gid),
$this->access->connection->ldapGroupDisplayName);

if ($displayName && (count($displayName) > 0)) {
if (($displayName !== false) && (count($displayName) > 0)) {
$displayName = $displayName[0];
$this->access->connection->writeToCache($cacheKey, $displayName);
return $displayName;
Expand Down
4 changes: 2 additions & 2 deletions apps/user_ldap/lib/ILDAPWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ public function modReplace($link, $userDN, $password);
/**
* Sets the value of the specified option to be $value
* @param resource $link LDAP link resource
* @param string $option a defined LDAP Server option
* @param int $value the new value for the option
* @param int $option a defined LDAP Server option
* @param mixed $value the new value for the option
* @return bool true on success, false otherwise
*/
public function setOption($link, $option, $value);
Expand Down
7 changes: 3 additions & 4 deletions apps/user_ldap/lib/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ public function getUsername() {

/**
* returns the home directory of the user if specified by LDAP settings
* @param string $valueFromLDAP
* @return bool|string
* @param ?string $valueFromLDAP
* @return false|string
* @throws \Exception
*/
public function getHomePath($valueFromLDAP = null) {
Expand All @@ -286,8 +286,7 @@ public function getHomePath($valueFromLDAP = null) {
&& strpos($this->access->connection->homeFolderNamingRule, 'attr:') === 0
&& $this->access->connection->homeFolderNamingRule !== 'attr:') {
$attr = substr($this->access->connection->homeFolderNamingRule, strlen('attr:'));
$homedir = $this->access->readAttribute(
$this->access->username2dn($this->getUsername()), $attr);
$homedir = $this->access->readAttribute($this->access->username2dn($this->getUsername()), $attr);
if ($homedir && isset($homedir[0])) {
$path = $homedir[0];
}
Expand Down
6 changes: 3 additions & 3 deletions apps/user_ldap/tests/AccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected function setUp(): void {
private function getConnectorAndLdapMock() {
$lw = $this->createMock(ILDAPWrapper::class);
$connector = $this->getMockBuilder(Connection::class)
->setConstructorArgs([$lw, null, null])
->setConstructorArgs([$lw, '', null])
->getMock();
$um = $this->getMockBuilder(Manager::class)
->setConstructorArgs([
Expand Down Expand Up @@ -495,7 +495,7 @@ public function testSetPasswordWithRejectedChange() {
->willReturn(true);
$connection = $this->createMock(LDAP::class);
$this->connection
->expects($this->once())
->expects($this->any())
->method('getConnectionResource')
->willReturn($connection);
$this->ldap
Expand All @@ -519,7 +519,7 @@ public function testSetPassword() {
->willReturn(true);
$connection = $this->createMock(LDAP::class);
$this->connection
->expects($this->once())
->expects($this->any())
->method('getConnectionResource')
->willReturn($connection);
$this->ldap
Expand Down
34 changes: 20 additions & 14 deletions apps/user_ldap/tests/Group_LDAPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,12 @@ private function getAccessMock() {
$lw = $this->createMock(ILDAPWrapper::class);
$connector = $this->getMockBuilder(Connection::class)
->setMethods($conMethods)
->setConstructorArgs([$lw, null, null])
->setConstructorArgs([$lw, '', null])
->getMock();

$access = $this->createMock(Access::class);

$access->expects($this->any())
->method('getConnection')
->willReturn($connector);
$access->connection = $connector;

$access->userManager = $this->createMock(Manager::class);

Expand All @@ -133,6 +131,8 @@ private function enableGroups(Access $access) {
->willReturnCallback(function ($name) {
if ($name === 'ldapDynamicGroupMemberURL') {
return '';
} elseif ($name === 'ldapBaseGroups') {
return [];
}
return 1;
});
Expand Down Expand Up @@ -953,6 +953,8 @@ public function testGetGroupsByMember(bool $nestedGroups) {
return 'member';
case 'ldapGroupFilter':
return $groupFilter;
case 'ldapBaseGroups':
return [];
}
return 1;
});
Expand Down Expand Up @@ -1321,16 +1323,16 @@ public function testGroupMembers($groupDN, $expectedMembers, $groupsInfo = null)
});

$access->connection = $this->createMock(Connection::class);
if (count($groupsInfo) > 1) {
$access->connection->expects($this->any())
->method('__get')
->willReturnCallback(function ($name) {
if ($name === 'ldapNestedGroups') {
return 1;
}
return null;
});
}
$access->connection->expects($this->any())
->method('__get')
->willReturnCallback(function ($name) {
if ($name === 'ldapNestedGroups') {
return 1;
} elseif ($name === 'ldapGroupMemberAssocAttr') {
return 'attr';
}
return null;
});

/** @var GroupPluginManager $pluginManager */
$pluginManager = $this->createMock(GroupPluginManager::class);
Expand Down Expand Up @@ -1373,6 +1375,10 @@ public function testGetDisplayName(string $expected, $ldapResult) {
return null;
});

$access->expects($this->any())
->method('groupname2dn')
->willReturn('fakedn');

/** @var GroupPluginManager $pluginManager */
$pluginManager = $this->createMock(GroupPluginManager::class);

Expand Down
8 changes: 8 additions & 0 deletions apps/user_ldap/tests/User/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,10 @@ public function testGetHomePathConfiguredNotAvailableAllowed() {
->method('readAttribute')
->willReturn(false);

$this->access->expects($this->once())
->method('username2dn')
->willReturn($this->dn);

// asks for "enforce_home_folder_naming_rule"
$this->config->expects($this->once())
->method('getAppValue')
Expand All @@ -1038,6 +1042,10 @@ public function testGetHomePathConfiguredNotAvailableNotAllowed() {
->method('readAttribute')
->willReturn(false);

$this->access->expects($this->once())
->method('username2dn')
->willReturn($this->dn);

// asks for "enforce_home_folder_naming_rule"
$this->config->expects($this->once())
->method('getAppValue')
Expand Down
16 changes: 9 additions & 7 deletions apps/user_ldap/tests/User_LDAPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -815,13 +815,15 @@ public function testGetHomeWithPlugin() {

private function prepareAccessForGetDisplayName() {
$this->connection->expects($this->any())
->method('__get')
->willReturnCallback(function ($name) {
if ($name === 'ldapUserDisplayName') {
return 'displayname';
}
return null;
});
->method('__get')
->willReturnCallback(function ($name) {
if ($name === 'ldapUserDisplayName') {
return 'displayname';
} elseif ($name === 'ldapUserDisplayName2') {
return 'displayname2';
}
return null;
});

$this->access->expects($this->any())
->method('readAttribute')
Expand Down
2 changes: 1 addition & 1 deletion apps/user_ldap/tests/WizardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private function getWizardAndMocks() {
/** @var Configuration|\PHPUnit\Framework\MockObject\MockObject $conf */
$conf = $this->getMockBuilder(Configuration::class)
->setMethods($confMethods)
->setConstructorArgs([$lw, null, null])
->setConstructorArgs(['', true])
->getMock();

/** @var Access|\PHPUnit\Framework\MockObject\MockObject $access */
Expand Down