LYS - Delete WooPay test orders on launch (#47832)
* Add woopayments/test-orders-count endpoint * Load test order count * Call deleteTestOrders * Remove test code * Use wc_get_orders to retrive test orders * Add changefile(s) from automation for the following project(s): woocommerce * typecast deleteTestOrders * Type removeTestOrders input * Remove woopayment activation check -- do not need as we are using wc_get_orders * Add tests for test order count and delete endpoints * Delete test class * Add tests for test order count and delete endpoints * Lint fixes * Lint fixes * Lint fixes * Remove unused import * Fix timing issue with counting test orders * Add hasWooPayments guard --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: rjchow <me@rjchow.com>
This commit is contained in:
parent
3bf01a50d3
commit
04a5e67d88
|
@ -16,6 +16,7 @@ import { getQuery, navigateTo } from '@woocommerce/navigation';
|
|||
import { OPTIONS_STORE_NAME, TaskListType, TaskType } from '@woocommerce/data';
|
||||
import { dispatch } from '@wordpress/data';
|
||||
import { recordEvent } from '@woocommerce/tracks';
|
||||
import apiFetch from '@wordpress/api-fetch';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
|
@ -71,6 +72,31 @@ const launchStoreAction = async () => {
|
|||
throw new Error( JSON.stringify( results ) );
|
||||
};
|
||||
|
||||
const getTestOrderCount = async () => {
|
||||
const result = ( await apiFetch( {
|
||||
path: '/wc-admin/launch-your-store/woopayments/test-orders/count',
|
||||
method: 'GET',
|
||||
} ) ) as { count: number };
|
||||
|
||||
return result.count;
|
||||
};
|
||||
|
||||
const deleteTestOrders = async ( {
|
||||
input,
|
||||
}: {
|
||||
input: {
|
||||
removeTestOrders: boolean;
|
||||
};
|
||||
} ) => {
|
||||
if ( ! input.removeTestOrders ) {
|
||||
return null;
|
||||
}
|
||||
return await apiFetch( {
|
||||
path: '/wc-admin/launch-your-store/woopayments/test-orders',
|
||||
method: 'DELETE',
|
||||
} );
|
||||
};
|
||||
|
||||
const recordStoreLaunchAttempt = ( {
|
||||
context,
|
||||
}: {
|
||||
|
@ -172,11 +198,18 @@ export const sidebarMachine = setup( {
|
|||
const { sidebar } = getQuery() as { sidebar?: string };
|
||||
return !! sidebar && sidebar === sidebarLocation;
|
||||
},
|
||||
hasWooPayments: () => {
|
||||
return window?.wcSettings?.admin?.plugins?.activePlugins.includes(
|
||||
'woocommerce-payments'
|
||||
);
|
||||
},
|
||||
},
|
||||
actors: {
|
||||
sidebarQueryParamListener,
|
||||
getTasklist: fromPromise( getLysTasklist ),
|
||||
getTestOrderCount: fromPromise( getTestOrderCount ),
|
||||
updateLaunchStoreOptions: fromPromise( launchStoreAction ),
|
||||
deleteTestOrders: fromPromise( deleteTestOrders ),
|
||||
fetchCongratsData,
|
||||
},
|
||||
} ).createMachine( {
|
||||
|
@ -228,6 +261,31 @@ export const sidebarMachine = setup( {
|
|||
actions: assign( {
|
||||
tasklist: ( { event } ) => event.output,
|
||||
} ),
|
||||
target: 'maybeCountTestOrders',
|
||||
},
|
||||
},
|
||||
},
|
||||
maybeCountTestOrders: {
|
||||
always: [
|
||||
{
|
||||
guard: 'hasWooPayments',
|
||||
target: 'countTestOrders',
|
||||
},
|
||||
{
|
||||
target: 'launchYourStoreHub',
|
||||
},
|
||||
],
|
||||
},
|
||||
countTestOrders: {
|
||||
invoke: {
|
||||
src: 'getTestOrderCount',
|
||||
onDone: {
|
||||
actions: assign( {
|
||||
testOrderCount: ( { event } ) => event.output,
|
||||
} ),
|
||||
target: 'launchYourStoreHub',
|
||||
},
|
||||
onError: {
|
||||
target: 'launchYourStoreHub',
|
||||
},
|
||||
},
|
||||
|
@ -255,38 +313,52 @@ export const sidebarMachine = setup( {
|
|||
assign( { launchStoreError: undefined } ), // clear the errors if any from previously
|
||||
'recordStoreLaunchAttempt',
|
||||
],
|
||||
invoke: {
|
||||
src: 'updateLaunchStoreOptions',
|
||||
onDone: {
|
||||
target: '#storeLaunchSuccessful',
|
||||
actions: [
|
||||
{
|
||||
type: 'recordStoreLaunchResults',
|
||||
params: { success: true },
|
||||
},
|
||||
],
|
||||
},
|
||||
onError: {
|
||||
actions: [
|
||||
assign( {
|
||||
launchStoreError: ( { event } ) => {
|
||||
return {
|
||||
message: JSON.stringify(
|
||||
event.error
|
||||
), // for some reason event.error is an empty object, worth investigating if we decide to use the error message somewhere
|
||||
};
|
||||
invoke: [
|
||||
{
|
||||
src: 'updateLaunchStoreOptions',
|
||||
onDone: {
|
||||
target: '#storeLaunchSuccessful',
|
||||
actions: [
|
||||
{
|
||||
type: 'recordStoreLaunchResults',
|
||||
params: { success: true },
|
||||
},
|
||||
} ),
|
||||
{
|
||||
type: 'recordStoreLaunchResults',
|
||||
params: {
|
||||
success: false,
|
||||
],
|
||||
},
|
||||
onError: {
|
||||
actions: [
|
||||
assign( {
|
||||
launchStoreError: ( { event } ) => {
|
||||
return {
|
||||
message: JSON.stringify(
|
||||
event.error
|
||||
), // for some reason event.error is an empty object, worth investigating if we decide to use the error message somewhere
|
||||
};
|
||||
},
|
||||
} ),
|
||||
{
|
||||
type: 'recordStoreLaunchResults',
|
||||
params: {
|
||||
success: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
target: '#launchYourStoreHub',
|
||||
],
|
||||
target: '#launchYourStoreHub',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
src: 'deleteTestOrders',
|
||||
input: ( { event } ) => {
|
||||
return {
|
||||
removeTestOrders: (
|
||||
event as {
|
||||
removeTestOrders: boolean;
|
||||
}
|
||||
).removeTestOrders,
|
||||
};
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
|
@ -170,6 +170,10 @@
|
|||
}
|
||||
}
|
||||
|
||||
.edit-site-sidebar-navigation-screen-test-data__group-header {
|
||||
margin-top: 40px;
|
||||
}
|
||||
|
||||
.edit-site-sidebar-navigation-item {
|
||||
border-radius: 4px;
|
||||
padding: 8px 8px 8px 16px;
|
||||
|
|
|
@ -13,6 +13,10 @@ declare global {
|
|||
currentUserData: {
|
||||
first_name: string;
|
||||
};
|
||||
plugins: {
|
||||
activePlugins: string[];
|
||||
installedPlugins: string[];
|
||||
},
|
||||
wcpayWelcomePageIncentive: {
|
||||
id: string;
|
||||
description: string;
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
Significance: minor
|
||||
Type: add
|
||||
|
||||
LYS - Add 'Remove test orders' for WooPayments
|
|
@ -77,6 +77,30 @@ class LaunchYourStore {
|
|||
),
|
||||
)
|
||||
);
|
||||
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base . '/woopayments/test-orders/count',
|
||||
array(
|
||||
array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array( $this, 'get_woopay_test_orders_count' ),
|
||||
'permission_callback' => array( $this, 'must_be_shop_manager_or_admin' ),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base . '/woopayments/test-orders',
|
||||
array(
|
||||
array(
|
||||
'methods' => 'DELETE',
|
||||
'callback' => array( $this, 'delete_woopay_test_orders' ),
|
||||
'permission_callback' => array( $this, 'must_be_shop_manager_or_admin' ),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -126,6 +150,55 @@ class LaunchYourStore {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the test orders created during Woo Payments test mode.
|
||||
*
|
||||
* @return \WP_REST_Response
|
||||
*/
|
||||
public function get_woopay_test_orders_count() {
|
||||
$return = function ( $count ) {
|
||||
return new \WP_REST_Response( array( 'count' => $count ) );
|
||||
};
|
||||
|
||||
$orders = wc_get_orders(
|
||||
array(
|
||||
// phpcs:ignore
|
||||
'meta_key' => '_wcpay_mode',
|
||||
// phpcs:ignore
|
||||
'meta_value' => 'test',
|
||||
'return' => 'ids',
|
||||
)
|
||||
);
|
||||
|
||||
return $return( count( $orders ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete WooPayments test orders.
|
||||
*
|
||||
* @return \WP_REST_Response
|
||||
*/
|
||||
public function delete_woopay_test_orders() {
|
||||
$return = function ( $status = 204 ) {
|
||||
return new \WP_REST_Response( null, $status );
|
||||
};
|
||||
|
||||
$orders = wc_get_orders(
|
||||
array(
|
||||
// phpcs:ignore
|
||||
'meta_key' => '_wcpay_mode',
|
||||
// phpcs:ignore
|
||||
'meta_value' => 'test',
|
||||
)
|
||||
);
|
||||
|
||||
foreach ( $orders as $order ) {
|
||||
$order->delete();
|
||||
}
|
||||
|
||||
return $return();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update woocommerce_admin_launch_your_store_survey_completed to yes or no
|
||||
*
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
|
||||
namespace Automattic\WooCommerce\Tests\Admin\API;
|
||||
|
||||
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper;
|
||||
use WC_REST_Unit_Test_Case;
|
||||
use WP_REST_Request;
|
||||
|
||||
/**
|
||||
* LaunchYourStoreTest API controller test.
|
||||
*
|
||||
* @class LaunchYourStoreTest.
|
||||
*/
|
||||
class LaunchYourStoreTest extends WC_REST_Unit_Test_Case {
|
||||
/**
|
||||
* Endpoint.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const ENDPOINT = '/wc-admin/launch-your-store';
|
||||
|
||||
/**
|
||||
* Set up.
|
||||
*/
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
// Register an administrator user and log in.
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
wp_set_current_user( $this->user );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get test order count.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function get_order_count() {
|
||||
$request = new WP_REST_Request( 'GET', self::ENDPOINT . '/woopayments/test-orders/count' );
|
||||
return $this->server->dispatch( $request )->get_data()['count'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new test order
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function add_test_order() {
|
||||
$order = OrderHelper::create_order( $this->user );
|
||||
$order->update_meta_data( '_wcpay_mode', 'test' );
|
||||
$order->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test order count.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_it_returns_test_order_count() {
|
||||
$count = $this->get_order_count();
|
||||
$this->assertEquals( 0, $count );
|
||||
|
||||
$this->add_test_order();
|
||||
|
||||
$count = $this->get_order_count();
|
||||
$this->assertEquals( 1, $count );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test delete endpoint returns 204.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_delete_endpoint_returns_204() {
|
||||
$request = new WP_REST_Request( 'DELETE', self::ENDPOINT . '/woopayments/test-orders' );
|
||||
$status = $this->server->dispatch( $request )->get_status();
|
||||
$this->assertEquals( 204, $status );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test delete order endpoint.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_delete_endpoint_deletes_test_order() {
|
||||
$this->add_test_order();
|
||||
$request = new WP_REST_Request( 'DELETE', self::ENDPOINT . '/woopayments/test-orders' );
|
||||
$this->server->dispatch( $request );
|
||||
$this->assertEquals( 0, $this->get_order_count() );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue