2019-01-31 20:06:40 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* REST API Init Class Test
|
|
|
|
*
|
2020-08-11 19:18:47 +00:00
|
|
|
* @package WooCommerce\Admin\Tests\API
|
2019-01-31 20:06:40 +00:00
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
|
2020-01-02 16:00:37 +00:00
|
|
|
use Automattic\WooCommerce\Admin\Schedulers\OrdersScheduler;
|
2019-08-05 18:14:25 +00:00
|
|
|
use \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore;
|
2019-08-01 22:06:00 +00:00
|
|
|
|
2019-01-31 20:06:40 +00:00
|
|
|
/**
|
|
|
|
* Class WC_Tests_API_Init
|
|
|
|
*/
|
|
|
|
class WC_Tests_API_Init extends WC_REST_Unit_Test_Case {
|
|
|
|
/**
|
|
|
|
* Set up.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
$this->queue = new WC_Admin_Test_Action_Queue();
|
2020-01-02 16:00:37 +00:00
|
|
|
OrdersScheduler::set_queue( $this->queue );
|
2019-01-31 20:06:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tear down.
|
|
|
|
*/
|
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
2020-01-02 16:00:37 +00:00
|
|
|
OrdersScheduler::set_queue( null );
|
2019-01-31 20:06:40 +00:00
|
|
|
$this->queue->actions = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cause a failure when updating order stats for the test order, without deleting it.
|
|
|
|
*
|
|
|
|
* @param string $query Query.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function filter_order_query( $query ) {
|
2019-02-07 21:48:16 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
2019-01-31 20:06:40 +00:00
|
|
|
if (
|
|
|
|
0 === strpos( $query, 'REPLACE INTO' ) &&
|
Data store refactor (https://github.com/woocommerce/woocommerce-admin/pull/2961)
* Base Report data store utility functions - 1
Standardizing the data store classes will be easier if the base class contains simple utility functions
that can replace logic implemented in multiple data stores.
- set_db_table_name() assigns a WP DB table name class variable for the data store
- get_db_table_name() retrieves the WP DB table name
- prepend_table_name() prepends a field in a query fragment with the data store table name
* add context, clause handling, and filters to reports data store
* add generated SQL clauses to class properties
* reduce id getter logic to single function with filter, add phpdocs to new filters
* update table_name to private string for use in constructor
* extract SQL query clause handling to its own class
- Will allow for use in subquery processing without creating a get_data stub
- Swap parameter order in add_sql_clause for readability
- Add support for clearing multple clauses in one call
* add context var to SqlQuery class
* implement SqlQuery in Categories data store
* implement subquery in categories data store
* coupons data stores, more underlying refactor
- fix warnings
- make filtered id functions static
- add limit parameter handling
- update coupons data store
- update coupon stats data store
* refactor coupon stats data store
* refactor customers and customer stats data stores
* add context to subqueries
* add missed prepend table name call
* refactor downloads data store, fix some warnings
* fix warnings, add separator parameter to filtered IDs
* refactor taxes and tax stats data stores
* refactor variations data store
* refactor product and product stats data stores
* make table_name static throughout for compat with static hook functions
* refactor order and order stats datastores
- use consistent visibility on initialize_queries()
- update db_table_name logic to use static keyword instead of self
* fix missed whitespace
* fix segmenting query, add SqlQuery join clause
* DRY data store constructors, class properties
* prefix table name when not yet assigned
* fix unit tests, interpolations, WPDB delete calls
* DRY get_object_where_filter()
* remove redundant table prefix from unit test init
* fix refactored SQL queries
* restore product paging
* remove unused query param arrays
* add first pass on data docs readme
* remove debug code, errant SQL spacing
* refactor out outer_from query element
* merge wheres, joins before filtering
* move all report column definitions to assign_report_columns
* fix data readme markdown
* small code formating fixes from review
* remove static from query/datastore context
* missed self:: in previous, add comments, small code moves
* rename get_statement() to get_query_statement()
* remove temporary query references
* static reference, remove reference parameter, fix coupon compare
* add todo reminders
* use correct query parameter in coupon data stores
2019-11-07 17:28:37 +00:00
|
|
|
false !== strpos( $query, OrdersStatsDataStore::get_db_table_name() )
|
2019-01-31 20:06:40 +00:00
|
|
|
) {
|
|
|
|
remove_filter( 'query', array( $this, 'filter_order_query' ) );
|
2019-02-07 21:48:16 +00:00
|
|
|
return "DESCRIBE $wpdb->posts"; // Execute any random query.
|
2019-01-31 20:06:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a retry job is scheduled for a failed sync.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_order_sync_retries_on_failure() {
|
|
|
|
// Create a test Order.
|
|
|
|
$product = new WC_Product_Simple();
|
|
|
|
$product->set_name( 'Test Product' );
|
|
|
|
$product->set_regular_price( 25 );
|
|
|
|
$product->save();
|
|
|
|
|
|
|
|
$order = WC_Helper_Order::create_order( 1, $product );
|
|
|
|
$order->set_status( 'completed' );
|
|
|
|
$order->set_total( 100 ); // $25 x 4.
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
// Clear the existing action queue (the above save adds an action).
|
|
|
|
$this->queue->actions = array();
|
|
|
|
|
|
|
|
// Force a failure by sabotaging the query run after retreiving order coupons.
|
|
|
|
add_filter( 'query', array( $this, 'filter_order_query' ) );
|
|
|
|
|
|
|
|
// Initiate sync.
|
2020-01-02 16:00:37 +00:00
|
|
|
OrdersScheduler::schedule_action( 'import', array( $order->get_id() ) );
|
2019-01-31 20:06:40 +00:00
|
|
|
|
|
|
|
// Verify that a retry job was scheduled.
|
|
|
|
$this->assertCount( 1, $this->queue->actions );
|
|
|
|
$this->assertArraySubset(
|
|
|
|
array(
|
2020-01-02 16:00:37 +00:00
|
|
|
'hook' => OrdersScheduler::get_action( 'import' ),
|
2019-01-31 20:06:40 +00:00
|
|
|
'args' => array( $order->get_id() ),
|
|
|
|
),
|
|
|
|
$this->queue->actions[0]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|