Commit 02396d615f2 for woocommerce
commit 02396d615f28d9ec492a72c0e33fd7a6b9c702aa
Author: Ján Mikláš <neosinner@gmail.com>
Date: Fri Nov 27 12:31:12 2026 +0100
Share the scheduled scan test catalog (#71522)
* Share the scheduled scan test catalog
* Add a scheduled scan test changelog entry
diff --git a/plugins/woocommerce/changelog/dev-share-scheduled-scan-catalog b/plugins/woocommerce/changelog/dev-share-scheduled-scan-catalog
new file mode 100644
index 00000000000..ebd4e05866a
--- /dev/null
+++ b/plugins/woocommerce/changelog/dev-share-scheduled-scan-catalog
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Share catalog fixtures across scheduled readiness scan tests.
diff --git a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ScheduledScanBatchTest.php b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ScheduledScanBatchTest.php
new file mode 100644
index 00000000000..5a917b1cc1f
--- /dev/null
+++ b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ScheduledScanBatchTest.php
@@ -0,0 +1,210 @@
+<?php
+declare( strict_types = 1 );
+
+namespace Automattic\WooCommerce\Tests\Internal\AgentReadiness;
+
+use Automattic\WooCommerce\Internal\AgentReadiness\AgentReadiness;
+use Automattic\WooCommerce\Internal\AgentReadiness\ProductFailures;
+use Automattic\WooCommerce\Internal\AgentReadiness\ProductScan;
+use Automattic\WooCommerce\Internal\AgentReadiness\ScheduledScan;
+
+/**
+ * Tests that require a product catalog spanning two scan batches.
+ */
+class ScheduledScanBatchTest extends ScheduledScanTestCase {
+
+ /**
+ * IDs in the shared catalog, in creation order.
+ *
+ * @var int[]
+ */
+ private static $catalog_ids = array();
+
+ /**
+ * Create a read-only catalog that crosses the production batch boundary.
+ */
+ public static function wpSetUpBeforeClass(): void {
+ self::$catalog_ids = self::with_direct_product_attribute_lookup_updates(
+ static fn() => self::create_products( ProductScan::BATCH_SIZE + 1 )
+ );
+ }
+
+ /**
+ * Delete the class catalog through the normal product hooks.
+ */
+ public static function wpTearDownAfterClass(): void {
+ foreach ( self::$catalog_ids as $product_id ) {
+ wp_delete_post( $product_id, true );
+ }
+ self::$catalog_ids = array();
+ }
+
+ /**
+ * @testdox A scan tests one batch per action, then stores the same report a single pass gives.
+ */
+ public function test_scan_runs_one_batch_per_action_and_matches_a_single_pass(): void {
+ $single = wc_get_container()->get( AgentReadiness::class )->get_report();
+ $batches = array();
+ $count = static function ( array $product_ids ) use ( &$batches ): void {
+ $batches[] = count( $product_ids );
+ };
+ add_action( 'woocommerce_agent_readiness_scan_batch', $count );
+
+ $this->sut->run();
+ $this->assertTrue( $this->sut->is_pending(), 'A scan testing batches is pending' );
+ $runs = $this->run_batches();
+ remove_action( 'woocommerce_agent_readiness_scan_batch', $count );
+
+ $this->assertSame( array( ProductScan::BATCH_SIZE, 1 ), $batches, 'One batch per action' );
+ $this->assertSame( 3, $runs, 'Two batches, then the report' );
+ $this->assertFalse( $this->sut->is_pending() );
+ $this->assertFalse( as_has_scheduled_action( ScheduledScan::NEXT_HOOK ), 'No retry is left behind' );
+ $report = wc_get_container()->get( AgentReadiness::class )->get_cached_report();
+ $this->assertSame( ProductScan::BATCH_SIZE + 1, $report['products'] );
+ foreach ( array( 'products', 'available', 'channels', 'truncated' ) as $key ) {
+ $this->assertSame( $single[ $key ], $report[ $key ], $key );
+ }
+ $this->assertSame( array_column( $single['checks'], 'count', 'id' ), array_column( $report['checks'], 'count', 'id' ) );
+ }
+
+ /**
+ * @testdox A batch that ends early is tried again from where the last finished batch stopped.
+ */
+ public function test_stopped_batch_resumes(): void {
+ $this->sut->run();
+ $chain = get_option( AgentReadiness::SCAN_OPTION )['id'];
+ $this->sut->run_batch( $chain );
+ $crash = static function (): void {
+ throw new \RuntimeException( 'Timed out' );
+ };
+ add_action( 'woocommerce_agent_readiness_scan_batch', $crash );
+ // Action Scheduler has started the queued batch, so it is no longer pending.
+ as_unschedule_all_actions( ScheduledScan::NEXT_HOOK );
+
+ try {
+ $this->sut->run_batch( $chain );
+ $this->fail( 'The batch should have stopped' );
+ } catch ( \RuntimeException $e ) {
+ unset( $e );
+ } finally {
+ remove_action( 'woocommerce_agent_readiness_scan_batch', $crash );
+ }
+
+ $retry = as_next_scheduled_action( ScheduledScan::NEXT_HOOK, array( $chain ), ScheduledScan::GROUP );
+ $this->assertIsInt( $retry, 'A retry is queued' );
+ $this->assertGreaterThan( time() + ScheduledScan::RETRY_AFTER / 2, $retry, 'The retry waits for the stopped action to time out' );
+ $this->assertSame( ProductScan::BATCH_SIZE, get_option( AgentReadiness::SCAN_OPTION )['position'], 'The finished batch is kept' );
+
+ $this->run_batches();
+ $this->assertSame( ProductScan::BATCH_SIZE + 1, wc_get_container()->get( AgentReadiness::class )->get_cached_report()['products'] );
+ }
+
+ /**
+ * @testdox Giving up after a finished batch removes failure rows that batch wrote for products the last report never covered, and keeps rows a save wrote while the scan ran.
+ */
+ public function test_give_up_drops_failure_rows_the_last_report_did_not_cover(): void {
+ $ids = self::$catalog_ids;
+ update_option( AgentReadiness::REPORT_OPTION, array( 'products' => 7 ) );
+ $this->sut->run();
+ $chain = get_option( AgentReadiness::SCAN_OPTION )['id'];
+ $this->sut->run_batch( $chain );
+ // The scan walks newest products first, so the first batch covers the last id created, not $ids[0].
+ $covered = (int) end( $ids );
+ $this->assertNotEmpty( ProductFailures::stored( $covered ), 'The finished batch wrote failure rows' );
+ // The first product created is left for the next batch. A save checks it now.
+ $saved = (int) $ids[0];
+ $this->assertSame( array(), ProductFailures::stored( $saved ), 'The first batch did not reach it' );
+ ProductFailures::store( $saved, array() );
+
+ $crash = static function (): void {
+ throw new \RuntimeException( 'Fatal on the next batch' );
+ };
+ add_action( 'woocommerce_agent_readiness_scan_batch', $crash );
+ for ( $i = 0; $i < AgentReadiness::MAX_ATTEMPTS; $i++ ) {
+ try {
+ $this->sut->run_batch( $chain );
+ } catch ( \RuntimeException $e ) {
+ unset( $e );
+ }
+ }
+ remove_action( 'woocommerce_agent_readiness_scan_batch', $crash );
+ $this->sut->run_batch( $chain );
+
+ $this->assertFalse( get_option( AgentReadiness::SCAN_OPTION ) );
+ $this->assertSame( array(), ProductFailures::stored( $covered ), 'Rows the abandoned scan added are removed' );
+ $this->assertSame( array( ProductFailures::CHECKED ), ProductFailures::stored( $saved ), 'Rows the save wrote stay' );
+ $this->assertSame( 7, get_option( AgentReadiness::REPORT_OPTION )['products'] );
+ }
+
+ /**
+ * @testdox A new scan request during a scan starts over, and the replaced scan's actions do nothing.
+ */
+ public function test_new_request_restarts_the_scan(): void {
+ $this->sut->run();
+ $first = get_option( AgentReadiness::SCAN_OPTION )['id'];
+ $this->sut->run_batch( $first );
+
+ $this->sut->queue();
+ $this->assertTrue( as_has_scheduled_action( ScheduledScan::RUN_NOW_HOOK ), 'A request during a scan queues a new one' );
+ $this->sut->run();
+ $second = get_option( AgentReadiness::SCAN_OPTION );
+
+ $this->assertNotSame( $first, $second['id'] );
+ $this->assertSame( 0, $second['position'] );
+ $this->assertFalse( as_has_scheduled_action( ScheduledScan::NEXT_HOOK, array( $first ) ), 'The old scan has nothing left to run' );
+ $this->sut->run_batch( $first );
+ $this->assertSame( 0, get_option( AgentReadiness::SCAN_OPTION )['position'], 'The old scan changes nothing' );
+
+ $this->run_batches();
+ $this->assertSame( ProductScan::BATCH_SIZE + 1, wc_get_container()->get( AgentReadiness::class )->get_cached_report()['products'] );
+ }
+
+ /**
+ * @testdox A batch replaced mid-run does not store the failure rows it collected.
+ */
+ public function test_replaced_batch_does_not_store_failures(): void {
+ $ids = self::$catalog_ids;
+ $this->sut->run();
+ $first = get_option( AgentReadiness::SCAN_OPTION )['id'];
+ $covered = (int) end( $ids );
+
+ $replace = function (): void {
+ $this->sut->run();
+ };
+ add_action( 'woocommerce_agent_readiness_scan_batch', $replace );
+ $this->sut->run_batch( $first );
+ remove_action( 'woocommerce_agent_readiness_scan_batch', $replace );
+
+ $this->assertNotSame( $first, get_option( AgentReadiness::SCAN_OPTION )['id'] );
+ $this->assertSame( array(), ProductFailures::stored( $covered ), 'The replaced batch stored nothing' );
+ }
+
+ /**
+ * @testdox A scan replaced while its batch stores rows does not write its progress over the new scan.
+ */
+ public function test_scan_replaced_during_the_store_loop_keeps_the_new_progress(): void {
+ $this->sut->run();
+ $first = get_option( AgentReadiness::SCAN_OPTION )['id'];
+
+ $replaced = false;
+ $replace = function ( $meta_id, $object_id, $meta_key ) use ( &$replaced ): void {
+ unset( $meta_id, $object_id );
+ if ( $replaced || ProductFailures::META_KEY !== $meta_key ) {
+ return;
+ }
+ $replaced = true;
+ $this->sut->run();
+ };
+ add_action( 'added_post_meta', $replace, 10, 3 );
+ try {
+ $this->sut->run_batch( $first );
+ } finally {
+ remove_action( 'added_post_meta', $replace, 10 );
+ }
+
+ $state = get_option( AgentReadiness::SCAN_OPTION );
+ $this->assertTrue( $replaced );
+ $this->assertNotSame( $first, $state['id'] );
+ $this->assertSame( 0, $state['position'], 'The new scan has not tested a batch yet' );
+ }
+}
diff --git a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ScheduledScanTest.php b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ScheduledScanTest.php
index f789c9e3038..463292b3c35 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ScheduledScanTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ScheduledScanTest.php
@@ -10,45 +10,11 @@ use Automattic\WooCommerce\Internal\AgentReadiness\RemoteRules;
use Automattic\WooCommerce\Internal\AgentReadiness\ScheduledScan;
use Automattic\WooCommerce\Internal\Caches\ProductCache;
use WC_Helper_Product;
-use WC_Unit_Test_Case;
/**
* Tests for the background readiness scan.
*/
-class ScheduledScanTest extends WC_Unit_Test_Case {
-
- /**
- * The System Under Test.
- *
- * @var ScheduledScan
- */
- private $sut;
-
- /**
- * Set up test fixtures.
- */
- public function setUp(): void {
- parent::setUp();
- // Report tests stay offline: store output checks would load the test site's pages.
- StoreOutputChecksTest::block_requests();
- add_filter( 'woocommerce_agent_readiness_rules_url', '__return_empty_string' );
- delete_option( AgentReadiness::REPORT_OPTION );
- as_unschedule_all_actions( ScheduledScan::RUN_NOW_HOOK );
- as_unschedule_all_actions( ScheduledScan::DAILY_HOOK );
- as_unschedule_all_actions( ScheduledScan::NEXT_HOOK );
- delete_option( AgentReadiness::SCAN_OPTION );
- $this->sut = wc_get_container()->get( ScheduledScan::class );
- }
-
- /**
- * Tear down test fixtures.
- */
- public function tearDown(): void {
- as_unschedule_all_actions( ScheduledScan::RUN_NOW_HOOK );
- as_unschedule_all_actions( ScheduledScan::DAILY_HOOK );
- as_unschedule_all_actions( ScheduledScan::NEXT_HOOK );
- parent::tearDown();
- }
+class ScheduledScanTest extends ScheduledScanTestCase {
/**
* Ids of the pending actions for a hook.
@@ -237,7 +203,7 @@ class ScheduledScanTest extends WC_Unit_Test_Case {
* @testdox A catalog over the limit lists its best sellers first, most sales first, then the newest changes; one that fits keeps the newest change first.
*/
public function test_catalog_over_the_limit_lists_best_sellers_first(): void {
- $ids = $this->create_products( 3 );
+ $ids = self::create_products( 3 );
$hidden = WC_Helper_Product::create_simple_product();
$hidden->set_catalog_visibility( 'hidden' );
$hidden->save();
@@ -497,111 +463,11 @@ class ScheduledScanTest extends WC_Unit_Test_Case {
wp_cache_set( 'notoptions', $notoptions, 'options' );
}
- /**
- * Run the scan's batch actions as Action Scheduler would, until the scan stores its report.
- *
- * @return int Batch actions run.
- */
- private function run_batches(): int {
- $runs = 0;
- while ( $runs < 20 && is_array( get_option( AgentReadiness::SCAN_OPTION ) ) ) {
- $this->sut->run_batch( get_option( AgentReadiness::SCAN_OPTION )['id'] );
- ++$runs;
- }
- return $runs;
- }
-
- /**
- * Create published simple products quickly, without images, so they fail the image check.
- *
- * @param int $count Products to create.
- * @return array<int, int> Product ids.
- */
- private function create_products( int $count ): array {
- $ids = array();
- for ( $i = 0; $i < $count; $i++ ) {
- $id = wp_insert_post(
- array(
- 'post_type' => 'product',
- 'post_status' => 'publish',
- 'post_title' => 'Product ' . $i,
- )
- );
- wp_set_object_terms( $id, 'simple', 'product_type' );
- update_post_meta( $id, '_regular_price', '10' );
- update_post_meta( $id, '_price', '10' );
- $ids[] = $id;
- }
- return $ids;
- }
-
- /**
- * @testdox A scan tests one batch per action, then stores the same report a single pass gives.
- */
- public function test_scan_runs_one_batch_per_action_and_matches_a_single_pass(): void {
- $this->create_products( ProductScan::BATCH_SIZE + 1 );
- $single = wc_get_container()->get( AgentReadiness::class )->get_report();
- $batches = array();
- $count = static function ( array $product_ids ) use ( &$batches ): void {
- $batches[] = count( $product_ids );
- };
- add_action( 'woocommerce_agent_readiness_scan_batch', $count );
-
- $this->sut->run();
- $this->assertTrue( $this->sut->is_pending(), 'A scan testing batches is pending' );
- $runs = $this->run_batches();
- remove_action( 'woocommerce_agent_readiness_scan_batch', $count );
-
- $this->assertSame( array( ProductScan::BATCH_SIZE, 1 ), $batches, 'One batch per action' );
- $this->assertSame( 3, $runs, 'Two batches, then the report' );
- $this->assertFalse( $this->sut->is_pending() );
- $this->assertFalse( as_has_scheduled_action( ScheduledScan::NEXT_HOOK ), 'No retry is left behind' );
- $report = wc_get_container()->get( AgentReadiness::class )->get_cached_report();
- $this->assertSame( ProductScan::BATCH_SIZE + 1, $report['products'] );
- foreach ( array( 'products', 'available', 'channels', 'truncated' ) as $key ) {
- $this->assertSame( $single[ $key ], $report[ $key ], $key );
- }
- $this->assertSame( array_column( $single['checks'], 'count', 'id' ), array_column( $report['checks'], 'count', 'id' ) );
- }
-
- /**
- * @testdox A batch that ends early is tried again from where the last finished batch stopped.
- */
- public function test_stopped_batch_resumes(): void {
- $this->create_products( ProductScan::BATCH_SIZE + 1 );
- $this->sut->run();
- $chain = get_option( AgentReadiness::SCAN_OPTION )['id'];
- $this->sut->run_batch( $chain );
- $crash = static function (): void {
- throw new \RuntimeException( 'Timed out' );
- };
- add_action( 'woocommerce_agent_readiness_scan_batch', $crash );
- // Action Scheduler has started the queued batch, so it is no longer pending.
- as_unschedule_all_actions( ScheduledScan::NEXT_HOOK );
-
- try {
- $this->sut->run_batch( $chain );
- $this->fail( 'The batch should have stopped' );
- } catch ( \RuntimeException $e ) {
- unset( $e );
- } finally {
- remove_action( 'woocommerce_agent_readiness_scan_batch', $crash );
- }
-
- $retry = as_next_scheduled_action( ScheduledScan::NEXT_HOOK, array( $chain ), ScheduledScan::GROUP );
- $this->assertIsInt( $retry, 'A retry is queued' );
- $this->assertGreaterThan( time() + ScheduledScan::RETRY_AFTER / 2, $retry, 'The retry waits for the stopped action to time out' );
- $this->assertSame( ProductScan::BATCH_SIZE, get_option( AgentReadiness::SCAN_OPTION )['position'], 'The finished batch is kept' );
-
- $this->run_batches();
- $this->assertSame( ProductScan::BATCH_SIZE + 1, wc_get_container()->get( AgentReadiness::class )->get_cached_report()['products'] );
- }
-
/**
* @testdox A batch that fails every time ends the scan after MAX_ATTEMPTS, keeping the last report.
*/
public function test_batch_that_always_fails_gives_up(): void {
- $this->create_products( 1 );
+ self::create_products( 1 );
update_option( AgentReadiness::REPORT_OPTION, array( 'products' => 7 ) );
$this->sut->run();
$crash = static function (): void {
@@ -625,122 +491,11 @@ class ScheduledScanTest extends WC_Unit_Test_Case {
$this->assertSame( 7, get_option( AgentReadiness::REPORT_OPTION )['products'] );
}
- /**
- * @testdox Giving up after a finished batch removes failure rows that batch wrote for products the last report never covered, and keeps rows a save wrote while the scan ran.
- */
- public function test_give_up_drops_failure_rows_the_last_report_did_not_cover(): void {
- $ids = $this->create_products( ProductScan::BATCH_SIZE + 1 );
- update_option( AgentReadiness::REPORT_OPTION, array( 'products' => 7 ) );
- $this->sut->run();
- $chain = get_option( AgentReadiness::SCAN_OPTION )['id'];
- $this->sut->run_batch( $chain );
- // The scan walks newest products first, so the first batch covers the last id created, not $ids[0].
- $covered = (int) end( $ids );
- $this->assertNotEmpty( ProductFailures::stored( $covered ), 'The finished batch wrote failure rows' );
- // The first product created is left for the next batch. A save checks it now.
- $saved = (int) $ids[0];
- $this->assertSame( array(), ProductFailures::stored( $saved ), 'The first batch did not reach it' );
- ProductFailures::store( $saved, array() );
-
- $crash = static function (): void {
- throw new \RuntimeException( 'Fatal on the next batch' );
- };
- add_action( 'woocommerce_agent_readiness_scan_batch', $crash );
- for ( $i = 0; $i < AgentReadiness::MAX_ATTEMPTS; $i++ ) {
- try {
- $this->sut->run_batch( $chain );
- } catch ( \RuntimeException $e ) {
- unset( $e );
- }
- }
- remove_action( 'woocommerce_agent_readiness_scan_batch', $crash );
- $this->sut->run_batch( $chain );
-
- $this->assertFalse( get_option( AgentReadiness::SCAN_OPTION ) );
- $this->assertSame( array(), ProductFailures::stored( $covered ), 'Rows the abandoned scan added are removed' );
- $this->assertSame( array( ProductFailures::CHECKED ), ProductFailures::stored( $saved ), 'Rows the save wrote stay' );
- $this->assertSame( 7, get_option( AgentReadiness::REPORT_OPTION )['products'] );
- }
-
- /**
- * @testdox A new scan request during a scan starts over, and the replaced scan's actions do nothing.
- */
- public function test_new_request_restarts_the_scan(): void {
- $this->create_products( ProductScan::BATCH_SIZE + 1 );
- $this->sut->run();
- $first = get_option( AgentReadiness::SCAN_OPTION )['id'];
- $this->sut->run_batch( $first );
-
- $this->sut->queue();
- $this->assertTrue( as_has_scheduled_action( ScheduledScan::RUN_NOW_HOOK ), 'A request during a scan queues a new one' );
- $this->sut->run();
- $second = get_option( AgentReadiness::SCAN_OPTION );
-
- $this->assertNotSame( $first, $second['id'] );
- $this->assertSame( 0, $second['position'] );
- $this->assertFalse( as_has_scheduled_action( ScheduledScan::NEXT_HOOK, array( $first ) ), 'The old scan has nothing left to run' );
- $this->sut->run_batch( $first );
- $this->assertSame( 0, get_option( AgentReadiness::SCAN_OPTION )['position'], 'The old scan changes nothing' );
-
- $this->run_batches();
- $this->assertSame( ProductScan::BATCH_SIZE + 1, wc_get_container()->get( AgentReadiness::class )->get_cached_report()['products'] );
- }
-
- /**
- * @testdox A batch replaced mid-run does not store the failure rows it collected.
- */
- public function test_replaced_batch_does_not_store_failures(): void {
- $ids = $this->create_products( ProductScan::BATCH_SIZE + 1 );
- $this->sut->run();
- $first = get_option( AgentReadiness::SCAN_OPTION )['id'];
- $covered = (int) end( $ids );
-
- $replace = function (): void {
- $this->sut->run();
- };
- add_action( 'woocommerce_agent_readiness_scan_batch', $replace );
- $this->sut->run_batch( $first );
- remove_action( 'woocommerce_agent_readiness_scan_batch', $replace );
-
- $this->assertNotSame( $first, get_option( AgentReadiness::SCAN_OPTION )['id'] );
- $this->assertSame( array(), ProductFailures::stored( $covered ), 'The replaced batch stored nothing' );
- }
-
- /**
- * @testdox A scan replaced while its batch stores rows does not write its progress over the new scan.
- */
- public function test_scan_replaced_during_the_store_loop_keeps_the_new_progress(): void {
- $this->create_products( ProductScan::BATCH_SIZE + 1 );
- $this->sut->run();
- $first = get_option( AgentReadiness::SCAN_OPTION )['id'];
-
- $replaced = false;
- $replace = function ( $meta_id, $object_id, $meta_key ) use ( &$replaced ): void {
- unset( $meta_id, $object_id );
- if ( $replaced || ProductFailures::META_KEY !== $meta_key ) {
- return;
- }
- $replaced = true;
- $this->sut->run();
- };
- add_action( 'added_post_meta', $replace, 10, 3 );
- try {
- $this->sut->run_batch( $first );
- } finally {
- remove_action( 'added_post_meta', $replace, 10 );
- }
-
- $state = get_option( AgentReadiness::SCAN_OPTION );
- $this->assertTrue( $replaced );
- $this->assertNotSame( $first, $state['id'] );
- $this->assertSame( 0, $state['position'], 'The new scan has not tested a batch yet' );
- }
-
/**
* @testdox A scan that finds nothing changed writes no product meta.
*/
public function test_unchanged_scan_writes_no_product_meta(): void {
- $ids = $this->create_products( 2 );
+ $ids = self::create_products( 2 );
$this->sut->run();
$this->run_batches();
$this->assertSame( '', get_post_meta( $ids[0], ProductFailures::WRITTEN_META, true ), 'The scan records no write time' );
@@ -771,11 +526,11 @@ class ScheduledScanTest extends WC_Unit_Test_Case {
* @testdox A report that fails to store leaves the previous report's failure rows in place.
*/
public function test_failed_report_write_keeps_the_previous_rows(): void {
- $stale = $this->create_products( 1 )[0];
+ $stale = self::create_products( 1 )[0];
ProductFailures::store( $stale, array() );
$this->hide_product( $stale );
update_option( AgentReadiness::REPORT_OPTION, array( 'products' => 4 ) );
- $this->create_products( 1 );
+ self::create_products( 1 );
$this->sut->run();
$keep_old = static function ( $value, $old ) {
unset( $value );
@@ -796,8 +551,8 @@ class ScheduledScanTest extends WC_Unit_Test_Case {
* @testdox Rows saved after a product was skipped by its batch are still there when the scan finishes.
*/
public function test_rows_saved_after_a_skipped_product_stay(): void {
- $kept = $this->create_products( 1 )[0];
- $stale = $this->create_products( 1 )[0];
+ $kept = self::create_products( 1 )[0];
+ $stale = self::create_products( 1 )[0];
ProductFailures::store( $kept, array() );
ProductFailures::store( $stale, array() );
$this->hide_product( $stale );
@@ -822,7 +577,7 @@ class ScheduledScanTest extends WC_Unit_Test_Case {
* @testdox Giving up after the report is stored keeps the rows that report counts.
*/
public function test_give_up_after_the_report_is_stored_keeps_its_rows(): void {
- $product_id = $this->create_products( 1 )[0];
+ $product_id = self::create_products( 1 )[0];
$this->sut->run();
$this->run_batches();
$report = get_option( AgentReadiness::REPORT_OPTION );
@@ -852,7 +607,7 @@ class ScheduledScanTest extends WC_Unit_Test_Case {
* @testdox An empty product list from a successful query is an empty catalog, and a query error stops the scan.
*/
public function test_empty_product_list_follows_the_query(): void {
- $this->create_products( 1 );
+ self::create_products( 1 );
$selection = static function ( $posts, $query ) {
if ( ProductScan::PRODUCT_LIMIT + 1 !== (int) $query->get( 'posts_per_page' ) ) {
return $posts;
@@ -890,7 +645,7 @@ class ScheduledScanTest extends WC_Unit_Test_Case {
*/
public function test_failed_list_query_is_not_cached(): void {
global $wpdb;
- $ids = $this->create_products( 1 );
+ $ids = self::create_products( 1 );
$break = static function ( $sql ) {
return false !== strpos( (string) $sql, 'LIMIT 0, ' . ( ProductScan::PRODUCT_LIMIT + 1 ) ) ? $sql . ' BROKEN' : $sql;
};
@@ -937,7 +692,7 @@ class ScheduledScanTest extends WC_Unit_Test_Case {
* @testdox A selected product that still exists but fails to load stops the batch so prior rows stay, and a later batch can finish.
*/
public function test_product_load_failure_retries_and_keeps_prior_rows(): void {
- $product_id = $this->create_products( 1 )[0];
+ $product_id = self::create_products( 1 )[0];
ProductFailures::store( $product_id, array( 'missing_images' => array() ) );
$prior = ProductFailures::stored( $product_id );
@@ -975,7 +730,7 @@ class ScheduledScanTest extends WC_Unit_Test_Case {
* @testdox On the last attempt, a product that still fails to load is skipped so the scan finishes and its rows stay.
*/
public function test_product_load_failure_on_last_attempt_skips_and_keeps_rows(): void {
- $ids = $this->create_products( 2 );
+ $ids = self::create_products( 2 );
ProductFailures::store( $ids[0], array( 'missing_images' => array() ) );
$prior = ProductFailures::stored( $ids[0] );
@@ -1009,7 +764,7 @@ class ScheduledScanTest extends WC_Unit_Test_Case {
* @testdox A product deleted between listing and its batch is skipped without retrying the batch.
*/
public function test_deleted_product_is_skipped_without_retry(): void {
- $ids = $this->create_products( 2 );
+ $ids = self::create_products( 2 );
ProductFailures::store( $ids[0], array() );
$this->sut->run();
$chain = get_option( AgentReadiness::SCAN_OPTION )['id'];
diff --git a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ScheduledScanTestCase.php b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ScheduledScanTestCase.php
new file mode 100644
index 00000000000..75c36ee59b8
--- /dev/null
+++ b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ScheduledScanTestCase.php
@@ -0,0 +1,85 @@
+<?php
+declare( strict_types = 1 );
+
+namespace Automattic\WooCommerce\Tests\Internal\AgentReadiness;
+
+use Automattic\WooCommerce\Internal\AgentReadiness\AgentReadiness;
+use Automattic\WooCommerce\Internal\AgentReadiness\ScheduledScan;
+use WC_Unit_Test_Case;
+
+/**
+ * Shared setup for the scheduled readiness scan tests.
+ */
+abstract class ScheduledScanTestCase extends WC_Unit_Test_Case {
+
+ /**
+ * The System Under Test.
+ *
+ * @var ScheduledScan
+ */
+ protected $sut;
+
+ /**
+ * Set up test fixtures.
+ */
+ public function setUp(): void {
+ parent::setUp();
+ // Report tests stay offline: store output checks would load the test site's pages.
+ StoreOutputChecksTest::block_requests();
+ add_filter( 'woocommerce_agent_readiness_rules_url', '__return_empty_string' );
+ delete_option( AgentReadiness::REPORT_OPTION );
+ as_unschedule_all_actions( ScheduledScan::RUN_NOW_HOOK );
+ as_unschedule_all_actions( ScheduledScan::DAILY_HOOK );
+ as_unschedule_all_actions( ScheduledScan::NEXT_HOOK );
+ delete_option( AgentReadiness::SCAN_OPTION );
+ $this->sut = wc_get_container()->get( ScheduledScan::class );
+ }
+
+ /**
+ * Tear down test fixtures.
+ */
+ public function tearDown(): void {
+ as_unschedule_all_actions( ScheduledScan::RUN_NOW_HOOK );
+ as_unschedule_all_actions( ScheduledScan::DAILY_HOOK );
+ as_unschedule_all_actions( ScheduledScan::NEXT_HOOK );
+ parent::tearDown();
+ }
+
+ /**
+ * Run the scan's batch actions as Action Scheduler would, until the scan stores its report.
+ *
+ * @return int Batch actions run.
+ */
+ protected function run_batches(): int {
+ $runs = 0;
+ while ( $runs < 20 && is_array( get_option( AgentReadiness::SCAN_OPTION ) ) ) {
+ $this->sut->run_batch( get_option( AgentReadiness::SCAN_OPTION )['id'] );
+ ++$runs;
+ }
+ return $runs;
+ }
+
+ /**
+ * Create published simple products quickly, without images, so they fail the image check.
+ *
+ * @param int $count Products to create.
+ * @return array<int, int> Product ids.
+ */
+ protected static function create_products( int $count ): array {
+ $ids = array();
+ for ( $i = 0; $i < $count; $i++ ) {
+ $id = wp_insert_post(
+ array(
+ 'post_type' => 'product',
+ 'post_status' => 'publish',
+ 'post_title' => 'Product ' . $i,
+ )
+ );
+ wp_set_object_terms( $id, 'simple', 'product_type' );
+ update_post_meta( $id, '_regular_price', '10' );
+ update_post_meta( $id, '_price', '10' );
+ $ids[] = $id;
+ }
+ return $ids;
+ }
+}