From 57e7e9aaf3c432e80f8454c07b42e210b98fef50 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Wed, 12 Aug 2026 10:25:49 -0500 Subject: [PATCH] fix: isolate CHRNX seed lifecycle tests --- README.md | 4 + .../2026-08-12-local-integration-isolation.md | 6 +- package.json | 5 +- scripts/lib/fixture-environment.php | 153 +++++++++++++++++ scripts/seed-test-content.php | 125 ++------------ tests/integration/seed-state.php | 162 ------------------ tests/integration/seed-state.sh | 34 ---- tests/php/FixtureEnvironmentTest.php | 71 ++++++++ tests/smoke/e2e-harness.test.mjs | 31 +++- tests/smoke/theme-contract.test.mjs | 2 +- 10 files changed, 277 insertions(+), 316 deletions(-) create mode 100644 scripts/lib/fixture-environment.php delete mode 100644 tests/integration/seed-state.php delete mode 100755 tests/integration/seed-state.sh create mode 100644 tests/php/FixtureEnvironmentTest.php diff --git a/README.md b/README.md index 3e560ce..15297bf 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,10 @@ node scripts/capture-visual-review.mjs The capture script rejects public hosts and writes its screenshots to `tmp/imagegen/`, which Git ignores. +PHPUnit always runs through wp-env's isolated `tests-cli` database. The browser +suite uses the separate development `cli` database; its serial scenario project +restores the default 7-page/12-post fixture dataset before exiting. + Use `npm run env:stop` to stop the local WordPress environment. If Homebrew's Docker CLI cannot find an existing standalone Compose v2 binary, diff --git a/docs/superpowers/plans/2026-08-12-local-integration-isolation.md b/docs/superpowers/plans/2026-08-12-local-integration-isolation.md index 2e20ec4..d90bcb2 100644 --- a/docs/superpowers/plans/2026-08-12-local-integration-isolation.md +++ b/docs/superpowers/plans/2026-08-12-local-integration-isolation.md @@ -6,7 +6,7 @@ **Goal:** Make local WordPress fixtures, Playwright scenarios, show archive links, and image rebuild checks deterministic, isolated, reversible, and safe. -**Architecture:** Browser readers run in three dependency roots while a serial Chromium scenario project mutates the local database only after all roots finish. The local seeder owns fixture records, snapshots environment options once, merges unrelated sticky posts, and restores exact original option values explicitly. Show archive routing derives only from validated archive context. Responsive image rebuilds accept the committed destination or one canonical direct child of the system temporary directory. +**Architecture:** Browser readers run in three dependency roots while a serial Chromium scenario project mutates the local database only after all roots finish. Snapshot and restore lifecycle tests execute only in the isolated WordPress PHPUnit database; automated tests never run a destructive lifecycle harness against the development E2E database. The local seeder owns fixture records, merges unrelated sticky posts, and restores exact original option bytes explicitly. **Tech Stack:** WordPress PHP, WP-CLI/wp-env on Colima, Playwright, Node test runner, POSIX shell. @@ -23,12 +23,12 @@ ## Task 2: Restore local WordPress configuration exactly -**Files:** `scripts/seed-test-content.php`, `scripts/seed-test-content.sh`, `tests/php/SeedStateTest.php`, `README.md` +**Files:** `scripts/seed-test-content.php`, `scripts/lib/fixture-environment.php`, `tests/php/FixtureEnvironmentTest.php`, `README.md` - [ ] Write adversarial WP/PHP integration tests for snapshot, sticky merge, scenarios, and exact restore. - [ ] Run the tests and record the expected RED failure. - [ ] Add versioned snapshot ownership and explicit restore scenario. -- [ ] Run default/scenarios/restore/default and prove exact restoration plus idempotency. +- [ ] Prove exact restoration and scenario ownership in the isolated PHPUnit DB. ## Task 3: Preserve show archive routing diff --git a/package.json b/package.json index 70e6058..cf9ed1c 100644 --- a/package.json +++ b/package.json @@ -16,13 +16,12 @@ "phpcs": "npm run composer -- exec phpcs -- -q", "test:smoke": "node --test tests/smoke/*.test.mjs", "test:js": "wp-scripts test-unit-js --runInBand", - "test:php": "wp-env run cli --env-cwd=wp-content/themes/chrnx -- composer exec phpunit --", - "test:integration": "./tests/integration/seed-state.sh", + "test:php": "wp-env run tests-cli --env-cwd=wp-content/themes/chrnx -- composer exec phpunit --", "test:e2e": "playwright test", "lint:js": "wp-scripts lint-js assets/js tests/js tests/e2e", "lint:css": "wp-scripts lint-style 'assets/css/**/*.css' 'blocks/**/*.css'", "lint:md": "wp-scripts lint-md-docs docs README.md", - "test": "npm run test:smoke && npm run lint:js && npm run lint:css && npm run phpcs && npm run test:js && npm run test:php && npm run test:integration && npm run test:e2e" + "test": "npm run test:smoke && npm run lint:js && npm run lint:css && npm run phpcs && npm run test:js && npm run test:php && npm run test:e2e" }, "devDependencies": { "@playwright/test": "1.62.1", diff --git a/scripts/lib/fixture-environment.php b/scripts/lib/fixture-environment.php new file mode 100644 index 0000000..e3e3c2a --- /dev/null +++ b/scripts/lib/fixture-environment.php @@ -0,0 +1,153 @@ +get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", $name ) ); + return array( + 'exists' => null !== $raw, + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Binary-safe storage of exact local option bytes, not executable code. + 'raw_base64' => null === $raw ? null : base64_encode( $raw ), + ); +} + +/** Find one post owned by a stable fixture key. */ +function chrnx_fixture_environment_find_post( string $fixture_key, string $post_type ): int { + $ids = get_posts( + array( + 'post_type' => $post_type, + 'post_status' => 'any', + 'posts_per_page' => 1, + 'fields' => 'ids', + 'no_found_rows' => true, + 'meta_key' => '_chrnx_fixture_key', + 'meta_value' => $fixture_key, + ) + ); + return empty( $ids ) ? 0 : absint( $ids[0] ); +} + +/** Capture exact outer state once. */ +function chrnx_fixture_environment_snapshot(): bool { + $snapshot_name = '_chrnx_fixture_environment_snapshot_v1'; + if ( is_array( get_option( $snapshot_name, false ) ) ) { + return false; + } + $options = array(); + foreach ( chrnx_fixture_environment_option_names() as $name ) { + $options[ $name ] = chrnx_fixture_environment_raw_option( $name ); + } + $subscribe_id = chrnx_fixture_environment_find_post( 'page-subscribe', 'page' ); + update_option( + $snapshot_name, + array( + 'version' => 2, + 'options' => $options, + 'subscribe' => array( + 'exists' => $subscribe_id > 0, + 'id' => $subscribe_id, + 'status' => $subscribe_id > 0 ? get_post_status( $subscribe_id ) : null, + ), + ), + false + ); + return true; +} + +/** Restore one raw option row exactly. */ +function chrnx_fixture_environment_restore_raw_option( string $name, array $state ): void { + global $wpdb; + if ( ! empty( $state['exists'] ) ) { + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- Binary-safe restoration of exact local option bytes, not executable code. + $raw = base64_decode( (string) ( $state['raw_base64'] ?? '' ), true ); + if ( ! is_string( $raw ) ) { + throw new InvalidArgumentException( 'Fixture option snapshot contains invalid bytes.' ); + } + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Exact raw restoration is required for reversible local fixtures. + $wpdb->query( $wpdb->prepare( "INSERT INTO {$wpdb->options} (option_name, option_value, autoload) VALUES (%s, %s, 'no') ON DUPLICATE KEY UPDATE option_value = VALUES(option_value)", $name, $raw ) ); + } else { + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Exact option nonexistence is required for reversible local fixtures. + $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name = %s", $name ) ); + } +} + +/** Restore the captured environment and remove its snapshot. */ +function chrnx_fixture_environment_restore(): bool { + $snapshot_name = '_chrnx_fixture_environment_snapshot_v1'; + $snapshot = get_option( $snapshot_name, null ); + if ( ! is_array( $snapshot ) || ! is_array( $snapshot['options'] ?? null ) ) { + return false; + } + if ( 2 === ( $snapshot['version'] ?? null ) ) { + foreach ( $snapshot['options'] as $name => $state ) { + if ( is_string( $name ) && is_array( $state ) ) { + chrnx_fixture_environment_restore_raw_option( $name, $state ); + } + } + } elseif ( 1 === ( $snapshot['version'] ?? null ) ) { + foreach ( $snapshot['options'] as $name => $state ) { + if ( ! is_string( $name ) || ! is_array( $state ) ) { + continue; + } + if ( ! empty( $state['exists'] ) ) { + update_option( $name, $state['value'] ?? null ); + } else { + delete_option( $name ); + } + } + } else { + return false; + } + $subscribe = $snapshot['subscribe'] ?? array(); + if ( ! empty( $subscribe['exists'] ) && get_post( absint( $subscribe['id'] ?? 0 ) ) ) { + wp_update_post( + array( + 'ID' => absint( $subscribe['id'] ), + 'post_status' => sanitize_key( $subscribe['status'] ?? 'publish' ), + ) + ); + } + delete_option( '_chrnx_fixture_featured_limit' ); + delete_option( $snapshot_name ); + wp_cache_flush(); + flush_rewrite_rules( false ); + return true; +} + +/** Apply one fixture-owned presentation without replacing unrelated sticky posts. */ +function chrnx_fixture_environment_apply( array $config ): void { + $fixture_ids = array_map( 'absint', $config['fixture_ids'] ?? $config['featured_ids'] ?? array() ); + $featured_ids = array_map( 'absint', $config['featured_ids'] ?? array() ); + $featured_count = max( 0, absint( $config['featured_count'] ?? count( $featured_ids ) ) ); + $current = array_map( 'absint', (array) get_option( 'sticky_posts', array() ) ); + $unrelated = array_values( array_diff( $current, $fixture_ids ) ); + update_option( 'sticky_posts', array_values( array_unique( array_merge( $unrelated, array_slice( $featured_ids, 0, $featured_count ) ) ) ) ); + foreach ( array( 'blogname', 'blogdescription', 'show_on_front', 'page_on_front', 'page_for_posts', 'permalink_structure' ) as $name ) { + if ( array_key_exists( $name, $config ) ) { + update_option( $name, $config[ $name ] ); + } + } + $subscribe_id = absint( $config['subscribe_id'] ?? 0 ); + if ( $subscribe_id > 0 && isset( $config['subscribe_status'] ) ) { + wp_update_post( + array( + 'ID' => $subscribe_id, + 'post_status' => sanitize_key( $config['subscribe_status'] ), + ) + ); + } + if ( array_key_exists( 'subscribe_available', $config ) ) { + update_option( 'chrnx_subscribe_page_id', $config['subscribe_available'] ? $subscribe_id : 0 ); + } +} diff --git a/scripts/seed-test-content.php b/scripts/seed-test-content.php index 4478dbb..7c308e0 100644 --- a/scripts/seed-test-content.php +++ b/scripts/seed-test-content.php @@ -7,6 +7,8 @@ * @package CHRNX */ +require_once __DIR__ . '/lib/fixture-environment.php'; + $chrnx_seed_host = wp_parse_url( home_url( '/' ), PHP_URL_HOST ); if ( ! in_array( $chrnx_seed_host, array( 'localhost', '127.0.0.1' ), true ) ) { WP_CLI::error( 'Refusing to seed a non-local WordPress site.' ); @@ -44,96 +46,6 @@ function chrnx_seed_read_fixture( string $filename ): array { * @param string $post_type Post type. * @return int Post ID or zero. */ -function chrnx_seed_find_post( string $fixture_key, string $post_type ): int { - $ids = get_posts( - array( - 'post_type' => $post_type, - 'post_status' => 'any', - 'posts_per_page' => 1, - 'fields' => 'ids', - 'no_found_rows' => true, - 'suppress_filters' => true, - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'meta_key' => '_chrnx_fixture_key', - 'meta_value' => $fixture_key, - ) - ); - return empty( $ids ) ? 0 : absint( $ids[0] ); -} - -/** Return an option value together with whether the row exists. */ -function chrnx_seed_get_option_state( string $name ): array { - $sentinel = new stdClass(); - $value = get_option( $name, $sentinel ); - return array( - 'exists' => $sentinel !== $value, - 'value' => $sentinel === $value ? null : $value, - ); -} - -/** Capture local configuration once before applying the visual fixture environment. */ -function chrnx_seed_snapshot_environment(): void { - $snapshot_name = '_chrnx_fixture_environment_snapshot_v1'; - if ( is_array( get_option( $snapshot_name, false ) ) ) { - return; - } - - $options = array(); - foreach ( array( 'sticky_posts', 'blogname', 'blogdescription', 'show_on_front', 'page_on_front', 'page_for_posts', 'permalink_structure', 'chrnx_subscribe_page_id' ) as $name ) { - $options[ $name ] = chrnx_seed_get_option_state( $name ); - } - $subscribe_id = chrnx_seed_find_post( 'page-subscribe', 'page' ); - $snapshot = array( - 'version' => 1, - 'options' => $options, - 'subscribe' => array( - 'exists' => $subscribe_id > 0, - 'id' => $subscribe_id, - 'status' => $subscribe_id > 0 ? get_post_status( $subscribe_id ) : null, - ), - ); - update_option( $snapshot_name, $snapshot, false ); -} - -/** Restore the exact local configuration captured before fixture ownership began. */ -function chrnx_seed_restore_environment(): void { - $snapshot_name = '_chrnx_fixture_environment_snapshot_v1'; - $snapshot = get_option( $snapshot_name, null ); - if ( ! is_array( $snapshot ) || 1 !== ( $snapshot['version'] ?? null ) || ! is_array( $snapshot['options'] ?? null ) ) { - WP_CLI::log( 'CHRNX_FIXTURES_RESTORE snapshot=none' ); - return; - } - - foreach ( $snapshot['options'] as $name => $state ) { - if ( ! is_string( $name ) || ! is_array( $state ) || ! array_key_exists( 'exists', $state ) ) { - WP_CLI::error( 'Fixture environment snapshot is malformed.' ); - } - if ( $state['exists'] ) { - update_option( $name, $state['value'] ?? null ); - } else { - delete_option( $name ); - } - } - - $subscribe = $snapshot['subscribe'] ?? array(); - if ( ! empty( $subscribe['exists'] ) ) { - $subscribe_id = absint( $subscribe['id'] ?? 0 ); - if ( $subscribe_id > 0 && 'page-subscribe' === get_post_meta( $subscribe_id, '_chrnx_fixture_key', true ) ) { - wp_update_post( - array( - 'ID' => $subscribe_id, - 'post_status' => sanitize_key( $subscribe['status'] ?? 'publish' ), - ) - ); - } - } - delete_option( '_chrnx_fixture_featured_limit' ); - delete_option( $snapshot_name ); - flush_rewrite_rules( false ); - WP_CLI::log( 'CHRNX_FIXTURES_RESTORE snapshot=restored' ); -} - /** * Insert or update one fixture-owned post without touching unrelated content. * @@ -148,7 +60,7 @@ function chrnx_seed_upsert_post( array $fixture, string $post_type, int $offset WP_CLI::error( 'Every fixture requires a fixtureKey.' ); } - $post_id = chrnx_seed_find_post( $key, $post_type ); + $post_id = chrnx_fixture_environment_find_post( $key, $post_type ); $payload = array( 'ID' => $post_id, 'post_type' => $post_type, @@ -170,11 +82,11 @@ function chrnx_seed_upsert_post( array $fixture, string $post_type, int $offset } if ( 'restore' === $chrnx_seed_scenario ) { - chrnx_seed_restore_environment(); + WP_CLI::log( chrnx_fixture_environment_restore() ? 'CHRNX_FIXTURES_RESTORE snapshot=restored' : 'CHRNX_FIXTURES_RESTORE snapshot=none' ); return; } -chrnx_seed_snapshot_environment(); +chrnx_fixture_environment_snapshot(); chrnx_seed_editorial_terms(); $chrnx_seed_pages = chrnx_seed_read_fixture( 'pages.json' ); $chrnx_seed_posts = chrnx_seed_read_fixture( 'posts.json' ); @@ -250,23 +162,22 @@ if ( preg_match( '/^featured-([123])$/', $chrnx_seed_scenario, $chrnx_seed_match } else { delete_option( '_chrnx_fixture_featured_limit' ); } -$chrnx_seed_current_stickies = array_map( 'absint', (array) get_option( 'sticky_posts', array() ) ); -$chrnx_seed_unrelated_stickies = array_values( array_diff( $chrnx_seed_current_stickies, array_values( $chrnx_seed_post_ids ) ) ); -$chrnx_seed_selected_stickies = array_slice( $chrnx_seed_sticky_ids, 0, $chrnx_seed_featured_count ); -update_option( 'sticky_posts', array_values( array_unique( array_merge( $chrnx_seed_unrelated_stickies, $chrnx_seed_selected_stickies ) ) ) ); -update_option( 'blogname', 'CHRNX' ); -update_option( 'blogdescription', 'Ideas made tangible.' ); -update_option( 'show_on_front', 'page' ); -update_option( 'page_on_front', $chrnx_seed_page_ids['home'] ); -update_option( 'page_for_posts', 0 ); -wp_update_post( +chrnx_fixture_environment_apply( array( - 'ID' => $chrnx_seed_page_ids['subscribe'], - 'post_status' => 'subscribe-unpublished' === $chrnx_seed_scenario ? 'draft' : 'publish', + 'fixture_ids' => array_values( $chrnx_seed_post_ids ), + 'featured_ids' => $chrnx_seed_sticky_ids, + 'featured_count' => $chrnx_seed_featured_count, + 'blogname' => 'CHRNX', + 'blogdescription' => 'Ideas made tangible.', + 'show_on_front' => 'page', + 'page_on_front' => $chrnx_seed_page_ids['home'], + 'page_for_posts' => 0, + 'permalink_structure' => '/%postname%/', + 'subscribe_id' => $chrnx_seed_page_ids['subscribe'], + 'subscribe_status' => 'subscribe-unpublished' === $chrnx_seed_scenario ? 'draft' : 'publish', + 'subscribe_available' => 'subscribe-unavailable' !== $chrnx_seed_scenario, ) ); -update_option( 'chrnx_subscribe_page_id', 'subscribe-unavailable' === $chrnx_seed_scenario ? 0 : $chrnx_seed_page_ids['subscribe'] ); -update_option( 'permalink_structure', '/%postname%/' ); flush_rewrite_rules( false ); WP_CLI::log( diff --git a/tests/integration/seed-state.php b/tests/integration/seed-state.php deleted file mode 100644 index 88ffa69..0000000 --- a/tests/integration/seed-state.php +++ /dev/null @@ -1,162 +0,0 @@ -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.' ); diff --git a/tests/integration/seed-state.sh b/tests/integration/seed-state.sh deleted file mode 100755 index 333c42e..0000000 --- a/tests/integration/seed-state.sh +++ /dev/null @@ -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 diff --git a/tests/php/FixtureEnvironmentTest.php b/tests/php/FixtureEnvironmentTest.php new file mode 100644 index 0000000..dbf6ba3 --- /dev/null +++ b/tests/php/FixtureEnvironmentTest.php @@ -0,0 +1,71 @@ +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 ); + } + } +} diff --git a/tests/smoke/e2e-harness.test.mjs b/tests/smoke/e2e-harness.test.mjs index c8c80b4..efe9ccf 100644 --- a/tests/smoke/e2e-harness.test.mjs +++ b/tests/smoke/e2e-harness.test.mjs @@ -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/ ); } ); diff --git a/tests/smoke/theme-contract.test.mjs b/tests/smoke/theme-contract.test.mjs index ac03cdb..3dc74e5 100644 --- a/tests/smoke/theme-contract.test.mjs +++ b/tests/smoke/theme-contract.test.mjs @@ -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 --', ); });