From b3b2ac2a554e1a8b581ed50be6b34dbd831cf1ac Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 26 Sep 2017 08:53:20 +0000 Subject: [PATCH] Themes: Report theme as broken that sets itself as its parent. Props davilera. Fixes #40820. git-svn-id: https://develop.svn.wordpress.org/trunk@41601 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-theme.php | 8 ++++++++ .../data/themedir1/child-parent-itself/style.css | 4 ++++ tests/phpunit/tests/theme/WPTheme.php | 10 ++++++++++ tests/phpunit/tests/theme/themeDir.php | 16 ++++++++++++++-- 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/data/themedir1/child-parent-itself/style.css diff --git a/src/wp-includes/class-wp-theme.php b/src/wp-includes/class-wp-theme.php index 0da9b5ea11..f4c2e1b710 100644 --- a/src/wp-includes/class-wp-theme.php +++ b/src/wp-includes/class-wp-theme.php @@ -250,6 +250,14 @@ final class WP_Theme implements ArrayAccess { } } + if ( ! $this->template && $this->stylesheet === $this->headers['Template'] ) { + /* translators: %s: Template */ + $this->errors = new WP_Error( 'theme_child_invalid', sprintf( __( 'The theme defines itself as its parent theme. Please check the "%s" header.' ), 'Template' ) ); + $this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet ) ); + + return; + } + // (If template is set from cache [and there are no errors], we know it's good.) if ( ! $this->template && ! ( $this->template = $this->headers['Template'] ) ) { $this->template = $this->stylesheet; diff --git a/tests/phpunit/data/themedir1/child-parent-itself/style.css b/tests/phpunit/data/themedir1/child-parent-itself/style.css new file mode 100644 index 0000000000..af71df63e8 --- /dev/null +++ b/tests/phpunit/data/themedir1/child-parent-itself/style.css @@ -0,0 +1,4 @@ +/* +Theme Name: Child and Parent Theme +Template: child-parent-itself +*/ \ No newline at end of file diff --git a/tests/phpunit/tests/theme/WPTheme.php b/tests/phpunit/tests/theme/WPTheme.php index da7374e090..b0886e51d8 100644 --- a/tests/phpunit/tests/theme/WPTheme.php +++ b/tests/phpunit/tests/theme/WPTheme.php @@ -140,6 +140,16 @@ class Tests_Theme_WPTheme extends WP_UnitTestCase { $this->assertFalse( $theme->display( 'Tags' ) ); } + /** + * @ticket 40820 + */ + function test_child_theme_with_itself_as_parent_should_appear_as_broken() { + $theme = new WP_Theme( 'child-parent-itself', $this->theme_root ); + $errors = $theme->errors(); + $this->assertWPError( $errors ); + $this->assertEquals( 'theme_child_invalid', $errors->get_error_code() ); + } + /** * Enable a single theme on a network. diff --git a/tests/phpunit/tests/theme/themeDir.php b/tests/phpunit/tests/theme/themeDir.php index ef6e64c210..67301429ef 100644 --- a/tests/phpunit/tests/theme/themeDir.php +++ b/tests/phpunit/tests/theme/themeDir.php @@ -175,8 +175,20 @@ class Tests_Theme_ThemeDir extends WP_UnitTestCase { * @expectedDeprecated get_broken_themes */ function test_broken_themes() { - $themes = get_themes(); - $expected = array('broken-theme' => array('Name' => 'broken-theme', 'Title' => 'broken-theme', 'Description' => __('Stylesheet is missing.'))); + $themes = get_themes(); + + $expected = array( + 'broken-theme' => array( + 'Name' => 'broken-theme', + 'Title' => 'broken-theme', + 'Description' => __( 'Stylesheet is missing.' ), + ), + 'Child and Parent Theme' => array( + 'Name' => 'Child and Parent Theme', + 'Title' => 'Child and Parent Theme', + 'Description' => sprintf( __( 'The theme defines itself as its parent theme. Please check the "%s" header.' ), 'Template' ), + ), + ); $this->assertEquals($expected, get_broken_themes() ); }