Don't recommend WCS&T on shipping settings page if WCShip or WCTax is active (#48701)
This commit is contained in:
parent
f0b9a9147f
commit
f257baa4e5
|
@ -49,6 +49,13 @@ const ShippingRecommendations: React.FC = () => {
|
|||
};
|
||||
} );
|
||||
|
||||
if (
|
||||
activePlugins.includes( 'woocommerce-shipping' ) ||
|
||||
activePlugins.includes( 'woocommerce-tax' )
|
||||
) {
|
||||
return <ShippingTour showShippingRecommendationsStep={ false } />;
|
||||
}
|
||||
|
||||
if (
|
||||
activePlugins.includes( 'woocommerce-services' ) &&
|
||||
isJetpackConnected
|
||||
|
|
|
@ -103,7 +103,11 @@ const ShippingRecommendations: React.FC = () => {
|
|||
select( PLUGINS_STORE_NAME ).getActivePlugins()
|
||||
);
|
||||
|
||||
if ( activePlugins.includes( 'woocommerce-services' ) ) {
|
||||
if (
|
||||
activePlugins.includes( 'woocommerce-services' ) ||
|
||||
activePlugins.includes( 'woocommerce-shipping' ) ||
|
||||
activePlugins.includes( 'woocommerce-tax' )
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -67,6 +67,29 @@ describe( 'ShippingRecommendations', () => {
|
|||
).not.toBeInTheDocument();
|
||||
} );
|
||||
|
||||
[
|
||||
[ 'woocommerce-shipping' ],
|
||||
[ 'woocommerce-tax' ],
|
||||
[ 'woocommerce-shipping', 'woocommerce-tax' ],
|
||||
].forEach( ( activePlugins ) => {
|
||||
it( `should not render if the following plugins are active: ${ JSON.stringify(
|
||||
activePlugins
|
||||
) }`, () => {
|
||||
( useSelect as jest.Mock ).mockImplementation( ( fn ) =>
|
||||
fn( () => ( {
|
||||
...defaultSelectReturn,
|
||||
getActivePlugins: () => activePlugins,
|
||||
} ) )
|
||||
);
|
||||
|
||||
render( <ShippingRecommendations /> );
|
||||
|
||||
expect(
|
||||
screen.queryByText( 'WooCommerce Shipping' )
|
||||
).not.toBeInTheDocument();
|
||||
} );
|
||||
} );
|
||||
|
||||
it( 'should not render when store location is not US', () => {
|
||||
( useSelect as jest.Mock ).mockImplementation( ( fn ) =>
|
||||
fn( () => ( {
|
||||
|
|
|
@ -50,6 +50,32 @@ describe( 'ShippingRecommendations', () => {
|
|||
).not.toBeInTheDocument();
|
||||
} );
|
||||
|
||||
it( 'should not render when the WooCommerce Shipping plugin is active', () => {
|
||||
useSelect.mockImplementation( ( fn ) =>
|
||||
fn( () => ( {
|
||||
getActivePlugins: () => [ 'woocommerce-shipping' ],
|
||||
} ) )
|
||||
);
|
||||
render( <ShippingRecommendations /> );
|
||||
|
||||
expect(
|
||||
screen.queryByText( 'WooCommerce Shipping' )
|
||||
).not.toBeInTheDocument();
|
||||
} );
|
||||
|
||||
it( 'should not render when the WooCommerce Tax plugin is active', () => {
|
||||
useSelect.mockImplementation( ( fn ) =>
|
||||
fn( () => ( {
|
||||
getActivePlugins: () => [ 'woocommerce-shipping' ],
|
||||
} ) )
|
||||
);
|
||||
render( <ShippingRecommendations /> );
|
||||
|
||||
expect(
|
||||
screen.queryByText( 'WooCommerce Shipping' )
|
||||
).not.toBeInTheDocument();
|
||||
} );
|
||||
|
||||
it( 'should render WCS when not installed', () => {
|
||||
render( <ShippingRecommendations /> );
|
||||
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
Significance: patch
|
||||
Type: tweak
|
||||
Comment: If either the upcoming WooCommerce Shipping or WooCommerce Tax extension is active, don't suggest WooCommerce Shipping & Tax on the WC > Settings > Shipping page. This only adjusts the extension recommendation merchants receive and does not affect any other functionality.
|
||||
|
|
@ -62,7 +62,9 @@ class ExperimentalShippingRecommendation extends Task {
|
|||
* @return bool
|
||||
*/
|
||||
public function can_view() {
|
||||
return Features::is_enabled( 'shipping-smart-defaults' );
|
||||
return Features::is_enabled( 'shipping-smart-defaults' ) &&
|
||||
! PluginsHelper::is_plugin_active( 'woocommerce-shipping' ) &&
|
||||
! PluginsHelper::is_plugin_active( 'woocommerce-tax' );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,173 @@
|
|||
<?php
|
||||
/**
|
||||
* Test the API controller class that handles the marketing campaigns REST response.
|
||||
*
|
||||
* @package WooCommerce\Admin\Tests\Admin\Features\OnboardingTasks\Tasks
|
||||
*/
|
||||
|
||||
namespace Automattic\WooCommerce\Tests\Admin\Features\OnboardingTasks\Tasks;
|
||||
|
||||
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\ExperimentalShippingRecommendation;
|
||||
use WC_Unit_Test_Case;
|
||||
|
||||
/**
|
||||
* ExperimentalShippingRecommendation test.
|
||||
*
|
||||
* @class ExperimentalShippingRecommendationTest.
|
||||
*/
|
||||
class ExperimentalShippingRecommendationTest extends WC_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
const HOOK_PRIORITY = 9999;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $enabled_admin_features_mock;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $active_plugin_basenames_mock;
|
||||
|
||||
/**
|
||||
* Set things up before each test case.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
add_filter( 'woocommerce_admin_features', array( $this, 'get_mocked_admin_features' ), self::HOOK_PRIORITY );
|
||||
add_filter( 'pre_option_active_plugins', array( $this, 'get_mocked_active_plugins' ), self::HOOK_PRIORITY );
|
||||
$this->enabled_admin_features_mock = array();
|
||||
$this->active_plugin_basenames_mock = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up after each test case.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function tearDown(): void {
|
||||
parent::tearDown();
|
||||
|
||||
remove_filter( 'woocommerce_admin_features', array( $this, 'get_mocked_admin_features' ), self::HOOK_PRIORITY );
|
||||
remove_filter( 'pre_option_active_plugins', array( $this, 'get_mocked_active_plugins' ), self::HOOK_PRIORITY );
|
||||
wp_cache_delete( 'plugins', 'plugins' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the "happy path".
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_can_view_returns_true() {
|
||||
$this->enabled_admin_features_mock = array( 'shipping-smart-defaults' );
|
||||
$this->mock_active_plugin_basenames( array() );
|
||||
|
||||
$this->assertTrue( ( new ExperimentalShippingRecommendation() )->can_view() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the task is unavailable if smart shipping defaults are disabled.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_can_view_returns_false_if_smart_defaults_are_disabled() {
|
||||
$this->enabled_admin_features_mock = array();
|
||||
$this->mock_active_plugin_basenames( array() );
|
||||
|
||||
$this->assertFalse( ( new ExperimentalShippingRecommendation() )->can_view() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that if the WooCommerce Shipping extension is enabled, the task is unavailable.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_can_view_returns_false_if_woocommerce_shipping_is_active() {
|
||||
$this->enabled_admin_features_mock = array( 'shipping-smart-defaults' );
|
||||
$this->mock_active_plugin_basenames(
|
||||
array(
|
||||
'woocommerce-shipping/woocommerce-shipping.php',
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertFalse( ( new ExperimentalShippingRecommendation() )->can_view() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that if the WooCommerce Tax extension is enabled, the task is unavailable.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_can_view_returns_false_if_woocommerce_tax_is_active() {
|
||||
$this->enabled_admin_features_mock = array( 'shipping-smart-defaults' );
|
||||
$this->mock_active_plugin_basenames(
|
||||
array(
|
||||
'woocommerce-tax/woocommerce-tax.php',
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertFalse( ( new ExperimentalShippingRecommendation() )->can_view() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that if the WooCommerce Shipping and WooCommerce Tax extensions are enabled, the task is unavailable.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_can_view_returns_false_if_multiple_conflicting_plugins_are_active() {
|
||||
$this->enabled_admin_features_mock = array( 'shipping-smart-defaults' );
|
||||
$this->mock_active_plugin_basenames(
|
||||
array(
|
||||
'woocommerce-shipping/woocommerce-shipping.php',
|
||||
'woocommerce-tax/woocommerce-tax.php',
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertFalse( ( new ExperimentalShippingRecommendation() )->can_view() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Mocks the list of basenames of active plugins.
|
||||
*
|
||||
* A basename is a reference to the plugin formatted as "foo/foo.php".
|
||||
*
|
||||
* @param string[] $plugin_basenames Array of plugin basenames, e.g. [ "foo/foo.php", "bar/bar.php" ].
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function mock_active_plugin_basenames( $plugin_basenames ) {
|
||||
// Overwrite `get_option()` results.
|
||||
$this->active_plugin_basenames_mock = $plugin_basenames;
|
||||
|
||||
// Overwrite `get_plugins()` results (used by a nested function of PluginsHelper::is_plugin_active()`).
|
||||
wp_cache_set( 'plugins', array( '' => array_flip( $plugin_basenames ) ), 'plugins' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a mocked list of enabled WC Admin features.
|
||||
*
|
||||
* @see \wc_admin_get_feature_config
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function get_mocked_admin_features() {
|
||||
return $this->enabled_admin_features_mock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a mocked list of active plugin basenames.
|
||||
*
|
||||
* @see self::mock_active_plugin_basenames
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function get_mocked_active_plugins() {
|
||||
return $this->active_plugin_basenames_mock;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue