Skip to content

Commit f5ee5d7

Browse files
authored
Merge pull request #1713 from pattonwebz/fix/wp-7-screen-mocks
Tests: fix screen mocks for WP 7.0 (instanceof WP_Screen)
2 parents 719272c + 2a7b569 commit f5ee5d7

2 files changed

Lines changed: 12 additions & 61 deletions

File tree

tests/phpunit/Admin/EnqueueAdminTest.php

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -375,40 +375,11 @@ public function testSrOnlyFormatDoesNotEnqueueOnOtherAdminPages() {
375375
* @param bool $is_block_editor Whether the screen should behave as block editor.
376376
*/
377377
private function set_mock_screen( bool $is_block_editor ): void {
378-
$GLOBALS['current_screen'] = new class( $is_block_editor ) {
379-
/**
380-
* True or false whether the screen is block editor.
381-
*
382-
* @var bool
383-
*/
384-
private $is_block_editor;
385-
386-
/**
387-
* Constructor.
388-
*
389-
* @param bool $is_block_editor Whether the screen should behave as block editor.
390-
*/
391-
public function __construct( bool $is_block_editor ) {
392-
$this->is_block_editor = $is_block_editor;
393-
}
394-
395-
/**
396-
* Mock is_block_editor method.
397-
*
398-
* @return bool
399-
*/
400-
public function is_block_editor(): bool {
401-
return $this->is_block_editor;
402-
}
403-
404-
/**
405-
* Mock in_admin method.
406-
*
407-
* @return bool
408-
*/
409-
public function in_admin(): bool {
410-
return true;
411-
}
412-
};
378+
// As of WP 7.0, get_current_screen() requires an actual WP_Screen
379+
// instance. Use WP_Screen::get() to obtain one without the side effects
380+
// of set_current_screen() (e.g. setting $hook_suffix, $typenow, firing
381+
// the current_screen action).
382+
$GLOBALS['current_screen'] = WP_Screen::get( 'post' );
383+
$GLOBALS['current_screen']->is_block_editor = $is_block_editor;
413384
}
414385
}

tests/phpunit/Admin/MetaBoxesTest.php

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -128,32 +128,12 @@ public function test_register_meta_boxes_keeps_classic_editor_when_setting_disab
128128
* @param bool $is_block_editor Whether the screen should behave as block editor.
129129
*/
130130
private function set_mock_screen( bool $is_block_editor ): void {
131-
$GLOBALS['current_screen'] = new class( $is_block_editor ) {
132-
/**
133-
* True or false whether the screen is block editor.
134-
*
135-
* @var bool
136-
*/
137-
private $is_block_editor;
138-
139-
/**
140-
* Constructor.
141-
*
142-
* @param bool $is_block_editor Whether the screen should behave as block editor.
143-
*/
144-
public function __construct( bool $is_block_editor ) {
145-
$this->is_block_editor = $is_block_editor;
146-
}
147-
148-
/**
149-
* Mock is_block_editor method.
150-
*
151-
* @return bool
152-
*/
153-
public function is_block_editor(): bool {
154-
return $this->is_block_editor;
155-
}
156-
};
131+
// As of WP 7.0, get_current_screen() requires an actual WP_Screen
132+
// instance. Use WP_Screen::get() to obtain one without the side effects
133+
// of set_current_screen() (e.g. setting $hook_suffix, $typenow, firing
134+
// the current_screen action).
135+
$GLOBALS['current_screen'] = WP_Screen::get( 'post' );
136+
$GLOBALS['current_screen']->is_block_editor = $is_block_editor;
157137
}
158138

159139
/**

0 commit comments

Comments
 (0)