Skip to content

[5.0] Fix ShowOnRule validation with wrong results#41918

Merged
HLeithner merged 12 commits intojoomla:5.0-devfrom
richard67:5.0-dev-fix-showon-field-validation-empty-check
Sep 25, 2023
Merged

[5.0] Fix ShowOnRule validation with wrong results#41918
HLeithner merged 12 commits intojoomla:5.0-devfrom
richard67:5.0-dev-fix-showon-field-validation-empty-check

Conversation

@richard67
Copy link
Copy Markdown
Member

@richard67 richard67 commented Sep 25, 2023

Pull Request for Issue #41788 .

Summary of Changes

The PR currently fixes the issue linked above, but in addition it fixes a general conceptional error with the validation rule added by PR #41460 :

  • Field names can not only contain alphanumeric characters but also dashes.
  • Showon can be used with any kind of field, i.e. also e.g. text or editor fields, so the field values can contain any kind of characters, including the : or !: which separate the field name from the value in showon rules. This was completely broken with PR [5.0] [com_fields] Showon Attribute doesn't accept correct syntax on Joomla 5 #41788 . This PR here fixes that.

Finally, it adds unit tests for the ShowOnRule validation rule, thanks @heelc29 for these.

Testing Instructions

  1. Go to Content -> Fields
  2. Edit an existing field or create a new one
  3. Go to Options tab
  4. Edit the "Showon Attribute" field.
  5. Try to set to one of the below values and save:
    (empty value)
    box:value1
    box::
    box:!
    box:3:21
    box:#@\[] (the last one is a space)
    box!:value1
    box!:
    box:value1[OR]square:value1
    box:value1[AND]square:value1
    box:value1[OR]square!:value1
    box:value1[AND]square!:value1
    box:value1[AND]square:value1,value2
    box:value1[AND]square!:value1,value2
    box:value1[AND]square!:value1,value2[OR]square!:value1
    box:value1,value2[AND]square:value1
    box:value1[AND]square!:value1:value2
    box-test-1:value1
    box-test-1!:value1
    box-test-1!:
    box-test-1:3:21
    box:value1[AND]square-test-1!:value1:value2
    box:value1[OR]square-test-1:value1
    box:value1[OR]square-test-1!:value1
    box
    [AND]box:value3[OR]square:2:3
    [AND][OR]
    box@abc:value1
    box_abc:value1
    box!abc:value1
  6. Try any other valid or invalid rules which come to your mind.

In addition, check the Drone logs for the unit tests without and with this PR, or run the unit tests yourself using ./libraries/vendor/bin/phpunit --testsuite Unit, and compare the results.

Actual result BEFORE applying this Pull Request

The following rules fail validation, but they should pass:
box::
box:!
box:#@\[] (the last one is a space)
box!:
box-test-1!:

The following rules pass validation, but they should fail:
[AND]box:value3[OR]square:2:3
box@abc:value1
box_abc:value1
box!abc:value1

Expected result AFTER applying this Pull Request

The following rules are validated with success:
(empty value)
box:value1
box::
box:!
box:3:21
box:#@\[] (the last one is a space)
box!:value1
box!:
box:value1[OR]square:value1
box:value1[AND]square:value1
box:value1[OR]square!:value1
box:value1[AND]square!:value1
box:value1[AND]square:value1,value2
box:value1[AND]square!:value1,value2
box:value1[AND]square!:value1,value2[OR]square!:value1
box:value1,value2[AND]square:value1
box:value1[AND]square!:value1:value2
box-test-1:value1
box-test-1!:value1
box-test-1!:
box-test-1:3:21
box:value1[AND]square-test-1!:value1:value2
box:value1[OR]square-test-1:value1
box:value1[OR]square-test-1!:value1

The following rules fail validation:
box
[AND]box:value3[OR]square:2:3
[AND][OR]
box@abc:value1
box_abc:value1
box!abc:value1

The new unit tests run with success.

Link to documentations

Please select:

  • No documentation changes for docs.joomla.org needed

  • No documentation changes for manual.joomla.org needed

@richard67 richard67 changed the title [5.0] [WiP] Fix ShowOnRule failing for "not empty" rule [5.0] Fix ShowOnRule validation with wrong results Sep 25, 2023
@richard67 richard67 marked this pull request as ready for review September 25, 2023 14:46
@obuisard
Copy link
Copy Markdown
Contributor

obuisard commented Sep 25, 2023

Also tested:
dhdhdhdhd -> rightly fails
ddd::dd -> rightly fails
ddd:;dd -> rightly fails
[AND][OR] -> rightly fails
box:value1[AND]square!:value1,value2[] -> rightly fails
box:value1[AND]square!:value1,value2[UP] -> rightly fails
box:value1[AND]square!:value1,value2[UP][OR]square!:value1 -> rightly fails
box:value1[AND]square!:value1:value2 -> rightly fails
box:value1[AND]square!:value1,value2[OR]square!:value1 -> succeeds

Looks 'pretty' solid to me

@obuisard
Copy link
Copy Markdown
Contributor

I have tested this item ✅ successfully on fbdd6ff


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/41918.

@richard67
Copy link
Copy Markdown
Member Author

PR is ready for tests now, I've just completed the instructions.

@heelc29
Copy link
Copy Markdown
Contributor

heelc29 commented Sep 25, 2023

@richard67 I tried adding an unit test for this rule: richard67#39
Can you check please?

@richard67
Copy link
Copy Markdown
Member Author

Unit tests have been added. Thanks @heelc29 .

Without this PR: Tests: 830, Assertions: 1303, Warnings: 1, Skipped: 1.

With this PR: Tests: 845, Assertions: 1318, Warnings: 1, Skipped: 1.

Difference = the 15 new tests added here. All tests passing.

@heelc29
Copy link
Copy Markdown
Contributor

heelc29 commented Sep 25, 2023

It looks like I broke phpcs ...

@richard67
Copy link
Copy Markdown
Member Author

It looks like I broke phpcs ...

@heelc29 Yes, and it seems massive. I assume it's tabs being used for indentation. Will check.

@richard67
Copy link
Copy Markdown
Member Author

@heelc29 The problems were the line endings. Your file had Windows (CR+LF), but we require Unix (LF). I recommend using an editor which shows this, like e.g. "notepad++".

@heelc29
Copy link
Copy Markdown
Contributor

heelc29 commented Sep 25, 2023

Your file had Windows (CR+LF), but we require Unix (LF). I recommend using an editor which shows this, like e.g. "notepad++".

@richard67 I use VS Code ... it shows only small in the corner. I will take care to change it to LF
image

@richard67
Copy link
Copy Markdown
Member Author

Your file had Windows (CR+LF), but we require Unix (LF). I recommend using an editor which shows this, like e.g. "notepad++".

@richard67 I use VS Code ... it shows only small in the corner. I will take care to change it to LF image

@heelc29 Can't VS Code be configured to use a custom editor and a custom comparison (diff) tool? If you've ever used notepad++ as editor and BejondCompare as diff tool, you will not want anything else anymore.

@heelc29
Copy link
Copy Markdown
Contributor

heelc29 commented Sep 25, 2023

I like Beyond Compare and use it daily at work ... its easy to compare complete folders and ZIPs 😀

@richard67
Copy link
Copy Markdown
Member Author

@obuisard Could you do a quick test again if it still works, and test with field names which contain dashes in addition? Thanks in advance.

@richard67
Copy link
Copy Markdown
Member Author

richard67 commented Sep 25, 2023

@AndySDH Can values also contain dashes, e.g. box:value-1?

@obuisard
Copy link
Copy Markdown
Contributor

box:value-1

Yes...

@obuisard
Copy link
Copy Markdown
Contributor

What should also be the behavior when there are just several empty spaces in the field? Right now, it fails the test.

@richard67
Copy link
Copy Markdown
Member Author

What should also be the behavior when there are just several empty spaces in the field? Right now, it fails the test.

@obuisard Depending on the field type, the value can be anything, right?

So e.g. a text field or editor field can also contain characters which it should not contain because we use them in showon conditions as special characters, e.g. [, ], :, !, right?

If so, these characters would have to be escaped somehow in the value, otherwise a showon could not be used, and catching this with that kind of validation rule would be hard.

So either we restrict showon to a subset of fields where that can not happen,or we revert the PR #41460 completely.

@obuisard
Copy link
Copy Markdown
Contributor

We can't really restrict showon to specific types because then we restrict the showon to core fields only.

@richard67
Copy link
Copy Markdown
Member Author

Then all we can do is to allow all characters in the value expect of those which we need to recognize the showon rules' parts (":" and "!"). If those characters then appear, showon will be broken so or so, without this PR and with this PR and also without the validation rule at all.

@AndySDH
Copy link
Copy Markdown
Contributor

AndySDH commented Sep 25, 2023

Depending on the field type, the value can be anything, right?

Yes exactly...

@richard67
Copy link
Copy Markdown
Member Author

I've created a fix now which allows anything for the field values, so only field names are restricted to alphanumeric characters and dashes, and the showon syntax must be correct, i.e. conditions are separated by [AND] or [OR], and the first : or !: after the name separates the name from the condition.

@obuisard
Copy link
Copy Markdown
Contributor

obuisard commented Sep 25, 2023

box:3:21 and box:: no longer fail, but it should be expected and accepted after all.

@richard67
Copy link
Copy Markdown
Member Author

box:3:21 no longer fails, but it should be expected

@obuisard The testing instructions haven't been updated yet. This value should NOT fail validation as a field value can be "3:21" in a text field (e.g. if it represents the time of the day).

@obuisard
Copy link
Copy Markdown
Contributor

box:3:21 no longer fails, but it should be expected

@obuisard The testing instructions haven't been updated yet. This value should NOT fail validation as a field value can be "3:21" in a text field (e.g. if it represents the time of the day).

Yes, totally agree.

@obuisard
Copy link
Copy Markdown
Contributor

I have tested this item ✅ successfully on caa7577


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/41918.

@AndySDH
Copy link
Copy Markdown
Contributor

AndySDH commented Sep 25, 2023

I have tested this item ✅ successfully on caa7577

All going well now. Nice work.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/41918.

@richard67
Copy link
Copy Markdown
Member Author

Just now I've finished testing instructions (actual result and expected result). Are you sure you've tested all that?

@AndySDH
Copy link
Copy Markdown
Contributor

AndySDH commented Sep 25, 2023

Yes, all is working as expected now, tested all combinations I could think of.

@richard67
Copy link
Copy Markdown
Member Author

Well, the unit tests do their job, so with the 2 successful human tests I think it can be RTC.

@richard67
Copy link
Copy Markdown
Member Author

@AndySDH Thanks for testing and pushing me into the right direction. As I am not using fields with showon so often and was not the author of that new validation rule, only tried to fix it, I was not aware that field values can be anything, so it was not really clear to me how wrong the validation rule is without this PR.

@AndySDH
Copy link
Copy Markdown
Contributor

AndySDH commented Sep 25, 2023

@AndySDH Thanks for testing and pushing me into the right direction. As I am not using fields with showon so often and was not the author of that new validation rule, only tried to fix it, I was not aware that field values can be anything, so it was not really clear to me how wrong the validation rule is without this PR.

You're welcome! Thanks to you actually for working on this improvement. And to @obuisard as well.

@obuisard
Copy link
Copy Markdown
Contributor

Just now I've finished testing instructions (actual result and expected result). Are you sure you've tested all that?

I have tested the values you added once I saw the new test values.

@alikon
Copy link
Copy Markdown
Contributor

alikon commented Sep 25, 2023

RTC


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/41918.

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Sep 25, 2023
@HLeithner HLeithner merged commit a9d106c into joomla:5.0-dev Sep 25, 2023
@HLeithner
Copy link
Copy Markdown
Member

Thx

@joomla-cms-bot joomla-cms-bot removed the RTC This Pull Request is Ready To Commit label Sep 25, 2023
@richard67 richard67 deleted the 5.0-dev-fix-showon-field-validation-empty-check branch September 25, 2023 20:55
HLeithner pushed a commit to HLeithner/joomla-cms that referenced this pull request Sep 26, 2023
* Fix ShowOnRule failing for "not empty" rule

* Fix wrong split criteria for "and" and "or"

* Allow comma-separated value list

* Add missing brackets around "|" expression

* Add missing begin and end of string anchors

* Remove useless "i" modifier as it is not needed

* add unit test

* Fix line ending - should be Unix not Windows

* A few more unit test cases

* Allow dashes in field names

* Allow any kind of character in field values

---------

Co-authored-by: Christian Heel <66922325+heelc29@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants