Commit d7812f8eeb9 for woocommerce

commit d7812f8eeb99c6986ca4f33fa4449f5e9d7e1893
Author: Albert Juhé Lluveras <contact@albertjuhe.com>
Date:   Mon Sep 14 09:10:28 2026 +0200

    Create strip_whitespace and strip_whitespace_and_password_form_ids utils for SingleProductTemplateTests to avoid repeating the same logic in every test (#68467)

    * Create utils for SingleProductTemplateTests

    * Add changelog

    * Remove unnecessary guards

    * Reuse same utils in SingleProductTemplateCompatibilityTests

    * Update test comments to testdox

    * Fix assert order in some tests

diff --git a/plugins/woocommerce/changelog/update-SingleProductTemplateTests-utils b/plugins/woocommerce/changelog/update-SingleProductTemplateTests-utils
new file mode 100644
index 00000000000..83674761e63
--- /dev/null
+++ b/plugins/woocommerce/changelog/update-SingleProductTemplateTests-utils
@@ -0,0 +1,5 @@
+Significance: patch
+Type: dev
+Comment: Create strip_whitespace and strip_whitespace_and_password_form_ids utils for SingleProductTemplateTests to avoid repeating the same logic in every test
+
+
diff --git a/plugins/woocommerce/tests/php/src/Blocks/Templates/SingleProductTemplateCompatibilityTests.php b/plugins/woocommerce/tests/php/src/Blocks/Templates/SingleProductTemplateCompatibilityTests.php
index 1d028a90322..66d39d88aa5 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/Templates/SingleProductTemplateCompatibilityTests.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/Templates/SingleProductTemplateCompatibilityTests.php
@@ -38,10 +38,10 @@ class SingleProductTemplateCompatibilityTests extends WP_UnitTestCase {

 		$result = SingleProductTemplateCompatibility::add_compatibility_layer( $default_single_product_template );

-		$result_without_whitespace                           = preg_replace( '/\s+/', '', $result );
-		$expected_single_product_template_without_whitespace = preg_replace( '/\s+/', '', $expected_single_product_template );
-
-		$this->assertEquals( $expected_single_product_template_without_whitespace, $result_without_whitespace, '' );
+		$this->assertEquals(
+			TemplateContentUtils::strip_whitespace( $expected_single_product_template ),
+			TemplateContentUtils::strip_whitespace( $result )
+		);
 	}

 	/**
@@ -73,10 +73,10 @@ class SingleProductTemplateCompatibilityTests extends WP_UnitTestCase {

 		$result = SingleProductTemplateCompatibility::add_compatibility_layer( $default_single_product_template );

-		$result_without_whitespace                           = preg_replace( '/\s+/', '', $result );
-		$expected_single_product_template_without_whitespace = preg_replace( '/\s+/', '', $expected_single_product_template );
-
-		$this->assertEquals( $expected_single_product_template_without_whitespace, $result_without_whitespace, '' );
+		$this->assertEquals(
+			TemplateContentUtils::strip_whitespace( $expected_single_product_template ),
+			TemplateContentUtils::strip_whitespace( $result )
+		);
 	}

 	/**
@@ -156,10 +156,10 @@ class SingleProductTemplateCompatibilityTests extends WP_UnitTestCase {

 		$result = SingleProductTemplateCompatibility::add_compatibility_layer( $default_single_product_template );

-		$result_without_whitespace                           = preg_replace( '/\s+/', '', $result );
-		$expected_single_product_template_without_whitespace = preg_replace( '/\s+/', '', $expected_single_product_template );
-
-		$this->assertEquals( $expected_single_product_template_without_whitespace, $result_without_whitespace, '' );
+		$this->assertEquals(
+			TemplateContentUtils::strip_whitespace( $expected_single_product_template ),
+			TemplateContentUtils::strip_whitespace( $result )
+		);
 	}

 	/**
@@ -191,10 +191,10 @@ class SingleProductTemplateCompatibilityTests extends WP_UnitTestCase {

 		$result = SingleProductTemplateCompatibility::add_compatibility_layer( $default_single_product_template );

-		$result_without_whitespace                           = preg_replace( '/\s+/', '', $result );
-		$expected_single_product_template_without_whitespace = preg_replace( '/\s+/', '', $expected_single_product_template );
-
-		$this->assertEquals( $expected_single_product_template_without_whitespace, $result_without_whitespace, '' );
+		$this->assertEquals(
+			TemplateContentUtils::strip_whitespace( $expected_single_product_template ),
+			TemplateContentUtils::strip_whitespace( $result )
+		);
 	}

 	/**
@@ -226,10 +226,10 @@ class SingleProductTemplateCompatibilityTests extends WP_UnitTestCase {

 		$result = SingleProductTemplateCompatibility::add_compatibility_layer( $default_single_product_template );

-		$result_without_whitespace                           = preg_replace( '/\s+/', '', $result );
-		$expected_single_product_template_without_whitespace = preg_replace( '/\s+/', '', $expected_single_product_template );
-
-		$this->assertEquals( $expected_single_product_template_without_whitespace, $result_without_whitespace, '' );
+		$this->assertEquals(
+			TemplateContentUtils::strip_whitespace( $expected_single_product_template ),
+			TemplateContentUtils::strip_whitespace( $result )
+		);
 	}

 	/**
@@ -293,10 +293,10 @@ class SingleProductTemplateCompatibilityTests extends WP_UnitTestCase {

 		$result = SingleProductTemplateCompatibility::add_compatibility_layer( $default_single_product_template );

-		$result_without_whitespace                           = preg_replace( '/\s+/', '', $result );
-		$expected_single_product_template_without_whitespace = preg_replace( '/\s+/', '', $expected_single_product_template );
-
-		$this->assertEquals( $expected_single_product_template_without_whitespace, $result_without_whitespace, '' );
+		$this->assertEquals(
+			TemplateContentUtils::strip_whitespace( $expected_single_product_template ),
+			TemplateContentUtils::strip_whitespace( $result )
+		);
 	}

 	/**
@@ -322,10 +322,10 @@ class SingleProductTemplateCompatibilityTests extends WP_UnitTestCase {

 		$result = SingleProductTemplateCompatibility::add_compatibility_layer( $default_single_product_template );

-		$result_without_whitespace                           = preg_replace( '/\s+/', '', $result );
-		$expected_single_product_template_without_whitespace = preg_replace( '/\s+/', '', $expected_single_product_template );
-
-		$this->assertEquals( $expected_single_product_template_without_whitespace, $result_without_whitespace, '' );
+		$this->assertEquals(
+			TemplateContentUtils::strip_whitespace( $expected_single_product_template ),
+			TemplateContentUtils::strip_whitespace( $result )
+		);
 	}


@@ -352,10 +352,10 @@ class SingleProductTemplateCompatibilityTests extends WP_UnitTestCase {

 		$result = SingleProductTemplateCompatibility::add_compatibility_layer( $default_single_product_template );

-		$result_without_whitespace                           = preg_replace( '/\s+/', '', $result );
-		$expected_single_product_template_without_whitespace = preg_replace( '/\s+/', '', $expected_single_product_template );
-
-		$this->assertEquals( $expected_single_product_template_without_whitespace, $result_without_whitespace, '' );
+		$this->assertEquals(
+			TemplateContentUtils::strip_whitespace( $expected_single_product_template ),
+			TemplateContentUtils::strip_whitespace( $result )
+		);
 	}

 	/**
@@ -393,10 +393,10 @@ class SingleProductTemplateCompatibilityTests extends WP_UnitTestCase {

 		$result = SingleProductTemplateCompatibility::add_compatibility_layer( $default_single_product_template );

-		$result_without_whitespace                           = preg_replace( '/\s+/', '', $result );
-		$expected_single_product_template_without_whitespace = preg_replace( '/\s+/', '', $expected_single_product_template );
-
-		$this->assertEquals( $expected_single_product_template_without_whitespace, $result_without_whitespace, '' );
+		$this->assertEquals(
+			TemplateContentUtils::strip_whitespace( $expected_single_product_template ),
+			TemplateContentUtils::strip_whitespace( $result )
+		);
 	}

 	/**
@@ -430,10 +430,10 @@ class SingleProductTemplateCompatibilityTests extends WP_UnitTestCase {

 		$result = SingleProductTemplateCompatibility::add_compatibility_layer( $default_single_product_template );

-		$result_without_whitespace                           = preg_replace( '/\s+/', '', $result );
-		$expected_single_product_template_without_whitespace = preg_replace( '/\s+/', '', $expected_single_product_template );
-
-		$this->assertEquals( $expected_single_product_template_without_whitespace, $result_without_whitespace, '' );
+		$this->assertEquals(
+			TemplateContentUtils::strip_whitespace( $expected_single_product_template ),
+			TemplateContentUtils::strip_whitespace( $result )
+		);
 	}

 	/**
@@ -468,10 +468,10 @@ class SingleProductTemplateCompatibilityTests extends WP_UnitTestCase {

 		$result = SingleProductTemplateCompatibility::add_compatibility_layer( $default_single_product_template );

-		$result_without_whitespace                           = preg_replace( '/\s+/', '', $result );
-		$expected_single_product_template_without_whitespace = preg_replace( '/\s+/', '', $expected_single_product_template );
-
-		$this->assertEquals( $expected_single_product_template_without_whitespace, $result_without_whitespace, '' );
+		$this->assertEquals(
+			TemplateContentUtils::strip_whitespace( $expected_single_product_template ),
+			TemplateContentUtils::strip_whitespace( $result )
+		);
 	}

 	/**
@@ -504,9 +504,9 @@ class SingleProductTemplateCompatibilityTests extends WP_UnitTestCase {
 		$result = SingleProductTemplateCompatibility::add_compatibility_layer( $default_single_product_template );
 		$result = SingleProductTemplateCompatibility::add_compatibility_layer( $result );

-		$result_without_whitespace                           = preg_replace( '/\s+/', '', $result );
-		$expected_single_product_template_without_whitespace = preg_replace( '/\s+/', '', $expected_single_product_template );
-
-		$this->assertEquals( $expected_single_product_template_without_whitespace, $result_without_whitespace, '' );
+		$this->assertEquals(
+			TemplateContentUtils::strip_whitespace( $expected_single_product_template ),
+			TemplateContentUtils::strip_whitespace( $result )
+		);
 	}
 }
diff --git a/plugins/woocommerce/tests/php/src/Blocks/Templates/SingleProductTemplateTests.php b/plugins/woocommerce/tests/php/src/Blocks/Templates/SingleProductTemplateTests.php
index 168cde22642..63434bed382 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/Templates/SingleProductTemplateTests.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/Templates/SingleProductTemplateTests.php
@@ -1,4 +1,5 @@
 <?php
+declare( strict_types = 1 );

 namespace Automattic\WooCommerce\Tests\Blocks\Templates;

@@ -12,10 +13,7 @@ use WP_UnitTestCase;
 class SingleProductTemplateTests extends WP_UnitTestCase {

 	/**
-	 * Test that the Product Catalog template content isn't updated mistakenly.
-	 * In other words, make sure the Single Product template logic doesn't leak
-	 * into other templates.
-	 *
+	 * @testdox Should not update Product Catalog template content so Single Product template logic does not leak into other templates.
 	 */
 	public function test_dont_update_single_product_content_for_other_templates() {
 		$single_product_template                  = new SingleProductTemplate();
@@ -43,9 +41,7 @@ class SingleProductTemplateTests extends WP_UnitTestCase {
 	}

 	/**
-	 * Test that the Single Product template content isn't updated if it
-	 * contains the Legacy Template block.
-	 *
+	 * @testdox Should not update Single Product template content when it contains the Legacy Template block.
 	 */
 	public function test_dont_update_single_product_content_with_legacy_template() {
 		$single_product_template                 = new SingleProductTemplate();
@@ -73,9 +69,7 @@ class SingleProductTemplateTests extends WP_UnitTestCase {
 	}

 	/**
-	 * Test that the Single Product template content is updated if it doesn't
-	 * contain the Legacy Template block.
-	 *
+	 * @testdox Should update Single Product template content when it does not contain the Legacy Template block.
 	 */
 	public function test_update_single_product_content_with_legacy_template() {
 		$single_product_template                  = new SingleProductTemplate();
@@ -104,23 +98,14 @@ class SingleProductTemplateTests extends WP_UnitTestCase {
 			),
 		);

-		$expected_single_product_template_without_whitespace = preg_replace(
-			'/\s+/',
-			'',
-			$expected_single_product_template_content
-		);
-		$result_without_whitespace                           = preg_replace( '/\s+/', '', $result[0]->content );
-
 		$this->assertEquals(
-			$expected_single_product_template_without_whitespace,
-			$result_without_whitespace
+			TemplateContentUtils::strip_whitespace( $expected_single_product_template_content ),
+			TemplateContentUtils::strip_whitespace( $result[0]->content )
 		);
 	}

 	/**
-	 * Test that the Single Product template content isn't updated if it
-	 * contains a pattern with the Legacy Template block.
-	 *
+	 * @testdox Should not update Single Product template content when a pattern contains the Legacy Template block.
 	 */
 	public function test_dont_update_single_product_content_with_legacy_template_inside_a_pattern() {
 		register_block_pattern(
@@ -156,9 +141,7 @@ class SingleProductTemplateTests extends WP_UnitTestCase {
 	}

 	/**
-	 * Test that the Single Product template content is updated if it doesn't
-	 * contain the Legacy Template block.
-	 *
+	 * @testdox Should update Single Product template content when a pattern does not contain the Legacy Template block.
 	 */
 	public function test_update_single_product_content_with_legacy_template_inside_a_pattern() {
 		register_block_pattern(
@@ -195,22 +178,14 @@ class SingleProductTemplateTests extends WP_UnitTestCase {
 			),
 		);

-		$expected_single_product_template_without_whitespace = preg_replace(
-			'/\s+/',
-			'',
-			$expected_single_product_template_content
-		);
-		$result_without_whitespace                           = preg_replace( '/\s+/', '', $result[0]->content );
-
 		$this->assertEquals(
-			$expected_single_product_template_without_whitespace,
-			$result_without_whitespace
+			TemplateContentUtils::strip_whitespace( $expected_single_product_template_content ),
+			TemplateContentUtils::strip_whitespace( $result[0]->content )
 		);
 	}

 	/**
-	 * Test that the password form isn't added to the Single Product Template.
-	 *
+	 * @testdox Should not add the password form when the template has no Single Product blocks.
 	 */
 	public function test_no_remove_block_when_no_single_product_is_in_the_template() {
 		$default_single_product_template = '
@@ -235,21 +210,14 @@ class SingleProductTemplateTests extends WP_UnitTestCase {
 			$default_single_product_template
 		);

-		$result_without_whitespace                           = preg_replace( '/\s+/', '', $result );
-		$expected_single_product_template_without_whitespace = preg_replace(
-			'/\s+/',
-			'',
-			$expected_single_product_template
-		);
-
 		$this->assertEquals(
-			$result_without_whitespace,
-			$expected_single_product_template_without_whitespace
+			TemplateContentUtils::strip_whitespace( $expected_single_product_template ),
+			TemplateContentUtils::strip_whitespace( $result )
 		);
 	}

 	/**
-	 * Test that the password form is added to the Single Product Template.
+	 * @testdox Should add the password form to the Single Product template.
 	 */
 	public function test_replace_single_product_blocks_with_input_form() {
 		$default_single_product_template = '
@@ -277,33 +245,14 @@ class SingleProductTemplateTests extends WP_UnitTestCase {
 			$default_single_product_template
 		);

-		$result_without_whitespace                          = preg_replace( '/\s+/', '', $result );
-		$result_without_whitespace_without_custom_pwbox_ids = preg_replace(
-			'/pwbox-\d+/',
-			'',
-			$result_without_whitespace
-		);
-
-		$expected_single_product_template_without_whitespace = preg_replace(
-			'/\s+/',
-			'',
-			$expected_single_product_template
-		);
-
-		$expected_single_product_template_without_whitespace_without_custom_pwbox_ids = preg_replace(
-			'/pwbox-\d+/',
-			'',
-			$expected_single_product_template_without_whitespace
-		);
-
 		$this->assertEquals(
-			$result_without_whitespace_without_custom_pwbox_ids,
-			$expected_single_product_template_without_whitespace_without_custom_pwbox_ids
+			TemplateContentUtils::strip_whitespace_and_password_form_ids( $expected_single_product_template ),
+			TemplateContentUtils::strip_whitespace_and_password_form_ids( $result )
 		);
 	}

 	/**
-	 * Test that the password form is added to the Single Product Template with the default template.
+	 * @testdox Should add the password form to the default Single Product template.
 	 */
 	public function test_replace_default_template_single_product_blocks_with_input_form() {
 		$default_single_product_template = '
@@ -408,28 +357,9 @@ class SingleProductTemplateTests extends WP_UnitTestCase {
 			$default_single_product_template
 		);

-		$result_without_whitespace                          = preg_replace( '/\s+/', '', $result );
-		$result_without_whitespace_without_custom_pwbox_ids = preg_replace(
-			'/pwbox-\d+/',
-			'',
-			$result_without_whitespace
-		);
-
-		$expected_single_product_template_without_whitespace = preg_replace(
-			'/\s+/',
-			'',
-			$expected_single_product_template
-		);
-
-		$expected_single_product_template_without_whitespace_without_custom_pwbox_ids = preg_replace(
-			'/pwbox-\d+/',
-			'',
-			$expected_single_product_template_without_whitespace
-		);
-
 		$this->assertEquals(
-			$result_without_whitespace_without_custom_pwbox_ids,
-			$expected_single_product_template_without_whitespace_without_custom_pwbox_ids
+			TemplateContentUtils::strip_whitespace_and_password_form_ids( $expected_single_product_template ),
+			TemplateContentUtils::strip_whitespace_and_password_form_ids( $result )
 		);
 	}
 }
diff --git a/plugins/woocommerce/tests/php/src/Blocks/Templates/TemplateContentUtils.php b/plugins/woocommerce/tests/php/src/Blocks/Templates/TemplateContentUtils.php
new file mode 100644
index 00000000000..520e3ae0f4c
--- /dev/null
+++ b/plugins/woocommerce/tests/php/src/Blocks/Templates/TemplateContentUtils.php
@@ -0,0 +1,32 @@
+<?php
+declare( strict_types = 1 );
+
+namespace Automattic\WooCommerce\Tests\Blocks\Templates;
+
+/**
+ * Helpers for comparing template HTML in tests.
+ */
+class TemplateContentUtils {
+
+	/**
+	 * Remove whitespace so template HTML can be compared without formatting differences.
+	 *
+	 * @param string $content Template HTML.
+	 * @return string
+	 */
+	public static function strip_whitespace( string $content ): string {
+		return preg_replace( '/\s+/', '', $content );
+	}
+
+	/**
+	 * Remove whitespace and dynamic password form IDs so HTML can be compared.
+	 *
+	 * WordPress generates unique IDs like pwbox-123 on each call to get_the_password_form().
+	 *
+	 * @param string $content Template HTML.
+	 * @return string
+	 */
+	public static function strip_whitespace_and_password_form_ids( string $content ): string {
+		return preg_replace( '/pwbox-\d+/', '', self::strip_whitespace( $content ) );
+	}
+}