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 ); if ( ! $outer_exists ) { $missing_sentinel = 'chrnx-missing-permalink'; $this->assertSame( $missing_sentinel, get_option( 'permalink_structure', $missing_sentinel ) ); $this->assertTrue( update_option( 'permalink_structure', '/reinserted/%post_id%/' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- A subsequent Options API insert must persist after exact raw absence is restored. $reinserted = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", 'permalink_structure' ) ); $this->assertSame( '/reinserted/%post_id%/', $reinserted ); } } /** 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 ); } } }