Make WordPress Core

Ticket #38540: 38540.2.diff

File 38540.2.diff, 9.2 KB (added by welcher, 8 years ago)

Patch with tests for get_theme_starter_content()

  • src/wp-includes/theme.php

     
    18181818 */
    18191819function get_theme_starter_content() {
    18201820        $theme_support = get_theme_support( 'starter-content' );
    1821         if ( ! empty( $theme_support ) ) {
     1821        if ( is_array( $theme_support ) && ! empty( $theme_support ) ) {
    18221822                $config = $theme_support[0];
    18231823        } else {
    18241824                $config = array();
  • tests/phpunit/tests/theme/getThemeStarterContent.php

     
     1<?php
     2
     3/**
     4 * @group themes
     5 */
     6class Tests_WP_Theme_Get_Theme_Starter_Content extends WP_UnitTestCase {
     7
     8        /**
     9         * @var array $core_content Content taken from wp-includes/theme.php.
     10         */
     11        public $core_content;
     12
     13        function setup_core_content() {
     14                $this->core_content = array(
     15                        'widgets' => array(
     16                                'text_business_info' => array( 'text', array(
     17                                        'title' => _x( 'Find Us', 'Theme starter content' ),
     18                                        'text' => join( '', array(
     19                                                '<p><strong>' . _x( 'Address', 'Theme starter content' ) . '</strong><br />',
     20                                                _x( '123 Main Street', 'Theme starter content' ) . '<br />' . _x( 'New York, NY 10001', 'Theme starter content' ) . '</p>',
     21                                                '<p><strong>' . _x( 'Hours', 'Theme starter content' ) . '</strong><br />',
     22                                                _x( 'Monday&mdash;Friday: 9:00AM&ndash;5:00PM', 'Theme starter content' ) . '<br />' . _x( 'Saturday &amp; Sunday: 11:00AM&ndash;3:00PM', 'Theme starter content' ) . '</p>'
     23                                        ) ),
     24                                ) ),
     25                                'search' => array( 'search', array(
     26                                        'title' => _x( 'Site Search', 'Theme starter content' ),
     27                                ) ),
     28                                'text_credits' => array( 'text', array(
     29                                        'title' => _x( 'Site Credits', 'Theme starter content' ),
     30                                        'text' => sprintf( _x( 'This site was created on %s', 'Theme starter content' ), get_date_from_gmt( current_time( 'mysql', 1 ), 'c' ) ),
     31                                ) ),
     32                        ),
     33                        'nav_menus' => array(
     34                                'page_home' => array(
     35                                        'type' => 'post_type',
     36                                        'object' => 'page',
     37                                        'object_id' => '{{home}}',
     38                                ),
     39                                'page_about' => array(
     40                                        'type' => 'post_type',
     41                                        'object' => 'page',
     42                                        'object_id' => '{{about-us}}',
     43                                ),
     44                                'page_blog' => array(
     45                                        'type' => 'post_type',
     46                                        'object' => 'page',
     47                                        'object_id' => '{{blog}}',
     48                                ),
     49                                'page_contact' => array(
     50                                        'type' => 'post_type',
     51                                        'object' => 'page',
     52                                        'object_id' => '{{contact-us}}',
     53                                ),
     54
     55                                'link_yelp' => array(
     56                                        'title' => _x( 'Yelp', 'Theme starter content' ),
     57                                        'url' => 'https://www.yelp.com',
     58                                ),
     59                                'link_facebook' => array(
     60                                        'title' => _x( 'Facebook', 'Theme starter content' ),
     61                                        'url' => 'https://www.facebook.com/wordpress',
     62                                ),
     63                                'link_twitter' => array(
     64                                        'title' => _x( 'Twitter', 'Theme starter content' ),
     65                                        'url' => 'https://twitter.com/wordpress',
     66                                ),
     67                                'link_instagram' => array(
     68                                        'title' => _x( 'Instagram', 'Theme starter content' ),
     69                                        'url' => 'https://www.instagram.com/explore/tags/wordcamp/',
     70                                ),
     71                                'link_email' => array(
     72                                        'title' => _x( 'Email', 'Theme starter content' ),
     73                                        'url' => 'mailto:wordpress@example.com',
     74                                ),
     75                        ),
     76                        'posts' => array(
     77                                'home' => array(
     78                                        'post_type' => 'page',
     79                                        'post_title' => _x( 'Homepage', 'Theme starter content' ),
     80                                        'post_content' => _x( 'Welcome home.', 'Theme starter content' ),
     81                                ),
     82                                'about-us' => array(
     83                                        'post_type' => 'page',
     84                                        'post_title' => _x( 'About Us', 'Theme starter content' ),
     85                                        'post_content' => _x( 'More than you ever wanted to know.', 'Theme starter content' ),
     86                                ),
     87                                'contact-us' => array(
     88                                        'post_type' => 'page',
     89                                        'post_title' => _x( 'Contact Us', 'Theme starter content' ),
     90                                        'post_content' => _x( 'Call us at 999-999-9999.', 'Theme starter content' ),
     91                                ),
     92                                'blog' => array(
     93                                        'post_type' => 'page',
     94                                        'post_title' => _x( 'Blog', 'Theme starter content' ),
     95                                ),
     96
     97                                'homepage-section' => array(
     98                                        'post_type' => 'page',
     99                                        'post_title' => _x( 'A homepage section', 'Theme starter content' ),
     100                                        'post_content' => _x( 'This is an example of a homepage section, which are managed in theme options.', 'Theme starter content' ),
     101                                ),
     102                        ),
     103                );
     104
     105        }
     106
     107
     108        /**
     109         * Testing passing an empty array
     110         */
     111        function test_add_theme_support_empty() {
     112                add_theme_support( 'starter-content', array() );
     113                $starter_content = get_theme_starter_content();
     114
     115                $this->assertEmpty( $starter_content );
     116        }
     117
     118        /**
     119         * Testing passing no parameter.
     120         */
     121        function test_add_theme_support_single_param() {
     122                add_theme_support( 'starter-content' );
     123                $starter_content = get_theme_starter_content();
     124
     125                $this->assertEmpty( $starter_content );
     126        }
     127
     128
     129        /**
     130         * Testing the items that have cases.
     131         *
     132         * Testing the text_credits content is problematic as the the dates won't match
     133         * so it's not included here.
     134         *
     135         * @dataProvider data_default_content_sections
     136         *
     137         */
     138        function test_default_content_sections( $content, $expected_content ) {
     139
     140                add_theme_support( 'starter-content', $content );
     141
     142                $starter_content = get_theme_starter_content();
     143
     144                $this->assertSame( $expected_content, $starter_content );
     145        }
     146
     147        /**
     148         * Dataprovider for test_default_content_sections
     149         *
     150         * @return array {
     151         *    array {
     152         *         array The content to pass to add_theme_support.
     153         *         array The expected output.
     154         *    }
     155         * }
     156         */
     157        function data_default_content_sections() {
     158
     159                $this->setup_core_content();
     160
     161                return array(
     162                        // Widgets
     163                        array(
     164                                array(
     165                                        'widgets' => array(
     166                                                'sidebar-1' => array(
     167                                                        'text_business_info',
     168                                                        'search',
     169                                                ),
     170                                        ),
     171                                ),
     172                                array(
     173                                        'widgets' => array(
     174                                                'sidebar-1' => array(
     175                                                        $this->core_content['widgets']['text_business_info'],
     176                                                        $this->core_content['widgets']['search'],
     177                                                ),
     178                                        ),
     179                                ),
     180                        ),
     181
     182                        // Nav Menus.
     183                        array(
     184                                array(
     185                                        'nav_menus' => array(
     186                                                'top' => array(
     187                                                        'name'  => 'Menu Name',
     188                                                        'items' => array(
     189                                                                'page_home',
     190                                                                'page_about',
     191                                                                'page_blog',
     192                                                                'page_contact',
     193                                                                'link_yelp',
     194                                                                'link_facebook',
     195                                                                'link_twitter',
     196                                                                'link_instagram',
     197                                                                'link_email',
     198                                                        ),
     199                                                ),
     200                                        ),
     201                                ),
     202                                array(
     203                                        'nav_menus' => array(
     204                                                'top' => array(
     205                                                        'name'  => 'Menu Name',
     206                                                        'items' => array(
     207                                                                $this->core_content['nav_menus']['page_home'],
     208                                                                $this->core_content['nav_menus']['page_about'],
     209                                                                $this->core_content['nav_menus']['page_blog'],
     210                                                                $this->core_content['nav_menus']['page_contact'],
     211                                                                $this->core_content['nav_menus']['link_yelp'],
     212                                                                $this->core_content['nav_menus']['link_facebook'],
     213                                                                $this->core_content['nav_menus']['link_twitter'],
     214                                                                $this->core_content['nav_menus']['link_instagram'],
     215                                                                $this->core_content['nav_menus']['link_email'],
     216                                                        ),
     217                                                ),
     218                                        ),
     219                                ),
     220                        ),
     221                        // Posts.
     222                        array(
     223                                array(
     224                                        'posts' => array(
     225                                                'home',
     226                                                'about-us',
     227                                                'contact-us',
     228                                                'blog',
     229                                                'homepage-section',
     230                                        ),
     231                                ),
     232                                array(
     233                                        'posts' => $this->core_content['posts'],
     234                                ),
     235                        ),
     236
     237                        // Options
     238                        array(
     239                                array(
     240                                        'options' => array(
     241                                                'show_on_front'  => 'page',
     242                                                'page_on_front'  => '{{home}}',
     243                                                'page_for_posts' => '{{blog}}',
     244                                        ),
     245                                ),
     246                                array(
     247                                        'options' => array(
     248                                                'show_on_front'  => 'page',
     249                                                'page_on_front'  => '{{home}}',
     250                                                'page_for_posts' => '{{blog}}',
     251                                        ),
     252                                ),
     253                        ),
     254
     255                        //Theme mods.
     256                        array(
     257                                array(
     258                                        'theme_mods' => array(
     259                                                'panel_1' => '{{homepage-section}}',
     260                                                'panel_2' => '{{about-us}}',
     261                                                'panel_3' => '{{blog}}',
     262                                                'panel_4' => '{{contact-us}}',
     263                                        ),
     264                                ),
     265                                array(
     266                                        'theme_mods' => array(
     267                                                'panel_1' => '{{homepage-section}}',
     268                                                'panel_2' => '{{about-us}}',
     269                                                'panel_3' => '{{blog}}',
     270                                                'panel_4' => '{{contact-us}}',
     271                                        ),
     272                                ),
     273                        ),
     274                );
     275        }
     276
     277        /**
     278         * Testing the filter with the text_credits widget.
     279         */
     280        function test_get_theme_starter_content_filter() {
     281
     282                add_theme_support( 'starter-content',
     283                        array(
     284                                'widgets' => array(
     285                                        'sidebar-1' => array(
     286                                                'text_credits',
     287                                        ),
     288                                ),
     289                        )
     290                );
     291
     292                $expected = array(
     293                        'widgets' => array(
     294                                'sidebar-1' => array(
     295                                         array(
     296                                                'text',
     297                                                 array(
     298                                                        'title' => __( 'Site Credits' ),
     299                                                    'text'  => 'Changed to a hardcoded string',
     300                                                 ),
     301                                        ),
     302                                ),
     303                        ),
     304                );
     305
     306                add_filter( 'get_theme_starter_content', array( $this, 'filter_text_credits' ) );
     307                $starter_content = get_theme_starter_content();
     308                $this->assertSame( $expected , $starter_content );
     309        }
     310
     311        /**
     312         * Filter the text_widget to remove the dynamic time.
     313         *
     314         * @param $content
     315         *
     316         * @return mixed
     317         */
     318        public function filter_text_credits( $content ) {
     319                $content['widgets']['sidebar-1'][0] = array(
     320                        'text',
     321                        array(
     322                                'title' => __( 'Site Credits' ),
     323                                'text'  => 'Changed to a hardcoded string',
     324                        ),
     325
     326                );
     327                return $content;
     328        }
     329
     330}
     331