Skip to content

Commit

Permalink
Finish out tests, make sure to use cprrect meta ID field in order by …
Browse files Browse the repository at this point in the history
…clause.
  • Loading branch information
justinshreve committed Mar 14, 2016
1 parent 97c0edf commit 5d6cdd0
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 41 deletions.
16 changes: 14 additions & 2 deletions includes/abstracts/abstract-wc-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ abstract public function save();

/**
* Get All Meta Data
* @since 2.7.0
* @return array
*/
public function get_meta_data() {
Expand All @@ -98,6 +99,7 @@ public function get_meta_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 ) {
Expand All @@ -107,6 +109,7 @@ protected function prefix_key( $key ) {
/**
* 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() {
Expand All @@ -115,6 +118,7 @@ protected function get_internal_meta_keys() {

/**
* 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
Expand All @@ -136,6 +140,7 @@ public function get_meta( $key = '', $single = true ) {

/**
* Set all meta data from array.
* @since 2.7.0
* @param array $data Key/Value pairs
*/
public function set_meta_data( $data ) {
Expand All @@ -155,6 +160,7 @@ public function set_meta_data( $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?
Expand All @@ -172,6 +178,7 @@ public function add_meta_data( $key, $value, $unique = false ) {

/**
* Update meta data by key or ID, if provided.
* @since 2.7.0
* @param string $key
* @param string $value
* @param int $meta_id
Expand All @@ -189,6 +196,7 @@ public function update_meta_data( $key, $value, $meta_id = '' ) {

/**
* Delete meta data.
* @since 2.7.0
* @param array $key Meta key
*/
public function delete_meta_data( $key ) {
Expand All @@ -198,6 +206,7 @@ public function delete_meta_data( $key ) {

/**
* Read Meta Data from the database. Ignore any internal properties.
* @since 2.7.0
*/
protected function read_meta_data() {
$this->_meta_data = array();
Expand All @@ -223,14 +232,15 @@ protected function read_meta_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 ) ) {
Expand All @@ -241,6 +251,7 @@ protected function read_meta_data() {

/**
* Update Meta Data in the database.
* @since 2.7.0
*/
protected function save_meta_data() {
global $wpdb;
Expand Down Expand Up @@ -276,6 +287,7 @@ protected function save_meta_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() {
Expand Down
78 changes: 56 additions & 22 deletions tests/framework/class-wc-mock-wc-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -53,24 +52,43 @@ function set_internal_meta_keys( $internal_meta_keys ) {
| 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,
Expand All @@ -80,27 +98,45 @@ public function get_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();
Expand All @@ -112,6 +148,9 @@ public function update() {
}
}

/**
* Simple delete.
*/
public function delete() {
if ( 'user' === $this->_meta_type ) {
wp_delete_user( $this->get_id() );
Expand All @@ -120,6 +159,9 @@ public function delete() {
}
}

/**
* Simple save.
*/
public function save() {
if ( ! $this->get_id() ) {
$this->create();
Expand All @@ -129,12 +171,4 @@ public function save() {
$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.
*/

}
Loading

0 comments on commit 5d6cdd0

Please sign in to comment.