This helper class should ONLY be used for unit tests!.
 /**
  * Create a order.
  *
  * @since 2.4
  *
  * @return WC_Order Order object.
  */
 public static function create_order()
 {
     // Create product
     $product = WC_Helper_Product::create_simple_product();
     WC_Helper_Shipping::create_simple_flat_rate();
     $order_data = array('status' => 'pending', 'customer_id' => 1, 'customer_note' => '', 'total' => '');
     $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
     // Required, else wc_create_order throws an exception
     $order = wc_create_order($order_data);
     // Add order products
     $item_id = $order->add_product($product, 4);
     // Set billing address
     $billing_address = array('country' => 'US', 'first_name' => 'Jeroen', 'last_name' => 'Sormani', 'company' => 'WooCompany', 'address_1' => 'WooAddress', 'address_2' => '', 'postcode' => '123456', 'city' => 'WooCity', 'state' => 'NY', 'email' => '*****@*****.**', 'phone' => '555-32123');
     $order->set_address($billing_address, 'billing');
     // Add shipping costs
     $shipping_taxes = WC_Tax::calc_shipping_tax('10', WC_Tax::get_shipping_tax_rates());
     $order->add_shipping(new WC_Shipping_Rate('flat_rate_shipping', 'Flat rate shipping', '10', $shipping_taxes, 'flat_rate'));
     // Set payment gateway
     $payment_gateways = WC()->payment_gateways->payment_gateways();
     $order->set_payment_method($payment_gateways['bacs']);
     // Set totals
     $order->set_total(10, 'shipping');
     $order->set_total(0, 'cart_discount');
     $order->set_total(0, 'cart_discount_tax');
     $order->set_total(0, 'tax');
     $order->set_total(0, 'shipping_tax');
     $order->set_total(40, 'total');
     // 4 x $10 simple helper product
     return wc_get_order($order->id);
 }
 /**
  * Create a order.
  *
  * @since 2.4
  *
  * @return WC_Order Order object.
  */
 public static function create_order($customer_id = 1)
 {
     // Create product
     $product = WC_Helper_Product::create_simple_product();
     WC_Helper_Shipping::create_simple_flat_rate();
     $order_data = array('status' => 'pending', 'customer_id' => $customer_id, 'customer_note' => '', 'total' => '');
     $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
     // Required, else wc_create_order throws an exception
     $order = wc_create_order($order_data);
     // Add order products
     $order->add_product($product, 4);
     // Set billing address
     $order->set_billing_first_name('Jeroen');
     $order->set_billing_last_name('Sormani');
     $order->set_billing_company('WooCompany');
     $order->set_billing_address_1('WooAddress');
     $order->set_billing_address_2('');
     $order->set_billing_city('WooCity');
     $order->set_billing_state('NY');
     $order->set_billing_postcode('123456');
     $order->set_billing_country('US');
     $order->set_billing_email('*****@*****.**');
     $order->set_billing_phone('555-32123');
     // Add shipping costs
     $shipping_taxes = WC_Tax::calc_shipping_tax('10', WC_Tax::get_shipping_tax_rates());
     $rate = new WC_Shipping_Rate('flat_rate_shipping', 'Flat rate shipping', '10', $shipping_taxes, 'flat_rate');
     $item = new WC_Order_Item_Shipping();
     $item->set_props(array('method_title' => $rate->label, 'method_id' => $rate->id, 'total' => wc_format_decimal($rate->cost), 'taxes' => $rate->taxes, 'meta_data' => $rate->get_meta_data()));
     $order->add_item($item);
     // Set payment gateway
     $payment_gateways = WC()->payment_gateways->payment_gateways();
     $order->set_payment_method($payment_gateways['bacs']);
     // Set totals
     $order->set_shipping_total(10);
     $order->set_discount_total(0);
     $order->set_discount_tax(0);
     $order->set_cart_tax(0);
     $order->set_shipping_tax(0);
     $order->set_total(40);
     // 4 x $10 simple helper product
     $order->save();
     return $order;
 }
Example #3
0
 /**
  * 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->get_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->get_id());
 }
Example #4
0
 /**
  * Test wc_cart_totals_subtotal_html()
  *
  * @todo  test with taxes incl./excl.
  * @since 2.4
  */
 public function test_wc_cart_totals_subtotal_html()
 {
     $product = \WC_Helper_Product::create_simple_product();
     WC()->cart->add_to_cart($product->id, 1);
     $this->expectOutputString(wc_price($product->price), wc_cart_totals_subtotal_html());
     \WC_Helper_Product::delete_product($product->id);
 }
Example #5
0
 /**
  * Test test_wc_api_order_get_variation_id_returns_correct_id.
  *
  * @since 2.4
  */
 public function test_wc_api_order_get_variation_id_returns_correct_id()
 {
     parent::setUp();
     $product = WC_Helper_Product::create_variation_product();
     $orders_api = WC()->api->WC_API_Orders;
     $variation_id = $orders_api->get_variation_id($product, array('size' => 'small'));
     $this->assertSame($product->id + 1, $variation_id);
     $variation_id = $orders_api->get_variation_id($product, array('size' => 'large'));
     $this->assertSame($product->id + 2, $variation_id);
 }
Example #6
0
 /**
  * Test wc_empty_cart()
  *
  * @since 2.3.0
  */
 public function test_wc_empty_cart()
 {
     // Create dummy product
     $product = \WC_Helper_Product::create_simple_product();
     // Add the product to the cart
     WC()->cart->add_to_cart($product->id, 1);
     // Empty the cart
     wc_empty_cart();
     // Check if the cart is empty
     $this->assertEquals(0, WC()->cart->get_cart_contents_count());
     // Delete the previously created product
     \WC_Helper_Product::delete_product($product->id);
 }
Example #7
0
 /**
  * Test wc_add_to_cart_message
  */
 public function test_wc_add_to_cart_message()
 {
     $product = WC_Helper_Product::create_simple_product();
     $message = wc_add_to_cart_message(array($product->id => 1), false, true);
     $this->assertEquals('<a href="http://example.org" class="button wc-forward">View Cart</a> &ldquo;Dummy Product&rdquo; has been added to your cart.', $message);
     $message = wc_add_to_cart_message(array($product->id => 3), false, true);
     $this->assertEquals('<a href="http://example.org" class="button wc-forward">View Cart</a> &ldquo;Dummy Product&rdquo; has been added to your cart.', $message);
     $message = wc_add_to_cart_message(array($product->id => 1), true, true);
     $this->assertEquals('<a href="http://example.org" class="button wc-forward">View Cart</a> &ldquo;Dummy Product&rdquo; has been added to your cart.', $message);
     $message = wc_add_to_cart_message(array($product->id => 3), true, true);
     $this->assertEquals('<a href="http://example.org" class="button wc-forward">View Cart</a> 3 &times; &ldquo;Dummy Product&rdquo; have been added to your cart.', $message);
     $message = wc_add_to_cart_message($product->id, false, true);
     $this->assertEquals('<a href="http://example.org" class="button wc-forward">View Cart</a> &ldquo;Dummy Product&rdquo; has been added to your cart.', $message);
     $message = wc_add_to_cart_message($product->id, true, true);
     $this->assertEquals('<a href="http://example.org" class="button wc-forward">View Cart</a> &ldquo;Dummy Product&rdquo; has been added to your cart.', $message);
 }
Example #8
0
 /**
  * Test: has_free_item
  */
 function test_has_free_item()
 {
     $object = new WC_Order();
     $object->add_product(WC_Helper_Product::create_simple_product(), 4);
     $this->assertFalse($object->has_free_item());
     $free_product = WC_Helper_Product::create_simple_product();
     $free_product->set_price(0);
     $object->add_product($free_product, 4);
     $this->assertTrue($object->has_free_item());
 }
 /**
  * Test the product review schema.
  *
  * @since 2.7.0
  */
 public function test_product_review_schema()
 {
     wp_set_current_user($this->user);
     $product = WC_Helper_Product::create_simple_product();
     $request = new WP_REST_Request('OPTIONS', '/wc/v1/products/' . $product->get_id() . '/reviews');
     $response = $this->server->dispatch($request);
     $data = $response->get_data();
     $properties = $data['schema']['properties'];
     $this->assertEquals(7, count($properties));
     $this->assertArrayHasKey('id', $properties);
     $this->assertArrayHasKey('review', $properties);
     $this->assertArrayHasKey('date_created', $properties);
     $this->assertArrayHasKey('rating', $properties);
     $this->assertArrayHasKey('name', $properties);
     $this->assertArrayHasKey('email', $properties);
     $this->assertArrayHasKey('verified', $properties);
 }
 /**
  * Test variation schema.
  *
  * @since 2.7.0
  */
 public function test_variation_schema()
 {
     wp_set_current_user($this->user);
     $product = WC_Helper_Product::create_simple_product();
     $request = new WP_REST_Request('OPTIONS', '/wc/v1/products/' . $product->get_id() . '/variations');
     $response = $this->server->dispatch($request);
     $data = $response->get_data();
     $properties = $data['schema']['properties'];
     $this->assertEquals(33, count($properties));
     $this->assertArrayHasKey('id', $properties);
     $this->assertArrayHasKey('date_created', $properties);
     $this->assertArrayHasKey('date_modified', $properties);
     $this->assertArrayHasKey('description', $properties);
     $this->assertArrayHasKey('permalink', $properties);
     $this->assertArrayHasKey('sku', $properties);
     $this->assertArrayHasKey('price', $properties);
     $this->assertArrayHasKey('regular_price', $properties);
     $this->assertArrayHasKey('sale_price', $properties);
     $this->assertArrayHasKey('date_on_sale_from', $properties);
     $this->assertArrayHasKey('date_on_sale_to', $properties);
     $this->assertArrayHasKey('on_sale', $properties);
     $this->assertArrayHasKey('visible', $properties);
     $this->assertArrayHasKey('purchasable', $properties);
     $this->assertArrayHasKey('virtual', $properties);
     $this->assertArrayHasKey('downloadable', $properties);
     $this->assertArrayHasKey('downloads', $properties);
     $this->assertArrayHasKey('download_limit', $properties);
     $this->assertArrayHasKey('download_expiry', $properties);
     $this->assertArrayHasKey('tax_status', $properties);
     $this->assertArrayHasKey('tax_class', $properties);
     $this->assertArrayHasKey('manage_stock', $properties);
     $this->assertArrayHasKey('stock_quantity', $properties);
     $this->assertArrayHasKey('in_stock', $properties);
     $this->assertArrayHasKey('backorders', $properties);
     $this->assertArrayHasKey('backorders_allowed', $properties);
     $this->assertArrayHasKey('backordered', $properties);
     $this->assertArrayHasKey('weight', $properties);
     $this->assertArrayHasKey('dimensions', $properties);
     $this->assertArrayHasKey('shipping_class', $properties);
     $this->assertArrayHasKey('shipping_class_id', $properties);
     $this->assertArrayHasKey('image', $properties);
     $this->assertArrayHasKey('attributes', $properties);
     $product->delete(true);
 }
Example #11
0
 /**
  * Tests to make sure you can filter products post statuses by both
  * the status query arg and WP_Query.
  *
  * @since 2.7.0
  */
 public function test_products_filter_post_status()
 {
     wp_set_current_user($this->user);
     for ($i = 0; $i < 8; $i++) {
         $product = WC_Helper_Product::create_simple_product();
         if (0 === $i % 2) {
             wp_update_post(array('ID' => $product->get_id(), 'post_status' => 'draft'));
         }
     }
     // Test filtering with filter[post_status]=publish
     $request = new WP_REST_Request('GET', '/wc/v1/products');
     $request->set_param('filter', array('post_status' => 'publish'));
     $response = $this->server->dispatch($request);
     $products = $response->get_data();
     $this->assertEquals(4, count($products));
     foreach ($products as $product) {
         $this->assertEquals('publish', $product['status']);
     }
     // Test filtering with filter[post_status]=draft
     $request = new WP_REST_Request('GET', '/wc/v1/products');
     $request->set_param('filter', array('post_status' => 'draft'));
     $response = $this->server->dispatch($request);
     $products = $response->get_data();
     $this->assertEquals(4, count($products));
     foreach ($products as $product) {
         $this->assertEquals('draft', $product['status']);
     }
     // Test filtering with status=publish
     $request = new WP_REST_Request('GET', '/wc/v1/products');
     $request->set_param('status', 'publish');
     $response = $this->server->dispatch($request);
     $products = $response->get_data();
     $this->assertEquals(4, count($products));
     foreach ($products as $product) {
         $this->assertEquals('publish', $product['status']);
     }
     // Test filtering with status=draft
     $request = new WP_REST_Request('GET', '/wc/v1/products');
     $request->set_param('status', 'draft');
     $response = $this->server->dispatch($request);
     $products = $response->get_data();
     $this->assertEquals(4, count($products));
     foreach ($products as $product) {
         $this->assertEquals('draft', $product['status']);
     }
     // Test filtering with status=draft and filter[post_status]=publish
     // filter[post_status]=publish should win
     $request = new WP_REST_Request('GET', '/wc/v1/products');
     $request->set_param('status', 'draft');
     $request->set_param('filter', array('post_status' => 'publish'));
     $response = $this->server->dispatch($request);
     $products = $response->get_data();
     $this->assertEquals(4, count($products));
     foreach ($products as $product) {
         $this->assertEquals('publish', $product['status']);
     }
     // Test filtering with no filters - which should return 'any' (all 8)
     $request = new WP_REST_Request('GET', '/wc/v1/products');
     $response = $this->server->dispatch($request);
     $products = $response->get_data();
     $this->assertEquals(8, count($products));
 }
Example #12
0
 /**
  * Test cart fee
  *
  * @since 2.3
  */
 public function test_cart_fee()
 {
     // Create product
     $product = \WC_Helper_Product::create_simple_product();
     update_post_meta($product->id, '_price', '10');
     update_post_meta($product->id, '_regular_price', '10');
     // We need this to have the calculate_totals() method calculate totals
     if (!defined('WOOCOMMERCE_CHECKOUT')) {
         define('WOOCOMMERCE_CHECKOUT', true);
     }
     // Add fee
     \WC_Helper_Fee::add_cart_fee();
     // Add product to cart
     WC()->cart->add_to_cart($product->id, 1);
     // Test if the cart total amount is equal 20
     $this->assertEquals(20, WC()->cart->total);
     // Clearing WC notices
     wc_clear_notices();
     // Clean up the cart
     WC()->cart->empty_cart();
     // Remove fee
     \WC_Helper_Fee::remove_cart_fee();
     // Delete product
     \WC_Helper_Product::delete_product($product->id);
 }
Example #13
0
 /**
  * Tests creating an order without required fields.
  * @since 2.7.0
  */
 public function test_create_order_invalid_fields()
 {
     wp_set_current_user($this->user);
     $product = WC_Helper_Product::create_simple_product();
     // non-existant customer
     $request = new WP_REST_Request('POST', '/wc/v1/orders');
     $request->set_body_params(array('payment_method' => 'bacs', 'payment_method_title' => 'Direct Bank Transfer', 'set_paid' => true, 'customer_id' => 99999, 'billing' => array('first_name' => 'John', 'last_name' => 'Doe', 'address_1' => '969 Market', 'address_2' => '', 'city' => 'San Francisco', 'state' => 'CA', 'postcode' => '94103', 'country' => 'US', 'email' => '*****@*****.**', 'phone' => '(555) 555-5555'), 'shipping' => array('first_name' => 'John', 'last_name' => 'Doe', 'address_1' => '969 Market', 'address_2' => '', 'city' => 'San Francisco', 'state' => 'CA', 'postcode' => '94103', 'country' => 'US'), 'line_items' => array(array('product_id' => $product->get_id(), 'quantity' => 2)), 'shipping_lines' => array(array('method_id' => 'flat_rate', 'method_title' => 'Flat Rate', 'total' => 10))));
     $response = $this->server->dispatch($request);
     $data = $response->get_data();
     $this->assertEquals(400, $response->get_status());
     wp_delete_post($product->get_id(), true);
 }
Example #14
0
 /**
  * Test backorders_require_notification().
  *
  * @since 2.3
  */
 public function test_backorders_require_notification()
 {
     // Create product
     $product = WC_Helper_Product::create_simple_product();
     $product->backorders = 'notify';
     $product->manage_stock = 'yes';
     $this->assertTrue($product->backorders_require_notification());
     $product->backorders = 'yes';
     $this->assertFalse($product->backorders_require_notification());
     $product->backorders = 'no';
     $this->assertFalse($product->backorders_require_notification());
     $product->backorders = 'yes';
     $product->manage_stock = 'no';
     $this->assertFalse($product->backorders_require_notification());
     // Delete product
     WC_Helper_Product::delete_product($product->id);
 }
Example #15
0
 /**
  * Test percent product discount method.
  *
  * @since 2.3
  */
 public function test_percent_product_discount()
 {
     // Create product
     $product = WC_Helper_Product::create_simple_product();
     update_post_meta($product->id, '_price', '10');
     update_post_meta($product->id, '_regular_price', '10');
     // Create coupon
     $coupon = WC_Helper_Coupon::create_coupon();
     update_post_meta($coupon->get_id(), 'discount_type', 'percent_product');
     update_post_meta($coupon->get_id(), 'coupon_amount', '5');
     // Create a flat rate method
     WC_Helper_Shipping::create_simple_flat_rate();
     // We need this to have the calculate_totals() method calculate totals
     if (!defined('WOOCOMMERCE_CHECKOUT')) {
         define('WOOCOMMERCE_CHECKOUT', true);
     }
     // Add fee
     WC_Helper_Fee::add_cart_fee();
     // Add product to cart
     WC()->cart->add_to_cart($product->id, 1);
     // Add coupon
     WC()->cart->add_discount($coupon->get_code());
     // Set the flat_rate shipping method
     WC()->session->set('chosen_shipping_methods', array('flat_rate'));
     WC()->cart->calculate_totals();
     // Test if the cart total amount is equal 29.5
     $this->assertEquals(29.5, WC()->cart->total);
     // Clearing WC notices
     wc_clear_notices();
     // Clean up the cart
     WC()->cart->empty_cart();
     // Remove coupons
     WC()->cart->remove_coupons();
     // Remove fee
     WC_Helper_Fee::remove_cart_fee();
     // Delete the flat rate method
     WC()->session->set('chosen_shipping_methods', array());
     WC_Helper_Shipping::delete_simple_flat_rate();
     // Delete coupon
     WC_Helper_Coupon::delete_coupon($coupon->get_id());
     // Delete product
     WC_Helper_Product::delete_product($product->id);
 }
Example #16
0
 /**
  * Tests getting a product using the factory.
  *
  * @since 2.7.0
  */
 function test_get_product()
 {
     $test_product = WC_Helper_Product::create_simple_product();
     $get_product = WC()->product_factory->get_product($test_product->get_id());
     $this->assertEquals($test_product->get_data(), $get_product->get_data());
 }
Example #17
0
 /**
  * Test product schema.
  *
  * @since 2.7.0
  */
 public function test_product_schema()
 {
     wp_set_current_user($this->user);
     $product = WC_Helper_Product::create_simple_product();
     $request = new WP_REST_Request('OPTIONS', '/wc/v1/products/' . $product->get_id());
     $response = $this->server->dispatch($request);
     $data = $response->get_data();
     $properties = $data['schema']['properties'];
     $this->assertEquals(61, count($properties));
     $product->delete(true);
 }
Example #18
0
 /**
  * Test reading a variable product.
  *
  * @since 2.7.0
  */
 public function test_variable_read()
 {
     $product = WC_Helper_Product::create_variation_product();
     $children = $product->get_children();
     // Test sale prices too
     update_post_meta($children[0], '_price', '8');
     update_post_meta($children[0], '_sale_price', '8');
     delete_transient('wc_var_prices_' . $product->get_id());
     $product = new WC_Product_Variable($product->get_id());
     $this->assertEquals(2, count($product->get_children()));
     $expected_prices['price'][$children[0]] = 8.0;
     $expected_prices['price'][$children[1]] = 15.0;
     $expected_prices['regular_price'][$children[0]] = 10.0;
     $expected_prices['regular_price'][$children[1]] = 15.0;
     $expected_prices['sale_price'][$children[0]] = 8.0;
     $expected_prices['sale_price'][$children[1]] = 15.0;
     $this->assertEquals($expected_prices, $product->get_variation_prices());
     $expected_attributes = array('pa_size' => array('small', 'large'));
     $this->assertEquals($expected_attributes, $product->get_variation_attributes());
     $product->delete();
 }
Example #19
-1
 /**
  * Test wc_get_product_id_by_sku()
  *
  * @since 2.3
  */
 public function test_wc_get_product_id_by_sku()
 {
     // Create product
     $product = \WC_Helper_Product::create_simple_product();
     $this->assertEquals($product->id, wc_get_product_id_by_sku($product->sku));
     // Delete Product
     \WC_Helper_Product::delete_product($product->id);
 }