diff --git a/includes/abstracts/abstract-wc-data.php b/includes/abstracts/abstract-wc-data.php index 7a8e9c84b21f8..9d40318860fa4 100644 --- a/includes/abstracts/abstract-wc-data.php +++ b/includes/abstracts/abstract-wc-data.php @@ -53,7 +53,7 @@ abstract class WC_Data { * Returns the unique ID for this object. * @return int */ - abstract public function get_id(); + abstract public function get_id(); /** * Returns all data for this object. @@ -80,7 +80,7 @@ abstract public function update(); /** * Updates object data in the database. */ - abstract public function delete(); + abstract public function delete(); /** * Save should create or update based on object existance. @@ -127,7 +127,7 @@ public function get_meta( $key = '', $single = true ) { if ( $single ) { $value = $this->_meta_data[ current( $meta_ids ) ]->value; } else { - $value = array_intersect_key( $this->_meta_data, $meta_ids ); + $value = array_intersect_key( $this->_meta_data, array_flip( $meta_ids ) ); } } @@ -286,10 +286,10 @@ private function _get_db_info() { $table = $wpdb->prefix; // If we are dealing with a type of metadata that is not a core type, the table should be prefixed. if ( ! in_array( $this->_meta_type, array( 'post', 'user', 'comment', 'term' ) ) ) { - $table = 'woocommerce_'; + $table .= 'woocommerce_'; } - $table = $this->_meta_type . 'meta'; + $table .= $this->_meta_type . 'meta'; // Figure out our field names. if ( 'post' === $this->_meta_type ) { diff --git a/includes/abstracts/abstract-wc-payment-token.php b/includes/abstracts/abstract-wc-payment-token.php index ae27b582d6a0a..9820185dd3ad0 100644 --- a/includes/abstracts/abstract-wc-payment-token.php +++ b/includes/abstracts/abstract-wc-payment-token.php @@ -16,7 +16,7 @@ * @category Abstract Class * @author WooThemes */ - abstract class WC_Payment_Token implements WC_Data { + abstract class WC_Payment_Token { /** @protected int Token ID. */ protected $id; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 83521aba1a7f4..512cb692a2485 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -91,6 +91,7 @@ public function includes() { // framework require_once( $this->tests_dir . '/framework/class-wc-unit-test-factory.php' ); require_once( $this->tests_dir . '/framework/class-wc-mock-session-handler.php' ); + require_once( $this->tests_dir . '/framework/class-wc-mock-wc-data.php' ); require_once( $this->tests_dir . '/framework/class-wc-payment-token-stub.php' ); // test cases diff --git a/tests/framework/class-wc-mock-wc-data.php b/tests/framework/class-wc-mock-wc-data.php new file mode 100644 index 0000000000000..aaf58451eee04 --- /dev/null +++ b/tests/framework/class-wc-mock-wc-data.php @@ -0,0 +1,140 @@ + 0, + 'content' => '', + ); + + // 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. + |-------------------------------------------------------------------------- + | 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 + | switch out our class settings. + | These functions just change the properties set above. + */ + + + function set_cache_group( $cache_group ) { + $this->_cache_group = $cache_group; + } + + function set_meta_type( $meta_type ) { + $this->_meta_type = $meta_type; + } + + function set_object_id_field( $object_id_field ) { + $this->object_id_field_for_meta = $object_id_field; + } + + function set_internal_meta_keys( $internal_meta_keys ) { + $this->_internal_meta_keys = $internal_meta_keys; + } + + /* + |-------------------------------------------------------------------------- + | Abstract methods. + |-------------------------------------------------------------------------- + | Define the abstract methods WC_Data classes expect, so we can go on to + | testing the good bits. + */ + + public function __construct( $id = '' ) { + if ( ! empty( $id ) ) { + $this->read( $id ); + } + } + + public function get_id() { + return intval( $this->_data['id'] ); + } + + public function get_content() { + return $this->_data['content']; + } + + public function set_content( $content ) { + $this->_data['content'] = $content; + } + + public function get_data() { + return array_merge( + $this->_data, + array( + 'meta_data' => $this->get_meta_data(), + ) + ); + } + + 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->_data['id'] = $content_id; + } + } + + public function read( $id ) { + if ( empty( $id ) || ! ( $post_object = get_post( $id ) ) ) { + return; + } + $this->_data['id'] = absint( $post_object->ID ); + $this->set_content( $post_object->post_title ); + $this->read_meta_data(); + } + + 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() ) ); + } + } + + public function delete() { + if ( 'user' === $this->_meta_type ) { + wp_delete_user( $this->get_id() ); + } else { + wp_delete_post( $this->get_id() ); + } + } + + public function save() { + if ( ! $this->get_id() ) { + $this->create(); + } else { + $this->update(); + } + $this->save_meta_data(); + } + + /* + |-------------------------------------------------------------------------- + | Provide a public interface to a few of our. + |-------------------------------------------------------------------------- + | Define the abstract methods WC_Data classes expect, so we can go on to + | testing the good bits. + */ + +} diff --git a/tests/unit-tests/crud/meta.php b/tests/unit-tests/crud/meta.php new file mode 100644 index 0000000000000..7536608470e9e --- /dev/null +++ b/tests/unit-tests/crud/meta.php @@ -0,0 +1,106 @@ +set_content( 'testing' ); + $object->save(); + $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 ); // reload to make sure we get our meta... + return $object; + } + + /** + * Tests reading and getting set metadata. + */ + function test_get_meta_data() { + $object = $this->create_test_post(); + $meta_data = $object->get_meta_data(); + $i = 1; + + $this->assertNotEmpty( $meta_data ); + foreach ( $meta_data as $mid => $data ) { + $this->assertEquals( "val{$i}", $data->value ); + $i++; + } + } + + /** + * Test getting meta by ID. + */ + function test_get_meta() { + $object = $this->create_test_post(); + + // test single meta key + $single_meta = $object->get_meta( 'test_meta_key' ); + $this->assertEquals( 'val1', $single_meta ); + + // test getting multiple + $meta = $object->get_meta( 'test_multi_meta_key', false ); + $i = 2; + foreach ( $meta as $data ) { + $this->assertEquals( 'test_multi_meta_key', $data->key ); + $this->assertEquals( "val{$i}", $data->value ); + $i++; + } + } + + /** + * Test setting meta. + */ + function test_set_meta_data() { + + } + + /** + * Test adding meta data. + */ + function test_add_meta_data() { + + } + + /** + * Test updating meta data. + */ + function test_update_meta_data() { + + } + + /** + * Test getting user meta data. + */ + function test_user_get_meta_data() { + + } + + /** + * Test setting user meta data. + */ + function test_user_set_meta_data() { + + } + + /** + * Test adding user meta data. + */ + function test_user_add_meta_data() { + + } + + /** + * Test updating user meta data. + */ + function test_user_update_meta_data() { + + } + +} diff --git a/woocommerce.php b/woocommerce.php index 75b8344e6e857..00eec9066f77e 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -223,7 +223,6 @@ private function is_request( $type ) { * Include required core files used in admin and on the frontend. */ public function includes() { - include_once( 'includes/interfaces/interface-wc-data.php' ); include_once( 'includes/class-wc-autoloader.php' ); include_once( 'includes/wc-core-functions.php' ); include_once( 'includes/wc-widget-functions.php' ); @@ -256,6 +255,7 @@ public function includes() { include_once( 'includes/class-wc-auth.php' ); // Auth Class include_once( 'includes/class-wc-post-types.php' ); // Registers post types + include_once( 'includes/abstracts/abstract-wc-data.php' ); // WC_Data for CRUD include_once( 'includes/abstracts/abstract-wc-payment-token.php' ); // Payment Tokens include_once( 'includes/abstracts/abstract-wc-product.php' ); // Products include_once( 'includes/abstracts/abstract-wc-order.php' ); // Orders