fix: isolate CHRNX seed lifecycle tests

This commit is contained in:
chrnx committed 2026-08-12 10:25:49 -05:00
1 parent 6e93c0ad20
commit 57e7e9aaf3
10 files changed
+277 -316

No files matched your search

-162
View File
@@ -1,162 +0,0 @@
<?php
/**
* Adversarial assertions for the local fixture environment lifecycle.
*
* @package CHRNX
*/
$chrnx_test_host = wp_parse_url( home_url( '/' ), PHP_URL_HOST );
if ( ! in_array( $chrnx_test_host, array( 'localhost', '127.0.0.1' ), true ) ) {
WP_CLI::error( 'Refusing fixture-state tests outside local wp-env.' );
}
$chrnx_test_mode = isset( $args[0] ) && is_string( $args[0] ) ? $args[0] : '';
$chrnx_test_option_names = array(
'sticky_posts',
'blogname',
'blogdescription',
'show_on_front',
'page_on_front',
'page_for_posts',
'permalink_structure',
'chrnx_subscribe_page_id',
);
/** Return exact option existence and serialized value. */
function chrnx_test_option_state( string $name ): array {
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Test asserts exact persisted option bytes.
$raw = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", $name ) );
return array(
'exists' => null !== $raw,
'raw' => $raw,
);
}
/** Return IDs for fixture-owned sticky posts. */
function chrnx_test_fixture_stickies(): array {
$sticky = array_map( 'absint', (array) get_option( 'sticky_posts', array() ) );
return array_values(
array_filter(
$sticky,
static function ( int $post_id ): bool {
return '' !== (string) get_post_meta( $post_id, '_chrnx_fixture_key', true );
}
)
);
}
/** Find one local fixture-owned post. */
function chrnx_test_find_fixture( string $key, string $post_type ): int {
$ids = get_posts(
array(
'post_type' => $post_type,
'post_status' => 'any',
'posts_per_page' => 1,
'fields' => 'ids',
'meta_key' => '_chrnx_fixture_key',
'meta_value' => $key,
)
);
return empty( $ids ) ? 0 : absint( $ids[0] );
}
/** Fail with a precise integration assertion. */
function chrnx_test_assert( bool $condition, string $message ): void {
if ( ! $condition ) {
WP_CLI::error( $message );
}
}
if ( 'setup' === $chrnx_test_mode ) {
$chrnx_test_created_id = wp_insert_post(
array(
'post_status' => 'publish',
'post_title' => 'Unrelated sticky sentinel',
'post_type' => 'post',
),
true
);
if ( is_wp_error( $chrnx_test_created_id ) ) {
WP_CLI::error( 'Could not create unrelated sticky sentinel.' );
}
update_option( 'sticky_posts', array( absint( $chrnx_test_created_id ) ) );
update_option( 'blogname', 'Pre-existing local title' );
update_option( 'blogdescription', 'Pre-existing local description' );
update_option( 'show_on_front', 'posts' );
update_option( 'page_on_front', 0 );
update_option( 'page_for_posts', 0 );
update_option( 'permalink_structure', '' );
update_option( 'chrnx_subscribe_page_id', 424242 );
$chrnx_test_subscribe_id = chrnx_test_find_fixture( 'page-subscribe', 'page' );
if ( $chrnx_test_subscribe_id > 0 ) {
wp_update_post(
array(
'ID' => $chrnx_test_subscribe_id,
'post_status' => 'private',
)
);
}
$chrnx_test_expected = array();
foreach ( $chrnx_test_option_names as $chrnx_test_option_name ) {
$chrnx_test_expected[ $chrnx_test_option_name ] = chrnx_test_option_state( $chrnx_test_option_name );
}
$chrnx_test_expected['subscribe'] = array(
'exists' => $chrnx_test_subscribe_id > 0,
'id' => $chrnx_test_subscribe_id,
'status' => $chrnx_test_subscribe_id > 0 ? get_post_status( $chrnx_test_subscribe_id ) : null,
);
update_option( '_chrnx_seed_test_expected', $chrnx_test_expected, false );
update_option( '_chrnx_seed_test_unrelated_id', absint( $chrnx_test_created_id ), false );
WP_CLI::success( 'Adversarial fixture environment prepared.' );
return;
}
$chrnx_test_unrelated_id = absint( get_option( '_chrnx_seed_test_unrelated_id', 0 ) );
if ( 'check-default' === $chrnx_test_mode || 'check-featured-1' === $chrnx_test_mode ) {
$chrnx_test_sticky = array_map( 'absint', (array) get_option( 'sticky_posts', array() ) );
chrnx_test_assert( in_array( $chrnx_test_unrelated_id, $chrnx_test_sticky, true ), 'Seeder removed an unrelated sticky post.' );
$chrnx_test_expected_count = 'check-featured-1' === $chrnx_test_mode ? 1 : 3;
chrnx_test_assert( count( chrnx_test_fixture_stickies() ) === $chrnx_test_expected_count, 'Fixture sticky scenario count is wrong.' );
if ( 'check-default' === $chrnx_test_mode ) {
chrnx_test_assert( 'CHRNX' === get_option( 'blogname' ), 'Default visual title was not applied.' );
chrnx_test_assert( '/%postname%/' === get_option( 'permalink_structure' ), 'Default visual permalinks were not applied.' );
}
WP_CLI::success( 'Fixture sticky ownership is isolated.' );
return;
}
if ( 'check-subscribe-unpublished' === $chrnx_test_mode ) {
$chrnx_test_subscribe_id = chrnx_test_find_fixture( 'page-subscribe', 'page' );
chrnx_test_assert( 'draft' === get_post_status( $chrnx_test_subscribe_id ), 'Subscribe unpublished scenario did not draft the fixture page.' );
return;
}
if ( 'check-subscribe-unavailable' === $chrnx_test_mode ) {
chrnx_test_assert( 0 === absint( get_option( 'chrnx_subscribe_page_id' ) ), 'Subscribe unavailable scenario did not clear its fixture pointer.' );
return;
}
if ( 'check-restore' === $chrnx_test_mode ) {
$chrnx_test_expected = get_option( '_chrnx_seed_test_expected', array() );
foreach ( $chrnx_test_option_names as $chrnx_test_option_name ) {
chrnx_test_assert( chrnx_test_option_state( $chrnx_test_option_name ) === $chrnx_test_expected[ $chrnx_test_option_name ], 'Restore mismatch for ' . $chrnx_test_option_name . '.' );
}
if ( ! empty( $chrnx_test_expected['subscribe']['exists'] ) ) {
chrnx_test_assert( get_post_status( $chrnx_test_expected['subscribe']['id'] ) === $chrnx_test_expected['subscribe']['status'], 'Subscribe fixture status was not restored.' );
}
chrnx_test_assert( false === get_option( '_chrnx_fixture_environment_snapshot_v1', false ), 'Snapshot was not removed after restore.' );
WP_CLI::success( 'Original local configuration restored exactly.' );
return;
}
if ( 'cleanup' === $chrnx_test_mode ) {
if ( $chrnx_test_unrelated_id > 0 ) {
wp_delete_post( $chrnx_test_unrelated_id, true );
}
delete_option( '_chrnx_seed_test_expected' );
delete_option( '_chrnx_seed_test_unrelated_id' );
return;
}
WP_CLI::error( 'Unknown fixture-state test mode.' );
-34
View File
@@ -1,34 +0,0 @@
#!/bin/sh
set -eu
chrnx_test_root=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd)
chrnx_test_docker=${CHRNX_DOCKER_CLI_DIR:-/opt/homebrew/opt/docker/bin}
chrnx_test_wp() {
PATH="$chrnx_test_docker:$PATH" npm exec -- wp-env run cli wp eval-file wp-content/themes/chrnx/tests/integration/seed-state.php "$1"
}
chrnx_test_seed() {
PATH="$chrnx_test_docker:$PATH" "$chrnx_test_root/scripts/seed-test-content.sh" "$1"
}
chrnx_test_cleanup() {
chrnx_test_seed restore >/dev/null 2>&1 || true
chrnx_test_wp cleanup >/dev/null 2>&1 || true
chrnx_test_seed default >/dev/null 2>&1 || true
}
trap chrnx_test_cleanup EXIT HUP INT TERM
cd -- "$chrnx_test_root"
chrnx_test_seed restore
chrnx_test_wp setup
chrnx_test_seed default
chrnx_test_wp check-default
chrnx_test_seed featured-1
chrnx_test_wp check-featured-1
chrnx_test_seed subscribe-unpublished
chrnx_test_wp check-subscribe-unpublished
chrnx_test_seed subscribe-unavailable
chrnx_test_wp check-subscribe-unavailable
chrnx_test_seed restore
chrnx_test_wp check-restore
chrnx_test_wp cleanup
chrnx_test_seed default
trap - EXIT HUP INT TERM
+71
View File
@@ -0,0 +1,71 @@
<?php
/**
* Tests for reversible local fixture environment ownership.
*
* @package CHRNX
*/
require_once dirname( __DIR__, 2 ) . '/scripts/lib/fixture-environment.php';
/** Verify the seeder environment lifecycle in the isolated WordPress test DB. */
class FixtureEnvironmentTest extends WP_UnitTestCase {
/** Snapshot/restore preserves exact options, unrelated stickies, and owned status. */
public function test_environment_snapshot_scenarios_and_restore_are_exact() {
global $wpdb;
$unrelated = self::factory()->post->create( array( 'post_title' => 'Unrelated sticky' ) );
$featured = array(
self::factory()->post->create( array( 'post_title' => 'Fixture one' ) ),
self::factory()->post->create( array( 'post_title' => 'Fixture two' ) ),
self::factory()->post->create( array( 'post_title' => 'Fixture three' ) ),
);
$subscribe = self::factory()->post->create(
array(
'post_type' => 'page',
'post_status' => 'private',
'post_title' => 'Existing fixture Subscribe',
)
);
update_post_meta( $subscribe, '_chrnx_fixture_key', 'page-subscribe' );
foreach ( $featured as $index => $post_id ) {
update_post_meta( $post_id, '_chrnx_fixture_key', 'post-fixture-' . $index );
}
update_option( 'sticky_posts', array( $unrelated ) );
update_option( 'blogname', 'Outer title' );
update_option( 'blogdescription', 'Outer description' );
update_option( 'show_on_front', 'posts' );
update_option( 'page_on_front', 0 );
delete_option( 'page_for_posts' );
update_option( 'permalink_structure', '' );
update_option( 'chrnx_subscribe_page_id', 987654 );
$before = array();
foreach ( chrnx_fixture_environment_option_names() as $name ) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Exact persisted option bytes are the contract.
$before[ $name ] = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", $name ) );
}
$this->assertTrue( chrnx_fixture_environment_snapshot() );
$this->assertFalse( chrnx_fixture_environment_snapshot(), 'A scenario must not replace the outer snapshot.' );
chrnx_fixture_environment_apply(
array(
'featured_ids' => $featured,
'featured_count' => 1,
'subscribe_id' => $subscribe,
'subscribe_status' => 'draft',
'subscribe_available' => false,
)
);
$this->assertSame( array( $unrelated, $featured[0] ), array_map( 'absint', get_option( 'sticky_posts' ) ) );
$this->assertSame( 'draft', get_post_status( $subscribe ) );
$this->assertSame( 0, absint( get_option( 'chrnx_subscribe_page_id' ) ) );
$this->assertTrue( chrnx_fixture_environment_restore() );
$this->assertSame( 'private', get_post_status( $subscribe ) );
$this->assertFalse( get_option( '_chrnx_fixture_environment_snapshot_v1', false ) );
foreach ( $before as $name => $raw ) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Exact persisted option bytes are the contract.
$after = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", $name ) );
$this->assertSame( $raw, $after, $name );
}
}
}
+25 -6
View File
@@ -34,11 +34,30 @@ test( 'database scenarios run only in a serial Chromium project after all read-o
assert.match( scenario, /test\.afterAll[\s\S]*seedScenario\(\s*['"]default['"]\s*\)/ );
} );
test( 'the npm integration chain finishes PHP and seed-state checks before browser workers', async () => {
test( 'the npm integration chain finishes isolated PHP before browser workers', async () => {
const manifest = JSON.parse( await readFile( 'package.json', 'utf8' ) );
assert.match( manifest.scripts[ 'test:integration' ], /tests\/integration\/seed-state\.sh/ );
assert.match(
manifest.scripts.test,
/npm run test:php && npm run test:integration && npm run test:e2e/,
);
assert.equal( manifest.scripts[ 'test:integration' ], undefined );
assert.match( manifest.scripts.test, /npm run test:php && npm run test:e2e/ );
assert.doesNotMatch( JSON.stringify( manifest.scripts ), /seed-state\.sh/ );
} );
test( 'restore lifecycle runs only in isolated PHPUnit and never in shared DB scripts', async () => {
const manifest = JSON.parse( await readFile( 'package.json', 'utf8' ) );
const phpTest = await readFile( 'tests/php/FixtureEnvironmentTest.php', 'utf8' );
const library = await readFile( 'scripts/lib/fixture-environment.php', 'utf8' );
const files = await readdir( 'tests/integration' ).catch( () => [] );
assert.deepEqual( files.filter( ( name ) => /seed-state|harness-preservation/.test( name ) ), [] );
assert.doesNotMatch( JSON.stringify( manifest.scripts ), /tests\/integration|seed-state/ );
assert.match( phpTest, /WP_UnitTestCase/ );
assert.match( phpTest, /chrnx_fixture_environment_snapshot/ );
assert.match( phpTest, /chrnx_fixture_environment_restore/ );
assert.match( library, /raw_base64/ );
} );
test( 'PHPUnit is pinned to the isolated tests-cli database', async () => {
const manifest = JSON.parse( await readFile( 'package.json', 'utf8' ) );
assert.match( manifest.scripts[ 'test:php' ], /wp-env run tests-cli/ );
assert.doesNotMatch( manifest.scripts[ 'test:php' ], /wp-env run cli/ );
assert.doesNotMatch( JSON.stringify( manifest.scripts ), /wp-env run cli[^\n"]*phpunit/ );
} );
+1 -1
View File
@@ -12,7 +12,7 @@ test('Composer provides the PHPUnit Polyfills version required by WordPress test
assert.equal(composer['require-dev']['phpunit/phpunit'], '^9.6');
assert.equal(
packageConfig.scripts['test:php'],
'wp-env run cli --env-cwd=wp-content/themes/chrnx -- composer exec phpunit --',
'wp-env run tests-cli --env-cwd=wp-content/themes/chrnx -- composer exec phpunit --',
);
});