diff --git a/libraries/src/Form/Field/MeterField.php b/libraries/src/Form/Field/MeterField.php index 6bffef494b7cf..1da05e8e8fae2 100644 --- a/libraries/src/Form/Field/MeterField.php +++ b/libraries/src/Form/Field/MeterField.php @@ -112,6 +112,8 @@ public function __get($name) case 'width': case 'animated': case 'color': + case 'max': + case 'min': return $this->$name; } @@ -175,6 +177,8 @@ public function setup(\SimpleXMLElement $element, $value, $group = null) $return = parent::setup($element, $value, $group); if ($return) { + $this->max = isset($this->element['max']) ? (int) $this->element['max'] : $this->max; + $this->min = isset($this->element['min']) ? (int) $this->element['min'] : $this->min; $this->width = isset($this->element['width']) ? (string) $this->element['width'] : ''; $this->color = isset($this->element['color']) ? (string) $this->element['color'] : ''; diff --git a/tests/Unit/Libraries/Cms/Form/Field/MeterFieldTest.php b/tests/Unit/Libraries/Cms/Form/Field/MeterFieldTest.php new file mode 100644 index 0000000000000..ab0baf151ee53 --- /dev/null +++ b/tests/Unit/Libraries/Cms/Form/Field/MeterFieldTest.php @@ -0,0 +1,160 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +namespace Joomla\Tests\Unit\Libraries\Cms\Form\Field; + +use Joomla\CMS\Form\Field\MeterField; + +/** + * Test class for MeterField. + * + * @package Joomla.UnitTest + * @subpackage Form + * @since __DEPLOY_VERSION__ + */ +class MeterFieldTest extends \PHPUnit\Framework\TestCase +{ + /** + * Tests the constructor + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function testIsConstructable() + { + $this->assertInstanceOf(MeterField::class, new MeterField()); + } + + /** + * Tests getting default properties. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function testGetWithDefaultValues() + { + $field = new MeterField(); + + $this->assertSame('Meter', $field->__get('type')); + $this->assertSame(0, $field->__get('min')); + $this->assertSame(100, $field->__get('max')); + $this->assertFalse($field->__get('active')); + $this->assertTrue($field->__get('animated')); + $this->assertNull($field->__get('width')); + $this->assertNull($field->__get('color')); + } + + /** + * Tests setting and getting properties. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function testSetAndGetProperties() + { + $field = new MeterField(); + + $field->__set('min', 25); + $field->__set('max', 250); + $field->__set('width', '200px'); + $field->__set('color', '#ff0000'); + $field->__set('active', 'true'); + $field->__set('animated', 'false'); + + $this->assertSame(25, $field->__get('min')); + $this->assertSame(250, $field->__get('max')); + $this->assertSame('200px', $field->__get('width')); + $this->assertSame('#ff0000', $field->__get('color')); + $this->assertTrue($field->__get('active')); + $this->assertFalse($field->__get('animated')); + } + + /** + * Tests setup method extracts min and max attributes from XML. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function testSetupWithCustomMinMaxAttributes() + { + $field = new MeterField(); + $element = new \SimpleXMLElement(''); + + $this->assertTrue($field->setup($element, 30, null)); + $this->assertSame(10, $field->__get('min')); + $this->assertSame(50, $field->__get('max')); + } + + /** + * Tests setup method with other custom attributes. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function testSetupWithCustomAttributes() + { + $field = new MeterField(); + $element = new \SimpleXMLElement( + '' + ); + + $this->assertTrue($field->setup($element, 50, null)); + $this->assertSame(5, $field->__get('min')); + $this->assertSame(95, $field->__get('max')); + $this->assertSame('150px', $field->__get('width')); + $this->assertSame('blue', $field->__get('color')); + $this->assertTrue($field->__get('active')); + $this->assertFalse($field->__get('animated')); + } + + /** + * Tests that layout data contains the configured min and max values. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function testGetLayoutDataWithCustomMinMax() + { + $field = new MeterField(); + $element = new \SimpleXMLElement(''); + + $field->setup($element, 100, null); + + $layoutData = $this->callGetLayoutData($field); + + $this->assertSame(20, $layoutData['min']); + $this->assertSame(200, $layoutData['max']); + $this->assertSame('300px', $layoutData['width']); + $this->assertSame('green', $layoutData['color']); + $this->assertSame(100, $layoutData['value']); + } + + /** + * Helper to invoke protected getLayoutData method. + * + * @param MeterField $field Field instance + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + protected function callGetLayoutData(MeterField $field): array + { + $reflection = new \ReflectionMethod(MeterField::class, 'getLayoutData'); + + return $reflection->invoke($field); + } +}