diff --git a/src/Service/UrlParsingService.php b/src/Service/UrlParsingService.php index 2bbeec37d..5eec41974 100644 --- a/src/Service/UrlParsingService.php +++ b/src/Service/UrlParsingService.php @@ -31,26 +31,20 @@ public function isUrl(string $value): bool */ public function parseTrackingCodeFromUrl(string $value): array { - // Case 1: Query parameter - if ((bool)preg_match('#(code|shipment|track|tracking)=([a-zA-Z0-9]+)#i', $value, $matches)) { - return [$matches[2], $value]; + $urlQuery = parse_url($value, PHP_URL_QUERY); + if ($urlQuery === null) { + $urlQuery = parse_url($value, PHP_URL_FRAGMENT); } - - // Case 2: Path-based tracking - if ((bool)preg_match('#/(code|shipment|track|tracking)/([a-zA-Z0-9]+)/#i', $value, $matches)) { - return [$matches[2], $value]; - } - - // Case 3: Hash-based tracking - if ((bool)preg_match('#\#(code|shipment|track|tracking)=([a-zA-Z0-9]+)#i', $value, $matches)) { - return [$matches[2], $value]; + if ($urlQuery === null) { + return ['', $value]; } + $urlQuery = (string)$urlQuery; + $urlWithoutQuery = str_replace($urlQuery, '', $value); - // could not determine code - return ['', $value]; + return [$urlQuery, $urlWithoutQuery . '%s']; } - public function encodePathAndQuery(string $fullUrl):string + public function encodePathAndQuery(string $fullUrl): string { $urlParts = parse_url($fullUrl); @@ -62,7 +56,7 @@ public function encodePathAndQuery(string $fullUrl):string $user = isset($urlParts['user']) ? $urlParts['user'] : ''; - $pass = isset($urlParts['pass']) ? ':' . $urlParts['pass'] : ''; + $pass = isset($urlParts['pass']) ? ':' . $urlParts['pass'] : ''; $pass = ($user || $pass) ? "$pass@" : ''; @@ -85,7 +79,7 @@ public function encodePathAndQuery(string $fullUrl):string $fragment = isset($urlParts['fragment']) ? '#' . rawurlencode($urlParts['fragment']) : ''; - return trim($scheme.$user.$pass.$host.$port.$path.$query.$fragment); + return trim($scheme . $user . $pass . $host . $port . $path . $query . $fragment); } /** diff --git a/tests/PHPUnit/Service/UrlParsingServiceTest.php b/tests/PHPUnit/Service/UrlParsingServiceTest.php index 52ed45b61..b5c2d8daf 100644 --- a/tests/PHPUnit/Service/UrlParsingServiceTest.php +++ b/tests/PHPUnit/Service/UrlParsingServiceTest.php @@ -44,30 +44,14 @@ public function testParseTrackingCodeQueryParameter(string $input, array $expect public function queryParameterProvider(): array { return [ - ['https://www.example.com/product?code=12345', ['12345', 'https://www.example.com/product?code=12345']], - ['https://www.example.com/product?shipment=abc123', ['abc123', 'https://www.example.com/product?shipment=abc123']], - ['https://www.example.com/product?track=track123', ['track123', 'https://www.example.com/product?track=track123']], - ['https://www.example.com/product?tracking=track456', ['track456', 'https://www.example.com/product?tracking=track456']], + ['https://www.example.com/product?code=12345&postal_code=123', ['code=12345&postal_code=123', 'https://www.example.com/product?%s']], + ['https://www.example.com/product?shipment=abc123', ['shipment=abc123', 'https://www.example.com/product?%s']], + ['https://www.example.com/product?track=track123', ['track=track123', 'https://www.example.com/product?%s']], + ['https://www.example.com/product?tracking=track456', ['tracking=track456', 'https://www.example.com/product?%s']], + ['https://www.example.com/tracking/?country=at&tracking_number=1023534500214250110508&postal_cod1e=1050', ['country=at&tracking_number=1023534500214250110508&postal_cod1e=1050', 'https://www.example.com/tracking/?%s']], ]; } - /** - * @dataProvider pathProvider - */ - public function testParseTrackingCodePath(string $input, array $expected) - { - $this->assertEquals($expected, $this->service->parseTrackingCodeFromUrl($input)); - } - - public function pathProvider(): array - { - return [ - ['https://www.example.com/code/12345/product', ['12345', 'https://www.example.com/code/12345/product']], - ['https://www.example.com/shipment/abc123/product', ['abc123', 'https://www.example.com/shipment/abc123/product']], - ['https://www.example.com/track/track123/product', ['track123', 'https://www.example.com/track/track123/product']], - ['https://www.example.com/tracking/track456/product', ['track456', 'https://www.example.com/tracking/track456/product']], - ]; - } /** * @dataProvider hashProvider @@ -80,10 +64,10 @@ public function testParseTrackingCodeHash(string $input, array $expected) public function hashProvider(): array { return [ - ['https://www.example.com/product#code=12345', ['12345', 'https://www.example.com/product#code=12345']], - ['https://www.example.com/product#shipment=abc123', ['abc123', 'https://www.example.com/product#shipment=abc123']], - ['https://www.example.com/product#track=track123', ['track123', 'https://www.example.com/product#track=track123']], - ['https://www.example.com/product#tracking=track456', ['track456', 'https://www.example.com/product#tracking=track456']], + ['https://www.example.com/product#code=12345', ['code=12345', 'https://www.example.com/product#%s']], + ['https://www.example.com/product#shipment=abc123', ['shipment=abc123', 'https://www.example.com/product#%s']], + ['https://www.example.com/product#track=track123', ['track=track123', 'https://www.example.com/product#%s']], + ['https://www.example.com/product#tracking=track456', ['tracking=track456', 'https://www.example.com/product#%s']], ]; }