Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
### Unreleased

### v1.5.0-beta3 (2020-10-09)

* Init the DeviceIdentifier to a fixed value in the CLI environment without setting any cookies, to prevent
errors if the process has already sent output.
* Add MutexWrapper with Mock and Db (mysql) backed implementations for preventing concurrent executions
of code.

Expand Down
16 changes: 13 additions & 3 deletions src/Logging/DeviceIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,19 @@ public static function forceGlobalTestValue(string $id): void
* before sending any output.
*
* @param bool $ssl_available
* @param bool $is_cli
*/
public static function initAndEnsureCookieSet(bool $ssl_available): void
public static function initAndEnsureCookieSet(bool $ssl_available, bool $is_cli = (PHP_SAPI === 'cli')): void
{
static::$instance = new DeviceIdentifier(new CookieWrapper($ssl_available));
if ($is_cli) {
// Stub the class without setting cookies for use in a CLI environment.
// Note that as it's not really appropriate to inject the `is_cli` arg to `::get()`, and we can't stub
// PHP_SAPI for testing, that method will still return `-unset-` if it is called before
// ::initAndEnsureCookieSet()
static::forceGlobalTestValue(static::CLI_ID);
} else {
static::$instance = new DeviceIdentifier(new CookieWrapper($ssl_available));
}
static::$instance->init();
}

Expand All @@ -68,8 +77,9 @@ public static function get(): string
return $i->getValue();
}

const CLI_ID = 'cli-------------------';
const COOKIE_LIFETIME = 'P5Y';
const VALID_REGEX = '/^[a-zA-Z0-9\-_=]{22}$/';
const VALID_REGEX = '/^[a-zA-Z0-9\-_=]{22}$/';

/**
* @var string|null
Expand Down
14 changes: 13 additions & 1 deletion test/unit/Logging/DeviceIdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function () { DeviceIdentifier::$instance = NULL; }
$old_cookie = $_COOKIE;
try {
$_COOKIE['did'] = '1234567890123456789012';
DeviceIdentifier::initAndEnsureCookieSet(TRUE);
DeviceIdentifier::initAndEnsureCookieSet(TRUE, FALSE);
$this->assertSame('1234567890123456789012', DeviceIdentifier::get());

// Note that here the init method has assigned a global instance so changes to $_COOKIE
Expand All @@ -168,6 +168,18 @@ function () { DeviceIdentifier::$instance = NULL; }
$this->assertSame('1234567890123456789012', DeviceIdentifier::get());
}

public function test_its_static_init_creates_instance_and_initialises_with_fixed_id_in_cli()
{
ScopeChangingCaller::call(
DeviceIdentifier::class,
function () { DeviceIdentifier::$instance = NULL; }
);

DeviceIdentifier::initAndEnsureCookieSet(TRUE); // Fall back to default arg
$this->assertSame(DeviceIdentifier::CLI_ID, DeviceIdentifier::get());
$this->assertRegExp(DeviceIdentifier::VALID_REGEX, DeviceIdentifier::get());
}

public function test_its_static_get_can_read_from_cookies_even_if_not_initialised()
{
ScopeChangingCaller::call(
Expand Down