From b8cee52bbc820caab90a73f33c30ebea95009790 Mon Sep 17 00:00:00 2001 From: Christian Heel <66922325+heelc29@users.noreply.github.com> Date: Tue, 20 Jan 2026 11:04:20 +0100 Subject: [PATCH 1/4] [5.4] add unit tests for form rules --- .../Cms/Form/Rule/BooleanRuleTest.php | 70 +++++++++++++ .../Cms/Form/Rule/CalendarRuleTest.php | 69 +++++++++++++ .../Libraries/Cms/Form/Rule/ColorRuleTest.php | 72 ++++++++++++++ .../Cms/Form/Rule/CssIdentifierRuleTest.php | 73 ++++++++++++++ .../Libraries/Cms/Form/Rule/EmailRuleTest.php | 79 +++++++++++++++ .../Cms/Form/Rule/EqualsRuleTest.php | 99 +++++++++++++++++++ .../Cms/Form/Rule/FilePathRuleTest.php | 10 +- .../Form/Rule/FolderPathExistsRuleTest.php | 8 +- .../Cms/Form/Rule/ModuleLayoutRuleTest.php | 97 ++++++++++++++++++ .../Cms/Form/Rule/NotequalsRuleTest.php | 96 ++++++++++++++++++ .../Cms/Form/Rule/NumberRuleTest.php | 81 +++++++++++++++ .../Cms/Form/Rule/OptionsRuleTest.php | 85 ++++++++++++++++ .../Cms/Form/Rule/ShowOnRuleTest.php | 2 +- .../Libraries/Cms/Form/Rule/UrlRuleTest.php | 94 ++++++++++++++++++ 14 files changed, 925 insertions(+), 10 deletions(-) create mode 100644 tests/Unit/Libraries/Cms/Form/Rule/BooleanRuleTest.php create mode 100644 tests/Unit/Libraries/Cms/Form/Rule/CalendarRuleTest.php create mode 100644 tests/Unit/Libraries/Cms/Form/Rule/ColorRuleTest.php create mode 100644 tests/Unit/Libraries/Cms/Form/Rule/CssIdentifierRuleTest.php create mode 100644 tests/Unit/Libraries/Cms/Form/Rule/EmailRuleTest.php create mode 100644 tests/Unit/Libraries/Cms/Form/Rule/EqualsRuleTest.php create mode 100644 tests/Unit/Libraries/Cms/Form/Rule/ModuleLayoutRuleTest.php create mode 100644 tests/Unit/Libraries/Cms/Form/Rule/NotequalsRuleTest.php create mode 100644 tests/Unit/Libraries/Cms/Form/Rule/NumberRuleTest.php create mode 100644 tests/Unit/Libraries/Cms/Form/Rule/OptionsRuleTest.php create mode 100644 tests/Unit/Libraries/Cms/Form/Rule/UrlRuleTest.php diff --git a/tests/Unit/Libraries/Cms/Form/Rule/BooleanRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/BooleanRuleTest.php new file mode 100644 index 0000000000000..8667ec6704cab --- /dev/null +++ b/tests/Unit/Libraries/Cms/Form/Rule/BooleanRuleTest.php @@ -0,0 +1,70 @@ + + * @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(''); + + 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, 'ture'], + [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 `` 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)); + } +} diff --git a/tests/Unit/Libraries/Cms/Form/Rule/CalendarRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/CalendarRuleTest.php new file mode 100644 index 0000000000000..b63b6d795f686 --- /dev/null +++ b/tests/Unit/Libraries/Cms/Form/Rule/CalendarRuleTest.php @@ -0,0 +1,69 @@ + + * @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(''); + + 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 `` 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)); + } +} diff --git a/tests/Unit/Libraries/Cms/Form/Rule/ColorRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/ColorRuleTest.php new file mode 100644 index 0000000000000..62433ada3d52d --- /dev/null +++ b/tests/Unit/Libraries/Cms/Form/Rule/ColorRuleTest.php @@ -0,0 +1,72 @@ + + * @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(''); + + 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 `` 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)); + } +} diff --git a/tests/Unit/Libraries/Cms/Form/Rule/CssIdentifierRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/CssIdentifierRuleTest.php new file mode 100644 index 0000000000000..21700aae07a4f --- /dev/null +++ b/tests/Unit/Libraries/Cms/Form/Rule/CssIdentifierRuleTest.php @@ -0,0 +1,73 @@ + + * @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(''); + + 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 `` 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)); + } +} diff --git a/tests/Unit/Libraries/Cms/Form/Rule/EmailRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/EmailRuleTest.php new file mode 100644 index 0000000000000..78237c7afb166 --- /dev/null +++ b/tests/Unit/Libraries/Cms/Form/Rule/EmailRuleTest.php @@ -0,0 +1,79 @@ + + * @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(''); + + return [ + [true, $xml, 'user@example.com'], + [true, $xml, 'firstname.name@example.de'], + [true, $xml, 'user+alias@example-mail.de'], + [true, $xml, 'john_doe@example123.net'], + [true, $xml, 'special.chars!#$%&\'*+/=?^_{|}~-user@example.org'], + [true, $xml, 'user@sub.domain.co.uk'], + [false, $xml, 'userexample.com'], + [false, $xml, '@example.com'], + [false, $xml, 'user@'], + [false, $xml, 'user@.example.com'], + [false, $xml, 'user@example..com'], + [false, $xml, 'user@@example.com'], + [false, $xml, 'user@ex!ample.com'], + [false, $xml, 'user@exam_ple.com'], + [false, $xml, 'user@example.com.'], + [false, $xml, 'user name@example.com'], + ]; + } + + /** + * Tests the EmailRule::test method. + * + * @param bool $expected The expected test result + * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `` 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); + } + } +} diff --git a/tests/Unit/Libraries/Cms/Form/Rule/EqualsRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/EqualsRuleTest.php new file mode 100644 index 0000000000000..97e3baffb29b1 --- /dev/null +++ b/tests/Unit/Libraries/Cms/Form/Rule/EqualsRuleTest.php @@ -0,0 +1,99 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +namespace Joomla\Tests\Unit\Libraries\Cms\Form\Rule; + +use Joomla\CMS\Form\Form; +use Joomla\CMS\Form\Rule\EqualsRule; +use Joomla\Registry\Registry; +use Joomla\Tests\Unit\UnitTestCase; + +/** + * Test class for EqualsRule. + * + * @since __DEPLOY_VERSION__ + */ +class EqualsRuleTest extends UnitTestCase +{ + /** + * Test data for the testRule method + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public function dataTest(): array + { + $xml = new \SimpleXMLElement(''); + $xml2 = new \SimpleXMLElement(''); + $form = $this->createMock(Form::class); + + return [ + [true, $xml, 'testvalue', null, new Registry(['testfield' => 'testvalue']), $form], + [true, $xml, 'testvalue', '', new Registry(['testfield' => 'testvalue']), $form], + [true, $xml, 'testvaluegroup', 'user', new Registry(['user' => ['testfield' => 'testvaluegroup']]), $form], + [true, $xml, '', null, new Registry(), $form], + [true, $xml, '1', null, new Registry(['testfield' => '1']), $form], + [true, $xml, '2', null, new Registry(['testfield' => '02']), $form], + [true, $xml, '3', null, new Registry(['testfield' => 3]), $form], + [true, $xml, '04', null, new Registry(['testfield' => '4']), $form], + [true, $xml, '5', null, new Registry(['testfield' => 5]), $form], + [true, $xml, 6, null, new Registry(['testfield' => '6']), $form], + [true, $xml, '0', null, new Registry(['testfield' => false]), $form], + [true, $xml, 0, null, new Registry(['testfield' => false]), $form], + [true, $xml, 4, null, new Registry(['testfield' => true]), $form], + [false, $xml, 'testvalue', null, new Registry(), $form], + [false, $xml, 'testvalue', null, new Registry(['testfield' => '']), $form], + [false, $xml, 'TESTVALUE', null, new Registry(['testfield' => 'testvalue']), $form], + [false, $xml, 'testvalue2', null, new Registry(['testfield' => 'testvalue']), $form], + [false, $xml, '', null, new Registry(['testfield' => 'testvalue']), $form], + [false, $xml, 'testvaluegroup', 'a', new Registry(['user' => ['testfield' => 'testvaluegroup']]), $form], + [false, $xml, 'a', null, new Registry(['testfield' => 0]), $form], + [\InvalidArgumentException::class, $xml, '', null, null, null], + [\InvalidArgumentException::class, $xml, '', null, new Registry(), null], + [\InvalidArgumentException::class, $xml, '', null, null, $form], + [\UnexpectedValueException::class, $xml2, '', null, null, null], + ]; + } + + /** + * Tests the EqualsRule::test method. + * + * @param bool|string $expected The expected test result + * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `` tag for the form field object. + * @param string|int $value The form field value to validate. + * @param ?string $group Group name + * @param ?Registry $input Input registry + * @param ?Form $form Form object + * + * @return void + * + * @since __DEPLOY_VERSION__ + * @dataProvider dataTest + */ + public function testRule(bool|string $expected, \SimpleXMLElement $element, string|int $value, ?string $group, ?Registry $input, ?Form $form): void + { + if (is_string($expected) && class_exists($expected)) { + $this->expectException($expected); + (new EqualsRule())->test($element, $value, $group, $input, $form); + } + + $this->assertEquals($expected, (new EqualsRule())->test($element, $value, $group, $input, $form)); + } +} diff --git a/tests/Unit/Libraries/Cms/Form/Rule/FilePathRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/FilePathRuleTest.php index 21b17bffe96e9..ec2298c139382 100644 --- a/tests/Unit/Libraries/Cms/Form/Rule/FilePathRuleTest.php +++ b/tests/Unit/Libraries/Cms/Form/Rule/FilePathRuleTest.php @@ -2,7 +2,7 @@ /** * @package Joomla.UnitTest - * @subpackage HTML + * @subpackage Form * * @copyright (C) 2021 Open Source Matters, Inc. * @license GNU General Public License version 2 or later; see LICENSE.txt @@ -72,16 +72,16 @@ public function dataTest(): array /** * Tests the FilePathRule::test method. * - * @param string $expected The expected test result - * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `` tag for the form field object. - * @param mixed $value The form field value to validate. + * @param bool $expected The expected test result + * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `` tag for the form field object. + * @param string $value The form field value to validate. * * @return void * * @since 3.9.26 * @dataProvider dataTest */ - public function testRule($expected, $element, $value) + public function testRule(bool $expected, \SimpleXMLElement $element, string $value): void { $this->assertEquals($expected, (new FilePathRule())->test($element, $value)); } diff --git a/tests/Unit/Libraries/Cms/Form/Rule/FolderPathExistsRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/FolderPathExistsRuleTest.php index e522c9b2fcee4..44e87a186df19 100644 --- a/tests/Unit/Libraries/Cms/Form/Rule/FolderPathExistsRuleTest.php +++ b/tests/Unit/Libraries/Cms/Form/Rule/FolderPathExistsRuleTest.php @@ -2,7 +2,7 @@ /** * @package Joomla.UnitTest - * @subpackage Rule + * @subpackage Form * * @copyright (C) 2021 Open Source Matters, Inc. * @license GNU General Public License version 2 or later; see LICENSE.txt @@ -76,16 +76,16 @@ public function dataTest(): array /** * Tests the FolderPathExistsRule::test method. * - * @param string $expected The expected test result + * @param bool $expected The expected test result * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `` tag for the form field object. - * @param mixed $value The form field value to validate. + * @param string $value The form field value to validate. * * @return void * * @since 4.0.0 * @dataProvider dataTest */ - public function testRule($expected, $element, $value) + public function testRule(bool $expected, \SimpleXMLElement $element, string $value): void { $this->assertEquals($expected, (new FolderPathExistsRule())->test($element, $value)); } diff --git a/tests/Unit/Libraries/Cms/Form/Rule/ModuleLayoutRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/ModuleLayoutRuleTest.php new file mode 100644 index 0000000000000..c398723eaf068 --- /dev/null +++ b/tests/Unit/Libraries/Cms/Form/Rule/ModuleLayoutRuleTest.php @@ -0,0 +1,97 @@ + + * @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\ModuleLayoutRule; +use Joomla\Tests\Unit\UnitTestCase; + +/** + * Test class for ModuleLayoutRule. + * + * @since __DEPLOY_VERSION__ + */ +class ModuleLayoutRuleTest extends UnitTestCase +{ + /** + * Test data for the testRule method + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public function dataTest(): array + { + $xml = new \SimpleXMLElement(''); + + return [ + [true, $xml, 'abc'], + [true, $xml, 'abc-def'], + [true, $xml, 'abc.def'], + [true, $xml, 'abc-123.def'], + [true, $xml, 'prefix:abc'], + [true, $xml, 'pre_fix-1:abc.def'], + [true, $xml, 'a'], + [true, $xml, 'A123'], + [true, $xml, 'Z-9.test'], + [true, $xml, 'x_y-z:abc.def'], + [true, $xml, 'abc.def.ghi'], + [true, $xml, 'abc-123.456-def'], + [true, $xml, 'prefix:abc-123.def'], + [true, $xml, 'PREFIX:Value'], + [true, $xml, 'x:abc'], + [true, $xml, 'abc123'], + [true, $xml, 'abc.def123'], + [true, $xml, 'abc-def.ghi-jkl'], + [true, $xml, 'abc.def-ghi.jkl'], + [true, $xml, '-abc'], + [true, $xml, '_:abc'], + [false, $xml, ''], + [false, $xml, '.abc'], + [false, $xml, 'abc def'], + [false, $xml, 'pre fix:abc'], + [false, $xml, 'abc::def'], + [false, $xml, 'abc@def'], + [false, $xml, 'abc/def'], + [false, $xml, 'prefix:.abc'], + [false, $xml, 'prefix:'], + [false, $xml, '.'], + [false, $xml, 'äbc'], + [false, $xml, 'prefix:..abc'], + [false, $xml, 'abc,def'], + [false, $xml, 'abc!def'], + [false, $xml, 'abc?def'], + [false, $xml, '$abc'], + [false, $xml, 'abc$def'], + [false, $xml, 'prefix:äöü'], + ]; + } + + /** + * Tests the ModuleLayoutRule::test method. + * + * @param bool $expected The expected test result + * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `` 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 ModuleLayoutRule())->test($element, $value)); + } +} diff --git a/tests/Unit/Libraries/Cms/Form/Rule/NotequalsRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/NotequalsRuleTest.php new file mode 100644 index 0000000000000..6a819a3e56354 --- /dev/null +++ b/tests/Unit/Libraries/Cms/Form/Rule/NotequalsRuleTest.php @@ -0,0 +1,96 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +namespace Joomla\Tests\Unit\Libraries\Cms\Form\Rule; + +use Joomla\CMS\Form\Form; +use Joomla\CMS\Form\Rule\NotequalsRule; +use Joomla\Registry\Registry; +use Joomla\Tests\Unit\UnitTestCase; + +/** + * Test class for NotequalsRule. + * + * @since __DEPLOY_VERSION__ + */ +class NotequalsRuleTest extends UnitTestCase +{ + /** + * Test data for the testRule method + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public function dataTest(): array + { + $xml = new \SimpleXMLElement(''); + $xml2 = new \SimpleXMLElement(''); + + return [ + [true, $xml, 'testvalue', null, new Registry(), null], + [true, $xml, 'testvalue', null, new Registry(['testfield' => '']), null], + [true, $xml, 'TESTVALUE', null, new Registry(['testfield' => 'testvalue']), null], + [true, $xml, 'testvalue2', null, new Registry(['testfield' => 'testvalue']), null], + [true, $xml, '', null, new Registry(['testfield' => 'testvalue']), null], + [true, $xml, 'testvaluegroup', 'user', new Registry(['user' => ['testfield' => 'testvaluegroup']]), null], + [true, $xml, 'testvaluegroup', 'a', new Registry(['user' => ['testfield' => 'testvaluegroup']]), null], + [true, $xml, 'a', null, new Registry(['testfield' => 0]), null], + [false, $xml, 'testvalue', null, new Registry(['testfield' => 'testvalue']), null], + [false, $xml, 'testvalue', '', new Registry(['testfield' => 'testvalue']), null], + [false, $xml, '', null, new Registry(), null], + [false, $xml, '1', null, new Registry(['testfield' => '1']), null], + [false, $xml, '2', null, new Registry(['testfield' => '02']), null], + [false, $xml, '3', null, new Registry(['testfield' => 3]), null], + [false, $xml, '04', null, new Registry(['testfield' => '4']), null], + [false, $xml, '5', null, new Registry(['testfield' => 5]), null], + [false, $xml, 6, null, new Registry(['testfield' => '6']), null], + [false, $xml, '0', null, new Registry(['testfield' => false]), null], + [false, $xml, 0, null, new Registry(['testfield' => false]), null], + [false, $xml, 4, null, new Registry(['testfield' => true]), null], + [\InvalidArgumentException::class, $xml, '', null, null, null], + [\UnexpectedValueException::class, $xml2, '', null, null, null], + ]; + } + + /** + * Tests the NotequalsRule::test method. + * + * @param bool|string $expected The expected test result + * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `` tag for the form field object. + * @param string|int $value The form field value to validate. + * @param ?string $group Group name + * @param ?Registry $input Input registry + * @param ?Form $form Form object + * + * @return void + * + * @since __DEPLOY_VERSION__ + * @dataProvider dataTest + */ + public function testRule(bool|string $expected, \SimpleXMLElement $element, string|int $value, ?string $group, ?Registry $input, ?Form $form): void + { + if (is_string($expected) && class_exists($expected)) { + $this->expectException($expected); + (new NotequalsRule())->test($element, $value, $group, $input, $form); + } + + $this->assertEquals($expected, (new NotequalsRule())->test($element, $value, $group, $input, $form)); + } +} diff --git a/tests/Unit/Libraries/Cms/Form/Rule/NumberRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/NumberRuleTest.php new file mode 100644 index 0000000000000..4c2e2cd5951ef --- /dev/null +++ b/tests/Unit/Libraries/Cms/Form/Rule/NumberRuleTest.php @@ -0,0 +1,81 @@ + + * @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\NumberRule; +use Joomla\Tests\Unit\UnitTestCase; + +/** + * Test class for NumberRule. + * + * @since __DEPLOY_VERSION__ + */ +class NumberRuleTest extends UnitTestCase +{ + /** + * Test data for the testRule method + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public function dataTest(): array + { + $xml = new \SimpleXMLElement(''); + + return [ + [true, $xml, '5'], + [true, $xml, '40'], + [true, $xml, '40.00'], + [true, $xml, '26.164842'], + [true, $xml, '018'], + [true, $xml, '1e1'], + [true, $xml, '14 '], + [true, $xml, '5 4'], + [true, $xml, '28hi'], + [true, $xml, ' 0018abc'], + [false, $xml, ''], + [false, $xml, '0'], + [false, $xml, '4.99999'], + [false, $xml, '40.00001'], + [false, $xml, '-8'], + [false, $xml, '-48.5'], + [false, $xml, '115'], + [false, $xml, '1e2'], + [false, $xml, 'abc'], + [false, $xml, 'a8'], + ]; + } + + /** + * Tests the NumberRule::test method. + * + * @param bool $expected The expected test result + * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `` 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 NumberRule())->test($element, $value)); + } +} diff --git a/tests/Unit/Libraries/Cms/Form/Rule/OptionsRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/OptionsRuleTest.php new file mode 100644 index 0000000000000..e416483c42648 --- /dev/null +++ b/tests/Unit/Libraries/Cms/Form/Rule/OptionsRuleTest.php @@ -0,0 +1,85 @@ + + * @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\OptionsRule; +use Joomla\Tests\Unit\UnitTestCase; + +/** + * Test class for OptionsRule. + * + * @since __DEPLOY_VERSION__ + */ +class OptionsRuleTest extends UnitTestCase +{ + /** + * Test data for the testRule method + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public function dataTest(): array + { + $xml = new \SimpleXMLElement(' + + + + + + + + + + + + '); + + return [ + [true, $xml, 'op1'], + [true, $xml, 'op2'], + [true, $xml, 'op3'], + [true, $xml, 'g1op1'], + [true, $xml, 'g2op2'], + [true, $xml, ['op1', 'op3']], + [true, $xml, ['op2', 'g2op1']], + [true, $xml, []], + [false, $xml, null], + [false, $xml, ''], + [false, $xml, 'op4'], + [false, $xml, 'g1op3'], + [false, $xml, ['op2', 'op4']], + [false, $xml, ['op1', 'g3op1']], + ]; + } + + /** + * Tests the OptionsRule::test method. + * + * @param bool $expected The expected test result + * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `` tag for the form field object. + * @param null|string|array $value The form field value to validate. + * + * @return void + * + * @since __DEPLOY_VERSION__ + * @dataProvider dataTest + */ + public function testRule(bool $expected, \SimpleXMLElement $element, null|string|array $value): void + { + $this->assertEquals($expected, (new OptionsRule())->test($element, $value)); + } +} diff --git a/tests/Unit/Libraries/Cms/Form/Rule/ShowOnRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/ShowOnRuleTest.php index e8533e8281492..34d9223e9ac72 100644 --- a/tests/Unit/Libraries/Cms/Form/Rule/ShowOnRuleTest.php +++ b/tests/Unit/Libraries/Cms/Form/Rule/ShowOnRuleTest.php @@ -76,7 +76,7 @@ public function dataTest(): array /** * Tests the ShowOnRule::test method. * - * @param boolean $expected The expected test result + * @param bool $expected The expected test result * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `` tag for the form field object. * @param string $value The form field value to validate. * diff --git a/tests/Unit/Libraries/Cms/Form/Rule/UrlRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/UrlRuleTest.php new file mode 100644 index 0000000000000..42f7010e72236 --- /dev/null +++ b/tests/Unit/Libraries/Cms/Form/Rule/UrlRuleTest.php @@ -0,0 +1,94 @@ + + * @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\UrlRule; +use Joomla\Tests\Unit\UnitTestCase; + +/** + * Test class for UrlRule. + * + * @since __DEPLOY_VERSION__ + */ +class UrlRuleTest extends UnitTestCase +{ + /** + * Test data for the testRule method + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public function dataTest(): array + { + $xml = new \SimpleXMLElement(''); + $xmlschemes = new \SimpleXMLElement(''); + $xmlrelative = new \SimpleXMLElement(''); + + return [ + [true, $xml, 'https://example.com'], + [true, $xml, 'https://example.com/test'], + [true, $xml, 'https://example.com:8080'], + [true, $xml, 'ftp://example.com/resource'], + [true, $xml, 'mailto:test@example.com'], + [true, $xml, 'tel:+49123456789'], + [true, $xml, 'file:///etc/passwd'], + [true, $xml, 'gopher://example.com'], + [true, $xmlschemes, 'https://example.com'], + [true, $xmlrelative, 'https://example.com'], + [true, $xmlrelative, '/relative/path'], + [false, $xml, ''], + [false, $xml, 'invalid://example.com'], + [false, $xml, 'example.com'], + [false, $xml, '/relative/path'], + [false, $xml, 'http:///example.com'], + [false, $xml, 'http:example.com'], + [false, $xml, "https://exa\x80mple.com"], + [false, $xml, "https://example.com/pa\x80th"], + [false, $xml, 'https://example.com:0'], + [false, $xmlschemes, 'ftp://example.com'], + ]; + } + + /** + * Tests the UrlRule::test method. + * + * @param bool $expected The expected test result + * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `` 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 UrlRule())->test(clone $element, $value)); + } +} From 9388f52de4a0b013d7e5b92a62fbac401b7909df Mon Sep 17 00:00:00 2001 From: Christian Heel <66922325+heelc29@users.noreply.github.com> Date: Tue, 20 Jan 2026 11:29:14 +0100 Subject: [PATCH 2/4] cs --- tests/Unit/Libraries/Cms/Form/Rule/EqualsRuleTest.php | 2 +- tests/Unit/Libraries/Cms/Form/Rule/NotequalsRuleTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Libraries/Cms/Form/Rule/EqualsRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/EqualsRuleTest.php index 97e3baffb29b1..79494e7152518 100644 --- a/tests/Unit/Libraries/Cms/Form/Rule/EqualsRuleTest.php +++ b/tests/Unit/Libraries/Cms/Form/Rule/EqualsRuleTest.php @@ -89,7 +89,7 @@ public function dataTest(): array */ public function testRule(bool|string $expected, \SimpleXMLElement $element, string|int $value, ?string $group, ?Registry $input, ?Form $form): void { - if (is_string($expected) && class_exists($expected)) { + if (\is_string($expected) && class_exists($expected)) { $this->expectException($expected); (new EqualsRule())->test($element, $value, $group, $input, $form); } diff --git a/tests/Unit/Libraries/Cms/Form/Rule/NotequalsRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/NotequalsRuleTest.php index 6a819a3e56354..b8c5ac95f1fc9 100644 --- a/tests/Unit/Libraries/Cms/Form/Rule/NotequalsRuleTest.php +++ b/tests/Unit/Libraries/Cms/Form/Rule/NotequalsRuleTest.php @@ -86,7 +86,7 @@ public function dataTest(): array */ public function testRule(bool|string $expected, \SimpleXMLElement $element, string|int $value, ?string $group, ?Registry $input, ?Form $form): void { - if (is_string($expected) && class_exists($expected)) { + if (\is_string($expected) && class_exists($expected)) { $this->expectException($expected); (new NotequalsRule())->test($element, $value, $group, $input, $form); } From 8eccb9b7e6a36c807dde61307f20594d00e77a33 Mon Sep 17 00:00:00 2001 From: Christian Heel <66922325+heelc29@users.noreply.github.com> Date: Tue, 20 Jan 2026 11:33:21 +0100 Subject: [PATCH 3/4] intentional typo --- tests/Unit/Libraries/Cms/Form/Rule/BooleanRuleTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Libraries/Cms/Form/Rule/BooleanRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/BooleanRuleTest.php index 8667ec6704cab..75a2b1630e0cb 100644 --- a/tests/Unit/Libraries/Cms/Form/Rule/BooleanRuleTest.php +++ b/tests/Unit/Libraries/Cms/Form/Rule/BooleanRuleTest.php @@ -45,7 +45,7 @@ public function dataTest(): array [true, $xml, 'True'], [true, $xml, 'False'], [false, $xml, ''], - [false, $xml, 'ture'], + [false, $xml, 'tu re'], [false, $xml, 'false1'], [false, $xml, 'none'], ]; From 9d8791a294dba1559bb898e594cc0d739d76a594 Mon Sep 17 00:00:00 2001 From: Christian Heel <66922325+heelc29@users.noreply.github.com> Date: Tue, 20 Jan 2026 12:14:39 +0100 Subject: [PATCH 4/4] add internal email format Co-Authored-By: Brian Teeman --- tests/Unit/Libraries/Cms/Form/Rule/EmailRuleTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Unit/Libraries/Cms/Form/Rule/EmailRuleTest.php b/tests/Unit/Libraries/Cms/Form/Rule/EmailRuleTest.php index 78237c7afb166..e434cf4795786 100644 --- a/tests/Unit/Libraries/Cms/Form/Rule/EmailRuleTest.php +++ b/tests/Unit/Libraries/Cms/Form/Rule/EmailRuleTest.php @@ -37,6 +37,7 @@ public function dataTest(): array return [ [true, $xml, 'user@example.com'], + [true, $xml, 'user@example'], [true, $xml, 'firstname.name@example.de'], [true, $xml, 'user+alias@example-mail.de'], [true, $xml, 'john_doe@example123.net'],