Commit 1faf15e495f for woocommerce

commit 1faf15e495f55ea24d89def3b57811f404f1ddf8
Author: Taha Paksu <3295+tpaksu@users.noreply.github.com>
Date:   Tue Mar 17 16:26:46 2026 +0300

    Fix fulfillments data store bugs and test flakiness (#63689)

    * Fix fulfillments data store bugs and test flakiness

    Fix timestamp synchronization issues in FulfillmentsDataStore where
    date_updated and date_fulfilled could differ between the object and DB.
    Remove duplicate set_date_fulfilled calls and fix wrong meta key check.

    Improve test resilience: fix order-dependent shared state, cumulative
    did_action assertions, exact error response assertions, fragile SVG
    path assertion, and duplicate save_meta_data call. Add missing test
    for date_fulfilled on create.

    * Update plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/DataStore/FulfillmentsDataStoreTest.php

    Co-authored-by: Fernando Espinosa <Ferdev@users.noreply.github.com>

    * Fix test missing parent initializers and destructors

    * Use fixed date in fulfillment update test to eliminate flakiness

    Replace timestamp-dependent assertion with a deterministic fixed
    date_fulfilled value, fixing PHPCS issues along the way.

    ---------

    Co-authored-by: Fernando Espinosa <Ferdev@users.noreply.github.com>

diff --git a/plugins/woocommerce/src/Admin/Features/Fulfillments/DataStore/FulfillmentsDataStore.php b/plugins/woocommerce/src/Admin/Features/Fulfillments/DataStore/FulfillmentsDataStore.php
index 62d7e82e968..d021cfb642e 100644
--- a/plugins/woocommerce/src/Admin/Features/Fulfillments/DataStore/FulfillmentsDataStore.php
+++ b/plugins/woocommerce/src/Admin/Features/Fulfillments/DataStore/FulfillmentsDataStore.php
@@ -104,11 +104,6 @@ class FulfillmentsDataStore extends \WC_Data_Store_WP implements \WC_Object_Data

 		$data->set_id( $data_id );

-		// If the fulfillment is fulfilled, set the fulfilled date.
-		if ( $data->get_is_fulfilled() ) {
-			$data->set_date_fulfilled( current_time( 'mysql' ) );
-		}
-
 		// Save the metadata for the fulfillment to the database.
 		$data->save_meta_data();

@@ -226,6 +221,8 @@ class FulfillmentsDataStore extends \WC_Data_Store_WP implements \WC_Object_Data

 		global $wpdb;

+		$data->set_date_updated( current_time( 'mysql' ) );
+
 		$wpdb->update(
 			$wpdb->prefix . 'wc_order_fulfillments',
 			array(
@@ -233,7 +230,7 @@ class FulfillmentsDataStore extends \WC_Data_Store_WP implements \WC_Object_Data
 				'entity_id'    => $data->get_entity_id(),
 				'status'       => $data->get_status(),
 				'is_fulfilled' => $data->get_is_fulfilled() ? 1 : 0,
-				'date_updated' => current_time( 'mysql' ),
+				'date_updated' => $data->get_date_updated(),
 				'date_deleted' => $data->get_date_deleted(),
 			),
 			array(
@@ -249,11 +246,6 @@ class FulfillmentsDataStore extends \WC_Data_Store_WP implements \WC_Object_Data
 			throw new \Exception( esc_html__( 'Failed to update fulfillment.', 'woocommerce' ) );
 		}

-		// If the fulfillment is fulfilled, set the fulfilled date.
-		if ( $data->get_is_fulfilled() && ! $data->meta_exists( '_fulfilled_date' ) ) {
-			$data->set_date_fulfilled( current_time( 'mysql' ) );
-		}
-
 		// Update the metadata for the fulfillment.
 		$data->save_meta_data();
 		$data->apply_changes();
diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/DataStore/FulfillmentsDataStoreTest.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/DataStore/FulfillmentsDataStoreTest.php
index cecae9e23fa..615c7fae315 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/DataStore/FulfillmentsDataStoreTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/DataStore/FulfillmentsDataStoreTest.php
@@ -44,6 +44,7 @@ class FulfillmentsDataStoreTest extends \WC_Unit_Test_Case {
 	 * Set up the test case.
 	 */
 	public function setUp(): void {
+		parent::setUp();
 		$this->data_store = wc_get_container()->get( FulfillmentsDataStore::class );
 	}

@@ -56,6 +57,7 @@ class FulfillmentsDataStoreTest extends \WC_Unit_Test_Case {
 		$wpdb->query( "DELETE FROM {$wpdb->prefix}wc_order_fulfillment_meta" );
 		// Clean up the fulfillment table.
 		$wpdb->query( "DELETE FROM {$wpdb->prefix}wc_order_fulfillments" );
+		parent::tearDown();
 	}

 	/**
@@ -84,6 +86,74 @@ class FulfillmentsDataStoreTest extends \WC_Unit_Test_Case {
 		$this->assertFulfillmentMetaInDB( $fulfillment );
 	}

+	/**
+	 * Tests that creating a fulfilled fulfillment sets the date_fulfilled metadata
+	 * consistently between the object and the database.
+	 */
+	public function test_create_fulfilled_fulfillment_sets_date_fulfilled() {
+		$fulfillment = new Fulfillment();
+		$fulfillment->set_entity_type( 'order-fulfillment' );
+		$fulfillment->set_entity_id( '123' );
+		$fulfillment->set_status( 'fulfilled' );
+		$fulfillment->set_items(
+			array(
+				array(
+					'item_id' => 1,
+					'qty'     => 2,
+				),
+			)
+		);
+
+		$this->data_store->create( $fulfillment );
+
+		$this->assertNotNull( $fulfillment->get_id() );
+		$this->assertTrue( $fulfillment->get_is_fulfilled() );
+		$this->assertNotNull( $fulfillment->get_date_fulfilled() );
+
+		// Read back from DB and verify the date_fulfilled matches.
+		$read_fulfillment = new Fulfillment( $fulfillment->get_id() );
+
+		$this->assertEquals( $fulfillment->get_date_fulfilled(), $read_fulfillment->get_date_fulfilled() );
+	}
+
+	/**
+	 * Tests that updating a fulfilled fulfillment preserves the original date_fulfilled value
+	 * and does not change it to the current time.
+	 */
+	public function test_update_fulfilled_fulfillment_preserves_date_fulfilled() {
+		$fulfillment = new Fulfillment();
+		$fulfillment->set_entity_type( 'order-fulfillment' );
+		$fulfillment->set_entity_id( '123' );
+		$fulfillment->set_status( 'unfulfilled' );
+		$fulfillment->set_items(
+			array(
+				array(
+					'item_id' => 1,
+					'qty'     => 2,
+				),
+			)
+		);
+		$this->data_store->create( $fulfillment );
+
+		// Mark as fulfilled and save.
+		$fulfillment->set_status( 'fulfilled' );
+		$this->data_store->update( $fulfillment );
+		$this->assertNotNull( $fulfillment->get_date_fulfilled() );
+
+		// Set a known fixed date_fulfilled to make the assertion deterministic.
+		$fixed_date = '2025-01-15 10:30:00';
+		$fulfillment->set_date_fulfilled( $fixed_date );
+		$fulfillment->set_entity_id( '456' );
+		$this->data_store->update( $fulfillment );
+
+		// Verify in-memory value is preserved.
+		$this->assertEquals( $fixed_date, $fulfillment->get_date_fulfilled() );
+
+		// Verify persisted value matches after reloading from DB.
+		$read_fulfillment = new Fulfillment( $fulfillment->get_id() );
+		$this->assertEquals( $fixed_date, $read_fulfillment->get_date_fulfilled() );
+	}
+
 	/**
 	 * Tests the create method of the order fulfillment data store with invalid entity type.
 	 */
@@ -700,7 +770,6 @@ class FulfillmentsDataStoreTest extends \WC_Unit_Test_Case {
 		$fulfillment->set_status( 'unfulfilled' );
 		$fulfillment->set_items( $items );
 		$fulfillment->save();
-		$fulfillment->save_meta_data();

 		$this->assertNotEquals( 0, $fulfillment->get_id() );

diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsManagerTest.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsManagerTest.php
index 7c8c4a9df37..569e8eec46e 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsManagerTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsManagerTest.php
@@ -184,7 +184,8 @@ class FulfillmentsManagerTest extends \WC_Unit_Test_Case {
 		$order        = OrderHelper::create_order( get_current_user_id(), $product );
 		$this->assertEmpty( $order->get_meta( '_fulfillment_status' ) );

-		$fulfillments[] = FulfillmentsHelper::create_fulfillment(
+		$create_count_before = did_action( 'woocommerce_fulfillment_after_create' );
+		$fulfillments[]      = FulfillmentsHelper::create_fulfillment(
 			array(
 				'entity_type'  => WC_Order::class,
 				'entity_id'    => $order->get_id(),
@@ -200,19 +201,21 @@ class FulfillmentsManagerTest extends \WC_Unit_Test_Case {
 				),
 			)
 		);
-		$this->assertTrue( did_action( 'woocommerce_fulfillment_after_create' ) > 0 );
+		$this->assertGreaterThan( $create_count_before, did_action( 'woocommerce_fulfillment_after_create' ) );
 		$order = wc_get_order( $order->get_id() );
 		$this->assertEquals( 'unfulfilled', $order->get_meta( '_fulfillment_status', true ) );

+		$update_count_before = did_action( 'woocommerce_fulfillment_after_update' );
 		$fulfillments[0]->set_status( 'fulfilled' );
 		$fulfillments[0]->save();

-		$this->assertTrue( did_action( 'woocommerce_fulfillment_after_update' ) > 0 );
+		$this->assertGreaterThan( $update_count_before, did_action( 'woocommerce_fulfillment_after_update' ) );
 		$order = wc_get_order( $order->get_id() );
 		$this->assertEquals( 'partially_fulfilled', $order->get_meta( '_fulfillment_status' ) );

+		$delete_count_before = did_action( 'woocommerce_fulfillment_after_delete' );
 		$fulfillments[0]->delete();
-		$this->assertTrue( did_action( 'woocommerce_fulfillment_after_delete' ) > 0 );
+		$this->assertGreaterThan( $delete_count_before, did_action( 'woocommerce_fulfillment_after_delete' ) );
 		$order = wc_get_order( $order->get_id() );
 		$this->assertEquals( '', $order->get_meta( '_fulfillment_status' ) );
 	}
diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsRendererTest.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsRendererTest.php
index 4118ddf52e1..9806fc3e8c6 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsRendererTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsRendererTest.php
@@ -118,8 +118,8 @@ class FulfillmentsRendererTest extends \WC_Unit_Test_Case {
 		$this->assertStringContainsString( '123456789', $output );
 		$this->assertStringContainsString( 'UPS', $output );
 		$this->assertStringContainsString( "<a href='#' class='fulfillments-trigger' data-order-id='" . $order->get_id() . "' title='" . esc_attr__( 'View Fulfillments', 'woocommerce' ) . "'>", $output );
-		$this->assertStringContainsString( "<svg width='16' height='16' viewBox='0 0 12 14' xmlns='http://www.w3.org/2000/svg'>", $output );
-		$this->assertStringContainsString( "<path d='M11.8333 2.83301L9.33329 0.333008L2.24996 7.41634L1.41663 10.7497L4.74996 9.91634L11.8333 2.83301ZM5.99996 12.4163H0.166626V13.6663H5.99996V12.4163Z' />", $output );
+		$this->assertStringContainsString( '<svg ', $output );
+		$this->assertStringContainsString( '<path ', $output );
 		$this->assertStringContainsString( '</svg>', $output );
 		$this->assertStringContainsString( '</a>', $output );
 	}
diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/OrderFulfillmentsRestControllerTest.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/OrderFulfillmentsRestControllerTest.php
index e72ca32bc84..06f9e6fdcc1 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/OrderFulfillmentsRestControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/OrderFulfillmentsRestControllerTest.php
@@ -157,14 +157,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response.
 		$this->assertEquals( WP_Http::FORBIDDEN, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'woocommerce_rest_cannot_view',
-				'message' => 'Sorry, you cannot view resources.',
-				'data'    => array( 'status' => WP_Http::FORBIDDEN ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'woocommerce_rest_cannot_view', $data['code'] );
+		$this->assertEquals( 'Sorry, you cannot view resources.', $data['message'] );
+		$this->assertEquals( WP_Http::FORBIDDEN, $data['data']['status'] );

 		// Clean up the test environment.
 		wp_set_current_user( $current_user->ID );
@@ -245,14 +241,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that a regular user cannot create a fulfillment.
 		$this->assertEquals( WP_Http::UNAUTHORIZED, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'woocommerce_rest_cannot_create',
-				'message' => 'Sorry, you cannot create resources.',
-				'data'    => array( 'status' => WP_Http::UNAUTHORIZED ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'woocommerce_rest_cannot_create', $data['code'] );
+		$this->assertEquals( 'Sorry, you cannot create resources.', $data['message'] );
+		$this->assertEquals( WP_Http::UNAUTHORIZED, $data['data']['status'] );
 	}

 	/**
@@ -379,14 +371,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that a fulfillment should contain at least one item.
 		$this->assertEquals( WP_Http::BAD_REQUEST, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 0,
-				'message' => 'The fulfillment should contain at least one item.',
-				'data'    => array( 'status' => WP_Http::BAD_REQUEST ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 0, $data['code'] );
+		$this->assertEquals( 'The fulfillment should contain at least one item.', $data['message'] );
+		$this->assertEquals( WP_Http::BAD_REQUEST, $data['data']['status'] );
 	}

 	/**
@@ -428,14 +416,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that the items are invalid.
 		$this->assertEquals( WP_Http::BAD_REQUEST, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 0,
-				'message' => 'Invalid item.',
-				'data'    => array( 'status' => WP_Http::BAD_REQUEST ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 0, $data['code'] );
+		$this->assertEquals( 'Invalid item.', $data['message'] );
+		$this->assertEquals( WP_Http::BAD_REQUEST, $data['data']['status'] );
 	}

 	/**
@@ -492,14 +476,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that the order ID is invalid.
 		$this->assertEquals( WP_Http::NOT_FOUND, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'woocommerce_rest_order_invalid_id',
-				'message' => 'Invalid order ID.',
-				'data'    => array( 'status' => WP_Http::NOT_FOUND ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'woocommerce_rest_order_invalid_id', $data['code'] );
+		$this->assertEquals( 'Invalid order ID.', $data['message'] );
+		$this->assertEquals( WP_Http::NOT_FOUND, $data['data']['status'] );
 	}

 	/**
@@ -584,14 +564,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that the order ID is invalid.
 		$this->assertEquals( WP_Http::NOT_FOUND, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'woocommerce_rest_order_invalid_id',
-				'message' => 'Invalid order ID.',
-				'data'    => array( 'status' => WP_Http::NOT_FOUND ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'woocommerce_rest_order_invalid_id', $data['code'] );
+		$this->assertEquals( 'Invalid order ID.', $data['message'] );
+		$this->assertEquals( WP_Http::NOT_FOUND, $data['data']['status'] );
 	}

 	/**
@@ -614,14 +590,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {
 		// Check the response. It should be an error saying that the fulfillment ID is invalid.
 		$this->assertEquals( WP_Http::BAD_REQUEST, $response->get_status() );

-		$this->assertEquals(
-			array(
-				'code'    => 0,
-				'message' => 'Fulfillment not found.',
-				'data'    => array( 'status' => WP_Http::BAD_REQUEST ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 0, $data['code'] );
+		$this->assertEquals( 'Fulfillment not found.', $data['message'] );
+		$this->assertEquals( WP_Http::BAD_REQUEST, $data['data']['status'] );
 	}

 	/**
@@ -650,14 +622,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that a regular user cannot view a fulfillment.
 		$this->assertEquals( WP_Http::FORBIDDEN, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'woocommerce_rest_cannot_view',
-				'message' => 'Sorry, you cannot view resources.',
-				'data'    => array( 'status' => WP_Http::FORBIDDEN ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'woocommerce_rest_cannot_view', $data['code'] );
+		$this->assertEquals( 'Sorry, you cannot view resources.', $data['message'] );
+		$this->assertEquals( WP_Http::FORBIDDEN, $data['data']['status'] );

 		wp_set_current_user( $current_user->ID );
 	}
@@ -720,14 +688,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {
 		$response = $this->server->dispatch( $request );
 		// Check the response. It should be an error saying that a regular user cannot update a fulfillment.
 		$this->assertEquals( WP_Http::FORBIDDEN, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'rest_forbidden',
-				'message' => 'Sorry, you are not allowed to do that.',
-				'data'    => array( 'status' => WP_Http::FORBIDDEN ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'rest_forbidden', $data['code'] );
+		$this->assertEquals( 'Sorry, you are not allowed to do that.', $data['message'] );
+		$this->assertEquals( WP_Http::FORBIDDEN, $data['data']['status'] );
 	}

 	/**
@@ -898,14 +862,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {
 		$response = $this->server->dispatch( $request );
 		// Check the response. It should be an error saying that the order ID is invalid.
 		$this->assertEquals( WP_Http::NOT_FOUND, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'woocommerce_rest_order_invalid_id',
-				'message' => 'Invalid order ID.',
-				'data'    => array( 'status' => WP_Http::NOT_FOUND ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'woocommerce_rest_order_invalid_id', $data['code'] );
+		$this->assertEquals( 'Invalid order ID.', $data['message'] );
+		$this->assertEquals( WP_Http::NOT_FOUND, $data['data']['status'] );

 		// Clean up the test environment.
 		wp_set_current_user( 0 );
@@ -968,14 +928,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {
 		$response = $this->server->dispatch( $request );
 		// Check the response. It should be an error saying that the fulfillment ID is invalid.
 		$this->assertEquals( WP_Http::BAD_REQUEST, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 0,
-				'message' => 'Fulfillment not found.',
-				'data'    => array( 'status' => WP_Http::BAD_REQUEST ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 0, $data['code'] );
+		$this->assertEquals( 'Fulfillment not found.', $data['message'] );
+		$this->assertEquals( WP_Http::BAD_REQUEST, $data['data']['status'] );

 		// Clean up the test environment.
 		wp_set_current_user( 0 );
@@ -1024,14 +980,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {
 		$response = $this->server->dispatch( $request );
 		// Check the response. It should be an error saying that a fulfillment should contain at least one item.
 		$this->assertEquals( WP_Http::BAD_REQUEST, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 0,
-				'message' => 'The fulfillment should contain at least one item.',
-				'data'    => array( 'status' => WP_Http::BAD_REQUEST ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 0, $data['code'] );
+		$this->assertEquals( 'The fulfillment should contain at least one item.', $data['message'] );
+		$this->assertEquals( WP_Http::BAD_REQUEST, $data['data']['status'] );

 		// Clean up the test environment.
 		wp_set_current_user( 0 );
@@ -1079,14 +1031,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {
 		$response = $this->server->dispatch( $request );
 		// Check the response. It should be an error saying that the item quantity is invalid.
 		$this->assertEquals( WP_Http::BAD_REQUEST, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 0,
-				'message' => 'Invalid item.',
-				'data'    => array( 'status' => WP_Http::BAD_REQUEST ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 0, $data['code'] );
+		$this->assertEquals( 'Invalid item.', $data['message'] );
+		$this->assertEquals( WP_Http::BAD_REQUEST, $data['data']['status'] );
 		// Clean up the test environment.
 		wp_set_current_user( 0 );
 	}
@@ -1167,28 +1115,24 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that a regular user cannot delete a fulfillment.
 		$this->assertEquals( WP_Http::FORBIDDEN, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'woocommerce_rest_cannot_delete',
-				'message' => 'Sorry, you cannot delete resources.',
-				'data'    => array( 'status' => WP_Http::FORBIDDEN ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'woocommerce_rest_cannot_delete', $data['code'] );
+		$this->assertEquals( 'Sorry, you cannot delete resources.', $data['message'] );
+		$this->assertEquals( WP_Http::FORBIDDEN, $data['data']['status'] );
 	}

 	/**
 	 * Test deleting a fulfillment for an admin user.
 	 */
 	public function test_delete_fulfillment_for_admin_user() {
-		// Get a previously created order.
-		$order_id = self::$created_order_ids[0];
+		// Use a dedicated order to avoid mutating shared state used by other tests.
+		$order_id = self::$created_order_ids[9];
 		$request  = new WP_REST_Request( 'GET', '/wc/v3/orders/' . $order_id . '/fulfillments' );
 		$response = $this->server->dispatch( $request );

 		$fulfillments = $response->get_data();
 		$this->assertIsArray( $fulfillments );
-		$this->assertCount( 10, $fulfillments );
+		$this->assertNotEmpty( $fulfillments );

 		$fulfillment_id = $fulfillments[0]['id'];

@@ -1225,14 +1169,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that the order ID is invalid.
 		$this->assertEquals( WP_Http::NOT_FOUND, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'woocommerce_rest_order_invalid_id',
-				'message' => 'Invalid order ID.',
-				'data'    => array( 'status' => WP_Http::NOT_FOUND ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'woocommerce_rest_order_invalid_id', $data['code'] );
+		$this->assertEquals( 'Invalid order ID.', $data['message'] );
+		$this->assertEquals( WP_Http::NOT_FOUND, $data['data']['status'] );
 	}

 	/**
@@ -1256,14 +1196,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that the fulfillment ID is invalid.
 		$this->assertEquals( WP_Http::BAD_REQUEST, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 0,
-				'message' => 'Fulfillment not found.',
-				'data'    => array( 'status' => WP_Http::BAD_REQUEST ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 0, $data['code'] );
+		$this->assertEquals( 'Fulfillment not found.', $data['message'] );
+		$this->assertEquals( WP_Http::BAD_REQUEST, $data['data']['status'] );
 	}

 	/**
@@ -1292,14 +1228,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that a regular user cannot delete a fulfillment.
 		$this->assertEquals( WP_Http::FORBIDDEN, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'woocommerce_rest_cannot_delete',
-				'message' => 'Sorry, you cannot delete resources.',
-				'data'    => array( 'status' => WP_Http::FORBIDDEN ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'woocommerce_rest_cannot_delete', $data['code'] );
+		$this->assertEquals( 'Sorry, you cannot delete resources.', $data['message'] );
+		$this->assertEquals( WP_Http::FORBIDDEN, $data['data']['status'] );

 		wp_set_current_user( $current_user->ID );
 	}
@@ -1432,14 +1364,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that the order ID is invalid.
 		$this->assertEquals( WP_Http::NOT_FOUND, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'woocommerce_rest_order_invalid_id',
-				'message' => 'Invalid order ID.',
-				'data'    => array( 'status' => WP_Http::NOT_FOUND ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'woocommerce_rest_order_invalid_id', $data['code'] );
+		$this->assertEquals( 'Invalid order ID.', $data['message'] );
+		$this->assertEquals( WP_Http::NOT_FOUND, $data['data']['status'] );
 	}

 	/**
@@ -1463,14 +1391,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that the fulfillment ID is invalid.
 		$this->assertEquals( WP_Http::BAD_REQUEST, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 0,
-				'message' => 'Fulfillment not found.',
-				'data'    => array( 'status' => WP_Http::BAD_REQUEST ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 0, $data['code'] );
+		$this->assertEquals( 'Fulfillment not found.', $data['message'] );
+		$this->assertEquals( WP_Http::BAD_REQUEST, $data['data']['status'] );
 	}

 	/**
@@ -1499,14 +1423,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that a regular user cannot view a fulfillment.
 		$this->assertEquals( WP_Http::FORBIDDEN, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'woocommerce_rest_cannot_view',
-				'message' => 'Sorry, you cannot view resources.',
-				'data'    => array( 'status' => WP_Http::FORBIDDEN ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'woocommerce_rest_cannot_view', $data['code'] );
+		$this->assertEquals( 'Sorry, you cannot view resources.', $data['message'] );
+		$this->assertEquals( WP_Http::FORBIDDEN, $data['data']['status'] );

 		wp_set_current_user( $current_user->ID );
 	}
@@ -1549,14 +1469,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that a regular user cannot update a fulfillment.
 		$this->assertEquals( WP_Http::UNAUTHORIZED, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'rest_forbidden',
-				'message' => 'Sorry, you are not allowed to do that.',
-				'data'    => array( 'status' => WP_Http::UNAUTHORIZED ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'rest_forbidden', $data['code'] );
+		$this->assertEquals( 'Sorry, you are not allowed to do that.', $data['message'] );
+		$this->assertEquals( WP_Http::UNAUTHORIZED, $data['data']['status'] );
 	}

 	/**
@@ -1682,14 +1598,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that the order ID is invalid.
 		$this->assertEquals( WP_Http::NOT_FOUND, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'woocommerce_rest_order_invalid_id',
-				'message' => 'Invalid order ID.',
-				'data'    => array( 'status' => WP_Http::NOT_FOUND ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'woocommerce_rest_order_invalid_id', $data['code'] );
+		$this->assertEquals( 'Invalid order ID.', $data['message'] );
+		$this->assertEquals( WP_Http::NOT_FOUND, $data['data']['status'] );

 		// Clean up the test environment.
 		wp_set_current_user( 0 );
@@ -1732,14 +1644,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that the fulfillment ID is invalid.
 		$this->assertEquals( WP_Http::BAD_REQUEST, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 0,
-				'message' => 'Fulfillment not found.',
-				'data'    => array( 'status' => WP_Http::BAD_REQUEST ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 0, $data['code'] );
+		$this->assertEquals( 'Fulfillment not found.', $data['message'] );
+		$this->assertEquals( WP_Http::BAD_REQUEST, $data['data']['status'] );

 		// Clean up the test environment.
 		wp_set_current_user( 0 );
@@ -1787,14 +1695,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that a regular user cannot update a fulfillment.
 		$this->assertEquals( WP_Http::FORBIDDEN, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'rest_forbidden',
-				'message' => 'Sorry, you are not allowed to do that.',
-				'data'    => array( 'status' => WP_Http::FORBIDDEN ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'rest_forbidden', $data['code'] );
+		$this->assertEquals( 'Sorry, you are not allowed to do that.', $data['message'] );
+		$this->assertEquals( WP_Http::FORBIDDEN, $data['data']['status'] );

 		// Clean up the test environment.
 		wp_set_current_user( $current_user->ID );
@@ -1829,14 +1733,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that a regular user cannot delete a fulfillment.
 		$this->assertEquals( WP_Http::UNAUTHORIZED, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'woocommerce_rest_cannot_delete',
-				'message' => 'Sorry, you cannot delete resources.',
-				'data'    => array( 'status' => WP_Http::UNAUTHORIZED ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'woocommerce_rest_cannot_delete', $data['code'] );
+		$this->assertEquals( 'Sorry, you cannot delete resources.', $data['message'] );
+		$this->assertEquals( WP_Http::UNAUTHORIZED, $data['data']['status'] );
 	}

 	/**
@@ -1905,14 +1805,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that the order ID is invalid.
 		$this->assertEquals( WP_Http::NOT_FOUND, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'woocommerce_rest_order_invalid_id',
-				'message' => 'Invalid order ID.',
-				'data'    => array( 'status' => WP_Http::NOT_FOUND ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'woocommerce_rest_order_invalid_id', $data['code'] );
+		$this->assertEquals( 'Invalid order ID.', $data['message'] );
+		$this->assertEquals( WP_Http::NOT_FOUND, $data['data']['status'] );
 		// Clean up the test environment.
 		wp_set_current_user( 0 );
 	}
@@ -1946,14 +1842,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that the fulfillment ID is invalid.
 		$this->assertEquals( WP_Http::BAD_REQUEST, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 0,
-				'message' => 'Fulfillment not found.',
-				'data'    => array( 'status' => WP_Http::BAD_REQUEST ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 0, $data['code'] );
+		$this->assertEquals( 'Fulfillment not found.', $data['message'] );
+		$this->assertEquals( WP_Http::BAD_REQUEST, $data['data']['status'] );
 		// Clean up the test environment.
 		wp_set_current_user( 0 );
 	}
@@ -1992,14 +1884,10 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {

 		// Check the response. It should be an error saying that a regular user cannot delete a fulfillment.
 		$this->assertEquals( WP_Http::FORBIDDEN, $response->get_status() );
-		$this->assertEquals(
-			array(
-				'code'    => 'woocommerce_rest_cannot_delete',
-				'message' => 'Sorry, you cannot delete resources.',
-				'data'    => array( 'status' => WP_Http::FORBIDDEN ),
-			),
-			$response->get_data()
-		);
+		$data = $response->get_data();
+		$this->assertEquals( 'woocommerce_rest_cannot_delete', $data['code'] );
+		$this->assertEquals( 'Sorry, you cannot delete resources.', $data['message'] );
+		$this->assertEquals( WP_Http::FORBIDDEN, $data['data']['status'] );

 		wp_set_current_user( $current_user->ID );
 	}