| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * Tests get_theme_starter_content(). |
| | 5 | * |
| | 6 | * @group themes |
| | 7 | */ |
| | 8 | class Tests_WP_Theme_Get_Theme_Starter_Content extends WP_UnitTestCase { |
| | 9 | |
| | 10 | /** |
| | 11 | * Testing passing an empty array as starter content. |
| | 12 | */ |
| | 13 | function test_add_theme_support_empty() { |
| | 14 | add_theme_support( 'starter-content', array() ); |
| | 15 | $starter_content = get_theme_starter_content(); |
| | 16 | |
| | 17 | $this->assertEmpty( $starter_content ); |
| | 18 | } |
| | 19 | |
| | 20 | /** |
| | 21 | * Testing passing nothing as starter content. |
| | 22 | */ |
| | 23 | function test_add_theme_support_single_param() { |
| | 24 | add_theme_support( 'starter-content' ); |
| | 25 | $starter_content = get_theme_starter_content(); |
| | 26 | |
| | 27 | $this->assertEmpty( $starter_content ); |
| | 28 | } |
| | 29 | |
| | 30 | /** |
| | 31 | * Testing that placeholder starter content gets expanded, that unrecognized placeholders are discarded, and that custom items are recognized. |
| | 32 | */ |
| | 33 | function test_default_content_sections() { |
| | 34 | |
| | 35 | /* |
| | 36 | * All placeholder identifiers should be referenced in this sample starter |
| | 37 | * content and then tested to ensure they get hydrated in the call to |
| | 38 | * get_theme_starter_content() to ensure that the starter content |
| | 39 | * placeholder identifiers remain intact in core. |
| | 40 | */ |
| | 41 | $dehydrated_starter_content = array( |
| | 42 | 'widgets' => array( |
| | 43 | 'sidebar-1' => array( |
| | 44 | 'text_business_info', |
| | 45 | 'text_about', |
| | 46 | 'archives', |
| | 47 | 'calendar', |
| | 48 | 'categories', |
| | 49 | 'meta', |
| | 50 | 'recent-comments', |
| | 51 | 'recent-posts', |
| | 52 | 'search', |
| | 53 | 'unknown', |
| | 54 | 'meta_custom' => array( 'meta', array( |
| | 55 | 'title' => 'Pre-hydrated meta widget.' |
| | 56 | ) ), |
| | 57 | ), |
| | 58 | ), |
| | 59 | 'nav_menus' => array( |
| | 60 | 'top' => array( |
| | 61 | 'name' => 'Menu Name', |
| | 62 | 'items' => array( |
| | 63 | 'page_home', |
| | 64 | 'page_about', |
| | 65 | 'page_blog', |
| | 66 | 'page_news', |
| | 67 | 'page_contact', |
| | 68 | 'link_email', |
| | 69 | 'link_facebook', |
| | 70 | 'link_foursquare', |
| | 71 | 'link_github', |
| | 72 | 'link_instagram', |
| | 73 | 'link_linkedin', |
| | 74 | 'link_pinterest', |
| | 75 | 'link_twitter', |
| | 76 | 'link_yelp', |
| | 77 | 'link_youtube', |
| | 78 | 'link_unknown', |
| | 79 | 'link_custom' => array( |
| | 80 | 'title' => 'Custom', |
| | 81 | 'url' => 'https://custom.example.com/' |
| | 82 | ), |
| | 83 | ), |
| | 84 | ), |
| | 85 | ), |
| | 86 | 'posts' => array( |
| | 87 | 'home', |
| | 88 | 'about', |
| | 89 | 'contact', |
| | 90 | 'blog', |
| | 91 | 'news', |
| | 92 | 'homepage-section', |
| | 93 | 'unknown', |
| | 94 | 'custom' => array( |
| | 95 | 'post_type' => 'post', |
| | 96 | 'post_title' => 'Custom', |
| | 97 | ), |
| | 98 | ), |
| | 99 | 'options' => array( |
| | 100 | 'show_on_front' => 'page', |
| | 101 | 'page_on_front' => '{{home}}', |
| | 102 | 'page_for_posts' => '{{blog}}', |
| | 103 | ), |
| | 104 | 'theme_mods' => array( |
| | 105 | 'panel_1' => '{{homepage-section}}', |
| | 106 | 'panel_2' => '{{about}}', |
| | 107 | 'panel_3' => '{{blog}}', |
| | 108 | 'panel_4' => '{{contact}}', |
| | 109 | ), |
| | 110 | ); |
| | 111 | |
| | 112 | add_theme_support( 'starter-content', $dehydrated_starter_content ); |
| | 113 | |
| | 114 | $hydrated_starter_content = get_theme_starter_content(); |
| | 115 | $this->assertSame( $hydrated_starter_content['theme_mods'], $dehydrated_starter_content['theme_mods'] ); |
| | 116 | $this->assertSame( $hydrated_starter_content['options'], $dehydrated_starter_content['options'] ); |
| | 117 | $this->assertCount( 16, $hydrated_starter_content['nav_menus']['top']['items'], 'Unknown should be dropped, custom should be present.' ); |
| | 118 | $this->assertCount( 10, $hydrated_starter_content['widgets']['sidebar-1'], 'Unknown should be dropped.' ); |
| | 119 | |
| | 120 | foreach ( $hydrated_starter_content['widgets']['sidebar-1'] as $widget ) { |
| | 121 | $this->assertInternalType( 'array', $widget ); |
| | 122 | $this->assertCount( 2, $widget ); |
| | 123 | $this->assertInternalType( 'string', $widget[0] ); |
| | 124 | $this->assertInternalType( 'array', $widget[1] ); |
| | 125 | $this->assertArrayHasKey( 'title', $widget[1] ); |
| | 126 | } |
| | 127 | |
| | 128 | foreach ( $hydrated_starter_content['nav_menus']['top']['items'] as $nav_menu_item ) { |
| | 129 | $this->assertInternalType( 'array', $nav_menu_item ); |
| | 130 | $this->assertTrue( ! empty( $nav_menu_item['object_id'] ) || ! empty( $nav_menu_item['url'] ) ); |
| | 131 | } |
| | 132 | |
| | 133 | foreach ( $hydrated_starter_content['posts'] as $key => $post ) { |
| | 134 | $this->assertInternalType( 'string', $key ); |
| | 135 | $this->assertFalse( is_numeric( $key ) ); |
| | 136 | $this->assertInternalType( 'array', $post ); |
| | 137 | $this->assertArrayHasKey( 'post_type', $post ); |
| | 138 | $this->assertArrayHasKey( 'post_title', $post ); |
| | 139 | } |
| | 140 | } |
| | 141 | |
| | 142 | /** |
| | 143 | * Testing the filter with the text_credits widget. |
| | 144 | */ |
| | 145 | function test_get_theme_starter_content_filter() { |
| | 146 | |
| | 147 | add_theme_support( 'starter-content', |
| | 148 | array( |
| | 149 | 'widgets' => array( |
| | 150 | 'sidebar-1' => array( |
| | 151 | 'text_about', |
| | 152 | ), |
| | 153 | ), |
| | 154 | ) |
| | 155 | ); |
| | 156 | |
| | 157 | add_filter( 'get_theme_starter_content', array( $this, 'filter_theme_starter_content' ), 10, 2 ); |
| | 158 | $starter_content = get_theme_starter_content(); |
| | 159 | |
| | 160 | $this->assertCount( 2, $starter_content['widgets']['sidebar-1'] ); |
| | 161 | $this->assertEquals( 'Filtered Widget', $starter_content['widgets']['sidebar-1'][1][1]['title'] ); |
| | 162 | } |
| | 163 | |
| | 164 | /** |
| | 165 | * Filter the append a widget starter content. |
| | 166 | * |
| | 167 | * @param array $content Starter content (hydrated). |
| | 168 | * @param array $config Starter content config (pre-hydrated). |
| | 169 | * @return array Filtered starter content. |
| | 170 | */ |
| | 171 | public function filter_theme_starter_content( $content, $config ) { |
| | 172 | $this->assertInternalType( 'array', $config ); |
| | 173 | $this->assertCount( 1, $config['widgets']['sidebar-1'] ); |
| | 174 | $content['widgets']['sidebar-1'][] = array( 'text', array( |
| | 175 | 'title' => 'Filtered Widget', |
| | 176 | 'text' => 'Custom ', |
| | 177 | ) ); |
| | 178 | return $content; |
| | 179 | } |
| | 180 | } |