Used array-safe string length detection.
Details
- Reviewers
alex
Change system code
- in the u:OnBeforeItemCreate event, after $object variable was initialized, add these lines:
$object->SetDBField('FirstName', array(1,2,3)); $object->SetDBField('MaxValueInc0Valid', 0); $object->SetDBField('MinValueInc0Valid', 0); $object->SetDBField('MaxValueExc0Valid', -5); $object->SetDBField('MinValueExc0Valid', 5); $object->SetDBField('MaxLen2Valid', '1'); $object->SetDBField('MinLen2Valid', '22'); $object->SetDBField('MaxValueInc0InValid', 5); $object->SetDBField('MinValueInc0InValid', -5); $object->SetDBField('MaxValueExc0InValid', 0); $object->SetDBField('MinValueExc0InValid', 0); $object->SetDBField('MaxLen2InValid', '333'); $object->SetDBField('MinLen2InValid', '1');
- in the users_config php remove type key from FirstName field configuration (to bypass type validation)
- in the users_config php add elements to VirtualFields array:
'MaxValueInc0Valid' => array('type' => 'int', 'max_value_inc' => 0), 'MinValueInc0Valid' => array('type' => 'int', 'min_value_inc' => 0), 'MaxValueExc0Valid' => array('type' => 'int', 'max_value_exc' => 0), 'MinValueExc0Valid' => array('type' => 'int', 'min_value_exc' => 0), 'MaxLen1Valid' => array('type' => 'string', 'max_len' => 1), 'MinLen1Valid' => array('type' => 'string', 'min_len' => 1), 'MaxValueInc0InValid' => array('type' => 'int', 'max_value_inc' => 0), 'MinValueInc0InValid' => array('type' => 'int', 'min_value_inc' => 0), 'MaxValueExc0InValid' => array('type' => 'int', 'max_value_exc' => 0), 'MinValueExc0InValid' => array('type' => 'int', 'min_value_exc' => 0), 'MaxLen2InValid' => array('type' => 'string', 'max_len' => 2), 'MinLen2InValid' => array('type' => 'string', 'min_len' => 2),
Testing
- login to Admin Console
- go to User Management → Users section
- press Add toolbar button
- press Save toolbar button
- confirm that strlen() expects parameter 1 to be string, array given error or similar is reported in Debugger
- confirm that Valid group virtual fields doesn't report any errors in Debugger
- confirm that all six value_out_of_range errors from InValid group virtual fields report errors in Debugger
Change system code
In the users_config php return type key for FirstName field configuration.
Testing
- login to Admin Console
- go to User Management → Users section
- press Add toolbar button
- press Save toolbar button
- confirm that Array to string conversion notice is not reported in Debugger
Diff Detail
- Repository
- rINP In-Portal
- Branch
- /in-portal/branches/5.2.x
- Lint
Lint OK - Unit
No Unit Test Coverage - Build Status
Buildable 887 Build 887: arc lint + arc unit
Event Timeline
All length checking code was changed and therefore test plan must include testing of all existing behavior (regression testing), not just the part that was fixed. I guess you can create field declaration with all possible validation options and just confirm, that:
- when array is passed in no validation error happens
- when invalid scalar value (maybe several values needs to be used to match each of validation options) is passed the validation error happens
- when valid scalar value (maybe several values needs to be used to match each of validation options) is passed no validation error happens
core/kernel/utility/validator.php | ||
---|---|---|
232–233 | In fact change // validate number into // Validate number. and this should fix all CS errors. |
core/kernel/utility/validator.php | ||
---|---|---|
231 |
refactoring | |
258–275 |
prevents string-validation rules to be apply to a valid number | |
292–293 | remove see above | |
295 | replace with return $res; see above |
No strlen error in debugger from changed ValidateRange method. 'FirstName' test value detected as invalid in other validation method (ValidateType) and not passed to ValidateRange method.
- Please keep the current test plan and add another test plan part covering original scenario from associated Confluence discussion:
When performing checkout (add product to cart > enter shipping info > enter billing info > preview order > place order) in In-Commerce on Front-End, then following warning happens, when going from shipping to billing step:
Warning (#3): strlen() expects parameter 1 to be string, array given in ...\core\kernel\utility\validator.php on line 267In some cases (and shipping step is that case) form can contain non-scalar type fields (e.g. arrays). When these fields are checked with "strlen" (or any other string-only function), then this results in above displayed warning.
- call settype function inside kValidator::ValidateType only, when is_scalar($val) returns true
- update test plan to reflect above made code changes
The current test plan doesn't cause the kValidator::ValidateRange method execution with a non-scalar (e.g. array) value.
Added code to avoid "Array to string conversion" notice. Added related test plan part.