Skip to content

[WIP] Add deprecation for passing incorrect type to Escaper #33637

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/internal/Magento/Framework/Escaper.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ public function escapeUrl($string)
*/
public function encodeUrlParam($string)
{
if (!is_string($string)) {
$message = 'Argument #1 passed to %s needs to be an string, but got %s.'
. ' In future versions this will throw an error';

trigger_error(sprintf($message, __METHOD__, gettype($string)), E_USER_DEPRECATED);
}

return $this->getEscaper()->escapeUrl((string)$string);
}

Expand Down Expand Up @@ -330,6 +337,13 @@ function ($matches) {
*/
public function escapeCss($string)
{
if (!is_string($string)) {
$message = 'Argument #1 passed to %s needs to be an string, but got %s.'
. ' In future versions this will throw an error';

trigger_error(sprintf($message, __METHOD__, gettype($string)), E_USER_DEPRECATED);
}

return $this->getEscaper()->escapeCss((string)$string);
}

Expand Down
29 changes: 25 additions & 4 deletions lib/internal/Magento/Framework/Test/Unit/EscaperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,23 @@ public function testEscapeUrl(string $data, string $expected): void
/**
* @covers \Magento\Framework\Escaper::escapeCss
*
* @param string $data
* @param int|string $data
* @param string $expected
* @param string|null $deprecatedType
* @return void
*
* @dataProvider escapeCssDataProvider
*/
public function testEscapeCss($data, string $expected): void
public function testEscapeCss($data, string $expected, string $deprecatedType = null): void
{
if ($deprecatedType) {
$this->expectDeprecation();

$message = 'Magento\Framework\Escaper::escapeCss needs to be an string, but got '
. $deprecatedType . '. In future versions this will throw an error';
$this->expectDeprecationMessage($message);
}

$this->assertEquals($expected, $this->escaper->escapeCss($data));
}

Expand All @@ -387,6 +396,7 @@ public function escapeCssDataProvider(): array
[
'data' => 1,
'expected' => '1',
'deprecatedType' => 'integer'
],
[
'data' => '*%string{foo}%::',
Expand All @@ -398,14 +408,23 @@ public function escapeCssDataProvider(): array
/**
* @covers \Magento\Framework\Escaper::encodeUrlParam
*
* @param string $data
* @param int|string $data
* @param string $expected
* @param string|null $deprecatedType
* @return void
*
* @dataProvider encodeUrlParamDataProvider
*/
public function testEncodeUrlParam($data, string $expected): void
public function testEncodeUrlParam($data, string $expected, string $deprecatedType = null): void
{
if($deprecatedType) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if($deprecatedType) {
if ($deprecatedType) {

$this->expectDeprecation();

$message = 'Magento\Framework\Escaper::encodeUrlParam needs to be an string, but got '
. $deprecatedType . '. In future versions this will throw an error';
$this->expectDeprecationMessage($message);
}

$this->assertEquals($expected, $this->escaper->encodeUrlParam($data));
}

Expand All @@ -426,10 +445,12 @@ public function encodeUrlParamDataProvider(): array
[
'data' => 1,
'expected' => "1",
'deprecatedType' => 'integer'
],
[
'data' => null,
'expected' => "",
'deprecatedType' => 'NULL'
]
];
}
Expand Down