Skip to content

Commit 6c3ee0f

Browse files
committed
Tests: Cover block template changes after cache population.
Verify `WP_Theme::get_post_templates()` includes newly created custom block templates and excludes deleted templates after its file-based cache has been populated. Developed in: #14051 Props irozum, huzaifaalmesbah. See #65819, #57886. git-svn-id: https://develop.svn.wordpress.org/trunk@65104 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 7375f7b commit 6c3ee0f

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

‎tests/phpunit/tests/theme/wpThemeGetPostTemplates.php‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,81 @@ public function test_get_post_templates_uses_get_file_data() {
117117
// Verify the `extra_theme_headers` filter is called.
118118
$this->assertGreaterThan( 0, $filter->get_call_count(), 'The `extra_theme_headers` filter should be called at least once.' );
119119
}
120+
121+
/**
122+
* @ticket 57886
123+
*/
124+
public function test_get_post_templates_includes_block_template_added_after_cache_is_populated() {
125+
$theme = $this->switch_to_block_theme();
126+
127+
// First call caches the file-based post templates and merges in the block templates that exist at this point.
128+
$theme->get_post_templates();
129+
130+
$this->create_custom_block_template( $theme, 'late-added-custom-template' );
131+
132+
$post_templates = $theme->get_post_templates();
133+
134+
// The cached file-based templates must not have locked in the set of block templates from the first call.
135+
$this->assertArrayHasKey( 'late-added-custom-template', $post_templates['post'], 'A block template created after the cache was populated should still be returned.' );
136+
}
137+
138+
/**
139+
* @ticket 57886
140+
*/
141+
public function test_get_post_templates_does_not_include_block_template_removed_after_cache_is_populated() {
142+
$theme = $this->switch_to_block_theme();
143+
$custom_template = $this->create_custom_block_template( $theme, 'removed-custom-template' );
144+
145+
// First call caches the file-based post templates and merges in the block template created above.
146+
$post_templates = $theme->get_post_templates();
147+
$this->assertArrayHasKey( 'removed-custom-template', $post_templates['post'], 'The block template should be present before it is deleted.' );
148+
149+
wp_delete_post( $custom_template->ID, true );
150+
151+
$post_templates = $theme->get_post_templates();
152+
153+
// The cached file-based templates must not have locked in the now-deleted block template.
154+
$this->assertArrayNotHasKey( 'removed-custom-template', $post_templates['post'], 'A block template deleted after the cache was populated should no longer be returned.' );
155+
}
156+
157+
/**
158+
* Switches to the "block-theme" test theme and runs the setup hooks it relies on
159+
* to gain `current_theme_supports( 'block-templates' )` support.
160+
*
161+
* @return WP_Theme
162+
*/
163+
private function switch_to_block_theme() {
164+
$theme = wp_get_theme( 'block-theme' );
165+
$this->assertNotEmpty( $theme );
166+
167+
switch_theme( $theme['Template'], $theme['Stylesheet'] );
168+
do_action( 'setup_theme' );
169+
do_action( 'after_setup_theme' );
170+
171+
return $theme;
172+
}
173+
174+
/**
175+
* Creates a `wp_template` post assigned to the given theme, the same way a user-customized
176+
* block template is stored.
177+
*
178+
* @param WP_Theme $theme Theme the template belongs to.
179+
* @param string $slug Template slug.
180+
* @return WP_Post
181+
*/
182+
private function create_custom_block_template( $theme, $slug ) {
183+
$custom_template = self::factory()->post->create_and_get(
184+
array(
185+
'post_type' => 'wp_template',
186+
'post_name' => $slug,
187+
'post_title' => $slug,
188+
'tax_input' => array(
189+
'wp_theme' => array( $theme->get_stylesheet() ),
190+
),
191+
)
192+
);
193+
wp_set_post_terms( $custom_template->ID, $theme->get_stylesheet(), 'wp_theme' );
194+
195+
return $custom_template;
196+
}
120197
}

0 commit comments

Comments
 (0)