name_num tests

This commit is contained in:
Mike Jolley 2019-04-24 10:44:22 +01:00
parent 121f06b030
commit 8fe68400be
1 changed files with 85 additions and 14 deletions

View File

@ -9,25 +9,17 @@
* 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 ) );
$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 );
// Default sort (menu_order).
$terms = array_values(
@ -100,4 +92,83 @@ class WC_Test_Taxonomies extends WC_Unit_Test_Case {
print_r( $terms, true ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
);
}
/**
* Test get_terms sorting for name_num.
*/
public function test_get_terms_orderby_name_num() {
$default_category_id = absint( get_option( 'default_product_cat', 0 ) );
wp_insert_term( '1', 'product_cat' );
wp_insert_term( '2', 'product_cat' );
wp_insert_term( '3', 'product_cat' );
wp_insert_term( '4', 'product_cat' );
wp_insert_term( '5', 'product_cat' );
wp_insert_term( '10', 'product_cat' );
wp_insert_term( '9', 'product_cat' );
wp_insert_term( '8', 'product_cat' );
wp_insert_term( '7', 'product_cat' );
wp_insert_term( '6', 'product_cat' );
// by name.
$terms = array_values(
wp_list_pluck(
get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'orderby' => 'name',
'exclude' => $default_category_id,
)
),
'name'
)
);
$this->assertEquals(
array(
'1',
'10',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
),
$terms,
print_r( $terms, true ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
);
// by numeric name.
$terms = array_values(
wp_list_pluck(
get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'orderby' => 'name_num',
'exclude' => $default_category_id,
)
),
'name'
)
);
$this->assertEquals(
array(
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
),
$terms,
print_r( $terms, true ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
);
}
}