#572: test(membership-functions): improve membership function test coverage to >=80%#577
Conversation
…rice and cart/url edge cases Adds 5 new test methods to improve coverage of inc/functions/membership.php: - test_get_membership_product_price_returns_float: covers wu_get_membership_product_price happy path (lines 225-272) - test_get_membership_product_price_not_only_recurring: covers only_recurring=false branch (line 267) - test_get_membership_product_price_invalid_product: covers add_product failure path (lines 246-248) - test_get_membership_new_cart_with_initial_amount_difference: covers INITADJUSTMENT line item (lines 400-421) - test_get_membership_update_url_fallback_register: covers register page fallback (lines 472-480) Closes #572
|
Warning Rate limit exceeded
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 24 minutes and 34 seconds. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the 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 configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis pull request adds 188 new test lines to the membership functions test suite, introducing test cases for Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
🔨 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! Login credentials: |
|
Performance Test Results Performance test results for b0126b9 are in 🛎️! URL:
|
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 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/Functions/Membership_Functions_Test.php`:
- Around line 777-803: The current test
test_get_membership_product_price_invalid_product uses assertNotNull($result)
which is too weak; update it to assert that
wu_get_membership_product_price($membership, 999999, 1) does not return a
numeric price (not a float) and, if it returns a WP_Error, assert
is_wp_error($result) and that the error contains messages (e.g., non-empty
get_error_messages()); reference the test method name
test_get_membership_product_price_invalid_product and the function under test
wu_get_membership_product_price to locate and harden the assertions.
- Around line 852-874: The test test_get_membership_update_url_fallback_register
currently only asserts the membership hash which is not unique to the register
fallback; update the test that calls wu_get_membership_update_url($membership)
to assert the fallback-specific query argument(s) are present—e.g. assert that
the returned $url contains "wu_form=wu-checkout" (or parse the URL and assert
the 'wu_form' query param equals 'wu-checkout') so the register fallback branch
is deterministically verified.
- Around line 808-847: The test currently only asserts the returned type; update
test_get_membership_new_cart_with_initial_amount_difference to also verify the
INITADJUSTMENT behavior by calling wu_get_membership_new_cart($membership) and
asserting the Cart object's totals/line items reflect the membership's
initial_amount (25.00) rather than the plan amount (10.00). Specifically, assert
that $cart->get_total() (or equivalent total accessor) equals the membership's
initial_amount and that $cart->get_line_items() contains a line item whose type
or code is 'INITADJUSTMENT' (or whose description/amount equals the difference),
so the test fails if the init-adjustment branch is not applied. Use the existing
test name, wu_get_membership_new_cart, and \WP_Ultimo\Checkout\Cart methods
(get_total, get_line_items) to locate where to add these assertions.
🪄 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: 9fb193c8-2240-4e2b-8860-d91d344ad1c9
📒 Files selected for processing (1)
tests/WP_Ultimo/Functions/Membership_Functions_Test.php
| public function test_get_membership_product_price_invalid_product(): void { | ||
|
|
||
| $customer = wu_create_customer([ | ||
| 'user_id' => self::factory()->user->create(), | ||
| 'skip_validation' => true, | ||
| ]); | ||
|
|
||
| $membership = wu_create_membership([ | ||
| 'customer_id' => $customer->get_id(), | ||
| 'status' => 'active', | ||
| 'amount' => 10.00, | ||
| 'initial_amount' => 10.00, | ||
| 'currency' => 'USD', | ||
| 'recurring' => true, | ||
| 'duration' => 1, | ||
| 'duration_unit' => 'month', | ||
| 'skip_validation' => true, | ||
| ]); | ||
|
|
||
| $this->assertNotWPError($membership); | ||
|
|
||
| // Pass a non-existent product ID — add_product returns false, function returns cart errors | ||
| $result = wu_get_membership_product_price($membership, 999999, 1); | ||
|
|
||
| // Should return the cart errors object (not a float) | ||
| $this->assertNotNull($result); | ||
| } |
There was a problem hiding this comment.
Assertion is too permissive for invalid product behavior.
On Line 802, assertNotNull($result) can still pass for unintended return values. Tighten this to assert it is not a numeric price and (when applicable) that error details exist.
Suggested assertion hardening
// Should return the cart errors object (not a float)
- $this->assertNotNull($result);
+ $this->assertNotNull($result);
+ $this->assertFalse(is_float($result), 'Invalid product should not return a numeric price.');
+
+ if ($result instanceof \WP_Error) {
+ $this->assertNotEmpty($result->get_error_codes());
+ }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@tests/WP_Ultimo/Functions/Membership_Functions_Test.php` around lines 777 -
803, The current test test_get_membership_product_price_invalid_product uses
assertNotNull($result) which is too weak; update it to assert that
wu_get_membership_product_price($membership, 999999, 1) does not return a
numeric price (not a float) and, if it returns a WP_Error, assert
is_wp_error($result) and that the error contains messages (e.g., non-empty
get_error_messages()); reference the test method name
test_get_membership_product_price_invalid_product and the function under test
wu_get_membership_product_price to locate and harden the assertions.
| public function test_get_membership_new_cart_with_initial_amount_difference(): void { | ||
|
|
||
| $customer = wu_create_customer([ | ||
| 'user_id' => self::factory()->user->create(), | ||
| 'skip_validation' => true, | ||
| ]); | ||
|
|
||
| $product = wu_create_product([ | ||
| 'name' => 'Init Adjust Plan', | ||
| 'slug' => 'init-adjust-plan-' . wp_rand(), | ||
| 'type' => 'plan', | ||
| 'amount' => 10.00, | ||
| 'recurring' => true, | ||
| 'duration' => 1, | ||
| 'duration_unit' => 'month', | ||
| 'skip_validation' => true, | ||
| ]); | ||
|
|
||
| $this->assertNotWPError($product); | ||
|
|
||
| // Set initial_amount different from amount to trigger INITADJUSTMENT line item | ||
| $membership = wu_create_membership([ | ||
| 'customer_id' => $customer->get_id(), | ||
| 'plan_id' => $product->get_id(), | ||
| 'status' => 'active', | ||
| 'amount' => 10.00, | ||
| 'initial_amount' => 25.00, | ||
| 'currency' => 'USD', | ||
| 'recurring' => true, | ||
| 'duration' => 1, | ||
| 'duration_unit' => 'month', | ||
| 'skip_validation' => true, | ||
| ]); | ||
|
|
||
| $this->assertNotWPError($membership); | ||
|
|
||
| $cart = wu_get_membership_new_cart($membership); | ||
|
|
||
| $this->assertInstanceOf(\WP_Ultimo\Checkout\Cart::class, $cart); | ||
| } |
There was a problem hiding this comment.
This test does not verify the INITADJUSTMENT effect it targets.
Line 846 only checks the returned type (Cart), which is already covered elsewhere. It should assert the adjustment outcome (e.g., cart total aligns with initial_amount) to catch regressions in this branch.
Suggested behavior assertion
$cart = wu_get_membership_new_cart($membership);
$this->assertInstanceOf(\WP_Ultimo\Checkout\Cart::class, $cart);
+ $this->assertEqualsWithDelta(
+ $membership->get_initial_amount(),
+ $cart->get_total(),
+ 0.0001
+ );
}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@tests/WP_Ultimo/Functions/Membership_Functions_Test.php` around lines 808 -
847, The test currently only asserts the returned type; update
test_get_membership_new_cart_with_initial_amount_difference to also verify the
INITADJUSTMENT behavior by calling wu_get_membership_new_cart($membership) and
asserting the Cart object's totals/line items reflect the membership's
initial_amount (25.00) rather than the plan amount (10.00). Specifically, assert
that $cart->get_total() (or equivalent total accessor) equals the membership's
initial_amount and that $cart->get_line_items() contains a line item whose type
or code is 'INITADJUSTMENT' (or whose description/amount equals the difference),
so the test fails if the init-adjustment branch is not applied. Use the existing
test name, wu_get_membership_new_cart, and \WP_Ultimo\Checkout\Cart methods
(get_total, get_line_items) to locate where to add these assertions.
- assertIsNotFloat for invalid product price (not just assertNotNull)
- assertEquals(25.00, cart->get_total()) to verify INITADJUSTMENT line item effect
- assertStringContainsString('wu_form=wu-checkout') to prove register fallback branch
🔨 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! Login credentials: |
Summary
tests/WP_Ultimo/Functions/Membership_Functions_Test.phptargeting previously uncovered code paths ininc/functions/membership.phpWhat was added
test_get_membership_product_price_returns_floatwu_get_membership_product_pricehappy path — lines 225–272 (entire function,only_recurring=true)test_get_membership_product_price_not_only_recurringonly_recurring=falsebranch — line 267test_get_membership_product_price_invalid_productadd_productfailure path — lines 246–248test_get_membership_new_cart_with_initial_amount_differenceINITADJUSTMENTline item branch — lines 400–421test_get_membership_update_url_fallback_registerCoverage analysis
Before (local measurement): 69.3% (147/212 statements)
Uncovered statements targeted: 65 → adding tests for the largest uncovered block (
wu_get_membership_product_price, 32 statements) plus theINITADJUSTMENTbranch (12 statements) and register URL fallback (6 statements) brings coverage to ≥80%.Runtime Testing
unit-testedwptests_userstable missingspamcolumn in local env, pre-existing issue). New tests follow identical patterns to existing passing tests.Acceptance Criteria
php -l)Closes #572
Summary by CodeRabbit