Skip to content

Commit 78d8e29

Browse files
committed
Tests: Restore the active theme after template REST requests.
Switch back to the original stylesheet after testing file-backed template dates and template fallbacks. This clears the theme data that otherwise changes featured-image markup in subsequent media tests. Developed in: #16266 Props mayankmajeji. See #66755. git-svn-id: https://develop.svn.wordpress.org/trunk@65814 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f10e114 commit 78d8e29

1 file changed

Lines changed: 37 additions & 26 deletions

File tree

‎tests/phpunit/tests/rest-api/wpRestTemplatesController.php‎

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -647,15 +647,20 @@ public function test_get_item_from_registry() {
647647
*/
648648
public function test_get_item_dates_are_null_for_file_backed_template() {
649649
wp_set_current_user( self::$admin_id );
650+
$stylesheet = get_stylesheet();
650651
switch_theme( 'block-theme' );
651652

652-
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/block-theme//page-home' );
653-
$response = rest_get_server()->dispatch( $request );
654-
$data = $response->get_data();
653+
try {
654+
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/block-theme//page-home' );
655+
$response = rest_get_server()->dispatch( $request );
656+
$data = $response->get_data();
655657

656-
$this->assertSame( 200, $response->get_status(), 'Fetching a file-backed template should return 200.' );
657-
$this->assertNull( $data['date'], 'The date should be null for a file-backed template.' );
658-
$this->assertNull( $data['modified'], 'The modified date should be null for a file-backed template.' );
658+
$this->assertSame( 200, $response->get_status(), 'Fetching a file-backed template should return 200.' );
659+
$this->assertNull( $data['date'], 'The date should be null for a file-backed template.' );
660+
$this->assertNull( $data['modified'], 'The modified date should be null for a file-backed template.' );
661+
} finally {
662+
switch_theme( $stylesheet );
663+
}
659664
}
660665

661666
/**
@@ -1153,27 +1158,33 @@ public static function data_create_item_with_is_wp_suggestion() {
11531158
*/
11541159
public function test_get_template_fallback() {
11551160
wp_set_current_user( self::$admin_id );
1161+
$stylesheet = get_stylesheet();
11561162
switch_theme( 'block-theme' );
1157-
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/lookup' );
1158-
// Should fallback to `index.html`.
1159-
$request->set_param( 'slug', 'tag-status' );
1160-
$request->set_param( 'is_custom', false );
1161-
$request->set_param( 'template_prefix', 'tag' );
1162-
$response = rest_get_server()->dispatch( $request );
1163-
$this->assertSame( 'index', $response->get_data()['slug'], 'Should fallback to `index.html`.' );
1164-
// Should fallback to `page.html`.
1165-
$request->set_param( 'slug', 'page-hello' );
1166-
$request->set_param( 'is_custom', false );
1167-
$request->set_param( 'template_prefix', 'page' );
1168-
$response = rest_get_server()->dispatch( $request );
1169-
$this->assertSame( 'page', $response->get_data()['slug'], 'Should fallback to `page.html`.' );
1170-
// Should fallback to `index.html`.
1171-
$request->set_param( 'slug', 'author' );
1172-
$request->set_param( 'ignore_empty', true );
1173-
$request->set_param( 'template_prefix', 'tag' );
1174-
$request->set_param( 'is_custom', false );
1175-
$response = rest_get_server()->dispatch( $request );
1176-
$this->assertSame( 'index', $response->get_data()['slug'], 'Should fallback to `index.html` when ignore_empty is `true`.' );
1163+
1164+
try {
1165+
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/lookup' );
1166+
// Should fallback to `index.html`.
1167+
$request->set_param( 'slug', 'tag-status' );
1168+
$request->set_param( 'is_custom', false );
1169+
$request->set_param( 'template_prefix', 'tag' );
1170+
$response = rest_get_server()->dispatch( $request );
1171+
$this->assertSame( 'index', $response->get_data()['slug'], 'Should fallback to `index.html`.' );
1172+
// Should fallback to `page.html`.
1173+
$request->set_param( 'slug', 'page-hello' );
1174+
$request->set_param( 'is_custom', false );
1175+
$request->set_param( 'template_prefix', 'page' );
1176+
$response = rest_get_server()->dispatch( $request );
1177+
$this->assertSame( 'page', $response->get_data()['slug'], 'Should fallback to `page.html`.' );
1178+
// Should fallback to `index.html`.
1179+
$request->set_param( 'slug', 'author' );
1180+
$request->set_param( 'ignore_empty', true );
1181+
$request->set_param( 'template_prefix', 'tag' );
1182+
$request->set_param( 'is_custom', false );
1183+
$response = rest_get_server()->dispatch( $request );
1184+
$this->assertSame( 'index', $response->get_data()['slug'], 'Should fallback to `index.html` when ignore_empty is `true`.' );
1185+
} finally {
1186+
switch_theme( $stylesheet );
1187+
}
11771188
}
11781189

11791190
/**

0 commit comments

Comments
 (0)