Skip to content

test: add template_id checkout validation tests and fix Dashboard_Widgets_Test#801

Merged
superdav42 merged 1 commit intomainfrom
bugfix/template-validation
Apr 12, 2026
Merged

test: add template_id checkout validation tests and fix Dashboard_Widgets_Test#801
superdav42 merged 1 commit intomainfrom
bugfix/template-validation

Conversation

@superdav42
Copy link
Copy Markdown
Collaborator

@superdav42 superdav42 commented Apr 12, 2026

Summary

Test results

  • Dashboard_Widgets_Test: 13/13 pass (was 11 pass + 1 fail)
  • New template_id validation tests: 7/7 pass

Do not merge

This PR is labelled do-not-merge for manual review before merging.

Summary by CodeRabbit

  • Tests
    • Enhanced test coverage for checkout field validation, ensuring template selection requirements are properly enforced.
    • Improved dashboard widget tests to validate correct behavior across network admin and per-site dashboard contexts.

…gets_Test

Add 7 unit tests covering the template_selection → template_id validation
mapping fix from PR #800:
- template_selection required attribute maps to template_id rule key
- min:1 guard rejects template_id=0 during checkout
- positive template_id passes validation
- no min:1 added when template_selection field is absent
- template_id=0 allowed with base rule (admin/network context)
- non-template required fields still map to themselves

Fix Dashboard_Widgets_Test failure caused by PR #785 adding
is_network_admin() guard to enqueue_scripts(). The test now uses
set_current_screen('dashboard-network') to simulate the network admin
context. Also adds a new test confirming the per-site dashboard does
NOT enqueue wu-activity-stream.
@superdav42 superdav42 added origin:interactive Created by interactive user session do-not-merge PR should not be merged automatically by the pulse labels Apr 12, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 12, 2026

📝 Walkthrough

Walkthrough

This PR adds comprehensive PHPUnit test coverage for Checkout validation rules tied to template_selection field behavior and Dashboard_Widgets enqueue script context handling (network vs per-site admin scenarios). All changes are test-only additions without production code modifications.

Changes

Cohort / File(s) Summary
Checkout Validation Tests
tests/WP_Ultimo/Checkout/Checkout_Test.php
Added 7 new test methods validating template_id constraints: required field mapping, min:1 constraint application when template_selection is present, rejection of template_id = 0 when required, and absence of min:1 when template_selection field is absent.
Dashboard Widgets Enqueue Tests
tests/WP_Ultimo/Dashboard_Widgets_Test.php
Updated existing test to explicitly simulate network admin context, and added new test asserting wu-activity-stream asset is not enqueued on per-site dashboard.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐰 A rabbit hops through test suites with glee,
Validating templates and dashboards we see,
With assertions so crisp and conditions so bright,
Each edge case covered—the logic's just right!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately summarizes the main changes: adding template_id checkout validation tests and fixing Dashboard_Widgets_Test.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bugfix/template-validation

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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tests/WP_Ultimo/Dashboard_Widgets_Test.php (1)

125-160: ⚠️ Potential issue | 🟡 Minor

Harden global cleanup to avoid cross-test leakage on failure.

Line 131/177 resets $wp_scripts->done, and Line 137/183 changes screen context, but cleanup only restores $wp_scripts->queue and $pagenow. If an assertion throws, subsequent tests can inherit mutated globals/screen state.

Suggested fix (apply to both tests)
 	global $pagenow, $wp_scripts;

 	$original       = $pagenow;
-	$original_queue = isset($wp_scripts) ? $wp_scripts->queue : [];
+	$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';

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

-	set_current_screen('dashboard-network');
+		set_current_screen('dashboard-network');

-	\WP_Ultimo\Scripts::get_instance()->register_default_scripts();
+		\WP_Ultimo\Scripts::get_instance()->register_default_scripts();

-	$instance = $this->get_instance();
-	$instance->enqueue_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'
-	);
+		$this->assertTrue(
+			wp_script_is('wu-activity-stream', 'enqueued'),
+			'wu-activity-stream should be enqueued on the network admin index.php'
+		);
+	} 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;
+	}

Also applies to: 171-197

🤖 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 125 - 160, Wrap the
test setup and assertions in a try/finally so globals are always restored;
capture the original screen (e.g. $original_screen = get_current_screen()),
original $pagenow and original $wp_scripts->queue/done, then in finally restore
$pagenow, restore $wp_scripts->queue and $wp_scripts->done (checking
isset($wp_scripts) first), and restore the screen via
set_current_screen($original_screen) so failures in enqueue_scripts()/assertions
cannot leak mutated state; apply the same pattern to both tests referencing
enqueue_scripts(), $pagenow, $wp_scripts and set_current_screen().
🧹 Nitpick comments (1)
tests/WP_Ultimo/Checkout/Checkout_Test.php (1)

4657-4671: Tighten this test to exercise generated checkout rules, not only a hardcoded rule string.

Line 4669 currently validates integer|min:1 directly, which is good for rule semantics but weaker for checkout-rule integration.

♻️ Suggested adjustment
 public function test_validate_accepts_positive_template_id(): void {
 
 	$checkout            = Checkout::get_instance();
-	$checkout->step      = ['fields' => []];
+	$checkout->step      = [
+		'fields' => [
+			['id' => 'template_selection', 'required' => true],
+		],
+	];
 	$checkout->steps     = [];
 	$checkout->step_name = null;
 
 	$this->ensure_session($checkout);
 
 	$_REQUEST['template_id'] = 5;
+	unset($_REQUEST['pre-flight'], $_REQUEST['checkout_form']);
 
-	// min:1 should pass for value 5
-	$result = $checkout->validate(['template_id' => 'integer|min:1']);
+	$rules = $checkout->get_validation_rules();
+	$this->assertStringContainsString('min:1', $rules['template_id']);
+	$result = $checkout->validate(['template_id' => 'integer|required|min:1']);
 
 	$this->assertTrue($result);
 
 	unset($_REQUEST['template_id']);
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/WP_Ultimo/Checkout/Checkout_Test.php` around lines 4657 - 4671, The
test is currently validating a hardcoded rule string; instead fetch the
checkout's generated rules and exercise them via the Checkout::validate method.
In test_validate_accepts_positive_template_id, obtain the rules from the
Checkout instance (e.g. $checkout->rules() or the method that returns generated
validation rules), extract the 'template_id' rule from that rules array, then
call $checkout->validate(['template_id' => 5], $rulesSubset) or validate against
the full rules array to ensure the generated checkout rules (not a hardcoded
string) accept a positive template_id; keep using Checkout::get_instance(), the
test method name, and validate() to locate and update the code.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@tests/WP_Ultimo/Dashboard_Widgets_Test.php`:
- Around line 125-160: Wrap the test setup and assertions in a try/finally so
globals are always restored; capture the original screen (e.g. $original_screen
= get_current_screen()), original $pagenow and original $wp_scripts->queue/done,
then in finally restore $pagenow, restore $wp_scripts->queue and
$wp_scripts->done (checking isset($wp_scripts) first), and restore the screen
via set_current_screen($original_screen) so failures in
enqueue_scripts()/assertions cannot leak mutated state; apply the same pattern
to both tests referencing enqueue_scripts(), $pagenow, $wp_scripts and
set_current_screen().

---

Nitpick comments:
In `@tests/WP_Ultimo/Checkout/Checkout_Test.php`:
- Around line 4657-4671: The test is currently validating a hardcoded rule
string; instead fetch the checkout's generated rules and exercise them via the
Checkout::validate method. In test_validate_accepts_positive_template_id, obtain
the rules from the Checkout instance (e.g. $checkout->rules() or the method that
returns generated validation rules), extract the 'template_id' rule from that
rules array, then call $checkout->validate(['template_id' => 5], $rulesSubset)
or validate against the full rules array to ensure the generated checkout rules
(not a hardcoded string) accept a positive template_id; keep using
Checkout::get_instance(), the test method name, and validate() to locate and
update the code.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 41eda309-aae8-4794-852e-7eb34abe0540

📥 Commits

Reviewing files that changed from the base of the PR and between cbeaa88 and 8d906ad.

📒 Files selected for processing (2)
  • tests/WP_Ultimo/Checkout/Checkout_Test.php
  • tests/WP_Ultimo/Dashboard_Widgets_Test.php

@github-actions
Copy link
Copy Markdown

Performance Test Results

Performance test results for 849c73f 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.82 MB 885.00 ms (+19.00 ms / +2% ) 156.50 ms (-3.50 ms / -2% ) 1093.00 ms 2056.00 ms (-54.00 ms / -3% ) 1963.45 ms (-66.65 ms / -3% ) 93.60 ms
1 56 49.02 MB 970.00 ms (+32.00 ms / +3% ) 145.50 ms (+3.50 ms / +2% ) 1114.00 ms (+29.50 ms / +3% ) 2146.00 ms (+66.00 ms / +3% ) 2062.30 ms (+64.85 ms / +3% ) 83.45 ms (+2.30 ms / +3% )

@superdav42 superdav42 merged commit 78e8f9c into main Apr 12, 2026
11 checks passed
@superdav42
Copy link
Copy Markdown
Collaborator Author

Summary

Test results

  • Dashboard_Widgets_Test: 13/13 pass (was 11 pass + 1 fail)
  • New template_id validation tests: 7/7 pass

Do not merge

This PR is labelled do-not-merge for manual review before merging.


Merged via PR #801 to main.
Merged by deterministic merge pass (pulse-wrapper.sh).


aidevops.sh v3.6.244 spent 8m on this as a headless bash routine.

superdav42 added a commit that referenced this pull request Apr 13, 2026
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
superdav42 added a commit that referenced this pull request Apr 13, 2026
…821)

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

- Capture $original_done and screen state before mutating globals
- Wrap test body in try/finally so globals always restored on failure  
- Restore $wp_scripts->queue, $wp_scripts->done, screen context, and $pagenow in finally

Also includes: fix: sanitize subdomain slug in wu_create_site (issue #812)

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

Labels

do-not-merge PR should not be merged automatically by the pulse origin:interactive Created by interactive user session

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant