[wp-trac] [WordPress Trac] #43881: url_to_postid doesn't works with custom post type in PhpUnit Test

WordPress Trac noreply at wordpress.org
Fri Apr 27 14:10:38 UTC 2018


#43881: url_to_postid doesn't works with custom post type in PhpUnit Test
--------------------------+-----------------------------
 Reporter:  skarabeq      |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:
 Severity:  blocker       |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 In the {{{functions.php}}} I have the following code:

 {{{#!php
 <?php
 function post_type_products()
 {
     register_post_type(
         'products',
         [
             'labels' => [
                 'name'               => 'products',
                 'singular_name'      => 'products',
                 'add_new'            => 'Add New',
                 'add_new_item'       => 'Add New Product',
                 'edit_item'          => 'Edit Product',
                 'new_item'           => 'New Product',
                 'all_items'          => 'All Product',
                 'view_item'          => 'View Product',
                 'search_items'       => 'Search for Product',
                 'not_found'          => 'No product found',
                 'not_found_in_trash' => 'No product found in the Trash',
                 'menu_name'          => 'products',
                 'parent_item_colon'  => '',
             ],
             'description'   => 'Product description',
             'public'        => true,
             'rewrite'       => ['slug' => 'products'],
             'menu_position' => 5,
             'supports'      => ['title', 'editor', 'excerpt'],
             'has_archive'   => true,
             'menu_icon'     => 'dashicons-id-alt',
         ]
     );
 }

 add_action('init', 'post_type_products');
 }}}

 Then I need to test it. And for this test I create the test record via
 factory class:

 {{{#!php
 <?php
 $post_data = [
             'post_title'     => 'Test Post',
             'post_content'   => 'Test Post Content',
             'post_excerpt'   => 'Test Post excerpt',
             'post_type'      => 'products'
         ];

       $this->factory->post->create($post_data);
 }}}

 The problem is when I got URL from following code:
 {{{$url = get_permalink($this->factory->post->create($post_data));}}}
 The function {{{url_to_postid}}} return {{{0}}}

 {{{#!php
 <?php
 $id = url_to_postid($url) // this will return 0;
 }}}

 This is big problem because in my backend code I use the URL of the custom
 post type for determinate the ID of this URL. When it works outside of the
 context of the unit test it's work very well. And I can got ID from the
 URL from custom post type products. But when this is in the unit test
 context the function {{{url_to_postid}}} doesn't work as well.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/43881>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list