Ticket #38540: 38540.4.diff
File 38540.4.diff, 10.3 KB (added by , 8 years ago) |
---|
-
src/wp-includes/theme.php
diff --git src/wp-includes/theme.php src/wp-includes/theme.php index 7514dd9..13bf9ca 100644
function get_editor_stylesheets() { 1824 1824 */ 1825 1825 function get_theme_starter_content() { 1826 1826 $theme_support = get_theme_support( 'starter-content' ); 1827 if ( ! empty( $theme_support) ) {1827 if ( is_array( $theme_support ) && ! empty( $theme_support[0] ) && is_array( $theme_support[0] ) ) { 1828 1828 $config = $theme_support[0]; 1829 1829 } else { 1830 1830 $config = array(); -
tests/phpunit/tests/customize/manager.php
diff --git tests/phpunit/tests/customize/manager.php tests/phpunit/tests/customize/manager.php index 950f292..63424a6 100644
class Tests_WP_Customize_Manager extends WP_UnitTestCase { 163 163 $this->assertInstanceOf( 'WPDieException', $exception ); 164 164 $this->assertContains( 'Invalid changeset UUID', $exception->getMessage() ); 165 165 166 update_option( 'fresh_site', 0 ); 166 167 $wp_customize = new WP_Customize_Manager(); 167 168 $wp_customize->setup_theme(); 169 $this->assertFalse( has_action( 'after_setup_theme', array( $wp_customize, 'import_theme_starter_content' ) ) ); 170 171 // Make sure that starter content import gets queued on a fresh site. 172 update_option( 'fresh_site', 1 ); 173 $wp_customize->setup_theme(); 174 $this->assertEquals( 100, has_action( 'after_setup_theme', array( $wp_customize, 'import_theme_starter_content' ) ) ); 168 175 } 169 176 170 177 /** … … class Tests_WP_Customize_Manager extends WP_UnitTestCase { 299 306 } 300 307 301 308 /** 309 * Test WP_Customize_Manager::import_theme_starter_content(). 310 * 311 * @covers WP_Customize_Manager::import_theme_starter_content() 312 * @covers WP_Customize_Manager::_save_starter_content_changeset() 313 */ 314 function test_import_theme_starter_content() { 315 wp_set_current_user( self::$admin_user_id ); 316 317 global $wp_customize; 318 $wp_customize = new WP_Customize_Manager(); 319 $starter_content_config = array( 320 'widgets' => array( 321 'sidebar-1' => array( 322 'text_business_info', 323 'meta_custom' => array( 'meta', array( 324 'title' => 'Pre-hydrated meta widget.' 325 ) ), 326 ), 327 ), 328 'nav_menus' => array( 329 'top' => array( 330 'name' => 'Menu Name', 331 'items' => array( 332 'page_home', 333 'page_about', 334 'page_blog', 335 'link_email', 336 'link_facebook', 337 'link_custom' => array( 338 'title' => 'Custom', 339 'url' => 'https://custom.example.com/' 340 ), 341 ), 342 ), 343 ), 344 'posts' => array( 345 'home', 346 'about', 347 'blog', 348 'custom' => array( 349 'post_type' => 'post', 350 'post_title' => 'Custom', 351 ), 352 ), 353 'options' => array( 354 'show_on_front' => 'page', 355 'page_on_front' => '{{home}}', 356 'page_for_posts' => '{{blog}}', 357 ), 358 ); 359 360 add_theme_support( 'starter-content', $starter_content_config ); 361 $this->assertEmpty( $wp_customize->unsanitized_post_values() ); 362 $wp_customize->import_theme_starter_content(); 363 $changeset_values = $wp_customize->unsanitized_post_values(); 364 $expected_setting_ids = array( 365 'widget_text[2]', 366 'widget_meta[3]', 367 'sidebars_widgets[sidebar-1]', 368 'nav_menus_created_posts', 369 'nav_menu[-1]', 370 'nav_menu_item[-1]', 371 'nav_menu_item[-2]', 372 'nav_menu_item[-3]', 373 'nav_menu_item[-4]', 374 'nav_menu_item[-5]', 375 'nav_menu_item[-6]', 376 'nav_menu_locations[top]', 377 'show_on_front', 378 'page_on_front', 379 'page_for_posts', 380 ); 381 $this->assertEqualSets( $expected_setting_ids, array_keys( $changeset_values ) ); 382 383 foreach ( array( 'widget_text[2]', 'widget_meta[3]' ) as $setting_id ) { 384 $this->assertInternalType( 'array', $changeset_values[ $setting_id ] ); 385 $instance_data = $wp_customize->widgets->sanitize_widget_instance( $changeset_values[ $setting_id ] ); 386 $this->assertInternalType( 'array', $instance_data ); 387 $this->assertArrayHasKey( 'title', $instance_data ); 388 } 389 390 $this->assertEquals( array( 'text-2', 'meta-3' ), $changeset_values['sidebars_widgets[sidebar-1]'] ); 391 392 $posts_by_name = array(); 393 foreach ( $changeset_values['nav_menus_created_posts'] as $post_id ) { 394 $post = get_post( $post_id ); 395 $this->assertEquals( 'auto-draft', $post->post_status ); 396 $posts_by_name[ $post->post_name ] = $post->ID; 397 } 398 399 $this->assertEquals( 'page', $changeset_values['show_on_front'] ); 400 $this->assertEquals( $posts_by_name['home'], $changeset_values['page_on_front'] ); 401 $this->assertEquals( $posts_by_name['blog'], $changeset_values['page_for_posts'] ); 402 403 $this->assertEquals( -1, $changeset_values['nav_menu_locations[top]'] ); 404 $this->assertEquals( $posts_by_name['home'], $changeset_values['nav_menu_item[-1]']['object_id'] ); 405 406 // @todo Ensure that we can merge on top of existing changeset. 407 } 408 409 /** 302 410 * Test WP_Customize_Manager::customize_preview_init(). 303 411 * 304 412 * @ticket 30937 -
new file tests/phpunit/tests/theme/getThemeStarterContent.php
diff --git tests/phpunit/tests/theme/getThemeStarterContent.php tests/phpunit/tests/theme/getThemeStarterContent.php new file mode 100644 index 0000000..27320da
- + 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 }