|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Regression tests for Payment_Manager pending-payment popup behaviour. |
| 4 | + * |
| 5 | + * @package WP_Ultimo |
| 6 | + */ |
| 7 | + |
| 8 | +namespace WP_Ultimo\Managers; |
| 9 | + |
| 10 | +use WP_UnitTestCase; |
| 11 | +use WP_Ultimo\Models\Customer; |
| 12 | + |
| 13 | +/** |
| 14 | + * Regression tests for Payment_Manager::check_pending_payments() and |
| 15 | + * render_pending_payments(). |
| 16 | + * |
| 17 | + * Ensures that pending and cancelled memberships (from abandoned checkouts) |
| 18 | + * do not trigger the "pending payment" popup on user login, which previously |
| 19 | + * pointed users at WC orders that may no longer exist. |
| 20 | + * |
| 21 | + * @see https://github.com/Ultimate-Multisite/ultimate-multisite/pull/360 |
| 22 | + */ |
| 23 | +class Payment_Manager_Pending_Popup_Test extends WP_UnitTestCase { |
| 24 | + |
| 25 | + private Payment_Manager $manager; |
| 26 | + private Customer $customer; |
| 27 | + private \WP_User $wp_user; |
| 28 | + |
| 29 | + /** |
| 30 | + * Set up a fresh customer and payment manager instance before each test. |
| 31 | + */ |
| 32 | + public function setUp(): void { |
| 33 | + |
| 34 | + parent::setUp(); |
| 35 | + |
| 36 | + $uid = uniqid('popup_'); |
| 37 | + |
| 38 | + $this->customer = wu_create_customer( |
| 39 | + [ |
| 40 | + 'username' => $uid, |
| 41 | + 'email' => $uid . '@example.com', |
| 42 | + 'password' => 'password123', |
| 43 | + ] |
| 44 | + ); |
| 45 | + |
| 46 | + $this->wp_user = $this->customer->get_user(); |
| 47 | + |
| 48 | + $this->manager = Payment_Manager::get_instance(); |
| 49 | + |
| 50 | + delete_user_meta($this->wp_user->ID, 'wu_show_pending_payment_popup'); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * A pending membership (abandoned checkout) must NOT trigger the popup. |
| 55 | + * |
| 56 | + * Before the fix the loop did not skip pending memberships, so any |
| 57 | + * abandoned checkout with a linked WU payment would silently set the meta |
| 58 | + * on every subsequent login. |
| 59 | + */ |
| 60 | + public function test_pending_membership_does_not_trigger_popup(): void { |
| 61 | + |
| 62 | + $product = wu_create_product( |
| 63 | + [ |
| 64 | + 'name' => 'Plan', |
| 65 | + 'slug' => 'plan-popup-pending-' . uniqid(), |
| 66 | + 'amount' => 50.00, |
| 67 | + 'type' => 'plan', |
| 68 | + 'active' => true, |
| 69 | + 'pricing_type' => 'paid', |
| 70 | + 'recurring' => true, |
| 71 | + 'duration' => 1, |
| 72 | + 'duration_unit' => 'month', |
| 73 | + ] |
| 74 | + ); |
| 75 | + |
| 76 | + $membership = wu_create_membership( |
| 77 | + [ |
| 78 | + 'customer_id' => $this->customer->get_id(), |
| 79 | + 'plan_id' => $product->get_id(), |
| 80 | + 'status' => 'pending', |
| 81 | + 'recurring' => true, |
| 82 | + ] |
| 83 | + ); |
| 84 | + |
| 85 | + wu_create_payment( |
| 86 | + [ |
| 87 | + 'customer_id' => $this->customer->get_id(), |
| 88 | + 'membership_id' => $membership->get_id(), |
| 89 | + 'status' => 'pending', |
| 90 | + 'total' => 50.00, |
| 91 | + 'gateway' => 'woocommerce', |
| 92 | + ] |
| 93 | + ); |
| 94 | + |
| 95 | + $this->manager->check_pending_payments($this->wp_user); |
| 96 | + |
| 97 | + $this->assertEmpty( |
| 98 | + get_user_meta($this->wp_user->ID, 'wu_show_pending_payment_popup', true), |
| 99 | + 'A pending membership must not trigger the pending payment popup.' |
| 100 | + ); |
| 101 | + |
| 102 | + $membership->delete(); |
| 103 | + $product->delete(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * A cancelled membership must NOT trigger the popup. |
| 108 | + */ |
| 109 | + public function test_cancelled_membership_does_not_trigger_popup(): void { |
| 110 | + |
| 111 | + $product = wu_create_product( |
| 112 | + [ |
| 113 | + 'name' => 'Plan', |
| 114 | + 'slug' => 'plan-popup-cancelled-' . uniqid(), |
| 115 | + 'amount' => 50.00, |
| 116 | + 'type' => 'plan', |
| 117 | + 'active' => true, |
| 118 | + 'pricing_type' => 'paid', |
| 119 | + 'recurring' => true, |
| 120 | + 'duration' => 1, |
| 121 | + 'duration_unit' => 'month', |
| 122 | + ] |
| 123 | + ); |
| 124 | + |
| 125 | + $membership = wu_create_membership( |
| 126 | + [ |
| 127 | + 'customer_id' => $this->customer->get_id(), |
| 128 | + 'plan_id' => $product->get_id(), |
| 129 | + 'status' => 'cancelled', |
| 130 | + 'recurring' => true, |
| 131 | + ] |
| 132 | + ); |
| 133 | + |
| 134 | + wu_create_payment( |
| 135 | + [ |
| 136 | + 'customer_id' => $this->customer->get_id(), |
| 137 | + 'membership_id' => $membership->get_id(), |
| 138 | + 'status' => 'pending', |
| 139 | + 'total' => 50.00, |
| 140 | + 'gateway' => 'woocommerce', |
| 141 | + ] |
| 142 | + ); |
| 143 | + |
| 144 | + $this->manager->check_pending_payments($this->wp_user); |
| 145 | + |
| 146 | + $this->assertEmpty( |
| 147 | + get_user_meta($this->wp_user->ID, 'wu_show_pending_payment_popup', true), |
| 148 | + 'A cancelled membership must not trigger the pending payment popup.' |
| 149 | + ); |
| 150 | + |
| 151 | + $membership->delete(); |
| 152 | + $product->delete(); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * An active membership with a genuine pending payment MUST trigger the popup. |
| 157 | + * Validates that the skip only applies to pending/cancelled memberships and |
| 158 | + * does not suppress legitimate payment reminders. |
| 159 | + */ |
| 160 | + public function test_active_membership_with_pending_payment_triggers_popup(): void { |
| 161 | + |
| 162 | + $product = wu_create_product( |
| 163 | + [ |
| 164 | + 'name' => 'Plan', |
| 165 | + 'slug' => 'plan-popup-active-' . uniqid(), |
| 166 | + 'amount' => 50.00, |
| 167 | + 'type' => 'plan', |
| 168 | + 'active' => true, |
| 169 | + 'pricing_type' => 'paid', |
| 170 | + 'recurring' => true, |
| 171 | + 'duration' => 1, |
| 172 | + 'duration_unit' => 'month', |
| 173 | + ] |
| 174 | + ); |
| 175 | + |
| 176 | + $membership = wu_create_membership( |
| 177 | + [ |
| 178 | + 'customer_id' => $this->customer->get_id(), |
| 179 | + 'plan_id' => $product->get_id(), |
| 180 | + 'status' => 'active', |
| 181 | + 'recurring' => true, |
| 182 | + ] |
| 183 | + ); |
| 184 | + |
| 185 | + wu_create_payment( |
| 186 | + [ |
| 187 | + 'customer_id' => $this->customer->get_id(), |
| 188 | + 'membership_id' => $membership->get_id(), |
| 189 | + 'status' => 'pending', |
| 190 | + 'total' => 50.00, |
| 191 | + 'gateway' => 'woocommerce', |
| 192 | + ] |
| 193 | + ); |
| 194 | + |
| 195 | + $this->manager->check_pending_payments($this->wp_user); |
| 196 | + |
| 197 | + $this->assertNotEmpty( |
| 198 | + get_user_meta($this->wp_user->ID, 'wu_show_pending_payment_popup', true), |
| 199 | + 'An active membership with a pending payment must trigger the popup.' |
| 200 | + ); |
| 201 | + |
| 202 | + $membership->delete(); |
| 203 | + $product->delete(); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Delete test data and reset state after each test. |
| 208 | + */ |
| 209 | + public function tearDown(): void { |
| 210 | + |
| 211 | + global $wpdb; |
| 212 | + $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}wu_memberships WHERE customer_id = %d", $this->customer->get_id())); |
| 213 | + $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}wu_payments WHERE customer_id = %d", $this->customer->get_id())); |
| 214 | + delete_user_meta($this->wp_user->ID, 'wu_show_pending_payment_popup'); |
| 215 | + $this->customer->delete(); |
| 216 | + |
| 217 | + parent::tearDown(); |
| 218 | + } |
| 219 | +} |
0 commit comments