woocommerce/tests/unit-tests/product/functions.php

189 lines
5.4 KiB
PHP
Raw Normal View History

2015-03-04 08:00:33 +00:00
<?php
2015-03-06 15:32:40 +00:00
namespace WooCommerce\Tests\Product;
2015-03-04 08:00:33 +00:00
/**
2015-11-03 13:31:20 +00:00
* Class Functions.
2015-03-06 15:32:40 +00:00
* @package WooCommerce\Tests\Product
2015-03-04 17:23:25 +00:00
* @since 2.3
2015-03-04 08:00:33 +00:00
*/
2015-03-06 15:32:40 +00:00
class Functions extends \WC_Unit_Test_Case {
2015-03-04 08:00:33 +00:00
/**
2015-11-03 13:31:20 +00:00
* Test wc_get_product().
2015-03-04 08:00:33 +00:00
*
2015-03-04 17:23:25 +00:00
* @since 2.3
2015-03-04 08:00:33 +00:00
*/
public function test_wc_get_product() {
// Create product
$product = \WC_Helper_Product::create_simple_product();
$product_copy = wc_get_product( $product->id );
$this->assertEquals( $product->id, $product_copy->id );
2015-03-04 08:00:33 +00:00
// Delete Product
\WC_Helper_Product::delete_product( $product->id );
2015-03-04 08:00:33 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Test wc_update_product_stock().
2015-03-04 08:00:33 +00:00
*
2015-03-04 17:23:25 +00:00
* @since 2.3
2015-03-04 08:00:33 +00:00
*/
public function test_wc_update_product_stock() {
2015-04-09 14:43:18 +00:00
// Create product
$product = \WC_Helper_Product::create_simple_product();
2015-03-04 08:00:33 +00:00
update_post_meta( $product->id, '_manage_stock', 'yes' );
2015-03-04 08:00:33 +00:00
wc_update_product_stock( $product->id, 5 );
$this->assertEquals( 5, $product->stock );
2015-03-04 08:00:33 +00:00
// Delete Product
\WC_Helper_Product::delete_product( $product->id );
2015-03-04 08:00:33 +00:00
}
2015-03-04 15:48:11 +00:00
2015-04-09 15:00:07 +00:00
/**
2015-11-03 13:31:20 +00:00
* Test wc_delete_product_transients().
2015-04-09 15:00:07 +00:00
*
* @since 2.4
*/
public function test_wc_delete_product_transients() {
// Create product
$product = \WC_Helper_Product::create_simple_product();
update_post_meta( $product->id, '_regular_price', wc_format_decimal( 10 ) );
update_post_meta( $product->id, '_price', wc_format_decimal( 5 ) );
update_post_meta( $product->id, '_sale_price', wc_format_decimal( 5 ) );
update_post_meta( $product->id, '_featured', 'yes' );
wc_get_product_ids_on_sale(); // Creates the transient for on sale products
wc_get_featured_product_ids(); // Creates the transient for featured products
wc_delete_product_transients();
$this->assertFalse( get_transient( 'wc_products_onsale' ) );
$this->assertFalse( get_transient( 'wc_featured_products' ) );
\WC_Helper_Product::delete_product( $product->id );
}
2015-04-09 14:43:18 +00:00
/**
2015-11-03 13:31:20 +00:00
* Test wc_get_product_ids_on_sale().
2015-04-09 14:43:18 +00:00
*
* @since 2.4
*/
public function test_wc_get_product_ids_on_sale() {
2015-04-09 14:56:45 +00:00
$this->assertEquals( array(), wc_get_product_ids_on_sale() );
delete_transient( 'wc_products_onsale' );
2015-04-09 14:43:18 +00:00
// Create product
$product = \WC_Helper_Product::create_simple_product();
update_post_meta( $product->id, '_regular_price', wc_format_decimal( 10 ) );
update_post_meta( $product->id, '_price', wc_format_decimal( 5 ) );
update_post_meta( $product->id, '_sale_price', wc_format_decimal( 5 ) );
$this->assertEquals( array( $product->id ), wc_get_product_ids_on_sale() );
// Delete Product
\WC_Helper_Product::delete_product( $product->id );
}
2015-04-09 14:50:28 +00:00
/**
2015-11-03 13:31:20 +00:00
* Test wc_get_featured_product_ids().
2015-04-09 14:50:28 +00:00
*
* @since 2.4
*/
public function test_wc_get_featured_product_ids() {
2015-04-09 14:56:45 +00:00
$this->assertEquals( array(), wc_get_featured_product_ids() );
delete_transient( 'wc_featured_products' );
2015-04-09 14:50:28 +00:00
// Create product
$product = \WC_Helper_Product::create_simple_product();
update_post_meta( $product->id, '_featured', 'yes' );
$this->assertEquals( array( $product->id ), wc_get_featured_product_ids() );
// Delete Product
\WC_Helper_Product::delete_product( $product->id );
}
2015-04-09 15:10:00 +00:00
/**
2015-11-03 13:31:20 +00:00
* Test wc_placeholder_img().
2015-04-09 15:10:00 +00:00
*
* @since 2.4
*/
public function test_wc_placeholder_img() {
$sizes = array(
'shop_thumbnail' => array( 'width' => '180', 'height' => '180' ),
'shop_single' => array( 'width' => '600', 'height' => '600' ),
'shop_catalog' => array( 'width' => '300', 'height' => '300' )
);
foreach ( $sizes as $size => $values ) {
2015-08-05 19:17:52 +00:00
$img = '<img src="' . wc_placeholder_img_src() . '" alt="' . esc_attr__( 'Placeholder', 'woocommerce' ) . '" width="' . $values['width'] . '" class="woocommerce-placeholder wp-post-image" height="' . $values['height'] . '" />';
$this->assertEquals( apply_filters( 'woocommerce_placeholder_img', $img ), wc_placeholder_img( $size ) );
2015-04-09 15:10:00 +00:00
}
2015-08-05 19:17:52 +00:00
$img = '<img src="' . wc_placeholder_img_src() . '" alt="' . esc_attr__( 'Placeholder', 'woocommerce' ) . '" width="180" class="woocommerce-placeholder wp-post-image" height="180" />';
$this->assertEquals( apply_filters( 'woocommerce_placeholder_img', $img ), wc_placeholder_img() );
2015-04-09 15:10:00 +00:00
}
2015-03-04 15:48:11 +00:00
/**
2015-11-03 13:31:20 +00:00
* Test wc_get_product_types().
2015-03-04 15:48:11 +00:00
*
2015-03-04 17:23:25 +00:00
* @since 2.3
2015-03-04 15:48:11 +00:00
*/
public function test_wc_get_product_types() {
$product_types = (array) apply_filters( 'product_type_selector', array(
'simple' => __( 'Simple product', 'woocommerce' ),
'grouped' => __( 'Grouped product', 'woocommerce' ),
'external' => __( 'External/Affiliate product', 'woocommerce' ),
'variable' => __( 'Variable product', 'woocommerce' )
) );
$this->assertEquals( $product_types, wc_get_product_types() );
}
/**
2015-11-03 13:31:20 +00:00
* Test wc_product_has_unique_sku().
2015-03-04 15:48:11 +00:00
*
2015-03-04 17:23:25 +00:00
* @since 2.3
2015-03-04 15:48:11 +00:00
*/
public function test_wc_product_has_unique_sku() {
2015-03-06 15:32:40 +00:00
$product_1 = \WC_Helper_Product::create_simple_product();
2015-03-04 15:48:11 +00:00
$this->assertEquals( true, wc_product_has_unique_sku( $product_1->id, $product_1->sku ) );
2015-03-06 15:32:40 +00:00
$product_2 = \WC_Helper_Product::create_simple_product();
2015-03-04 15:48:11 +00:00
$this->assertEquals( false, wc_product_has_unique_sku( $product_2->id, $product_2->sku ) );
2015-03-06 15:32:40 +00:00
\WC_Helper_Product::delete_product( $product_1->id );
2015-03-04 15:48:11 +00:00
$this->assertEquals( true, wc_product_has_unique_sku( $product_2->id, $product_2->sku ) );
2015-03-06 15:32:40 +00:00
\WC_Helper_Product::delete_product( $product_2->id );
2015-03-04 15:48:11 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Test wc_get_product_id_by_sku().
2015-03-04 15:48:11 +00:00
*
2015-03-04 17:23:25 +00:00
* @since 2.3
2015-03-04 15:48:11 +00:00
*/
public function test_wc_get_product_id_by_sku() {
2015-04-09 14:43:18 +00:00
// Create product
$product = \WC_Helper_Product::create_simple_product();
2015-03-04 15:48:11 +00:00
$this->assertEquals( $product->id, wc_get_product_id_by_sku( $product->sku ) );
2015-03-04 15:48:11 +00:00
// Delete Product
\WC_Helper_Product::delete_product( $product->id );
2015-03-04 15:48:11 +00:00
}
2015-03-04 08:00:33 +00:00
}