Skip to content

Commit

Permalink
feat(ldapselectfield): remove ldap connection test when saving question
Browse files Browse the repository at this point in the history
prevents running unit tests
connection and rendering test can be done in preview
  • Loading branch information
btry committed Apr 20, 2022
1 parent 46c689e commit 536edbf
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 47 deletions.
37 changes: 0 additions & 37 deletions inc/field/ldapselectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
use RuleRightParameter;
use Glpi\Application\View\TemplateRenderer;
use PluginFormcreatorAbstractField;
use Toolbox;

class LdapselectField extends SelectField
{
Expand Down Expand Up @@ -187,53 +186,17 @@ public function prepareQuestionInputForSave($input) {
}

$ldap_values['ldap_attribute'] = $input['ldap_attribute'] ?? ($ldap_values['ldap_attribute'] ?? null);
$attribute = [];
if (isset($ldap_values['ldap_attribute'])) {
$ldap_dropdown = RuleRightParameter::getById((int) $ldap_values['ldap_attribute']);
if (!($ldap_dropdown instanceof RuleRightParameter)) {
return [];
}
$attribute = [$ldap_dropdown->fields['value']];
}

if (isset($input['ldap_filter'])) {
$input['ldap_filter'] = html_entity_decode($input['ldap_filter']);
}
$ldap_values['ldap_filter'] = $input['ldap_filter'] ?? ($ldap_values['ldap_filter'] ?? '');
set_error_handler([self::class, 'ldapErrorHandler'], E_WARNING);

try {
$cookie = '';
$ds = $config_ldap->connect();
if ($ds === false) {
Session::addMessageAfterRedirect(__('Connection to the directory failed.', 'formcreator'), false, ERROR);
}
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
if (AuthLDAP::isLdapPageSizeAvailable($config_ldap)) {
$controls = [
[
'oid' => LDAP_CONTROL_PAGEDRESULTS,
'iscritical' => true,
'value' => [
'size' => $config_ldap->fields['pagesize'],
'cookie' => $cookie
]
]
];
$result = ldap_search($ds, $config_ldap->fields['basedn'], $ldap_values->ldap_filter, $attribute, 0, -1, -1, LDAP_DEREF_NEVER, $controls);
ldap_parse_result($ds, $result, $errcode, $matcheddn, $errmsg, $referrals, $controls);
$cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] ?? '';
} else {
$result = ldap_search($ds, $config_ldap->fields['basedn'], $ldap_values->ldap_filter, $attribute);
}
ldap_get_entries($ds, $result);
} catch (Exception $e) {
restore_error_handler();
trigger_error($e->getMessage(), E_USER_WARNING);
Session::addMessageAfterRedirect(__('Cannot recover LDAP informations!', 'formcreator'), false, ERROR);
}

restore_error_handler();

$input['values'] = json_encode($ldap_values, JSON_UNESCAPED_UNICODE);
unset($input['ldap_auth']);
Expand Down
12 changes: 2 additions & 10 deletions tests/3-unit/GlpiPlugin/Formcreator/Field/LdapSelectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ public function providerPrepareQuestionInputForSave() {
]),
'input' => [
'ldap_auth' => $authLdap->getID(),
'ldap_filter' => 'по', // Some cyrillic sample
'ldap_filter' => 'по',
'ldap_attribute' => '1',
],
'expected' => [
'values' => json_encode([
'ldap_auth' => $authLdap->getID(),
'ldap_attribute' => '1',
'ldap_filter' => 'по',
'ldap_attribute' => '',
], JSON_UNESCAPED_UNICODE),
]
],
Expand All @@ -180,14 +180,6 @@ public function providerPrepareQuestionInputForSave() {
* @return void
*/
public function testPrepareQuestionInputForSave(\PluginFormcreatorQuestion $question, array $input, array $expected) {
// Make the form private
$question = $this->getQuestion([
'ldap_auth' => $input['ldap_auth'],
'fieldtype' => 'ldapselect',
'ldap_filter' => '',
'ldap_attribute' => '',
]);

$instance = $this->newTestedInstance($question);

$output = $instance->prepareQuestionInputForSave($input);
Expand Down

0 comments on commit 536edbf

Please sign in to comment.