Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search validate ints #18584

Draft
wants to merge 2 commits into
base: QA_5_2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 33 additions & 13 deletions js/src/table/change.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,31 @@ function checkForCheckbox (multiEdit) {
return true;
}

/**
* @test-module Change
*/
var Change = {};

/**
* Validator method for IN(...), NOT IN(...)
* BETWEEN and NOT BETWEEN
* @param {string} value
* @return {boolean}
*/
Change.validationFunctionForMultipleInt = function (value) {
return value.split(/\s*,\s*/).every((v) => Change.validationFunctionForInt(v));
};

/**
* Validator method for INTs
* See all possible syntaxes in tests of https://regexr.com/7h1ci
* @param {string} value
* @return {boolean}
*/
Change.validationFunctionForInt = function (value) {
return /^\s*([+-]*(0x[0-9a-f]+|([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)(e[+-]?[0-9]+)?))\s*$/i.test(value);
};

// used in Search page mostly for INT fields
// eslint-disable-next-line no-unused-vars
function verifyAfterSearchFieldChange (index, searchFormId) {
Expand All @@ -174,22 +199,17 @@ function verifyAfterSearchFieldChange (index, searchFormId) {
$(element).valid();
}
});
// validator method for IN(...), NOT IN(...)
// BETWEEN and NOT BETWEEN
// See all possible syntaxes in tests of https://regexr.com/7h1eq
jQuery.validator.addMethod('validationFunctionForMultipleInt', function (value) {
return value.match(/^(((0x[0-9a-f]+)|([+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)(e[+-]?[0-9]+)?))(,|$))+$/i) !== null;
},
Messages.strEnterValidNumber
jQuery.validator.addMethod(
'validationFunctionForMultipleInt',
Change.validationFunctionForMultipleInt,
Messages.strEnterValidNumber
);
validateMultipleIntField($thisInput, true);
} else {
// validator method for INTs
// See all possible syntaxes in tests of https://regexr.com/7h1ci
jQuery.validator.addMethod('validationFunctionForInt', function (value) {
return value.match(/^(0x[0-9a-f]+$)|([+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)(e[+-]?[0-9]+)?)$/i) !== null;
},
Messages.strEnterValidNumber
jQuery.validator.addMethod(
'validationFunctionForInt',
Change.validationFunctionForInt,
Messages.strEnterValidNumber
);
$(searchFormId).validate({
// update errors as we write
Expand Down
107 changes: 107 additions & 0 deletions test/javascript/change.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* eslint-env node, jest */

const Change = require('phpmyadmin/table/change');

describe('change', () => {
function testSingle (fn) {
test('empty string', function () {
expect(fn('')).toBe(false);
});
test('whitespace string', function () {
expect(fn(' ')).toBe(false);
});
test('space around', function () {
expect(fn(' 2 ')).toBe(true);
});
test('exponential', function () {
expect(fn('102823466E+38')).toBe(true);
});
test('negative decimal', function () {
expect(fn('-3.402823466')).toBe(true);
});
test('positive decimal', function () {
expect(fn('+3.402823466')).toBe(true);
});
test('positive integer', function () {
expect(fn('+3402823466')).toBe(true);
});
test('super small negative exponential', function () {
expect(fn('-2.2250738585072014E-308')).toBe(true);
});
test('hexadecimal', function () {
expect(fn('0x12341')).toBe(true);
});
test('negative hexadecimal', function () {
expect(fn('-0xaf')).toBe(true);
});
test('multiple signs', function () {
expect(fn('-+-2')).toBe(true);
});
test('No decimals for hex', function () {
expect(fn('0x2.2')).toBe(false);
});
test('Accept a letter for hex', function () {
expect(fn('0xf')).toBe(true);
});
test('Accept multiple letters for hex', function () {
expect(fn('0xffabc')).toBe(true);
});
test('Upper case hex', function () {
expect(fn('0xFAFB')).toBe(true);
});
test('Forbid invalid hex chars', function () {
expect(fn('0xag')).toBe(false);
});
test('Not matches chars', function () {
expect(fn('abcdef')).toBe(false);
});
test('pipe seperator', function () {
expect(fn('0123456789|0123456789')).toBe(false);
});
test('Leading , is not accepted', function () {
expect(fn(',0x12341')).toBe(false);
});
test('Trailing , is not accepted', function () {
expect(fn('0x12341,')).toBe(false);
});
}

describe('validationFunctionForMultipleInt', () => {
const fn = Change.validationFunctionForMultipleInt;
testSingle(fn);

test('two numbers with leading zeros', function () {
expect(fn('0123456789,0123456789')).toBe(true);
});
test('negative exponential and decimal with leading zero', function () {
expect(fn('-3.402823466E+38,0123456789')).toBe(true);
});
test('negative decimal followed by negative exponential', function () {
expect(fn('-3.402823466,-3.402823466E+38')).toBe(true);
});
test('Multi hex & int', function () {
expect(fn('0xf,0xa,0xb,124')).toBe(true);
});
test('Forbid invalid hex chars', function () {
expect(fn('0xaf,0xag')).toBe(false);
});
test('Multi hex more complex', function () {
expect(fn('0xaf,0x0ad')).toBe(true);
});
test('Space after comma numbers', function () {
expect(fn('1, 2, 3')).toBe(true);
});
test('Spaces everywhere', function () {
expect(fn(' 1 , 2 , 3 ')).toBe(true);
});
});

describe('validationFunctionForInt', () => {
const fn = Change.validationFunctionForInt;
testSingle(fn);

test('two numbers', function () {
expect(fn('123,465')).toBe(false);
});
});
});