fix SQL error in category sort (https://github.com/woocommerce/woocommerce-admin/pull/3521)
* fix SQL error in category sort Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com>
This commit is contained in:
parent
ff6eeadbef
commit
d192e55dda
|
@ -133,7 +133,11 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
|
|||
if ( false !== strpos( $order_by_clause, '_terms' ) ) {
|
||||
$join = "JOIN {$wpdb->terms} AS _terms ON {$id_cell} = _terms.term_id";
|
||||
if ( 'inner' === $from_arg ) {
|
||||
$this->subquery->add_sql_clause( 'join', $join );
|
||||
// Even though this is an (inner) JOIN, we're adding it as a `left_join` to
|
||||
// affect its order in the query statement. The SqlQuery::$sql_filters variable
|
||||
// determines the order in which joins are concatenated.
|
||||
// See: https://github.com/woocommerce/woocommerce-admin/blob/1f261998e7287b77bc13c3d4ee2e84b717da7957/src/API/Reports/SqlQuery.php#L46-L50.
|
||||
$this->subquery->add_sql_clause( 'left_join', $join );
|
||||
} else {
|
||||
$this->add_sql_clause( 'join', $join );
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
use Automattic\WooCommerce\Admin\CategoryLookup;
|
||||
|
||||
/**
|
||||
* Class WC_Tests_API_Reports_Categories
|
||||
*/
|
||||
|
@ -147,6 +149,87 @@ class WC_Tests_API_Reports_Categories extends WC_REST_Unit_Test_Case {
|
|||
$this->assertArrayHasKey( 'category', $category_report['_links'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports with sorting.
|
||||
*
|
||||
* @since 3.9.0
|
||||
*/
|
||||
public function test_get_reports_categories_sort() {
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Populate all of the data.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$customer = WC_Helper_Customer::create_customer( 'sortcustomer', 'wcadminuser2', 'sortcustomer@woo.local' );
|
||||
$order = WC_Helper_Order::create_order( $customer->get_id(), $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 ); // $25 x 4.
|
||||
$order->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
// Populate all of the data.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product 2' );
|
||||
$product->set_regular_price( 100 );
|
||||
$second_category = wp_insert_term( 'Second Category', 'product_cat' );
|
||||
$second_category_id = $second_category['term_id'];
|
||||
$product->set_category_ids( array( $second_category_id ) );
|
||||
$product->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( $customer->get_id(), $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 400 ); // $100 x 4.
|
||||
$order->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
CategoryLookup::instance()->regenerate();
|
||||
|
||||
$uncategorized_term = get_term_by( 'slug', 'uncategorized', 'product_cat' );
|
||||
$params = array(
|
||||
'orderby' => 'category',
|
||||
'order' => 'desc',
|
||||
'interval' => 'week',
|
||||
'extended_info' => true,
|
||||
);
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params( $params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $reports ) );
|
||||
|
||||
$category_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( $uncategorized_term->term_id, $category_report['category_id'] );
|
||||
|
||||
$category_report = next( $reports );
|
||||
|
||||
$this->assertEquals( $second_category_id, $category_report['category_id'] );
|
||||
|
||||
$params['order'] = 'asc';
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params( $params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$category_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( $second_category_id, $category_report['category_id'] );
|
||||
|
||||
$category_report = next( $reports );
|
||||
|
||||
$this->assertEquals( $uncategorized_term->term_id, $category_report['category_id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue