Automattic / woocommerce-services

WooCommerce Services is a feature plugin that integrates hosted services into WooCommerce (3.0+), and currently includes automated tax rates and the ability to purchase and print USPS shipping labels.
GNU General Public License v2.0
108 stars 20 forks source link

Add state machine to drive migration states #2757

Closed harriswong closed 5 months ago

harriswong commented 5 months ago

Description

This PR replaces the task queue with a state machine to drive the migration process. This PR will remember where the last "stopped" state is should an error occur. This allows the user to continue where they left off. For example, if the user installed the plugin but failed to activate it, the next time they run the migration, it will start from "activating" instead of the beginning.

image

Related issue(s)

N/A

Steps to test

Happy path - test the full migration

  1. Go to woocommerce-services/classes/class-wc-connect-service-settings-store.php and replace the following is_eligible_for_migration function. This function decides if the modal should popup or not

        public function is_eligible_for_migration() {
            WC_Connect_Options::delete_option( 'wcshipping_migration_state' );
            $migration_state = WC_Connect_Options::get_option( 'wcshipping_migration_state' );
    
            $migration_pending = WC_Connect_WCST_To_WCShipping_Migration_State_Enum::COMPLETED !== $migration_state;
            $migration_enabled = $this->service_schemas_store->is_wcship_wctax_migration_enabled();
    
            return $migration_pending && $migration_enabled;
        }
  2. Make sure you have "WooCommerce Shipping" and "WooCommerce Tax" in your plugins folder. We can't test the "install" part because they aren't part of dotorg yet.
  3. Go to any order page with WCS&T enabled, click the WCS&T "Create shipping label" red button,
  4. Click "Update now" image
  5. It should install, activate Woo Shipping & Woo Tax, deactivate WCS&T, and redirect you to the plugins page.
  6. [Optional] Check DB's wp_options table, wc_connect_options should have the value ..."wcshipping_migration_state";i:12;} appended at the end. Key thing here is 12 is the "done state".

Test 404 on install and activate, fix the issue, then continue

  1. Repeat Step 1 to 3 above. Refresh the page so it deletes the wcshipping_migration_state option.
  2. Now, comment out WC_Connect_Options::delete_option( 'wcshipping_migration_state' ); from the happy path step 1). In this test, we want to keep wcshipping_migration_state so we can test the exceptions.

What we want to test here is to mimic a 404 errors downloading from dotorg. Or some API errors during activation. Here, we will test by tripping the circuit breaker at the wc-admin/plugins/install and wc-admin/plugins/activate API calls. The goal is to test if the migration can continue where it last failed.

  1. npm run start. Open up client/components/migration/migration-runner.js. Append 2 to the API URL so they fail and throw 404. Change the following
    • fetch( getBaseURL() + 'wc-admin/plugins/install', { to fetch( getBaseURL() + 'wc-admin/plugins/install2', {
    • fetch( getBaseURL() + 'wc-admin/plugins/activate', { to fetch( getBaseURL() + 'wc-admin/plugins/activate2', {
  2. Click the "Update now" button
  3. You should see this in the console because /install2 failed image The migration_state value should be 5 because it failed at installation. 5.1 [optional] Check DB wp_options, wc_connect_options should have value appended at the end "wcshipping_migration_state";i:5;}.
  4. Now, fix the issue by changing /install2 back to install. Refresh the page. install should work, and activate will now fail. The installation should pick up from the "install" step.
  5. Confirm it starts from "install" and failed at "activate". The migration_state should be 7. image 7.1. [optional] Check DB wp_options, wc_connect_options should have value appended at the end "wcshipping_migration_state";i:7;}.
  6. Now, fix the activate2 to activate. The migration should pick up from state 7 and go through the whole migration. image
  7. [optional] Check DB wp_options, wc_connect_options should have value appended at the end "wcshipping_migration_state";i:12;}.
  8. You should be on the plugins page http://localhost/wp-admin/plugins.php?plugin_status=all&paged=1&s. Confirm the WCS&T is deactivated, and Woo Shipping and Woo Tax are enabled. image

Checklist

harriswong commented 5 months ago

👋 Hi @Ferdev I think I addressed all the feedback on the PR, let me know what you think! Please feel free to merge for me if it looks good and if I am AFK.