Commit d769a1302f5 for woocommerce

commit d769a1302f5e6784f1c23dafb7413768d7a7a65c
Author: Vlad Olaru <vlad.olaru@automattic.com>
Date:   Fri Nov 27 18:23:53 2026 +0200

    Route the Mini-Cart cart and checkout page checks through LegacyProxy (#70991)

    * refactor: Route the Mini-Cart cart and checkout page checks through LegacyProxy

    MiniCart calls is_cart() || is_checkout() in render(), enqueue_data()
    and get_markup() to fall back to static markup on those pages. In
    PHPUnit, any earlier test that renders the cart or checkout defines
    WOOCOMMERCE_CART or WOOCOMMERCE_CHECKOUT, and those functions then return
    true for the rest of the process. The drawer test has to run in a
    separate process, which reboots WordPress (~1.5 s), to see the regular
    page branch.

    Move the check into a private is_cart_or_checkout_page() helper that
    calls both functions through LegacyProxy, as SavedForLater already does
    for is_cart(). Outside tests, LegacyProxy::call_function() is
    call_user_func_array(), so the real functions run in the same order,
    with the same short-circuit and the same woocommerce_is_cart and
    woocommerce_is_checkout filters. Method signatures do not change, and
    no WordPress.org plugin extends MiniCart.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

    * test: Run the Mini-Cart drawer test in-process and cover the page checks

    test_closed_drawer_is_inert_and_hidden ran in a separate process so a
    WOOCOMMERCE_CART constant left by an earlier test could not hide the
    overlay. Now that MiniCart reads the page through LegacyProxy, the test
    mocks is_cart() and is_checkout() to false and runs in the main process.
    It now also checks that render() adds the overlay to wp_footer, which
    the old test never did, since it printed the overlay directly.

    Two cart and checkout page tests cover the three call sites:

    - One uses the real woocommerce_is_cart and woocommerce_is_checkout
      filters, so it proves they still reach MiniCart through the proxy. It
      checks the hidden, disabled button and that no overlay is added.
    - One mocks the page through LegacyProxy, with a MiniCartMock built on a
      mocked AssetDataRegistry that must receive no data. It fails if
      enqueue_data() runs on these pages, and also if MiniCart stops asking
      the proxy: nothing else marks the request as the cart or checkout.

    Each mutation fails at least one test: dropping the drawer's inert
    binding, render() not building the overlay, the overlay not hooked into
    wp_footer, the helper ignoring is_checkout(), removing any one of the
    three page checks, and the helper calling is_cart() and is_checkout()
    directly. The MiniCart class drops from ~1.8 s to ~0.35 s locally.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

    * test: Cover Mini-Cart rendering when the real page check and LegacyProxy disagree

    The proxy routing test expects the hidden Mini-Cart. When an earlier
    test leaves WOOCOMMERCE_CART or WOOCOMMERCE_CHECKOUT defined, a direct
    is_cart() or is_checkout() call gives that same result, so a regression
    in enqueue_data() alone could pass in that test order.

    The new case forces the real check to true through the conditional
    filters while LegacyProxy reports neither page, and expects the
    interactive block: asset data added and the overlay hooked on
    wp_footer. It fails if enqueue_data() or render() goes back to direct
    calls, regardless of the constants earlier tests define.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

    * test: Start every Mini-Cart test off the cart and checkout pages

    In the order CI runs the suite, 16 cart, checkout and AJAX tests define
    WOOCOMMERCE_CART or WOOCOMMERCE_CHECKOUT before the first Mini-Cart
    test. is_cart() and is_checkout() then return true for the rest of the
    process, and nothing can make them false again: their filters can only
    turn them on. So every Mini-Cart test that did not mock the page check
    rendered the hidden cart-page button. Seven tests written against the
    regular block (badge, product count, coming soon) never reached its
    markup in CI: dropping the badge colour from that markup failed the
    badge test with the class run alone and passed in CI order.

    setUp() now stubs both checks to false through LegacyProxy, so each
    test starts on a page that is neither. The cart and checkout tests call
    use_cart_or_checkout_page(), which puts the real function back for that
    page and turns it on through its public filter. With the constants
    defined the real function is true either way, so the filter decides
    the result only when the class runs alone.

    Review feedback asked for the two tests that only pinned the proxy to
    go, since they set up a state no store can reach. They are removed.
    The one behaviour only they covered, enqueue_data() adding no asset
    data on the cart and checkout pages, is now asserted in
    test_overlay_is_not_printed_on_cart_and_checkout_pages.

    Each of these fails at least one test with the class alone and in CI
    order: removing any one of the three page checks, the helper ignoring
    is_checkout(), the overlay not hooked into wp_footer, and dropping the
    badge colour from the interactive markup. Calling is_cart() and
    is_checkout() directly fails the drawer test in CI order and passes
    with the class alone, where the two are equivalent.

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

    ---------

    Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

diff --git a/plugins/woocommerce/changelog/update-mini-cart-page-checks-legacy-proxy b/plugins/woocommerce/changelog/update-mini-cart-page-checks-legacy-proxy
new file mode 100644
index 00000000000..a93855dcb1d
--- /dev/null
+++ b/plugins/woocommerce/changelog/update-mini-cart-page-checks-legacy-proxy
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+Comment: Make the Mini-Cart cart and checkout page checks mockable in tests; no behavior change for stores.
+
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/MiniCart.php b/plugins/woocommerce/src/Blocks/BlockTypes/MiniCart.php
index 7f2320537cc..fcfe32b2cda 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/MiniCart.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/MiniCart.php
@@ -16,6 +16,7 @@ use Automattic\WooCommerce\Admin\Features\Features;
 use Automattic\WooCommerce\Enums\TaxDisplayMode;
 use Automattic\WooCommerce\Blocks\Utils\BlocksSharedState;
 use Automattic\WooCommerce\Internal\ComingSoon\ComingSoonHelper;
+use Automattic\WooCommerce\Proxies\LegacyProxy;
 use Automattic\Block_Delimiter;

 /**
@@ -168,7 +169,7 @@ class MiniCart extends AbstractBlock {
 	 *                           not in the post content on editor load.
 	 */
 	protected function enqueue_data( array $attributes = array() ) {
-		if ( is_cart() || is_checkout() ) {
+		if ( $this->is_cart_or_checkout_page() ) {
 			return;
 		}

@@ -298,7 +299,7 @@ class MiniCart extends AbstractBlock {
 		 * In the cart and checkout pages, the block is either rendered hidden or removed.
 		 * It is not interactive, so it can fall back to the existing implementation.
 		 */
-		if ( is_cart() || is_checkout() ) {
+		if ( $this->is_cart_or_checkout_page() ) {
 			return $content . $this->get_markup( MiniCartUtils::migrate_attributes_to_color_panel( $attributes ) );
 		}

@@ -687,7 +688,7 @@ class MiniCart extends AbstractBlock {
 		</span>
 		' . $this->get_cart_price_markup( $attributes );

-		if ( is_cart() || is_checkout() ) {
+		if ( $this->is_cart_or_checkout_page() ) {
 			if ( $this->should_not_render_mini_cart( $attributes ) ) {
 				return '';
 			}
@@ -814,4 +815,18 @@ class MiniCart extends AbstractBlock {
 		$context['woocommerce/productCollection/referenceType'] = 'cart';
 		return $context;
 	}
+
+	/**
+	 * Check whether the current page is the cart or checkout page, where the block is not interactive.
+	 *
+	 * Goes through LegacyProxy so tests can mock the result: once WOOCOMMERCE_CART or WOOCOMMERCE_CHECKOUT
+	 * is defined, is_cart() or is_checkout() returns true for the rest of the request.
+	 *
+	 * @return bool
+	 */
+	private function is_cart_or_checkout_page(): bool {
+		$legacy_proxy = wc_get_container()->get( LegacyProxy::class );
+
+		return (bool) $legacy_proxy->call_function( 'is_cart' ) || (bool) $legacy_proxy->call_function( 'is_checkout' );
+	}
 }
diff --git a/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/MiniCart.php b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/MiniCart.php
index 8b7e8058c75..ef6aa717d7f 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/MiniCart.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/MiniCart.php
@@ -3,6 +3,7 @@ declare( strict_types = 1 );
 namespace Automattic\WooCommerce\Tests\Blocks\BlockTypes;

 use Automattic\WooCommerce\Blocks\BlockTypes\MiniCart as MiniCartBlock;
+use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
 use Automattic\WooCommerce\Blocks\Package;
 use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
 use Automattic\WooCommerce\Tests\Blocks\Helpers\BlockTypeRegistryTrait;
@@ -131,6 +132,16 @@ class MiniCart extends \WC_Unit_Test_Case {
 		);
 		WC()->cart->empty_cart();
 		add_filter( 'woocommerce_is_rest_api_request', '__return_false', 1 );
+
+		// Earlier tests leave WOOCOMMERCE_CART and WOOCOMMERCE_CHECKOUT defined for the rest of the process, so the real
+		// is_cart() and is_checkout() can never return false again here. Start each test on neither page;
+		// use_cart_or_checkout_page() switches a test back to the real checks.
+		$this->register_legacy_proxy_function_mocks(
+			array(
+				'is_cart'     => '__return_false',
+				'is_checkout' => '__return_false',
+			)
+		);
 	}

 	/**
@@ -483,14 +494,14 @@ class MiniCart extends \WC_Unit_Test_Case {
 	/**
 	 * @testdox Should not enqueue the view script module when rendered on the cart or checkout page.
 	 *
-	 * @testWith [ "woocommerce_is_cart" ]
-	 *           [ "woocommerce_is_checkout" ]
+	 * @testWith [ "is_cart" ]
+	 *           [ "is_checkout" ]
 	 *
-	 * @param string $page_filter Conditional filter forcing the cart or checkout context.
+	 * @param string $page_check The conditional function for the page: is_cart or is_checkout.
 	 */
-	public function test_view_script_module_is_not_enqueued_on_cart_and_checkout_pages( string $page_filter ): void {
+	public function test_view_script_module_is_not_enqueued_on_cart_and_checkout_pages( string $page_check ): void {
 		wp_dequeue_script_module( 'woocommerce/mini-cart' );
-		add_filter( $page_filter, '__return_true' );
+		$this->use_cart_or_checkout_page( $page_check );

 		$block = parse_blocks( '<!-- wp:woocommerce/mini-cart /-->' );
 		render_block( $block[0] );
@@ -508,14 +519,16 @@ class MiniCart extends \WC_Unit_Test_Case {
 	}

 	/**
-	 * Cart/checkout constants defined by other tests cannot be reset in this process.
-	 *
 	 * @testdox Should keep the closed drawer out of the tab order and the accessibility tree.
-	 * @runInSeparateProcess
-	 * @preserveGlobalState disabled
 	 */
 	public function test_closed_drawer_is_inert_and_hidden(): void {
-		$processor = new \WP_HTML_Tag_Processor( $this->render_overlay() );
+		$this->render_mini_cart_block();
+
+		$this->assertSame( 10, has_action( 'wp_footer', array( $this->mock, 'render_mini_cart_overlay' ) ), 'The overlay should be printed in the footer.' );
+
+		ob_start();
+		$this->mock->render_mini_cart_overlay();
+		$processor = new \WP_HTML_Tag_Processor( (string) ob_get_clean() );

 		$this->assertTrue( $processor->next_tag( array( 'class_name' => 'wc-block-mini-cart__drawer' ) ), 'The drawer should be rendered.' );
 		$this->assertTrue( $processor->get_attribute( 'inert' ), 'The closed drawer should be inert so its contents are not focusable.' );
@@ -523,17 +536,59 @@ class MiniCart extends \WC_Unit_Test_Case {
 	}

 	/**
-	 * Render the Mini-Cart block and return the overlay markup it prints in the footer.
+	 * @testdox Should not print the drawer overlay in the footer on the cart or checkout page.
 	 *
-	 * @return string
+	 * @testWith [ "is_cart" ]
+	 *           [ "is_checkout" ]
+	 *
+	 * @param string $page_check The conditional function for the page: is_cart or is_checkout.
 	 */
-	private function render_overlay(): string {
-		$block = parse_blocks( '<!-- wp:woocommerce/mini-cart /-->' );
-		render_block( $block[0] );
+	public function test_overlay_is_not_printed_on_cart_and_checkout_pages( string $page_check ): void {
+		$this->use_cart_or_checkout_page( $page_check );
+		$asset_data_registry = $this->createMock( AssetDataRegistry::class );
+		$asset_data_registry->expects( $this->never() )->method( 'add' );
+		$this->mock = $this->replace_block_type( 'woocommerce/mini-cart', static fn () => new MiniCartMock( $asset_data_registry ) );

-		ob_start();
-		$this->mock->render_mini_cart_overlay();
-		return (string) ob_get_clean();
+		$output = $this->render_mini_cart_block();
+
+		$this->assertFalse( has_action( 'wp_footer', array( $this->mock, 'render_mini_cart_overlay' ) ), 'The overlay should not be printed on the cart and checkout pages.' );
+		$this->assert_hidden_mini_cart_markup( $output );
+	}
+
+	/**
+	 * Assert the markup is the hidden, disabled Mini-Cart button rendered on the cart and checkout pages.
+	 *
+	 * @param string $output Rendered block markup.
+	 */
+	private function assert_hidden_mini_cart_markup( string $output ): void {
+		$processor = new \WP_HTML_Tag_Processor( $output );
+
+		$this->assertTrue( $processor->next_tag( array( 'class_name' => 'wc-block-mini-cart' ) ), 'The Mini-Cart wrapper should be rendered.' );
+		$this->assertStringContainsString( 'visibility:hidden', (string) $processor->get_attribute( 'style' ), 'The Mini-Cart should be hidden on the cart and checkout pages.' );
+		$this->assertTrue( $processor->next_tag( array( 'class_name' => 'wc-block-mini-cart__button' ) ), 'The Mini-Cart button should be rendered.' );
+		$this->assertTrue( $processor->get_attribute( 'disabled' ), 'The Mini-Cart button should be disabled on the cart and checkout pages.' );
+	}
+
+	/**
+	 * Put the current test on the cart or checkout page.
+	 *
+	 * Replaces the setUp() stub with the real page check and turns it on through its public filter.
+	 *
+	 * @param string $page_check The conditional function for the page: is_cart or is_checkout.
+	 */
+	private function use_cart_or_checkout_page( string $page_check ): void {
+		$this->register_legacy_proxy_function_mocks( array( $page_check => $page_check ) );
+		add_filter( 'woocommerce_' . $page_check, '__return_true' );
+	}
+
+	/**
+	 * Render the Mini-Cart block.
+	 *
+	 * @return string Rendered block markup.
+	 */
+	private function render_mini_cart_block(): string {
+		$block = parse_blocks( '<!-- wp:woocommerce/mini-cart /-->' );
+		return render_block( $block[0] );
 	}

 	/**
diff --git a/plugins/woocommerce/tests/php/src/Blocks/Mocks/MiniCartMock.php b/plugins/woocommerce/tests/php/src/Blocks/Mocks/MiniCartMock.php
index 1fe3d7de01c..2b17c92f2ac 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/Mocks/MiniCartMock.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/Mocks/MiniCartMock.php
@@ -15,11 +15,13 @@ use Automattic\WooCommerce\Blocks\Integrations\IntegrationRegistry;
 class MiniCartMock extends MiniCart {
 	/**
 	 * Initialize our mock class.
+	 *
+	 * @param AssetDataRegistry|null $asset_data_registry Registry to use instead of the shared one.
 	 */
-	public function __construct() {
+	public function __construct( ?AssetDataRegistry $asset_data_registry = null ) {
 		parent::__construct(
 			Package::container()->get( Api::class ),
-			Package::container()->get( AssetDataRegistry::class ),
+			$asset_data_registry ?? Package::container()->get( AssetDataRegistry::class ),
 			new IntegrationRegistry()
 		);
 	}