From 9fb3ad28e9d661af3130c22e52d4ed81f91a8d1a Mon Sep 17 00:00:00 2001 From: Marcel Thole Date: Tue, 7 Jan 2025 12:54:43 +0100 Subject: [PATCH] use strict checks for strlen and use DataProvider for the tests and add missing testcases for doublequote Signed-off-by: Marcel Thole --- src/HtmlEntities.php | 8 ++--- test/HtmlEntitiesTest.php | 72 ++++++++++++++++++++++++--------------- 2 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/HtmlEntities.php b/src/HtmlEntities.php index cea2718c..17a77f5e 100644 --- a/src/HtmlEntities.php +++ b/src/HtmlEntities.php @@ -7,7 +7,7 @@ use function function_exists; use function htmlentities; use function iconv; -use function is_scalar; +use function is_string; use function strlen; use const ENT_QUOTES; @@ -61,19 +61,19 @@ public function __construct(array $options = []) */ public function filter(mixed $value): mixed { - if (! is_scalar($value)) { + if (! is_string($value) || $value === '') { return $value; } $value = (string) $value; $filtered = htmlentities($value, $this->quoteStyle, $this->encoding, $this->doubleQuote); - if (strlen($value) && ! strlen($filtered)) { + if (strlen($value) > 0 && strlen($filtered) === 0) { if (! function_exists('iconv')) { throw new Exception\DomainException('Encoding mismatch has resulted in htmlentities errors'); } $value = iconv('', $this->encoding . '//IGNORE', $value); $filtered = htmlentities($value, $this->quoteStyle, $this->encoding, $this->doubleQuote); - if (! strlen($filtered)) { + if (strlen($filtered) === 0) { throw new Exception\DomainException('Encoding mismatch has resulted in htmlentities errors'); } } diff --git a/test/HtmlEntitiesTest.php b/test/HtmlEntitiesTest.php index 2776b715..b73ce5d7 100644 --- a/test/HtmlEntitiesTest.php +++ b/test/HtmlEntitiesTest.php @@ -20,37 +20,27 @@ class HtmlEntitiesTest extends TestCase { - /** - * Ensures that the filter follows expected behavior - */ - public function testBasic(): void + #[DataProvider('defaultSettingsDataProvider')] + public function testBasic(string $input, string $expected): void { - $valuesExpected = [ - 'string' => 'string', - '<' => '<', - '>' => '>', - '\'' => ''', - '"' => '"', - '&' => '&', - ]; - $filter = new HtmlEntitiesFilter(); - foreach ($valuesExpected as $input => $output) { - self::assertSame($output, $filter($input)); - } + $filter = new HtmlEntitiesFilter(); + + self::assertSame($expected, $filter($input)); + self::assertSame($expected, $filter->__invoke($input)); + self::assertSame($expected, $filter->filter($input)); } - /** - * Ensure that fluent interfaces are supported - */ - #[Group('Laminas-3172')] - public function testFluentInterface(): void + public static function defaultSettingsDataProvider(): array { - $instance = new HtmlEntitiesFilter([ - 'encoding' => 'UTF-8', - 'quotestyle' => ENT_QUOTES, - 'doublequote' => false, - ]); - self::assertInstanceOf(HtmlEntitiesFilter::class, $instance); + return [ + ['string', 'string'], + ['<', '<'], + ['>', '>'], + ['\'', '''], + ['"', '"'], + ['&', '&'], + ['&', '&amp;'], + ]; } /** @@ -62,6 +52,9 @@ public function testQuoteStyleQuotesEncodeBoth(): void $input = "A 'single' and " . '"double"'; $result = 'A 'single' and "double"'; + $filterWithDefault = new HtmlEntitiesFilter(); + self::assertSame($result, $filterWithDefault->filter($input)); + $filter = new HtmlEntitiesFilter(['quotestyle' => ENT_QUOTES]); self::assertSame($result, $filter->filter($input)); } @@ -92,6 +85,27 @@ public function testQuoteStyleQuotesEncodeNone(): void self::assertSame($result, $filter->filter($input)); } + public function testDoubleQuoteEncodeDefault(): void + { + $input = '&'; + $result = '&amp;'; + + $filterDefault = new HtmlEntitiesFilter(); + self::assertSame($result, $filterDefault->filter($input)); + + $filter = new HtmlEntitiesFilter(['doublequote' => true]); + self::assertSame($result, $filter->filter($input)); + } + + public function testDoubleQuoteEncodeOff(): void + { + $input = '&'; + $result = '&'; + + $filter = new HtmlEntitiesFilter(['doublequote' => false]); + self::assertSame($result, $filter->filter($input)); + } + #[Group('Laminas-11344')] public function testCorrectsForEncodingMismatch(): void { @@ -127,6 +141,10 @@ public static function returnUnfilteredDataProvider(): array return [ [null], [new stdClass()], + [''], + [false], + [true], + [12345], [ [ '<',