Repository navigation
Tests: Add unit tests for wp_get_active_and_valid_themes()
#14482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
shail-mehta
wants to merge
2
commits into
WordPress:trunk
from
shail-mehta:65819-add-wp-get-active-and-valid-themes-tests
+275
−0
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,275 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Tests for wp_get_active_and_valid_themes(). | ||
| * | ||
| * @group load | ||
| * @group themes | ||
| * | ||
| * @covers ::wp_get_active_and_valid_themes | ||
| */ | ||
| class Tests_Load_WpGetActiveAndValidThemes extends WP_UnitTestCase { | ||
|
|
||
| const TEST_SESSION_ID = 'test_active_valid_themes_session'; | ||
|
|
||
| /** | ||
| * Original $wp_stylesheet_path global. | ||
| * | ||
| * @var mixed | ||
| */ | ||
| private $original_stylesheet_path; | ||
|
|
||
| /** | ||
| * Original $wp_template_path global. | ||
| * | ||
| * @var mixed | ||
| */ | ||
| private $original_template_path; | ||
|
|
||
| /** | ||
| * Original $pagenow global. | ||
| * | ||
| * @var mixed | ||
| */ | ||
| private $original_pagenow; | ||
|
|
||
| /** | ||
| * Original installing status. | ||
| * | ||
| * @var bool | ||
| */ | ||
| private $original_installing; | ||
|
|
||
| /** | ||
| * Original stylesheet option. | ||
| * | ||
| * @var mixed | ||
| */ | ||
| private $original_stylesheet; | ||
|
|
||
| /** | ||
| * Original template option. | ||
| * | ||
| * @var mixed | ||
| */ | ||
| private $original_template; | ||
|
|
||
| /** | ||
| * Original recovery mode is_active property. | ||
| * | ||
| * @var bool | ||
| */ | ||
| private $original_is_active; | ||
|
|
||
| /** | ||
| * Original recovery mode session_id property. | ||
| * | ||
| * @var string | ||
| */ | ||
| private $original_session_id; | ||
|
|
||
| /** | ||
| * Original $_paused_themes global. | ||
| * | ||
| * @var array|null | ||
| */ | ||
| private $original_paused_themes; | ||
|
|
||
| public function set_up() { | ||
| parent::set_up(); | ||
|
|
||
| global $wp_stylesheet_path, $wp_template_path, $pagenow; | ||
|
|
||
| $this->original_stylesheet_path = $wp_stylesheet_path ?? null; | ||
| $this->original_template_path = $wp_template_path ?? null; | ||
| $this->original_pagenow = $pagenow ?? null; | ||
| $this->original_installing = wp_installing(); | ||
| $this->original_stylesheet = get_option( 'stylesheet' ); | ||
| $this->original_template = get_option( 'template' ); | ||
| $this->original_is_active = $this->get_recovery_mode_property( 'is_active' ); | ||
| $this->original_session_id = $this->get_recovery_mode_property( 'session_id' ); | ||
| $this->original_paused_themes = $GLOBALS['_paused_themes'] ?? null; | ||
|
|
||
| unset( $GLOBALS['_paused_themes'] ); | ||
| wp_installing( false ); | ||
| } | ||
|
|
||
| public function tear_down() { | ||
| global $wp_stylesheet_path, $wp_template_path, $pagenow; | ||
|
|
||
| $wp_stylesheet_path = $this->original_stylesheet_path; | ||
| $wp_template_path = $this->original_template_path; | ||
|
|
||
| if ( null !== $this->original_pagenow ) { | ||
| $pagenow = $this->original_pagenow; | ||
| } else { | ||
| unset( $GLOBALS['pagenow'] ); | ||
| } | ||
|
|
||
| wp_installing( $this->original_installing ); | ||
| update_option( 'stylesheet', $this->original_stylesheet ); | ||
| update_option( 'template', $this->original_template ); | ||
| $this->set_recovery_mode_state( $this->original_is_active, $this->original_session_id ); | ||
| delete_option( self::TEST_SESSION_ID . '_paused_extensions' ); | ||
|
|
||
| if ( null !== $this->original_paused_themes ) { | ||
| $GLOBALS['_paused_themes'] = $this->original_paused_themes; | ||
| } else { | ||
| unset( $GLOBALS['_paused_themes'] ); | ||
| } | ||
|
|
||
| remove_all_filters( 'wp_using_themes' ); | ||
|
|
||
| parent::tear_down(); | ||
| } | ||
|
|
||
| /** | ||
| * Gets a WP_Recovery_Mode property value. | ||
| * | ||
| * @param string $property Property name. | ||
| * @return mixed Property value. | ||
| */ | ||
| private function get_recovery_mode_property( $property ) { | ||
| $reflection = new ReflectionProperty( wp_recovery_mode(), $property ); | ||
| if ( PHP_VERSION_ID < 80100 ) { | ||
| $reflection->setAccessible( true ); | ||
| } | ||
|
|
||
| return $reflection->getValue( wp_recovery_mode() ); | ||
| } | ||
|
|
||
| /** | ||
| * Sets WP_Recovery_Mode protected state. | ||
| * | ||
| * @param bool $is_active Whether recovery mode is active. | ||
| * @param string $session_id Recovery mode session ID. | ||
| */ | ||
| private function set_recovery_mode_state( $is_active, $session_id ) { | ||
| $recovery_mode = wp_recovery_mode(); | ||
|
|
||
| $is_active_property = new ReflectionProperty( $recovery_mode, 'is_active' ); | ||
| if ( PHP_VERSION_ID < 80100 ) { | ||
| $is_active_property->setAccessible( true ); | ||
| } | ||
| $is_active_property->setValue( $recovery_mode, $is_active ); | ||
|
|
||
| $session_id_property = new ReflectionProperty( $recovery_mode, 'session_id' ); | ||
| if ( PHP_VERSION_ID < 80100 ) { | ||
| $session_id_property->setAccessible( true ); | ||
| } | ||
| $session_id_property->setValue( $recovery_mode, $session_id ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 65819 | ||
| */ | ||
| public function test_returns_template_path_for_parent_theme() { | ||
| global $wp_stylesheet_path, $wp_template_path; | ||
|
|
||
| $wp_template_path = get_theme_root() . '/twentytwentyone'; | ||
| $wp_stylesheet_path = $wp_template_path; | ||
| update_option( 'template', 'twentytwentyone' ); | ||
| update_option( 'stylesheet', 'twentytwentyone' ); | ||
|
|
||
| $this->assertSame( array( $wp_template_path ), wp_get_active_and_valid_themes() ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 65819 | ||
| */ | ||
| public function test_includes_stylesheet_path_for_child_theme() { | ||
| global $wp_stylesheet_path, $wp_template_path; | ||
| // These are not a real parent/child pair. is_child_theme() only compares the two paths. | ||
| $wp_template_path = get_theme_root() . '/twentytwentyone'; | ||
| $wp_stylesheet_path = get_theme_root() . '/twentytwentyfive'; | ||
| update_option( 'template', 'twentytwentyone' ); | ||
| update_option( 'stylesheet', 'twentytwentyfive' ); | ||
|
|
||
| $this->assertSame( | ||
| array( $wp_stylesheet_path, $wp_template_path ), | ||
| wp_get_active_and_valid_themes() | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 65819 | ||
| */ | ||
| public function test_returns_empty_array_while_installing() { | ||
| global $wp_template_path, $pagenow; | ||
|
|
||
| $wp_template_path = get_theme_root() . '/twentytwentyone'; | ||
| $pagenow = 'index.php'; | ||
| wp_installing( true ); | ||
|
|
||
| $this->assertSame( array(), wp_get_active_and_valid_themes() ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 65819 | ||
| */ | ||
| public function test_returns_themes_while_installing_on_wp_activate() { | ||
| global $wp_stylesheet_path, $wp_template_path, $pagenow; | ||
|
|
||
| $wp_template_path = get_theme_root() . '/twentytwentyone'; | ||
| $wp_stylesheet_path = $wp_template_path; | ||
| $pagenow = 'wp-activate.php'; | ||
| wp_installing( true ); | ||
|
|
||
| $this->assertSame( array( $wp_template_path ), wp_get_active_and_valid_themes() ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 65819 | ||
| */ | ||
| public function test_skips_paused_themes_in_recovery_mode() { | ||
| global $wp_stylesheet_path, $wp_template_path; | ||
|
|
||
| $wp_template_path = get_theme_root() . '/twentytwentyone'; | ||
| $wp_stylesheet_path = get_theme_root() . '/twentytwentyfive'; | ||
| update_option( 'template', 'twentytwentyone' ); | ||
| update_option( 'stylesheet', 'twentytwentyfive' ); | ||
|
|
||
| $this->set_recovery_mode_state( true, self::TEST_SESSION_ID ); | ||
| $this->assertTrue( | ||
| wp_paused_themes()->set( | ||
| 'twentytwentyfive', | ||
| array( | ||
| 'type' => E_ERROR, | ||
| 'file' => $wp_stylesheet_path . '/functions.php', | ||
| 'line' => 1, | ||
| 'message' => 'Fatal error', | ||
| ) | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertSame( array( 1 => $wp_template_path ), wp_get_active_and_valid_themes() ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 65819 | ||
| */ | ||
| public function test_disables_themes_when_all_are_paused_in_recovery_mode() { | ||
| global $wp_stylesheet_path, $wp_template_path; | ||
|
|
||
| $wp_template_path = get_theme_root() . '/twentytwentyone'; | ||
| $wp_stylesheet_path = $wp_template_path; | ||
| update_option( 'template', 'twentytwentyone' ); | ||
| update_option( 'stylesheet', 'twentytwentyone' ); | ||
|
|
||
| $this->set_recovery_mode_state( true, self::TEST_SESSION_ID ); | ||
| $this->assertTrue( | ||
| wp_paused_themes()->set( | ||
| 'twentytwentyone', | ||
| array( | ||
| 'type' => E_ERROR, | ||
| 'file' => $wp_template_path . '/functions.php', | ||
| 'line' => 1, | ||
| 'message' => 'Fatal error', | ||
| ) | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertSame( array(), wp_get_active_and_valid_themes() ); | ||
| $this->assertNotFalse( has_filter( 'wp_using_themes', '__return_false' ) ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional:
twentytwentyfiveis not really a child theme oftwentytwentyone. The test works becauseis_child_theme()only compares the two paths. A one-line comment saying that would help readers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added One line comment in e661b71