|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests theme.json related public APIs. |
| 4 | + * |
| 5 | + * @package Gutenberg |
| 6 | + */ |
| 7 | + |
| 8 | +class WP_Theme_Json_Test extends WP_UnitTestCase { |
| 9 | + |
| 10 | + public function set_up() { |
| 11 | + parent::set_up(); |
| 12 | + $this->theme_root = realpath( __DIR__ . '/data/themedir1' ); |
| 13 | + |
| 14 | + $this->orig_theme_dir = $GLOBALS['wp_theme_directories']; |
| 15 | + |
| 16 | + // /themes is necessary as theme.php functions assume /themes is the root if there is only one root. |
| 17 | + $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root ); |
| 18 | + |
| 19 | + add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) ); |
| 20 | + add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) ); |
| 21 | + add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); |
| 22 | + $this->queries = array(); |
| 23 | + // Clear caches. |
| 24 | + wp_clean_themes_cache(); |
| 25 | + unset( $GLOBALS['wp_themes'] ); |
| 26 | + } |
| 27 | + |
| 28 | + public function tear_down() { |
| 29 | + $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir; |
| 30 | + wp_clean_themes_cache(); |
| 31 | + unset( $GLOBALS['wp_themes'] ); |
| 32 | + parent::tear_down(); |
| 33 | + } |
| 34 | + |
| 35 | + public function filter_set_theme_root() { |
| 36 | + return $this->theme_root; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Test that it reports correctly themes that have a theme.json. |
| 41 | + * |
| 42 | + * @group theme_json |
| 43 | + * |
| 44 | + * @covers wp_theme_has_theme_json |
| 45 | + */ |
| 46 | + public function test_theme_has_theme_json() { |
| 47 | + switch_theme( 'block-theme' ); |
| 48 | + $this->assertTrue( wp_theme_has_theme_json() ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Test that it reports correctly themes that do not have a theme.json. |
| 53 | + * |
| 54 | + * @group theme_json |
| 55 | + * |
| 56 | + * @covers wp_theme_has_theme_json |
| 57 | + */ |
| 58 | + public function test_theme_has_no_theme_json() { |
| 59 | + switch_theme( 'default' ); |
| 60 | + $this->assertFalse( wp_theme_has_theme_json() ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Test it reports correctly child themes that have a theme.json. |
| 65 | + * |
| 66 | + * @group theme_json |
| 67 | + * |
| 68 | + * @covers wp_theme_has_theme_json |
| 69 | + */ |
| 70 | + public function test_child_theme_has_theme_json() { |
| 71 | + switch_theme( 'block-theme-child' ); |
| 72 | + $this->assertTrue( wp_theme_has_theme_json() ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Test that it reports correctly child themes that do not have a theme.json |
| 77 | + * and the parent does. |
| 78 | + * |
| 79 | + * @group theme_json |
| 80 | + * |
| 81 | + * @covers wp_theme_has_theme_json |
| 82 | + */ |
| 83 | + public function test_child_theme_has_not_theme_json_but_parent_has() { |
| 84 | + switch_theme( 'block-theme-child-no-theme-json' ); |
| 85 | + $this->assertTrue( wp_theme_has_theme_json() ); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Test that it reports correctly child themes that do not have a theme.json |
| 90 | + * and the parent does not either. |
| 91 | + * |
| 92 | + * @group theme_json |
| 93 | + * |
| 94 | + * @covers wp_theme_has_theme_json |
| 95 | + */ |
| 96 | + public function test_neither_child_or_parent_themes_have_theme_json() { |
| 97 | + switch_theme( 'default-child-no-theme-json' ); |
| 98 | + $this->assertFalse( wp_theme_has_theme_json() ); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Test that switching themes recalculates theme support. |
| 103 | + * |
| 104 | + * @group theme_json |
| 105 | + * |
| 106 | + * @covers wp_theme_has_theme_json |
| 107 | + */ |
| 108 | + public function test_switching_themes_recalculates_support() { |
| 109 | + // The "default" theme doesn't have theme.json support. |
| 110 | + switch_theme( 'default' ); |
| 111 | + $default = wp_theme_has_theme_json(); |
| 112 | + |
| 113 | + // Switch to a theme that does have support. |
| 114 | + switch_theme( 'block-theme' ); |
| 115 | + $block_theme = wp_theme_has_theme_json(); |
| 116 | + |
| 117 | + $this->assertFalse( $default ); |
| 118 | + $this->assertTrue( $block_theme ); |
| 119 | + } |
| 120 | +} |
0 commit comments