Skip to content

Commit

Permalink
use strict checks for strlen and use DataProvider for the tests and a…
Browse files Browse the repository at this point in the history
…dd missing testcases for doublequote

Signed-off-by: Marcel Thole <[email protected]>
  • Loading branch information
marcelthole committed Jan 7, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 7bea300 commit cf44f2c
Showing 2 changed files with 49 additions and 31 deletions.
8 changes: 4 additions & 4 deletions src/HtmlEntities.php
Original file line number Diff line number Diff line change
@@ -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');
}
}
72 changes: 45 additions & 27 deletions test/HtmlEntitiesTest.php
Original file line number Diff line number Diff line change
@@ -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',
'<' => '&lt;',
'>' => '&gt;',
'\'' => '&#039;',
'"' => '&quot;',
'&' => '&amp;',
];
$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'],
['<', '&lt;'],
['>', '&gt;'],
['\'', '&#039;'],
['"', '&quot;'],
['&', '&amp;'],
['&amp;', '&amp;amp;'],
];
}

/**
@@ -62,6 +52,9 @@ public function testQuoteStyleQuotesEncodeBoth(): void
$input = "A 'single' and " . '"double"';
$result = 'A &#039;single&#039; and &quot;double&quot;';

$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 = '&amp;';
$result = '&amp;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 = '&amp;';
$result = '&amp;';

$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],
[
[
'<',

0 comments on commit cf44f2c

Please sign in to comment.