2016-03-11 21:48:16 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2020-03-12 15:57:32 +00:00
|
|
|
* Tests for the WC_Data class.
|
|
|
|
*
|
2016-03-11 21:48:16 +00:00
|
|
|
* @package WooCommerce\Tests\CRUD
|
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class WC_Tests_CRUD_Data
|
|
|
|
*/
|
2016-08-25 16:42:47 +00:00
|
|
|
class WC_Tests_CRUD_Data extends WC_Unit_Test_Case {
|
2016-03-11 21:48:16 +00:00
|
|
|
|
2017-03-23 00:11:21 +00:00
|
|
|
/**
|
2020-03-12 15:57:32 +00:00
|
|
|
* Restore UTC on failure.
|
2017-03-23 00:11:21 +00:00
|
|
|
*/
|
2019-05-01 22:05:00 +00:00
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
2017-03-23 00:11:21 +00:00
|
|
|
// @codingStandardsIgnoreStart
|
|
|
|
date_default_timezone_set( 'UTC' );
|
|
|
|
// @codingStandardsIgnoreEnd
|
|
|
|
update_option( 'gmt_offset', 0 );
|
|
|
|
update_option( 'timezone_string', '' );
|
|
|
|
}
|
|
|
|
|
2016-03-14 18:11:21 +00:00
|
|
|
/**
|
|
|
|
* Create a test post we can add/test meta against.
|
|
|
|
*/
|
2016-03-11 21:48:16 +00:00
|
|
|
public function create_test_post() {
|
2016-03-23 15:35:06 +00:00
|
|
|
$object = new WC_Mock_WC_Data();
|
2016-11-22 11:34:14 +00:00
|
|
|
$object->data_store->set_meta_type( 'post' );
|
|
|
|
$object->data_store->set_object_id_field( '' );
|
2016-03-11 21:48:16 +00:00
|
|
|
$object->set_content( 'testing' );
|
|
|
|
$object->save();
|
2016-03-14 18:11:21 +00:00
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a test user we can add/test meta against.
|
|
|
|
*/
|
|
|
|
public function create_test_user() {
|
2016-03-23 15:35:06 +00:00
|
|
|
$object = new WC_Mock_WC_Data();
|
2016-11-22 11:34:14 +00:00
|
|
|
$object->data_store->set_meta_type( 'user' );
|
|
|
|
$object->data_store->set_object_id_field( 'user_id' );
|
2016-03-14 18:11:21 +00:00
|
|
|
$object->set_content( 'testing@woo.dev' );
|
|
|
|
$object->save();
|
2016-03-11 21:48:16 +00:00
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
2016-08-25 16:42:47 +00:00
|
|
|
/**
|
2020-03-12 15:57:32 +00:00
|
|
|
* Test: get_data.
|
2016-08-25 16:42:47 +00:00
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_get_data() {
|
2016-08-25 16:42:47 +00:00
|
|
|
$object = new WC_Mock_WC_Data();
|
|
|
|
$this->assertInternalType( 'array', $object->get_data() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-12 15:57:32 +00:00
|
|
|
* Test: delete_meta_data_by_mid.
|
2016-08-25 16:42:47 +00:00
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_delete_meta_data_by_mid() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = $this->create_test_post();
|
2016-08-25 16:42:47 +00:00
|
|
|
$object_id = $object->get_id();
|
2019-05-01 22:05:00 +00:00
|
|
|
$meta_id = add_metadata( 'post', $object_id, 'test_meta_key', 'val1', true );
|
2016-08-25 16:42:47 +00:00
|
|
|
$object->delete_meta_data_by_mid( $meta_id );
|
|
|
|
$this->assertEmpty( $object->get_meta( 'test_meta_key' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-12 15:57:32 +00:00
|
|
|
* Test: set_props.
|
2016-08-25 16:42:47 +00:00
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_set_props() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = new WC_Mock_WC_Data();
|
2016-08-25 16:42:47 +00:00
|
|
|
$data_to_set = array(
|
|
|
|
'content' => 'I am a fish',
|
2016-08-27 01:46:45 +00:00
|
|
|
'bool_value' => true,
|
2016-08-25 16:42:47 +00:00
|
|
|
);
|
2019-05-01 22:05:00 +00:00
|
|
|
$result = $object->set_props( $data_to_set );
|
2016-08-25 16:42:47 +00:00
|
|
|
$this->assertFalse( is_wp_error( $result ) );
|
|
|
|
$this->assertEquals( 'I am a fish', $object->get_content() );
|
2016-12-20 22:33:35 +00:00
|
|
|
$this->assertTrue( $object->get_bool_value() );
|
2016-08-25 16:42:47 +00:00
|
|
|
|
|
|
|
$data_to_set = array(
|
|
|
|
'content' => 'I am also a fish',
|
2016-08-27 01:46:45 +00:00
|
|
|
'bool_value' => 'thisisinvalid',
|
2016-08-25 16:42:47 +00:00
|
|
|
);
|
2019-05-01 22:05:00 +00:00
|
|
|
$result = $object->set_props( $data_to_set );
|
2016-08-25 16:42:47 +00:00
|
|
|
$this->assertTrue( is_wp_error( $result ) );
|
|
|
|
$this->assertEquals( 'I am also a fish', $object->get_content() );
|
|
|
|
$this->assertNotEquals( 'thisisinvalid', $object->get_bool_value() );
|
|
|
|
}
|
|
|
|
|
2016-03-11 21:48:16 +00:00
|
|
|
/**
|
|
|
|
* Tests reading and getting set metadata.
|
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_get_meta_data() {
|
2016-03-11 21:48:16 +00:00
|
|
|
$object = $this->create_test_post();
|
2016-03-14 18:11:21 +00:00
|
|
|
$object_id = $object->get_id();
|
2016-11-22 11:34:14 +00:00
|
|
|
$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();
|
2016-03-14 18:11:21 +00:00
|
|
|
|
2016-03-11 21:48:16 +00:00
|
|
|
$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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:51:52 +00:00
|
|
|
/**
|
|
|
|
* Tests that the meta data cache is not shared among instances.
|
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_get_meta_data_shared_bug() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = new WC_Order();
|
2017-04-26 17:51:52 +00:00
|
|
|
$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();
|
|
|
|
|
2019-05-01 22:05:00 +00:00
|
|
|
$order = new WC_Order( $object->get_id() );
|
|
|
|
$metas = $order->get_meta_data();
|
2017-04-26 17:51:52 +00:00
|
|
|
$metas[0]->value = 'wrong value';
|
|
|
|
|
|
|
|
$order = new WC_Order( $object->get_id() );
|
|
|
|
$metas = $order->get_meta_data();
|
|
|
|
$this->assertNotEquals( 'wrong value', $metas[0]->value );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-12 15:57:32 +00:00
|
|
|
* Tests the cache invalidation after an order is saved.
|
2017-04-26 17:51:52 +00:00
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_get_meta_data_cache_invalidation() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = new WC_Order();
|
2017-06-01 16:03:13 +00:00
|
|
|
$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();
|
|
|
|
|
2019-05-01 22:05:00 +00:00
|
|
|
$order = new WC_Order( $object->get_id() );
|
|
|
|
$metas = $order->get_meta_data();
|
2017-06-01 16:03:13 +00:00
|
|
|
$metas[0]->value = 'updated value';
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
$order = new WC_Order( $object->get_id() );
|
|
|
|
$metas = $order->get_meta_data();
|
|
|
|
$this->assertEquals( 'updated value', $metas[0]->value );
|
|
|
|
}
|
|
|
|
|
2020-03-12 15:57:32 +00:00
|
|
|
/**
|
|
|
|
* Ensure get_meta_data() can overwrite array meta values with scalar values.
|
|
|
|
*/
|
|
|
|
public function test_get_meta_data_cache_invalidation_array_to_scalar() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = new WC_Order();
|
2017-05-23 23:15:13 +00:00
|
|
|
$object->add_meta_data( 'test_meta_key', array( 'val1' ), true );
|
2017-04-26 17:51:52 +00:00
|
|
|
$object->add_meta_data( 'test_multi_meta_key', 'val2' );
|
|
|
|
$object->add_meta_data( 'test_multi_meta_key', 'val3' );
|
|
|
|
$object->save();
|
|
|
|
|
2019-05-01 22:05:00 +00:00
|
|
|
$order = new WC_Order( $object->get_id() );
|
|
|
|
$metas = $order->get_meta_data();
|
2017-04-26 17:51:52 +00:00
|
|
|
$metas[0]->value = 'updated value';
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
$order = new WC_Order( $object->get_id() );
|
|
|
|
$metas = $order->get_meta_data();
|
|
|
|
$this->assertEquals( 'updated value', $metas[0]->value );
|
|
|
|
}
|
|
|
|
|
2016-03-11 21:48:16 +00:00
|
|
|
/**
|
|
|
|
* Test getting meta by ID.
|
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_get_meta() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = $this->create_test_post();
|
2016-03-14 18:11:21 +00:00
|
|
|
$object_id = $object->get_id();
|
2016-11-22 11:34:14 +00:00
|
|
|
$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 );
|
2016-03-11 21:48:16 +00:00
|
|
|
|
2020-03-12 15:57:32 +00:00
|
|
|
// test single meta key.
|
2016-03-11 21:48:16 +00:00
|
|
|
$single_meta = $object->get_meta( 'test_meta_key' );
|
|
|
|
$this->assertEquals( 'val1', $single_meta );
|
|
|
|
|
2020-03-12 15:57:32 +00:00
|
|
|
// test getting multiple.
|
2016-03-11 21:48:16 +00:00
|
|
|
$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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 12:08:39 +00:00
|
|
|
/**
|
|
|
|
* Test seeing if meta exists.
|
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_has_meta() {
|
2017-03-14 12:08:39 +00:00
|
|
|
$object = $this->create_test_post();
|
|
|
|
$object_id = $object->get_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 );
|
|
|
|
|
2017-03-14 16:49:13 +00:00
|
|
|
$this->assertTrue( $object->meta_exists( 'test_meta_key' ) );
|
|
|
|
$this->assertTrue( $object->meta_exists( 'test_multi_meta_key' ) );
|
|
|
|
$this->assertFalse( $object->meta_exists( 'thiskeyisnothere' ) );
|
2017-03-14 12:08:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 19:16:49 +00:00
|
|
|
/**
|
2020-03-12 15:57:32 +00:00
|
|
|
* Test getting meta that hasn't been set.
|
2017-03-02 19:16:49 +00:00
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_get_meta_no_meta() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = $this->create_test_post();
|
2017-03-02 19:16:49 +00:00
|
|
|
$object_id = $object->get_id();
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = new WC_Mock_WC_Data( $object_id );
|
2017-03-02 19:16:49 +00:00
|
|
|
|
2019-05-01 22:05:00 +00:00
|
|
|
$single_on = $object->get_meta( 'doesnt-exist', true );
|
2017-03-02 19:16:49 +00:00
|
|
|
$single_off = $object->get_meta( 'also-doesnt-exist', false );
|
|
|
|
|
|
|
|
$this->assertEquals( '', $single_on );
|
|
|
|
$this->assertEquals( array(), $single_off );
|
|
|
|
}
|
|
|
|
|
2016-03-11 21:48:16 +00:00
|
|
|
/**
|
|
|
|
* Test setting meta.
|
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_set_meta_data() {
|
2016-03-14 18:11:21 +00:00
|
|
|
global $wpdb;
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = $this->create_test_post();
|
2016-03-14 18:11:21 +00:00
|
|
|
$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 );
|
2016-11-22 11:34:14 +00:00
|
|
|
$object = new WC_Mock_WC_Data( $object_id );
|
2016-03-14 18:11:21 +00:00
|
|
|
|
|
|
|
$metadata = array();
|
2019-05-01 22:05:00 +00:00
|
|
|
$raw_metadata = $wpdb->get_results(
|
|
|
|
$wpdb->prepare(
|
|
|
|
"
|
2016-03-14 18:11:21 +00:00
|
|
|
SELECT meta_id, meta_key, meta_value
|
|
|
|
FROM {$wpdb->prefix}postmeta
|
|
|
|
WHERE post_id = %d ORDER BY meta_id
|
2019-05-01 22:05:00 +00:00
|
|
|
",
|
|
|
|
$object_id
|
|
|
|
)
|
|
|
|
);
|
2016-03-14 18:11:21 +00:00
|
|
|
|
|
|
|
foreach ( $raw_metadata as $meta ) {
|
2016-03-18 19:18:41 +00:00
|
|
|
$metadata[] = (object) array(
|
2016-08-22 19:58:34 +00:00
|
|
|
'id' => $meta->meta_id,
|
2016-08-22 18:37:34 +00:00
|
|
|
'key' => $meta->meta_key,
|
|
|
|
'value' => $meta->meta_value,
|
2016-03-18 19:18:41 +00:00
|
|
|
);
|
2016-03-14 18:11:21 +00:00
|
|
|
}
|
2016-03-11 21:48:16 +00:00
|
|
|
|
2016-03-23 15:35:06 +00:00
|
|
|
$object = new WC_Mock_WC_Data();
|
2016-03-14 18:11:21 +00:00
|
|
|
$object->set_meta_data( $metadata );
|
|
|
|
|
2017-04-27 13:28:19 +00:00
|
|
|
foreach ( $object->get_meta_data() as $id => $meta ) {
|
2017-05-31 13:40:29 +00:00
|
|
|
$this->assertEquals( $metadata[ $id ]->id, $meta->id );
|
|
|
|
$this->assertEquals( $metadata[ $id ]->key, $meta->key );
|
|
|
|
$this->assertEquals( $metadata[ $id ]->value, $meta->value );
|
2017-04-27 13:28:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test adding meta data.
|
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_add_meta_data() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = $this->create_test_post();
|
2016-03-14 18:11:21 +00:00
|
|
|
$object_id = $object->get_id();
|
2019-05-01 22:05:00 +00:00
|
|
|
$data = 'add_meta_data_' . time();
|
2016-03-14 18:11:21 +00:00
|
|
|
$object->add_meta_data( 'test_new_field', $data );
|
|
|
|
$meta = $object->get_meta( 'test_new_field' );
|
|
|
|
$this->assertEquals( $data, $meta );
|
2016-03-11 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test updating meta data.
|
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_update_meta_data() {
|
2016-03-14 18:11:21 +00:00
|
|
|
global $wpdb;
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = $this->create_test_post();
|
2016-03-14 18:11:21 +00:00
|
|
|
$object_id = $object->get_id();
|
|
|
|
add_metadata( 'post', $object_id, 'test_meta_key', 'val1', true );
|
2016-11-22 11:34:14 +00:00
|
|
|
$object = new WC_Mock_WC_Data( $object_id );
|
2016-03-11 21:48:16 +00:00
|
|
|
|
2016-03-14 18:11:21 +00:00
|
|
|
$this->assertEquals( 'val1', $object->get_meta( 'test_meta_key' ) );
|
2016-03-11 21:48:16 +00:00
|
|
|
|
2019-05-01 22:05:00 +00:00
|
|
|
$metadata = array();
|
|
|
|
$meta_id = $wpdb->get_var(
|
|
|
|
$wpdb->prepare(
|
|
|
|
"
|
2016-03-14 18:11:21 +00:00
|
|
|
SELECT meta_id
|
|
|
|
FROM {$wpdb->prefix}postmeta
|
|
|
|
WHERE post_id = %d LIMIT 1
|
2019-05-01 22:05:00 +00:00
|
|
|
",
|
|
|
|
$object_id
|
|
|
|
)
|
|
|
|
);
|
2016-03-11 21:48:16 +00:00
|
|
|
|
2016-03-14 18:11:21 +00:00
|
|
|
$object->update_meta_data( 'test_meta_key', 'updated_value', $meta_id );
|
|
|
|
$this->assertEquals( 'updated_value', $object->get_meta( 'test_meta_key' ) );
|
2016-03-11 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-14 18:11:21 +00:00
|
|
|
* Test deleting meta.
|
2016-03-11 21:48:16 +00:00
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_delete_meta_data() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = $this->create_test_post();
|
2016-03-14 18:11:21 +00:00
|
|
|
$object_id = $object->get_id();
|
|
|
|
add_metadata( 'post', $object_id, 'test_meta_key', 'val1', true );
|
2016-11-22 11:34:14 +00:00
|
|
|
$object = new WC_Mock_WC_Data( $object_id );
|
2016-03-14 18:11:21 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 'val1', $object->get_meta( 'test_meta_key' ) );
|
2016-03-11 21:48:16 +00:00
|
|
|
|
2016-03-14 18:11:21 +00:00
|
|
|
$object->delete_meta_data( 'test_meta_key' );
|
|
|
|
|
|
|
|
$this->assertEmpty( $object->get_meta( 'test_meta_key' ) );
|
2016-03-11 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 18:11:21 +00:00
|
|
|
|
2016-03-11 21:48:16 +00:00
|
|
|
/**
|
2020-03-12 15:57:32 +00:00
|
|
|
* Test saving metadata (Actually making sure changes are written to DB).
|
2016-03-11 21:48:16 +00:00
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_save_meta_data() {
|
2016-03-14 18:11:21 +00:00
|
|
|
global $wpdb;
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = $this->create_test_post();
|
2016-03-14 18:11:21 +00:00
|
|
|
$object_id = $object->get_id();
|
2016-11-22 11:34:14 +00:00
|
|
|
$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 );
|
2016-03-11 21:48:16 +00:00
|
|
|
|
2019-05-01 22:05:00 +00:00
|
|
|
$raw_metadata = $wpdb->get_results(
|
|
|
|
$wpdb->prepare(
|
|
|
|
"
|
2016-03-14 18:11:21 +00:00
|
|
|
SELECT meta_id, meta_key, meta_value
|
|
|
|
FROM {$wpdb->prefix}postmeta
|
|
|
|
WHERE post_id = %d ORDER BY meta_id
|
2019-05-01 22:05:00 +00:00
|
|
|
",
|
|
|
|
$object_id
|
|
|
|
)
|
|
|
|
);
|
2016-03-14 18:11:21 +00:00
|
|
|
|
|
|
|
$object->delete_meta_data( 'test_meta_key' );
|
|
|
|
$object->update_meta_data( 'test_meta_key_2', 'updated_value', $raw_metadata[1]->meta_id );
|
|
|
|
|
|
|
|
$object->save();
|
2020-03-12 15:57:32 +00:00
|
|
|
$object = new WC_Mock_WC_Data( $object_id ); // rereads from the DB.
|
2016-03-14 18:11:21 +00:00
|
|
|
|
|
|
|
$this->assertEmpty( $object->get_meta( 'test_meta_key' ) );
|
|
|
|
$this->assertEquals( 'updated_value', $object->get_meta( 'test_meta_key_2' ) );
|
2016-03-11 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-14 18:11:21 +00:00
|
|
|
* Test reading/getting user meta data too.
|
2016-03-11 21:48:16 +00:00
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_usermeta() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = $this->create_test_user();
|
2016-03-14 18:11:21 +00:00
|
|
|
$object_id = $object->get_id();
|
2016-11-22 11:34:14 +00:00
|
|
|
$object->add_meta_data( 'test_meta_key', 'val1', true );
|
|
|
|
$object->add_meta_data( 'test_meta_key_2', 'val2', true );
|
|
|
|
$object->save_meta_data();
|
2016-03-11 21:48:16 +00:00
|
|
|
|
2016-03-14 18:11:21 +00:00
|
|
|
$this->assertEquals( 'val1', $object->get_meta( 'test_meta_key' ) );
|
|
|
|
$this->assertEquals( 'val2', $object->get_meta( 'test_meta_key_2' ) );
|
2016-03-11 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 19:18:41 +00:00
|
|
|
/**
|
|
|
|
* Test adding meta data/updating meta data just added without keys colliding when changing
|
|
|
|
* data before a save.
|
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_add_meta_data_overwrite_before_save() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = new WC_Mock_WC_Data();
|
2016-03-18 19:18:41 +00:00
|
|
|
$object->add_meta_data( 'test_field_0', 'another field', true );
|
|
|
|
$object->add_meta_data( 'test_field_1', 'another field', true );
|
|
|
|
$object->add_meta_data( 'test_field_2', 'val1', true );
|
|
|
|
$object->update_meta_data( 'test_field_0', 'another field 2' );
|
|
|
|
$this->assertEquals( 'val1', $object->get_meta( 'test_field_2' ) );
|
|
|
|
}
|
2017-03-08 16:52:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test protected method set_date_prop by testing a order date setter.
|
2020-03-12 15:57:32 +00:00
|
|
|
*
|
|
|
|
* @param string $timezone The default timezone to operate under.
|
2017-03-08 16:52:21 +00:00
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function set_date_prop_gmt_offset( $timezone = 'UTC' ) {
|
2017-03-23 00:11:21 +00:00
|
|
|
// @codingStandardsIgnoreStart
|
|
|
|
date_default_timezone_set( $timezone );
|
|
|
|
|
2017-03-08 16:52:21 +00:00
|
|
|
$object = new WC_Order();
|
|
|
|
|
|
|
|
// Change timezone in WP.
|
|
|
|
update_option( 'gmt_offset', -4 );
|
|
|
|
|
|
|
|
// Set date to a UTC timestamp and expect a valid UTC timestamp back.
|
|
|
|
$object->set_date_created( 1488979186 );
|
|
|
|
$this->assertEquals( 1488979186, $object->get_date_created()->getTimestamp() );
|
|
|
|
|
|
|
|
// Set date to a string without timezone info. This will be assumed in local timezone and thus should match the offset timestamp.
|
|
|
|
$object->set_date_created( '2017-01-02' );
|
2017-03-23 00:11:21 +00:00
|
|
|
$this->assertEquals( -14400, $object->get_date_created()->getOffset() );
|
|
|
|
$this->assertEquals( '2017-01-02 00:00:00', $object->get_date_created()->date( 'Y-m-d H:i:s' ) );
|
2017-03-08 16:52:21 +00:00
|
|
|
$this->assertEquals( 1483315200 - $object->get_date_created()->getOffset(), $object->get_date_created()->getTimestamp() );
|
|
|
|
$this->assertEquals( 1483315200, $object->get_date_created()->getOffsetTimestamp() );
|
|
|
|
|
|
|
|
// Date time with no timezone.
|
|
|
|
$object->set_date_created( '2017-01-02T00:00' );
|
|
|
|
$this->assertEquals( 1483315200 - $object->get_date_created()->getOffset(), $object->get_date_created()->getTimestamp() );
|
|
|
|
$this->assertEquals( 1483315200, $object->get_date_created()->getOffsetTimestamp() );
|
2017-03-09 11:05:54 +00:00
|
|
|
$this->assertEquals( '2017-01-02 00:00:00', $object->get_date_created()->date( 'Y-m-d H:i:s' ) );
|
2017-03-08 16:52:21 +00:00
|
|
|
|
|
|
|
// ISO 8601 date time with offset.
|
|
|
|
$object->set_date_created( '2017-01-01T20:00:00-04:00' );
|
|
|
|
$this->assertEquals( 1483315200, $object->get_date_created()->getTimestamp() );
|
2017-03-09 11:05:54 +00:00
|
|
|
$this->assertEquals( '2017-01-01 20:00:00', $object->get_date_created()->date( 'Y-m-d H:i:s' ) );
|
2017-03-08 16:52:21 +00:00
|
|
|
|
|
|
|
// ISO 8601 date time different offset to site timezone.
|
|
|
|
$object->set_date_created( '2017-01-01T16:00:00-08:00' );
|
|
|
|
$this->assertEquals( 1483315200, $object->get_date_created()->getTimestamp() );
|
2017-03-09 11:05:54 +00:00
|
|
|
$this->assertEquals( '2017-01-01 20:00:00', $object->get_date_created()->date( 'Y-m-d H:i:s' ) );
|
2017-03-08 16:52:21 +00:00
|
|
|
|
|
|
|
// ISO 8601 date time in UTC.
|
|
|
|
$object->set_date_created( '2017-01-02T00:00:00+00:00' );
|
|
|
|
$this->assertEquals( 1483315200, $object->get_date_created()->getTimestamp() );
|
2017-03-09 11:05:54 +00:00
|
|
|
$this->assertEquals( '2017-01-01 20:00:00', $object->get_date_created()->date( 'Y-m-d H:i:s' ) );
|
2017-03-08 16:52:21 +00:00
|
|
|
|
|
|
|
// Restore default.
|
|
|
|
update_option( 'gmt_offset', 0 );
|
2017-03-23 00:11:21 +00:00
|
|
|
|
|
|
|
date_default_timezone_set( 'UTC' );
|
|
|
|
// @codingStandardsIgnoreEnd
|
2017-03-09 11:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test protected method set_date_prop by testing a order date setter.
|
2020-03-12 15:57:32 +00:00
|
|
|
*
|
|
|
|
* @param string $timezone The default timezone to operate under.
|
2017-03-09 11:05:54 +00:00
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function set_date_prop_timezone_string( $timezone = 'UTC' ) {
|
2017-03-23 00:11:21 +00:00
|
|
|
// @codingStandardsIgnoreStart
|
|
|
|
date_default_timezone_set( $timezone );
|
|
|
|
|
2017-03-09 11:05:54 +00:00
|
|
|
$object = new WC_Order();
|
2017-03-08 17:44:43 +00:00
|
|
|
|
|
|
|
// Repeat tests with timezone_string. America/New_York is -5 in the winter and -4 in summer.
|
|
|
|
update_option( 'timezone_string', 'America/New_York' );
|
|
|
|
|
|
|
|
// Set date to a UTC timestamp and expect a valid UTC timestamp back.
|
|
|
|
$object->set_date_created( 1488979186 );
|
|
|
|
$this->assertEquals( 1488979186, $object->get_date_created()->getTimestamp() );
|
|
|
|
|
|
|
|
// Set date to a string without timezone info. This will be assumed in local timezone and thus should match the offset timestamp.
|
|
|
|
$object->set_date_created( '2017-01-02' );
|
|
|
|
$this->assertEquals( 1483315200 - $object->get_date_created()->getOffset(), $object->get_date_created()->getTimestamp() );
|
|
|
|
$this->assertEquals( 1483315200, $object->get_date_created()->getOffsetTimestamp() );
|
2017-03-09 11:05:54 +00:00
|
|
|
$this->assertEquals( '2017-01-02 00:00:00', $object->get_date_created()->date( 'Y-m-d H:i:s' ) );
|
2017-03-08 17:44:43 +00:00
|
|
|
|
|
|
|
// Date time with no timezone.
|
|
|
|
$object->set_date_created( '2017-01-02T00:00' );
|
|
|
|
$this->assertEquals( 1483315200 - $object->get_date_created()->getOffset(), $object->get_date_created()->getTimestamp() );
|
|
|
|
$this->assertEquals( 1483315200, $object->get_date_created()->getOffsetTimestamp() );
|
2017-03-09 11:05:54 +00:00
|
|
|
$this->assertEquals( '2017-01-02 00:00:00', $object->get_date_created()->date( 'Y-m-d H:i:s' ) );
|
2017-03-08 17:44:43 +00:00
|
|
|
|
|
|
|
// ISO 8601 date time with offset.
|
|
|
|
$object->set_date_created( '2017-01-01T19:00:00-05:00' );
|
|
|
|
$this->assertEquals( 1483315200, $object->get_date_created()->getTimestamp() );
|
2017-03-09 11:05:54 +00:00
|
|
|
$this->assertEquals( '2017-01-01 19:00:00', $object->get_date_created()->date( 'Y-m-d H:i:s' ) );
|
2017-03-08 17:44:43 +00:00
|
|
|
|
|
|
|
// ISO 8601 date time different offset to site timezone.
|
|
|
|
$object->set_date_created( '2017-01-01T16:00:00-08:00' );
|
|
|
|
$this->assertEquals( 1483315200, $object->get_date_created()->getTimestamp() );
|
2017-03-09 11:05:54 +00:00
|
|
|
$this->assertEquals( '2017-01-01 19:00:00', $object->get_date_created()->date( 'Y-m-d H:i:s' ) );
|
2017-03-08 17:44:43 +00:00
|
|
|
|
|
|
|
// ISO 8601 date time in UTC.
|
|
|
|
$object->set_date_created( '2017-01-02T00:00:00+00:00' );
|
|
|
|
$this->assertEquals( 1483315200, $object->get_date_created()->getTimestamp() );
|
2017-03-09 11:05:54 +00:00
|
|
|
$this->assertEquals( '2017-01-01 19:00:00', $object->get_date_created()->date( 'Y-m-d H:i:s' ) );
|
2017-03-08 17:44:43 +00:00
|
|
|
|
|
|
|
// Restore default.
|
|
|
|
update_option( 'timezone_string', '' );
|
2017-03-23 00:11:21 +00:00
|
|
|
|
|
|
|
date_default_timezone_set( 'UTC' );
|
|
|
|
// @codingStandardsIgnoreEnd
|
2017-03-08 16:52:21 +00:00
|
|
|
}
|
2017-03-09 11:05:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test protected method set_date_prop by testing a order date setter.
|
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_set_date_prop_server_timezone() {
|
2017-03-23 00:11:21 +00:00
|
|
|
$this->set_date_prop_gmt_offset();
|
|
|
|
$this->set_date_prop_timezone_string();
|
2017-03-09 11:05:54 +00:00
|
|
|
|
|
|
|
// Repeat all tests with different server timezone.
|
2017-03-23 00:11:21 +00:00
|
|
|
$this->set_date_prop_gmt_offset( 'Pacific/Fiji' );
|
|
|
|
$this->set_date_prop_timezone_string( 'Pacific/Fiji' );
|
2017-03-09 11:05:54 +00:00
|
|
|
|
2017-03-23 00:11:21 +00:00
|
|
|
// Repeat all tests with different server timezone.
|
|
|
|
$this->set_date_prop_gmt_offset( 'Pacific/Tahiti' );
|
|
|
|
$this->set_date_prop_timezone_string( 'Pacific/Tahiti' );
|
2017-03-09 11:05:54 +00:00
|
|
|
}
|
2017-03-15 15:52:40 +00:00
|
|
|
|
2017-03-08 20:02:14 +00:00
|
|
|
/**
|
2020-03-12 15:57:32 +00:00
|
|
|
* Test applying changes.
|
2017-03-08 20:02:14 +00:00
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_apply_changes() {
|
2017-03-08 19:51:38 +00:00
|
|
|
$data = array(
|
|
|
|
'prop1' => 'value1',
|
|
|
|
'prop2' => 'value2',
|
|
|
|
);
|
|
|
|
|
|
|
|
$changes = array(
|
|
|
|
'prop1' => 'new_value1',
|
2017-03-21 15:38:35 +00:00
|
|
|
'prop3' => 'value3',
|
2017-03-08 19:51:38 +00:00
|
|
|
);
|
|
|
|
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = new WC_Mock_WC_Data();
|
2017-03-08 19:51:38 +00:00
|
|
|
$object->set_data( $data );
|
|
|
|
$object->set_changes( $changes );
|
|
|
|
$object->apply_changes();
|
|
|
|
|
2019-05-01 22:05:00 +00:00
|
|
|
$new_data = $object->get_data();
|
2017-03-08 19:51:38 +00:00
|
|
|
$new_changes = $object->get_changes();
|
|
|
|
|
|
|
|
$this->assertEquals( 'new_value1', $new_data['prop1'] );
|
|
|
|
$this->assertEquals( 'value2', $new_data['prop2'] );
|
|
|
|
$this->assertEquals( 'value3', $new_data['prop3'] );
|
|
|
|
$this->assertEmpty( $new_changes );
|
|
|
|
}
|
|
|
|
|
2017-03-08 20:02:14 +00:00
|
|
|
/**
|
2020-03-12 15:57:32 +00:00
|
|
|
* Test applying changes with a nested array.
|
2017-03-08 20:02:14 +00:00
|
|
|
*/
|
2020-03-12 15:57:32 +00:00
|
|
|
public function test_apply_changes_nested() {
|
2017-03-08 19:51:38 +00:00
|
|
|
$data = array(
|
|
|
|
'prop1' => 'value1',
|
|
|
|
'prop2' => array(
|
|
|
|
'subprop1' => 1,
|
|
|
|
'subprop2' => 2,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$changes = array(
|
|
|
|
'prop2' => array(
|
|
|
|
'subprop1' => 1000,
|
|
|
|
'subprop3' => 3,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2019-05-01 22:05:00 +00:00
|
|
|
$object = new WC_Mock_WC_Data();
|
2017-03-08 19:51:38 +00:00
|
|
|
$object->set_data( $data );
|
|
|
|
$object->set_changes( $changes );
|
|
|
|
$object->apply_changes();
|
|
|
|
|
|
|
|
$new_data = $object->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 'value1', $new_data['prop1'] );
|
|
|
|
$this->assertEquals( 1000, $new_data['prop2']['subprop1'] );
|
|
|
|
$this->assertEquals( 2, $new_data['prop2']['subprop2'] );
|
|
|
|
$this->assertEquals( 3, $new_data['prop2']['subprop3'] );
|
|
|
|
}
|
2016-03-11 21:48:16 +00:00
|
|
|
}
|