Commit f15ff214c69 for woocommerce

commit f15ff214c69dbeccf6247916dd8cbec0aef64fa6
Author: Adrian Moldovan <3854374+adimoldovan@users.noreply.github.com>
Date:   Thu Jul 9 15:31:59 2026 +0300

    php tests: Balance output buffers in WC_AJAX order-add-meta nonce test (#66425)

diff --git a/plugins/woocommerce/changelog/php-tests-unbalanced-output-buffers-risky-test b/plugins/woocommerce/changelog/php-tests-unbalanced-output-buffers-risky-test
new file mode 100644
index 00000000000..e59bcbed74b
--- /dev/null
+++ b/plugins/woocommerce/changelog/php-tests-unbalanced-output-buffers-risky-test
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+
+Balance the output-buffer level in the WC_AJAX order-add-meta nonce test so it is no longer reported as risky.
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-ajax-test.php b/plugins/woocommerce/tests/php/includes/class-wc-ajax-test.php
index b866bd0b103..db1872c54c5 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-ajax-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-ajax-test.php
@@ -107,11 +107,18 @@ class WC_AJAX_Test extends \WP_Ajax_UnitTestCase {
 		$_POST['permissions'] = 'read';
 		$_POST['description'] = $description;

+		$output_buffering_level = ob_get_level();
+
 		try {
 			$this->_handleAjax( 'woocommerce_update_api_key' );
 		} catch ( WPAjaxDieContinueException $e ) {
-			// wp_die() doesn't actually occur, so we need to clean up WC_AJAX::update_api_key's output buffer.
-			ob_end_clean();
+			unset( $e );
+		} finally {
+			// wp_die() doesn't actually occur, so clean up any output buffer
+			// WC_AJAX::update_api_key leaves open, keeping the level balanced.
+			while ( ob_get_level() > $output_buffering_level ) {
+				ob_end_clean();
+			}
 		}

 		$response = json_decode( $this->_last_response, true );
@@ -650,10 +657,19 @@ class WC_AJAX_Test extends \WP_Ajax_UnitTestCase {
 		$_POST['metakeyinput']         = 'my_test_key';
 		$_POST['metavalue']            = 'my_test_value';

+		$output_buffering_level = ob_get_level();
+
 		try {
+			// Note that _handleAjax makes use of output buffering, which the die
+			// handler usually cleans up; the finally block below closes only any
+			// buffer it leaves dangling so the buffer level stays balanced.
 			$this->_handleAjax( 'woocommerce_order_add_meta' );
 		} catch ( WPAjaxDieContinueException $e ) {
-			ob_end_clean();
+			unset( $e );
+		} finally {
+			while ( ob_get_level() > $output_buffering_level ) {
+				ob_end_clean();
+			}
 		}

 		$this->assertStringContainsString(
@@ -674,13 +690,15 @@ class WC_AJAX_Test extends \WP_Ajax_UnitTestCase {
 		$output_buffering_level = ob_get_level();

 		try {
-			// Note that _handleAjax makes use of output buffering...
+			// Note that _handleAjax makes use of output buffering, which the die
+			// handler usually cleans up; the finally block below closes only any
+			// buffer it leaves dangling so the buffer level stays balanced.
 			$this->_handleAjax( $ajax_action );
 		} catch ( Exception $e ) {
-			// ...However, if an exception is raised, it may not be able to clean-up,
-			// which can lead to PhpUnit emitting risky test warnings.
-			if ( ob_get_level() === $output_buffering_level + 1 ) {
-				ob_get_clean();
+			unset( $e );
+		} finally {
+			while ( ob_get_level() > $output_buffering_level ) {
+				ob_end_clean();
 			}
 		}