Changeset 54198 for trunk/tests/phpunit/tests/blocks/getBlockTemplates.php
- Timestamp:
- 09/18/2022 01:08:33 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/blocks/getBlockTemplates.php
r54187 r54198 12 12 * @var WP_Post 13 13 */ 14 private static $ template;14 private static $index_template; 15 15 16 16 /** … … 22 22 * @var WP_Post 23 23 */ 24 private static $template_part; 25 26 public static function set_up_before_class() { 27 parent::set_up_before_class(); 28 24 private static $small_header_template_part; 25 26 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 29 27 /* 30 28 * This template has to have the same ID ("block-theme/index") as the template 31 29 * that is shipped with the "block-theme" theme. This is needed for testing purposes. 32 30 */ 33 s tatic::$template = self::factory()->post->create_and_get(31 self::$index_template = $factory->post->create_and_get( 34 32 array( 35 33 'post_type' => 'wp_template', … … 37 35 'tax_input' => array( 38 36 'wp_theme' => array( 39 s tatic::TEST_THEME,37 self::TEST_THEME, 40 38 ), 41 39 ), … … 43 41 ); 44 42 45 wp_set_post_terms( s tatic::$template->ID, static::TEST_THEME, 'wp_theme' );46 47 s tatic::$custom_single_post_template = self::factory()->post->create_and_get(43 wp_set_post_terms( self::$index_template->ID, self::TEST_THEME, 'wp_theme' ); 44 45 self::$custom_single_post_template = $factory->post->create_and_get( 48 46 array( 49 47 'post_type' => 'wp_template', … … 54 52 'tax_input' => array( 55 53 'wp_theme' => array( 56 s tatic::TEST_THEME,54 self::TEST_THEME, 57 55 ), 58 56 ), … … 60 58 ); 61 59 62 wp_set_post_terms( s tatic::$custom_single_post_template->ID, static::TEST_THEME, 'wp_theme' );60 wp_set_post_terms( self::$custom_single_post_template->ID, self::TEST_THEME, 'wp_theme' ); 63 61 64 62 /* … … 66 64 * that is shipped with the "block-theme" theme. This is needed for testing purposes. 67 65 */ 68 self::$ template_part = self::factory()->post->create_and_get(66 self::$small_header_template_part = $factory->post->create_and_get( 69 67 array( 70 68 'post_type' => 'wp_template_part', … … 72 70 'tax_input' => array( 73 71 'wp_theme' => array( 74 s tatic::TEST_THEME,72 self::TEST_THEME, 75 73 ), 76 74 'wp_template_part_area' => array( … … 81 79 ); 82 80 83 wp_set_post_terms( self::$template_part->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' ); 84 wp_set_post_terms( self::$template_part->ID, static::TEST_THEME, 'wp_theme' ); 85 } 86 87 public static function tear_down_after_class() { 88 wp_delete_post( static::$template->ID ); 89 wp_delete_post( static::$custom_single_post_template->ID ); 90 wp_delete_post( static::$template_part->ID ); 91 92 parent::tear_down_after_class(); 81 wp_set_post_terms( self::$small_header_template_part->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' ); 82 wp_set_post_terms( self::$small_header_template_part->ID, self::TEST_THEME, 'wp_theme' ); 83 } 84 85 public static function wpTearDownAfterClass() { 86 wp_delete_post( self::$index_template->ID ); 87 wp_delete_post( self::$custom_single_post_template->ID ); 88 wp_delete_post( self::$small_header_template_part->ID ); 93 89 } 94 90 95 91 public function set_up() { 96 92 parent::set_up(); 97 switch_theme( static::TEST_THEME ); 93 switch_theme( self::TEST_THEME ); 94 } 95 96 /** 97 * Gets the template IDs from the given array. 98 * 99 * @param object[] $templates Array of template objects to parse. 100 * @return string[] The template IDs. 101 */ 102 private function get_template_ids( $templates ) { 103 return array_map( 104 static function( $template ) { 105 return $template->id; 106 }, 107 $templates 108 ); 109 } 110 111 /** 112 * Should retrieve block templates (file and CPT) 113 */ 114 public function test_get_block_templates() { 115 // All results. 116 $templates = get_block_templates( array(), 'wp_template' ); 117 $template_ids = $this->get_template_ids( $templates ); 118 119 // Avoid testing the entire array because the theme might add/remove templates. 120 $this->assertContains( get_stylesheet() . '//' . 'custom-single-post-template', $template_ids ); 121 122 // The result might change in a block theme. 123 $this->assertContains( get_stylesheet() . '//' . 'index', $template_ids ); 124 125 // Filter by slug. 126 $templates = get_block_templates( array( 'slug__in' => array( 'custom-single-post-template' ) ), 'wp_template' ); 127 $template_ids = $this->get_template_ids( $templates ); 128 $this->assertSame( array( get_stylesheet() . '//' . 'custom-single-post-template' ), $template_ids ); 129 130 // Filter by CPT ID. 131 $templates = get_block_templates( array( 'wp_id' => self::$custom_single_post_template->ID ), 'wp_template' ); 132 $template_ids = $this->get_template_ids( $templates ); 133 $this->assertSame( array( get_stylesheet() . '//' . 'custom-single-post-template' ), $template_ids ); 134 135 // Filter template part by area. 136 // Requires a block theme. 137 $templates = get_block_templates( array( 'area' => WP_TEMPLATE_PART_AREA_HEADER ), 'wp_template_part' ); 138 $template_ids = $this->get_template_ids( $templates ); 139 $this->assertSame( 140 array( 141 get_stylesheet() . '//' . 'small-header', 142 ), 143 $template_ids 144 ); 98 145 } 99 146 … … 175 222 ); 176 223 } 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 $templates190 );191 }192 224 }
Note: See TracChangeset
for help on using the changeset viewer.