Make WordPress Core

Ticket #39078: 39078.4.diff

File 39078.4.diff, 11.3 KB (added by westonruter, 8 years ago)

Fix ajax unit tests: https://github.com/xwp/wordpress-develop/pull/211/commits/84b9898

  • src/wp-includes/class-wp-customize-manager.php

    diff --git src/wp-includes/class-wp-customize-manager.php src/wp-includes/class-wp-customize-manager.php
    index 440a537..085a204 100644
    final class WP_Customize_Manager { 
    10061006                                'posts_per_page' => -1,
    10071007                        ) );
    10081008                        foreach ( $existing_posts_query->posts as $existing_post ) {
    1009                                 $existing_starter_content_posts[ $existing_post->post_type . ':' . $existing_post->post_name ] = $existing_post;
     1009                                $post_name = $existing_post->post_name;
     1010                                if ( empty( $post_name ) ) {
     1011                                        $post_name = get_post_meta( $existing_post->ID, '_customize_draft_post_name', true );
     1012                                }
     1013                                $existing_starter_content_posts[ $existing_post->post_type . ':' . $post_name ] = $existing_post;
    10101014                        }
    10111015                }
    10121016
  • src/wp-includes/class-wp-customize-nav-menus.php

    diff --git src/wp-includes/class-wp-customize-nav-menus.php src/wp-includes/class-wp-customize-nav-menus.php
    index 1305a14..ca1b72f 100644
    final class WP_Customize_Nav_Menus { 
    803803                if ( empty( $postarr['post_name'] ) ) {
    804804                        $postarr['post_name'] = sanitize_title( $postarr['post_title'] );
    805805                }
     806                if ( ! isset( $postarr['meta_input'] ) ) {
     807                        $postarr['meta_input'] = array();
     808                }
     809                $postarr['meta_input']['_customize_draft_post_name'] = $postarr['post_name'];
     810                unset( $postarr['post_name'] );
    806811
    807812                add_filter( 'wp_insert_post_empty_content', '__return_false', 1000 );
    808813                $r = wp_insert_post( wp_slash( $postarr ), true );
    final class WP_Customize_Nav_Menus { 
    11921197                if ( ! empty( $post_ids ) ) {
    11931198                        foreach ( $post_ids as $post_id ) {
    11941199                                $target_status = 'attachment' === get_post_type( $post_id ) ? 'inherit' : 'publish';
     1200                                $args = array(
     1201                                        'ID' => $post_id,
     1202                                        'post_status' => $target_status,
     1203                                );
     1204                                $post_name = get_post_meta( $post_id, '_customize_draft_post_name', true );
     1205                                if ( $post_name ) {
     1206                                        $args['post_name'] = $post_name;
     1207                                }
    11951208
    11961209                                // Note that wp_publish_post() cannot be used because unique slugs need to be assigned.
    1197                                 wp_update_post( array( 'ID' => $post_id, 'post_status' => $target_status ) );
     1210                                wp_update_post( wp_slash( $args ) );
     1211
     1212                                delete_post_meta( $post_id, '_customize_draft_post_name' );
    11981213                        }
    11991214                }
    12001215        }
  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index 1496d6d..c81f5c5 100644
    function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p 
    36783678
    36793679        if ( 'attachment' == $post_type ) {
    36803680                // Attachment slugs must be unique across all types.
    3681                 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_status != 'auto-draft' AND post_name = %s AND ID != %d LIMIT 1";
     3681                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
    36823682                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) );
    36833683
    36843684                /**
    function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p 
    37063706                 * Page slugs must be unique within their own trees. Pages are in a separate
    37073707                 * namespace than posts so page slugs are allowed to overlap post slugs.
    37083708                 */
    3709                 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_status != 'auto-draft' AND post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1";
     3709                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1";
    37103710                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
    37113711
    37123712                /**
    function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p 
    37303730                }
    37313731        } else {
    37323732                // Post slugs must be unique across all posts.
    3733                 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_status != 'auto-draft' AND post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
     3733                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
    37343734                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );
    37353735
    37363736                // Prevent new post slugs that could result in URLs that conflict with date archives.
  • tests/phpunit/tests/ajax/CustomizeMenus.php

    diff --git tests/phpunit/tests/ajax/CustomizeMenus.php tests/phpunit/tests/ajax/CustomizeMenus.php
    index fef7831..7dc2bf1 100644
    class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase { 
    601601                $post = get_post( $response['data']['post_id'] );
    602602                $this->assertEquals( 'Hello World', $post->post_title );
    603603                $this->assertEquals( 'post', $post->post_type );
    604                 $this->assertEquals( 'hello-world', $post->post_name );
     604                $this->assertEquals( '', $post->post_name );
     605                $this->assertEquals( 'hello-world', get_post_meta( $post->ID, '_customize_draft_post_name', true ) );
    605606        }
    606607
    607608        /**
  • tests/phpunit/tests/customize/manager.php

    diff --git tests/phpunit/tests/customize/manager.php tests/phpunit/tests/customize/manager.php
    index 345e666..84eb3d4 100644
    class Tests_WP_Customize_Manager extends WP_UnitTestCase { 
    454454                        } else {
    455455                                $this->assertEquals( 'auto-draft', $post->post_status );
    456456                        }
    457                         $posts_by_name[ $post->post_name ] = $post->ID;
     457                        $post_name = $post->post_name;
     458                        if ( empty( $post_name ) ) {
     459                                $post_name = get_post_meta( $post->ID, '_customize_draft_post_name', true );
     460                        }
     461                        $posts_by_name[ $post_name ] = $post->ID;
    458462                }
    459463                $this->assertEquals( array( 'waffles', 'canola', 'home', 'about', 'blog', 'custom' ), array_keys( $posts_by_name ) );
    460464                $this->assertEquals( 'Custom', get_post( $posts_by_name['custom'] )->post_title );
  • tests/phpunit/tests/customize/nav-menus.php

    diff --git tests/phpunit/tests/customize/nav-menus.php tests/phpunit/tests/customize/nav-menus.php
    index f8b38a1..e68ab05 100644
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    549549                $r = $menus->insert_auto_draft_post( array( 'post_title' => 'Hello World', 'post_type' => 'post' ) );
    550550                $this->assertInstanceOf( 'WP_Post', $r );
    551551                $this->assertEquals( 'Hello World', $r->post_title );
    552                 $this->assertEquals( 'hello-world', $r->post_name );
     552                $this->assertEquals( '', $r->post_name );
     553                $this->assertEquals( 'hello-world', get_post_meta( $r->ID, '_customize_draft_post_name', true ) );
    553554                $this->assertEquals( 'post', $r->post_type );
    554555
    555556                $r = $menus->insert_auto_draft_post( array( 'post_title' => 'Hello World', 'post_type' => 'post', 'post_name' => 'greetings-world', 'post_content' => 'Hi World' ) );
    556557                $this->assertInstanceOf( 'WP_Post', $r );
    557558                $this->assertEquals( 'Hello World', $r->post_title );
    558559                $this->assertEquals( 'post', $r->post_type );
    559                 $this->assertEquals( 'greetings-world', $r->post_name );
     560                $this->assertEquals( '', $r->post_name );
     561                $this->assertEquals( 'greetings-world', get_post_meta( $r->ID, '_customize_draft_post_name', true ) );
    560562                $this->assertEquals( 'Hi World', $r->post_content );
    561563        }
    562564
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    739741                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
    740742                do_action( 'customize_register', $this->wp_customize );
    741743
    742                 $post_ids = $this->factory()->post->create_many( 3, array(
    743                         'post_status' => 'auto-draft',
    744                         'post_type' => 'post',
    745                         'post_name' => 'auto-draft',
    746                 ) );
     744                $post_ids = array();
     745                for ( $i = 0; $i < 3; $i += 1 ) {
     746                        $r = $menus->insert_auto_draft_post( array(
     747                                'post_title' => 'Auto Draft ' . $i,
     748                                'post_type' => 'post',
     749                                'post_name' => 'auto-draft-' . $i,
     750                        ) );
     751                        $this->assertInstanceOf( 'WP_Post', $r );
     752                        $post_ids[] = $r->ID;
     753                }
     754
    747755                $pre_published_post_id = $this->factory()->post->create( array( 'post_status' => 'publish' ) );
    748756
    749757                $setting_id = 'nav_menus_created_posts';
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    754762                $this->assertEquals( $post_ids, $setting->post_value() );
    755763                foreach ( $post_ids as $post_id ) {
    756764                        $this->assertEquals( 'auto-draft', get_post_status( $post_id ) );
     765                        $this->assertEmpty( get_post( $post_id )->post_name );
     766                        $this->assertNotEmpty( get_post_meta( $post_id, '_customize_draft_post_name', true ) );
    757767                }
    758768
    759769                $save_action_count = did_action( 'customize_save_nav_menus_created_posts' );
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    761771                $this->assertEquals( $save_action_count + 1, did_action( 'customize_save_nav_menus_created_posts' ) );
    762772                foreach ( $post_ids as $post_id ) {
    763773                        $this->assertEquals( 'publish', get_post_status( $post_id ) );
     774                        $this->assertRegExp( '/^auto-draft-\d+$/', get_post( $post_id )->post_name );
     775                        $this->assertEmpty( get_post_meta( $post_id, '_customize_draft_post_name', true ) );
    764776                }
    765777
    766778                // Ensure that unique slugs were assigned.
  • tests/phpunit/tests/post/wpUniquePostSlug.php

    diff --git tests/phpunit/tests/post/wpUniquePostSlug.php tests/phpunit/tests/post/wpUniquePostSlug.php
    index a728454..536017d 100644
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    347347                $found = wp_unique_post_slug( 'embed', $p, 'publish', 'attachment', 0 );
    348348                $this->assertSame( 'embed-2', $found );
    349349        }
    350 
    351         /**
    352          * @ticket 38928
    353          */
    354         public function test_non_unique_slugs_for_existing_auto_draft_posts() {
    355                 $auto_draft_post_id = self::factory()->post->create( array(
    356                         'post_type' => 'post',
    357                         'post_name' => 'existing-post',
    358                         'post_status' => 'auto-draft',
    359                 ) );
    360                 $auto_draft_page_id = self::factory()->post->create( array(
    361                         'post_type' => 'page',
    362                         'post_name' => 'existing-page',
    363                         'post_status' => 'auto-draft',
    364                 ) );
    365                 $auto_draft_attachment_id = self::factory()->attachment->create_object( 'image.jpg', $auto_draft_page_id, array(
    366                         'post_mime_type' => 'image/jpeg',
    367                         'post_type' => 'attachment',
    368                         'post_name' => 'existing-attachment',
    369                         'post_status' => 'auto-draft',
    370                 ) );
    371 
    372                 $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) );
    373                 $page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
    374                 $attachment_id = self::factory()->attachment->create_object( 'image2.jpg', $page_id, array(
    375                         'post_mime_type' => 'image/jpeg',
    376                         'post_type' => 'attachment',
    377                         'post_name' => 'existing-image',
    378                 ) );
    379 
    380                 $this->assertEquals( 'existing-post', wp_unique_post_slug( 'existing-post', $post_id, 'publish', get_post_type( $post_id ), 0 ) );
    381                 wp_publish_post( $auto_draft_post_id );
    382                 $this->assertEquals( 'existing-post-2', wp_unique_post_slug( 'existing-post', $post_id, 'publish', get_post_type( $post_id ), 0 ) );
    383 
    384                 $this->assertEquals( 'existing-page', wp_unique_post_slug( 'existing-page', $page_id, 'publish', get_post_type( $page_id ), 0 ) );
    385                 wp_publish_post( $auto_draft_page_id );
    386                 $this->assertEquals( 'existing-page-2', wp_unique_post_slug( 'existing-page', $page_id, 'publish', get_post_type( $page_id ), 0 ) );
    387 
    388                 $this->assertEquals( 'existing-attachment', wp_unique_post_slug( 'existing-attachment', $attachment_id, 'publish', get_post_type( $attachment_id ), 0 ) );
    389                 wp_publish_post( $auto_draft_attachment_id );
    390                 $this->assertEquals( 'existing-attachment-2', wp_unique_post_slug( 'existing-attachment', $attachment_id, 'publish', get_post_type( $attachment_id ), 0 ) );
    391         }
    392350}