From 5d6cdd05f11bd75cb156167451465e151bb64977 Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Mon, 14 Mar 2016 11:11:21 -0700 Subject: [PATCH] Finish out tests, make sure to use cprrect meta ID field in order by clause. --- includes/abstracts/abstract-wc-data.php | 16 ++- tests/framework/class-wc-mock-wc-data.php | 78 ++++++++---- tests/unit-tests/crud/meta.php | 142 ++++++++++++++++++---- 3 files changed, 188 insertions(+), 48 deletions(-) diff --git a/includes/abstracts/abstract-wc-data.php b/includes/abstracts/abstract-wc-data.php index 9d40318860f..1a08f592510 100644 --- a/includes/abstracts/abstract-wc-data.php +++ b/includes/abstracts/abstract-wc-data.php @@ -89,6 +89,7 @@ abstract class WC_Data { /** * Get All Meta Data + * @since 2.7.0 * @return array */ public function get_meta_data() { @@ -98,6 +99,7 @@ abstract class WC_Data { /** * Internal meta keys we don't want exposed as part of meta_data. This is in * addition to all data props with _ prefix. + * @since 2.7.0 * @return array() */ protected function prefix_key( $key ) { @@ -107,6 +109,7 @@ abstract class WC_Data { /** * Internal meta keys we don't want exposed as part of meta_data. This is in * addition to all data props with _ prefix. + * @since 2.7.0 * @return array() */ protected function get_internal_meta_keys() { @@ -115,6 +118,7 @@ abstract class WC_Data { /** * Get Meta Data by Key. + * @since 2.7.0 * @param string $key * @param bool $single return first found meta with key, or all with $key * @return mixed @@ -136,6 +140,7 @@ abstract class WC_Data { /** * Set all meta data from array. + * @since 2.7.0 * @param array $data Key/Value pairs */ public function set_meta_data( $data ) { @@ -155,6 +160,7 @@ abstract class WC_Data { /** * Add meta data. + * @since 2.7.0 * @param array $key Meta key * @param array $value Meta value * @param array $unique Should this be a unique key? @@ -172,6 +178,7 @@ abstract class WC_Data { /** * Update meta data by key or ID, if provided. + * @since 2.7.0 * @param string $key * @param string $value * @param int $meta_id @@ -189,6 +196,7 @@ abstract class WC_Data { /** * Delete meta data. + * @since 2.7.0 * @param array $key Meta key */ public function delete_meta_data( $key ) { @@ -198,6 +206,7 @@ abstract class WC_Data { /** * Read Meta Data from the database. Ignore any internal properties. + * @since 2.7.0 */ protected function read_meta_data() { $this->_meta_data = array(); @@ -223,14 +232,15 @@ abstract class WC_Data { $raw_meta_data = $wpdb->get_results( $wpdb->prepare( " SELECT " . $db_info['meta_id_field'] . ", meta_key, meta_value FROM " . $db_info['table'] . " - WHERE " . $db_info['object_id_field'] . " = %d ORDER BY meta_id + WHERE " . $db_info['object_id_field'] . " = %d ORDER BY " . $db_info['meta_id_field'] . " ", $this->get_id() ) ); foreach ( $raw_meta_data as $meta ) { if ( in_array( $meta->meta_key, $this->get_internal_meta_keys() ) ) { continue; } - $this->_meta_data[ $meta->meta_id ] = (object) array( 'key' => $meta->meta_key, 'value' => $meta->meta_value ); + + $this->_meta_data[ $meta->{$db_info['meta_id_field']} ] = (object) array( 'key' => $meta->meta_key, 'value' => $meta->meta_value ); } if ( ! empty ( $this->_cache_group ) ) { @@ -241,6 +251,7 @@ abstract class WC_Data { /** * Update Meta Data in the database. + * @since 2.7.0 */ protected function save_meta_data() { global $wpdb; @@ -276,6 +287,7 @@ abstract class WC_Data { /** * Table structure is slightly different between meta types, this function will return what we need to know. + * @since 2.7.0 * @return array Array elements: table, object_id_field, meta_id_field */ private function _get_db_info() { diff --git a/tests/framework/class-wc-mock-wc-data.php b/tests/framework/class-wc-mock-wc-data.php index aaf58451eee..5898c6851fb 100644 --- a/tests/framework/class-wc-mock-wc-data.php +++ b/tests/framework/class-wc-mock-wc-data.php @@ -28,23 +28,22 @@ class WC_Mock_WC_data extends WC_Data { | These functions just change the properties set above. */ - - function set_cache_group( $cache_group ) { - $this->_cache_group = $cache_group; - } - + /** + * Set meta type (user or post). + * @param string $meta_type + */ function set_meta_type( $meta_type ) { $this->_meta_type = $meta_type; } + /** + * Set object ID field dynamically for testing. + * @param string $object_id_field + */ 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. @@ -53,24 +52,43 @@ class WC_Mock_WC_data extends WC_Data { | testing the good bits. */ + /** + * Simple read. + */ public function __construct( $id = '' ) { if ( ! empty( $id ) ) { $this->read( $id ); } } + /** + * Simple get ID. + * @return integer + */ public function get_id() { return intval( $this->_data['id'] ); } + /** + * Simple get content. + * @return string + */ public function get_content() { return $this->_data['content']; } + /** + * SImple set content. + * @param string $content + */ public function set_content( $content ) { $this->_data['content'] = $content; } + /** + * Simple get data as array. + * @return array + */ public function get_data() { return array_merge( $this->_data, @@ -80,27 +98,45 @@ 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->_data['id'] = $content_id; } } + /** + * Simple read. + */ public function read( $id ) { - if ( empty( $id ) || ! ( $post_object = get_post( $id ) ) ) { - return; + + if ( 'user' === $this->_meta_type ) { + if ( empty( $id ) || ! ( $user_object = get_userdata( $id ) ) ) { + return; + } + $this->_data['id'] = absint( $user_object->ID ); + $this->set_content( $user_object->user_email ); + } else { + if ( empty( $id ) || ! ( $post_object = get_post( $id ) ) ) { + return; + } + $this->_data['id'] = absint( $post_object->ID ); + $this->set_content( $post_object->post_title ); } - $this->_data['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(); @@ -112,6 +148,9 @@ class WC_Mock_WC_data extends WC_Data { } } + /** + * Simple delete. + */ public function delete() { if ( 'user' === $this->_meta_type ) { wp_delete_user( $this->get_id() ); @@ -120,6 +159,9 @@ class WC_Mock_WC_data extends WC_Data { } } + /** + * Simple save. + */ public function save() { if ( ! $this->get_id() ) { $this->create(); @@ -129,12 +171,4 @@ class WC_Mock_WC_data extends WC_Data { $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 index 7536608470e..ea748181771 100644 --- a/tests/unit-tests/crud/meta.php +++ b/tests/unit-tests/crud/meta.php @@ -7,15 +7,25 @@ namespace WooCommerce\Tests\CRUD; */ class Meta extends \WC_Unit_Test_Case { + /** + * Create a test post we can add/test meta against. + */ public function create_test_post() { $object = new \WC_Mock_WC_data(); $object->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; + } + + /** + * Create a test user we can add/test meta against. + */ + public function create_test_user() { + $object = new \WC_Mock_WC_data(); + $object->set_meta_type( 'user' ); + $object->set_object_id_field( 'user_id' ); + $object->set_content( 'testing@woo.dev' ); + $object->save(); return $object; } @@ -24,6 +34,12 @@ class Meta 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 ); + $meta_data = $object->get_meta_data(); $i = 1; @@ -39,6 +55,11 @@ class Meta 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 ); // test single meta key $single_meta = $object->get_meta( 'test_meta_key' ); @@ -58,49 +79,122 @@ class Meta extends \WC_Unit_Test_Case { * Test setting meta. */ function test_set_meta_data() { + 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 ); + $metadata = array(); + $raw_metadata = $wpdb->get_results( $wpdb->prepare( " + SELECT meta_id, meta_key, meta_value + FROM {$wpdb->prefix}postmeta + WHERE post_id = %d ORDER BY meta_id + ", $object_id ) ); + + foreach ( $raw_metadata as $meta ) { + $metadata[ $meta->meta_id ] = (object) array( 'key' => $meta->meta_key, 'value' => $meta->meta_value ); + } + + $object = new \WC_Mock_WC_data(); + $object->set_meta_data( $metadata ); + + $this->assertEquals( $metadata, $object->get_meta_data() ); } /** * Test adding meta data. */ function test_add_meta_data() { - + $object = $this->create_test_post(); + $object_id = $object->get_id(); + $data = 'add_meta_data_' . time(); + $object->add_meta_data( 'test_new_field', $data ); + $meta = $object->get_meta( 'test_new_field' ); + $this->assertEquals( $data, $meta ); } /** * Test updating meta data. */ function test_update_meta_data() { + global $wpdb; + $object = $this->create_test_post(); + $object_id = $object->get_id(); + add_metadata( 'post', $object_id, 'test_meta_key', 'val1', true ); + $object->read( $object_id ); + $this->assertEquals( 'val1', $object->get_meta( 'test_meta_key' ) ); + + $metadata = array(); + $meta_id = $wpdb->get_var( $wpdb->prepare( " + SELECT meta_id + FROM {$wpdb->prefix}postmeta + WHERE post_id = %d LIMIT 1 + ", $object_id ) ); + + $object->update_meta_data( 'test_meta_key', 'updated_value', $meta_id ); + $this->assertEquals( 'updated_value', $object->get_meta( 'test_meta_key' ) ); } /** - * Test getting user meta data. + * Test deleting meta. */ - function test_user_get_meta_data() { + function test_delete_meta_data() { + $object = $this->create_test_post(); + $object_id = $object->get_id(); + add_metadata( 'post', $object_id, 'test_meta_key', 'val1', true ); + $object->read( $object_id ); + $this->assertEquals( 'val1', $object->get_meta( 'test_meta_key' ) ); + + $object->delete_meta_data( 'test_meta_key' ); + + $this->assertEmpty( $object->get_meta( 'test_meta_key' ) ); + } + + + /** + * Test saving metadata.. (Actually making sure changes are written to DB) + */ + function test_save_meta_data() { + 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 ); + + $raw_metadata = $wpdb->get_results( $wpdb->prepare( " + SELECT meta_id, meta_key, meta_value + FROM {$wpdb->prefix}postmeta + WHERE post_id = %d ORDER BY meta_id + ", $object_id ) ); + + + $object->delete_meta_data( 'test_meta_key' ); + $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 + + $this->assertEmpty( $object->get_meta( 'test_meta_key' ) ); + $this->assertEquals( 'updated_value', $object->get_meta( 'test_meta_key_2' ) ); } /** - * Test setting user meta data. + * Test reading/getting user meta data too. */ - 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() { + 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 ); + $this->assertEquals( 'val1', $object->get_meta( 'test_meta_key' ) ); + $this->assertEquals( 'val2', $object->get_meta( 'test_meta_key_2' ) ); } }