Test cases for sorting

This commit is contained in:
Mike Jolley 2019-04-24 10:38:38 +01:00
parent f20a48b1e4
commit 3945aae94e
1 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,103 @@
<?php
/**
* Taxonomy tests class.
*
* @package WooCommerce\Tests\Core
*/
/**
* WC_Test_Taxonomies class.
*/
class WC_Test_Taxonomies extends WC_Unit_Test_Case {
/**
* Setup test.
*/
public function setUp() {
parent::setUp();
$category_1 = wp_insert_term( 'Zulu Category', 'product_cat' );
$category_2 = wp_insert_term( 'Alpha Category', 'product_cat' );
$category_3 = wp_insert_term( 'Beta Category', 'product_cat' );
update_term_meta( $category_1['term_id'], 'order', 2 );
update_term_meta( $category_2['term_id'], 'order', 1 );
update_term_meta( $category_3['term_id'], 'order', 3 );
}
/**
* Test get_terms sorting.
*/
public function test_get_terms_orderby() {
$default_category_id = absint( get_option( 'default_product_cat', 0 ) );
// Default sort (menu_order).
$terms = array_values(
wp_list_pluck(
get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'exclude' => $default_category_id,
)
),
'name'
)
);
$this->assertEquals(
array(
'Alpha Category',
'Zulu Category',
'Beta Category',
),
$terms,
print_r( $terms, true ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
);
// Force sort by name.
$terms = array_values(
wp_list_pluck(
get_terms(
array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'hide_empty' => false,
'exclude' => $default_category_id,
)
),
'name'
)
);
$this->assertEquals(
array(
'Alpha Category',
'Beta Category',
'Zulu Category',
),
$terms,
print_r( $terms, true ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
);
// Force sort by ID.
$terms = array_values(
wp_list_pluck(
get_terms(
array(
'taxonomy' => 'product_cat',
'orderby' => 'term_id',
'hide_empty' => false,
'exclude' => $default_category_id,
)
),
'name'
)
);
$this->assertEquals(
array(
'Zulu Category',
'Alpha Category',
'Beta Category',
),
$terms,
print_r( $terms, true ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
);
}
}