2017-10-10 19:13:37 +00:00
< ? php
2017-07-07 21:50:27 +00:00
/**
* Class Functions .
* @ package WooCommerce\Tests\Attributes
* @ since 3.2 . 0
*/
class WC_Tests_Attributes_Functions extends WC_Unit_Test_Case {
/**
* Tests wc_get_attribute () .
*
* @ since 3.2 . 0
*/
public function test_wc_get_attribute () {
$args = array (
'name' => 'Brand' ,
'type' => 'select' ,
'order_by' => 'name' ,
'has_archives' => true ,
);
$id = wc_create_attribute ( $args );
$attribute = ( array ) wc_get_attribute ( $id );
$expected = array (
'id' => $id ,
'name' => 'Brand' ,
'slug' => 'pa_brand' ,
'type' => 'select' ,
'order_by' => 'name' ,
'has_archives' => true ,
);
wc_delete_attribute ( $id );
$this -> assertEquals ( $expected , $attribute );
}
/**
* Tests wc_create_attribute () .
*
* @ since 3.2 . 0
*/
public function test_wc_create_attribute () {
// Test success.
$id = wc_create_attribute ( array ( 'name' => 'Brand' ) );
$this -> assertInternalType ( 'int' , $id );
// Test failures.
$err = wc_create_attribute ( array () );
$this -> assertEquals ( 'missing_attribute_name' , $err -> get_error_code () );
$err = wc_create_attribute ( array ( 'name' => 'This is a big name for a product attribute!' ) );
$this -> assertEquals ( 'invalid_product_attribute_slug_too_long' , $err -> get_error_code () );
$err = wc_create_attribute ( array ( 'name' => 'Cat' ) );
$this -> assertEquals ( 'invalid_product_attribute_slug_reserved_name' , $err -> get_error_code () );
register_taxonomy ( 'pa_brand' , array ( 'product' ), array ( 'labels' => array ( 'name' => 'Brand' ) ) );
$err = wc_create_attribute ( array ( 'name' => 'Brand' ) );
$this -> assertEquals ( 'invalid_product_attribute_slug_already_exists' , $err -> get_error_code () );
unregister_taxonomy ( 'pa_brand' );
wc_delete_attribute ( $id );
}
/**
2018-09-13 17:48:05 +00:00
* Test that updating a global attribute will not modify local attribute data .
*
* @ since 3.4 . 6
*/
public function test_wc_create_attribute_serialized_data () {
global $wpdb ;
$global_attribute_data = WC_Helper_Product :: create_attribute ( 'test' , array ( 'Chicken' , 'Nuggets' ) );
$local_attribute = new WC_Product_Attribute ();
$local_attribute -> set_id ( 0 );
$local_attribute -> set_name ( 'Test Local Attribute' );
$local_attribute -> set_options ( array ( 'Fish' , 'Fingers' , 's:7:"pa_test' ) );
$local_attribute -> set_position ( 0 );
$local_attribute -> set_visible ( true );
$local_attribute -> set_variation ( false );
$global_attribute = new WC_Product_Attribute ();
$global_attribute -> set_id ( $global_attribute_data [ 'attribute_id' ] );
$global_attribute -> set_name ( $global_attribute_data [ 'attribute_taxonomy' ] );
$global_attribute -> set_options ( $global_attribute_data [ 'term_ids' ] );
$global_attribute -> set_position ( 1 );
$global_attribute -> set_visible ( true );
$global_attribute -> set_variation ( false );
$product = new WC_Product_Simple ();
$product -> set_attributes ( array (
'test-local' => $local_attribute ,
'test-global' => $global_attribute ,
) );
$product -> save ();
$meta_before_update = $wpdb -> get_results ( " SELECT meta_value FROM { $wpdb -> postmeta } WHERE meta_key = '_product_attributes' AND post_id = " . $product -> get_id (), ARRAY_A );
$product_meta_before_update = @ unserialize ( $meta_before_update [ 0 ][ 'meta_value' ] );
$this -> assertNotFalse ( $product_meta_before_update , 'Meta should be an unserializable string' );
// Update the global attribute.
$updated_global_attribute_id = wc_create_attribute (
array (
'id' => $global_attribute_data [ 'attribute_id' ],
'name' => 'Test Update' ,
'old_slug' => 'test' ,
'slug' => 'testupdate' ,
)
);
$this -> assertEquals ( $updated_global_attribute_id , $global_attribute_data [ 'attribute_id' ] );
// Changes to the global attribute should update in the product without causing side-effects.
$meta_after_update = $wpdb -> get_results ( " SELECT meta_value FROM { $wpdb -> postmeta } WHERE meta_key = '_product_attributes' AND post_id = " . $product -> get_id (), ARRAY_A );
$product_meta_after_update = @ unserialize ( $meta_after_update [ 0 ][ 'meta_value' ] );
$this -> assertNotFalse ( $product_meta_after_update , 'Meta should be an unserializable string' );
// @todo check individual attribute values are good once meta after update is unserializable.
}
/**
2017-07-07 21:50:27 +00:00
* Tests wc_update_attribute () .
*
* @ since 3.2 . 0
*/
public function test_wc_update_attribute () {
$args = array (
'name' => 'Brand' ,
'type' => 'select' ,
'order_by' => 'name' ,
'has_archives' => true ,
);
$id = wc_create_attribute ( $args );
$updated = array (
'id' => $id ,
'name' => 'Brand' ,
'slug' => 'pa_brand' ,
2017-12-08 11:35:26 +00:00
'type' => 'select' ,
2017-07-07 21:50:27 +00:00
'order_by' => 'menu_order' ,
'has_archives' => true ,
);
wc_update_attribute ( $id , $updated );
$attribute = ( array ) wc_get_attribute ( $id );
wc_delete_attribute ( $id );
$this -> assertEquals ( $updated , $attribute );
}
/**
* Tests wc_delete_attribute () .
*
* @ since 3.2 . 0
*/
public function test_wc_delete_attribute () {
// Success.
$id = wc_create_attribute ( array ( 'name' => 'Brand' ) );
$result = wc_delete_attribute ( $id );
$this -> assertTrue ( $result );
// Failure.
$result = wc_delete_attribute ( 9999999 );
$this -> assertFalse ( $result );
}
}