Add error handling and tests for ShippingPartnerSuggestions (#44641)

* Add error handling and tests for ShippingPartnerSuggestions, add missed generic error handling for RemoteFreeExtension evaluator

* Changelog
This commit is contained in:
Ilyas Foo 2024-02-16 15:18:30 +08:00 committed by GitHub
parent 1d91b89d16
commit 0ea64cf93b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 155 additions and 19 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
Add error handling and tests for ShippingPartnerSuggestions

View File

@ -2,7 +2,7 @@
namespace Automattic\WooCommerce\Admin\Features\ShippingPartnerSuggestions;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\RuleEvaluator;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\EvaluateSuggestion;
/**
* Class ShippingPartnerSuggestions
@ -15,31 +15,31 @@ class ShippingPartnerSuggestions {
* @param array|null $specs shipping partner suggestion spec array.
* @return array
*/
public static function get_suggestions( $specs = null ) {
$suggestions = array();
if ( null === $specs ) {
$specs = self::get_specs_from_datasource();
public static function get_suggestions( array $specs = null ) {
$locale = get_user_locale();
$specs = is_array( $specs ) ? $specs : self::get_specs();
$results = EvaluateSuggestion::evaluate_specs( $specs );
// When suggestions is empty, replace it with defaults and save for 3 hours.
if ( empty( $results['suggestions'] ) ) {
ShippingPartnerSuggestionsDataSourcePoller::get_instance()->set_specs_transient( array( $locale => DefaultShippingPartners::get_all() ), 3 * HOUR_IN_SECONDS );
return EvaluateSuggestion::evaluate_specs( DefaultShippingPartners::get_all() )['suggestions'];
}
$rule_evaluator = new RuleEvaluator();
foreach ( $specs as &$spec ) {
$spec = is_array( $spec ) ? (object) $spec : $spec;
if ( isset( $spec->is_visible ) ) {
$is_visible = $rule_evaluator->evaluate( $spec->is_visible );
if ( $is_visible ) {
$spec->is_visible = true;
$suggestions[] = $spec;
}
}
// When suggestions is not empty but has errors, save it for 3 hours.
if ( count( $results['errors'] ) > 0 ) {
ShippingPartnerSuggestionsDataSourcePoller::get_instance()->set_specs_transient( array( $locale => $specs ), 3 * HOUR_IN_SECONDS );
}
return $suggestions;
return $results['suggestions'];
}
/**
* Get specs or fetch remotely if they don't exist.
*/
public static function get_specs_from_datasource() {
public static function get_specs() {
if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) {
/**
* It can be used to modify shipping partner suggestions spec.

View File

@ -25,8 +25,11 @@ class EvaluateExtension {
$rule_evaluator = new RuleEvaluator();
if ( isset( $extension->is_visible ) ) {
$is_visible = $rule_evaluator->evaluate( $extension->is_visible );
$extension->is_visible = $is_visible;
try {
$is_visible = $rule_evaluator->evaluate( $extension->is_visible );
$extension->is_visible = $is_visible;
} catch ( \Throwable $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
}
} else {
$extension->is_visible = true;
}

View File

@ -0,0 +1,129 @@
<?php
namespace Automattic\WooCommerce\Tests\Internal\Admin\ShippingPartnerSuggestions;
use Automattic\WooCommerce\Admin\DataSourcePoller;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\Shipping;
use Automattic\WooCommerce\Admin\Features\ShippingPartnerSuggestions\DefaultShippingPartners;
use Automattic\WooCommerce\Admin\Features\ShippingPartnerSuggestions\ShippingPartnerSuggestions;
use Automattic\WooCommerce\Admin\Features\ShippingPartnerSuggestions\ShippingPartnerSuggestionsDataSourcePoller;
use WC_Unit_Test_Case;
/**
* class WC_Admin_Tests_ShippingPartnerSuggestions_Init
*
* @covers \Automattic\WooCommerce\Admin\Features\ShippingPartnerSuggestions\ShippingPartnerSuggestions
*/
class ShippingPartnerSuggestionsTest extends WC_Unit_Test_Case {
/**
* Set up.
*/
public function setUp(): void {
parent::setUp();
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
)
);
delete_option( 'woocommerce_show_marketplace_suggestions' );
add_filter(
'transient_woocommerce_admin_' . ShippingPartnerSuggestionsDataSourcePoller::ID . '_specs',
function( $value ) {
if ( $value ) {
return $value;
}
$locale = get_user_locale();
return array(
$locale => array(
(object) array(
'id' => 'mock-shipping-partner-1',
'is_visible' => (object) array(
'type' => 'base_location_country',
'value' => 'ZA',
'operation' => '=',
),
),
(object) array(
'id' => 'mock-shipping-partner-2',
'is_visible' => (object) array(
'type' => 'base_location_country',
'value' => 'US',
'operation' => '=',
),
),
),
);
}
);
}
/**
* Tear down.
*/
public function tearDown(): void {
parent::tearDown();
ShippingPartnerSuggestionsDataSourcePoller::get_instance()->delete_specs_transient();
remove_all_filters( 'transient_woocommerce_admin_' . ShippingPartnerSuggestionsDataSourcePoller::ID . '_specs' );
update_option( 'woocommerce_default_country', 'US' );
}
/**
* Test that default specs are provided when remote sources don't exist.
*/
public function test_get_default_specs() {
remove_all_filters( 'transient_woocommerce_admin_' . ShippingPartnerSuggestionsDataSourcePoller::ID . '_specs' );
add_filter(
DataSourcePoller::FILTER_NAME,
function() {
return array();
}
);
$specs = ShippingPartnerSuggestions::get_specs();
$defaults = DefaultShippingPartners::get_all();
remove_all_filters( DataSourcePoller::FILTER_NAME );
$this->assertEquals( $defaults, $specs );
}
/**
* Test that specs are read from cache when they exist.
*/
public function test_specs_transient() {
set_transient(
'woocommerce_admin_' . ShippingPartnerSuggestionsDataSourcePoller::ID . '_specs',
array(
'en_US' => array(
array(
'id' => 'mock1',
),
array(
'id' => 'mock2',
),
),
)
);
$specs = ShippingPartnerSuggestions::get_specs();
$this->assertCount( 2, $specs );
}
/**
* Test that non-matching suggestions are not shown.
*/
public function test_non_matching_suggestions() {
update_option( 'woocommerce_default_country', 'US' );
$suggestions = ShippingPartnerSuggestions::get_suggestions();
$this->assertCount( 1, $suggestions );
}
/**
* Test that matched suggestions are shown.
*/
public function test_matching_suggestions() {
update_option( 'woocommerce_default_country', 'ZA' );
$suggestions = ShippingPartnerSuggestions::get_suggestions();
$this->assertEquals( 'mock-shipping-partner-1', $suggestions[0]->id );
}
}