2016-03-11 21:48:16 +00:00
|
|
|
<?php
|
2016-11-22 13:54:51 +00:00
|
|
|
class WC_Mock_WC_Data_Store extends WC_Data_Store_WP implements WC_Object_Data_Store_Interface {
|
2016-03-11 21:48:16 +00:00
|
|
|
|
2019-05-01 22:05:00 +00:00
|
|
|
protected $meta_type = 'post';
|
2016-03-11 21:48:16 +00:00
|
|
|
protected $object_id_field_for_meta = '';
|
2019-05-01 22:05:00 +00:00
|
|
|
protected $internal_meta_keys = array();
|
2016-03-11 21:48:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
2016-11-22 11:34:14 +00:00
|
|
|
| Setters for internal properties.
|
2016-03-11 21:48:16 +00:00
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| 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.
|
|
|
|
*/
|
|
|
|
|
2016-03-14 18:11:21 +00:00
|
|
|
/**
|
|
|
|
* Set meta type (user or post).
|
|
|
|
* @param string $meta_type
|
|
|
|
*/
|
2016-03-11 21:48:16 +00:00
|
|
|
function set_meta_type( $meta_type ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->meta_type = $meta_type;
|
2016-03-11 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 18:11:21 +00:00
|
|
|
/**
|
|
|
|
* Set object ID field dynamically for testing.
|
|
|
|
* @param string $object_id_field
|
|
|
|
*/
|
2016-03-11 21:48:16 +00:00
|
|
|
function set_object_id_field( $object_id_field ) {
|
|
|
|
$this->object_id_field_for_meta = $object_id_field;
|
|
|
|
}
|
|
|
|
|
2016-11-22 11:34:14 +00:00
|
|
|
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 ) {
|
2019-05-01 22:05:00 +00:00
|
|
|
wp_update_user(
|
|
|
|
array(
|
|
|
|
'ID' => $customer_id,
|
|
|
|
'user_email' => $object->get_content(),
|
|
|
|
)
|
|
|
|
);
|
2016-11-22 11:34:14 +00:00
|
|
|
} else {
|
2019-05-01 22:05:00 +00:00
|
|
|
wp_update_post(
|
|
|
|
array(
|
|
|
|
'ID' => $content_id,
|
|
|
|
'post_title' => $object->get_content(),
|
|
|
|
)
|
|
|
|
);
|
2016-11-22 11:34:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2016-03-11 21:48:16 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Abstract methods.
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Define the abstract methods WC_Data classes expect, so we can go on to
|
|
|
|
| testing the good bits.
|
|
|
|
*/
|
|
|
|
|
2016-03-14 18:11:21 +00:00
|
|
|
/**
|
|
|
|
* Simple read.
|
|
|
|
*/
|
2016-03-11 21:48:16 +00:00
|
|
|
public function __construct( $id = '' ) {
|
2016-08-25 16:42:47 +00:00
|
|
|
parent::__construct();
|
2016-03-11 21:48:16 +00:00
|
|
|
if ( ! empty( $id ) ) {
|
2016-11-22 11:34:14 +00:00
|
|
|
$this->set_id( $id );
|
|
|
|
} else {
|
|
|
|
$this->set_object_read( true );
|
|
|
|
}
|
|
|
|
|
2019-05-01 22:05:00 +00:00
|
|
|
$this->data_store = new WC_Mock_WC_Data_Store();
|
2016-11-22 11:34:14 +00:00
|
|
|
|
|
|
|
if ( $this->get_id() > 0 ) {
|
|
|
|
$this->data_store->read( $this );
|
2016-03-11 21:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 18:11:21 +00:00
|
|
|
/**
|
|
|
|
* Simple get content.
|
2016-11-22 11:34:14 +00:00
|
|
|
*
|
|
|
|
* @param string $context
|
2016-03-14 18:11:21 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-22 11:34:14 +00:00
|
|
|
public function get_content( $context = 'view' ) {
|
|
|
|
return $this->get_prop( 'content', $context );
|
2016-03-11 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 18:11:21 +00:00
|
|
|
/**
|
2016-08-25 16:42:47 +00:00
|
|
|
* Simple set content.
|
2016-11-22 11:34:14 +00:00
|
|
|
*
|
2016-03-14 18:11:21 +00:00
|
|
|
* @param string $content
|
|
|
|
*/
|
2016-03-11 21:48:16 +00:00
|
|
|
public function set_content( $content ) {
|
2016-11-23 01:47:13 +00:00
|
|
|
$this->set_prop( 'content', $content );
|
2016-08-25 16:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple get bool value.
|
2016-11-22 11:34:14 +00:00
|
|
|
*
|
|
|
|
* @param string $context
|
2016-08-25 16:42:47 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-11-22 11:34:14 +00:00
|
|
|
public function get_bool_value( $context = 'view' ) {
|
|
|
|
return $this->get_prop( 'bool_value', $context );
|
2016-08-25 16:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple set bool value.
|
2016-11-22 11:34:14 +00:00
|
|
|
*
|
2016-08-25 16:42:47 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function set_bool_value( $value ) {
|
|
|
|
if ( ! is_bool( $value ) ) {
|
|
|
|
$this->error( 'invalid_bool_value', 'O noes' );
|
|
|
|
}
|
2016-11-22 11:34:14 +00:00
|
|
|
$this->set_prop( 'bool_value', $value );
|
2016-03-11 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 18:11:21 +00:00
|
|
|
/**
|
|
|
|
* Simple get data as array.
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-03-11 21:48:16 +00:00
|
|
|
public function get_data() {
|
|
|
|
return array_merge(
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->data,
|
2016-03-11 21:48:16 +00:00
|
|
|
array(
|
|
|
|
'meta_data' => $this->get_meta_data(),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-03-08 19:51:38 +00:00
|
|
|
/**
|
|
|
|
* Set the data to any arbitrary data.
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
public function set_data( $data ) {
|
|
|
|
$this->data = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the changes to any arbitrary changes.
|
|
|
|
* @param array $changes
|
|
|
|
*/
|
|
|
|
public function set_changes( $changes ) {
|
|
|
|
$this->changes = $changes;
|
|
|
|
}
|
|
|
|
|
2016-03-14 18:11:21 +00:00
|
|
|
/**
|
|
|
|
* Simple save.
|
|
|
|
*/
|
2016-03-11 21:48:16 +00:00
|
|
|
public function save() {
|
2016-11-22 11:34:14 +00:00
|
|
|
if ( $this->data_store ) {
|
|
|
|
if ( $this->get_id() ) {
|
|
|
|
$this->data_store->update( $this );
|
|
|
|
} else {
|
|
|
|
$this->data_store->create( $this );
|
|
|
|
}
|
2016-03-11 21:48:16 +00:00
|
|
|
}
|
|
|
|
$this->save_meta_data();
|
2016-11-22 11:34:14 +00:00
|
|
|
return $this->get_id();
|
2016-03-11 21:48:16 +00:00
|
|
|
}
|
|
|
|
}
|