Make WordPress Core


Ignore:
Timestamp:
09/16/2022 03:10:58 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Tests: Update the test for respecting the post type in get_block_templates().

  • Use assertSameSets() to avoid a failure when array items are returned in different order (example).
  • Move the test to a more appropriate place now that the function has its own test class.
  • Rename the test method to match the function name.

Follow-up to [52062], [53927], [54184].

See #55881.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/getBlockTemplates.php

    r53927 r54187  
    1313     */
    1414    private static $template;
     15
     16    /**
     17     * @var WP_Post
     18     */
     19    private static $custom_single_post_template;
    1520
    1621    /**
     
    4045        wp_set_post_terms( static::$template->ID, static::TEST_THEME, 'wp_theme' );
    4146
     47        static::$custom_single_post_template = self::factory()->post->create_and_get(
     48            array(
     49                'post_type'    => 'wp_template',
     50                'post_name'    => 'custom-single-post-template',
     51                'post_title'   => 'Custom Single Post template (modified)',
     52                'post_content' => 'Content',
     53                'post_excerpt' => 'Description of custom single post template',
     54                'tax_input'    => array(
     55                    'wp_theme' => array(
     56                        static::TEST_THEME,
     57                    ),
     58                ),
     59            )
     60        );
     61
     62        wp_set_post_terms( static::$custom_single_post_template->ID, static::TEST_THEME, 'wp_theme' );
     63
    4264        /*
    4365         * This template part has to have the same ID ("block-theme/small-header") as the template part
     
    6587    public static function tear_down_after_class() {
    6688        wp_delete_post( static::$template->ID );
     89        wp_delete_post( static::$custom_single_post_template->ID );
    6790        wp_delete_post( static::$template_part->ID );
    6891
     
    114137        );
    115138    }
     139
     140    /**
     141     * @dataProvider data_get_block_templates_should_respect_posttypes_property
     142     * @ticket 55881
     143     *
     144     * @param string $post_type Post type for query.
     145     * @param array  $expected  Expected template IDs.
     146     */
     147    public function test_get_block_templates_should_respect_posttypes_property( $post_type, $expected ) {
     148        $templates = get_block_templates( array( 'post_type' => $post_type ) );
     149
     150        $this->assertSameSets(
     151            $expected,
     152            $this->get_template_ids( $templates )
     153        );
     154    }
     155
     156    /**
     157     * Data provider.
     158     *
     159     * @return array
     160     */
     161    public function data_get_block_templates_should_respect_posttypes_property() {
     162        return array(
     163            'post' => array(
     164                'post_type' => 'post',
     165                'expected'  => array(
     166                    'block-theme//custom-single-post-template',
     167                ),
     168            ),
     169            'page' => array(
     170                'post_type' => 'page',
     171                'expected'  => array(
     172                    'block-theme//page-home',
     173                ),
     174            ),
     175        );
     176    }
     177
     178    /**
     179     * Gets the template IDs from the given array.
     180     *
     181     * @param object[] $templates Array of template objects to parse.
     182     * @return string[] The template IDs.
     183     */
     184    private function get_template_ids( $templates ) {
     185        return array_map(
     186            static function( $template ) {
     187                return $template->id;
     188            },
     189            $templates
     190        );
     191    }
    116192}
Note: See TracChangeset for help on using the changeset viewer.