diff --git a/plugins/woocommerce-admin/tests/api-init.php b/plugins/woocommerce-admin/tests/api-init.php new file mode 100644 index 00000000000..5e196af6961 --- /dev/null +++ b/plugins/woocommerce-admin/tests/api-init.php @@ -0,0 +1,85 @@ +queue = new WC_Admin_Test_Action_Queue(); + WC_Admin_Api_Init::set_queue( $this->queue ); + } + + /** + * Tear down. + */ + public function tearDown() { + parent::tearDown(); + WC_Admin_Api_Init::set_queue( null ); + $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 ) { + if ( + 0 === strpos( $query, 'REPLACE INTO' ) && + false !== strpos( $query, WC_Admin_Reports_Orders_Stats_Data_Store::TABLE_NAME ) + ) { + remove_filter( 'query', array( $this, 'filter_order_query' ) ); + return 'THIS WONT MATCH'; + } + + 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. + WC_Admin_Api_Init::orders_lookup_process_order( $order->get_id() ); + + // Verify that a retry job was scheduled. + $this->assertCount( 1, $this->queue->actions ); + $this->assertArraySubset( + array( + 'hook' => WC_Admin_Api_Init::SINGLE_ORDER_ACTION, + 'args' => array( $order->get_id() ), + ), + $this->queue->actions[0] + ); + } +}