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

115 lines
2.6 KiB
PHP
Raw Normal View History

2015-03-04 08:00:33 +00:00
<?php
/**
* Test WC product functions
*
2015-03-04 17:23:25 +00:00
* @since 2.3
2015-03-04 08:00:33 +00:00
*/
class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
/**
* @var object
* @access private
*/
private $_product = null;
/**
* Helper method to get a product
*
2015-03-04 17:23:25 +00:00
* @since 2.3
* @access private
*/
private function _get_product() {
$this->_product = WC_Helper_Product::create_simple_product();
}
/**
* Helper method to delete a product
*
2015-03-04 17:23:25 +00:00
* @since 2.3
* @access private
*/
private function _delete_product() {
// Delete the previously created product
WC_Helper_Product::delete_product( $this->_product->id );
$this->_product = null;
}
2015-03-04 08:00:33 +00:00
/**
* Test wc_get_product()
*
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() {
$this->_get_product();
$product_copy = wc_get_product( $this->_product->id );
$this->assertEquals( $this->_product->id, $product_copy->id );
2015-03-04 08:00:33 +00:00
$this->_delete_product();
2015-03-04 08:00:33 +00:00
}
/**
* Test wc_update_product_stock()
*
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() {
$this->_get_product();
2015-03-04 08:00:33 +00:00
update_post_meta( $this->_product->id, '_manage_stock', 'yes' );
2015-03-04 08:00:33 +00:00
wc_update_product_stock( $this->_product->id, 5 );
$this->assertEquals( 5, $this->_product->stock );
2015-03-04 08:00:33 +00:00
$this->_delete_product();
2015-03-04 08:00:33 +00:00
}
2015-03-04 15:48:11 +00:00
/**
* Test wc_get_product_types()
*
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() );
}
/**
* Test wc_product_has_unique_sku()
*
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() {
$product_1 = WC_Helper_Product::create_simple_product();
$this->assertEquals( true, wc_product_has_unique_sku( $product_1->id, $product_1->sku ) );
$product_2 = WC_Helper_Product::create_simple_product();
$this->assertEquals( false, wc_product_has_unique_sku( $product_2->id, $product_2->sku ) );
WC_Helper_Product::delete_product( $product_1->id );
$this->assertEquals( true, wc_product_has_unique_sku( $product_2->id, $product_2->sku ) );
WC_Helper_Product::delete_product( $product_2->id );
}
/**
* Test wc_get_product_id_by_sku()
*
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() {
$this->_get_product();
$this->assertEquals( $this->_product->id, wc_get_product_id_by_sku( $this->_product->sku ) );
$this->_delete_product();
}
2015-03-04 08:00:33 +00:00
}