Change attribute slug length requirement and add wc_create_attribute tests (#31795)

* Allow for 28-character attribute slugs
* Add wc_create_attribute tests
This commit is contained in:
Sakri Koskimies 2022-06-09 11:51:38 +03:00 committed by GitHub
parent e3744709c7
commit e80312eef0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 4 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Change attribute slug length requirement and add wc_create_attribute tests

View File

@ -1929,7 +1929,7 @@ class WC_API_Products extends WC_API_Resource {
throw new WC_API_Exception( 'woocommerce_api_missing_product_attribute_name', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'name' ), 400 );
}
if ( strlen( $slug ) >= 28 ) {
if ( strlen( $slug ) > 28 ) {
throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_slug_too_long', sprintf( __( 'Slug "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce' ), $slug ), 400 );
} elseif ( wc_check_if_attribute_name_is_reserved( $slug ) ) {
throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_slug_reserved_name', sprintf( __( 'Slug "%s" is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), $slug ), 400 );

View File

@ -2482,7 +2482,7 @@ class WC_API_Products extends WC_API_Resource {
throw new WC_API_Exception( 'woocommerce_api_missing_product_attribute_name', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'name' ), 400 );
}
if ( strlen( $slug ) >= 28 ) {
if ( strlen( $slug ) > 28 ) {
throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_slug_too_long', sprintf( __( 'Slug "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce' ), $slug ), 400 );
} elseif ( wc_check_if_attribute_name_is_reserved( $slug ) ) {
throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_slug_reserved_name', sprintf( __( 'Slug "%s" is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), $slug ), 400 );

View File

@ -604,7 +604,7 @@ class WC_REST_Product_Attributes_V1_Controller extends WC_REST_Controller {
* @return bool|WP_Error
*/
protected function validate_attribute_slug( $slug, $new_data = true ) {
if ( strlen( $slug ) >= 28 ) {
if ( strlen( $slug ) > 28 ) {
/* translators: %s: slug being validated */
return new WP_Error( 'woocommerce_rest_invalid_product_attribute_slug_too_long', sprintf( __( 'Slug "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
} elseif ( wc_check_if_attribute_name_is_reserved( $slug ) ) {

View File

@ -483,7 +483,7 @@ function wc_create_attribute( $args ) {
}
// Validate slug.
if ( strlen( $slug ) >= 28 ) {
if ( strlen( $slug ) > 28 ) {
/* translators: %s: attribute slug */
return new WP_Error( 'invalid_product_attribute_slug_too_long', sprintf( __( 'Slug "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
} elseif ( wc_check_if_attribute_name_is_reserved( $slug ) ) {

View File

@ -114,6 +114,61 @@ class WC_Attribute_Functions_Test extends \WC_Unit_Test_Case {
);
}
/**
* Test wc_create_attribute() function.
*/
public function test_wc_create_attribute() {
$ids = array();
$ids[] = wc_create_attribute( array( 'name' => 'Brand' ) );
$this->assertInternalType(
'int',
end( $ids ),
'wc_create_attribute should return a numeric id on success.'
);
$ids[] = wc_create_attribute( array( 'name' => str_repeat( 'n', 28 ) ) );
$this->assertInternalType(
'int',
end( $ids ),
'Attribute creation should succeed when its slug is 28 characters long.'
);
$err = wc_create_attribute( array() );
$this->assertEquals(
'missing_attribute_name',
$err->get_error_code(),
'Attributes should not be allowed to be created without specifying a name.'
);
$err = wc_create_attribute( array( 'name' => str_repeat( 'n', 29 ) ) );
$this->assertEquals(
'invalid_product_attribute_slug_too_long',
$err->get_error_code(),
'Attribute slugs should not be allowed to be over 28 characters long.'
);
$err = wc_create_attribute( array( 'name' => 'Cat' ) );
$this->assertEquals(
'invalid_product_attribute_slug_reserved_name',
$err->get_error_code(),
'Attributes should not be allowed to be created with reserved names.'
);
register_taxonomy( 'pa_brand', array( 'product' ), array( 'labels' => array( 'name' => 'Brand' ) ) );
$err = wc_create_attribute( array( 'name' => 'Brand' ) );
$this->assertEquals(
'invalid_product_attribute_slug_already_exists',
$err->get_error_code(),
'Duplicate attribute slugs should not be allowed to exist.'
);
unregister_taxonomy( 'pa_brand' );
foreach ( $ids as $id ) {
wc_delete_attribute( $id );
}
}
public function get_attribute_names_and_slugs() {
return [
[ 'Dash Me', 'dash-me' ],