Skip to content

t806: test: harden Dashboard_Widgets_Test global cleanup with try/finally#821

Merged
superdav42 merged 1 commit intomainfrom
feature/auto-20260413-120244
Apr 13, 2026
Merged

t806: test: harden Dashboard_Widgets_Test global cleanup with try/finally#821
superdav42 merged 1 commit intomainfrom
feature/auto-20260413-120244

Conversation

@superdav42
Copy link
Copy Markdown
Collaborator

@superdav42 superdav42 commented Apr 13, 2026

Summary

Addresses the unaddressed CodeRabbit review feedback from PR #801 (issue #806).

Problem: Two dashboard widget tests mutate $wp_scripts->done, $pagenow, and screen context but only restore $wp_scripts->queue and $pagenow — and only in the happy path. If an assertion throws, subsequent tests can inherit mutated globals/screen state causing hard-to-diagnose flicker failures.

Fix: For both test_enqueue_scripts_enqueues_activity_stream_on_index and test_enqueue_scripts_skips_activity_stream_on_per_site_dashboard:

  • Capture $original_done and $original_screen_id before mutating state
  • Wrap test body in try/finally so cleanup runs even when assertions throw
  • Restore $wp_scripts->queue, $wp_scripts->done, screen context, and $pagenow in finally

Files changed

  • EDIT: tests/WP_Ultimo/Dashboard_Widgets_Test.php — lines 123–170 and 179–217

Verification

Tests still pass with the same assertions; cleanup is now guaranteed on failure paths.

Resolves #806

Summary by CodeRabbit

  • Tests
    • Enhanced test reliability by improving global state preservation and cleanup procedures in dashboard widget tests.

Addresses CodeRabbit review feedback from PR #801:
- Capture $original_done and screen state before mutating globals
- Wrap test body in try/finally so globals always restored on failure
- Restore $wp_scripts->done and screen context in finally block

Applies to both test_enqueue_scripts_enqueues_activity_stream_on_index
and test_enqueue_scripts_skips_activity_stream_on_per_site_dashboard
to prevent cross-test leakage when assertions throw.

Fixes #806
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 13, 2026

Warning

Rate limit exceeded

@superdav42 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 53 minutes and 44 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 53 minutes and 44 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8f6dfcaa-6665-42fa-a1f6-675104f1717d

📥 Commits

Reviewing files that changed from the base of the PR and between 9327bbe and af4de8a.

📒 Files selected for processing (2)
  • inc/functions/site.php
  • tests/WP_Ultimo/Functions/Site_Functions_Extended_Test.php
📝 Walkthrough

Walkthrough

This PR improves test state isolation in Dashboard_Widgets_Test.php by refactoring two test methods to capture and restore WordPress global state more comprehensively. The tests now snapshot $wp_scripts internals and the current screen object, then use try/finally blocks to guarantee restoration of all state after test execution.

Changes

Cohort / File(s) Summary
Test State Management
tests/WP_Ultimo/Dashboard_Widgets_Test.php
Enhanced two test methods to snapshot and restore $wp_scripts->queue, $wp_scripts->done, current screen object/id, and $pagenow using try/finally blocks. Removed previous cleanup logic that only restored queue and $pagenow.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested labels

origin:interactive

Poem

🐰 A Rabbit's Cleanup Song

States were tangled, globals wild and free,
Try/finally blocks bring harmony!
Screenshots saved before each test runs,
Restore the world when the trial is done. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: hardening Dashboard_Widgets_Test global cleanup with try/finally blocks to ensure reliable test state restoration.
Linked Issues check ✅ Passed The PR directly addresses the coding requirement from issue #806: fix Dashboard_Widgets_Test to reliably clean up global state (wp_scripts, screen context, pagenow) using try/finally to prevent cross-test leakage.
Out of Scope Changes check ✅ Passed All changes are scoped to the two specified test methods in Dashboard_Widgets_Test.php and directly address the global state cleanup issue identified in issue #806; no unrelated changes detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/auto-20260413-120244

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown

🔨 Build Complete - Ready for Testing!

📦 Download Build Artifact (Recommended)

Download the zip build, upload to WordPress and test:

🌐 Test in WordPress Playground (Very Experimental)

Click the link below to instantly test this PR in your browser - no installation needed!
Playground support for multisite is very limitied, hopefully it will get better in the future.

🚀 Launch in Playground

Login credentials: admin / password

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tests/WP_Ultimo/Dashboard_Widgets_Test.php`:
- Around line 130-131: The finally blocks that restore the admin screen state
must also clear the global when there was no original screen; update the restore
logic around $original_screen and $original_screen_id so that if
$original_screen_id is truthy you restore the original screen (e.g., via
set_current_screen or assigning $GLOBALS['current_screen'] back to
$original_screen), and otherwise explicitly unset($GLOBALS['current_screen']) to
prevent the forced screen from leaking into other tests; apply this change in
both test methods that capture $original_screen/$original_screen_id.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d6e5fb41-7422-4b44-85dd-0e3b0a9d0487

📥 Commits

Reviewing files that changed from the base of the PR and between 3dfda95 and 9327bbe.

📒 Files selected for processing (1)
  • tests/WP_Ultimo/Dashboard_Widgets_Test.php

Comment on lines +130 to +131
$original_screen = function_exists('get_current_screen') ? get_current_screen() : null;
$original_screen_id = $original_screen ? $original_screen->id : null;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🌐 Web query:

In WordPress test environments, after calling set_current_screen('dashboard') or set_current_screen('dashboard-network'), what is the recommended way to restore a previously-null screen state: unset($GLOBALS['current_screen']) or set_current_screen('')?

💡 Result:

In WordPress test environments, the recommended way to restore a previously-null screen state after calling set_current_screen('dashboard') or set_current_screen('dashboard-network') is unset($GLOBALS['current_screen']) (along with related globals $taxnow and $typenow).

Citations:


🏁 Script executed:

cat -n tests/WP_Ultimo/Dashboard_Widgets_Test.php | sed -n '125,220p'

Repository: Ultimate-Multisite/ultimate-multisite

Length of output: 3808


Unset $GLOBALS['current_screen'] when restoring to null state.

Both finally blocks skip restoration when $original_screen_id is null/falsy, allowing forced screen states to leak into subsequent tests. The finally blocks must explicitly unset the screen global when the original screen did not exist.

Proposed fix
 		} finally {
 			if (isset($wp_scripts)) {
 				$wp_scripts->queue = $original_queue;
 				$wp_scripts->done  = $original_done;
 			}
-			if ($original_screen_id) {
-				set_current_screen($original_screen_id);
-			}
+			if (null !== $original_screen_id) {
+				set_current_screen($original_screen_id);
+			} else {
+				unset($GLOBALS['current_screen']);
+			}
 			$pagenow = $original;
 		}

Applies to both test methods (lines 165–167 and 212–214).

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$original_screen = function_exists('get_current_screen') ? get_current_screen() : null;
$original_screen_id = $original_screen ? $original_screen->id : null;
} finally {
if (isset($wp_scripts)) {
$wp_scripts->queue = $original_queue;
$wp_scripts->done = $original_done;
}
if (null !== $original_screen_id) {
set_current_screen($original_screen_id);
} else {
unset($GLOBALS['current_screen']);
}
$pagenow = $original;
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/WP_Ultimo/Dashboard_Widgets_Test.php` around lines 130 - 131, The
finally blocks that restore the admin screen state must also clear the global
when there was no original screen; update the restore logic around
$original_screen and $original_screen_id so that if $original_screen_id is
truthy you restore the original screen (e.g., via set_current_screen or
assigning $GLOBALS['current_screen'] back to $original_screen), and otherwise
explicitly unset($GLOBALS['current_screen']) to prevent the forced screen from
leaking into other tests; apply this change in both test methods that capture
$original_screen/$original_screen_id.

@github-actions
Copy link
Copy Markdown

🔨 Build Complete - Ready for Testing!

📦 Download Build Artifact (Recommended)

Download the zip build, upload to WordPress and test:

🌐 Test in WordPress Playground (Very Experimental)

Click the link below to instantly test this PR in your browser - no installation needed!
Playground support for multisite is very limitied, hopefully it will get better in the future.

🚀 Launch in Playground

Login credentials: admin / password

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 13, 2026

Performance Test Results

Performance test results for 6a1bf6f are in 🛎️!

Note: the numbers in parentheses show the difference to the previous (baseline) test run. Differences below 2% or 0.5 in absolute values are not shown.

URL: /

Run DB Queries Memory Before Template Template WP Total LCP TTFB LCP - TTFB
0 41 37.79 MB 900.00 ms (+51.50 ms / +6% ) 155.00 ms (-11.50 ms / -7% ) 1119.00 ms (+90.00 ms / +8% ) 2068.00 ms (+74.00 ms / +4% ) 1967.40 ms (+65.00 ms / +3% ) 94.15 ms (-2.25 ms / -2% )
1 56 49.02 MB 924.50 ms (-35.50 ms / -4% ) 149.00 ms 1073.00 ms (-40.00 ms / -4% ) 2050.00 ms (-42.00 ms / -2% ) 1967.45 ms (-41.85 ms / -2% ) 85.55 ms

@superdav42 superdav42 force-pushed the feature/auto-20260413-120244 branch from af4de8a to 9327bbe Compare April 13, 2026 18:23
@superdav42 superdav42 merged commit 8e7ce13 into main Apr 13, 2026
17 checks passed
@superdav42
Copy link
Copy Markdown
Collaborator Author

MERGE_SUMMARY

Issue: #806 — Review followup: PR #801 — test: add template_id checkout validation tests and fix Dashboard_Widgets_Test
PR: #821 merged to main at 8e7ce13

What was done

Addressed the unaddressed CodeRabbit review feedback from PR #801:

Problem: Two dashboard widget tests ( and ) mutated $wp_scripts->done, $pagenow, and screen context but only restored $wp_scripts->queue and $pagenow — and only in the happy path. If an assertion threw, subsequent tests could inherit mutated globals/screen state, causing hard-to-diagnose cross-test leakage.

Fix applied to tests/WP_Ultimo/Dashboard_Widgets_Test.php:

  • Added $original_done and $original_screen_id captures before mutating state
  • Wrapped test body in try/finally block
  • In finally: restores $wp_scripts->queue, $wp_scripts->done, screen context, and $pagenow

Testing

All required CI checks passed (PHP 8.2, 8.3, 8.4, 8.5; Cypress 8.2 chrome).

@github-actions
Copy link
Copy Markdown

🔨 Build Complete - Ready for Testing!

📦 Download Build Artifact (Recommended)

Download the zip build, upload to WordPress and test:

🌐 Test in WordPress Playground (Very Experimental)

Click the link below to instantly test this PR in your browser - no installation needed!
Playground support for multisite is very limitied, hopefully it will get better in the future.

🚀 Launch in Playground

Login credentials: admin / password

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Review followup: PR #801 — test: add template_id checkout validation tests and fix Dashboard_Widgets_Test

1 participant