Merge branch 'fix/36685' into fix/36684

This commit is contained in:
Vedanshu Jain 2023-02-27 16:29:26 +05:30
commit b286c50ebd
6 changed files with 73 additions and 36 deletions

View File

@ -14,12 +14,14 @@ permissions: {}
jobs:
test:
name: PHP ${{ matrix.php }} WP ${{ matrix.wp }}
name: PHP ${{ matrix.php }} WP ${{ matrix.wp }} ${{ matrix.hpos && 'HPOS' || '' }}
timeout-minutes: 30
runs-on: ubuntu-20.04
permissions:
contents: read
continue-on-error: ${{ matrix.wp == 'nightly' }}
env:
HPOS: ${{ matrix.hpos }}
strategy:
fail-fast: false
matrix:
@ -32,6 +34,9 @@ jobs:
php: 7.4
- wp: '5.9'
php: 7.4
- wp: 'latest'
php: '7.4'
hpos: true
services:
database:
image: mysql:5.6

View File

@ -1,4 +1,4 @@
Significance: patch
Type: fix
Directly fetch order prop from DB for compatibility with CPT store.
Fetch order first to refresh cache before returning prop.

View File

@ -573,7 +573,8 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
*/
public function get_download_permissions_granted( $order ) {
$order_id = is_int( $order ) ? $order : $order->get_id();
return $this->get_field_value( $order_id, 'download_permission_granted', self::get_operational_data_table_name() );
$order = wc_get_order( $order_id );
return $order->get_download_permissions_granted();
}
/**
@ -599,7 +600,8 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
*/
public function get_recorded_sales( $order ) {
$order_id = is_int( $order ) ? $order : $order->get_id();
return $this->get_field_value( $order_id, 'recorded_sales', self::get_operational_data_table_name() );
$order = wc_get_order( $order_id );
return $order->get_recorded_sales();
}
/**
@ -625,7 +627,8 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
*/
public function get_recorded_coupon_usage_counts( $order ) {
$order_id = is_int( $order ) ? $order : $order->get_id();
return $this->get_field_value( $order_id, 'coupon_usages_are_counted', self::get_operational_data_table_name() );
$order = wc_get_order( $order_id );
return $order->get_recorded_coupon_usage_counts();
}
/**
@ -651,7 +654,8 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
*/
public function get_email_sent( $order ) {
$order_id = is_int( $order ) ? $order : $order->get_id();
return $this->get_field_value( $order_id, 'new_order_email_sent', self::get_operational_data_table_name() );
$order = wc_get_order( $order_id );
return $order->get_new_order_email_sent();
}
/**
@ -702,7 +706,8 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
*/
public function get_stock_reduced( $order ) {
$order_id = is_int( $order ) ? $order : $order->get_id();
return $this->get_field_value( $order_id, 'order_stock_reduced', self::get_operational_data_table_name() );
$order = wc_get_order( $order_id );
return $order->get_order_stock_reduced();
}
/**
@ -899,31 +904,6 @@ WHERE
// phpcs:enable
}
/**
* Returns field value for an order directly from the database, skipping the value stored in order prop.
* Useful when you are not sure if the order prop is manipulated by a callee function without having to refresh the order.
*
* @param int $order_id Order ID.
* @param string $column_name Name of the column to fetch value from.
* @param string $table_name Name of the table to fetch value from.
*
* @return string|null Field value or null if not found.
*/
public function get_field_value( $order_id, $column_name, $table_name ) {
global $wpdb;
$where_clause = ' WHERE 1=1 ';
if ( self::get_addresses_table_name() === $table_name ) {
$address_type = explode( '_', $column_name )[0];
$column_name = str_replace( $address_type . '_', '', $column_name );
$where_clause .= $wpdb->prepare( ' AND type = %s', $address_type );
}
$select_clause = 'SELECT `' . esc_sql( $column_name ) . '` FROM `' . esc_sql( $table_name ) . '` ';
$where_clause .= $wpdb->prepare( ' AND order_id = %d', $order_id );
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Both select_clause and where_clause are escaped.
return $wpdb->get_var( "$select_clause $where_clause LIMIT 1" );
}
/**
* Search order data for a term and return matching order IDs.
*

View File

@ -89,7 +89,9 @@ class WC_Unit_Tests_Bootstrap {
// re-initialize dependency injection, this needs to be the last operation after everything else is in place.
$this->initialize_dependency_injection();
if ( getenv( 'HPOS' ) ) {
$this->initialize_hpos();
}
error_reporting(error_reporting() & ~E_DEPRECATED);
}
@ -142,6 +144,11 @@ class WC_Unit_Tests_Bootstrap {
CodeHacker::enable();
}
/**
* Initialize HPOS if tests need to run in HPOS context.
*
* @return void
*/
private function initialize_hpos() {
\Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::delete_order_custom_tables();
\Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order_custom_table_if_not_exist();

View File

@ -5,6 +5,35 @@
*/
class WC_Order_Factory_Test extends WC_Unit_Test_Case {
/**
* Store COT state at the start of the test so we can restore it later.
*
* @var bool
*/
private $cot_state;
/**
* Disable COT before the test.
*
* @return void
*/
public function setUp(): void {
parent::setUp();
$this->cot_state = \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled();
\Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::toggle_cot( false );
}
/**
* Restore COT state after the test.
*
* @return void
*/
public function tearDown(): void {
parent::tearDown();
wp_cache_flush();
\Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::toggle_cot( $this->cot_state );
}
/**
* @testDox get_orders should be able to return multiple orders of different types.
*/

View File

@ -7,6 +7,7 @@ use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableQuery;
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper;
use Automattic\WooCommerce\RestApi\UnitTests\HPOSToggleTrait;
use Automattic\WooCommerce\Utilities\OrderUtil;
/**
* Class OrdersTableDataStoreTests.
@ -37,6 +38,12 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
*/
private $cpt_data_store;
/**
* Whether COT was enabled before the test.
* @var bool
*/
private $cot_state;
/**
* Initializes system under test.
*/
@ -47,6 +54,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
parent::setUp();
// Remove the Test Suites use of temporary tables https://wordpress.stackexchange.com/a/220308.
$this->setup_cot();
$this->cot_state = OrderUtil::custom_orders_table_usage_is_enabled();
$this->toggle_cot( false );
$this->sut = wc_get_container()->get( OrdersTableDataStore::class );
$this->migrator = wc_get_container()->get( PostsToOrdersMigrationController::class );
@ -59,6 +67,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
public function tearDown(): void {
//phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set -- We need to change the timezone to test the date sync fields.
update_option( 'timezone_string', $this->original_time_zone );
$this->toggle_cot( $this->cot_state );
$this->clean_up_cot_setup();
parent::tearDown();
}
@ -205,6 +214,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
wp_cache_flush();
$order = new WC_Order();
$order->set_id( $post_order->get_id() );
$this->toggle_cot( true );
$this->switch_data_store( $order, $this->sut );
$this->sut->read( $order );
@ -239,6 +249,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
foreach ( $datastore_updates as $prop => $value ) {
$this->assertEquals( $value, $this->sut->{"get_$prop"}( $order ), "Unable to match prop $prop" );
}
$this->toggle_cot( false );
}
/**
@ -1767,6 +1778,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
* Ideally, this should be possible only from getters and setters for objects, but for backward compatibility, earlier ways are also supported.
*/
public function test_internal_ds_getters_and_setters() {
$this->toggle_cot( true );
$props_to_test = array(
'_download_permissions_granted',
'_recorded_sales',
@ -1813,6 +1825,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
$order->save();
}
$this->assert_get_prop_via_ds_object_and_metadata( $props_to_test, $order, false, $ds_getter_setter_names );
$this->toggle_cot( false );
}
/**
@ -1855,7 +1868,8 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
* @testDox Legacy getters and setters for props migrated from data stores should be set/reset properly.
*/
public function test_legacy_getters_setters() {
$order_id = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_complex_wp_post_order();
$this->toggle_cot( true );
$order_id = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_complex_data_store_order( $this->sut );
$order = wc_get_order( $order_id );
$this->switch_data_store( $order, $this->sut );
$bool_props = array(
@ -1887,7 +1901,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
$this->assert_props_value_via_data_store( $order, $bool_props, true );
$this->assert_props_value_via_order_object( $order, $bool_props, true );
$this->toggle_cot( false );
}
/**
@ -1984,8 +1998,9 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
*/
public function test_read_multiple_dont_sync_again_for_same_order() {
$this->toggle_cot( true );
$this->enable_cot_sync();
$order = $this->create_complex_cot_order();
$this->sut->backfill_post_record( $order );
$this->enable_cot_sync();
$order_id = $order->get_id();
@ -1999,6 +2014,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
$this->assertTrue( $should_sync_callable->call( $this->sut, $order ) );
$this->sut->read_multiple( $orders );
$this->assertFalse( $should_sync_callable->call( $this->sut, $order ) );
$this->toggle_cot( false );
}
/**