-
Notifications
You must be signed in to change notification settings - Fork 10.7k
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
Changes from 11 commits
9dcb641
13a60b3
d824646
03cc602
96e27d3
a36e36d
bc61c9c
1cb0457
142f8d2
bfdf2ae
b088329
e794793
0ae2595
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: update | ||
|
||
Improve remote specifications transient handling and error management |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,12 +133,12 @@ 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(); | ||
$locale = get_user_locale(); | ||
$specs_group[ $locale ] = $specs; | ||
// Persist the specs as a transient. | ||
set_transient( | ||
$this->args['transient_name'], | ||
$this->set_specs_transient( | ||
$specs_group, | ||
$this->args['transient_expiry'] | ||
); | ||
|
@@ -154,6 +154,20 @@ public function delete_specs_transient() { | |
return delete_transient( $this->args['transient_name'] ); | ||
} | ||
|
||
/** | ||
* Set the specs transient. | ||
* | ||
* @param array $specs The specs to set in the transient. | ||
* @param int $expiration The expiration time for the transient. | ||
*/ | ||
public function set_specs_transient( $specs, $expiration = 0 ) { | ||
chihsuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
set_transient( | ||
$this->args['transient_name'], | ||
$specs, | ||
$expiration, | ||
); | ||
} | ||
|
||
/** | ||
* Read a single data source and return the read specs | ||
* | ||
|
@@ -183,7 +197,7 @@ protected static function read_data_source( $url ) { | |
// phpcs:ignore | ||
$logger->error( print_r( $response, true ), $logger_context ); | ||
|
||
return []; | ||
return array(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's a lint issue.
|
||
} | ||
|
||
$body = $response['body']; | ||
|
@@ -195,7 +209,7 @@ protected static function read_data_source( $url ) { | |
$logger_context | ||
); | ||
|
||
return []; | ||
return array(); | ||
} | ||
|
||
if ( ! is_array( $specs ) ) { | ||
|
@@ -204,7 +218,7 @@ protected static function read_data_source( $url ) { | |
$logger_context | ||
); | ||
|
||
return []; | ||
return array(); | ||
} | ||
|
||
return $specs; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\Init as PaymentGatewaySuggestions; | ||
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\DefaultPaymentGateways; | ||
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\PaymentGatewaySuggestionsDataSourcePoller; | ||
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\PassRuleProcessor; | ||
|
||
/** | ||
* class WC_Admin_Tests_PaymentGatewaySuggestions_Init | ||
|
@@ -59,13 +60,21 @@ public function get_mock_specs() { | |
return array( | ||
'en_US' => array( | ||
array( | ||
'id' => 'mock-gateway', | ||
'id' => 'mock-gateway-1', | ||
'is_visible' => (object) array( | ||
'type' => 'base_location_country', | ||
'value' => 'ZA', | ||
'operation' => '=', | ||
), | ||
), | ||
array( | ||
'id' => 'mock-gateway-2', | ||
'is_visible' => (object) array( | ||
'type' => 'base_location_country', | ||
'value' => 'US', | ||
'operation' => '=', | ||
), | ||
), | ||
), | ||
); | ||
} | ||
|
@@ -111,27 +120,15 @@ public function test_specs_transient() { | |
/** | ||
* Test that non-matched suggestions are not shown. | ||
*/ | ||
public function test_non_matching_suggestions() { | ||
update_option( 'woocommerce_default_country', 'US' ); | ||
set_transient( | ||
'woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs', | ||
$this->get_mock_specs() | ||
); | ||
$suggestions = PaymentGatewaySuggestions::get_suggestions(); | ||
$this->assertCount( 0, $suggestions ); | ||
} | ||
|
||
/** | ||
* Test that matched suggestions are shown. | ||
*/ | ||
public function test_matching_suggestions() { | ||
update_option( 'woocommerce_default_country', 'ZA' ); | ||
update_option( 'woocommerce_default_country', 'US' ); | ||
set_transient( | ||
'woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs', | ||
$this->get_mock_specs() | ||
); | ||
$suggestions = PaymentGatewaySuggestions::get_suggestions(); | ||
$this->assertEquals( 'mock-gateway', $suggestions[0]->id ); | ||
$this->assertCount( 1, $suggestions ); | ||
$this->assertEquals( 'mock-gateway-2', $suggestions[0]->id ); | ||
} | ||
|
||
/** | ||
|
@@ -165,6 +162,27 @@ function( $_locale ) { | |
$this->assertEquals( 'default-gateway', $suggestions[0]->id ); | ||
} | ||
|
||
/** | ||
* Test that empty suggestions are replaced with defaults. | ||
*/ | ||
public function test_empty_suggestions() { | ||
set_transient( | ||
'woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs', | ||
array( | ||
'en_US' => array(), | ||
) | ||
); | ||
|
||
$suggestions = PaymentGatewaySuggestions::get_suggestions(); | ||
$stored_transients = get_transient( 'woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs' ); | ||
|
||
$this->assertEquals( 'bacs', $suggestions[0]->id ); | ||
$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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think changing it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed in 0ae2595. Thanks! 😆 |
||
} | ||
|
||
/** | ||
* Test that the suggestions can be displayed when a user has marketplace | ||
* suggestions enabled and is a user capable of installing plugins. | ||
|
There was a problem hiding this comment.
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