diff --git a/tests/phpunit/data/themedir2/test-parent/functions.php b/tests/phpunit/data/themedir2/test-parent/functions.php new file mode 100644 index 0000000000000..f538fe42304c2 --- /dev/null +++ b/tests/phpunit/data/themedir2/test-parent/functions.php @@ -0,0 +1,7 @@ + diff --git a/tests/phpunit/data/themedir2/test-parent/index.php b/tests/phpunit/data/themedir2/test-parent/index.php new file mode 100644 index 0000000000000..f538fe42304c2 --- /dev/null +++ b/tests/phpunit/data/themedir2/test-parent/index.php @@ -0,0 +1,7 @@ + diff --git a/tests/phpunit/data/themedir2/test-parent/style.css b/tests/phpunit/data/themedir2/test-parent/style.css new file mode 100644 index 0000000000000..aaa61dfea4bda --- /dev/null +++ b/tests/phpunit/data/themedir2/test-parent/style.css @@ -0,0 +1,12 @@ +/* +Theme Name: Test Parent Theme +Theme URI: http://example.org/ +Description: An example parent theme +Version: 1.3 +Author: Minnie Bannister +Author URI: http://example.com/ +Template: test-parent +*/ + + + diff --git a/tests/phpunit/data/themedir2/test/functions.php b/tests/phpunit/data/themedir2/test/functions.php new file mode 100644 index 0000000000000..f538fe42304c2 --- /dev/null +++ b/tests/phpunit/data/themedir2/test/functions.php @@ -0,0 +1,7 @@ + diff --git a/tests/phpunit/data/themedir2/test/index.php b/tests/phpunit/data/themedir2/test/index.php new file mode 100644 index 0000000000000..f538fe42304c2 --- /dev/null +++ b/tests/phpunit/data/themedir2/test/index.php @@ -0,0 +1,7 @@ + diff --git a/tests/phpunit/data/themedir2/test/style.css b/tests/phpunit/data/themedir2/test/style.css new file mode 100644 index 0000000000000..b3d9e77cd2323 --- /dev/null +++ b/tests/phpunit/data/themedir2/test/style.css @@ -0,0 +1,12 @@ +/* +Theme Name: Test Theme +Theme URI: http://example.org/ +Description: An example theme +Version: 1.3 +Author: Minnie Bannister +Author URI: http://example.com/ +Template: test-parent +*/ + + + diff --git a/tests/phpunit/tests/theme.php b/tests/phpunit/tests/theme.php index 7260d6af57c46..2d98e062374fd 100644 --- a/tests/phpunit/tests/theme.php +++ b/tests/phpunit/tests/theme.php @@ -1179,4 +1179,118 @@ private function helper_requires_block_theme() { $this->markTestSkipped( "Could not switch to $block_theme." ); } } + + /** + * Make sure filters added after the initial call are fired. + * + * @ticket 59847 + */ + public function test_get_stylesheet_directory_filters_apply() { + // Call the function prior to the filter being added. + get_stylesheet_directory(); + + $expected = 'test_root/dir'; + + // Add the filer. + add_filter( + 'stylesheet_directory', + function () use ( $expected ) { + return $expected; + } + ); + + $this->assertSame( $expected, get_stylesheet_directory() ); + } + + /** + * Make sure filters added after the initial call are fired. + * + * @ticket 59847 + */ + public function test_get_template_directory_filters_apply() { + // Call the function prior to the filter being added. + get_template_directory(); + + $expected = 'test_root/dir'; + + // Add the filer. + add_filter( + 'template_directory', + function () use ( $expected ) { + return $expected; + } + ); + + $this->assertSame( $expected, get_template_directory() ); + } + + /** + * @ticket 59847 + */ + public function test_get_stylesheet_directory_uses_registered_theme_dir() { + $old_theme = wp_get_theme(); + + switch_theme( 'test' ); + + $old_root = get_theme_root( 'test' ); + $path1 = get_stylesheet_directory(); + + $new_root = DIR_TESTDATA . '/themedir2'; + register_theme_directory( $new_root ); + + // Mock the stylesheet root option to mimic that the active root has changed. + add_filter( + 'pre_option_stylesheet_root', + function () use ( $new_root ) { + return $new_root; + } + ); + + $path2 = get_stylesheet_directory(); + + // Cleanup. + switch_theme( $old_theme->get_stylesheet() ); + + $this->assertEquals( $old_root . '/test', $path1, 'The original stylesheet path is not correct' ); + $this->assertEquals( $new_root . '/test', $path2, 'The new stylesheet path is not correct' ); + } + + /** + * @ticket 59847 + */ + public function test_get_template_directory_uses_registered_theme_dir() { + $old_theme = wp_get_theme(); + + switch_theme( 'test' ); + + // Mock parent theme to be returned as the template. + add_filter( + 'pre_option_template', + function () { + return 'test-parent'; + } + ); + + $old_root = get_theme_root( 'test' ); + $path1 = get_template_directory(); + + $new_root = DIR_TESTDATA . '/themedir2'; + register_theme_directory( $new_root ); + + // Mock the template root option to mimic that the active root has changed. + add_filter( + 'pre_option_template_root', + function () use ( $new_root ) { + return $new_root; + } + ); + + $path2 = get_template_directory(); + + // Cleanup. + switch_theme( $old_theme->get_stylesheet() ); + + $this->assertEquals( $old_root . '/test-parent', $path1, 'The original template path is not correct' ); + $this->assertEquals( $new_root . '/test-parent', $path2, 'The new template path is not correct' ); + } }