mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
- config/url_matching.php: trim each exploded env value before filtering empties, so "foo, bar" yields normalised names without whitespace - ClientConfigApiTest: authenticate the minimal-token test with only client-config:read, proving the endpoint is not gated on other abilities - UrlNormalizeForMatchTest: snapshot and restore the env vars instead of unconditionally unsetting them, and pin the trimming behaviour - api.md: tag the URL example fence as text - drop two unused fixture assignments
156 lines
6.7 KiB
PHP
156 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Models;
|
|
|
|
use App\Models\Url;
|
|
use Tests\TestCase;
|
|
|
|
class UrlNormalizeForMatchTest extends TestCase
|
|
{
|
|
/**
|
|
* @return array<string, array{0: string, 1: string}>
|
|
*/
|
|
public static function normalisationProvider(): array
|
|
{
|
|
return [
|
|
'strips www and lowercases host' => ['https://WWW.Target.com.au/p/Xbox/?ref=nav', 'target.com.au/p/xbox'],
|
|
'scheme is irrelevant' => ['http://target.com.au/p/xbox', 'target.com.au/p/xbox'],
|
|
'trailing slash removed' => ['https://target.com.au/p/xbox/', 'target.com.au/p/xbox'],
|
|
'fragment discarded' => ['https://shop.com/p/x#reviews', 'shop.com/p/x'],
|
|
'port discarded' => ['https://shop.com:8443/p/x', 'shop.com/p/x'],
|
|
'no path' => ['https://shop.com', 'shop.com'],
|
|
'bare slash path' => ['https://shop.com/', 'shop.com'],
|
|
'scheme-less input assumes https' => ['target.com.au/p/xbox', 'target.com.au/p/xbox'],
|
|
'tracking dropped, significant kept' => ['https://shop.com/p/tee?variant=42&utm_source=fb&gclid=x', 'shop.com/p/tee?variant=42'],
|
|
'param order does not matter' => ['https://shop.com/p/tee?utm_medium=x&variant=42', 'shop.com/p/tee?variant=42'],
|
|
'remaining params sorted' => ['https://shop.com/p/tee?sku=B&pid=A', 'shop.com/p/tee?pid=a&sku=b'],
|
|
'affinity survives aff denylist' => ['https://shop.com/p/tee?affinity=hi&aff=x&aff_id=y', 'shop.com/p/tee?affinity=hi'],
|
|
'amazon affiliate params' => ['https://amazon.com.au/dp/B01?th=1&psc=1&tag=aff-22', 'amazon.com.au/dp/b01'],
|
|
'amazon search params' => ['https://amazon.com.au/dp/B01?keywords=x&qid=1&sr=8-1&pd_rd_w=z&ref_=sr', 'amazon.com.au/dp/b01'],
|
|
'valueless param kept verbatim' => ['https://shop.com/p/x?foo', 'shop.com/p/x?foo'],
|
|
'empty valued param kept verbatim' => ['https://shop.com/p/x?foo=', 'shop.com/p/x?foo='],
|
|
'malformed input' => ['not a url', ''],
|
|
'empty input' => ['', ''],
|
|
'whitespace only' => [' ', ''],
|
|
'no host' => ['https://', ''],
|
|
];
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('normalisationProvider')]
|
|
public function test_normalize_for_match(string $input, string $expected): void
|
|
{
|
|
$this->assertSame($expected, Url::normalizeForMatch($input));
|
|
}
|
|
|
|
public function test_result_is_truncated_to_255_characters(): void
|
|
{
|
|
$long = 'https://shop.com/'.str_repeat('a', 400);
|
|
|
|
$this->assertSame(255, strlen(Url::normalizeForMatch($long)));
|
|
}
|
|
|
|
public function test_normalize_host(): void
|
|
{
|
|
$this->assertSame('target.com.au', Url::normalizeHost('www.Target.com.au'));
|
|
$this->assertSame('target.com.au', Url::normalizeHost('TARGET.COM.AU'));
|
|
$this->assertSame('target.com.au', Url::normalizeHost('target.com.au:8443'));
|
|
$this->assertSame('', Url::normalizeHost(' '));
|
|
}
|
|
|
|
public function test_denylist_is_config_driven(): void
|
|
{
|
|
config()->set('url_matching.tracking_params', ['mycustomparam']);
|
|
config()->set('url_matching.tracking_param_prefixes', []);
|
|
|
|
$this->assertSame(
|
|
'shop.com/p/x?gclid=keep',
|
|
Url::normalizeForMatch('https://shop.com/p/x?mycustomparam=drop&gclid=keep')
|
|
);
|
|
}
|
|
|
|
public function test_config_prefixes_are_applied(): void
|
|
{
|
|
config()->set('url_matching.tracking_params', []);
|
|
config()->set('url_matching.tracking_param_prefixes', ['xyz_']);
|
|
|
|
$this->assertSame(
|
|
'shop.com/p/x?keep=1',
|
|
Url::normalizeForMatch('https://shop.com/p/x?xyz_a=1&keep=1')
|
|
);
|
|
}
|
|
|
|
public function test_config_values_are_trimmed_and_lowercased(): void
|
|
{
|
|
config()->set('url_matching.tracking_params', [' GCLID ', '', 'gclid']);
|
|
config()->set('url_matching.tracking_param_prefixes', []);
|
|
|
|
$this->assertSame('shop.com/p/x', Url::normalizeForMatch('https://shop.com/p/x?gclid=abc'));
|
|
}
|
|
|
|
public function test_env_extra_appends_rather_than_replaces(): void
|
|
{
|
|
$paramsKey = 'URL_MATCHING_TRACKING_PARAMS_EXTRA';
|
|
$prefixesKey = 'URL_MATCHING_TRACKING_PARAM_PREFIXES_EXTRA';
|
|
|
|
// Snapshot rather than blindly unsetting on the way out: these are real
|
|
// configuration variables a developer may legitimately have set, and
|
|
// clobbering them would leak into every later test in the process.
|
|
$snapshot = [];
|
|
|
|
foreach ([$paramsKey, $prefixesKey] as $key) {
|
|
$snapshot[$key] = [
|
|
'getenv' => getenv($key),
|
|
'env' => array_key_exists($key, $_ENV) ? $_ENV[$key] : null,
|
|
'envSet' => array_key_exists($key, $_ENV),
|
|
'server' => array_key_exists($key, $_SERVER) ? $_SERVER[$key] : null,
|
|
'serverSet' => array_key_exists($key, $_SERVER),
|
|
];
|
|
}
|
|
|
|
// Deliberately spaced and with a trailing empty element, so this also pins
|
|
// that each value is trimmed and blanks are dropped.
|
|
putenv("{$paramsKey}=mycustomparam, spacedparam ,");
|
|
$_ENV[$paramsKey] = 'mycustomparam, spacedparam ,';
|
|
$_SERVER[$paramsKey] = 'mycustomparam, spacedparam ,';
|
|
|
|
putenv("{$prefixesKey}= xyz_ ");
|
|
$_ENV[$prefixesKey] = ' xyz_ ';
|
|
$_SERVER[$prefixesKey] = ' xyz_ ';
|
|
|
|
try {
|
|
$config = require config_path('url_matching.php');
|
|
|
|
$this->assertContains('mycustomparam', $config['tracking_params']);
|
|
$this->assertContains('spacedparam', $config['tracking_params']);
|
|
$this->assertContains('gclid', $config['tracking_params']);
|
|
$this->assertNotContains(' spacedparam ', $config['tracking_params']);
|
|
$this->assertNotContains('', $config['tracking_params']);
|
|
$this->assertGreaterThanOrEqual(27, count(array_filter($config['tracking_params'])));
|
|
|
|
$this->assertContains('xyz_', $config['tracking_param_prefixes']);
|
|
$this->assertContains('utm_', $config['tracking_param_prefixes']);
|
|
$this->assertNotContains(' xyz_ ', $config['tracking_param_prefixes']);
|
|
} finally {
|
|
foreach ($snapshot as $key => $previous) {
|
|
if ($previous['getenv'] === false) {
|
|
putenv($key);
|
|
} else {
|
|
putenv("{$key}={$previous['getenv']}");
|
|
}
|
|
|
|
if ($previous['envSet']) {
|
|
$_ENV[$key] = $previous['env'];
|
|
} else {
|
|
unset($_ENV[$key]);
|
|
}
|
|
|
|
if ($previous['serverSet']) {
|
|
$_SERVER[$key] = $previous['server'];
|
|
} else {
|
|
unset($_SERVER[$key]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|