Commit 1069f6e8d57 for woocommerce
commit 1069f6e8d57ecdfd3d325cd6863969c5e47b8232
Author: Adrian Moldovan <3854374+adimoldovan@users.noreply.github.com>
Date: Thu Jul 9 19:27:45 2026 +0300
php tests: Fix duplicate PRIMARY key DB error in customer data store test (#66451)
diff --git a/plugins/woocommerce/changelog/fix-customer-data-store-test-duplicate-primary-key b/plugins/woocommerce/changelog/fix-customer-data-store-test-duplicate-primary-key
new file mode 100644
index 00000000000..f4cde99f9da
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-customer-data-store-test-duplicate-primary-key
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Fix a duplicate PRIMARY key database error logged by WC_Customer_Data_Store_CPT_Test::test_get_last_customer_order_using_cot by deriving the raw order IDs from the orders table's current max ID so they cannot collide, and by making the fixture independent of the order storage mode.
diff --git a/plugins/woocommerce/tests/php/includes/data-stores/class-wc-customer-data-store-test.php b/plugins/woocommerce/tests/php/includes/data-stores/class-wc-customer-data-store-test.php
index 31f24555ed1..040b0d41a83 100644
--- a/plugins/woocommerce/tests/php/includes/data-stores/class-wc-customer-data-store-test.php
+++ b/plugins/woocommerce/tests/php/includes/data-stores/class-wc-customer-data-store-test.php
@@ -124,33 +124,60 @@ class WC_Customer_Data_Store_CPT_Test extends WC_Unit_Test_Case {
public function test_get_last_customer_order_using_cot() {
global $wpdb;
- $customer_1 = WC_Helper_Customer::create_customer( 'test1', 'pass1', 'test1@example.com' );
- $customer_2 = WC_Helper_Customer::create_customer( 'test2', 'pass2', 'test2@example.com' );
- $last_valid_order = WC_Helper_Order::create_order( $customer_1->get_id() );
+ $customer_1 = WC_Helper_Customer::create_customer( 'test1', 'pass1', 'test1@example.com' );
+ $customer_2 = WC_Helper_Customer::create_customer( 'test2', 'pass2', 'test2@example.com' );
update_option( CustomOrdersTableController::CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION, 'yes' );
+ $orders_table = OrdersTableDataStore::get_orders_table_name();
+
+ // Derive the base ID from the orders table itself so the raw rows never collide with a row that
+ // already exists there (under HPOS, order creation writes into this table). Using MAX(id) keeps
+ // the test collision-safe whether or not HPOS is enabled, and inserting every order directly
+ // into the orders table keeps it independent of the storage mode.
+ $base_id = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COALESCE( MAX( id ), 0 ) FROM %i', $orders_table ) );
+
+ $customer_1_id = $customer_1->get_id();
+ $customer_2_id = $customer_2->get_id();
+
+ // Customer 1 has two completed orders plus a higher-ID invalid order; get_last_order() must
+ // skip the invalid one and return the most recent completed order ($last_valid_order_id).
+ // Customer 2's orders have even higher IDs, so they must be excluded by customer scoping
+ // rather than by ID ordering.
+ $last_valid_order_id = $base_id + 2;
+
$sql =
- 'INSERT INTO ' . OrdersTableDataStore::get_orders_table_name() . "
+ 'INSERT INTO %i' . "
( id, customer_id, status, type )
VALUES
- ( 1, %d, '" . OrderInternalStatus::COMPLETED . "', 'shop_order' ),
( %d, %d, '" . OrderInternalStatus::COMPLETED . "', 'shop_order' ),
- ( 3, %d, 'wc-invalid-status', 'shop_order' ),
- ( 4, %d, '" . OrderInternalStatus::COMPLETED . "', 'shop_order' ),
- ( 5, %d, '" . OrderInternalStatus::COMPLETED . "', 'shop_order' )";
+ ( %d, %d, '" . OrderInternalStatus::COMPLETED . "', 'shop_order' ),
+ ( %d, %d, 'wc-invalid-status', 'shop_order' ),
+ ( %d, %d, '" . OrderInternalStatus::COMPLETED . "', 'shop_order' ),
+ ( %d, %d, '" . OrderInternalStatus::COMPLETED . "', 'shop_order' )";
- $customer_1_id = $customer_1->get_id();
- $customer_2_id = $customer_2->get_id();
//phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
- $query = $wpdb->prepare( $sql, $customer_1_id, $last_valid_order->get_id(), $customer_1_id, $customer_1_id, $customer_2_id, $customer_2_id );
+ $query = $wpdb->prepare(
+ $sql,
+ $orders_table,
+ $base_id + 1,
+ $customer_1_id,
+ $last_valid_order_id,
+ $customer_1_id,
+ $base_id + 3,
+ $customer_1_id,
+ $base_id + 4,
+ $customer_2_id,
+ $base_id + 5,
+ $customer_2_id
+ );
$wpdb->query( $query );
//phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
$sut = new WC_Customer_Data_Store();
$actual_order = $sut->get_last_order( $customer_1 );
- $this->assertEquals( $last_valid_order->get_id(), $actual_order->get_id() );
+ $this->assertEquals( $last_valid_order_id, $actual_order->get_id() );
}
/**