Skip to content

Commit eae2b73

Browse files
committed
Shop url sanitization
1 parent 56e8727 commit eae2b73

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

src/Entity/AbstractShop.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public function getShopId(): string
4444

4545
public function getShopUrl(): string
4646
{
47-
return $this->shopUrl;
47+
/** @var string $url */
48+
$url = preg_replace('#([^:])//+#', '$1/', $this->shopUrl);
49+
50+
return rtrim($url, '/');
4851
}
4952

5053
public function getShopSecret(): string

tests/Entity/AbstractShopTest.php

+61-3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ class AbstractShopTest extends TestCase
1111
{
1212
public function testMethods(): void
1313
{
14-
$shop = new TestShop('1', '2', '3');
14+
$shop = new TestShop('1', 'https://test-url.com/', '3');
1515
$shop->setShopSecret('2');
16-
$shop->setShopUrl('3');
16+
$shop->setShopUrl('https://test-url.com/');
1717

1818
static::assertSame('1', $shop->getShopId());
1919
static::assertSame('2', $shop->getShopSecret());
20-
static::assertSame('3', $shop->getShopUrl());
20+
static::assertSame('https://test-url.com', $shop->getShopUrl());
2121
static::assertFalse($shop->isShopActive());
2222
$shop->setShopActive(true);
2323
static::assertTrue($shop->isShopActive());
@@ -26,6 +26,64 @@ public function testMethods(): void
2626
static::assertSame('a', $shop->getShopClientId());
2727
static::assertSame('b', $shop->getShopClientSecret());
2828
}
29+
30+
#[DataProvider('shopValidUrlDataProvider')]
31+
public function testInvalidUrl(
32+
string $shopUrl,
33+
string $expectedUrl
34+
): void {
35+
$shop = new TestShop('shopId', $shopUrl, 'shopSecret');
36+
static::assertSame($expectedUrl, $shop->getShopUrl());
37+
}
38+
39+
public static function shopValidUrlDataProvider(): \Generator
40+
{
41+
yield 'Valid URL without trailing slash' => [
42+
'shopUrl' => 'https://test.com',
43+
'expectedUrl' => 'https://test.com',
44+
];
45+
46+
yield 'Valid URL with trailing slash' => [
47+
'shopUrl' => 'https://test.com/',
48+
'expectedUrl' => 'https://test.com',
49+
];
50+
51+
yield 'Invalid URL with trailing slash' => [
52+
'shopUrl' => 'https://test.com/test/',
53+
'expectedUrl' => 'https://test.com/test',
54+
];
55+
56+
yield 'Invalid URL with double slashes' => [
57+
'shopUrl' => 'https://test.com//test',
58+
'expectedUrl' => 'https://test.com/test',
59+
];
60+
61+
62+
yield 'Invalid URL with 2 slashes and trailing slash' => [
63+
'shopUrl' => 'https://test.com//test/',
64+
'expectedUrl' => 'https://test.com/test',
65+
];
66+
67+
yield 'Invalid URL with 3 slashes and trailing slash' => [
68+
'shopUrl' => 'https://test.com///test/',
69+
'expectedUrl' => 'https://test.com/test',
70+
];
71+
72+
yield 'Invalid URL with multiple slashes' => [
73+
'shopUrl' => 'https://test.com///test/test1//test2',
74+
'expectedUrl' => 'https://test.com/test/test1/test2',
75+
];
76+
77+
yield 'Invalid URL with multiple slashes and trailing slash' => [
78+
'shopUrl' => 'https://test.com///test/test1//test2/',
79+
'expectedUrl' => 'https://test.com/test/test1/test2',
80+
];
81+
82+
yield 'Invalid URL with multiple slashes and multplie trailing slash' => [
83+
'shopUrl' => 'https://test.com///test/test1//test2//',
84+
'expectedUrl' => 'https://test.com/test/test1/test2',
85+
];
86+
}
2987
}
3088

3189
class TestShop extends AbstractShop

0 commit comments

Comments
 (0)