72 lines
3.2 KiB
PHP
72 lines
3.2 KiB
PHP
<?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 );
|
|
}
|
|
}
|
|
}
|