aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCôme Chilliet <come.chilliet@nextcloud.com>2022-03-03 16:35:06 +0100
committerCôme Chilliet <come.chilliet@nextcloud.com>2022-03-03 16:35:06 +0100
commitdab5ea958a58e79b7f40b49bbe3af8839dc9c9d1 (patch)
treeba32d3a96807f5f126ce7e2fcf82d1c0796ead43
parent0c5bd588ed4528d46df244a686b6f91299766836 (diff)
downloadnextcloud-server-dab5ea958a58e79b7f40b49bbe3af8839dc9c9d1.tar.gz
nextcloud-server-dab5ea958a58e79b7f40b49bbe3af8839dc9c9d1.zip
Fix unit tests
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
-rw-r--r--apps/user_ldap/lib/Access.php8
-rw-r--r--apps/user_ldap/lib/Connection.php38
-rw-r--r--apps/user_ldap/lib/Group_LDAP.php2
-rw-r--r--apps/user_ldap/lib/ILDAPWrapper.php4
-rw-r--r--apps/user_ldap/lib/User/User.php7
-rw-r--r--apps/user_ldap/tests/AccessTest.php6
-rw-r--r--apps/user_ldap/tests/Group_LDAPTest.php34
-rw-r--r--apps/user_ldap/tests/User/UserTest.php8
-rw-r--r--apps/user_ldap/tests/User_LDAPTest.php16
-rw-r--r--apps/user_ldap/tests/WizardTest.php2
10 files changed, 81 insertions, 44 deletions
diff --git a/apps/user_ldap/lib/Access.php b/apps/user_ldap/lib/Access.php
index 028e01e2c41..daa5d343185 100644
--- a/apps/user_ldap/lib/Access.php
+++ b/apps/user_ldap/lib/Access.php
@@ -1467,7 +1467,7 @@ class Access extends LDAPUtility {
* @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);
@@ -1479,7 +1479,7 @@ class Access extends LDAPUtility {
* @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);
@@ -1571,10 +1571,8 @@ class Access extends LDAPUtility {
/**
* 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 . '=*'
diff --git a/apps/user_ldap/lib/Connection.php b/apps/user_ldap/lib/Connection.php
index fefa1f0e329..565fb415e58 100644
--- a/apps/user_ldap/lib/Connection.php
+++ b/apps/user_ldap/lib/Connection.php
@@ -78,10 +78,25 @@ class Connection extends LDAPUtility {
* @var resource|\LDAP\Connection|null
*/
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;
/**
@@ -94,16 +109,27 @@ 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 */
@@ -111,16 +137,14 @@ class Connection extends LDAPUtility {
/**
* 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();
diff --git a/apps/user_ldap/lib/Group_LDAP.php b/apps/user_ldap/lib/Group_LDAP.php
index 0f701f66b19..766b77bf521 100644
--- a/apps/user_ldap/lib/Group_LDAP.php
+++ b/apps/user_ldap/lib/Group_LDAP.php
@@ -1347,7 +1347,7 @@ class Group_LDAP extends BackendUtility implements GroupInterface, IGroupLDAP, I
$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;
diff --git a/apps/user_ldap/lib/ILDAPWrapper.php b/apps/user_ldap/lib/ILDAPWrapper.php
index 569f31315bc..e72d85ac2b9 100644
--- a/apps/user_ldap/lib/ILDAPWrapper.php
+++ b/apps/user_ldap/lib/ILDAPWrapper.php
@@ -178,8 +178,8 @@ interface ILDAPWrapper {
/**
* Sets the value of the specified option to be $value
* @param resource|\LDAP\Connection $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);
diff --git a/apps/user_ldap/lib/User/User.php b/apps/user_ldap/lib/User/User.php
index ab1500ff368..15894ce04b7 100644
--- a/apps/user_ldap/lib/User/User.php
+++ b/apps/user_ldap/lib/User/User.php
@@ -266,8 +266,8 @@ class User {
/**
* 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) {
@@ -278,8 +278,7 @@ class User {
&& 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];
}
diff --git a/apps/user_ldap/tests/AccessTest.php b/apps/user_ldap/tests/AccessTest.php
index 16f79d7819d..c2902a67766 100644
--- a/apps/user_ldap/tests/AccessTest.php
+++ b/apps/user_ldap/tests/AccessTest.php
@@ -112,7 +112,7 @@ class AccessTest extends TestCase {
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([
@@ -494,7 +494,7 @@ class AccessTest extends TestCase {
->willReturn(true);
$connection = $this->createMock(LDAP::class);
$this->connection
- ->expects($this->once())
+ ->expects($this->any())
->method('getConnectionResource')
->willReturn($connection);
$this->ldap
@@ -518,7 +518,7 @@ class AccessTest extends TestCase {
->willReturn(true);
$connection = $this->createMock(LDAP::class);
$this->connection
- ->expects($this->once())
+ ->expects($this->any())
->method('getConnectionResource')
->willReturn($connection);
$this->ldap
diff --git a/apps/user_ldap/tests/Group_LDAPTest.php b/apps/user_ldap/tests/Group_LDAPTest.php
index cb637dcc108..f8327c0776c 100644
--- a/apps/user_ldap/tests/Group_LDAPTest.php
+++ b/apps/user_ldap/tests/Group_LDAPTest.php
@@ -104,14 +104,12 @@ class Group_LDAPTest extends TestCase {
$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);
@@ -133,6 +131,8 @@ class Group_LDAPTest extends TestCase {
->willReturnCallback(function ($name) {
if ($name === 'ldapDynamicGroupMemberURL') {
return '';
+ } elseif ($name === 'ldapBaseGroups') {
+ return [];
}
return 1;
});
@@ -953,6 +953,8 @@ class Group_LDAPTest extends TestCase {
return 'member';
case 'ldapGroupFilter':
return $groupFilter;
+ case 'ldapBaseGroups':
+ return [];
}
return 1;
});
@@ -1321,16 +1323,16 @@ class Group_LDAPTest extends TestCase {
});
$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);
@@ -1373,6 +1375,10 @@ class Group_LDAPTest extends TestCase {
return null;
});
+ $access->expects($this->any())
+ ->method('groupname2dn')
+ ->willReturn('fakedn');
+
/** @var GroupPluginManager $pluginManager */
$pluginManager = $this->createMock(GroupPluginManager::class);
diff --git a/apps/user_ldap/tests/User/UserTest.php b/apps/user_ldap/tests/User/UserTest.php
index cf3daa9f2c5..e86eb5e9e2e 100644
--- a/apps/user_ldap/tests/User/UserTest.php
+++ b/apps/user_ldap/tests/User/UserTest.php
@@ -1016,6 +1016,10 @@ class UserTest extends \Test\TestCase {
->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')
@@ -1038,6 +1042,10 @@ class UserTest extends \Test\TestCase {
->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')
diff --git a/apps/user_ldap/tests/User_LDAPTest.php b/apps/user_ldap/tests/User_LDAPTest.php
index 3142a256c9a..b00c93e79f0 100644
--- a/apps/user_ldap/tests/User_LDAPTest.php
+++ b/apps/user_ldap/tests/User_LDAPTest.php
@@ -815,13 +815,15 @@ class User_LDAPTest extends TestCase {
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')
diff --git a/apps/user_ldap/tests/WizardTest.php b/apps/user_ldap/tests/WizardTest.php
index ae25aad44ab..5382a0c7f6f 100644
--- a/apps/user_ldap/tests/WizardTest.php
+++ b/apps/user_ldap/tests/WizardTest.php
@@ -72,7 +72,7 @@ class WizardTest extends TestCase {
/** @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 */