woocommerce/tests/unit-tests/order-items/order-item-meta.php

100 lines
2.7 KiB
PHP
Raw Normal View History

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() {
add_filter( 'deprecated_function_trigger_error', '__return_false' );
}
/**
* 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(
'key' => $metadata->key,
'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 );
// Clean up.
$item->delete( true );
}
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() {
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"><p>1</p>', $not_flat );
$this->assertContains( 'class="variation-category">category:', $not_flat );
$this->assertContains( 'class="variation-category"><p>Testing Categories</p>', $not_flat );
// Clean up.
$item->delete( true );
}
2017-05-25 21:43:36 +00:00
}