test: verify Related Stories workflows

This commit is contained in:
chrnx committed 2026-08-12 21:54:50 -05:00
1 parent d8ad3d388f
commit 3a34a9990f
8 files changed
+702 -110

No files matched your search

+136 -1
View File
@@ -14,7 +14,7 @@ if ( ! in_array( $chrnx_seed_host, array( 'localhost', '127.0.0.1' ), true ) ) {
WP_CLI::error( 'Refusing to seed a non-local WordPress site.' );
}
$chrnx_seed_scenario = isset( $args[0] ) && is_string( $args[0] ) ? $args[0] : 'default';
$chrnx_seed_scenarios = array( 'default', 'featured-1', 'featured-2', 'featured-3', 'subscribe-unpublished', 'subscribe-unavailable', 'restore' );
$chrnx_seed_scenarios = array( 'default', 'featured-1', 'featured-2', 'featured-3', 'subscribe-unpublished', 'subscribe-unavailable', 'related-1', 'related-2', 'related-4', 'restore' );
if ( ! in_array( $chrnx_seed_scenario, $chrnx_seed_scenarios, true ) ) {
WP_CLI::error( 'Unknown local fixture scenario.' );
}
@@ -81,11 +81,123 @@ function chrnx_seed_upsert_post( array $fixture, string $post_type, int $offset
return absint( $result );
}
/** Return the option that stores temporary unrelated-post statuses. */
function chrnx_seed_related_status_snapshot_name(): string {
return '_chrnx_fixture_related_unowned_status_snapshot_v1';
}
/**
* Capture every eligible unrelated post before a Related Stories scenario.
*
* @return array<int,array{id:int,status:string}> Exact records from the first scenario.
*/
function chrnx_seed_snapshot_related_unowned_statuses(): array {
$snapshot_name = chrnx_seed_related_status_snapshot_name();
$snapshot = get_option( $snapshot_name, false );
if ( is_array( $snapshot ) && 1 === ( $snapshot['version'] ?? null ) && is_array( $snapshot['posts'] ?? null ) ) {
return $snapshot['posts'];
}
$candidate_ids = get_posts(
array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
'no_found_rows' => true,
'meta_query' => array(
array(
'key' => '_chrnx_fixture_key',
'compare' => 'NOT EXISTS',
),
),
)
);
$records = array();
foreach ( $candidate_ids as $candidate_id ) {
$candidate_id = absint( $candidate_id );
if ( chrnx_is_eligible_editorial_post( $candidate_id ) ) {
$records[] = array(
'id' => $candidate_id,
'status' => (string) get_post_status( $candidate_id ),
);
}
}
update_option(
$snapshot_name,
array(
'version' => 1,
'posts' => $records,
),
false
);
return $records;
}
/** Temporarily draft the exact unrelated posts captured for a Related Stories scenario. */
function chrnx_seed_draft_related_unowned_posts(): void {
foreach ( chrnx_seed_snapshot_related_unowned_statuses() as $record ) {
$post_id = absint( $record['id'] ?? 0 );
if ( $post_id <= 0 || ! get_post( $post_id ) ) {
continue;
}
$result = wp_update_post(
array(
'ID' => $post_id,
'post_status' => 'draft',
),
true
);
if ( is_wp_error( $result ) ) {
WP_CLI::error( 'Could not isolate an unrelated Related Stories fixture candidate.' );
}
}
}
/** Restore exact unrelated-post statuses after a Related Stories scenario. */
function chrnx_seed_restore_related_unowned_statuses(): bool {
$snapshot_name = chrnx_seed_related_status_snapshot_name();
$snapshot = get_option( $snapshot_name, false );
if ( ! is_array( $snapshot ) ) {
return false;
}
if ( 1 !== ( $snapshot['version'] ?? null ) || ! is_array( $snapshot['posts'] ?? null ) ) {
WP_CLI::error( 'Related Stories fixture snapshot is invalid.' );
}
foreach ( $snapshot['posts'] as $record ) {
$post_id = absint( $record['id'] ?? 0 );
$status = sanitize_key( $record['status'] ?? '' );
if ( $post_id <= 0 || ! get_post( $post_id ) ) {
continue;
}
if ( ! array_key_exists( $status, get_post_stati() ) ) {
WP_CLI::error( 'Related Stories fixture snapshot contains an invalid post status.' );
}
$result = wp_update_post(
array(
'ID' => $post_id,
'post_status' => $status,
),
true
);
if ( is_wp_error( $result ) ) {
WP_CLI::error( 'Could not restore an unrelated Related Stories fixture candidate.' );
}
}
delete_option( $snapshot_name );
return true;
}
if ( 'restore' === $chrnx_seed_scenario ) {
chrnx_seed_restore_related_unowned_statuses();
WP_CLI::log( chrnx_fixture_environment_restore() ? 'CHRNX_FIXTURES_RESTORE snapshot=restored' : 'CHRNX_FIXTURES_RESTORE snapshot=none' );
return;
}
if ( 'default' === $chrnx_seed_scenario ) {
chrnx_seed_restore_related_unowned_statuses();
}
chrnx_fixture_environment_snapshot();
chrnx_seed_editorial_terms();
$chrnx_seed_pages = chrnx_seed_read_fixture( 'pages.json' );
@@ -149,6 +261,29 @@ foreach ( $chrnx_seed_posts as $chrnx_seed_index => $chrnx_seed_post ) {
$chrnx_seed_post_ids[ $chrnx_seed_post['fixtureKey'] ] = $chrnx_seed_post_id;
}
if ( preg_match( '/^related-([124])$/', $chrnx_seed_scenario, $chrnx_seed_related_match ) ) {
chrnx_seed_draft_related_unowned_posts();
$chrnx_seed_related_count = absint( $chrnx_seed_related_match[1] );
$chrnx_seed_current_key = 'post-literature-long';
$chrnx_seed_related_candidates = array_values( array_diff( array_keys( $chrnx_seed_post_ids ), array( $chrnx_seed_current_key ) ) );
$chrnx_seed_published_keys = array_merge(
array( $chrnx_seed_current_key ),
array_slice( $chrnx_seed_related_candidates, 0, $chrnx_seed_related_count )
);
foreach ( $chrnx_seed_post_ids as $chrnx_seed_post_key => $chrnx_seed_owned_post_id ) {
$chrnx_seed_status_result = wp_update_post(
array(
'ID' => $chrnx_seed_owned_post_id,
'post_status' => in_array( $chrnx_seed_post_key, $chrnx_seed_published_keys, true ) ? 'publish' : 'draft',
),
true
);
if ( is_wp_error( $chrnx_seed_status_result ) ) {
WP_CLI::error( 'Could not apply Related Stories fixture status.' );
}
}
}
$chrnx_seed_sticky_ids = array();
foreach ( $chrnx_seed_posts as $chrnx_seed_post ) {
if ( ! empty( $chrnx_seed_post['featured'] ) && isset( $chrnx_seed_post_ids[ $chrnx_seed_post['fixtureKey'] ] ) ) {