Commit 0f348bd6ab9 for woocommerce
commit 0f348bd6ab91384780622a250707861098387f17
Author: Vlad Olaru <vlad.olaru@automattic.com>
Date: Mon Nov 16 10:46:44 2026 +0200
[tests] Speed up the slowest PHPUnit tests (#70990)
* test: Seed batch-boundary product fixtures at the rows the code reads
Five tests cross a batch size by creating hundreds of products through
the full save path: 51 products for the scheduled sales batch (two CRUD
saves each) and 500-501 product posts for the product transient cleanup
batch. On CI they take 1.7-2.4 s each and top the slow-test report.
The code under test only reads persisted rows: get_ending_sales() reads
postmeta and the transient cleanup reads ID and post_type from the posts
table. Following the "seed at the boundary the code reads" rule in
tests/README.md, two WC_Helper_Product helpers now insert those rows in
one query each. The single-product tests keep covering the CRUD path.
Locally the scheduled sales tests drop from ~690 ms to ~265 ms, the rest
being wc_scheduled_sales() itself. The cleanup tests drop from 510-640
ms to under 40 ms.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Build cheaper orders in two batch-boundary tests
HposOrderExportHandlerTest creates 101 orders to cross the export batch
and OrderLogsCleanupTest creates 105 and 200 orders to cross the cleanup
batch. On CI they take 1.0-2.0 s.
The export test saved every filler order as processing, which runs the
status-transition work on each save; a pending order saves about four
times faster and the export includes it the same way. The cleanup tests
built each order with wc_create_order() and then saved it again; one
save of a new WC_Order with the same date and meta is enough.
Locally the export test drops from ~620 ms to ~210 ms and the follow-up
cleanup test from ~500 ms to ~275 ms.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Size the related products cache loop to each cache budget
test_related_product_cache_has_size_limit fills the related products
cache with 1000 queries for both budgets. The 8 KB database transient
budget holds 85 entries, so most of those 1000 writes rewrite an
already full option row; the data set takes ~1.4 s on CI.
Pass the query count per data set: 150 for the 8 KB budget and 1000 for
the 64 KB object cache budget (682 entries), so each loop still runs
well past its budget before the eviction checks. Locally the database
data set drops from ~600 ms to ~230 ms.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Delete Analytics stats in two batches from three orders
test_delete_stats created 25 full orders, a count from 2019 that no
assertion depends on, and takes ~1.2 s on CI. With the default delete
batch size of 10 it did exercise a multi-batch delete.
Create three orders and set the delete batch size to two through the
woocommerce_analytics_regenerate_batch_size filter, so deleting still
takes two batches. A mutation that queues a single delete batch fails
the test. Locally it drops from ~480 ms to ~215 ms.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Split the PayPal request URL test per tax setup
test_request_url ran every scenario for the live site and for three tax
setups in one method: 16 full orders, ~2.3 s on CI. Each
WC_Helper_Order::create_order() call also created its own throwaway
product for a line item the test removes straight away.
Create that product once per test and pass it to create_order(). Split
the method into a live-site test and a @testWith over the three sandbox
tax setups, so a failure names its setup. The same 544 assertions run.
The shared product is the real saving: locally the total drops from
~850 ms to ~700 ms. The split spreads the rest, so the slowest test is
now ~220 ms.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Run two Mini-Cart and FeaturePlugin tests in the main process
Each @runInSeparateProcess test boots WordPress again in a child
process, about 1.5 s locally. The slow-test report only sees the time
spent inside the child, 9-48 ms here, so the cost never shows up.
The Mini-Cart drawer test was isolated because an earlier test that
renders the cart or checkout page defines WOOCOMMERCE_CART or
WOOCOMMERCE_CHECKOUT, and render() then skips the overlay. The test
checks the overlay markup, not that guard, so it now builds the overlay
directly. It passes with WOOCOMMERCE_CART defined.
FeaturePlugin::includes() only adds hooks and reuses singletons that
already exist, and the hook snapshot restores those hooks after the
test. The Internal/Admin tests pass in random order with it in-process.
GeneratePatternTranslationsTest keeps its isolation: it loads the
WP-CLI vendor autoloader, which would defeat the WP_CLI mocks other
tests define behind class_exists() checks.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Keep the Mini-Cart drawer test in its own process
The previous commit built the Mini-Cart overlay directly through
reflection so the drawer test could run in the main process. Mutation
testing showed that loses a check: when render() stops building the
overlay on regular pages, the old test fails and the new one passes.
No test-only change can bring that back in the main process. Once an
earlier test defines WOOCOMMERCE_CART, is_cart() stays true for the rest
of the run, and MiniCart calls is_cart() directly. Restore the separate
process until MiniCart routes those checks through LegacyProxy, which a
follow-up PR will do.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Correct the in-process count in the changelog comment
The changelog comment said two tests now run in the main process.
Commit 15db1fa7aed put the Mini-Cart drawer test back in its own
process, so only the FeaturePlugin export test moved.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Seed the variation gallery migration rows with the shared helper
MigrationTest::create_variation_posts() bulk-inserted bare variation
posts and looked their IDs up by a unique title. That duplicated
WC_Helper_Product::insert_product_post_rows(), which this branch adds
for the same purpose.
Call the helper for the post rows and keep the test's own legacy
gallery meta insert. The migration only joins on post_type, and the
decoy post has no post_name, so the helper's lookup still excludes it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Assert the related cache loop evicts its own oldest query
test_related_product_cache_has_size_limit now runs 150 queries for the
8 KB database budget instead of 1000. Nothing checked that the loop
still overflows the budget: if cache entries shrank enough, every
query would fit, nothing would be evicted, and the test would still
pass.
Record the key of the first query the loop caches and assert it is
gone after the loop. Cutting the loops to 60 and 500 queries, below
the 85 and 682 entries each budget holds, fails both data sets.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Share the missed sale end meta between the product helpers
create_missed_sale_end_product_ids() hardcoded the same sale state
that create_missed_sale_end_product() builds through CRUD: regular
price 100, sale price 50, a stored price of 50, and a sale that
started 300 s and ended 100 s ago. Both describe one fixture, so a
change to the offsets or prices in one helper would leave the other
behind, and tests using the bulk helper would quietly stop matching
tests using the single-product one.
Move the values into one private method that both helpers read.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/test-speed-up-slow-phpunit-tests b/plugins/woocommerce/changelog/test-speed-up-slow-phpunit-tests
new file mode 100644
index 00000000000..1a3789ff53e
--- /dev/null
+++ b/plugins/woocommerce/changelog/test-speed-up-slow-phpunit-tests
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+Comment: Speed up the slowest PHPUnit tests and run one of them in-process; no production change.
+
diff --git a/plugins/woocommerce/tests/legacy/framework/helpers/class-wc-helper-product.php b/plugins/woocommerce/tests/legacy/framework/helpers/class-wc-helper-product.php
index 2d6cba22550..94a7adbc8df 100644
--- a/plugins/woocommerce/tests/legacy/framework/helpers/class-wc-helper-product.php
+++ b/plugins/woocommerce/tests/legacy/framework/helpers/class-wc-helper-product.php
@@ -5,6 +5,7 @@
* @package WooCommerce\Tests
*/
+use Automattic\WooCommerce\Enums\ProductStatus;
use Automattic\WooCommerce\Enums\ProductStockStatus;
use Automattic\WooCommerce\Enums\ProductTaxStatus;
@@ -77,18 +78,95 @@ class WC_Helper_Product {
* @return WC_Product
*/
public static function create_missed_sale_end_product() {
+ $meta = self::get_missed_sale_end_meta();
$product = self::create_simple_product();
- $product->set_regular_price( '100' );
- $product->set_sale_price( '50' );
+ $product->set_regular_price( $meta['_regular_price'] );
+ $product->set_sale_price( $meta['_sale_price'] );
$product->save();
- update_post_meta( $product->get_id(), '_price', 50 );
- update_post_meta( $product->get_id(), '_sale_price_dates_from', time() - 300 );
- update_post_meta( $product->get_id(), '_sale_price_dates_to', time() - 100 );
+ foreach ( array( '_price', '_sale_price_dates_from', '_sale_price_dates_to' ) as $key ) {
+ update_post_meta( $product->get_id(), $key, $meta[ $key ] );
+ }
return $product;
}
+ /**
+ * Get the price and sale date meta of a product whose sale has ended while its stored price is still the sale price.
+ *
+ * @return array<string, string|int> Meta values keyed by meta key.
+ */
+ private static function get_missed_sale_end_meta(): array {
+ return array(
+ '_regular_price' => '100',
+ '_sale_price' => '50',
+ '_price' => '50',
+ '_sale_price_dates_from' => time() - 300,
+ '_sale_price_dates_to' => time() - 100,
+ );
+ }
+
+ /**
+ * Create simple products whose sale has ended, writing only the rows the scheduled sales query reads.
+ *
+ * Skips the product save cascade, so batch-boundary tests can create many of them cheaply.
+ *
+ * @param int $count Number of products to create.
+ * @return int[] The product IDs, in ascending order.
+ * @throws RuntimeException When the rows cannot be inserted.
+ */
+ public static function create_missed_sale_end_product_ids( int $count ): array {
+ global $wpdb;
+
+ $ids = self::insert_product_post_rows( $count );
+ $meta = self::get_missed_sale_end_meta();
+
+ $values = array();
+ foreach ( $ids as $id ) {
+ foreach ( $meta as $key => $value ) {
+ $values[] = $wpdb->prepare( '(%d, %s, %s)', $id, $key, $value );
+ }
+ }
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared -- Each row is prepared above.
+ if ( count( $values ) !== $wpdb->query( "INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) VALUES " . implode( ', ', $values ) ) ) {
+ throw new RuntimeException( esc_html( 'Could not insert the sale meta rows: ' . $wpdb->last_error ) );
+ }
+
+ return $ids;
+ }
+
+ /**
+ * Insert bare product post rows in one query, for tests of code that only reads the posts table.
+ *
+ * The rows have no meta, terms or lookup data.
+ *
+ * @param int $count Number of rows to insert.
+ * @param string $post_type Post type of the rows.
+ * @return int[] The new post IDs, in ascending order.
+ * @throws RuntimeException When the rows cannot be inserted.
+ */
+ public static function insert_product_post_rows( int $count, string $post_type = 'product' ): array {
+ global $wpdb;
+
+ $name_prefix = 'product-post-row-' . wp_generate_password( 8, false ) . '-';
+ $values = array();
+ for ( $i = 0; $i < $count; $i++ ) {
+ $values[] = $wpdb->prepare( "(%s, %s, 'Product post row', '', '', '', '', '', %s)", $post_type, ProductStatus::PUBLISH, $name_prefix . $i );
+ }
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared -- Each row is prepared above.
+ $inserted = $wpdb->query( "INSERT INTO {$wpdb->posts} (post_type, post_status, post_title, post_content, post_excerpt, to_ping, pinged, post_content_filtered, post_name) VALUES " . implode( ', ', $values ) );
+ if ( $count !== $inserted ) {
+ throw new RuntimeException( esc_html( 'Could not insert the product post rows: ' . $wpdb->last_error ) );
+ }
+
+ return array_map(
+ 'intval',
+ $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_name LIKE %s ORDER BY ID", $wpdb->esc_like( $name_prefix ) . '%' ) )
+ );
+ }
+
/**
* Create a downloadable product.
*
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/gateways/paypal/request.php b/plugins/woocommerce/tests/legacy/unit-tests/gateways/paypal/request.php
index 62516bcde4a..68bf1144bf0 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/gateways/paypal/request.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/gateways/paypal/request.php
@@ -24,6 +24,13 @@ class WC_Tests_Paypal_Gateway_Request extends WC_Unit_Test_Case {
*/
protected $order;
+ /**
+ * Product for the line item each new order starts with, shared so that every order does not create its own.
+ *
+ * @var WC_Product
+ */
+ protected $order_product;
+
/**
* Create $product_count simple products and store them in $this->products.
*
@@ -89,6 +96,7 @@ class WC_Tests_Paypal_Gateway_Request extends WC_Unit_Test_Case {
$this->paypal_gateway = new WC_Gateway_Paypal();
$this->paypal_request = new WC_Gateway_Paypal_Request( $this->paypal_gateway );
+ $this->order_product = WC_Helper_Product::create_simple_product();
}
@@ -106,7 +114,7 @@ class WC_Tests_Paypal_Gateway_Request extends WC_Unit_Test_Case {
// Create products.
$this->create_products( $product_count );
- $this->order = WC_Helper_Order::create_order();
+ $this->order = WC_Helper_Order::create_order( 1, $this->order_product );
$this->add_products_to_order( $this->order, $product_prices );
// Set payment method to Paypal.
@@ -221,7 +229,7 @@ class WC_Tests_Paypal_Gateway_Request extends WC_Unit_Test_Case {
* @param bool $testmode Whether to use Paypal sandbox.
*/
protected function check_product_title_containing_html( $testmode ) {
- $order = WC_Helper_Order::create_order();
+ $order = WC_Helper_Order::create_order( 1, $this->order_product );
foreach ( $order->get_items() as $item ) {
$order->remove_item( $item->get_id() );
@@ -348,65 +356,60 @@ class WC_Tests_Paypal_Gateway_Request extends WC_Unit_Test_Case {
}
/**
- * Test for request_url() method.
+ * Test for request_url() method on the live PayPal site.
*
* @group timeout
* @throws WC_Data_Exception Exception on failure.
*/
- public function test_request_url() {
- // User set up.
+ public function test_request_url_without_sandbox() {
$this->login_as_administrator();
-
- // wc_tax_enabled(), wc_prices_include_tax() and WC_Gateway_Paypal_Request::prepare_line_items() determine if
- // shipping tax should be included, these are the correct options.
- // Note that prepare_line_items() can return false in 2 cases, tested separately below:
- // - order totals mismatch and
- // - item amount < 0.
- $correct_options = array(
- // woocommerce_calc_taxes, woocommerce_prices_include_tax, $shipping_tax_included values.
- array( 'no', 'no', false ),
- array( 'yes', 'no', false ),
- // phpcs:ignore Squiz.PHP.CommentedOutCode.Found
- // array( 'no', 'yes', false ), // this is not a valid option due to definition of wc_prices_include_tax().
- array( 'yes', 'yes', true ),
- );
-
- // One test without sandbox.
- $testmode = false;
update_option( 'woocommerce_calc_taxes', 'no' );
update_option( 'woocommerce_prices_include_tax', 'no' );
- $shipping_tax_included = false;
- $this->check_small_order( 5, $shipping_tax_included, $testmode );
-
- // Other tests with sandbox active.
- $testmode = true;
- foreach ( $correct_options as $values ) {
- update_option( 'woocommerce_calc_taxes', $values[0] );
- update_option( 'woocommerce_prices_include_tax', $values[1] );
- $shipping_tax_included = $values[2];
- // Test order with < 9 items (URL shorter than limit).
- $this->check_small_order( 5, $shipping_tax_included, $testmode );
+ $this->check_small_order( 5, false, false );
+ }
- // Test order with >9 items with URL shorter than limit.
- $this->check_small_order( 11, $shipping_tax_included, $testmode );
+ /**
+ * Test for request_url() method in the sandbox, for each tax setup.
+ *
+ * wc_tax_enabled(), wc_prices_include_tax() and WC_Gateway_Paypal_Request::prepare_line_items() determine if
+ * shipping tax should be included. Taxes off with prices including tax is not a valid setup, see wc_prices_include_tax().
+ * prepare_line_items() also returns false on an order totals mismatch or a negative item amount, tested separately below.
+ *
+ * @group timeout
+ * @testWith ["no", "no", false]
+ * ["yes", "no", false]
+ * ["yes", "yes", true]
+ *
+ * @param string $calc_taxes The woocommerce_calc_taxes option.
+ * @param string $prices_include_tax The woocommerce_prices_include_tax option.
+ * @param bool $shipping_tax_included Whether the shipping tax should be included.
+ * @throws WC_Data_Exception Exception on failure.
+ */
+ public function test_request_url( string $calc_taxes, string $prices_include_tax, bool $shipping_tax_included ) {
+ $this->login_as_administrator();
+ update_option( 'woocommerce_calc_taxes', $calc_taxes );
+ update_option( 'woocommerce_prices_include_tax', $prices_include_tax );
+ $testmode = true;
- // Test order with URL longer than limit.
- // Many items in order -> forced to use one line item -> shipping tax included.
- $this->check_large_order( true, $testmode );
+ // Test order with < 9 items (URL shorter than limit).
+ $this->check_small_order( 5, $shipping_tax_included, $testmode );
- // Test removing tags from line item name.
- $this->check_product_title_containing_html( $testmode );
+ // Test order with >9 items with URL shorter than limit.
+ $this->check_small_order( 11, $shipping_tax_included, $testmode );
- // Test amount < 0.
- $this->check_negative_amount( $testmode );
+ // Test order with URL longer than limit.
+ // Many items in order -> forced to use one line item -> shipping tax included.
+ $this->check_large_order( true, $testmode );
- // Check order totals mismatch.
- $this->check_totals_mismatch( $testmode );
+ // Test removing tags from line item name.
+ $this->check_product_title_containing_html( $testmode );
- }
+ // Test amount < 0.
+ $this->check_negative_amount( $testmode );
+ // Check order totals mismatch.
+ $this->check_totals_mismatch( $testmode );
}
-
}
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-import.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-import.php
index 3b438d0fdf5..15029db56f6 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-import.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-import.php
@@ -271,12 +271,23 @@ class WC_Admin_Tests_API_Reports_Import extends WC_REST_Unit_Test_Case {
$product->set_regular_price( 25 );
$product->save();
- for ( $i = 0; $i < 25; $i++ ) {
+ for ( $i = 0; $i < 3; $i++ ) {
$order = WC_Helper_Order::create_order( 1, $product );
$order->set_status( OrderStatus::COMPLETED );
$order->save();
}
+ // Delete the orders in two batches, as a larger store would.
+ add_filter(
+ 'woocommerce_analytics_regenerate_batch_size',
+ function ( $batch_size, $scheduler, $action ) {
+ unset( $scheduler );
+ return 'delete' === $action ? 2 : $batch_size;
+ },
+ 10,
+ 3
+ );
+
// Check that stats exist before deleting.
WC_Helper_Queue::run_all_pending( 'wc-admin-data' );
@@ -286,7 +297,7 @@ class WC_Admin_Tests_API_Reports_Import extends WC_REST_Unit_Test_Case {
$reports = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
- $this->assertCount( 25, $reports );
+ $this->assertCount( 3, $reports );
$request = new WP_REST_Request( 'GET', '/wc-analytics/reports/customers' );
$request->set_query_params( array( 'per_page' => 25 ) );
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-system-status-tools-v2-controller-test.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-system-status-tools-v2-controller-test.php
index c7d2cbf6649..74499062acb 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-system-status-tools-v2-controller-test.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-system-status-tools-v2-controller-test.php
@@ -217,7 +217,7 @@ class WC_REST_System_Status_Tools_V2_Controller_Test extends WC_REST_Unit_Test_C
*/
public function test_clear_transients_clears_product_and_analytics_caches( bool $external_cache, bool $fail_enqueue ): void {
$batch_size = ProductUtil::TRANSIENT_CLEANUP_BATCH_SIZE;
- $ids = $this->factory->post->create_many( $batch_size + 1, array( 'post_type' => 'product' ) );
+ $ids = WC_Helper_Product::insert_product_post_rows( $batch_size + 1 );
$variation_id = $this->factory->post->create( array( 'post_type' => 'product_variation' ) );
$previous = wp_using_ext_object_cache();
wp_using_ext_object_cache( $external_cache );
diff --git a/plugins/woocommerce/tests/php/includes/wc-product-functions-test.php b/plugins/woocommerce/tests/php/includes/wc-product-functions-test.php
index bf2e503b5ad..d07cc23681d 100644
--- a/plugins/woocommerce/tests/php/includes/wc-product-functions-test.php
+++ b/plugins/woocommerce/tests/php/includes/wc-product-functions-test.php
@@ -1721,13 +1721,14 @@ class WC_Product_Functions_Tests extends \WC_Unit_Test_Case {
/**
* @testdox Related caches trim old variants and keep two large alternating queries cacheable.
- * @testWith [false, 8192]
- * [true, 65536]
+ * @testWith [false, 8192, 150]
+ * [true, 65536, 1000]
*
* @param bool $external_cache Whether transients use the object cache.
* @param int $budget Cache size target.
+ * @param int $queries Distinct queries to cache, enough to run past the budget (85 and 682 entries fit).
*/
- public function test_related_product_cache_has_size_limit( bool $external_cache, int $budget ): void {
+ public function test_related_product_cache_has_size_limit( bool $external_cache, int $budget, int $queries ): void {
$product_id = $this->factory->post->create( array( 'post_type' => 'product' ) );
$related_id = $this->factory->post->create( array( 'post_type' => 'product' ) );
$category = $this->factory->term->create( array( 'taxonomy' => 'product_cat' ) );
@@ -1747,13 +1748,16 @@ class WC_Product_Functions_Tests extends \WC_Unit_Test_Case {
$this->assertLessThanOrEqual( $budget, strlen( maybe_serialize( $cached ) ), 'An oversized pre-upgrade cache must be trimmed.' );
$this->assertArrayHasKey( 'legacy=19999', $cached, 'The newest pre-upgrade queries should remain cached.' );
- $min_count = PHP_INT_MAX;
- for ( $context = 0; $context < 1000; ++$context ) {
+ $min_count = PHP_INT_MAX;
+ $first_query = null;
+ for ( $context = 0; $context < $queries; ++$context ) {
wc_get_related_products( $product_id, 5, array( 100000 + $context ) );
- if ( $context >= 500 ) {
+ $first_query = $first_query ?? array_key_last( get_transient( $key ) );
+ if ( $context >= $queries / 2 ) {
$min_count = min( $min_count, count( get_transient( $key ) ) );
}
}
+ $this->assertArrayNotHasKey( $first_query, get_transient( $key ), 'The loop must run past the budget so its own oldest query is evicted.' );
$this->assertLessThanOrEqual( $budget, strlen( maybe_serialize( get_transient( $key ) ) ) );
$this->assertGreaterThan( 2, $min_count, 'Only the oldest queries should be evicted.' );
foreach ( array( 100000, 200000 ) as $exclude_start ) {
@@ -3063,11 +3067,7 @@ class WC_Product_Functions_Tests extends \WC_Unit_Test_Case {
* @testdox Every product is processed when the backlog spans more than one batch.
*/
public function test_wc_scheduled_sales_processes_every_product_across_batches(): void {
- $ids = array();
- for ( $i = 0; $i < 51; $i++ ) {
- $product = WC_Helper_Product::create_missed_sale_end_product();
- $ids[] = $product->get_id();
- }
+ $ids = WC_Helper_Product::create_missed_sale_end_product_ids( 51 );
$before_payloads = array();
$after_payloads = array();
@@ -3110,12 +3110,7 @@ class WC_Product_Functions_Tests extends \WC_Unit_Test_Case {
* @testdox Priming happens per batch, not once for the whole backlog.
*/
public function test_wc_scheduled_sales_primes_one_batch_at_a_time(): void {
- $ids = array();
-
- for ( $i = 0; $i < 51; $i++ ) {
- $product = WC_Helper_Product::create_missed_sale_end_product();
- $ids[] = $product->get_id();
- }
+ WC_Helper_Product::create_missed_sale_end_product_ids( 51 );
$queued = array();
add_action(
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/FeaturePluginTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/FeaturePluginTest.php
index d97e4edf501..7996a249d2e 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/FeaturePluginTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/FeaturePluginTest.php
@@ -143,8 +143,6 @@ class FeaturePluginTest extends WC_Unit_Test_Case {
/**
* @testdox Serving and expiring existing exports stays enabled when Analytics is disabled.
- * @runInSeparateProcess
- * @preserveGlobalState disabled
*/
public function test_export_files_are_still_handled_when_analytics_is_disabled(): void {
$export_hook = ReportExporter::get_action( 'export_report' );
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/ImportExport/HposOrderExportHandlerTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/ImportExport/HposOrderExportHandlerTest.php
index 85500c5500b..9144fd99824 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/ImportExport/HposOrderExportHandlerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/ImportExport/HposOrderExportHandlerTest.php
@@ -130,7 +130,8 @@ class HposOrderExportHandlerTest extends WC_Unit_Test_Case {
$expected_ids = array( (string) $mirrored->get_id() );
$batch_size = ( new ReflectionClassConstant( HposOrderExportHandler::class, 'BATCH_SIZE' ) )->getValue();
for ( $i = 0; $i <= $batch_size; ++$i ) {
- $expected_ids[] = (string) $this->create_order()->get_id();
+ // Pending orders skip the status-transition work a processing order triggers on save.
+ $expected_ids[] = (string) $this->create_order( OrderStatus::PENDING )->get_id();
}
$legacy_status_id = (int) end( $expected_ids );
global $wpdb;
diff --git a/plugins/woocommerce/tests/php/src/Internal/Logging/OrderLogsCleanupTest.php b/plugins/woocommerce/tests/php/src/Internal/Logging/OrderLogsCleanupTest.php
index 31848534f5f..442c28f8f86 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Logging/OrderLogsCleanupTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Logging/OrderLogsCleanupTest.php
@@ -591,7 +591,7 @@ class OrderLogsCleanupTest extends \WC_Unit_Test_Case {
$order_count = OrderLogsCleanupHelper::MAX_ORDERS_PER_RUN + $extra;
for ( $i = 0; $i < $order_count; $i++ ) {
- $order = wc_create_order();
+ $order = new \WC_Order();
$order->set_date_created( strtotime( '-5 days' ) );
$order->add_meta_data( '_debug_log_source', 'place-order-debug-drain-' . $i, true );
$order->save();
@@ -638,7 +638,7 @@ class OrderLogsCleanupTest extends \WC_Unit_Test_Case {
// Two full batches, so the second run still has a batch left to hand over.
for ( $i = 0; $i < 2 * OrderLogsCleanupHelper::MAX_ORDERS_PER_RUN; $i++ ) {
- $order = wc_create_order();
+ $order = new \WC_Order();
$order->set_date_created( strtotime( '-5 days' ) );
$order->add_meta_data( '_debug_log_source', 'place-order-debug-running-' . $i, true );
$order->save();
diff --git a/plugins/woocommerce/tests/php/src/Internal/ScheduledSaleRunTest.php b/plugins/woocommerce/tests/php/src/Internal/ScheduledSaleRunTest.php
index 07f81c6609a..715009701f6 100644
--- a/plugins/woocommerce/tests/php/src/Internal/ScheduledSaleRunTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/ScheduledSaleRunTest.php
@@ -329,11 +329,11 @@ class ScheduledSaleRunTest extends WC_Unit_Test_Case {
*/
public function test_processes_a_duplicated_id_across_batches_once(): void {
$product = WC_Helper_Product::create_missed_sale_end_product();
- $rows = array( $product->get_id() );
- for ( $i = 1; $i < ScheduledSaleRun::BATCH_SIZE; $i++ ) {
- $rows[] = WC_Helper_Product::create_missed_sale_end_product()->get_id();
- }
- $rows[] = (string) $product->get_id();
+ $rows = array_merge(
+ array( $product->get_id() ),
+ WC_Helper_Product::create_missed_sale_end_product_ids( ScheduledSaleRun::BATCH_SIZE - 1 ),
+ array( (string) $product->get_id() )
+ );
$saves = 0;
add_action(
diff --git a/plugins/woocommerce/tests/php/src/Internal/Utilities/ProductUtilTest.php b/plugins/woocommerce/tests/php/src/Internal/Utilities/ProductUtilTest.php
index 4d097feb01a..887b0e4c6fd 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Utilities/ProductUtilTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Utilities/ProductUtilTest.php
@@ -86,7 +86,7 @@ class ProductUtilTest extends \WC_Unit_Test_Case {
* @testdox A product transient cleanup batch fails when it cannot schedule the next batch.
*/
public function test_cleanup_batch_fails_when_next_batch_cannot_be_scheduled(): void {
- $this->factory->post->create_many( ProductUtil::TRANSIENT_CLEANUP_BATCH_SIZE, array( 'post_type' => 'product' ) );
+ \WC_Helper_Product::insert_product_post_rows( ProductUtil::TRANSIENT_CLEANUP_BATCH_SIZE );
$previous = wp_using_ext_object_cache();
wp_using_ext_object_cache( true );
add_filter( 'pre_as_enqueue_async_action', '__return_zero' );
diff --git a/plugins/woocommerce/tests/php/src/Internal/VariationGallery/MigrationTest.php b/plugins/woocommerce/tests/php/src/Internal/VariationGallery/MigrationTest.php
index 781599fa93a..25e4619039c 100644
--- a/plugins/woocommerce/tests/php/src/Internal/VariationGallery/MigrationTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/VariationGallery/MigrationTest.php
@@ -138,30 +138,7 @@ class MigrationTest extends \WC_Unit_Test_Case {
private function create_variation_posts( int $count ): array {
global $wpdb;
- $post_title = 'Variation gallery migration batch fixture ' . wp_generate_uuid4();
- $post_placeholders = array_fill( 0, $count, '(%s, %s, %s)' );
- $post_values = array();
-
- for ( $index = 0; $index < $count; ++$index ) {
- array_push( $post_values, 'product_variation', 'publish', $post_title );
- }
-
- $posts_query = $wpdb->prepare(
- "INSERT INTO {$wpdb->posts} (post_type, post_status, post_title) VALUES " . implode( ', ', $post_placeholders ), // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQL.NotPrepared -- Placeholders are generated above.
- $post_values
- );
- $wpdb->query( $posts_query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query is prepared immediately above.
-
- $variation_ids = array_map(
- 'intval',
- $wpdb->get_col(
- $wpdb->prepare(
- "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND post_title = %s ORDER BY ID ASC",
- 'product_variation',
- $post_title
- )
- )
- );
+ $variation_ids = WC_Helper_Product::insert_product_post_rows( $count, 'product_variation' );
$this->assertCount( $count, $variation_ids );
$meta_placeholders = array_fill( 0, $count, '(%d, %s, %s)' );