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(); foreach ( $item->get_meta_data() as $metadata ) { $expected[ $metadata->id ] = array( 'key' => $metadata->key, 'label' => wc_attribute_label( $metadata->key, null ), 'value' => $metadata->value, ); } $result = $meta->get_formatted(); $this->assertEquals( 3, count( $result ) ); $this->assertEquals( $expected, $result ); // Clean up. $item->delete( true ); } /** * Test the get_formatted method of WC_Order_Item_Meta. * * @since 3.2.0 */ public function test_get_formatted() { wp_insert_term( 'Testing Categories', 'category', array( 'slug' => 'testing' ) ); $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 = array( 'regularkey' => '1', 'category' => 'Testing Categories' ); $actual = wp_list_pluck( $meta->get_formatted(), 'value', 'key' ); $this->assertEquals( $expected, $actual ); // Clean up. $item->delete( true ); } /** * 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"; $flat = $meta->display( true, true ); $this->assertEquals( $expected, $flat ); $not_flat = $meta->display( false, true ); $this->assertContains( 'class="variation-regularkey">regularkey:', $not_flat ); $this->assertContains( 'class="variation-regularkey">

1

', $not_flat ); $this->assertContains( 'class="variation-category">category:', $not_flat ); $this->assertContains( 'class="variation-category">

Testing Categories

', $not_flat ); // Clean up. $item->delete( true ); } }