mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-09 12:17:13 +00:00
Two lint suppressions shipped with the multisite OIDC work. Both are replaced by the thing they were standing in for. settings.php claimed an exemption from the mirrored-test rule because every call it makes belongs to the WordPress runtime. The six functions stub in a few lines, so the claim does not hold. SettingsTest.php declares WP_CLI and the globals over a state holder and includes the real payload per case: * a single site is written without switching blogs * every site of a network is written, switch_to_blog paired with restore * a site whose write does not persist is named by blog id * every site is still visited before that failure is reported * a payload that is not a map halts, and so does an absent one The fourth case is the one worth having: a `break` on the first failure would pass the other three and silently stop writing. Neither existing house pattern fits. `eval` suits the tag-less tinker snippet of web-app-akaunting, `proc_open` the standalone script of web-app-joomla; this is a tagged file that needs a runtime, so it is stubs plus `include`, which also avoids the `nocheck: eval` the akaunting test carries. The installer test's cache-read exemption rested on a cached read serving a stale call log. Measured, that risk is confined to the suite and sits around 3e-13: the log lives in a fresh TemporaryDirectory per run, mkdtemp verifies non-existence, and each path is read once. What complying actually costs is roughly 25 dead entries in an 8192-slot LRU, so the read goes through utils.cache.files.read_text. Proven by `make test-unit-php`: 32 tests, 68 assertions. The first version of this test passed nothing at all -- it sits one directory deeper than the other PHP tests, so dirname(__DIR__, 6) resolved to tests/ and the include found no file. Without the test that would have stayed invisible. The suite drives stubs, so it proves the payload's control flow and not that WordPress behaves as stubbed. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
176 lines
4.3 KiB
PHP
176 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
final class WpOidcState
|
|
{
|
|
public static bool $multisite = false;
|
|
|
|
public static array $sites = [];
|
|
|
|
public static ?int $currentBlog = null;
|
|
|
|
public static array $options = [];
|
|
|
|
public static array $switches = [];
|
|
|
|
public static array $restores = [];
|
|
|
|
public static array $blogsWhoseWriteNeverPersists = [];
|
|
|
|
public static function reset(): void
|
|
{
|
|
self::$multisite = false;
|
|
self::$sites = [];
|
|
self::$currentBlog = null;
|
|
self::$options = [];
|
|
self::$switches = [];
|
|
self::$restores = [];
|
|
self::$blogsWhoseWriteNeverPersists = [];
|
|
}
|
|
|
|
public static function blog(): int
|
|
{
|
|
return self::$currentBlog ?? 0;
|
|
}
|
|
}
|
|
|
|
final class WpCliHalt extends RuntimeException
|
|
{
|
|
}
|
|
|
|
final class WP_CLI
|
|
{
|
|
public static function error(string $message): void
|
|
{
|
|
throw new WpCliHalt($message);
|
|
}
|
|
}
|
|
|
|
function is_multisite(): bool
|
|
{
|
|
return WpOidcState::$multisite;
|
|
}
|
|
|
|
function get_sites(array $args = []): array
|
|
{
|
|
return WpOidcState::$sites;
|
|
}
|
|
|
|
function switch_to_blog(int $blogId): void
|
|
{
|
|
WpOidcState::$currentBlog = $blogId;
|
|
WpOidcState::$switches[] = $blogId;
|
|
}
|
|
|
|
function restore_current_blog(): void
|
|
{
|
|
WpOidcState::$restores[] = WpOidcState::blog();
|
|
WpOidcState::$currentBlog = null;
|
|
}
|
|
|
|
function update_option(string $key, $value): bool
|
|
{
|
|
$blog = WpOidcState::blog();
|
|
if (in_array($blog, WpOidcState::$blogsWhoseWriteNeverPersists, true)) {
|
|
return false;
|
|
}
|
|
WpOidcState::$options[$blog][$key] = $value;
|
|
|
|
return true;
|
|
}
|
|
|
|
function get_option(string $key, $default = false)
|
|
{
|
|
return WpOidcState::$options[WpOidcState::blog()][$key] ?? $default;
|
|
}
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class SettingsTest extends TestCase
|
|
{
|
|
private const SCRIPT = '/roles/web-app-wordpress/files/php/oidc/settings.php';
|
|
|
|
private const OPTION = 'openid_connect_generic_settings';
|
|
|
|
private const SETTINGS = ['client_id' => 'wp', 'client_secret' => 's3cret'];
|
|
|
|
protected function setUp(): void
|
|
{
|
|
WpOidcState::reset();
|
|
}
|
|
|
|
private function runPayload(?string $payload): void
|
|
{
|
|
putenv($payload === null ? 'WP_OIDC_SETTINGS' : 'WP_OIDC_SETTINGS=' . $payload);
|
|
include dirname(__DIR__, 7) . self::SCRIPT;
|
|
}
|
|
|
|
private function encoded(): string
|
|
{
|
|
return base64_encode((string) json_encode(self::SETTINGS));
|
|
}
|
|
|
|
public function testASingleSiteIsWrittenWithoutSwitchingBlogs(): void
|
|
{
|
|
$this->runPayload($this->encoded());
|
|
|
|
self::assertSame(self::SETTINGS, WpOidcState::$options[0][self::OPTION]);
|
|
self::assertSame([], WpOidcState::$switches);
|
|
}
|
|
|
|
public function testEverySiteOfANetworkReceivesTheSettings(): void
|
|
{
|
|
WpOidcState::$multisite = true;
|
|
WpOidcState::$sites = [1, 7, 9];
|
|
|
|
$this->runPayload($this->encoded());
|
|
|
|
self::assertSame([1, 7, 9], WpOidcState::$switches);
|
|
self::assertSame([1, 7, 9], WpOidcState::$restores);
|
|
foreach ([1, 7, 9] as $blog) {
|
|
self::assertSame(self::SETTINGS, WpOidcState::$options[$blog][self::OPTION]);
|
|
}
|
|
}
|
|
|
|
public function testASiteWhoseWriteDoesNotPersistIsNamed(): void
|
|
{
|
|
WpOidcState::$multisite = true;
|
|
WpOidcState::$sites = [1, 7, 9];
|
|
WpOidcState::$blogsWhoseWriteNeverPersists = [7];
|
|
|
|
$this->expectException(WpCliHalt::class);
|
|
$this->expectExceptionMessageMatches('/\b7\b/');
|
|
|
|
$this->runPayload($this->encoded());
|
|
}
|
|
|
|
public function testEverySiteIsVisitedBeforeTheFailureIsReported(): void
|
|
{
|
|
WpOidcState::$multisite = true;
|
|
WpOidcState::$sites = [1, 7, 9];
|
|
WpOidcState::$blogsWhoseWriteNeverPersists = [1];
|
|
|
|
try {
|
|
$this->runPayload($this->encoded());
|
|
self::fail('a blocked write must halt the run');
|
|
} catch (WpCliHalt $halt) {
|
|
self::assertSame([1, 7, 9], WpOidcState::$switches);
|
|
}
|
|
}
|
|
|
|
public function testAPayloadThatIsNotAMapHalts(): void
|
|
{
|
|
$this->expectException(WpCliHalt::class);
|
|
|
|
$this->runPayload(base64_encode('"a string"'));
|
|
}
|
|
|
|
public function testAnAbsentPayloadHalts(): void
|
|
{
|
|
$this->expectException(WpCliHalt::class);
|
|
|
|
$this->runPayload(null);
|
|
}
|
|
}
|