Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 75 additions & 55 deletions tests/WP_Ultimo/Dashboard_Widgets_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,39 +124,49 @@ public function test_enqueue_scripts_enqueues_activity_stream_on_index(): void {

global $pagenow, $wp_scripts;

$original = $pagenow;
$original_queue = isset($wp_scripts) ? $wp_scripts->queue : [];
$pagenow = 'index.php';

if (isset($wp_scripts)) {
$wp_scripts->queue = [];
$wp_scripts->done = [];
}

// Simulate the network admin so is_network_admin() returns true.
set_current_screen('dashboard-network');

// Ensure wu-functions is registered so the dependency chain resolves.
\WP_Ultimo\Scripts::get_instance()->register_default_scripts();

$instance = $this->get_instance();
$instance->enqueue_scripts();

$this->assertTrue(
wp_script_is('wu-activity-stream', 'enqueued'),
'wu-activity-stream should be enqueued on the network admin index.php'
);

// Verify wu-functions and moment are declared dependencies.
$script = $wp_scripts->registered['wu-activity-stream'] ?? null;
$this->assertNotNull($script, 'wu-activity-stream should be registered');
$this->assertContains('wu-functions', $script->deps);
$this->assertContains('moment', $script->deps);

if (isset($wp_scripts)) {
$wp_scripts->queue = $original_queue;
$original = $pagenow;
$original_queue = isset($wp_scripts) ? $wp_scripts->queue : [];
$original_done = isset($wp_scripts) ? $wp_scripts->done : [];
$original_screen = function_exists('get_current_screen') ? get_current_screen() : null;
$original_screen_id = $original_screen ? $original_screen->id : null;
Comment on lines +130 to +131
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.

$pagenow = 'index.php';

try {
if (isset($wp_scripts)) {
$wp_scripts->queue = [];
$wp_scripts->done = [];
}

// Simulate the network admin so is_network_admin() returns true.
set_current_screen('dashboard-network');

// Ensure wu-functions is registered so the dependency chain resolves.
\WP_Ultimo\Scripts::get_instance()->register_default_scripts();

$instance = $this->get_instance();
$instance->enqueue_scripts();

$this->assertTrue(
wp_script_is('wu-activity-stream', 'enqueued'),
'wu-activity-stream should be enqueued on the network admin index.php'
);

// Verify wu-functions and moment are declared dependencies.
$script = $wp_scripts->registered['wu-activity-stream'] ?? null;
$this->assertNotNull($script, 'wu-activity-stream should be registered');
$this->assertContains('wu-functions', $script->deps);
$this->assertContains('moment', $script->deps);

} 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);
}
$pagenow = $original;
}
$pagenow = $original;
}

/**
Expand All @@ -170,30 +180,40 @@ public function test_enqueue_scripts_skips_activity_stream_on_per_site_dashboard

global $pagenow, $wp_scripts;

$original = $pagenow;
$original_queue = isset($wp_scripts) ? $wp_scripts->queue : [];
$pagenow = 'index.php';

if (isset($wp_scripts)) {
$wp_scripts->queue = [];
$wp_scripts->done = [];
}

// Simulate the per-site dashboard (not network admin).
set_current_screen('dashboard');

$instance = $this->get_instance();
$instance->enqueue_scripts();

$this->assertFalse(
wp_script_is('wu-activity-stream', 'enqueued'),
'wu-activity-stream should NOT be enqueued on the per-site dashboard'
);

if (isset($wp_scripts)) {
$wp_scripts->queue = $original_queue;
$original = $pagenow;
$original_queue = isset($wp_scripts) ? $wp_scripts->queue : [];
$original_done = isset($wp_scripts) ? $wp_scripts->done : [];
$original_screen = function_exists('get_current_screen') ? get_current_screen() : null;
$original_screen_id = $original_screen ? $original_screen->id : null;
$pagenow = 'index.php';

try {
if (isset($wp_scripts)) {
$wp_scripts->queue = [];
$wp_scripts->done = [];
}

// Simulate the per-site dashboard (not network admin).
set_current_screen('dashboard');

$instance = $this->get_instance();
$instance->enqueue_scripts();

$this->assertFalse(
wp_script_is('wu-activity-stream', 'enqueued'),
'wu-activity-stream should NOT be enqueued on the per-site dashboard'
);

} 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);
}
$pagenow = $original;
}
$pagenow = $original;
}

/**
Expand Down
Loading