|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * E2E helper for the Fixer Option modes. Mapped into wp-content/mu-plugins via .wp-env.json. |
| 4 | + * |
| 5 | + * Seeds one broken link that already has an archived version and a recent check, so the |
| 6 | + * front-end checker resolves it locally (no live API call). A spec can then toggle the |
| 7 | + * fixer mode and assert the front-end behaviour for each: |
| 8 | + * |
| 9 | + * - replace_link : href rewritten to the archive URL + `iawmlf-broken-link` class added. |
| 10 | + * - check_only : link checked (data-iawmlf-archived-url set) but href left untouched, no class. |
| 11 | + * - do_nothing : the checker script is never enqueued, so the link is left entirely alone. |
| 12 | + * |
| 13 | + * Commands: |
| 14 | + * wp iawmlf-e2e-fixer-mode seed -> seeds the post + broken/archived link, prints URLs. |
| 15 | + * wp iawmlf-e2e-fixer-mode mode <value> -> sets the fixer option (replace_link|check_only|do_nothing). |
| 16 | + * wp iawmlf-e2e-fixer-mode cleanup -> removes the post + link and resets the fixer option. |
| 17 | + * |
| 18 | + * @package Internet_Archive\Wayback_Machine_Link_Fixer\E2E |
| 19 | + */ |
| 20 | + |
| 21 | +defined( 'ABSPATH' ) || exit; |
| 22 | + |
| 23 | +use Internet_Archive\Wayback_Machine_Link_Fixer\Settings\Settings; |
| 24 | + |
| 25 | +const IAWMLF_E2E_FIXER_LINK_URL = 'https://example.com/iawmlf-e2e-fixer-mode'; |
| 26 | +const IAWMLF_E2E_FIXER_ARCHIVED_URL = 'https://web.archive.org/web/2024/https://example.com/iawmlf-e2e-fixer-mode'; |
| 27 | +const IAWMLF_E2E_FIXER_POST_SLUG = 'iawmlf-e2e-fixer-mode'; |
| 28 | + |
| 29 | +if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
| 30 | + return; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Delete the seeded post + link and reset the fixer option (idempotent). |
| 35 | + * |
| 36 | + * @return void |
| 37 | + */ |
| 38 | +function iawmlf_e2e_fixer_mode_cleanup(): void { |
| 39 | + global $wpdb; |
| 40 | + |
| 41 | + update_option( Settings::FIXER_OPTION, Settings::FIXER_OPTION_REPLACE_LINK ); |
| 42 | + $wpdb->delete( Settings::get_link_table_name(), array( 'url' => IAWMLF_E2E_FIXER_LINK_URL ), array( '%s' ) ); |
| 43 | + |
| 44 | + foreach ( get_posts( |
| 45 | + array( |
| 46 | + 'name' => IAWMLF_E2E_FIXER_POST_SLUG, |
| 47 | + 'post_type' => 'post', |
| 48 | + 'post_status' => 'any', |
| 49 | + 'numberposts' => 1, |
| 50 | + ) |
| 51 | + ) as $p ) { |
| 52 | + wp_delete_post( $p->ID, true ); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// Seed one broken link with an archived version + a recent check, and a post that links to it. |
| 57 | +WP_CLI::add_command( |
| 58 | + 'iawmlf-e2e-fixer-mode seed', |
| 59 | + function (): void { |
| 60 | + global $wpdb; |
| 61 | + |
| 62 | + update_option( Settings::POST_ACTIVATION_ONBOARDING_KEY, Settings::ONBOARDING_COMPLETED_OPTION ); |
| 63 | + update_option( Settings::SETUP_WIZARD_COMPLETED_KEY, true ); |
| 64 | + update_option( Settings::SETUP_WIZARD_STEP_KEY, 'complete' ); |
| 65 | + update_option( Settings::LINK_EXCLUSIONS, array() ); |
| 66 | + |
| 67 | + iawmlf_e2e_fixer_mode_cleanup(); |
| 68 | + |
| 69 | + // Broken, with an archived version and a recent check so the front-end resolves it |
| 70 | + // locally (checks within the delay window skip the live REST call). |
| 71 | + $wpdb->insert( |
| 72 | + Settings::get_link_table_name(), |
| 73 | + array( |
| 74 | + 'url' => IAWMLF_E2E_FIXER_LINK_URL, |
| 75 | + 'archived' => IAWMLF_E2E_FIXER_ARCHIVED_URL, |
| 76 | + 'checks' => wp_json_encode( array( array( 'date' => gmdate( 'Y-m-d H:i:s' ), 'http_code' => 404 ) ) ), |
| 77 | + 'message' => '', |
| 78 | + 'redirect_url' => '', |
| 79 | + 'is_broken' => 1, |
| 80 | + 'excluded' => 0, |
| 81 | + 'archive_process' => 'done', |
| 82 | + ), |
| 83 | + array( '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s' ) |
| 84 | + ); |
| 85 | + $link_id = (int) $wpdb->insert_id; |
| 86 | + |
| 87 | + $post_id = wp_insert_post( |
| 88 | + array( |
| 89 | + 'post_title' => 'IAWMLF E2E Fixer Mode', |
| 90 | + 'post_name' => IAWMLF_E2E_FIXER_POST_SLUG, |
| 91 | + 'post_status' => 'publish', |
| 92 | + 'post_type' => 'post', |
| 93 | + 'post_content' => sprintf( '<p>Check: <a href="%s">fixer mode link</a></p>', esc_url( IAWMLF_E2E_FIXER_LINK_URL ) ), |
| 94 | + ) |
| 95 | + ); |
| 96 | + |
| 97 | + update_post_meta( $post_id, Settings::LINK_META_KEY, array( $link_id ) ); |
| 98 | + |
| 99 | + echo 'POST_URL=' . get_permalink( $post_id ) . "\n"; |
| 100 | + echo 'LINK_URL=' . IAWMLF_E2E_FIXER_LINK_URL . "\n"; |
| 101 | + echo 'ARCHIVED_URL=' . IAWMLF_E2E_FIXER_ARCHIVED_URL . "\n"; |
| 102 | + } |
| 103 | +); |
| 104 | + |
| 105 | +// Set the fixer option: wp iawmlf-e2e-fixer-mode mode <replace_link|check_only|do_nothing>. |
| 106 | +WP_CLI::add_command( |
| 107 | + 'iawmlf-e2e-fixer-mode mode', |
| 108 | + function ( array $args ): void { |
| 109 | + $mode = isset( $args[0] ) ? (string) $args[0] : ''; |
| 110 | + $valid = array( |
| 111 | + Settings::FIXER_OPTION_REPLACE_LINK, |
| 112 | + Settings::FIXER_OPTION_CHECK_ONLY, |
| 113 | + Settings::FIXER_OPTION_DO_NOTHING, |
| 114 | + ); |
| 115 | + |
| 116 | + if ( ! in_array( $mode, $valid, true ) ) { |
| 117 | + WP_CLI::error( 'Invalid mode: ' . $mode ); |
| 118 | + } |
| 119 | + |
| 120 | + update_option( Settings::FIXER_OPTION, $mode ); |
| 121 | + echo 'MODE=' . $mode . "\n"; |
| 122 | + } |
| 123 | +); |
| 124 | + |
| 125 | +WP_CLI::add_command( |
| 126 | + 'iawmlf-e2e-fixer-mode cleanup', |
| 127 | + function (): void { |
| 128 | + iawmlf_e2e_fixer_mode_cleanup(); |
| 129 | + echo "CLEANED=1\n"; |
| 130 | + } |
| 131 | +); |
0 commit comments