Skip to content

Conversation

@priethor
Copy link
Contributor

@priethor priethor commented Dec 31, 2025

What

Closes #334.

Replaces weak assertTrue(true) assertions in WC_Order form with behavior testing.

Why

To make tests more meaningful.

How

Refactoring 5 tests to use specific assertions:

  • test_add_meta_boxes_returns_early_for_null_order: Verifying the order_edit_form_top action is not added when order is null.
  • test_order_edit_form_top_uses_correct_post_id_format: Checking output contains woo_order_{id} format and the acf-form-data container.
  • test_render_meta_box_uses_correct_post_id_format: Using a data provider to test both WP_Post and WC_Order inputs, verifying the acf/render_fields action receives the correct post_id format.
  • test_save_order_requires_hpos: Confirming the action is not removed when HPOS is disabled (early return).

Consolidating two similar render_meta_box tests into a single data-provider-driven test to reduce duplication.

Testing Instructions

Run the WC_Order form tests:

composer test:php -- --filter Test_Form_WC_Order --testdox

…a summary:

## Summary

Refactored 5 weak `assertTrue(true)` assertions in `test-form-wc-order.php` with meaningful assertions:

| Test | Before | After |
|------|--------|-------|
| `test_add_meta_boxes_returns_early_for_null_order` | `assertTrue(true)` | `assertFalse(has_action(...))` - verifies the action is NOT added when order is null |
| `test_order_edit_form_top_uses_correct_post_id_format` | `assertTrue(true)` | `assertStringContainsString()` - verifies output contains `woo_order_456` and `acf-form-data` container |
| `test_render_meta_box_handles_wp_post` | `assertTrue(true)` | `assertEquals()` via `acf/render_fields` action hook - verifies post_id is `woo_order_789` |
| `test_render_meta_box_handles_wc_order` | `assertTrue(true)` | `assertEquals()` via `acf/render_fields` action hook - verifies post_id is `woo_order_321` |
| `test_save_order_requires_hpos` | `assertTrue(true)` | `assertNotFalse(has_action(...))` - verifies action is NOT removed when HPOS is disabled |

All 23 tests pass with 28 assertions (up from 23 assertions before).
**Issues found and fixed:**

1. **Action callback leaks** - The `acf/render_fields` action callbacks were added but never removed, potentially polluting other tests. Fixed by storing the callback in a variable and calling `remove_action()` after the test.

2. **No verification that action was called** - The tests would pass even if `acf/render_fields` was never fired (e.g., if `acf_get_fields` returned empty). Added `assertNotNull($rendered_post_id)` to verify the action was actually triggered.

3. **Unused `$output` variable** - The `ob_get_clean()` result was stored but never used. Changed to just `ob_get_clean()` without assignment.

**Final state:**
- 23 tests, 30 assertions (up from original 23 assertions)
- All tests pass
- Lint passes
- Tests are now more honest and properly isolated
**Refactoring done:**
- Consolidated `test_render_meta_box_handles_wp_post` and `test_render_meta_box_handles_wc_order` into a single data-provider-driven test `test_render_meta_box_uses_correct_post_id_format`
- Reduced code duplication by ~30 lines
- Same test coverage (23 tests, 30 assertions)
@github-actions
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props priethor.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@codecov
Copy link

codecov bot commented Dec 31, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 25.13%. Comparing base (e26e0e4) to head (0689293).
⚠️ Report is 1 commits behind head on trunk.

Additional details and impacted files
@@            Coverage Diff            @@
##              trunk     #335   +/-   ##
=========================================
  Coverage     25.13%   25.13%           
  Complexity     4410     4410           
=========================================
  Files           291      291           
  Lines         36916    36916           
=========================================
  Hits           9278     9278           
  Misses        27638    27638           
Flag Coverage Δ
e2e 28.87% <ø> (ø)
javascript 6.53% <ø> (ø)
phpunit 24.98% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@priethor priethor self-assigned this Dec 31, 2025
@priethor priethor added this to the 6.9.0 milestone Dec 31, 2025
@priethor priethor changed the title Refactor weak assertions in test-form-wc-order.php Refactor weak assertions in test-form-wc-order.php Dec 31, 2025
@priethor priethor merged commit f7cf76b into trunk Dec 31, 2025
10 checks passed
@priethor priethor deleted the fix/334 branch December 31, 2025 11:13
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.

Tests: Refactor weak assertions in test-form-wc-order.php

2 participants