Commit 62c1e214e71 for woocommerce
commit 62c1e214e717345f637bad144374d823715ac7fb
Author: Vlad Olaru <vlad.olaru@automattic.com>
Date: Fri Aug 7 16:27:54 2026 +0300
[tests] Run Blocks tests that touch WordPress state on a WordPress test case (#67302)
* fix: guard the fake WC Calypso Bridge function declarations in tests
`WC_Admin_Tests_RemoteSpecs_RuleProcessors_IsWooExpressRuleProcessor`
declares two stand-ins for functions the WC Calypso Bridge plugin
provides, so the rule processor has something to call. Declaring a
function is irreversible and process-wide, and neither declaration was
guarded.
That makes a redeclaration fatal rather than merely untidy. The
`wc_calypso_bridge_is_woo_express_trial_plan` one is declared inside
`test_is_trial_plan`, so adding a data provider to that test — an
ordinary thing to do — would run the declaration more than once and kill
the process. Both would also collide with the real plugin if it were ever
loaded in the same run.
Both are now behind `function_exists()`.
* fix: run the LegacyProxy get_global test
`get_global_can_be_used_to_get_the_value_of_a_global` has no `test_`
prefix and no `@test` annotation, so PHPUnit has never collected it. It
carries a `@testdox` line, which is only a display label and has no
bearing on collection.
Nothing suggests that was deliberate. It arrived in #33140, the same
change that introduced `LegacyProxy::get_global()`, and has not been
touched since. Every one of the eight other tests in the file is
prefixed, and the same commit added three correctly prefixed tests to
`MockableLegacyProxyTest` — including the mockable counterpart of this
one. A test that is never collected produces no output at all, which is
why it went unnoticed for three and a half years.
The consequence is that `LegacyProxy::get_global()` has had no direct
coverage since it was added; only the mockable subclass is exercised. The
test passes once collected.
* fix: discard the Spy_REST_Server installed by the Store API controller tests
`ControllerTests::setUp()` replaces the `$wp_rest_server` global with a
`Spy_REST_Server`, and the class had no teardown. `$wp_rest_server` is
not among the globals WordPress resets between tests, so the spy stayed
in place for the remainder of the process and `rest_get_server()` kept
handing it out to every later test.
Nulling it in `tearDown()` lets the next caller build a fresh server.
* chore: add changelog for the test hygiene follow-ups
* fix: stop using the deprecated WC_Order::add_coupon in Store API order tests
`WC_Order::add_coupon()` has been deprecated since WooCommerce 3.0
(`includes/legacy/abstract-wc-legacy-order.php:31`). The two coupon tests
in `OrderControllerTests` still call it.
Nothing surfaced that, because the class extends the bare PHPUnit
Polyfills `TestCase`, which installs no `expectDeprecated()` catcher —
the notice was raised and silently discarded on every run.
Replaced with what the deprecation notice prescribes, and with what the
deprecated wrapper does internally: build a `WC_Order_Item_Coupon`, set
its props, and add it via `WC_Order::add_item()`. `apply_coupon()` is not
the right replacement here — it validates the coupon, and both of these
tests deliberately attach one that fails validation so the code under
test can be seen rejecting it.
* fix: restore test isolation for Blocks tests on the bare polyfill base
Twelve Blocks test classes extend `Yoast\PHPUnitPolyfills\TestCases\TestCase`
— a compatibility shim for PHPUnit 8/9 `setUp()`/`tearDown()` signature
differences, not a WordPress test case. Classes on it get no per-test
database transaction, no hook backup and restore, no global scope
cleanup, no `$this->factory`, no deprecation catcher, and no outbound
HTTP blocking. Every write commits permanently to the shared test
database and every filter they add leaks into the rest of the process.
That was a deliberate choice in the standalone woocommerce-blocks repo,
where the WP core test framework was dropped in blocks PR #4567 to remove
a build dependency, and where these tests ran alone in their own process.
The tests moved into this monorepo in Dec 2023 and now run interleaved
with the rest of the suite, so the premise no longer holds. The codebase
has already moved on without them: 82 of 94 Blocks test classes use a
WordPress test case today.
Nine of the twelve are converted to `WC_Unit_Test_Case`. Three are left
alone, because for a genuinely dependency-free unit test the bare base is
the correct choice and a transaction would be pure cost: `Registry\Container`
(a DI container with no WordPress surface), `ExtendSchemaTests`, and
`CartTokenUtilsTests` (which mocks the `salt` filter but balances it, and
cannot poison `wp_salt()`'s static cache because that cache stores the
unfiltered value and re-applies the filter on every return).
Two visibility fixes were required: `ProductQueryFiltersTest::setUp()` and
`CheckoutFieldsFrontendTest::tearDown()` were `protected`, which cannot
override the `public` declarations they now inherit, and would have been a
fatal at class load rather than a test failure.
Three classes needed explicit teardown that no base class provides,
because the state is neither in the database nor in hooks:
- `NoticeHandlerTests` clears the WC notice queue, which lives on the
session singleton. `NoticeHandler::convert_notices_to_wp_errors()` does
not clear it despite its docblock saying it does, so the notice
otherwise survives to the end of the process and is written to
`wp_woocommerce_sessions` by the session handler's shutdown hook.
- `Hydration` restores `WC()->cart->cart_context`, which
`CartController::load_cart()` sets to 'store-api' and never puts back.
Production code branches on it in three places, so the leak changes
behaviour for every later test in the process.
- `DocumentObjectTests` empties the cart before handing back to the
parent rather than after, so it happens while the session is still live.
The concrete defect this fixes: `CartControllerTests` failed three tests
when run on its own, because an earlier test in the class poisoned the
`wc_shipping_method_count` transient, making `WC_Cart::needs_shipping()`
false and yielding zero shipping packages. It passed in the full suite
only by ordering luck, and it took `OrderControllerTests` down with it
whenever the two ran together. Both are green in isolation now.
* chore: add changelog for the Blocks test isolation conversion
* fix: restore test isolation for two more tests outside the Blocks scan
A monorepo-wide sweep for test classes without a deprecation catcher
turned up two more that belong with the previous conversion. Both were
missed earlier because that scan grepped for one import string inside
`tests/php/src/Blocks/` only.
`CheckoutLinkTest` extends `PHPUnit\Framework\TestCase` directly rather
than the polyfill, which is why a search for the Yoast import did not
find it. It creates three products and a coupon, adds them to the cart
through the checkout-link service, and never empties the cart — so its
contents were visible to every later test in the process. Its manual
`wp_delete_post()` cleanup also only ran on the success path. The cart is
a singleton and is not covered by the rollback, so it is emptied
explicitly in teardown.
`AbstractAutomatticAddressProviderTest` sits outside `Blocks/`
altogether. It called `parent::tearDown()` first, before its own cleanup,
and line 77 does `remove_all_actions( 'wp_enqueue_scripts' )` — which
removes every enqueue callback registered anywhere in the process, not
just the one the test added. Hook restore now repairs that after each
test. The parent call moves last.
Both declared `setUp()`/`tearDown()` `protected`, which cannot override
the `public` declarations they now inherit; that would fatal at class
load rather than fail a test.
For the record, the sweep that found them also answered the question that
prompted it: a probe on `deprecated_function_run`, `deprecated_argument_run`,
`deprecated_hook_run`, `deprecated_file_included` and `doing_it_wrong_run`
logged 1,434 events across a full suite run and none of them originated
in a test class without a catcher. The deprecated `WC_Order::add_coupon`
call fixed earlier in this branch was the only one being swallowed.
* Empty the cart in OrderControllerTests teardown
The cart lives on the WC singleton, which the per-test database rollback
does not touch, so the product added by
test_validate_order_before_payment_invalid_addresses leaked into every
later test in the process.
Plugging the leak exposed a hidden dependency:
test_validate_existing_order_before_payment_invalid_addresses only
passed because it inherited that leaked cart. validate_addresses()
inspects the selected shipping rates from the global cart's packages
even for existing orders, and with an empty cart the empty
selected-rates set is treated as all-local-pickup, skipping the
shipping country check. The test now adds the order's product to the
cart itself.
* Simplify coupon line item creation in OrderControllerTests
The construction was copied verbatim from the deprecated
WC_Order::add_coupon(), redundancies included: discount and
discount_tax are the WC_Order_Item_Coupon defaults, order_id is set by
save_items() during the order save, and the early item save is
unnecessary because the order save persists attached items.
* Reset leaked singleton state in DocumentObjectTests teardown
The coupon applied in setUp() queues a success notice on the WC
session, which emptying the cart does not clear, and
test_additional_fields_schema deregistered its two checkout fields
inline, so a failed assertion left them registered on the
container-cached CheckoutFields singleton for every later test.
Clear the notices and deregister the fields unconditionally in
tearDown(); deregister_checkout_field() is a no-op for unknown fields.
* Remove dead disable_float_support() from QuantityLimitsTests
The method was never called, and the parent test case restores the
$wp_filter snapshot in tear_down(), which already undoes everything
enable_float_support() changes.
* Update CheckoutLinkTest teardown to cover real leaked state
$GLOBALS['added_to_cart'] and $GLOBALS['applied_coupons'] are
referenced nowhere in the codebase, and $_GET is reset by the parent's
clean_up_global_scope() before every test, so that cleanup was dead
weight. What actually leaks is WC singleton state: the cart contents
and the coupon success notice queued by get_checkout_link(), so the
teardown now clears exactly those.
diff --git a/plugins/woocommerce/changelog/fix-blocks-tests-restore-isolation b/plugins/woocommerce/changelog/fix-blocks-tests-restore-isolation
new file mode 100644
index 00000000000..77f1fdda6a9
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-blocks-tests-restore-isolation
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+
+Run the Blocks test classes that touch WordPress state on a WordPress test case, so their database writes are rolled back and their filters no longer leak into the rest of the suite.
diff --git a/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutFieldsFrontendTest.php b/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutFieldsFrontendTest.php
index 2aeb6658b37..5dbe5a574ac 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutFieldsFrontendTest.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutFieldsFrontendTest.php
@@ -9,12 +9,11 @@ use Automattic\WooCommerce\Blocks\Package;
use Exception;
use WC_Customer;
use WP_Error;
-use Yoast\PHPUnitPolyfills\TestCases\TestCase;
/**
* Test \Automattic\WooCommerce\Blocks\Domain\Services\Hydration class.
*/
-class CheckoutFieldsFrontendTest extends TestCase {
+class CheckoutFieldsFrontendTest extends \WC_Unit_Test_Case {
/**
* System under test.
*
@@ -53,7 +52,7 @@ class CheckoutFieldsFrontendTest extends TestCase {
/**
* Tear down.
*/
- protected function tearDown(): void {
+ public function tearDown(): void {
remove_filter( 'woocommerce_add_notice', [ $this, 'capture_notice' ] );
remove_filter( 'woocommerce_add_error', [ $this, 'capture_error' ] );
remove_filter( 'woocommerce_add_success', [ $this, 'capture_success' ] );
diff --git a/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutFieldsSchema/DocumentObjectTests.php b/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutFieldsSchema/DocumentObjectTests.php
index 6d542087001..11cc3041235 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutFieldsSchema/DocumentObjectTests.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutFieldsSchema/DocumentObjectTests.php
@@ -8,7 +8,6 @@ use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields;
use Automattic\WooCommerce\StoreApi\Utilities\CheckoutTrait;
use Automattic\WooCommerce\Tests\Blocks\Helpers\FixtureData;
use Automattic\WooCommerce\Blocks\Package;
-use Yoast\PHPUnitPolyfills\TestCases\TestCase;
use Opis\JsonSchema\{
Validator,
ValidationResult,
@@ -20,7 +19,7 @@ use WC_Customer;
/**
* DocumentObjectTests class.
*/
-class DocumentObjectTests extends TestCase {
+class DocumentObjectTests extends \WC_Unit_Test_Case {
/**
* Trait to use for the test_additional_fields_schema test.
*
@@ -121,8 +120,15 @@ class DocumentObjectTests extends TestCase {
* Tear down the test environment.
*/
public function tearDown(): void {
- parent::tearDown();
+ // The cart, the notice queue, and the CheckoutFields registry all live on singletons
+ // that neither the database rollback nor the hook restore touches, so reset them
+ // unconditionally before handing back to the parent.
wc_empty_cart();
+ wc_clear_notices();
+ $this->additional_fields_controller->deregister_checkout_field( 'namespace/contact_field' );
+ $this->additional_fields_controller->deregister_checkout_field( 'namespace/order_field' );
+
+ parent::tearDown();
}
/**
* test_default_document_schema.
@@ -286,9 +292,6 @@ class DocumentObjectTests extends TestCase {
'namespace/order_field' => 'Order field',
]
);
-
- $this->additional_fields_controller->deregister_checkout_field( 'namespace/contact_field' );
- $this->additional_fields_controller->deregister_checkout_field( 'namespace/order_field' );
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutLinkTest.php b/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutLinkTest.php
index 65ecef5d6ed..598ba253677 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutLinkTest.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutLinkTest.php
@@ -5,30 +5,21 @@ namespace Automattic\WooCommerce\Tests\Blocks\Domain\Services;
use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutLink;
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper;
-use PHPUnit\Framework\TestCase;
/**
* Unit tests for CheckoutLink.
*/
-class CheckoutLinkTest extends TestCase {
- /**
- * Setup the test environment.
- */
- protected function setUp(): void {
- parent::setUp();
- $_GET = [];
- // Reset global cart/session if needed.
- $GLOBALS['added_to_cart'] = [];
- $GLOBALS['applied_coupons'] = [];
- }
-
+class CheckoutLinkTest extends \WC_Unit_Test_Case {
/**
* Tear down the test environment.
*/
- protected function tearDown(): void {
- $_GET = [];
- $GLOBALS['added_to_cart'] = [];
- $GLOBALS['applied_coupons'] = [];
+ public function tearDown(): void {
+ // The cart and the notice queue live on the WC singleton, so the rollback does not
+ // clear them. Without this the products this test adds stay in the cart, and the
+ // coupon success notice stays queued, for every later test in the process.
+ WC()->cart->empty_cart();
+ wc_clear_notices();
+
parent::tearDown();
}
diff --git a/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/Hydration.php b/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/Hydration.php
index ef09561db19..55e5ee18a1e 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/Hydration.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/Hydration.php
@@ -4,12 +4,11 @@ declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Blocks\Domain\Services;
use Automattic\WooCommerce\Blocks\Package;
-use Yoast\PHPUnitPolyfills\TestCases\TestCase;
/**
* Test \Automattic\WooCommerce\Blocks\Domain\Services\Hydration class.
*/
-class Hydration extends TestCase {
+class Hydration extends \WC_Unit_Test_Case {
/**
* System under test.
@@ -26,6 +25,20 @@ class Hydration extends TestCase {
$this->sut = Package::container()->get( \Automattic\WooCommerce\Blocks\Domain\Services\Hydration::class );
}
+ /**
+ * Restore the global cart.
+ *
+ * Loading a Store API cart route sets `cart_context` to 'store-api' on the WC cart
+ * singleton and never puts it back. Neither the database rollback nor the hook restore
+ * covers that, and production code branches on it, so reset it here.
+ */
+ public function tearDown(): void {
+ WC()->cart->empty_cart();
+ WC()->cart->cart_context = 'shortcode';
+
+ parent::tearDown();
+ }
+
/**
* @testDox REST API response is returned without loading entire REST server.
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Schemas/OrderItemSchemaTest.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Schemas/OrderItemSchemaTest.php
index 0d88bad6118..7e39e47d4e5 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Schemas/OrderItemSchemaTest.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Schemas/OrderItemSchemaTest.php
@@ -11,12 +11,11 @@ use Automattic\WooCommerce\StoreApi\Formatters\MoneyFormatter;
use Automattic\WooCommerce\StoreApi\Formatters\HtmlFormatter;
use Automattic\WooCommerce\StoreApi\Formatters\CurrencyFormatter;
use WC_Helper_Order;
-use Yoast\PHPUnitPolyfills\TestCases\TestCase;
/**
* OrderItemSchemaTest class.
*/
-class OrderItemSchemaTest extends TestCase {
+class OrderItemSchemaTest extends \WC_Unit_Test_Case {
/**
* The system under test.
*
@@ -60,10 +59,11 @@ class OrderItemSchemaTest extends TestCase {
* Tear down after test.
*/
public function tearDown(): void {
- parent::tearDown();
$this->sut = null;
$this->schema_controller = null;
$this->mock_extend = null;
+
+ parent::tearDown();
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/CartControllerTests.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/CartControllerTests.php
index c17e9b32986..ef61b14c34f 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/CartControllerTests.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/CartControllerTests.php
@@ -5,25 +5,24 @@ namespace Automattic\WooCommerce\Tests\Blocks\StoreApi\Utilities;
use Automattic\WooCommerce\StoreApi\Utilities\CartController;
use Automattic\WooCommerce\Tests\Blocks\Helpers\FixtureData;
-use Yoast\PHPUnitPolyfills\TestCases\TestCase;
use Automattic\WooCommerce\Enums\ProductStockStatus;
/**
* Unit tests for the CartController class.
*/
-class CartControllerTests extends TestCase {
+class CartControllerTests extends \WC_Unit_Test_Case {
/**
* tearDown.
*/
public function tearDown(): void {
- parent::tearDown();
WC()->cart->empty_cart();
- remove_all_filters( 'woocommerce_cart_shipping_packages' );
// Reset DI container to clear any mocks.
$container = wc_get_container();
$container->reset_all_resolved();
$container->reset_all_replacements();
+
+ parent::tearDown();
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/NoticeHandlerTests.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/NoticeHandlerTests.php
index 25ddafb19ba..16d7cacb79a 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/NoticeHandlerTests.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/NoticeHandlerTests.php
@@ -5,12 +5,24 @@ namespace Automattic\WooCommerce\Tests\Blocks\StoreApi\Utilities;
use Automattic\WooCommerce\StoreApi\Exceptions\RouteException;
use Automattic\WooCommerce\StoreApi\Utilities\NoticeHandler;
-use Yoast\PHPUnitPolyfills\TestCases\TestCase;
/**
* NoticeHandler unit tests.
*/
-class NoticeHandlerTests extends TestCase {
+class NoticeHandlerTests extends \WC_Unit_Test_Case {
+
+ /**
+ * Clear the WooCommerce notice queue.
+ *
+ * Notices live on the WC session singleton, which neither the per-test database
+ * transaction nor the hook restore touches, so they have to be cleared explicitly or
+ * they leak into every later test in the process.
+ */
+ public function tearDown(): void {
+ wc_clear_notices();
+
+ parent::tearDown();
+ }
/**
* Test convert_notices_to_exceptions.
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php
index fd4e6f33bae..b3b5ddb20de 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php
@@ -9,12 +9,11 @@ use Automattic\WooCommerce\Enums\OrderStatus;
use Automattic\WooCommerce\StoreApi\Exceptions\RouteException;
use Automattic\WooCommerce\StoreApi\Utilities\OrderController;
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper;
-use Yoast\PHPUnitPolyfills\TestCases\TestCase;
/**
* OrderControllerTests class.
*/
-class OrderControllerTests extends TestCase {
+class OrderControllerTests extends \WC_Unit_Test_Case {
/**
* Whether the checkout phone field option existed before the test.
*
@@ -72,6 +71,10 @@ class OrderControllerTests extends TestCase {
*/
public function tearDown(): void {
try {
+ // The cart lives on the WC singleton, which the database rollback does not touch,
+ // so empty it or the products some tests add leak into every later test.
+ WC()->cart->empty_cart();
+
WC()->countries->locale = null;
$this->sut = null;
@@ -157,9 +160,11 @@ class OrderControllerTests extends TestCase {
$this->expectExceptionCode( 409 );
$this->expectExceptionMessage( '"fake-coupon" was removed from the cart. Please enter a valid email at checkout to use coupon code "fake-coupon".' );
- $order = WC_Helper_Order::create_order();
- $coupon = CouponHelper::create_coupon( 'fake-coupon', 'publish', array( 'customer_email' => 'random-email@example.com' ) );
- $order->add_coupon( $coupon->get_code() );
+ $order = WC_Helper_Order::create_order();
+ $coupon = CouponHelper::create_coupon( 'fake-coupon', 'publish', array( 'customer_email' => 'random-email@example.com' ) );
+ $coupon_item = new \WC_Order_Item_Coupon();
+ $coupon_item->set_code( $coupon->get_code() );
+ $order->add_item( $coupon_item );
$order->save();
$this->assertEquals( array( 'fake-coupon' ), $order->get_coupon_codes() );
@@ -179,9 +184,11 @@ class OrderControllerTests extends TestCase {
$this->expectExceptionCode( 409 );
$this->expectExceptionMessage( '"fake-coupon" was removed from the order. Please enter a valid email at checkout to use coupon code "fake-coupon".' );
- $order = WC_Helper_Order::create_order();
- $coupon = CouponHelper::create_coupon( 'fake-coupon', 'publish', array( 'customer_email' => 'random-email@example.com' ) );
- $order->add_coupon( $coupon->get_code() );
+ $order = WC_Helper_Order::create_order();
+ $coupon = CouponHelper::create_coupon( 'fake-coupon', 'publish', array( 'customer_email' => 'random-email@example.com' ) );
+ $coupon_item = new \WC_Order_Item_Coupon();
+ $coupon_item->set_code( $coupon->get_code() );
+ $order->add_item( $coupon_item );
$order->save();
$this->assertEquals( array( 'fake-coupon' ), $order->get_coupon_codes() );
@@ -241,7 +248,16 @@ class OrderControllerTests extends TestCase {
$order->set_shipping_country( 'Invalid' );
$order->save();
- // There is no need to update the cart here, we just check the order.
+ // validate_addresses() inspects the selected shipping rates from the global cart's
+ // packages even for existing orders, so the cart must contain the shippable product
+ // for the shipping country check to run.
+ /** @var \WC_Order_Item_Product $item */
+ $array = $order->get_items();
+ $item = reset( $array );
+ $this->assertInstanceOf( \WC_Order_Item_Product::class, $item );
+
+ WC()->cart->add_to_cart( $item->get_product()->get_id() );
+
$this->sut->validate_existing_order_before_payment( $order );
}
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/ProductQueryFiltersTest.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/ProductQueryFiltersTest.php
index b6bc894e40c..e03a93fd8c5 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/ProductQueryFiltersTest.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/ProductQueryFiltersTest.php
@@ -5,16 +5,15 @@ namespace Automattic\WooCommerce\Tests\Blocks\StoreApi\Utilities;
use Automattic\WooCommerce\StoreApi\Utilities\ProductQueryFilters;
use Automattic\WooCommerce\Tests\Blocks\Helpers\FixtureData;
-use Yoast\PHPUnitPolyfills\TestCases\TestCase;
/**
* Unit tests for the ProductQueryFilters class.
*/
-class ProductQueryFiltersTest extends TestCase {
+class ProductQueryFiltersTest extends \WC_Unit_Test_Case {
/**
* Setup test product data. Called before every test.
*/
- protected function setUp(): void {
+ public function setUp(): void {
parent::setUp();
$fixtures = new FixtureData();
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/QuantityLimitsTests.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/QuantityLimitsTests.php
index 5e5c5d43d13..f6594e1726e 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/QuantityLimitsTests.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/QuantityLimitsTests.php
@@ -5,12 +5,11 @@ namespace Automattic\WooCommerce\Tests\Blocks\StoreApi\Utilities;
use Automattic\WooCommerce\Tests\Blocks\Helpers\FixtureData;
use Automattic\WooCommerce\StoreApi\Utilities\QuantityLimits;
-use Yoast\PHPUnitPolyfills\TestCases\TestCase;
/**
* QuantityLimitsTests class.
*/
-class QuantityLimitsTests extends TestCase {
+class QuantityLimitsTests extends \WC_Unit_Test_Case {
/**
* @var string
*/
@@ -20,8 +19,9 @@ class QuantityLimitsTests extends TestCase {
* Set up test environment.
*/
public function setUp(): void {
- $this->manage_stock = get_option( 'woocommerce_manage_stock' );
parent::setUp();
+
+ $this->manage_stock = get_option( 'woocommerce_manage_stock' );
}
/**
@@ -47,16 +47,6 @@ class QuantityLimitsTests extends TestCase {
add_filter( 'woocommerce_stock_amount', 'floatval' );
}
- /**
- * Disable float support and restore integer support.
- */
- private function disable_float_support() {
- // Remove all existing filters first.
- remove_all_filters( 'woocommerce_stock_amount' );
- // Add only intval.
- add_filter( 'woocommerce_stock_amount', 'intval' );
- }
-
/**
* Test quantity limits with float support enabled.
*/
diff --git a/plugins/woocommerce/tests/php/src/Internal/AddressProvider/AbstractAutomatticAddressProviderTest.php b/plugins/woocommerce/tests/php/src/Internal/AddressProvider/AbstractAutomatticAddressProviderTest.php
index 89da19ccf82..5b90d47ea64 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AddressProvider/AbstractAutomatticAddressProviderTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AddressProvider/AbstractAutomatticAddressProviderTest.php
@@ -7,12 +7,11 @@ use Automattic\WooCommerce\Internal\AddressProvider\AbstractAutomatticAddressPro
use Automattic\WooCommerce\StoreApi\Utilities\JsonWebToken;
use Automattic\Jetpack\Constants;
use WC_Address_Provider;
-use Yoast\PHPUnitPolyfills\TestCases\TestCase;
/**
* Tests for AbstractAutomatticAddressProvider functionality
*/
-class AbstractAutomatticAddressProviderTest extends TestCase {
+class AbstractAutomatticAddressProviderTest extends \WC_Unit_Test_Case {
/**
* The mock logger.
@@ -31,7 +30,7 @@ class AbstractAutomatticAddressProviderTest extends TestCase {
/**
* Setup test case.
*/
- protected function setUp(): void {
+ public function setUp(): void {
parent::setUp();
// Setup mock logger.
@@ -70,8 +69,7 @@ class AbstractAutomatticAddressProviderTest extends TestCase {
/**
* Tear down test case.
*/
- protected function tearDown(): void {
- parent::tearDown();
+ public function tearDown(): void {
remove_all_filters( 'pre_update_option_woocommerce_address_autocomplete_enabled' );
remove_all_filters( 'woocommerce_is_checkout' );
remove_all_actions( 'wp_enqueue_scripts' );
@@ -85,6 +83,8 @@ class AbstractAutomatticAddressProviderTest extends TestCase {
delete_option( 'test-provider_address_autocomplete_jwt' );
delete_option( 'test-provider_jwt_retry_data' );
delete_option( 'woocommerce_address_autocomplete_enabled' );
+
+ parent::tearDown();
}
/**