Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#31135 closed defect (bug) (fixed)

register_taxonomy() should call a _doing_it_wrong() when called with an empty taxonomy name

Reported by: tyxla's profile tyxla Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 4.2 Priority: normal
Severity: minor Version: 3.0
Component: Taxonomy Keywords: has-patch commit
Focuses: Cc:

Description

If you misuse register_taxonomy() with an empty string as a first parameter, this will currently not throw any notice.

Anyway, it is definitely wrong and can lead to some very weird results both in the administration and in the frontend.

I suggest that a _doing_it_wrong() should be called to cover this case.

Attachments (5)

31135.patch (1.1 KB) - added by tyxla 10 years ago.
Adding a _doing_it_wrong() when register_taxonomy() is called with an empty taxonomy name.
31135.2.patch (1.8 KB) - added by tyxla 10 years ago.
Updating the first patch. Now using a single _doing_it_wrong() call for both taxonomy min and max, also adding unit tests for both cases.
31135.3.patch (1.4 KB) - added by tyxla 10 years ago.
Updating unit tests as per @DrewAPicture's suggestions.
31135.4.diff (1.4 KB) - added by MikeHansenMe 10 years ago.
uses empty instead of < 1
31135.5.diff (1.6 KB) - added by MikeHansenMe 10 years ago.
skip assigning length to variable since we only need it once

Download all attachments as: .zip

Change History (16)

@tyxla
10 years ago

Adding a _doing_it_wrong() when register_taxonomy() is called with an empty taxonomy name.

#1 @tyxla
10 years ago

  • Keywords has-patch added

#2 @DrewAPicture
10 years ago

  • Keywords needs-unit-tests added
  • Milestone changed from Awaiting Review to 4.2
  • Severity changed from normal to minor
  • Type changed from enhancement to defect (bug)

Hi @tyxla, thanks for the patch, that's a good catch. I think we can probably modify the existing _doing_it_wrong() call to account for both cases, maybe "Taxonomies must be between 1 and 32 characters in length." We also need a unit test covering the empty string case.

#3 @tyxla
10 years ago

@DrewAPicture totally agree, just like on #31134. Adding a patch in a minute.

@tyxla
10 years ago

Updating the first patch. Now using a single _doing_it_wrong() call for both taxonomy min and max, also adding unit tests for both cases.

#4 @tyxla
10 years ago

  • Keywords needs-unit-tests removed

#5 @DrewAPicture
10 years ago

Just a couple of things on 31135.2.patch:

  • Rather than tacking tests on to the main test_register_taxonomy() method, you should split them out to more specifically-named tests. For instance, the "> 32 characters" case is already covered by test_register_long_taxonomy() a few lines below your changes
  • Realistically, we're checking for the returned WP_Error object instead of a _doing_it_wrong() to confirm the test, see below for an example of this:
    <?php
    /**
     * @ticket 21593
     *
     * @expectedIncorrectUsage register_taxonomy
     */
    function test_register_long_taxonomy() {
            $this->assertInstanceOf( 'WP_Error', register_taxonomy( 'abcdefghijklmnopqrstuvwxyz0123456789', 'post', array() ) );
    }
    
  • So perhaps creating a test_register_short_taxonomy() block might be the way to go. I'd follow the same syntax as for long

#6 @tyxla
10 years ago

Thanks for making things more clear on how _doing_it_wrong() cases are tested. And I really missed the test_register_long_taxonomy() test.

Updating the patch with your suggestions right away.

@tyxla
10 years ago

Updating unit tests as per @DrewAPicture's suggestions.

#7 @DrewAPicture
10 years ago

  • Keywords commit added
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

Related: #28683

@MikeHansenMe
10 years ago

uses empty instead of < 1

@MikeHansenMe
10 years ago

skip assigning length to variable since we only need it once

#8 @mattheweppelsheimer
10 years ago

+1 to this ticket as well as #31134. #28683 didn't go far enough.

This ticket was mentioned in Slack in #core by drew. View the logs.


10 years ago

#10 @johnbillion
10 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 31449:

Return a WP_Error if an empty name is provided when registering a taxonomy.

Fixes #31135
Props tyxla, MikeHansenMe

#11 @SergeyBiryukov
10 years ago

In 31457:

Use more descriptive names for register_post_type() and register_taxonomy() tests with too long and too short names.

see #31134, #31135.

Note: See TracTickets for help on using tickets.