Commit fd5c5d6a authored by Elias Villiger's avatar Elias Villiger
Browse files

Feature 5414 - Add some auto checktype unit tests

parent 2ae2ecb6
......@@ -792,7 +792,7 @@ class Support {
* @param $typeSpec
* @throws UserFormException
*/
private function adjustFeToColumnDefinition(array &$formElement, $typeSpec) {
public static function adjustFeToColumnDefinition(array &$formElement, $typeSpec) {
self::adjustMaxLength($formElement, $typeSpec);
self::adjustDecimalFormat($formElement, $typeSpec);
......@@ -817,14 +817,15 @@ class Support {
'tinyint' => [-128, 127, SANITIZE_ALLOW_NUMERICAL, 0, 255, SANITIZE_ALLOW_DIGIT],
'smallint' => [-32768, 32767, SANITIZE_ALLOW_NUMERICAL, 0, 65535, SANITIZE_ALLOW_DIGIT],
'mediumint' => [-8388608, 8388607, SANITIZE_ALLOW_NUMERICAL, 0, 16777215, SANITIZE_ALLOW_DIGIT],
'int' => [0, 4294967295, SANITIZE_ALLOW_NUMERICAL, -2147483648, 2147483647, SANITIZE_ALLOW_DIGIT],
'int' => [-2147483648, 2147483647, SANITIZE_ALLOW_NUMERICAL, 0, 4294967295, SANITIZE_ALLOW_DIGIT],
'bigint' => [-9223372036854775808, 9223372036854775807, SANITIZE_ALLOW_NUMERICAL, 0, 18446744073709551615, SANITIZE_ALLOW_DIGIT],
];
$min = '';
$max = '';
$checkType = false;
$checkType = SANITIZE_ALLOW_ALNUMX;
$inputType = '';
$isANumber = true;
switch ($formElement[FE_TYPE]) {
case FE_TYPE_PASSWORD:
......@@ -868,8 +869,16 @@ class Support {
$inputType = 'number';
$checkType = SANITIZE_ALLOW_DIGIT;
break;
default:
$isANumber = false;
break;
}
// Numbers don't need a maxLength because they are being handled by min/max and/or decimalFormat
if ($isANumber)
$formElement[FE_MAX_LENGTH] = '';
if (!empty($formElement[FE_TYPEAHEAD_SQL]) || !empty($formElement[FE_TYPEAHEAD_LDAP])) {
$inputType = '';
$checkType = SANITIZE_ALLOW_ALNUMX;
......@@ -878,16 +887,11 @@ class Support {
// Set parameters if not set by user
if ($formElement[FE_CHECK_TYPE] === SANITIZE_ALLOW_AUTO) {
if ($checkType === false) {
$formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALNUMX; // fallback
} else {
$formElement[FE_CHECK_TYPE] = $checkType;
}
$formElement[FE_CHECK_TYPE] = $checkType;
}
self::setIfNotSet($formElement, FE_MIN, $min);
self::setIfNotSet($formElement, FE_MAX, $max);
self::setIfNotSet($formElement, FE_INPUT_TYPE, $inputType);
if (empty($formElement[FE_MIN])) $formElement[FE_MIN] = $min;
if (empty($formElement[FE_MAX])) $formElement[FE_MAX] = $max;
if (empty($formElement[FE_INPUT_TYPE])) $formElement[FE_INPUT_TYPE] = $inputType;
}
/**
......@@ -940,7 +944,7 @@ class Support {
$feMaxLength = 10;
break;
case 'datetime':
$feMaxLength = empty($formElement[FE_SHOW_SECONDS]) ? 16 : 19;
$feMaxLength = 19;
break;
case 'time':
$feMaxLength = 8;
......
......@@ -731,6 +731,80 @@ class SupportTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals(1073741824000, Support::returnBytes('1000g'));
}
public function testAdjustFeToColumnDefinition() {
// Test CheckType Auto
$formElementTemplate = [
FE_NAME => 'feTest',
FE_MAX_LENGTH => '',
FE_ENCODE => FE_ENCODE_SPECIALCHAR,
FE_CHECK_TYPE => SANITIZE_ALLOW_AUTO,
FE_TYPE => FE_TYPE_TEXT,
FE_INPUT_TYPE => '',
FE_MIN => '',
FE_MAX => '',
];
$formElement = $formElementTemplate;
$expected = $formElement;
Support::adjustFeToColumnDefinition($formElement, 'int(11) not null');
$expected[FE_CHECK_TYPE] = SANITIZE_ALLOW_NUMERICAL;
$expected[FE_INPUT_TYPE] = 'number';
$expected[FE_MIN] = -2147483648;
$expected[FE_MAX] = 2147483647;
$this->assertEquals($expected, $formElement, "CheckType Auto should switch to numerical for signed int");
$formElement = $formElementTemplate;
$expected = $formElement;
Support::adjustFeToColumnDefinition($formElement, 'int(11) unsigned not null');
$expected[FE_CHECK_TYPE] = SANITIZE_ALLOW_DIGIT;
$expected[FE_INPUT_TYPE] = 'number';
$expected[FE_MIN] = 0;
$expected[FE_MAX] = 4294967295;
$this->assertEquals($expected, $formElement, "CheckType Auto should switch to digit for unsigned int");
$formElement = $formElementTemplate;
$expected = $formElement;
Support::adjustFeToColumnDefinition($formElement, 'decimal(10,2) not null');
$expected[FE_CHECK_TYPE] = SANITIZE_ALLOW_NUMERICAL;
$this->assertEquals($expected, $formElement, "CheckType Auto should switch to numerical for decimal");
$formElement = $formElementTemplate;
$formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALNUMX;
Support::adjustFeToColumnDefinition($formElement, 'int(11) not null');
$this->assertEquals(SANITIZE_ALLOW_ALNUMX, $formElement[FE_CHECK_TYPE], "CheckType should not switch if set to non-auto");
$formElement = $formElementTemplate;
$expected = $formElement;
Support::adjustFeToColumnDefinition($formElement, 'varchar(255)');
$expected[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALL;
$expected[FE_MAX_LENGTH] = 255;
$this->assertEquals($expected, $formElement, "Checktype Auto should switch to all for text with encode=specialchars");
$formElement = $formElementTemplate;
$formElement[FE_ENCODE] = FE_ENCODE_NONE;
$expected = $formElement;
Support::adjustFeToColumnDefinition($formElement, 'varchar(64)');
$expected[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALNUMX;
$expected[FE_MAX_LENGTH] = 64;
$this->assertEquals($expected, $formElement, "Checktype Auto should switch to alnumx for text with encode=none");
$formElement = $formElementTemplate;
$formElement[FE_TYPEAHEAD_SQL] = "SELECT chocolate WHERE tastes IS good";
$expected = $formElement;
Support::adjustFeToColumnDefinition($formElement, 'int(11) not null');
$expected[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALNUMX;
$expected[FE_MIN] = -2147483648;
$expected[FE_MAX] = 2147483647;
$this->assertEquals($expected, $formElement, "Checktype Auto should switch to alnumx if typeAhead is defined");
$formElement = $formElementTemplate;
$formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALNUMX;
$expected = $formElement;
Support::adjustFeToColumnDefinition($formElement, 'datetime');
$expected[FE_MAX_LENGTH] = strlen('0000/00/00 00:00:00');
$this->assertEquals($expected, $formElement, "maxLength for datetime should be correct");
}
protected function setUp() {
parent::setUp();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment