Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve remote specifications transient handling and error management #44384

Merged
merged 13 commits into from Feb 15, 2024

Conversation

chihsuan
Copy link
Member

@chihsuan chihsuan commented Feb 6, 2024

Submission Review Guidelines:

Changes proposed in this Pull Request:

Closes #44261.

  1. Improve remote specifications transient handling and error management

For PaymentGatewaySuggestions and RemoteFreeExtensions,

  • When "suggestions" is empty, replace transient with defaults and save for 3 hours
  • When "suggestions" is not empty but has errors, save transient for 3 hours

For WCPayPromotion:

  • When it has errors, save the transient for 3 hours
  • We don't need to replace the transient with defaults because the default is an empty array. DataSourcePoller will always re-fetch the data when it's an empty array.
  1. Adds unit tests

How to test the changes in this Pull Request:

Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:

Testing

Test remote specs still work as expected

  1. If using an existing site, clear transients by running wp transient delete --all

  2. Ensure WooCommerce > Settings > Advanced > Woo.com > Display suggestions within WooCommerce is checked

  3. Go to Setup Wizard, /wp-admin/admin.php?page=wc-admin&path=/setup-wizard

  4. Click continue

  5. In the Which one of these best describes you? step, select I'm already selling and I'm selling both online and offline

  6. Select Australia — Australian Capital Territory and continue

  7. Skip plugin installation

  8. Observe no fatal when loading homescreen

  9. Go to Tools > WCA Test Helper > Tools, Run wc_admin_daily job

  10. Ensure the job is run successfully

  11. Go to /wp-admin/admin.php?page=wc-admin&task=payments

  12. Confirm it displays the gateways like below

  1. Run wp transient get timeout_woocommerce_admin_payment_gateway_suggestions_specs
  2. Confirm it returns a timestamp that is about 7 days from now
  3. Run wp transient get timeout_woocommerce_admin_remote_free_extensions_specs
  4. Confirm it returns a timestamp that is about 7 days from now
  5. Run wp transient get timeout_woocommerce_admin_payment_method_promotion_specs
  6. Confirm it returns a timestamp that is about 7 days from now

Has errors

Setup:

  1. Cherry-pick the commit a2bf028 from this branch
  2. Or manually set the following APIs
throw new Exception( 'Fail' );
  1. Runwp transient delete --all
  2. Ensure WooCommerce > Settings > Advanced > Woo.com > Display suggestions within WooCommerce is checked
  3. Go to Tools > WCA Test Helper > Tools, Run wc_admin_daily job
  4. Ensure the job is run successfully
  5. Go to /wp-admin/admin.php?page=wc-admin&task=payments
  6. Observe that it shows Airwallex Payments gateway
  1. Go to /wp-admin/admin.php?page=wc-admin&task=marketing
  2. Observe that it shows Google Listings & Ads, Pinterest for WooCommerce, and TikTok for WooCommerce in GROW YOUR STOR section.
  3. Run wp transient get woocommerce_admin_payment_gateway_suggestions_specs --format=json
  4. Observe that it returns default specs instead of specs from https://api.npoint.io/83d63d29d478ececdbc5
{"en_US":[{"id":"affirm","title":"Affirm","content":"Affirm\u2019s tailored Buy Now Pay Later programs remove price as a barrier, turning browsers into buyers, increasing average order value, and expanding your customer base."......
  1. Run wp transient get timeout_woocommerce_admin_payment_gateway_suggestions_specs
  2. Confirm it returns a timestamp that is less than 3 hours from now
  3. Run wp transient get woocommerce_admin_remote_free_extensions_specs --format=json
  4. Observe that it returns default specs instead of specs from https://api.npoint.io/34e72d8b75957e47ff1e
{"en_US":[{"key":"obw\/basics","title":"Get the basics","plugins":[{"name":"WooPayments","image_url":"http:\/\/localhost:8885\/mywordpress\/wp-content\/plugins\/woocommerce\/assets\/images\/onboarding\/wcpay.svg","description":"Accept credit cards and other popular payment methods with <a href=\"https:\/\/woo.com\/products\/woocommerce-payments\" target=\"_blank\">WooPayments<\/a>","is_visible":[{"type":"or","operands":[{"type":"base_location_country","value":"US","operation":"="},{"type":"base_location_country","value":"PR","operation":"="},{"type":"base_location_country","value":"AU","operation":"="},{"type":"base_location_country","value":"CA","operation":"="},....
  1. Run wp transient get timeout_woocommerce_admin_remote_free_extensions_specs
  2. Confirm it returns a timestamp that is less than 3 hours from now.
  3. Run wp transient get woocommerce_admin_payment_method_promotion_specs --format=json
  4. Observe that it returns same data as https://api.npoint.io/6eed3f7093fa2ca8fd1f
  5. Run wp transient get timeout_woocommerce_admin_payment_method_promotion_specs
  6. Confirm it returns a timestamp that is less than 3 hours from now.

Test

Changelog entry

  • Automatically create a changelog entry from the details below.

Significance

  • Patch
  • Minor
  • Major

Type

  • Fix - Fixes an existing bug
  • Add - Adds functionality
  • Update - Update existing functionality
  • Dev - Development related task
  • Tweak - A minor adjustment to the codebase
  • Performance - Address performance issues
  • Enhancement - Improvement to existing functionality

Message

Comment

@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Feb 6, 2024
@chihsuan chihsuan changed the title Store default specs into transient when spec rules fails Store default specs into transient when spec rules fail Feb 6, 2024
@chihsuan chihsuan self-assigned this Feb 6, 2024
@chihsuan chihsuan force-pushed the update/store-default-specs-when-fails branch from b50ab27 to d824646 Compare February 6, 2024 08:00
Copy link
Contributor

github-actions bot commented Feb 6, 2024

Test Results Summary

Commit SHA: 0ae2595

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests25900202610m 36s
E2E Tests254006003146m 22s

To view the full API test report, click here.
To view the full E2E test report, click here.
To view all test reports, visit the WooCommerce Test Reports Dashboard.

@@ -133,7 +133,8 @@ public function read_specs_from_data_sources() {
$this->merge_specs( $specs_from_data_source, $specs, $url );
}

$specs_group = get_transient( $this->args['transient_name'] ) ?? array();
$specs_group = get_transient( $this->args['transient_name'] );
$specs_group = is_array( $specs_group ) ? $specs_group : array();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix PHP 8.1+ Automatic conversion of false to array is deprecated

@chihsuan chihsuan requested review from a team, adrianduffell and rjchow February 6, 2024 08:35
Copy link
Contributor

github-actions bot commented Feb 6, 2024

Hi @adrianduffell, @rjchow,

Apart from reviewing the code changes, please make sure to review the testing instructions as well.

You can follow this guide to find out what good testing instructions should look like:
https://github.com/woocommerce/woocommerce/wiki/Writing-high-quality-testing-instructions

Copy link
Contributor

@ilyasfoo ilyasfoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies can I change my mind, it seems like the solution is a bit weird since we return $suggestions in the first attempt, but after that we'll return the defaults for 3 hours 😅

Perhaps instead, could we do this instead?

  1. When $suggestions is empty, replace it with defaults and save for 3 hours
  2. When $suggestions is not empty but has errors, save it for 3 hours

plugins/woocommerce/src/Admin/DataSourcePoller.php Outdated Show resolved Hide resolved
}
}

if ( $has_error ) {
Copy link
Contributor

@moon0326 moon0326 Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about moving this code into the catch block? I don't think we need $has_error if it's only used once.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally wanted to do that too, but because it's a loop, we might end up calling set_specs_transient many times. 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my bad 🤦 I did not see the loop.

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>
@chihsuan
Copy link
Member Author

chihsuan commented Feb 7, 2024

Apologies can I change my mind, it seems like the solution is a bit weird since we return $suggestions in the first attempt, but after that we'll return the defaults for 3 hours 😅

Perhaps instead, could we do this instead?

  1. When $suggestions is empty, replace it with defaults and save for 3 hours
  2. When $suggestions is not empty but has errors, save it for 3 hours

Sure! I will update my PR.

@chihsuan chihsuan force-pushed the update/store-default-specs-when-fails branch from 9b220f7 to 9199e6a Compare February 7, 2024 09:12
@chihsuan chihsuan changed the title Store default specs into transient when spec rules fail Improved remote specifications transient handling and error management Feb 7, 2024
@chihsuan chihsuan changed the title Improved remote specifications transient handling and error management Improve remote specifications transient handling and error management Feb 7, 2024
@chihsuan
Copy link
Member Author

chihsuan commented Feb 7, 2024

@ilyasfoo I've made the changes. Could you please review this PR when you get a chance? Thanks. 🙏

I think we could create a parent Init class to provide some helpful methods and reduce duplicates, but I think that would be a follow-up.

@chihsuan chihsuan force-pushed the update/store-default-specs-when-fails branch from c5b8bee to b088329 Compare February 7, 2024 11:01
Copy link
Contributor

@ilyasfoo ilyasfoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, tested well!

Minor comments, pre-approving 🚢

$this->assertEquals( count( $stored_transients['en_US'] ), count( DefaultPaymentGateways::get_all() ) );

$expires = (int) get_transient( '_transient_timeout_woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs' );
$this->assertTrue( ( $expires - time() ) < 3 * HOUR_IN_SECONDS );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think changing it to <= might be safer to accommodate for supercomputers running unit tests 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in 0ae2595. Thanks! 😆

Copy link
Contributor

@rjchow rjchow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question and one minor nit - would it be possible to add a test case for the error flow?

@@ -183,7 +197,7 @@ protected static function read_data_source( $url ) {
// phpcs:ignore
$logger->error( print_r( $response, true ), $logger_context );

return [];
return array();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't quite figure out the intent of changing [] for array(), is it a lint issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's a lint issue.

Short array syntax is not allowedPHP_CodeSniffer(Generic.Arrays.DisallowShortArraySyntax.Found)

@chihsuan
Copy link
Member Author

chihsuan commented Feb 13, 2024

One question and one minor nit - would it be possible to add a test case for the error flow?

@rjchow I wanted to add unit tests for that too, but since it's a static method, I can't mock it to return errors. 😞 I've tried different approaches, but none of them seem to work.

There are also many discussions on Slack about this, but still couldn't find any solutions.

A related issue:
sebastianbergmann/phpunit-documentation#77

@chihsuan chihsuan requested a review from rjchow February 14, 2024 00:37
@chihsuan chihsuan merged commit a5711d0 into trunk Feb 15, 2024
39 checks passed
@chihsuan chihsuan deleted the update/store-default-specs-when-fails branch February 15, 2024 03:00
@github-actions github-actions bot added this to the 8.7.0 milestone Feb 15, 2024
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label Feb 15, 2024
@Stojdza Stojdza added needs: internal testing Indicates if the PR requires further testing conducted by Solaris status: analysis complete Indicates if a PR has been analysed by Solaris and removed needs: analysis Indicates if the PR requires a PR testing scrub session. labels Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: internal testing Indicates if the PR requires further testing conducted by Solaris plugin: woocommerce Issues related to the WooCommerce Core plugin. status: analysis complete Indicates if a PR has been analysed by Solaris
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Only store a transient when it’s successfully validated and reduce its stored period
5 participants