-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[5.4] add unit tests for form rules #46723
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
Open
heelc29
wants to merge
5
commits into
joomla:5.4-dev
Choose a base branch
from
heelc29:5.4/unittest/formfules
base: 5.4-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @package Joomla.UnitTest | ||
| * @subpackage Form | ||
| * | ||
| * @copyright (C) 2026 Open Source Matters, Inc. <https://www.joomla.org> | ||
| * @license GNU General Public License version 2 or later; see LICENSE.txt | ||
| */ | ||
|
|
||
| namespace Joomla\Tests\Unit\Libraries\Cms\Form\Rule; | ||
|
|
||
| use Joomla\CMS\Form\Rule\BooleanRule; | ||
| use Joomla\Tests\Unit\UnitTestCase; | ||
|
|
||
| /** | ||
| * Test class for BooleanRule. | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| class BooleanRuleTest extends UnitTestCase | ||
| { | ||
| /** | ||
| * Test data for the testRule method | ||
| * | ||
| * @return array | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| public function dataTest(): array | ||
| { | ||
| $xml = new \SimpleXMLElement('<field | ||
| name="unittest" | ||
| type="text" | ||
| validate="boolean" | ||
| />'); | ||
|
|
||
| return [ | ||
| [true, $xml, 'true'], | ||
| [true, $xml, 'false'], | ||
| [true, $xml, '0'], | ||
| [true, $xml, '1'], | ||
| [true, $xml, 'TRUE'], | ||
| [true, $xml, 'FALSE'], | ||
| [true, $xml, 'True'], | ||
| [true, $xml, 'False'], | ||
| [false, $xml, ''], | ||
| [false, $xml, 'tu re'], | ||
| [false, $xml, 'false1'], | ||
| [false, $xml, 'none'], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Tests the BooleanRule::test method. | ||
| * | ||
| * @param bool $expected The expected test result | ||
| * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object. | ||
| * @param string $value The form field value to validate. | ||
| * | ||
| * @return void | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| * @dataProvider dataTest | ||
| */ | ||
| public function testRule(bool $expected, \SimpleXMLElement $element, string $value): void | ||
| { | ||
| $this->assertEquals($expected, (new BooleanRule())->test($element, $value)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @package Joomla.UnitTest | ||
| * @subpackage Form | ||
| * | ||
| * @copyright (C) 2026 Open Source Matters, Inc. <https://www.joomla.org> | ||
| * @license GNU General Public License version 2 or later; see LICENSE.txt | ||
| */ | ||
|
|
||
| namespace Joomla\Tests\Unit\Libraries\Cms\Form\Rule; | ||
|
|
||
| use Joomla\CMS\Form\Rule\CalendarRule; | ||
| use Joomla\Tests\Unit\UnitTestCase; | ||
|
|
||
| /** | ||
| * Test class for CalendarRule. | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| class CalendarRuleTest extends UnitTestCase | ||
| { | ||
| /** | ||
| * Test data for the testRule method | ||
| * | ||
| * @return array | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| public function dataTest(): array | ||
| { | ||
| $xml = new \SimpleXMLElement('<field | ||
| name="unittest" | ||
| type="text" | ||
| validate="calendar" | ||
| />'); | ||
|
|
||
| return [ | ||
| [true, $xml, ''], | ||
| [true, $xml, 'now'], | ||
| [true, $xml, 'NOW'], | ||
| [true, $xml, 'Now'], | ||
| [true, $xml, '2026-05-01'], | ||
| [true, $xml, '2026-05-01 00:00:00'], | ||
| [true, $xml, '2028-10-13 14:15:16'], | ||
| [false, $xml, 'test'], | ||
| [false, $xml, '2026-05-01 12:65:00'], | ||
| [false, $xml, '2026-05-01 26:30:00'], | ||
| [false, $xml, '2026-13-01'], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Tests the CalendarRule::test method. | ||
| * | ||
| * @param bool $expected The expected test result | ||
| * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object. | ||
| * @param string $value The form field value to validate. | ||
| * | ||
| * @return void | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| * @dataProvider dataTest | ||
| */ | ||
| public function testRule(bool $expected, \SimpleXMLElement $element, string $value): void | ||
| { | ||
| $this->assertEquals($expected, (new CalendarRule())->test($element, $value)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @package Joomla.UnitTest | ||
| * @subpackage Form | ||
| * | ||
| * @copyright (C) 2026 Open Source Matters, Inc. <https://www.joomla.org> | ||
| * @license GNU General Public License version 2 or later; see LICENSE.txt | ||
| */ | ||
|
|
||
| namespace Joomla\Tests\Unit\Libraries\Cms\Form\Rule; | ||
|
|
||
| use Joomla\CMS\Form\Rule\ColorRule; | ||
| use Joomla\Tests\Unit\UnitTestCase; | ||
|
|
||
| /** | ||
| * Test class for ColorRule. | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| class ColorRuleTest extends UnitTestCase | ||
| { | ||
| /** | ||
| * Test data for the testRule method | ||
| * | ||
| * @return array | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| public function dataTest(): array | ||
| { | ||
| $xml = new \SimpleXMLElement('<field | ||
| name="unittest" | ||
| type="text" | ||
| validate="color" | ||
| />'); | ||
|
|
||
| return [ | ||
| [true, $xml, ''], | ||
| [true, $xml, '#000000'], | ||
| [true, $xml, '#FFFFFF'], | ||
| [true, $xml, '#FF0000'], | ||
| [true, $xml, '#00FF00'], | ||
| [true, $xml, '#0000ff'], | ||
| [true, $xml, '#c0a'], | ||
| [true, $xml, '#4FF'], | ||
| [false, $xml, 'test'], | ||
| [false, $xml, '451'], | ||
| [false, $xml, '123456'], | ||
| [false, $xml, '#12345G'], | ||
| [false, $xml, '#4H0'], | ||
| [false, $xml, '#FFFF'], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Tests the ColorRule::test method. | ||
| * | ||
| * @param bool $expected The expected test result | ||
| * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object. | ||
| * @param string $value The form field value to validate. | ||
| * | ||
| * @return void | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| * @dataProvider dataTest | ||
| */ | ||
| public function testRule(bool $expected, \SimpleXMLElement $element, string $value): void | ||
| { | ||
| $this->assertEquals($expected, (new ColorRule())->test($element, $value)); | ||
| } | ||
| } |
73 changes: 73 additions & 0 deletions
73
tests/Unit/Libraries/Cms/Form/Rule/CssIdentifierRuleTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @package Joomla.UnitTest | ||
| * @subpackage Form | ||
| * | ||
| * @copyright (C) 2026 Open Source Matters, Inc. <https://www.joomla.org> | ||
| * @license GNU General Public License version 2 or later; see LICENSE.txt | ||
| */ | ||
|
|
||
| namespace Joomla\Tests\Unit\Libraries\Cms\Form\Rule; | ||
|
|
||
| use Joomla\CMS\Form\Rule\CssIdentifierRule; | ||
| use Joomla\Tests\Unit\UnitTestCase; | ||
|
|
||
| /** | ||
| * Test class for CssIdentifierRule. | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| class CssIdentifierRuleTest extends UnitTestCase | ||
| { | ||
| /** | ||
| * Test data for the testRule method | ||
| * | ||
| * @return array | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| public function dataTest(): array | ||
| { | ||
| $xml = new \SimpleXMLElement('<field | ||
| name="unittest" | ||
| type="text" | ||
| validate="cssidentifier" | ||
| />'); | ||
|
|
||
| return [ | ||
| [true, $xml, ''], | ||
| [true, $xml, 'header'], | ||
| [true, $xml, 'main-content'], | ||
| [true, $xml, 'footer'], | ||
| [true, $xml, '-sidebar'], | ||
| [true, $xml, 'navigation'], | ||
| [true, $xml, 'hero-section'], | ||
| [true, $xml, 'login-form2'], | ||
| [true, $xml, 'user_profile'], | ||
| [true, $xml, '@search-box'], | ||
| [true, $xml, 'contact-form table'], | ||
| [false, $xml, '2footer'], | ||
| [false, $xml, '-5navigation'], | ||
| [false, $xml, '--header'], | ||
| [false, $xml, 'main-content!'], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Tests the CssIdentifierRule::test method. | ||
| * | ||
| * @param bool $expected The expected test result | ||
| * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object. | ||
| * @param string $value The form field value to validate. | ||
| * | ||
| * @return void | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| * @dataProvider dataTest | ||
| */ | ||
| public function testRule(bool $expected, \SimpleXMLElement $element, string $value): void | ||
| { | ||
| $this->assertEquals($expected, (new CssIdentifierRule())->test($element, $value)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @package Joomla.UnitTest | ||
| * @subpackage Form | ||
| * | ||
| * @copyright (C) 2026 Open Source Matters, Inc. <https://www.joomla.org> | ||
| * @license GNU General Public License version 2 or later; see LICENSE.txt | ||
| */ | ||
|
|
||
| namespace Joomla\Tests\Unit\Libraries\Cms\Form\Rule; | ||
|
|
||
| use Joomla\CMS\Form\Rule\EmailRule; | ||
| use Joomla\Tests\Unit\UnitTestCase; | ||
|
|
||
| /** | ||
| * Test class for EmailRule. | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| class EmailRuleTest extends UnitTestCase | ||
| { | ||
| /** | ||
| * Test data for the testRule method | ||
| * | ||
| * @return array | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| public function dataTest(): array | ||
| { | ||
| $xml = new \SimpleXMLElement('<field | ||
| name="unittest" | ||
| type="text" | ||
| validate="email" | ||
| />'); | ||
|
|
||
| return [ | ||
| [true, $xml, '[email protected]'], | ||
heelc29 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [true, $xml, 'user@example'], | ||
| [true, $xml, '[email protected]'], | ||
| [true, $xml, '[email protected]'], | ||
| [true, $xml, '[email protected]'], | ||
| [true, $xml, 'special.chars!#$%&\'*+/=?^_{|}[email protected]'], | ||
| [true, $xml, '[email protected]'], | ||
| [false, $xml, 'userexample.com'], | ||
| [false, $xml, '@example.com'], | ||
| [false, $xml, 'user@'], | ||
| [false, $xml, '[email protected]'], | ||
| [false, $xml, '[email protected]'], | ||
| [false, $xml, 'user@@example.com'], | ||
| [false, $xml, 'user@ex!ample.com'], | ||
| [false, $xml, 'user@exam_ple.com'], | ||
| [false, $xml, '[email protected].'], | ||
| [false, $xml, 'user [email protected]'], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Tests the EmailRule::test method. | ||
| * | ||
| * @param bool $expected The expected test result | ||
| * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object. | ||
| * @param string $value The form field value to validate. | ||
| * | ||
| * @return void | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| * @dataProvider dataTest | ||
| */ | ||
| public function testRule(bool $expected, \SimpleXMLElement $element, string $value): void | ||
| { | ||
| if ($expected) { | ||
| $this->assertTrue((new EmailRule())->test($element, $value)); | ||
| } else { | ||
| $this->expectException(\UnexpectedValueException::class); | ||
| (new EmailRule())->test($element, $value); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.