|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the WC_Subscriptions_Cart class. |
| 4 | + */ |
| 5 | +class WC_Subscriptions_Cart_Test extends WP_UnitTestCase { |
| 6 | + |
| 7 | + /** |
| 8 | + * @var WC_Cart |
| 9 | + */ |
| 10 | + private $cart; |
| 11 | + |
| 12 | + /** |
| 13 | + * The number of times woocommerce_subscriptions_calculated_total was triggered. |
| 14 | + * |
| 15 | + * @var int |
| 16 | + */ |
| 17 | + private $calculated_subscription_totals_count = 0; |
| 18 | + |
| 19 | + /** |
| 20 | + * Set up the test class. |
| 21 | + */ |
| 22 | + public function set_up() { |
| 23 | + parent::set_up(); |
| 24 | + |
| 25 | + $this->cart = WC()->cart; |
| 26 | + |
| 27 | + // Count the number of times woocommerce_subscriptions_calculated_total is triggered. |
| 28 | + add_action( |
| 29 | + 'woocommerce_subscriptions_calculated_total', |
| 30 | + function( $subscription_total ) { |
| 31 | + $this->calculated_subscription_totals_count++; |
| 32 | + return $subscription_total; |
| 33 | + } |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Tear down the test class. |
| 39 | + */ |
| 40 | + public function tear_down() { |
| 41 | + parent::tear_down(); |
| 42 | + |
| 43 | + $this->cart->empty_cart(); |
| 44 | + $this->cart->recurring_carts = []; |
| 45 | + |
| 46 | + $this->calculated_subscription_totals_count = 0; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Test that recurring carts are created when calculating totals. |
| 51 | + */ |
| 52 | + public function test_calculate_subscription_totals() { |
| 53 | + $product = WCS_Helper_Product::create_simple_subscription_product( [ 'price' => 10 ] ); |
| 54 | + |
| 55 | + // First, check that there are no recurring carts. |
| 56 | + $this->assertEmpty( $this->cart->recurring_carts ); |
| 57 | + |
| 58 | + $this->cart->add_to_cart( $product->get_id() ); |
| 59 | + |
| 60 | + // Calculate the totals. This should create a recurring cart. |
| 61 | + $this->cart->calculate_totals(); |
| 62 | + |
| 63 | + // Check that the recurring cart was created. |
| 64 | + $this->assertNotEmpty( $this->cart->recurring_carts ); |
| 65 | + $this->assertCount( 1, $this->cart->recurring_carts ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Test that recurring carts are created when calculating totals. |
| 70 | + */ |
| 71 | + public function test_calculate_subscription_totals_multiple_recurring_carts() { |
| 72 | + $product_1 = WCS_Helper_Product::create_simple_subscription_product( [ 'price' => 10 ] ); |
| 73 | + $product_2 = WCS_Helper_Product::create_simple_subscription_product( |
| 74 | + [ |
| 75 | + 'price' => 20, |
| 76 | + 'subscription_period' => 'year', |
| 77 | + ] |
| 78 | + ); |
| 79 | + |
| 80 | + // First, check that there are no recurring carts. |
| 81 | + $this->assertEmpty( $this->cart->recurring_carts ); |
| 82 | + |
| 83 | + $this->cart->add_to_cart( $product_1->get_id() ); |
| 84 | + $this->cart->add_to_cart( $product_2->get_id() ); |
| 85 | + |
| 86 | + // Calculate the totals. This should create a recurring cart. |
| 87 | + $this->cart->calculate_totals(); |
| 88 | + |
| 89 | + // Check that the recurring cart was created. |
| 90 | + $this->assertNotEmpty( $this->cart->recurring_carts ); |
| 91 | + $this->assertCount( 2, $this->cart->recurring_carts ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Test that recurring carts are created when calculating totals. |
| 96 | + */ |
| 97 | + public function test_calculate_subscription_totals_multiple_items_one_cart() { |
| 98 | + $product_1 = WCS_Helper_Product::create_simple_subscription_product( |
| 99 | + [ |
| 100 | + 'price' => 10, |
| 101 | + 'subscription_period' => 'year', |
| 102 | + ] |
| 103 | + ); |
| 104 | + $product_2 = WCS_Helper_Product::create_simple_subscription_product( |
| 105 | + [ |
| 106 | + 'price' => 20, |
| 107 | + 'subscription_period' => 'year', |
| 108 | + ] |
| 109 | + ); |
| 110 | + |
| 111 | + // First, check that there are no recurring carts. |
| 112 | + $this->assertEmpty( $this->cart->recurring_carts ); |
| 113 | + |
| 114 | + $this->cart->add_to_cart( $product_1->get_id() ); |
| 115 | + $this->cart->add_to_cart( $product_2->get_id() ); |
| 116 | + |
| 117 | + // Calculate the totals. This should create a recurring cart. |
| 118 | + $this->cart->calculate_totals(); |
| 119 | + |
| 120 | + // Check that the recurring cart was created. |
| 121 | + $this->assertNotEmpty( $this->cart->recurring_carts ); |
| 122 | + $this->assertCount( 1, $this->cart->recurring_carts ); // Only one recurring cart should be created for both items. |
| 123 | + $this->assertCount( 2, reset( $this->cart->recurring_carts )->get_cart() ); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Test that recurring carts are created when calculating totals with a mixed cart. |
| 128 | + */ |
| 129 | + public function test_calculate_subscription_totals_with_mixed_cart() { |
| 130 | + $subscription = WCS_Helper_Product::create_simple_subscription_product( [ 'price' => 10 ] ); |
| 131 | + $simple = WC_Helper_Product::create_simple_product(); |
| 132 | + |
| 133 | + // First, check that there are no recurring carts. |
| 134 | + $this->assertEmpty( $this->cart->recurring_carts ); |
| 135 | + |
| 136 | + $this->cart->add_to_cart( $subscription->get_id() ); |
| 137 | + $this->cart->add_to_cart( $simple->get_id() ); |
| 138 | + |
| 139 | + // Calculate the totals. This should create a recurring cart. |
| 140 | + $this->cart->calculate_totals(); |
| 141 | + |
| 142 | + // Check that the recurring cart was created. |
| 143 | + $this->assertNotEmpty( $this->cart->recurring_carts ); |
| 144 | + $this->assertCount( 1, $this->cart->recurring_carts ); |
| 145 | + $this->assertCount( 1, reset( $this->cart->recurring_carts )->get_cart() ); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Test that recurring carts are created only once when calculating totals with nested calls. |
| 150 | + */ |
| 151 | + public function test_calculate_subscription_totals_with_nested_calls() { |
| 152 | + $subscription = WCS_Helper_Product::create_simple_subscription_product( [ 'price' => 10 ] ); |
| 153 | + $simple = WC_Helper_Product::create_simple_product(); |
| 154 | + |
| 155 | + // First, check that there are no recurring carts. |
| 156 | + $this->assertEmpty( $this->cart->recurring_carts ); |
| 157 | + |
| 158 | + $this->cart->add_to_cart( $subscription->get_id() ); |
| 159 | + $this->cart->add_to_cart( $simple->get_id() ); |
| 160 | + |
| 161 | + add_action( 'woocommerce_calculate_totals', [ $this, 'mock_nested_callback' ] ); |
| 162 | + |
| 163 | + $this->calculated_subscription_totals_count = 0; |
| 164 | + |
| 165 | + // Calculate the totals. This should create a recurring cart. |
| 166 | + $this->cart->calculate_totals(); |
| 167 | + |
| 168 | + // Check that the recurring cart was created. |
| 169 | + $this->assertNotEmpty( $this->cart->recurring_carts ); |
| 170 | + $this->assertCount( 1, $this->cart->recurring_carts ); |
| 171 | + $this->assertCount( 1, reset( $this->cart->recurring_carts )->get_cart() ); |
| 172 | + $this->assertEquals( 1, $this->calculated_subscription_totals_count ); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * A function to mock calling WC()->cart->calculate_totals() from within the calculate_totals action. |
| 177 | + * |
| 178 | + * @param WC_Cart $cart |
| 179 | + */ |
| 180 | + public function mock_nested_callback( $cart ) { |
| 181 | + if ( ! isset( $cart->recurring_cart_key ) ) { |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + // Nest the calculate_totals call by calling it again. |
| 186 | + WC()->cart->calculate_totals(); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Test that the calculation type is set when set_recurring_cart_key_before_calculate_totals() is called. |
| 191 | + */ |
| 192 | + public function test_set_recurring_cart_key_before_calculate_totals() { |
| 193 | + $cart = $this->getMockBuilder( 'WC_Cart' ) |
| 194 | + ->disableOriginalConstructor() |
| 195 | + ->setMethods( null ) // This makes the mock retain normal PHP object behavior |
| 196 | + ->getMock(); |
| 197 | + |
| 198 | + /** |
| 199 | + * Recurring cart. |
| 200 | + */ |
| 201 | + $cart->recurring_cart_key = 'test_key'; |
| 202 | + WC_Subscriptions_Cart::set_recurring_cart_key_before_calculate_totals( $cart ); |
| 203 | + |
| 204 | + $this->assertEquals( 'recurring_total', WC_Subscriptions_Cart::get_calculation_type() ); |
| 205 | + |
| 206 | + /** |
| 207 | + * Non-recurring cart. |
| 208 | + */ |
| 209 | + $cart->recurring_cart_key = ''; |
| 210 | + WC_Subscriptions_Cart::set_recurring_cart_key_before_calculate_totals( $cart ); |
| 211 | + |
| 212 | + $this->assertEquals( 'none', WC_Subscriptions_Cart::get_calculation_type() ); |
| 213 | + |
| 214 | + /** |
| 215 | + * Non-recurring cart. |
| 216 | + */ |
| 217 | + unset( $cart->recurring_cart_key ); |
| 218 | + WC_Subscriptions_Cart::set_recurring_cart_key_before_calculate_totals( $cart ); |
| 219 | + |
| 220 | + $this->assertEquals( 'none', WC_Subscriptions_Cart::get_calculation_type() ); |
| 221 | + |
| 222 | + unset( $cart ); |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Test that the calculation type is set when set_recurring_cart_key_before_calculate_totals() and then is called. |
| 227 | + */ |
| 228 | + public function test_update_recurring_cart_key_after_calculate_totals_empty_stack() { |
| 229 | + $cart = $this->getMockBuilder( 'WC_Cart' ) |
| 230 | + ->disableOriginalConstructor() |
| 231 | + ->setMethods( null ) // This makes the mock retain normal PHP object behavior |
| 232 | + ->getMock(); |
| 233 | + |
| 234 | + /** |
| 235 | + * Empty stack. |
| 236 | + */ |
| 237 | + $cart->recurring_cart_key = 'test_key'; |
| 238 | + WC_Subscriptions_Cart::set_recurring_cart_key_before_calculate_totals( $cart ); |
| 239 | + WC_Subscriptions_Cart::update_recurring_cart_key_after_calculate_totals( $cart ); |
| 240 | + |
| 241 | + // Check that after a recurring cart has been been set (started) and then finished, the stack is empty and returns 'none'. |
| 242 | + $this->assertEquals( 'none', WC_Subscriptions_Cart::get_calculation_type() ); |
| 243 | + |
| 244 | + unset( $cart ); |
| 245 | + } |
| 246 | + |
| 247 | + /** |
| 248 | + * Test that the calculation type is set when update_recurring_cart_key_after_calculate_totals() is called and there' a recurring cart in the stack. |
| 249 | + */ |
| 250 | + public function test_update_recurring_cart_key_after_calculate_totals_with_recurring_cart() { |
| 251 | + $cart = $this->getMockBuilder( 'WC_Cart' ) |
| 252 | + ->disableOriginalConstructor() |
| 253 | + ->setMethods( null ) // This makes the mock retain normal PHP object behavior |
| 254 | + ->getMock(); |
| 255 | + |
| 256 | + /** |
| 257 | + * Recurring cart left in stack. |
| 258 | + */ |
| 259 | + $cart->recurring_cart_key = 'test_key'; |
| 260 | + |
| 261 | + // First, populate the stack with a cart with a recurring key. |
| 262 | + WC_Subscriptions_Cart::set_recurring_cart_key_before_calculate_totals( $cart ); |
| 263 | + |
| 264 | + // Now, populate the stack with a cart without a recurring key. |
| 265 | + $cart->recurring_cart_key = ''; |
| 266 | + WC_Subscriptions_Cart::set_recurring_cart_key_before_calculate_totals( $cart ); |
| 267 | + WC_Subscriptions_Cart::update_recurring_cart_key_after_calculate_totals( $cart ); |
| 268 | + |
| 269 | + $this->assertEquals( 'recurring_total', WC_Subscriptions_Cart::get_calculation_type() ); |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * Test that the calculation type is set when update_recurring_cart_key_after_calculate_totals() is called and there' a recurring cart in the stack. |
| 274 | + */ |
| 275 | + public function test_update_recurring_cart_key_after_calculate_totals_with_initial_cart() { |
| 276 | + $cart = $this->getMockBuilder( 'WC_Cart' ) |
| 277 | + ->disableOriginalConstructor() |
| 278 | + ->setMethods( null ) // This makes the mock retain normal PHP object behavior |
| 279 | + ->getMock(); |
| 280 | + |
| 281 | + // First, populate the stack with a cart without a recurring key. |
| 282 | + WC_Subscriptions_Cart::set_recurring_cart_key_before_calculate_totals( $cart ); |
| 283 | + |
| 284 | + // Now, populate the stack with a cart with a recurring key. |
| 285 | + $cart->recurring_cart_key = 'test_recurring_cart_key'; |
| 286 | + WC_Subscriptions_Cart::set_recurring_cart_key_before_calculate_totals( $cart ); |
| 287 | + WC_Subscriptions_Cart::update_recurring_cart_key_after_calculate_totals( $cart ); |
| 288 | + |
| 289 | + $this->assertEquals( 'none', WC_Subscriptions_Cart::get_calculation_type() ); |
| 290 | + } |
| 291 | +} |
0 commit comments