Skip to content

Commit

Permalink
NTR: fix shipping extraction (#818)
Browse files Browse the repository at this point in the history
Co-authored-by: Vitalij Mik <[email protected]>
  • Loading branch information
BlackScorp and Vitalij Mik authored Aug 28, 2024
1 parent 43a4621 commit 38dbe33
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 42 deletions.
28 changes: 11 additions & 17 deletions src/Service/UrlParsingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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@" : '';

Expand All @@ -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);
}

/**
Expand Down
34 changes: 9 additions & 25 deletions tests/PHPUnit/Service/UrlParsingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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']],
];
}

Expand Down

0 comments on commit 38dbe33

Please sign in to comment.