diff --git a/scripts/lib/fixture-environment.php b/scripts/lib/fixture-environment.php index e3e3c2a..ef7b1e9 100644 --- a/scripts/lib/fixture-environment.php +++ b/scripts/lib/fixture-environment.php @@ -82,6 +82,17 @@ function chrnx_fixture_environment_restore_raw_option( string $name, array $stat } } +/** Synchronize the live permalink structure, optionally preserving an exact raw option row. */ +function chrnx_fixture_environment_sync_permalink_structure( string $permalink_structure, ?array $raw_state = null ): void { + global $wp_rewrite; + $wp_rewrite->set_permalink_structure( $permalink_structure ); + if ( is_array( $raw_state ) ) { + chrnx_fixture_environment_restore_raw_option( 'permalink_structure', $raw_state ); + wp_cache_delete( 'permalink_structure', 'options' ); + } + flush_rewrite_rules( false ); +} + /** Restore the captured environment and remove its snapshot. */ function chrnx_fixture_environment_restore(): bool { $snapshot_name = '_chrnx_fixture_environment_snapshot_v1'; @@ -121,7 +132,10 @@ function chrnx_fixture_environment_restore(): bool { delete_option( '_chrnx_fixture_featured_limit' ); delete_option( $snapshot_name ); wp_cache_flush(); - flush_rewrite_rules( false ); + $permalink_raw_state = 2 === ( $snapshot['version'] ?? null ) && is_array( $snapshot['options']['permalink_structure'] ?? null ) + ? $snapshot['options']['permalink_structure'] + : null; + chrnx_fixture_environment_sync_permalink_structure( (string) get_option( 'permalink_structure', '' ), $permalink_raw_state ); return true; } @@ -133,11 +147,14 @@ function chrnx_fixture_environment_apply( array $config ): void { $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 ) { + foreach ( array( 'blogname', 'blogdescription', 'show_on_front', 'page_on_front', 'page_for_posts' ) as $name ) { if ( array_key_exists( $name, $config ) ) { update_option( $name, $config[ $name ] ); } } + if ( array_key_exists( 'permalink_structure', $config ) ) { + chrnx_fixture_environment_sync_permalink_structure( (string) $config['permalink_structure'] ); + } $subscribe_id = absint( $config['subscribe_id'] ?? 0 ); if ( $subscribe_id > 0 && isset( $config['subscribe_status'] ) ) { wp_update_post( diff --git a/scripts/seed-test-content.php b/scripts/seed-test-content.php index 7c308e0..59be910 100644 --- a/scripts/seed-test-content.php +++ b/scripts/seed-test-content.php @@ -178,7 +178,6 @@ chrnx_fixture_environment_apply( 'subscribe_available' => 'subscribe-unavailable' !== $chrnx_seed_scenario, ) ); -flush_rewrite_rules( false ); WP_CLI::log( sprintf( diff --git a/tests/e2e/visual-states.spec.mjs b/tests/e2e/visual-states.spec.mjs index aa83243..5a42523 100644 --- a/tests/e2e/visual-states.spec.mjs +++ b/tests/e2e/visual-states.spec.mjs @@ -35,6 +35,26 @@ test( 'home remains coherent at all approved review viewports', async ( { page } } } ); +test( 'seeded post permalink resolves its single template', async ( { page } ) => { + const title = 'The save file remembers what the hero pretends to forget'; + const expectedPath = '/the-save-file-remembers-what-the-hero-pretends-to-forget/'; + await page.goto( '/' ); + const fixtureCard = page.locator( '.chrnx-featured-story', { hasText: title } ); + await expect( fixtureCard.getByRole( 'heading', { name: title, exact: true } ) ).toBeVisible(); + const fixtureLink = fixtureCard.getByRole( 'link' ); + + const [ navigationResponse ] = await Promise.all( [ + page.waitForNavigation(), + fixtureLink.click(), + ] ); + + expect( navigationResponse ).not.toBeNull(); + expect( navigationResponse.status() ).toBe( 200 ); + await expect( page ).toHaveURL( new RegExp( `${ expectedPath }$` ) ); + await expect( page.locator( 'main.chrnx-entry--single' ) ).toBeVisible(); + await expect( page.getByRole( 'heading', { level: 1, name: title, exact: true } ) ).toBeVisible(); +} ); + test( 'topic, music, podcast, and about surfaces use real content and approved art', async ( { page, } ) => { diff --git a/tests/php/FixtureEnvironmentTest.php b/tests/php/FixtureEnvironmentTest.php index dbf6ba3..5dd3803 100644 --- a/tests/php/FixtureEnvironmentTest.php +++ b/tests/php/FixtureEnvironmentTest.php @@ -9,6 +9,46 @@ 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 { + /** Provide exact raw permalink option states that fixture restore must preserve. */ + public function permalink_option_states_provider(): array { + return array( + 'present row' => array( true, '/outer/%post_id%/' ), + 'absent row' => array( false, null ), + ); + } + + /** + * Applying and restoring fixtures synchronize the option row and live rewrite state. + * + * @dataProvider permalink_option_states_provider + */ + public function test_permalink_apply_and_restore_synchronize_raw_option_and_live_rewrite( bool $outer_exists, ?string $outer_value ): void { + global $wpdb, $wp_rewrite; + if ( $outer_exists ) { + update_option( 'permalink_structure', $outer_value ); + } else { + delete_option( 'permalink_structure' ); + } + $wp_rewrite->permalink_structure = $outer_exists ? $outer_value : ''; + + $this->assertTrue( chrnx_fixture_environment_snapshot() ); + chrnx_fixture_environment_apply( array( 'permalink_structure' => '/%postname%/' ) ); + + $this->assertSame( '/%postname%/', get_option( 'permalink_structure' ) ); + $this->assertSame( '/%postname%/', $wp_rewrite->permalink_structure ); + $this->assertTrue( chrnx_fixture_environment_restore() ); + + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Exact raw option existence and bytes are the restore contract. + $restored = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", 'permalink_structure' ), ARRAY_A ); + if ( $outer_exists ) { + $this->assertIsArray( $restored ); + $this->assertSame( $outer_value, $restored['option_value'] ); + } else { + $this->assertNull( $restored ); + } + $this->assertSame( $outer_exists ? $outer_value : '', $wp_rewrite->permalink_structure ); + } + /** Snapshot/restore preserves exact options, unrelated stickies, and owned status. */ public function test_environment_snapshot_scenarios_and_restore_are_exact() { global $wpdb;