diff --git a/includes/wc-template-functions.php b/includes/wc-template-functions.php index f74a8f7c129..52271c6f287 100644 --- a/includes/wc-template-functions.php +++ b/includes/wc-template-functions.php @@ -2776,7 +2776,7 @@ if ( ! function_exists( 'wc_dropdown_variation_attribute_options' ) ) { // Get selected value. if ( false === $args['selected'] && $args['attribute'] && $args['product'] instanceof WC_Product ) { $selected_key = 'attribute_' . sanitize_title( $args['attribute'] ); - $args['selected'] = isset( $_REQUEST[ $selected_key ] ) ? wc_clean( urldecode( wp_unslash( $_REQUEST[ $selected_key ] ) ) ) : $args['product']->get_variation_default_attribute( $args['attribute'] ); // WPCS: input var ok, CSRF ok, sanitization ok. + $args['selected'] = isset( $_REQUEST[ $selected_key ] ) ? wc_clean( wp_unslash( $_REQUEST[ $selected_key ] ) ) : $args['product']->get_variation_default_attribute( $args['attribute'] ); // WPCS: input var ok, CSRF ok, sanitization ok. } $options = $args['options']; diff --git a/tests/unit-tests/templates/functions.php b/tests/unit-tests/templates/functions.php index 359d4213384..465f571e7b4 100644 --- a/tests/unit-tests/templates/functions.php +++ b/tests/unit-tests/templates/functions.php @@ -65,4 +65,39 @@ class WC_Tests_Template_Functions extends WC_Unit_Test_Case { $product->delete( true ); wp_delete_term( $category['term_id'], 'product_cat' ); } + + public function test_wc_dropdown_variation_attribute_options_no_attributes() { + $this->expectOutputString( '' ); + + wc_dropdown_variation_attribute_options(); + } + + public function test_wc_dropdown_variation_attribute_options_should_return_attributes_list() { + $product = WC_Helper_Product::create_variation_product(); + + $this->expectOutputString( '' ); + + wc_dropdown_variation_attribute_options( + array( + 'product' => $product, + 'attribute' => 'pa_size', + ) + ); + } + + public function test_wc_dropdown_variation_attribute_options_should_return_attributes_list_and_selected_element() { + $product = WC_Helper_Product::create_variation_product(); + $_REQUEST['attribute_pa_size'] = 'large'; + + $this->expectOutputString( '' ); + + wc_dropdown_variation_attribute_options( + array( + 'product' => $product, + 'attribute' => 'pa_size', + ) + ); + + unset( $_REQUEST['attribute_pa_size'] ); + } }