/** * Test the is_customer_outside_base method */ public function test_is_customer_outside_base() { // Get the original settings for the session and the WooCommerce options $original_chosen_shipping_methods = \WC_Helper_Customer::get_chosen_shipping_methods(); $original_tax_based_on = \WC_Helper_Customer::get_tax_based_on(); $original_customer_details = \WC_Helper_Customer::get_customer_details(); $customer = \WC_Helper_Customer::create_mock_customer(); // Create dummy product, and add the product to the cart. $product = \WC_Helper_Product::create_simple_product(); WC()->cart->add_to_cart($product->id, 1); // Customer is going with the Local Pickup option, and the store calculates tax based on the store location. \WC_Helper_Customer::set_chosen_shipping_methods(array('local_pickup')); \WC_Helper_Customer::set_tax_based_on('base'); $this->assertEquals($customer->is_customer_outside_base(), false); // Customer is going with the Local Pickup option, and the store calculates tax based on the customer's billing address. \WC_Helper_Customer::set_chosen_shipping_methods(array('local_pickup')); \WC_Helper_Customer::set_tax_based_on('billing'); $this->assertEquals($customer->is_customer_outside_base(), false); // Customer is going with the Free Shipping option, and the store calculates tax based on the customer's billing address. \WC_Helper_Customer::set_chosen_shipping_methods(array('free_shipping')); \WC_Helper_Customer::set_tax_based_on('billing'); $this->assertEquals($customer->is_customer_outside_base(), true); // Customer is going with the Free Shipping option, and the store calculates tax based on the store base location. \WC_Helper_Customer::set_chosen_shipping_methods(array('free_shipping')); \WC_Helper_Customer::set_tax_based_on('base'); $this->assertEquals($customer->is_customer_outside_base(), false); //Now reset the settings back to the way they were before this test \WC_Helper_Customer::set_chosen_shipping_methods($original_chosen_shipping_methods); \WC_Helper_Customer::set_tax_based_on($original_tax_based_on); \WC_Helper_Customer::set_customer_details($original_customer_details); // Clean up the cart WC()->cart->empty_cart(); // Clean up product \WC_Helper_Product::delete_product($product->id); }
/** * Create a mock customer for testing purposes. * * @return WC_Customer */ public static function create_mock_customer() { $customer_data = array('country' => 'US', 'state' => 'PA', 'postcode' => '19123', 'city' => 'Philadelphia', 'address' => '123 South Street', 'address_2' => 'Apt 1', 'shipping_country' => 'US', 'shipping_state' => 'PA', 'shipping_postcode' => '19123', 'shipping_city' => 'Philadelphia', 'shipping_address' => '123 South Street', 'shipping_address_2' => 'Apt 1', 'is_vat_exempt' => false, 'calculated_shipping' => false); WC_Helper_Customer::set_customer_details($customer_data); $customer = new WC_Customer(0, true); return $customer; }
/** * Test setting meta. * @since 2.7.0 */ public function test_set_meta() { $customer = WC_Helper_Customer::create_customer(); $customer_id = $customer->get_id(); $meta_value = time() . '-custom-value'; $customer->add_meta_data('my-field', $meta_value, true); $customer->save(); $customer->read($customer_id); $this->assertEquals($meta_value, $customer->get_meta('my-field')); }
/** * Test customer batch endpoint. * * @since 2.7.0 */ public function test_batch_customer() { wp_set_current_user(1); $customer_1 = WC_Helper_Customer::create_customer('test_batch_customer', 'test123', '*****@*****.**'); $customer_2 = WC_Helper_Customer::create_customer('test_batch_customer2', 'test123', '*****@*****.**'); $customer_3 = WC_Helper_Customer::create_customer('test_batch_customer3', 'test123', '*****@*****.**'); $customer_4 = WC_Helper_Customer::create_customer('test_batch_customer4', 'test123', '*****@*****.**'); $request = new WP_REST_Request('POST', '/wc/v1/customers/batch'); $request->set_body_params(array('update' => array(array('id' => $customer_1->get_id(), 'last_name' => 'McTest')), 'delete' => array($customer_2->get_id(), $customer_3->get_id()), 'create' => array(array('username' => 'newuser', 'password' => 'test123', 'email' => '*****@*****.**')))); $response = $this->server->dispatch($request); $data = $response->get_data(); $this->assertEquals('McTest', $data['update'][0]['last_name']); $this->assertEquals('newuser', $data['create'][0]['username']); $this->assertEmpty($data['create'][0]['last_name']); $this->assertEquals($customer_2->get_id(), $data['delete'][0]['id']); $this->assertEquals($customer_3->get_id(), $data['delete'][1]['id']); $request = new WP_REST_Request('GET', '/wc/v1/customers'); $response = $this->server->dispatch($request); $data = $response->get_data(); $this->assertEquals(3, count($data)); }