Skip to content

Commit

Permalink
Fixes Mage_Core_Helper_Url::removeRequestParam() for ___SID/SID (
Browse files Browse the repository at this point in the history
…#4295)

* added tests

* fixes

* return earlier

* return earlier
  • Loading branch information
sreichel authored Oct 23, 2024
1 parent 4fdf499 commit 5207750
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 33 deletions.
31 changes: 23 additions & 8 deletions app/code/core/Mage/Core/Helper/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,28 @@ public function addRequestParam($url, $param)
*/
public function removeRequestParam($url, $paramKey, $caseSensitive = false)
{
$regExpression = '/\\?[^#]*?(' . preg_quote($paramKey, '/') . '\\=[^#&]*&?)/' . ($caseSensitive ? '' : 'i');
while (preg_match($regExpression, $url, $mathes) != 0) {
$paramString = $mathes[1];
if (preg_match('/&$/', $paramString) == 0) {
$url = preg_replace('/(&|\\?)?' . preg_quote($paramString, '/') . '/', '', $url);
} else {
$url = str_replace($paramString, '', $url);
if (!str_contains($url, '?')) {
return $url;
}

list($baseUrl, $query) = explode('?', $url, 2);
// phpcs:ignore Ecg.Security.ForbiddenFunction.Found
parse_str($query, $params);

if (!$caseSensitive) {
$paramsLower = array_change_key_case($params);
$paramKeyLower = strtolower($paramKey);

if (array_key_exists($paramKeyLower, $paramsLower)) {
$params[$paramKey] = $paramsLower[$paramKeyLower];
}
}
return $url;

if (array_key_exists($paramKey, $params)) {
unset($params[$paramKey]);
}

return $baseUrl . ($params === [] ? '' : '?' . http_build_query($params));
}

/**
Expand All @@ -164,6 +176,7 @@ protected function _getSingletonModel($name, $arguments = [])
*/
public function encodePunycode($url)
{
// phpcs:ignore Ecg.Security.ForbiddenFunction.Found
$parsedUrl = parse_url($url);
if (!$this->_isPunycode($parsedUrl['host'])) {
$host = idn_to_ascii($parsedUrl['host']);
Expand All @@ -182,6 +195,7 @@ public function encodePunycode($url)
*/
public function decodePunycode($url)
{
// phpcs:ignore Ecg.Security.ForbiddenFunction.Found
$parsedUrl = parse_url($url);
if ($this->_isPunycode($parsedUrl['host'])) {
$host = idn_to_utf8($parsedUrl['host']);
Expand All @@ -197,6 +211,7 @@ public function decodePunycode($url)
* @param string $host domain name
* @return bool
*/
// phpcs:ignore Ecg.PHP.PrivateClassMember.PrivateClassMemberError
private function _isPunycode($host)
{
if (str_starts_with($host, 'xn--') || str_contains($host, '.xn--')
Expand Down
13 changes: 6 additions & 7 deletions tests/unit/Mage/Core/Helper/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,12 @@ public function provideFormatTimezoneDate(): Generator
$date,
'long'
];
yield 'date short w/ time' => [
$dateShortTime,
$date,
'short',
true,
false,
];
// yield 'date short w/ time' => [
// $dateShortTime,
// $date,
// 'short',
// true,
// ];
}

/**
Expand Down
132 changes: 114 additions & 18 deletions tests/unit/Mage/Core/Helper/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,26 @@

namespace OpenMage\Tests\Unit\Mage\Core\Helper;

use Generator;
use Mage;
use Mage_Core_Helper_Url;
use PHPUnit\Framework\TestCase;

class UrlTest extends TestCase
{
public const TEST_URL_1 = 'http://example.com?foo=ba';
public const TEST_URL_BASE = 'https://example.com';

public const TEST_URL_2 = 'http://example.com?foo=bar&boo=baz';
public const TEST_URL_PARAM = 'https://example.com?foo=bar';

public const TEST_URL_PUNY = 'http://XN--example.com?foo=bar&boo=baz';
public const TEST_URL_PARAMS = 'https://example.com?foo=bar&BOO=baz';

public const TEST_URL_SID1 = 'https://example.com?SID=S&foo=bar&BOO=baz';

public const TEST_URL_SID2 = 'https://example.com?___SID=S&foo=bar&BOO=baz';

public const TEST_URL_SID_BOTH = 'https://example.com?___SID=S&SID=S&foo=bar&BOO=baz';

public const TEST_URL_PUNY = 'https://XN--example.com?foo=bar&BOO=baz';

public Mage_Core_Helper_Url $subject;

Expand All @@ -47,13 +56,25 @@ public function testGetCurrentBase64Url(): void
}

/**
* @dataProvider provideGetEncodedUrl
* @group Mage_Core
* @group Mage_Core_Helper
*/
public function testGetEncodedUrl(): void
public function testGetEncodedUrl(string $expectedResult, ?string $url): void
{
$this->assertSame($expectedResult, $this->subject->getEncodedUrl($url));
}

public function provideGetEncodedUrl(): Generator
{
$this->assertIsString($this->subject->getEncodedUrl());
$this->assertIsString($this->subject->getEncodedUrl(self::TEST_URL_1));
yield 'null' => [
'aHR0cDovLw,,',
null,
];
yield 'base url' => [
'aHR0cHM6Ly9leGFtcGxlLmNvbQ,,',
self::TEST_URL_BASE,
];
}

/**
Expand All @@ -66,25 +87,98 @@ public function testGetHomeUrl(): void
}

/**
* @dataProvider provideAddRequestParam
* @group Mage_Core
* @group Mage_Core_Helper
*/
public function testAddRequestParam(): void
public function testAddRequestParam(string $expectedResult, string $url, array $param): void
{
$this->assertIsString($this->subject->addRequestParam(self::TEST_URL_1, [0 => 'int']));
$this->assertIsString($this->subject->addRequestParam(self::TEST_URL_1, ['null' => null]));
$this->assertIsString($this->subject->addRequestParam(self::TEST_URL_1, ['key' => 'value']));
$this->assertIsString($this->subject->addRequestParam(self::TEST_URL_1, ['key' => ['subKey' => 'subValue']]));
$this->assertSame($expectedResult, $this->subject->addRequestParam($url, $param));
}

public function provideAddRequestParam(): Generator
{
yield 'int key' => [
self::TEST_URL_BASE . '?',
self::TEST_URL_BASE,
[0 => 'int'],
];
yield 'int value' => [
self::TEST_URL_BASE . '?int=0',
self::TEST_URL_BASE,
['int' => 0],
];
yield 'null' => [
self::TEST_URL_BASE . '?null',
self::TEST_URL_BASE,
['null' => null],
];
yield 'string' => [
self::TEST_URL_PARAM,
self::TEST_URL_BASE,
['foo' => 'bar'],
];
yield 'string extend' => [
self::TEST_URL_PARAMS,
self::TEST_URL_PARAM,
['BOO' => 'baz'],
];
yield 'array' => [
self::TEST_URL_BASE . '?key[]=subValue',
self::TEST_URL_BASE,
['key' => ['subKey' => 'subValue']],
];
}

/**
* @dataProvider provideRemoveRequestParam
* @group Mage_Core
* @group Mage_Core_Helper
*/
public function testRemoveRequestParam(): void
public function testRemoveRequestParam(string $expectedResult, string $url, string $paramKey, bool $caseSensitive = false): void
{
$this->assertSame($expectedResult, $this->subject->removeRequestParam($url, $paramKey, $caseSensitive));
}

public function provideRemoveRequestParam(): Generator
{
$this->assertIsString($this->subject->removeRequestParam(self::TEST_URL_1, 'foo'));
$this->assertIsString($this->subject->removeRequestParam(self::TEST_URL_2, 'foo'));
yield 'remove #1' => [
self::TEST_URL_BASE,
self::TEST_URL_PARAM,
'foo'
];
yield 'remove #2' => [
self::TEST_URL_PARAMS,
self::TEST_URL_PARAMS,
'boo'
];
yield 'remove #1 case sensitive' => [
self::TEST_URL_PARAM,
self::TEST_URL_PARAM,
'FOO',
true
];
yield 'remove #2 case sensitive' => [
self::TEST_URL_PARAM,
self::TEST_URL_PARAMS,
'BOO',
true
];
yield 'not-exists' => [
self::TEST_URL_PARAMS,
self::TEST_URL_PARAMS,
'not-exists'
];
yield '___SID' => [
self::TEST_URL_SID1,
self::TEST_URL_SID_BOTH,
'___SID'
];
yield 'SID' => [
self::TEST_URL_SID2,
self::TEST_URL_SID_BOTH,
'SID'
];
}

/**
Expand All @@ -93,16 +187,18 @@ public function testRemoveRequestParam(): void
*/
public function testEncodePunycode(): void
{
$this->assertIsString($this->subject->encodePunycode(self::TEST_URL_1));
$this->assertIsString($this->subject->encodePunycode(self::TEST_URL_PUNY));
$this->assertSame(self::TEST_URL_BASE, $this->subject->encodePunycode(self::TEST_URL_BASE));
$this->assertSame(self::TEST_URL_PUNY, $this->subject->encodePunycode(self::TEST_URL_PUNY));
$this->markTestIncomplete('This test has to be checked.');
}
/**
* @group Mage_Core
* @group Mage_Core_Helper
*/
public function testDecodePunycode(): void
{
$this->assertIsString($this->subject->decodePunycode(self::TEST_URL_1));
$this->assertIsString($this->subject->decodePunycode(self::TEST_URL_PUNY));
$this->assertSame(self::TEST_URL_BASE, $this->subject->decodePunycode(self::TEST_URL_BASE));
$this->assertSame('https://?foo=bar&BOO=baz', $this->subject->decodePunycode(self::TEST_URL_PUNY));
$this->markTestIncomplete('This test has to be checked.');
}
}

0 comments on commit 5207750

Please sign in to comment.