Commit fa330aaa696 for woocommerce
commit fa330aaa69675b2e440d7e45c9fd4d1a802959a4
Author: Vlad Olaru <vlad.olaru@automattic.com>
Date: Sat Nov 14 09:29:28 2026 +0200
[tests] Enable fulfillments per test so its hooks stop leaking into later tests (#70906)
* test: Stop the logger spy filter leaking into later tests
LoggerSpyTrait added its woocommerce_logging_class filter in a @before
method, which PHPUnit runs before setUp(). WordPress's test case
snapshots all hooks once per process, in the first test's set_up(), and
restores that snapshot after every test. When a class using the trait
ran first, the spy filter joined the snapshot and came back after every
later test, so wc_get_logger() returned the first test's mock for the
rest of the run. With a random order that puts AnalyticsTest first,
WC_Order_Step_Logger_Functions_Test's debug-level test then finds no
log file and fails.
Override set_up() in the trait instead and add the spy after
parent::set_up(). The filter stays out of the snapshot, the normal
restore removes it, and the spy is still in place for the rest of the
test class's own setUp(), which NotificationsProcessorTests relies on.
The @before/@after methods and the explicit removal are no longer
needed.
Add a test that pins the timing: no spy filter before the base set-up,
spy in place right after it. It holds in any test order.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
* test: Register fulfillment hooks per test in ProvidersTest
ProvidersTest registered the fulfillments controller's hooks only in
setUpBeforeClass(). WordPress's test case restores its hook snapshot
after every test, so those hooks were gone after the first test unless
the class happened to run first in the process. In a normal suite run
the providers endpoint then returned an empty list, and the loop in
test_response_format_validation checked nothing: the class made 11
assertions in the suite against 473 when run alone.
Register the hooks in setUp() as well, and assert the providers list is
not empty before checking each provider's shape. The class-level setup
stays because it creates the fulfillments tables outside a test
transaction.
Add the changelog entry for this branch.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
* test: Check the logger spy filter where the base set-up starts
LoggerSpyTraitTest recorded whether the spy filter existed before its
own setUp() called parent::setUp(). That runs before the trait's
set_up() is entered, so a trait that added the filter inside set_up()
but ahead of parent::set_up() would still pass, while the filter would
again join WordPress's hook snapshot and leak.
Record it in an intermediate base class's set_up() instead, which runs
when the trait calls parent::set_up(): the last point before the
snapshot can be taken. With the filter moved ahead of parent::set_up()
in the trait, the previous test passed and this one fails.
Raised in review on #70849.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
* test: Fail logger spy tests that run without the spy
LoggerSpyTrait adds its spy from set_up(). A test class that defines
its own set_up() replaces the trait's, so no spy is installed: the
assert helpers then report a misleading "no entries" failure, and
assertNoErrorLogged() or a direct assertEmpty( $this->captured_logs )
pass whatever is logged. The trait now fails such tests with a message
that names the cause, from the helpers and from
assert_post_conditions(), which also covers direct reads of
captured_logs. No class overrides set_up() today; a throwaway class
that did passed both kinds of test before and fails both now.
LoggerSpyTraitTest only checked for the trait's own callback before
the base set-up, so a filter added there through a closure would leak
and still pass. It now checks that woocommerce_logging_class has no
callback at all at that point, which nothing in src/ or includes/
adds; the closure version now fails it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Enable fulfillments per test instead of per class
Fifteen fulfillments test classes turned the feature on and called
FulfillmentsController::initialize_fulfillments() in
setUpBeforeClass(). That call creates the fulfillments tables, which
has to happen outside a test's transaction, but it also registers the
hooks of every fulfillments class. WordPress snapshots hooks in the
first test's set_up() of the process and restores that snapshot after
every test, so the class-level hooks either vanished after the class's
first test or, when the class ran first in the process, stayed for the
whole run with the feature off.
With a fulfillments class run first, the leaked hooks auto-fulfil a
downloadable item on an order moving to processing, and break two
tests outside these classes: the order confirmation Totals block test
(FulfillmentsRenderer::maybe_read_fulfillments() returns null) and
OrderReviews\SchedulerTest::test_is_idempotent (the auto-fulfil
callback gets one argument).
FulfillmentsHelper now has create_tables(), called once per class,
which creates the tables without registering hooks, and
enable_fulfillments(), called per test after parent::setUp(), which
sets the flag and registers the hooks. The test transaction rolls back
the flag and the hook restore removes the hooks, so the class-level
flag save and restore goes away. create_tables() reaches the private
maybe_create_db_tables() through reflection, to avoid adding
production API for a test need; a rename fails loudly.
The FulfillmentsController::register() calls are dropped too: the
plugin already registers the controller at load time.
Refs #70854
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Keep the fulfillments privacy tests on their own registrations
enable_fulfillments() registers the container's FulfillmentsPrivacy.
FulfillmentsPrivacyTest builds its own instance and registers it in
the end-to-end tests, so two exporters and two erasers ran, and the
tests still passed with $this->sut->register() removed: the container's
hooks did the work. Set only the feature flag in setUp() instead, so
each test runs on the hooks it adds. With the registrations removed,
the three end-to-end tests now fail.
Refs #70854
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Move the remaining fulfillments tests to the shared helper
ShippingProvidersTest and TrackingNumbersTest enabled the feature per
test by hand and restored the flag in tearDown(), which the test
transaction already does. They now create the tables once per class
and call FulfillmentsHelper::enable_fulfillments(), like the other
fulfillments tests.
Also drop the register() calls left in four setUp() methods, which
re-added the callbacks enable_fulfillments() had just registered on
the same container instances, and note in the helper's docblock that
the shipping provider taxonomy stays registered.
Refs #70854
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Use a fresh renderer in the fulfillments hooks tests
The hooks tests took FulfillmentsRenderer from the container after
enabling fulfillments. Enabling the feature registers that same
container instance, so the HPOS column hooks the tests assert were
already in place before the test's own register() call. With HPOS on,
test_hooks passed even with that call removed.
Build a fresh renderer instead, as FulfillmentsPrivacyTest does with
its subject. The tests only check which hooks register() and
init_admin_hooks() add, so they need neither the feature flag nor the
fulfillments tables; drop both set-up calls.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/test-fulfillments-per-test-hooks b/plugins/woocommerce/changelog/test-fulfillments-per-test-hooks
new file mode 100644
index 00000000000..9ed3d5498ae
--- /dev/null
+++ b/plugins/woocommerce/changelog/test-fulfillments-per-test-hooks
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Enable fulfillments per test in the fulfillments tests so their hooks stop leaking; no production change.
diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/DataStore/FulfillmentsDataStoreHookTest.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/DataStore/FulfillmentsDataStoreHookTest.php
index fe443636755..426aeb53f98 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/DataStore/FulfillmentsDataStoreHookTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/DataStore/FulfillmentsDataStoreHookTest.php
@@ -5,6 +5,7 @@ namespace Automattic\WooCommerce\Tests\Admin\Features\Fulfillments\DataStore;
use Automattic\WooCommerce\Admin\Features\Fulfillments\DataStore\FulfillmentsDataStore;
use Automattic\WooCommerce\Admin\Features\Fulfillments\Fulfillment;
+use Automattic\WooCommerce\Tests\Admin\Features\Fulfillments\Helpers\FulfillmentsHelper;
use WC_Helper_Order;
use WC_Order;
use WC_Unit_Test_Case;
@@ -31,18 +32,7 @@ class FulfillmentsDataStoreHookTest extends WC_Unit_Test_Case {
*/
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $controller = wc_get_container()->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
- }
-
- /**
- * Runs after all the tests of the class.
- */
- public static function tearDownAfterClass(): void {
- update_option( 'woocommerce_feature_fulfillments_enabled', 'no' );
- parent::tearDownAfterClass();
+ FulfillmentsHelper::create_tables();
}
/**
@@ -50,6 +40,7 @@ class FulfillmentsDataStoreHookTest extends WC_Unit_Test_Case {
*/
public function setUp(): void {
parent::setUp();
+ FulfillmentsHelper::enable_fulfillments();
$this->store = new FulfillmentsDataStore();
$this->order = WC_Helper_Order::create_order( get_current_user_id() );
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 4b46332dd6e..d494f29a75c 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
@@ -6,6 +6,7 @@ namespace Automattic\WooCommerce\Tests\Admin\Features\Fulfillments\DataStore;
use Automattic\WooCommerce\Admin\Features\Fulfillments\DataStore\FulfillmentsDataStore;
use Automattic\WooCommerce\Admin\Features\Fulfillments\Fulfillment;
+use Automattic\WooCommerce\Tests\Admin\Features\Fulfillments\Helpers\FulfillmentsHelper;
use WC_Meta_Data;
/**
@@ -26,18 +27,7 @@ class FulfillmentsDataStoreTest extends \WC_Unit_Test_Case {
*/
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $controller = wc_get_container()->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
- }
-
- /**
- * Runs after all the tests of the class.
- */
- public static function tearDownAfterClass(): void {
- update_option( 'woocommerce_feature_fulfillments_enabled', 'no' );
- parent::tearDownAfterClass();
+ FulfillmentsHelper::create_tables();
}
/**
@@ -45,6 +35,7 @@ class FulfillmentsDataStoreTest extends \WC_Unit_Test_Case {
*/
public function setUp(): void {
parent::setUp();
+ FulfillmentsHelper::enable_fulfillments();
$this->data_store = wc_get_container()->get( FulfillmentsDataStore::class );
}
diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentOrderNotesTest.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentOrderNotesTest.php
index d0984627f4a..f4d574dc427 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentOrderNotesTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentOrderNotesTest.php
@@ -24,35 +24,12 @@ class FulfillmentOrderNotesTest extends \WC_Unit_Test_Case {
*/
private FulfillmentsManager $manager;
- /**
- * The original value of the fulfillments feature flag before tests.
- *
- * @var string|false
- */
- private static $original_fulfillments_enabled;
-
/**
* Set up the test environment.
*/
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
- self::$original_fulfillments_enabled = get_option( 'woocommerce_feature_fulfillments_enabled' );
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $controller = wc_get_container()->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
- }
-
- /**
- * Tear down the test environment.
- */
- public static function tearDownAfterClass(): void {
- if ( false === self::$original_fulfillments_enabled ) {
- delete_option( 'woocommerce_feature_fulfillments_enabled' );
- } else {
- update_option( 'woocommerce_feature_fulfillments_enabled', self::$original_fulfillments_enabled );
- }
- parent::tearDownAfterClass();
+ FulfillmentsHelper::create_tables();
}
/**
@@ -60,8 +37,8 @@ class FulfillmentOrderNotesTest extends \WC_Unit_Test_Case {
*/
public function setUp(): void {
parent::setUp();
+ FulfillmentsHelper::enable_fulfillments();
$this->manager = wc_get_container()->get( FulfillmentsManager::class );
- $this->manager->register();
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentTest.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentTest.php
index 7cdfd260173..3aecfe4b712 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentTest.php
@@ -11,35 +11,20 @@ use WC_Order;
*/
class FulfillmentTest extends \WC_Unit_Test_Case {
- /**
- * Original value of the fulfillments feature flag.
- *
- * @var mixed
- */
- private static $original_fulfillments_flag;
-
/**
* Set up the test environment.
*/
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
- self::$original_fulfillments_flag = get_option( 'woocommerce_feature_fulfillments_enabled' );
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $controller = wc_get_container()->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
+ FulfillmentsHelper::create_tables();
}
/**
- * Tear down the test environment.
+ * Turn the fulfillments feature on for each test.
*/
- public static function tearDownAfterClass(): void {
- if ( false === self::$original_fulfillments_flag ) {
- delete_option( 'woocommerce_feature_fulfillments_enabled' );
- } else {
- update_option( 'woocommerce_feature_fulfillments_enabled', self::$original_fulfillments_flag );
- }
- parent::tearDownAfterClass();
+ public function setUp(): void {
+ parent::setUp();
+ FulfillmentsHelper::enable_fulfillments();
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentUtilsTest.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentUtilsTest.php
index fe8cf11a602..806699b456a 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentUtilsTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentUtilsTest.php
@@ -25,35 +25,21 @@ class FulfillmentUtilsTest extends \WC_Unit_Test_Case {
}
);
}
- /**
- * Original value of the fulfillments feature flag.
- *
- * @var mixed
- */
- private static $original_fulfillments_flag;
/**
* Set up the test environment.
*/
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
- self::$original_fulfillments_flag = get_option( 'woocommerce_feature_fulfillments_enabled' );
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $controller = wc_get_container()->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
+ FulfillmentsHelper::create_tables();
}
/**
- * Tear down the test environment.
+ * Turn the fulfillments feature on for each test.
*/
- public static function tearDownAfterClass(): void {
- if ( false === self::$original_fulfillments_flag ) {
- delete_option( 'woocommerce_feature_fulfillments_enabled' );
- } else {
- update_option( 'woocommerce_feature_fulfillments_enabled', self::$original_fulfillments_flag );
- }
- parent::tearDownAfterClass();
+ public function setUp(): void {
+ parent::setUp();
+ FulfillmentsHelper::enable_fulfillments();
}
/**
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 2d1d49814d4..0475b150f12 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsManagerTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsManagerTest.php
@@ -19,35 +19,12 @@ class FulfillmentsManagerTest extends \WC_Unit_Test_Case {
*/
private FulfillmentsManager $manager;
- /**
- * Original value of the fulfillments feature flag.
- *
- * @var mixed
- */
- private static $original_fulfillments_flag;
-
/**
* Set up the test environment.
*/
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
- self::$original_fulfillments_flag = get_option( 'woocommerce_feature_fulfillments_enabled' );
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $controller = wc_get_container()->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
- }
-
- /**
- * Tear down the test environment.
- */
- public static function tearDownAfterClass(): void {
- if ( false === self::$original_fulfillments_flag ) {
- delete_option( 'woocommerce_feature_fulfillments_enabled' );
- } else {
- update_option( 'woocommerce_feature_fulfillments_enabled', self::$original_fulfillments_flag );
- }
- parent::tearDownAfterClass();
+ FulfillmentsHelper::create_tables();
}
/**
@@ -55,8 +32,8 @@ class FulfillmentsManagerTest extends \WC_Unit_Test_Case {
*/
public function setUp(): void {
parent::setUp();
+ FulfillmentsHelper::enable_fulfillments();
$this->manager = wc_get_container()->get( FulfillmentsManager::class );
- $this->manager->register();
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsPrivacyTest.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsPrivacyTest.php
index e6fff756191..5f82f95a148 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsPrivacyTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsPrivacyTest.php
@@ -25,34 +25,11 @@ class FulfillmentsPrivacyTest extends WC_Unit_Test_Case {
private $sut;
/**
- * Original value of the fulfillments feature flag.
- *
- * @var mixed
- */
- private static $original_fulfillments_flag;
-
- /**
- * Enable the fulfillments feature and register its data store.
+ * Create the fulfillments tables.
*/
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
- self::$original_fulfillments_flag = get_option( 'woocommerce_feature_fulfillments_enabled' );
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $controller = wc_get_container()->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
- }
-
- /**
- * Restore the fulfillments feature flag.
- */
- public static function tearDownAfterClass(): void {
- if ( false === self::$original_fulfillments_flag ) {
- delete_option( 'woocommerce_feature_fulfillments_enabled' );
- } else {
- update_option( 'woocommerce_feature_fulfillments_enabled', self::$original_fulfillments_flag );
- }
- parent::tearDownAfterClass();
+ FulfillmentsHelper::create_tables();
}
/**
@@ -60,6 +37,9 @@ class FulfillmentsPrivacyTest extends WC_Unit_Test_Case {
*/
public function setUp(): void {
parent::setUp();
+ // Only the flag, not enable_fulfillments(): it would hook the container's FulfillmentsPrivacy
+ // too, and the end-to-end tests would pass on those hooks instead of the ones $this->sut adds.
+ update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
$this->sut = new FulfillmentsPrivacy();
}
diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsRefundTest.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsRefundTest.php
index d907a3954e1..59924a75e0f 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsRefundTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsRefundTest.php
@@ -29,36 +29,12 @@ class FulfillmentsRefundTest extends \WC_Unit_Test_Case {
*/
private FulfillmentsManager $manager;
- /**
- * Original value of the fulfillments feature flag.
- *
- * @var mixed
- */
- private static $original_fulfillments_flag;
-
/**
* Set up the test environment.
*/
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
- self::$original_fulfillments_flag = get_option( 'woocommerce_feature_fulfillments_enabled' );
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $container = wc_get_container();
- $controller = $container->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
- }
-
- /**
- * Tear down the test environment.
- */
- public static function tearDownAfterClass(): void {
- if ( false === self::$original_fulfillments_flag ) {
- delete_option( 'woocommerce_feature_fulfillments_enabled' );
- } else {
- update_option( 'woocommerce_feature_fulfillments_enabled', self::$original_fulfillments_flag );
- }
- parent::tearDownAfterClass();
+ FulfillmentsHelper::create_tables();
}
/**
@@ -66,9 +42,9 @@ class FulfillmentsRefundTest extends \WC_Unit_Test_Case {
*/
public function setUp(): void {
parent::setUp();
+ FulfillmentsHelper::enable_fulfillments();
$this->data_store = wc_get_container()->get( FulfillmentsDataStore::class );
$this->manager = wc_get_container()->get( FulfillmentsManager::class );
- $this->manager->register();
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsRendererHooksTest.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsRendererHooksTest.php
index 94ad20f5cf6..14515678efa 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsRendererHooksTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsRendererHooksTest.php
@@ -17,43 +17,14 @@ class FulfillmentsRendererHooksTest extends \WC_Unit_Test_Case {
*/
private FulfillmentsRenderer $renderer;
- /**
- * Original value of the fulfillments feature flag.
- *
- * @var mixed
- */
- private static $original_fulfillments_flag;
-
- /**
- * Set up the test environment.
- */
- public static function setUpBeforeClass(): void {
- parent::setUpBeforeClass();
- self::$original_fulfillments_flag = get_option( 'woocommerce_feature_fulfillments_enabled' );
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $controller = wc_get_container()->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
- }
-
- /**
- * Tear down the test environment.
- */
- public static function tearDownAfterClass(): void {
- if ( false === self::$original_fulfillments_flag ) {
- delete_option( 'woocommerce_feature_fulfillments_enabled' );
- } else {
- update_option( 'woocommerce_feature_fulfillments_enabled', self::$original_fulfillments_flag );
- }
- parent::tearDownAfterClass();
- }
-
/**
* Set up the test case.
*/
public function setUp(): void {
parent::setUp();
- $this->renderer = wc_get_container()->get( FulfillmentsRenderer::class );
+ // A fresh instance, not the container's: enabling fulfillments registers the container's renderer, and
+ // these tests must pass on the hooks they register themselves. They need neither the feature nor its tables.
+ $this->renderer = new FulfillmentsRenderer();
}
/**
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 ccde9391b21..f2d088c1047 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsRendererTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsRendererTest.php
@@ -28,35 +28,12 @@ class FulfillmentsRendererTest extends \WC_Unit_Test_Case {
*/
private FulfillmentsRenderer $renderer;
- /**
- * Original value of the fulfillments feature flag.
- *
- * @var mixed
- */
- private static $original_fulfillments_flag;
-
/**
* Set up the test environment.
*/
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
- self::$original_fulfillments_flag = get_option( 'woocommerce_feature_fulfillments_enabled' );
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $controller = wc_get_container()->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
- }
-
- /**
- * Tear down the test environment.
- */
- public static function tearDownAfterClass(): void {
- if ( false === self::$original_fulfillments_flag ) {
- delete_option( 'woocommerce_feature_fulfillments_enabled' );
- } else {
- update_option( 'woocommerce_feature_fulfillments_enabled', self::$original_fulfillments_flag );
- }
- parent::tearDownAfterClass();
+ FulfillmentsHelper::create_tables();
}
/**
@@ -64,8 +41,8 @@ class FulfillmentsRendererTest extends \WC_Unit_Test_Case {
*/
public function setUp(): void {
parent::setUp();
+ FulfillmentsHelper::enable_fulfillments();
$this->renderer = wc_get_container()->get( FulfillmentsRenderer::class );
- $this->renderer->register();
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsSettingsTest.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsSettingsTest.php
index e4bff6ddfeb..5c2bd664b76 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsSettingsTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/FulfillmentsSettingsTest.php
@@ -4,6 +4,7 @@ namespace Automattic\WooCommerce\Tests\Admin\Features\Fulfillments;
use Automattic\WooCommerce\Admin\Features\Fulfillments\DataStore\FulfillmentsDataStore;
use Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsSettings;
+use Automattic\WooCommerce\Tests\Admin\Features\Fulfillments\Helpers\FulfillmentsHelper;
use WC_Order;
use WC_Order_Item_Product;
use WC_Unit_Test_Case;
@@ -15,35 +16,20 @@ use WC_Unit_Test_Case;
*/
class FulfillmentsSettingsTest extends WC_Unit_Test_Case {
- /**
- * Original value of the fulfillments feature flag.
- *
- * @var mixed
- */
- private static $original_fulfillments_flag;
-
/**
* Set up the test environment.
*/
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
- self::$original_fulfillments_flag = get_option( 'woocommerce_feature_fulfillments_enabled' );
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $controller = wc_get_container()->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
+ FulfillmentsHelper::create_tables();
}
/**
- * Tear down the test environment.
+ * Turn the fulfillments feature on for each test.
*/
- public static function tearDownAfterClass(): void {
- if ( false === self::$original_fulfillments_flag ) {
- delete_option( 'woocommerce_feature_fulfillments_enabled' );
- } else {
- update_option( 'woocommerce_feature_fulfillments_enabled', self::$original_fulfillments_flag );
- }
- parent::tearDownAfterClass();
+ public function setUp(): void {
+ parent::setUp();
+ FulfillmentsHelper::enable_fulfillments();
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/Helpers/FulfillmentsHelper.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/Helpers/FulfillmentsHelper.php
index 966b2c010e0..ad824d2de10 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/Helpers/FulfillmentsHelper.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/Helpers/FulfillmentsHelper.php
@@ -3,6 +3,7 @@
namespace Automattic\WooCommerce\Tests\Admin\Features\Fulfillments\Helpers;
use Automattic\WooCommerce\Admin\Features\Fulfillments\Fulfillment;
+use Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController;
use WC_Order;
/**
@@ -13,6 +14,36 @@ use WC_Order;
* @since 9.0.0
*/
class FulfillmentsHelper {
+ /**
+ * Create the fulfillments tables without registering any fulfillments hooks.
+ *
+ * Call it from setUpBeforeClass(): creating tables inside a test's transaction is not allowed.
+ * Hooks added there would stay for the whole run when the class runs first, so add them per test
+ * with enable_fulfillments() instead.
+ *
+ * @return void
+ */
+ public static function create_tables(): void {
+ $controller = wc_get_container()->get( FulfillmentsController::class );
+ $method = new \ReflectionMethod( $controller, 'maybe_create_db_tables' );
+ $method->setAccessible( true );
+ $method->invoke( $controller );
+ }
+
+ /**
+ * Turn the fulfillments feature on for the current test and register its hooks.
+ *
+ * Call it from setUp(), after parent::setUp(): the test transaction rolls back the option and the
+ * base tear-down removes the hooks. The shipping provider taxonomy it registers stays for the rest
+ * of the process. The tables must already exist (see create_tables()).
+ *
+ * @return void
+ */
+ public static function enable_fulfillments(): void {
+ update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
+ wc_get_container()->get( FulfillmentsController::class )->initialize_fulfillments();
+ }
+
/**
* Helper function to create a fulfillment.
*
diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/Importer/FulfillmentsCsvImporterTest.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/Importer/FulfillmentsCsvImporterTest.php
index 9ff0c0906d7..d2f996cdcf0 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/Importer/FulfillmentsCsvImporterTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/Importer/FulfillmentsCsvImporterTest.php
@@ -4,21 +4,14 @@ namespace Automattic\WooCommerce\Tests\Admin\Features\Fulfillments\Importer;
require_once __DIR__ . '/FulfillmentsCsvReadTracker.php';
-use Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController;
use Automattic\WooCommerce\Admin\Features\Fulfillments\Importer\FulfillmentsCsvImporter;
+use Automattic\WooCommerce\Tests\Admin\Features\Fulfillments\Helpers\FulfillmentsHelper;
/**
* Tests for the Fulfillments CSV importer service.
*/
class FulfillmentsCsvImporterTest extends \WC_Unit_Test_Case {
- /**
- * Original value of the fulfillments feature flag.
- *
- * @var mixed
- */
- private static $original_fulfillments_flag;
-
/**
* Paths to temporary CSV files created by tests; cleaned up in tearDown.
*
@@ -27,30 +20,22 @@ class FulfillmentsCsvImporterTest extends \WC_Unit_Test_Case {
private array $temp_files = array();
/**
- * Bootstrap the fulfillments feature for the test run.
+ * Register the CSV read tracker and create the fulfillments tables.
*/
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
if ( ! in_array( 'wcimp', stream_get_wrappers(), true ) ) {
stream_wrapper_register( 'wcimp', FulfillmentsCsvReadTracker::class );
}
- self::$original_fulfillments_flag = get_option( 'woocommerce_feature_fulfillments_enabled' );
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $controller = wc_get_container()->get( FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
+ FulfillmentsHelper::create_tables();
}
/**
- * Restore the original feature flag value.
+ * Turn the fulfillments feature on for each test.
*/
- public static function tearDownAfterClass(): void {
- if ( false === self::$original_fulfillments_flag ) {
- delete_option( 'woocommerce_feature_fulfillments_enabled' );
- } else {
- update_option( 'woocommerce_feature_fulfillments_enabled', self::$original_fulfillments_flag );
- }
- parent::tearDownAfterClass();
+ public function setUp(): void {
+ parent::setUp();
+ FulfillmentsHelper::enable_fulfillments();
}
/**
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 a9eabba07f0..2cc8d59f8ee 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/OrderFulfillmentsRestControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/OrderFulfillmentsRestControllerTest.php
@@ -60,18 +60,12 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {
*/
private static array $created_fulfillment_ids = array();
- /**
- * Original value of the fulfillments feature flag.
- *
- * @var mixed
- */
- private static $original_fulfillments_flag;
-
/**
* Setup test case.
*/
public function setUp(): void {
parent::setUp();
+ FulfillmentsHelper::enable_fulfillments();
$this->controller = new OrderFulfillmentsRestController();
$this->controller->register_routes();
@@ -84,11 +78,7 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {
parent::setupBeforeClass();
self::enable_direct_product_attribute_lookup_updates();
- self::$original_fulfillments_flag = get_option( 'woocommerce_feature_fulfillments_enabled' );
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $controller = wc_get_container()->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
+ FulfillmentsHelper::create_tables();
self::$created_user_id = wp_create_user( 'test_user', 'password', 'nonadmin@example.com' );
add_action( 'action_scheduler_stored_action', array( self::class, 'track_import_action' ) );
@@ -159,23 +149,14 @@ class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {
wp_delete_user( self::$created_user_id );
} finally {
try {
- if ( false === self::$original_fulfillments_flag ) {
- delete_option( 'woocommerce_feature_fulfillments_enabled' );
- } else {
- update_option( 'woocommerce_feature_fulfillments_enabled', self::$original_fulfillments_flag );
- }
+ parent::tearDownAfterClass();
} finally {
- try {
- parent::tearDownAfterClass();
- } finally {
- self::$created_order_ids = array();
- self::$created_import_action_ids = array();
- self::$created_fulfillment_ids = array();
- self::$created_user_id = -1;
- self::$customer_order_id = -1;
- self::$original_fulfillments_flag = null;
- self::disable_direct_product_attribute_lookup_updates();
- }
+ self::$created_order_ids = array();
+ self::$created_import_action_ids = array();
+ self::$created_fulfillment_ids = array();
+ self::$created_user_id = -1;
+ self::$customer_order_id = -1;
+ self::disable_direct_product_attribute_lookup_updates();
}
}
}
diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/ShippingProvidersTest.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/ShippingProvidersTest.php
index 363646aa381..d0088bb6dfb 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/ShippingProvidersTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/ShippingProvidersTest.php
@@ -4,6 +4,7 @@ namespace Automattic\WooCommerce\Tests\Admin\Features\Fulfillments;
use Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentUtils;
use Automattic\WooCommerce\Admin\Features\Fulfillments\Providers as ShippingProviders;
+use Automattic\WooCommerce\Tests\Admin\Features\Fulfillments\Helpers\FulfillmentsHelper;
/**
* ShippingProvidersTest class.
@@ -12,41 +13,19 @@ use Automattic\WooCommerce\Admin\Features\Fulfillments\Providers as ShippingProv
*/
class ShippingProvidersTest extends \WP_UnitTestCase {
- /**
- * Original value of the fulfillments feature flag.
- *
- * @var mixed
- */
- private $original_fulfillments_flag;
-
/**
* Set up the test environment.
*/
- public function setUp(): void {
- parent::setUp();
- $this->original_fulfillments_flag = get_option( 'woocommerce_feature_fulfillments_enabled' );
- }
-
- /**
- * Tear down the test environment.
- */
- public function tearDown(): void {
- if ( false === $this->original_fulfillments_flag ) {
- delete_option( 'woocommerce_feature_fulfillments_enabled' );
- } else {
- update_option( 'woocommerce_feature_fulfillments_enabled', $this->original_fulfillments_flag );
- }
- parent::tearDown();
+ public static function setUpBeforeClass(): void {
+ parent::setUpBeforeClass();
+ FulfillmentsHelper::create_tables();
}
/**
* Test that the shipping providers configuration returns the correct instances.
*/
public function test_shipping_providers_configuration(): void {
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $controller = wc_get_container()->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
+ FulfillmentsHelper::enable_fulfillments();
$shipping_providers = FulfillmentUtils::get_shipping_providers();
$this->assertNotEmpty( $shipping_providers, 'Expected at least one registered shipping provider.' );
diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/TrackingNumbersTest.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/TrackingNumbersTest.php
index 261d5c05fa2..77e62b02f3c 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/TrackingNumbersTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/TrackingNumbersTest.php
@@ -4,6 +4,7 @@ namespace Automattic\WooCommerce\Tests\Admin\Features\Fulfillments;
use Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsManager;
use Automattic\WooCommerce\Admin\Features\Fulfillments\Providers\TrackingCombinator;
+use Automattic\WooCommerce\Tests\Admin\Features\Fulfillments\Helpers\FulfillmentsHelper;
use WP_UnitTestCase;
/**
@@ -17,37 +18,22 @@ class TrackingNumbersTest extends WP_UnitTestCase {
private $combinator;
/**
- * Original value of the fulfillments feature flag.
- *
- * @var mixed
+ * Create the fulfillments tables.
*/
- private $original_fulfillments_flag;
+ public static function setUpBeforeClass(): void {
+ parent::setUpBeforeClass();
+ FulfillmentsHelper::create_tables();
+ }
/**
* Set up the combinator instance.
*/
public function setUp(): void {
parent::setUp();
- $this->original_fulfillments_flag = get_option( 'woocommerce_feature_fulfillments_enabled' );
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $controller = wc_get_container()->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
+ FulfillmentsHelper::enable_fulfillments();
$this->combinator = wc_get_container()->get( FulfillmentsManager::class );
}
- /**
- * Tear down the test environment.
- */
- public function tearDown(): void {
- if ( false === $this->original_fulfillments_flag ) {
- delete_option( 'woocommerce_feature_fulfillments_enabled' );
- } else {
- update_option( 'woocommerce_feature_fulfillments_enabled', $this->original_fulfillments_flag );
- }
- parent::tearDown();
- }
-
/**
* Data provider for all major shipping provider patterns.
*
diff --git a/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Fulfillments/ControllerTest.php b/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Fulfillments/ControllerTest.php
index 071d10bcff5..ee87ce3cfe5 100644
--- a/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Fulfillments/ControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Fulfillments/ControllerTest.php
@@ -25,13 +25,6 @@ class ControllerTest extends WC_Unit_Test_Case {
*/
private FulfillmentsController $controller;
- /**
- * Original value of the fulfillments feature flag.
- *
- * @var mixed
- */
- private static $original_fulfillments_flag;
-
/**
* Admin user for tests, shared across all tests in the class for REST authentication.
*
@@ -65,23 +58,7 @@ class ControllerTest extends WC_Unit_Test_Case {
*/
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
- self::$original_fulfillments_flag = get_option( 'woocommerce_feature_fulfillments_enabled' );
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $controller = wc_get_container()->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
- }
-
- /**
- * Tear down the test environment.
- */
- public static function tearDownAfterClass(): void {
- if ( false === self::$original_fulfillments_flag ) {
- delete_option( 'woocommerce_feature_fulfillments_enabled' );
- } else {
- update_option( 'woocommerce_feature_fulfillments_enabled', self::$original_fulfillments_flag );
- }
- parent::tearDownAfterClass();
+ FulfillmentsHelper::create_tables();
}
/**
@@ -108,6 +85,7 @@ class ControllerTest extends WC_Unit_Test_Case {
*/
public function setUp(): void {
parent::setUp();
+ FulfillmentsHelper::enable_fulfillments();
$this->controller = new FulfillmentsController();
$this->controller->init( new FulfillmentSchema(), new OrderFulfillmentsRestController() );
diff --git a/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Fulfillments/ProvidersTest.php b/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Fulfillments/ProvidersTest.php
index 29bc0d9c3e0..49b47bda2fc 100644
--- a/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Fulfillments/ProvidersTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Fulfillments/ProvidersTest.php
@@ -6,6 +6,7 @@ namespace Automattic\WooCommerce\Tests\Internal\RestApi\Routes\V4\Fulfillments;
use Automattic\WooCommerce\Admin\Features\Fulfillments\OrderFulfillmentsRestController;
use Automattic\WooCommerce\Internal\RestApi\Routes\V4\Fulfillments\Controller as FulfillmentsController;
use Automattic\WooCommerce\Internal\RestApi\Routes\V4\Fulfillments\Schema\FulfillmentSchema;
+use Automattic\WooCommerce\Tests\Admin\Features\Fulfillments\Helpers\FulfillmentsHelper;
use WC_REST_Unit_Test_Case;
use WP_REST_Request;
@@ -21,13 +22,6 @@ class ProvidersTest extends WC_REST_Unit_Test_Case {
*/
private FulfillmentsController $controller;
- /**
- * Original value of the fulfillments feature flag.
- *
- * @var mixed
- */
- private static $original_fulfillments_flag;
-
/**
* Admin user for tests, shared across all tests in the class for REST authentication.
*
@@ -54,23 +48,7 @@ class ProvidersTest extends WC_REST_Unit_Test_Case {
*/
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
- self::$original_fulfillments_flag = get_option( 'woocommerce_feature_fulfillments_enabled' );
- update_option( 'woocommerce_feature_fulfillments_enabled', 'yes' );
- $controller = wc_get_container()->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );
- $controller->register();
- $controller->initialize_fulfillments();
- }
-
- /**
- * Tear down the test environment.
- */
- public static function tearDownAfterClass(): void {
- if ( false === self::$original_fulfillments_flag ) {
- delete_option( 'woocommerce_feature_fulfillments_enabled' );
- } else {
- update_option( 'woocommerce_feature_fulfillments_enabled', self::$original_fulfillments_flag );
- }
- parent::tearDownAfterClass();
+ FulfillmentsHelper::create_tables();
}
/**
@@ -103,6 +81,7 @@ class ProvidersTest extends WC_REST_Unit_Test_Case {
*/
public function setUp(): void {
parent::setUp();
+ FulfillmentsHelper::enable_fulfillments();
// Hooks added in setUpBeforeClass() are removed after the first test, so add them per test.
$fulfillments = wc_get_container()->get( \Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentsController::class );