Merge pull request #13514 from woocommerce/fix-13509

Apply_changes nested array fix and tests
This commit is contained in:
Mike Jolley 2017-03-09 17:32:19 +00:00 committed by GitHub
commit 99776e6374
4 changed files with 109 additions and 3 deletions

View File

@ -550,7 +550,7 @@ abstract class WC_Data {
* @since 2.7.0
*/
public function apply_changes() {
$this->data = array_merge( $this->data, $this->changes );
$this->data = array_replace_recursive( $this->data, $this->changes );
$this->changes = array();
}

View File

@ -193,6 +193,22 @@ class WC_Mock_WC_Data extends WC_Data {
);
}
/**
* 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;
}
/**
* Simple save.
*/

View File

@ -273,4 +273,64 @@ class WC_Tests_CRUD_Data extends WC_Unit_Test_Case {
$object->update_meta_data( 'test_field_0', 'another field 2' );
$this->assertEquals( 'val1', $object->get_meta( 'test_field_2' ) );
}
/**
* Test applying changes
*/
function test_apply_changes() {
$data = array(
'prop1' => 'value1',
'prop2' => 'value2',
);
$changes = array(
'prop1' => 'new_value1',
'prop3' => 'value3'
);
$object = new WC_Mock_WC_Data;
$object->set_data( $data );
$object->set_changes( $changes );
$object->apply_changes();
$new_data = $object->get_data();
$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 );
}
/**
* Test applying changes with a nested array
*/
function test_apply_changes_nested() {
$data = array(
'prop1' => 'value1',
'prop2' => array(
'subprop1' => 1,
'subprop2' => 2,
),
);
$changes = array(
'prop2' => array(
'subprop1' => 1000,
'subprop3' => 3,
),
);
$object = new WC_Mock_WC_Data;
$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'] );
}
}

View File

@ -814,7 +814,7 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
*/
function test_get_billing_state() {
$object = new WC_Order();
$set_to = 'Boulder';
$set_to = 'Oregon';
$object->set_billing_state( $set_to );
$this->assertEquals( $set_to, $object->get_billing_state() );
}
@ -864,6 +864,21 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
$this->assertEquals( $set_to, $object->get_billing_phone() );
}
/**
* Test: Setting/getting billing settings after an order is saved
*/
function test_set_billing_after_save() {
$object = new WC_Order();
$phone = '123456678';
$object->set_billing_phone( $phone );
$object->save();
$state = 'Oregon';
$object->set_billing_state( $state );
$this->assertEquals( $phone, $object->get_billing_phone() );
$this->assertEquals( $state, $object->get_billing_state() );
}
/**
* Test: get_shipping_first_name
*/
@ -929,7 +944,7 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
*/
function test_get_shipping_state() {
$object = new WC_Order();
$set_to = 'Boulder';
$set_to = 'Oregon';
$object->set_shipping_state( $set_to );
$this->assertEquals( $set_to, $object->get_shipping_state() );
}
@ -954,6 +969,21 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
$this->assertEquals( $set_to, $object->get_shipping_country() );
}
/**
* Test: Setting/getting shipping settings after an order is saved
*/
function test_set_shipping_after_save() {
$object = new WC_Order();
$country = 'US';
$object->set_shipping_country( $country );
$object->save();
$state = 'Oregon';
$object->set_shipping_state( $state );
$this->assertEquals( $country, $object->get_shipping_country() );
$this->assertEquals( $state, $object->get_shipping_state() );
}
/**
* Test: get_payment_method
*/