PHP 7.3 fixes
This commit is contained in:
parent
c3a7fac3f3
commit
0879961b61
|
@ -554,89 +554,68 @@ abstract class baseModule {
|
|||
* @see baseModule::get_metaData()
|
||||
*/
|
||||
public function check_profileOptions($options, $typeId) {
|
||||
$messages = array();
|
||||
$errors = array();
|
||||
if (isset($this->meta['profile_checks'])) {
|
||||
$identifiers = array_keys($this->meta['profile_checks']);
|
||||
for ($i = 0; $i < sizeof($identifiers); $i++) {
|
||||
// empty input
|
||||
if (!isset($options[$identifiers[$i]][0]) || ($options[$identifiers[$i]][0] == '')) {
|
||||
// check if option is required
|
||||
if (isset($this->meta['profile_checks'][$identifiers[$i]]['required']) && $this->meta['profile_checks'][$identifiers[$i]]['required']) {
|
||||
$messages[] = $this->meta['profile_checks'][$identifiers[$i]]['required_message'];
|
||||
}
|
||||
foreach ($this->meta['profile_checks'] as $identifier => $check) {
|
||||
// check if option is required
|
||||
if (isset($check['required']) && $check['required']
|
||||
&& (!isset($options[$identifier][0]) || ($options[$identifier][0] == ''))) {
|
||||
$errors[] = $check['required_message'];
|
||||
continue;
|
||||
}
|
||||
switch ($this->meta['profile_checks'][$identifiers[$i]]['type']) {
|
||||
switch ($check['type']) {
|
||||
// check by regular expression (from account.inc)
|
||||
case "ext_preg":
|
||||
// ignore empty fileds
|
||||
if ($options[$identifiers[$i]][0] == '') {
|
||||
continue;
|
||||
}
|
||||
if (! get_preg($options[$identifiers[$i]][0], $this->meta['profile_checks'][$identifiers[$i]]['regex'])) {
|
||||
$messages[] = $this->meta['profile_checks'][$identifiers[$i]]['error_message'];
|
||||
if (!empty($options[$identifier][0])
|
||||
&& !get_preg($options[$identifier][0], $check['regex'])) {
|
||||
$errors[] = $check['error_message'];
|
||||
}
|
||||
break;
|
||||
// check by regular expression (case insensitive)
|
||||
case 'regex_i':
|
||||
// ignore empty fileds
|
||||
if ($options[$identifiers[$i]][0] == '') {
|
||||
continue;
|
||||
}
|
||||
if (! preg_match('/' . $this->meta['profile_checks'][$identifiers[$i]]['regex'] . '/i', $options[$identifiers[$i]][0])) {
|
||||
$messages[] = $this->meta['profile_checks'][$identifiers[$i]]['error_message'];
|
||||
if (!empty($options[$identifier][0])
|
||||
&& !preg_match('/' . $check['regex'] . '/i', $options[$identifier][0])) {
|
||||
$errors[] = $check['error_message'];
|
||||
}
|
||||
break;
|
||||
// check by regular expression (case sensitive)
|
||||
case 'regex':
|
||||
// ignore empty fileds
|
||||
if ($options[$identifiers[$i]][0] == '') {
|
||||
continue;
|
||||
}
|
||||
if (! preg_match('/' . $this->meta['profile_checks'][$identifiers[$i]]['regex'] . '/', $options[$identifiers[$i]][0])) {
|
||||
$messages[] = $this->meta['profile_checks'][$identifiers[$i]]['error_message'];
|
||||
if (!empty($options[$identifier][0])
|
||||
&& !preg_match('/' . $check['regex'] . '/', $options[$identifier][0])) {
|
||||
$errors[] = $check['error_message'];
|
||||
}
|
||||
break;
|
||||
// check by integer comparison (greater)
|
||||
case 'int_greater':
|
||||
$val1 = $options[$check['cmp_name1']][0];
|
||||
$val2 = $options[$check['cmp_name2']][0];
|
||||
// ignore if both fields are empty
|
||||
if (($options[$this->meta['profile_checks'][$identifiers[$i]]['cmp_name1']][0] == '') && ($options[$this->meta['profile_checks'][$identifiers[$i]]['cmp_name2']][0] == '')) {
|
||||
continue;
|
||||
}
|
||||
// print error message if only one field is empty
|
||||
if (($options[$this->meta['profile_checks'][$identifiers[$i]]['cmp_name1']][0] == '') || ($options[$this->meta['profile_checks'][$identifiers[$i]]['cmp_name2']][0] == '')) {
|
||||
$messages[] = $this->meta['profile_checks'][$identifiers[$i]]['error_message'];
|
||||
continue;
|
||||
}
|
||||
// compare
|
||||
if (!(intval($options[$this->meta['profile_checks'][$identifiers[$i]]['cmp_name1']][0]) > intval($options[$this->meta['profile_checks'][$identifiers[$i]]['cmp_name2']][0]))) {
|
||||
$messages[] = $this->meta['profile_checks'][$identifiers[$i]]['error_message'];
|
||||
if (!(empty($val1) && empty($val2))
|
||||
&& (($val1 == '') || ($val2 == '') || !(intval($val1) > intval($val2)))) {
|
||||
$errors[] = $check['error_message'];
|
||||
}
|
||||
break;
|
||||
// check by integer comparison (greater or equal)
|
||||
case 'int_greaterOrEqual':
|
||||
$val1 = $options[$check['cmp_name1']][0];
|
||||
$val2 = $options[$check['cmp_name2']][0];
|
||||
// ignore if both fields are empty
|
||||
if (($options[$this->meta['profile_checks'][$identifiers[$i]]['cmp_name1']][0] == '') && ($options[$this->meta['profile_checks'][$identifiers[$i]]['cmp_name2']][0] == '')) {
|
||||
continue;
|
||||
}
|
||||
// print error message if only one field is empty
|
||||
if (($options[$this->meta['profile_checks'][$identifiers[$i]]['cmp_name1']][0] == '') || ($options[$this->meta['profile_checks'][$identifiers[$i]]['cmp_name2']][0] == '')) {
|
||||
$messages[] = $this->meta['profile_checks'][$identifiers[$i]]['error_message'];
|
||||
continue;
|
||||
}
|
||||
// compare
|
||||
if (!(intval($options[$this->meta['profile_checks'][$identifiers[$i]]['cmp_name1']][0]) >= intval($options[$this->meta['profile_checks'][$identifiers[$i]]['cmp_name2']][0]))) {
|
||||
$messages[] = $this->meta['profile_checks'][$identifiers[$i]]['error_message'];
|
||||
if (!(empty($val1) && empty($val2))
|
||||
&& (($val1 == '') || ($val2 == '') || !(intval($val1) >= intval($val2)))) {
|
||||
$errors[] = $check['error_message'];
|
||||
}
|
||||
break;
|
||||
// print error message for invalid types
|
||||
default:
|
||||
StatusMessage("ERROR", "Unsupported type!", $this->meta['profile_checks'][$identifiers[$i]]['type']);
|
||||
StatusMessage("ERROR", "Unsupported type!", $check['type']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $messages;
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,296 @@
|
|||
<?php
|
||||
/*
|
||||
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
|
||||
Copyright (C) 2019 Roland Gruber
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
*/
|
||||
|
||||
include_once __DIR__ . '/../../lib/baseModule.inc';
|
||||
|
||||
/**
|
||||
* LAMConfig test case.
|
||||
*
|
||||
* @author Roland Gruber
|
||||
*/
|
||||
class BaseModuleTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
function setup() {
|
||||
$_SESSION['language'] = 'en_GB.utf8:UTF-8:English (Great Britain)';
|
||||
}
|
||||
|
||||
function test_check_profileOptions_ext_preg() {
|
||||
$module = new baseModuleDummy('user');
|
||||
|
||||
$meta['profile_checks']['test_val1'] = array(
|
||||
'type' => 'ext_preg',
|
||||
'regex' => 'digit',
|
||||
'error_message' => array('ERROR', 'error1'));
|
||||
|
||||
$module->setMeta($meta);
|
||||
|
||||
$options = array(
|
||||
'test_val1' => array('10'),
|
||||
);
|
||||
$errors = $module->check_profileOptions($options, 'user1');
|
||||
$this->assertEmpty($errors, print_r($errors, true));
|
||||
}
|
||||
|
||||
function test_check_profileOptions_ext_preg_fail() {
|
||||
$module = new baseModuleDummy('user');
|
||||
|
||||
$meta['profile_checks']['test_val1'] = array(
|
||||
'type' => 'ext_preg',
|
||||
'regex' => 'digit',
|
||||
'error_message' => array('ERROR', 'error1'));
|
||||
|
||||
$module->setMeta($meta);
|
||||
|
||||
$options = array(
|
||||
'test_val1' => array('a'),
|
||||
);
|
||||
$errors = $module->check_profileOptions($options, 'user1');
|
||||
$this->assertEquals(array(array('ERROR', 'error1')), $errors);
|
||||
}
|
||||
|
||||
function test_check_profileOptions_regex() {
|
||||
$module = new baseModuleDummy('user');
|
||||
|
||||
$meta['profile_checks']['test_reg1'] = array(
|
||||
'type' => 'regex',
|
||||
'regex' => 'ab+a',
|
||||
'error_message' => array('ERROR', 'error1'));
|
||||
|
||||
$module->setMeta($meta);
|
||||
|
||||
$options = array(
|
||||
'test_reg1' => array('abbba'),
|
||||
);
|
||||
$errors = $module->check_profileOptions($options, 'user1');
|
||||
$this->assertEmpty($errors, print_r($errors, true));
|
||||
}
|
||||
|
||||
function test_check_profileOptions_regex_fail() {
|
||||
$module = new baseModuleDummy('user');
|
||||
|
||||
$meta['profile_checks']['test_reg1'] = array(
|
||||
'type' => 'regex',
|
||||
'regex' => 'ab+a',
|
||||
'error_message' => array('ERROR', 'error1'));
|
||||
|
||||
$module->setMeta($meta);
|
||||
|
||||
$options = array(
|
||||
'test_reg1' => array('aCa'),
|
||||
);
|
||||
$errors = $module->check_profileOptions($options, 'user1');
|
||||
$this->assertEquals(array(array('ERROR', 'error1')), $errors);
|
||||
}
|
||||
|
||||
function test_check_profileOptions_cmp() {
|
||||
$module = new baseModuleDummy('user');
|
||||
|
||||
$meta['profile_checks']['test_val1'] = array(
|
||||
'type' => 'ext_preg',
|
||||
'regex' => 'digit',
|
||||
'error_message' => array('ERROR', 'error1'));
|
||||
$meta['profile_checks']['test_val2'] = array(
|
||||
'type' => 'ext_preg',
|
||||
'regex' => 'digit',
|
||||
'error_message' => array('ERROR', 'error2'));
|
||||
$meta['profile_checks']['test_cmp'] = array(
|
||||
'type' => 'int_greater',
|
||||
'cmp_name1' => 'test_val2',
|
||||
'cmp_name2' => 'test_val1',
|
||||
'error_message' => array('ERROR', 'errorCMP'));
|
||||
|
||||
$module->setMeta($meta);
|
||||
|
||||
$options = array(
|
||||
'test_val1' => array('10'),
|
||||
'test_val2' => array('20'),
|
||||
);
|
||||
$errors = $module->check_profileOptions($options, 'user1');
|
||||
$this->assertEmpty($errors, print_r($errors, true));
|
||||
}
|
||||
|
||||
function test_check_profileOptions_cmp_fail_equal() {
|
||||
$module = new baseModuleDummy('user');
|
||||
|
||||
$meta['profile_checks']['test_val1'] = array(
|
||||
'type' => 'ext_preg',
|
||||
'regex' => 'digit',
|
||||
'error_message' => array('ERROR', 'error1'));
|
||||
$meta['profile_checks']['test_val2'] = array(
|
||||
'type' => 'ext_preg',
|
||||
'regex' => 'digit',
|
||||
'error_message' => array('ERROR', 'error2'));
|
||||
$meta['profile_checks']['test_cmp'] = array(
|
||||
'type' => 'int_greater',
|
||||
'cmp_name1' => 'test_val2',
|
||||
'cmp_name2' => 'test_val1',
|
||||
'error_message' => array('ERROR', 'errorCMP'));
|
||||
|
||||
$module->setMeta($meta);
|
||||
|
||||
$options = array(
|
||||
'test_val1' => array('10'),
|
||||
'test_val2' => array('10'),
|
||||
);
|
||||
$errors = $module->check_profileOptions($options, 'user1');
|
||||
$this->assertEquals(array(array('ERROR', 'errorCMP')), $errors);
|
||||
}
|
||||
|
||||
function test_check_profileOptions_cmp_fail_smaller() {
|
||||
$module = new baseModuleDummy('user');
|
||||
|
||||
$meta['profile_checks']['test_val1'] = array(
|
||||
'type' => 'ext_preg',
|
||||
'regex' => 'digit',
|
||||
'error_message' => array('ERROR', 'error1'));
|
||||
$meta['profile_checks']['test_val2'] = array(
|
||||
'type' => 'ext_preg',
|
||||
'regex' => 'digit',
|
||||
'error_message' => array('ERROR', 'error2'));
|
||||
$meta['profile_checks']['test_cmp'] = array(
|
||||
'type' => 'int_greater',
|
||||
'cmp_name1' => 'test_val2',
|
||||
'cmp_name2' => 'test_val1',
|
||||
'error_message' => array('ERROR', 'errorCMP'));
|
||||
|
||||
$module->setMeta($meta);
|
||||
|
||||
$options = array(
|
||||
'test_val1' => array('20'),
|
||||
'test_val2' => array('10'),
|
||||
);
|
||||
$errors = $module->check_profileOptions($options, 'user1');
|
||||
$this->assertEquals(array(array('ERROR', 'errorCMP')), $errors);
|
||||
}
|
||||
|
||||
function test_check_profileOptions_cmpEqual_greater() {
|
||||
$module = new baseModuleDummy('user');
|
||||
|
||||
$meta['profile_checks']['test_val1'] = array(
|
||||
'type' => 'ext_preg',
|
||||
'regex' => 'digit',
|
||||
'error_message' => array('ERROR', 'error1'));
|
||||
$meta['profile_checks']['test_val2'] = array(
|
||||
'type' => 'ext_preg',
|
||||
'regex' => 'digit',
|
||||
'error_message' => array('ERROR', 'error2'));
|
||||
$meta['profile_checks']['test_cmp'] = array(
|
||||
'type' => 'int_greaterOrEqual',
|
||||
'cmp_name1' => 'test_val2',
|
||||
'cmp_name2' => 'test_val1',
|
||||
'error_message' => array('ERROR', 'errorCMP'));
|
||||
|
||||
$module->setMeta($meta);
|
||||
|
||||
$options = array(
|
||||
'test_val1' => array('10'),
|
||||
'test_val2' => array('20'),
|
||||
);
|
||||
$errors = $module->check_profileOptions($options, 'user1');
|
||||
$this->assertEmpty($errors, print_r($errors, true));
|
||||
}
|
||||
|
||||
function test_check_profileOptions_cmpEqual_equal() {
|
||||
$module = new baseModuleDummy('user');
|
||||
|
||||
$meta['profile_checks']['test_val1'] = array(
|
||||
'type' => 'ext_preg',
|
||||
'regex' => 'digit',
|
||||
'error_message' => array('ERROR', 'error1'));
|
||||
$meta['profile_checks']['test_val2'] = array(
|
||||
'type' => 'ext_preg',
|
||||
'regex' => 'digit',
|
||||
'error_message' => array('ERROR', 'error2'));
|
||||
$meta['profile_checks']['test_cmp'] = array(
|
||||
'type' => 'int_greaterOrEqual',
|
||||
'cmp_name1' => 'test_val2',
|
||||
'cmp_name2' => 'test_val1',
|
||||
'error_message' => array('ERROR', 'errorCMP'));
|
||||
|
||||
$module->setMeta($meta);
|
||||
|
||||
$options = array(
|
||||
'test_val1' => array('10'),
|
||||
'test_val2' => array('10'),
|
||||
);
|
||||
$errors = $module->check_profileOptions($options, 'user1');
|
||||
$this->assertEmpty($errors, print_r($errors, true));
|
||||
}
|
||||
|
||||
function test_check_profileOptions_cmpEqual_fail() {
|
||||
$module = new baseModuleDummy('user');
|
||||
|
||||
$meta['profile_checks']['test_val1'] = array(
|
||||
'type' => 'ext_preg',
|
||||
'regex' => 'digit',
|
||||
'error_message' => array('ERROR', 'error1'));
|
||||
$meta['profile_checks']['test_val2'] = array(
|
||||
'type' => 'ext_preg',
|
||||
'regex' => 'digit',
|
||||
'error_message' => array('ERROR', 'error2'));
|
||||
$meta['profile_checks']['test_cmp'] = array(
|
||||
'type' => 'int_greaterOrEqual',
|
||||
'cmp_name1' => 'test_val2',
|
||||
'cmp_name2' => 'test_val1',
|
||||
'error_message' => array('ERROR', 'errorCMP'));
|
||||
|
||||
$module->setMeta($meta);
|
||||
|
||||
$options = array(
|
||||
'test_val1' => array('20'),
|
||||
'test_val2' => array('10'),
|
||||
);
|
||||
$errors = $module->check_profileOptions($options, 'user1');
|
||||
$this->assertEquals(array(array('ERROR', 'errorCMP')), $errors);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class baseModuleDummy extends baseModule {
|
||||
|
||||
public function setMeta($meta) {
|
||||
$this->meta = $meta;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see baseModule::can_manage()
|
||||
*/
|
||||
public function can_manage() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see baseModule::process_attributes()
|
||||
*/
|
||||
public function process_attributes() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see baseModule::display_html_attributes()
|
||||
*/
|
||||
public function display_html_attributes() {
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue