Add default values for Launch your store task (#45306)

* Add LYS task default values

* Add changelog

* Add unit tests

* Init LaunchYourStore task only when feature flag is enabled

* Add action to woocommerce class

* Remove WCAdminHelper use

* Add lys options to permission list

* Set launch-status option

* Fix woocommerce_store_pages_only

* Use WC_Install::is_new_install() instead

* Use woocommerce_newly_installed action

* Check if feature flag is enabled

* Use singleton WooCommerce object in tests

---------

Co-authored-by: Adrian Duffell <9312929+adrianduffell@users.noreply.github.com>
This commit is contained in:
Chi-Hsuan Huang 2024-03-08 17:27:46 +08:00 committed by GitHub
parent 913953f586
commit 10561c8c1a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: add
Add default option values for Launch your store task

View File

@ -24,6 +24,8 @@ use Automattic\WooCommerce\Internal\Utilities\WebhookUtil;
use Automattic\WooCommerce\Internal\Admin\Marketplace;
use Automattic\WooCommerce\Proxies\LegacyProxy;
use Automattic\WooCommerce\Utilities\{ LoggingUtil, TimeUtil };
use Automattic\WooCommerce\Admin\WCAdminHelper;
use Automattic\WooCommerce\Admin\Features\Features;
/**
* Main WooCommerce Class.
@ -253,6 +255,11 @@ final class WooCommerce {
add_action( 'woocommerce_installed', array( $this, 'add_woocommerce_remote_variant' ) );
add_action( 'woocommerce_updated', array( $this, 'add_woocommerce_remote_variant' ) );
if ( Features::is_enabled( 'launch-your-store' ) ) {
add_action( 'woocommerce_newly_installed', array( $this, 'add_lys_default_values' ) );
add_action( 'woocommerce_updated', array( $this, 'add_lys_default_values' ) );
}
// These classes set up hooks on instantiation.
$container = wc_get_container();
$container->get( ProductDownloadDirectories::class );
@ -307,6 +314,35 @@ final class WooCommerce {
}
}
/**
* Set default option values for launch your store task.
*/
public function add_lys_default_values() {
$is_new_install = current_action() === 'woocommerce_newly_installed';
$coming_soon = $is_new_install ? 'yes' : 'no';
$launch_status = $is_new_install ? 'unlaunched' : 'launched';
$store_pages_only = WCAdminHelper::is_site_fresh() ? 'no' : 'yes';
$private_link = 'yes';
$share_key = wp_generate_password( 32, false );
if ( false === get_option( 'woocommerce_coming_soon', false ) ) {
update_option( 'woocommerce_coming_soon', $coming_soon );
}
if ( false === get_option( 'woocommerce_store_pages_only', false ) ) {
update_option( 'woocommerce_store_pages_only', $store_pages_only );
}
if ( false === get_option( 'woocommerce_private_link', false ) ) {
update_option( 'woocommerce_private_link', $private_link );
}
if ( false === get_option( 'woocommerce_share_key', false ) ) {
update_option( 'woocommerce_share_key', $share_key );
}
if ( false === get_option( 'launch-status', false ) ) {
update_option( 'launch-status', $launch_status );
}
}
/**
* Ensures fatal errors are logged so they can be picked up in the status report.
*

View File

@ -220,6 +220,11 @@ class Options extends \WC_REST_Data_Controller {
'woocommerce_admin_customize_store_completed',
'woocommerce_admin_customize_store_completed_theme_id',
'woocommerce_admin_customize_store_survey_completed',
'woocommerce_coming_soon',
'woocommerce_store_pages_only',
'woocommerce_private_link',
'woocommerce_share_key',
'launch-status',
// WC Test helper options.
'wc-admin-test-helper-rest-api-filters',
'wc_admin_helper_feature_values',

View File

@ -4,6 +4,19 @@
* Unit tests for the WooCommerce class.
*/
class WooCommerce_Test extends \WC_Unit_Test_Case {
/**
* Setup test data. Called before every test.
*/
public function setUp(): void {
parent::setUp();
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
)
);
wp_set_current_user( $this->user );
}
/**
* Test that the $api property is defined, public and initialized correctly.
@ -14,4 +27,83 @@ class WooCommerce_Test extends \WC_Unit_Test_Case {
$this->assertTrue( $property->isPublic() );
$this->assertInstanceOf( WC_API::class, $property->getValue( WC() ) );
}
/**
* Tear down.
*/
public function tearDown(): void {
parent::tearDown();
delete_option( 'woocommerce_coming_soon' );
delete_option( 'woocommerce_store_pages_only' );
delete_option( 'woocommerce_private_link' );
delete_option( 'woocommerce_share_key' );
delete_option( 'launch-status' );
}
/**
* Test for add_lys_default_values method on fresh installation.
*/
public function test_add_lys_default_values_on_fresh_installation() {
update_option( 'fresh_site', '1' );
$this->set_current_action( 'woocommerce_newly_installed' );
( WooCommerce::instance() )->add_lys_default_values();
$this->assertEquals( 'yes', get_option( 'woocommerce_coming_soon' ) );
$this->assertEquals( 'no', get_option( 'woocommerce_store_pages_only' ) );
$this->assertEquals( 'yes', get_option( 'woocommerce_private_link' ) );
$this->assertNotEmpty( get_option( 'woocommerce_share_key' ) );
$this->assertMatchesRegularExpression( '/^[a-zA-Z0-9]{32}$/', get_option( 'woocommerce_share_key' ) );
$this->assertEquals( 'unlaunched', get_option( 'launch-status' ) );
}
/**
* Test for add_lys_default_values method on WooCommerce update.
*/
public function test_add_lys_default_values_on_woocommerce_update() {
update_option( 'fresh_site', '0' );
$this->set_current_action( 'woocommerce_updated' );
( WooCommerce::instance() )->add_lys_default_values();
$this->assertEquals( 'no', get_option( 'woocommerce_coming_soon' ) );
$this->assertEquals( 'yes', get_option( 'woocommerce_store_pages_only' ) );
$this->assertEquals( 'yes', get_option( 'woocommerce_private_link' ) );
$this->assertNotEmpty( get_option( 'woocommerce_share_key' ) );
$this->assertMatchesRegularExpression( '/^[a-zA-Z0-9]{32}$/', get_option( 'woocommerce_share_key' ) );
$this->assertEquals( 'launched', get_option( 'launch-status' ) );
}
/**
* Test for add_lys_default_values method when options are already set.
*
*/
public function test_add_lys_default_values_when_options_are_already_set() {
update_option( 'fresh_site', '0' );
update_option( 'woocommerce_coming_soon', 'yes' );
update_option( 'woocommerce_store_pages_only', 'no' );
update_option( 'woocommerce_private_link', 'yes' );
update_option( 'woocommerce_share_key', 'test' );
update_option( 'launch-status', 'unlaunched' );
$this->set_current_action( 'woocommerce_updated' );
( WooCommerce::instance() )->add_lys_default_values();
$this->assertEquals( 'yes', get_option( 'woocommerce_coming_soon' ) );
$this->assertEquals( 'no', get_option( 'woocommerce_store_pages_only' ) );
$this->assertEquals( 'yes', get_option( 'woocommerce_private_link' ) );
$this->assertEquals( 'test', get_option( 'woocommerce_share_key' ) );
$this->assertEquals( 'unlaunched', get_option( 'launch-status' ) );
}
/**
* Helper method to set the current action for testing.
*
* @param string $action The action to set.
*/
private function set_current_action( $action ) {
global $wp_current_filter;
$wp_current_filter[] = $action; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}
}