Add tests on Ldap class and fix some related bugs

This commit is contained in:
Benjamin Renard 2023-03-24 02:48:12 +01:00
parent d183309280
commit 191a119606
6 changed files with 1327 additions and 21 deletions

View File

@ -4,7 +4,9 @@
"description": "Object oriented interface for searching and manipulating LDAP entries & filters",
"require-dev": {
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9"
"phpunit/phpunit": "^9",
"lstrojny/phpunit-function-mocker": "^1.1",
"mockery/mockery": "^1.5"
},
"authors": [
{

View File

@ -10,3 +10,9 @@ parameters:
-
message: "#Method .*::test.*\\(\\) has no return type specified\\.#"
path: tests/*
-
message: "#Call to an undefined method Mockery\\\\ExpectationInterface|Mockery\\\\HigherOrderMessage::once\\(\\)\\.#"
path: tests/*
-
message: "#Call to an undefined method PHPUnit\\\\Extension\\\\FunctionMocker::expects\\(\\)\\.#"
path: tests/*

View File

@ -416,8 +416,6 @@ class Entry {
if ($this->ldap)
// @phpstan-ignore-next-line
return $this->ldap->error($error);
// @phpstan-ignore-next-line
error_log($error);
throw new LdapException($error); // @phpstan-ignore-line
}

View File

@ -21,6 +21,8 @@ namespace EesyLDAP;
* @property-read string $scope
* @property-read bool $raise_on_error
* @property-read bool $use_schema
* @property-read array<string,string> $config_aliases
* @property-read array<string,mixed> $default_config
* @property-read Schema|false $schema
*/
class Ldap {
@ -180,7 +182,7 @@ class Ldap {
break;
case 'filter':
if (is_string($value))
$value = new Filter($value);
$value = Filter::parse($value);
elseif (!$value instanceof Filter) {
$this -> error(
"Invalid filter found in configuration: expect to be a string or a \EesyLDAP\Filter ".
@ -199,8 +201,6 @@ class Ldap {
);
continue 2;
}
}
$this -> config[$key] = $value;
}
@ -238,7 +238,7 @@ class Ldap {
public function connect($raise=null, $bind_dn=null, $bind_password=null) {
if ($this -> _link)
return true;
if (!self :: check_ldap_extension($raise))
if (!$this -> check_ldap_extension($raise))
return false;
foreach($this -> hosts as $host) {
@ -264,7 +264,7 @@ class Ldap {
continue;
}
if (!$this -> bind($bind_dn, $bind_password)) {
if (!$this -> bind($bind_dn, $bind_password, false)) {
$this -> close();
$this -> error(
"Fail to bind on %s", false);
@ -277,7 +277,7 @@ class Ldap {
if (in_array($option, $this -> required_options)) {
$this -> close();
$this -> error('Fail to set option %s on %s', false, $option, $host);
continue;
continue 2;
}
}
}
@ -350,8 +350,8 @@ class Ldap {
return $this -> error("%s: unkown error", $raise, $prefix);
$err = $this->_link?$this->_link->err2str($errno):false;
if (!$err)
return $this -> error("%s: error #%s", $raise, $errno);
return $this -> error("%s: %s (#%s)", $raise, $err, $errno);
return $this -> error("%s: error #%s", $raise, $prefix, $errno);
return $this -> error("%s: %s (#%s)", $raise, $prefix, $err, $errno);
}
/**
@ -366,8 +366,6 @@ class Ldap {
if ($extra_args)
$error = call_user_func_array('sprintf', array_merge(array($error), $extra_args));
// Note: sprintf always return string
// @phpstan-ignore-next-line
error_log($error);
if (is_null($raise))
$raise = $this -> raise_on_error;
if ($raise)
@ -449,14 +447,17 @@ class Ldap {
* - attributes: Array of attribute names, which the entries in the expected result should contain.
* It is good practice to limit this to just the ones you need.
*
* @param string|null $base Base DN of the search (optional, default: null == use config)
* @param string|null $filter Filter of the search (optional, default: null == use config)
* @param string|Entry|null $base Base DN of the search (optional, default: null == use config)
* @param string|null $scope Scope of the search (optional, default: null == use params or config)
* @param array<string>|null $attributes Expected attributes returned by the search (optional,
* default: null == use params or config)
* @param array<string,mixed>|null $params Other optional parameters of the search (optional, default: null == use config)
* @param bool|null $raise See error() (optional, default: null)
* @return array<string,Entry>|false Array of Entry object with DN as key, or False in case of error
* @throws LdapException
*/
public function search($base=null, $filter=null, $params=null, $raise=null) {
public function search($filter=null, $base=null, $scope=null, $attributes=null, $params=null, $raise=null) {
if (!$this -> _link)
return $this -> error("Can't search: no LDAP link");
if (is_null($base))
@ -469,17 +470,22 @@ class Ldap {
$filter = $filter->as_string(); // convert Filter object as string
// Adjust search function to expected scope
$scope = self :: get_param($params, 'scope', $this->scope, 'string');
$scope = $scope?$scope:self :: get_param($params, 'scope', $this->scope, 'string');
if (!is_string($scope) || !array_key_exists($scope, self :: $search_function_by_scope))
return $this -> error("Invalid scope '%s' specified", $raise, $scope);
// Compute expected attributes
$attributes = $attributes?$attributes:self :: get_param($params, 'attributes', array(), 'array');
if (!is_array($attributes))
return $this -> error("Invalid expected attributes specified: must be an array", $raise);
// Run the search
$search = @call_user_func(
// @phpstan-ignore-next-line
array($this->_link, self :: $search_function_by_scope[$scope]),
$base,
$filter,
self :: get_param($params, 'attributes', array(), 'array'),
$attributes,
self :: get_param($params, 'attrsonly', false, 'bool')?1:0,
self :: get_param($params, 'sizelimit', 0, 'int'),
self :: get_param($params, 'timelimit', 0, 'int')
@ -521,16 +527,17 @@ class Ldap {
*
* @param string $dn DN of the expected entry
* @param string|null $filter Filter of the search (optional, default: null == use config)
* @param array<string>|null $attributes Expected attributes returned by the search (optional,
* default: null == use params or config)
* @param array<string,mixed>|null $params Other optional parameters of the search (optional,
* default: null == use config, see search())
* @param bool|null $raise See error() (optional, default: null)
* @return Entry|false The requested Entry object, or False in case of error
* @throws LdapException
*/
public function get_entry($dn, $filter=null, $params=null, $raise=null) {
public function get_entry($dn, $filter=null, $attributes=null, $params=null, $raise=null) {
$params = is_array($params)?$params:array();
$params['scope'] = 'base';
$entries = $this -> search($dn, $filter, $params, $raise);
$entries = $this -> search($filter, $dn, 'base', $attributes, $params, $raise);
if (is_array($entries) && count($entries) == 1)
return array_pop($entries);
return false;

View File

@ -100,7 +100,7 @@ class Schema {
public static function load(&$ldap, $raise=null) {
$entry = $ldap->get_entry(
'cn=SubSchema', '(objectclass=*)',
array('attributes' => array_keys(self :: $attributes_to_entry_types))
array_keys(self :: $attributes_to_entry_types)
);
if (!$entry instanceof Entry)
return $ldap->error('Fail to load cn=SubSchema entry', $raise);

1293
tests/LdapTest.php Normal file

File diff suppressed because it is too large Load Diff