WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#31134 closed defect (bug) (fixed)

register_post_type() should call a _doing_it_wrong() when called with an empty post type name

Reported by: tyxla Owned by: SergeyBiryukov
Milestone: 4.2 Priority: normal
Severity: minor Version: 3.0
Component: Posts, Post Types Keywords: has-patch commit
Focuses: Cc:

Description

If you misuse register_post_type() 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)

31134.patch (1.1 KB) - added by tyxla 7 years ago.
Adding a _doing_it_wrong() when register_post_type() is called with an empty post type name.
31134.2.patch (1.8 KB) - added by tyxla 7 years ago.
Updating first patch. Now using a single _doing_it_wrong() call for both post type min and max, also adding unit tests for both cases.
31134.3.patch (1.6 KB) - added by tyxla 7 years ago.
Updating unit tests as per @DrewAPicture's suggestions
31134.4.diff (1.8 KB) - added by MikeHansenMe 7 years ago.
uses empty and splits unit tests
31134.5.diff (1.7 KB) - added by MikeHansenMe 7 years ago.
skip assigning length to variable since we only need it once

Download all attachments as: .zip

Change History (17)

@tyxla
7 years ago

Adding a _doing_it_wrong() when register_post_type() is called with an empty post type name.

#1 @tyxla
7 years ago

  • Keywords has-patch added

#2 @toscho
7 years ago

  • Keywords close added

Do we really have to catch every possible edge case?

Besides that, there is now too much code duplication, because you suggested the same thing for #31135. If we really do this, the code should be separated to a function like wp_str_min_max( $str, $min, $max, $encoding ) or something similar.

#3 @DrewAPicture
7 years ago

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

As I suggested on #31135, I think we can double up on the _doing_it_wrong() call by defining the minimum and maximum in the same condition and string. It would be also be helpful to have a unit test to cover the empty string case.

#4 follow-up: @tyxla
7 years ago

@DrewAPicture yeah, all makes sense, thanks. A single _doing_it_wrong() call would be better, and unit tests would be needed here. Let me attach a patch. Since there is no unit test for the maximum post type length, I'll add one for that too.

Last edited 7 years ago by tyxla (previous) (diff)

@tyxla
7 years ago

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

#5 @tyxla
7 years ago

  • Keywords needs-unit-tests removed

#6 in reply to: ↑ 4 @DrewAPicture
7 years ago

  • Keywords needs-unit-tests added

Replying to tyxla:

Since the issues between registering taxonomies and post types are fairly aligned, see comment:5:ticket:31135 for how I would approach the tests for this, except for post types instead of taxonomies. The difference of course would be that there isn't currently a test that I can find for either short or long post types.

So in this case, maybe creating a single test_register_post_type_length() would suffice, again, following similar syntax to that of test_register_long_taxonomy() – in terms of:

  • Notating the ticket number
  • Using @expectedIncorrectUsage
  • Checking for the WP_Error object to confirm instead of the _doing_it_wrong()

#7 @tyxla
7 years ago

  • Keywords needs-unit-tests removed

Thanks for your suggestions, they definitely make things better. Attaching an updated patch again.

@tyxla
7 years ago

Updating unit tests as per @DrewAPicture's suggestions

#8 @DrewAPicture
7 years ago

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

Related: #28683

@MikeHansenMe
7 years ago

uses empty and splits unit tests

@MikeHansenMe
7 years ago

skip assigning length to variable since we only need it once

#9 @mattheweppelsheimer
7 years ago

+1 to this ticket. Looks like #28683 didn't go far enough.

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


7 years ago

#11 @johnbillion
7 years ago

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

In 31450:

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

Fixes #31134
Props tyxla, MikeHansenMe

#12 @SergeyBiryukov
7 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.