Fix up data tests and some coding standards issues
This commit is contained in:
parent
0814c2c579
commit
dd0a2317f1
|
@ -884,7 +884,6 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
|
|||
$item->set_backorder_meta();
|
||||
$item->set_order_id( $this->get_id() );
|
||||
$item->save();
|
||||
|
||||
$this->add_item( $item );
|
||||
wc_do_deprecated_action( 'woocommerce_order_add_product', array( $this->get_id(), $item->get_id(), $product, $qty, $args ), '2.7', 'Use woocommerce_new_order_item action instead.' );
|
||||
return $item->get_id();
|
||||
|
|
|
@ -5,6 +5,9 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
|
||||
/**
|
||||
* Shared logic for WP based data.
|
||||
* Contains functions like meta handling for all default data stores.
|
||||
* Your own data store doesn't need to use WC_Data_Store_WP -- you can write
|
||||
* your own meta handling functions.
|
||||
*
|
||||
* @version 2.7.0
|
||||
* @category Class
|
||||
|
@ -28,7 +31,7 @@ class WC_Data_Store_WP {
|
|||
protected $object_id_field_for_meta = '';
|
||||
|
||||
/**
|
||||
* Data stored in meta keys, but not considered "meta" for a coupon.
|
||||
* Data stored in meta keys, but not considered "meta" for an object.
|
||||
* @since 2.7.0
|
||||
* @var array
|
||||
*/
|
||||
|
@ -50,6 +53,13 @@ class WC_Data_Store_WP {
|
|||
return wp_list_pluck( $terms, 'term_id' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of meta for an object.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Data
|
||||
* @return array
|
||||
*/
|
||||
public function read_meta( &$object ) {
|
||||
global $wpdb;
|
||||
$db_info = $this->get_db_info();
|
||||
|
@ -63,14 +73,37 @@ class WC_Data_Store_WP {
|
|||
return array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes meta based on meta ID.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Data
|
||||
* @param stdClass (containing at least ->id)
|
||||
* @return array
|
||||
*/
|
||||
public function delete_meta( &$object, $meta ) {
|
||||
delete_metadata_by_mid( $this->meta_type, $meta->id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new piece of meta.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Data
|
||||
* @param stdClass (containing ->key and ->value)
|
||||
* @return meta ID
|
||||
*/
|
||||
public function add_meta( &$object, $meta ) {
|
||||
return add_metadata( $this->meta_type, $object->get_id(), $meta->key, $meta->value, false );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update meta.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Data
|
||||
* @param stdClass (containing ->id, ->key and ->value)
|
||||
*/
|
||||
public function update_meta( &$object, $meta ) {
|
||||
update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key );
|
||||
}
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* WC Meta Data Store Interface
|
||||
*
|
||||
* Functions that must be defined by meta store classes.
|
||||
*
|
||||
* @version 2.7.0
|
||||
* @category Interface
|
||||
* @author WooCommerce
|
||||
*/
|
||||
interface WC_Meta_Data_Store_Interface {
|
||||
/**
|
||||
* Returns an array of objects containing meta_id, meta_key, and meta_value.
|
||||
* @param WC_Data $object
|
||||
* @return array
|
||||
*/
|
||||
public function read_meta( $object );
|
||||
|
||||
/**
|
||||
* Deletes a piece of meta.
|
||||
* @param WC_Data $object
|
||||
* @param object $meta Object containing ->id, ->key, and possibily ->value.
|
||||
*/
|
||||
public function delete_meta( $object, $meta );
|
||||
|
||||
/**
|
||||
* Adds meta.
|
||||
* @param WC_Data $object
|
||||
* @param object $meta Object containing ->key and ->value
|
||||
* @return int Meta ID
|
||||
*/
|
||||
public function add_meta( $object, $meta );
|
||||
|
||||
/**
|
||||
* Updates meta.
|
||||
* @param WC_Data $object
|
||||
* @param object $meta Object containing ->id, ->key and ->value
|
||||
*/
|
||||
public function update_meta( $object, $meta );
|
||||
}
|
|
@ -37,11 +37,33 @@ interface WC_Object_Data_Store {
|
|||
*/
|
||||
public function delete( &$data, $args = array() );
|
||||
|
||||
/**
|
||||
* Returns an array of meta for an object.
|
||||
* @param WC_Data
|
||||
* @return array
|
||||
*/
|
||||
public function read_meta( &$data );
|
||||
|
||||
/**
|
||||
* Deletes meta based on meta ID.
|
||||
* @param WC_Data
|
||||
* @param stdClass (containing at least ->id)
|
||||
* @return array
|
||||
*/
|
||||
public function delete_meta( &$data, $meta );
|
||||
|
||||
/**
|
||||
* Add new piece of meta.
|
||||
* @param WC_Data
|
||||
* @param stdClass (containing ->key and ->value)
|
||||
* @return meta ID
|
||||
*/
|
||||
public function add_meta( &$data, $meta );
|
||||
|
||||
/**
|
||||
* Update meta.
|
||||
* @param WC_Data
|
||||
* @param stdClass (containing ->id, ->key and ->value)
|
||||
*/
|
||||
public function update_meta( &$data, $meta );
|
||||
}
|
||||
|
|
|
@ -1,26 +1,13 @@
|
|||
<?php
|
||||
/**
|
||||
* Used for exposing and testing the various Abstract WC_Data methods.
|
||||
*/
|
||||
class WC_Mock_WC_Data extends WC_Data {
|
||||
class WC_Mock_WC_Data_Store extends WC_Data_Store_WP implements WC_Object_Data_Store {
|
||||
|
||||
/**
|
||||
* Data array
|
||||
*/
|
||||
protected $data = array(
|
||||
'content' => '',
|
||||
'bool_value' => false,
|
||||
);
|
||||
|
||||
// see WC_Data
|
||||
protected $cache_group = '';
|
||||
protected $meta_type = 'post';
|
||||
protected $object_id_field_for_meta = '';
|
||||
protected $internal_meta_keys = array();
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Setters for internal WC_Data properties.
|
||||
| Setters for internal properties.
|
||||
|--------------------------------------------------------------------------
|
||||
| Normally we wouldn't want to be able to change this once the class is defined,
|
||||
| but to make testing different types of meta/storage, we should be able to
|
||||
|
@ -44,6 +31,88 @@ class WC_Mock_WC_Data extends WC_Data {
|
|||
$this->object_id_field_for_meta = $object_id_field;
|
||||
}
|
||||
|
||||
public function create( &$object ) {
|
||||
if ( 'user' === $this->meta_type ) {
|
||||
$content_id = wc_create_new_customer( $object->get_content(), 'username-' . time(), 'hunter2' );
|
||||
} else {
|
||||
$content_id = wp_insert_post( array( 'post_title' => $object->get_content() ) );
|
||||
}
|
||||
if ( $content_id ) {
|
||||
$object->set_id( $content_id );
|
||||
}
|
||||
|
||||
$object->apply_changes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple read.
|
||||
*/
|
||||
public function read( &$object ) {
|
||||
$object->set_defaults();
|
||||
$id = $object->get_id();
|
||||
|
||||
if ( 'user' === $this->meta_type ) {
|
||||
if ( empty( $id ) || ! ( $user_object = get_userdata( $id ) ) ) {
|
||||
return;
|
||||
}
|
||||
$object->set_content( $user_object->user_email );
|
||||
} else {
|
||||
if ( empty( $id ) || ! ( $post_object = get_post( $id ) ) ) {
|
||||
return;
|
||||
}
|
||||
$object->set_content( $post_object->post_title );
|
||||
}
|
||||
|
||||
$object->read_meta_data();
|
||||
$object->set_object_read( true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple update.
|
||||
*/
|
||||
public function update( &$object ) {
|
||||
global $wpdb;
|
||||
$content_id = $object->get_id();
|
||||
|
||||
if ( 'user' === $this->meta_type ) {
|
||||
wp_update_user( array( 'ID' => $customer_id, 'user_email' => $object->get_content() ) );
|
||||
} else {
|
||||
wp_update_post( array( 'ID' => $content_id, 'post_title' => $object->get_content() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple delete.
|
||||
*/
|
||||
public function delete( &$object, $args = array() ) {
|
||||
if ( 'user' === $this->meta_type ) {
|
||||
wp_delete_user( $object->get_id() );
|
||||
} else {
|
||||
wp_delete_post( $object->get_id() );
|
||||
}
|
||||
|
||||
$object->set_id( 0 );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Used for exposing and testing the various Abstract WC_Data methods.
|
||||
*/
|
||||
class WC_Mock_WC_Data extends WC_Data {
|
||||
|
||||
/**
|
||||
* Data array
|
||||
*/
|
||||
protected $data = array(
|
||||
'content' => '',
|
||||
'bool_value' => false,
|
||||
);
|
||||
|
||||
// see WC_Data
|
||||
protected $cache_group = '';
|
||||
public $data_store;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Abstract methods.
|
||||
|
@ -58,43 +127,57 @@ class WC_Mock_WC_Data extends WC_Data {
|
|||
public function __construct( $id = '' ) {
|
||||
parent::__construct();
|
||||
if ( ! empty( $id ) ) {
|
||||
$this->read( $id );
|
||||
$this->set_id( $id );
|
||||
} else {
|
||||
$this->set_object_read( true );
|
||||
}
|
||||
|
||||
$this->data_store = new WC_Mock_WC_Data_Store;
|
||||
|
||||
if ( $this->get_id() > 0 ) {
|
||||
$this->data_store->read( $this );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple get content.
|
||||
*
|
||||
* @param string $context
|
||||
* @return string
|
||||
*/
|
||||
public function get_content() {
|
||||
return $this->data['content'];
|
||||
public function get_content( $context = 'view' ) {
|
||||
return $this->get_prop( 'content', $context );
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple set content.
|
||||
*
|
||||
* @param string $content
|
||||
*/
|
||||
public function set_content( $content ) {
|
||||
$this->data['content'] = $content;
|
||||
$this->set_prop('content', $content );
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple get bool value.
|
||||
*
|
||||
* @param string $context
|
||||
* @return bool
|
||||
*/
|
||||
public function get_bool_value() {
|
||||
return $this->data['bool_value'];
|
||||
public function get_bool_value( $context = 'view' ) {
|
||||
return $this->get_prop( 'bool_value', $context );
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple set bool value.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function set_bool_value( $value ) {
|
||||
if ( ! is_bool( $value ) ) {
|
||||
$this->error( 'invalid_bool_value', 'O noes' );
|
||||
}
|
||||
$this->data['bool_value'] = $value;
|
||||
$this->set_prop( 'bool_value', $value );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -110,77 +193,18 @@ class WC_Mock_WC_Data extends WC_Data {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple create.
|
||||
*/
|
||||
public function create() {
|
||||
if ( 'user' === $this->meta_type ) {
|
||||
$content_id = wc_create_new_customer( $this->get_content(), 'username-' . time(), 'hunter2' );
|
||||
} else {
|
||||
$content_id = wp_insert_post( array( 'post_title' => $this->get_content() ) );
|
||||
}
|
||||
if ( $content_id ) {
|
||||
$this->set_id( $content_id );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple read.
|
||||
*/
|
||||
public function read( $id ) {
|
||||
$this->set_defaults();
|
||||
|
||||
if ( 'user' === $this->meta_type ) {
|
||||
if ( empty( $id ) || ! ( $user_object = get_userdata( $id ) ) ) {
|
||||
return;
|
||||
}
|
||||
$this->set_id( absint( $user_object->ID ) );
|
||||
$this->set_content( $user_object->user_email );
|
||||
} else {
|
||||
if ( empty( $id ) || ! ( $post_object = get_post( $id ) ) ) {
|
||||
return;
|
||||
}
|
||||
$this->set_id( absint( $post_object->ID ) );
|
||||
$this->set_content( $post_object->post_title );
|
||||
}
|
||||
|
||||
$this->read_meta_data();
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple update.
|
||||
*/
|
||||
public function update() {
|
||||
global $wpdb;
|
||||
$content_id = $this->get_id();
|
||||
|
||||
if ( 'user' === $this->meta_type ) {
|
||||
wp_update_user( array( 'ID' => $customer_id, 'user_email' => $this->get_content() ) );
|
||||
} else {
|
||||
wp_update_post( array( 'ID' => $content_id, 'post_title' => $this->get_content() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple delete.
|
||||
*/
|
||||
public function delete( $force_delete = false ) {
|
||||
if ( 'user' === $this->meta_type ) {
|
||||
wp_delete_user( $this->get_id() );
|
||||
} else {
|
||||
wp_delete_post( $this->get_id() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple save.
|
||||
*/
|
||||
public function save() {
|
||||
if ( ! $this->get_id() ) {
|
||||
$this->create();
|
||||
} else {
|
||||
$this->update();
|
||||
if ( $this->data_store ) {
|
||||
if ( $this->get_id() ) {
|
||||
$this->data_store->update( $this );
|
||||
} else {
|
||||
$this->data_store->create( $this );
|
||||
}
|
||||
}
|
||||
$this->save_meta_data();
|
||||
return $this->get_id();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -90,7 +90,6 @@ class WC_Helper_Order {
|
|||
$order->set_cart_tax( 0 );
|
||||
$order->set_shipping_tax( 0 );
|
||||
$order->set_total( 40 ); // 4 x $10 simple helper product
|
||||
|
||||
$order->save();
|
||||
|
||||
return $order;
|
||||
|
|
|
@ -11,6 +11,8 @@ class WC_Tests_CRUD_Data extends WC_Unit_Test_Case {
|
|||
*/
|
||||
public function create_test_post() {
|
||||
$object = new WC_Mock_WC_Data();
|
||||
$object->data_store->set_meta_type( 'post' );
|
||||
$object->data_store->set_object_id_field( '' );
|
||||
$object->set_content( 'testing' );
|
||||
$object->save();
|
||||
return $object;
|
||||
|
@ -21,8 +23,8 @@ class WC_Tests_CRUD_Data extends WC_Unit_Test_Case {
|
|||
*/
|
||||
public function create_test_user() {
|
||||
$object = new WC_Mock_WC_Data();
|
||||
$object->set_meta_type( 'user' );
|
||||
$object->set_object_id_field( 'user_id' );
|
||||
$object->data_store->set_meta_type( 'user' );
|
||||
$object->data_store->set_object_id_field( 'user_id' );
|
||||
$object->set_content( 'testing@woo.dev' );
|
||||
$object->save();
|
||||
return $object;
|
||||
|
@ -77,10 +79,10 @@ class WC_Tests_CRUD_Data extends WC_Unit_Test_Case {
|
|||
function test_get_meta_data() {
|
||||
$object = $this->create_test_post();
|
||||
$object_id = $object->get_id();
|
||||
add_metadata( 'post', $object_id, 'test_meta_key', 'val1', true );
|
||||
add_metadata( 'post', $object_id, 'test_multi_meta_key', 'val2' );
|
||||
add_metadata( 'post', $object_id, 'test_multi_meta_key', 'val3' );
|
||||
$object->read( $object_id );
|
||||
$object->add_meta_data( 'test_meta_key', 'val1', true );
|
||||
$object->add_meta_data( 'test_multi_meta_key', 'val2' );
|
||||
$object->add_meta_data( 'test_multi_meta_key', 'val3' );
|
||||
$object->save_meta_data();
|
||||
|
||||
$meta_data = $object->get_meta_data();
|
||||
$i = 1;
|
||||
|
@ -98,10 +100,11 @@ class WC_Tests_CRUD_Data extends WC_Unit_Test_Case {
|
|||
function test_get_meta() {
|
||||
$object = $this->create_test_post();
|
||||
$object_id = $object->get_id();
|
||||
add_metadata( 'post', $object_id, 'test_meta_key', 'val1', true );
|
||||
add_metadata( 'post', $object_id, 'test_multi_meta_key', 'val2' );
|
||||
add_metadata( 'post', $object_id, 'test_multi_meta_key', 'val3' );
|
||||
$object->read( $object_id );
|
||||
$object->add_meta_data( 'test_meta_key', 'val1', true );
|
||||
$object->add_meta_data( 'test_multi_meta_key', 'val2' );
|
||||
$object->add_meta_data( 'test_multi_meta_key', 'val3' );
|
||||
$object->save_meta_data();
|
||||
$object = new WC_Mock_WC_Data( $object_id );
|
||||
|
||||
// test single meta key
|
||||
$single_meta = $object->get_meta( 'test_meta_key' );
|
||||
|
@ -126,7 +129,7 @@ class WC_Tests_CRUD_Data extends WC_Unit_Test_Case {
|
|||
$object_id = $object->get_id();
|
||||
add_metadata( 'post', $object_id, 'test_meta_key', 'val1', true );
|
||||
add_metadata( 'post', $object_id, 'test_meta_key_2', 'val2', true );
|
||||
$object->read( $object_id );
|
||||
$object = new WC_Mock_WC_Data( $object_id );
|
||||
|
||||
$metadata = array();
|
||||
$raw_metadata = $wpdb->get_results( $wpdb->prepare( "
|
||||
|
@ -169,7 +172,7 @@ class WC_Tests_CRUD_Data extends WC_Unit_Test_Case {
|
|||
$object = $this->create_test_post();
|
||||
$object_id = $object->get_id();
|
||||
add_metadata( 'post', $object_id, 'test_meta_key', 'val1', true );
|
||||
$object->read( $object_id );
|
||||
$object = new WC_Mock_WC_Data( $object_id );
|
||||
|
||||
$this->assertEquals( 'val1', $object->get_meta( 'test_meta_key' ) );
|
||||
|
||||
|
@ -191,7 +194,7 @@ class WC_Tests_CRUD_Data extends WC_Unit_Test_Case {
|
|||
$object = $this->create_test_post();
|
||||
$object_id = $object->get_id();
|
||||
add_metadata( 'post', $object_id, 'test_meta_key', 'val1', true );
|
||||
$object->read( $object_id );
|
||||
$object = new WC_Mock_WC_Data( $object_id );
|
||||
|
||||
$this->assertEquals( 'val1', $object->get_meta( 'test_meta_key' ) );
|
||||
|
||||
|
@ -208,9 +211,10 @@ class WC_Tests_CRUD_Data extends WC_Unit_Test_Case {
|
|||
global $wpdb;
|
||||
$object = $this->create_test_post();
|
||||
$object_id = $object->get_id();
|
||||
add_metadata( 'post', $object_id, 'test_meta_key', 'val1', true );
|
||||
add_metadata( 'post', $object_id, 'test_meta_key_2', 'val2', true );
|
||||
$object->read( $object_id );
|
||||
$object->add_meta_data( 'test_meta_key', 'val1', true );
|
||||
$object->add_meta_data( 'test_meta_key_2', 'val2', true );
|
||||
$object->save_meta_data();
|
||||
$object = new WC_Mock_WC_Data( $object_id );
|
||||
|
||||
$raw_metadata = $wpdb->get_results( $wpdb->prepare( "
|
||||
SELECT meta_id, meta_key, meta_value
|
||||
|
@ -222,7 +226,7 @@ class WC_Tests_CRUD_Data extends WC_Unit_Test_Case {
|
|||
$object->update_meta_data( 'test_meta_key_2', 'updated_value', $raw_metadata[1]->meta_id );
|
||||
|
||||
$object->save();
|
||||
$object->read( $object_id ); // rereads from the DB
|
||||
$object = new WC_Mock_WC_Data( $object_id ); // rereads from the DB
|
||||
|
||||
$this->assertEmpty( $object->get_meta( 'test_meta_key' ) );
|
||||
$this->assertEquals( 'updated_value', $object->get_meta( 'test_meta_key_2' ) );
|
||||
|
@ -234,9 +238,9 @@ class WC_Tests_CRUD_Data extends WC_Unit_Test_Case {
|
|||
function test_usermeta() {
|
||||
$object = $this->create_test_user();
|
||||
$object_id = $object->get_id();
|
||||
add_metadata( 'user', $object_id, 'test_meta_key', 'val1', true );
|
||||
add_metadata( 'user', $object_id, 'test_meta_key_2', 'val2', true );
|
||||
$object->read( $object_id );
|
||||
$object->add_meta_data( 'test_meta_key', 'val1', true );
|
||||
$object->add_meta_data( 'test_meta_key_2', 'val2', true );
|
||||
$object->save_meta_data();
|
||||
|
||||
$this->assertEquals( 'val1', $object->get_meta( 'test_meta_key' ) );
|
||||
$this->assertEquals( 'val2', $object->get_meta( 'test_meta_key_2' ) );
|
||||
|
|
|
@ -220,7 +220,6 @@ class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
|
|||
$customer_id = $customer->get_id();
|
||||
$order = WC_Helper_Order::create_order( $customer_id );
|
||||
$customer = new WC_Customer( $customer_id );
|
||||
|
||||
$last_order = $customer->get_last_order();
|
||||
$this->assertEquals( $order->get_id(), $last_order ? $last_order->get_id() : 0 );
|
||||
$this->assertEquals( $order->get_date_created(), $last_order ? $last_order->get_date_created() : 0 );
|
||||
|
|
Loading…
Reference in New Issue