Files
chrnx-dev-wp/tests/integration/seed-state.php
T

163 lines
6.2 KiB
PHP

<?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.' );