2017-05-25 21:43:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Order Item Meta Tests.
|
|
|
|
* @package WooCommerce\Tests\Order_Items
|
|
|
|
* @since 3.0.8
|
|
|
|
*/
|
|
|
|
class WC_Tests_Order_Item_Meta extends WC_Unit_Test_Case {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Suppress deprecation notice from WC_Order_Item_Meta constructor.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
2018-04-20 18:48:00 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
wp_insert_term( 'Testing Categories', 'category', array( 'slug' => 'testing' ) );
|
|
|
|
$this->setExpectedDeprecated( 'WC_Order_Item_Meta::__construct' );
|
2017-05-25 21:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test retrieving meta values where some have the same key.
|
|
|
|
*/
|
|
|
|
public function test_multiple_meta_values() {
|
|
|
|
$item = new WC_Order_Item_Fee();
|
|
|
|
$item->add_meta_data( 'testkey', '1' );
|
|
|
|
$item->add_meta_data( 'testkey', '2' );
|
|
|
|
$item->add_meta_data( 'otherkey', 'val' );
|
|
|
|
$item->save();
|
|
|
|
|
|
|
|
$meta = new WC_Order_Item_Meta( $item );
|
|
|
|
|
|
|
|
$expected = array();
|
2017-07-06 18:12:02 +00:00
|
|
|
foreach ( $item->get_meta_data() as $metadata ) {
|
2017-05-25 21:43:36 +00:00
|
|
|
$expected[ $metadata->id ] = array(
|
2018-04-20 17:53:06 +00:00
|
|
|
'key' => $metadata->key,
|
2017-05-25 21:43:36 +00:00
|
|
|
'label' => wc_attribute_label( $metadata->key, null ),
|
2017-07-06 18:12:02 +00:00
|
|
|
'value' => $metadata->value,
|
2017-05-25 21:45:36 +00:00
|
|
|
);
|
2017-05-25 21:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$result = $meta->get_formatted();
|
|
|
|
|
|
|
|
$this->assertEquals( 3, count( $result ) );
|
|
|
|
$this->assertEquals( $expected, $result );
|
|
|
|
}
|
2017-09-28 18:40:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the get_formatted method of WC_Order_Item_Meta.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
public function test_get_formatted() {
|
|
|
|
$item = new WC_Order_Item_Fee();
|
|
|
|
$item->add_meta_data( 'regularkey', '1' );
|
|
|
|
$item->add_meta_data( 'category', 'testing' );
|
|
|
|
$item->add_meta_data( '_hiddenkey', '3' );
|
|
|
|
$item->save();
|
|
|
|
|
|
|
|
$meta = new WC_Order_Item_Meta( $item );
|
|
|
|
|
2018-04-20 17:53:06 +00:00
|
|
|
$expected = array(
|
|
|
|
'regularkey' => '1',
|
|
|
|
'category' => 'Testing Categories',
|
|
|
|
);
|
|
|
|
$actual = wp_list_pluck( $meta->get_formatted(), 'value', 'key' );
|
2017-09-28 18:40:33 +00:00
|
|
|
$this->assertEquals( $expected, $actual );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the display method of WC_Order_Item_Meta.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
public function test_display() {
|
|
|
|
$item = new WC_Order_Item_Fee();
|
|
|
|
$item->add_meta_data( 'regularkey', '1' );
|
|
|
|
$item->add_meta_data( 'category', 'testing' );
|
|
|
|
$item->add_meta_data( '_hiddenkey', '3' );
|
|
|
|
$item->save();
|
|
|
|
|
|
|
|
$meta = new WC_Order_Item_Meta( $item );
|
|
|
|
|
|
|
|
$expected = "regularkey: 1, \ncategory: Testing Categories";
|
2018-04-20 17:53:06 +00:00
|
|
|
$flat = $meta->display( true, true );
|
2017-09-28 18:40:33 +00:00
|
|
|
$this->assertEquals( $expected, $flat );
|
|
|
|
|
|
|
|
$not_flat = $meta->display( false, true );
|
|
|
|
$this->assertContains( 'class="variation-regularkey">regularkey:', $not_flat );
|
|
|
|
$this->assertContains( 'class="variation-regularkey"><p>1</p>', $not_flat );
|
|
|
|
$this->assertContains( 'class="variation-category">category:', $not_flat );
|
|
|
|
$this->assertContains( 'class="variation-category"><p>Testing Categories</p>', $not_flat );
|
|
|
|
}
|
2017-05-25 21:43:36 +00:00
|
|
|
}
|