Make WordPress Core

Ticket #31078: 31078.6.diff

File 31078.6.diff, 19.7 KB (added by obenland, 9 years ago)

Adds some unit tests

  • src/wp-includes/deprecated.php

     
    36203620
    36213621        return $headers;
    36223622}
     3623
     3624/**
     3625 * Formerly used to display or retrieve page title for all areas of blog.
     3626 *
     3627 * By default, the page title will display the separator before the page title,
     3628 * so that the blog title will be before the page title. This is not good for
     3629 * title display, since the blog title shows up on most tabs and not what is
     3630 * important, which is the page that the user is looking at.
     3631 *
     3632 * There are also SEO benefits to having the blog title after or to the 'right'
     3633 * or the page title. However, it is mostly common sense to have the blog title
     3634 * to the right with most browsers supporting tabs. You can achieve this by
     3635 * using the seplocation parameter and setting the value to 'right'. This change
     3636 * was introduced around 2.5.0, in case backwards compatibility of themes is
     3637 * important.
     3638 *
     3639 * @since 1.0.0
     3640 * @deprecated 4.4.0
     3641 * @deprecated Use add_theme_support( 'title-tag' );
     3642 *
     3643 * @param string $sep Optional, default is '»'. How to separate the various items within the page title.
     3644 * @param bool $display Optional, default is true. Whether to display or retrieve title.
     3645 * @param string $seplocation Optional. Direction to display title, 'right'.
     3646 * @return string|null String on retrieve, null when displaying.
     3647 */
     3648function wp_title( $sep = '»', $display = true, $seplocation = '' ) {
     3649        _deprecated_function( __FUNCTION__, '4.4', 'add_theme_support( \'title-tag\' )' );
     3650
     3651        global $wp_locale;
     3652
     3653        $m        = get_query_var( 'm' );
     3654        $year     = get_query_var( 'year' );
     3655        $monthnum = get_query_var( 'monthnum' );
     3656        $day      = get_query_var( 'day' );
     3657        $search   = get_query_var( 's' );
     3658        $title    = '';
     3659
     3660        $t_sep = '%WP_TITILE_SEP%'; // Temporary separator, for accurate flipping, if necessary
     3661
     3662        // If there is a post
     3663        if ( is_single() || ( is_home() && ! is_front_page() ) || ( is_page() && ! is_front_page() ) ) {
     3664                $title = single_post_title( '', false );
     3665        }
     3666
     3667        // If there's a post type archive
     3668        if ( is_post_type_archive() ) {
     3669                $post_type = get_query_var( 'post_type' );
     3670                if ( is_array( $post_type ) ) {
     3671                        $post_type = reset( $post_type );
     3672                }
     3673                $post_type_object = get_post_type_object( $post_type );
     3674                if ( ! $post_type_object->has_archive ) {
     3675                        $title = post_type_archive_title( '', false );
     3676                }
     3677        }
     3678
     3679        // If there's a category or tag
     3680        if ( is_category() || is_tag() ) {
     3681                $title = single_term_title( '', false );
     3682        }
     3683
     3684        // If there's a taxonomy
     3685        if ( is_tax() ) {
     3686                $term = get_queried_object();
     3687                if ( $term ) {
     3688                        $tax   = get_taxonomy( $term->taxonomy );
     3689                        $title = single_term_title( $tax->labels->name . $t_sep, false );
     3690                }
     3691        }
     3692
     3693        // If there's an author
     3694        if ( is_author() && ! is_post_type_archive() ) {
     3695                $author = get_queried_object();
     3696                if ( $author ) {
     3697                        $title = $author->display_name;
     3698                }
     3699        }
     3700
     3701        // Post type archives with has_archive should override terms.
     3702        if ( is_post_type_archive() && $post_type_object->has_archive ) {
     3703                $title = post_type_archive_title( '', false );
     3704        }
     3705
     3706        // If there's a month
     3707        if ( is_archive() && ! empty( $m ) ) {
     3708                $my_year  = substr( $m, 0, 4 );
     3709                $my_month = $wp_locale->get_month( substr( $m, 4, 2 ) );
     3710                $my_day   = intval( substr( $m, 6, 2 ) );
     3711                $title    = $my_year . ( $my_month ? $t_sep . $my_month : '' ) . ( $my_day ? $t_sep . $my_day : '' );
     3712        }
     3713
     3714        // If there's a year
     3715        if ( is_archive() && ! empty( $year ) ) {
     3716                $title = $year;
     3717                if ( ! empty( $monthnum ) ) {
     3718                        $title .= $t_sep . $wp_locale->get_month( $monthnum );
     3719                }
     3720                if ( ! empty( $day ) ) {
     3721                        $title .= $t_sep . zeroise( $day, 2 );
     3722                }
     3723        }
     3724
     3725        // If it's a search
     3726        if ( is_search() ) {
     3727                /* translators: 1: separator, 2: search phrase */
     3728                $title = sprintf( __( 'Search Results %1$s %2$s' ), $t_sep, strip_tags( $search ) );
     3729        }
     3730
     3731        // If it's a 404 page
     3732        if ( is_404() ) {
     3733                $title = __( 'Page not found' );
     3734        }
     3735
     3736        $prefix = '';
     3737        if ( ! empty( $title ) ) {
     3738                $prefix = " $sep ";
     3739        }
     3740
     3741        /**
     3742         * Filter the parts of the page title.
     3743         *
     3744         * @since 4.0.0
     3745         *
     3746         * @param array $title_array Parts of the page title.
     3747         */
     3748        $title_array = apply_filters( 'wp_title_parts', explode( $t_sep, $title ) );
     3749
     3750        // Determines position of the separator and direction of the breadcrumb
     3751        if ( 'right' == $seplocation ) { // sep on right, so reverse the order
     3752                $title_array = array_reverse( $title_array );
     3753                $title       = implode( " $sep ", $title_array ) . $prefix;
     3754        } else {
     3755                $title = $prefix . implode( " $sep ", $title_array );
     3756        }
     3757
     3758        /**
     3759         * Filter the text of the page title.
     3760         *
     3761         * @since 2.0.0
     3762         *
     3763         * @param string $title Page title.
     3764         * @param string $sep Title separator.
     3765         * @param string $seplocation Location of the separator (left or right).
     3766         */
     3767        $title = apply_filters( 'wp_title', $title, $sep, $seplocation );
     3768
     3769        // Send it out
     3770        if ( $display ) {
     3771                echo $title;
     3772        } else {
     3773                return $title;
     3774        }
     3775}
  • src/wp-includes/general-template.php

     
    779779 *
    780780 * @ignore
    781781 * @since 4.1.0
     782 * @since 4.4.0 Improved title output replaced `wp_title()`.
    782783 * @access private
    783784 *
    784  * @see wp_title()
     785 * @global int $page
     786 * @global int $paged
    785787 */
    786788function _wp_render_title_tag() {
    787789        if ( ! current_theme_supports( 'title-tag' ) ) {
    788790                return;
    789791        }
    790792
    791         // This can only work internally on wp_head.
    792         if ( ! did_action( 'wp_head' ) && ! doing_action( 'wp_head' ) ) {
     793        /**
     794         * Allows to short-circuit the title generation.
     795         *
     796         * @since 4.4.0
     797         *
     798         * @param string $title The document title. Default empty string.
     799         */
     800        $title = apply_filters( 'title_tag_pre', '' );
     801        if ( ! empty( $title ) ) {
     802                echo "<title>$title</title>\n";
    793803                return;
    794804        }
    795805
    796         echo '<title>' . wp_title( '|', false, 'right' ) . "</title>\n";
    797 }
     806        global $page, $paged;
    798807
    799 /**
    800  * Display or retrieve page title for all areas of blog.
    801  *
    802  * By default, the page title will display the separator before the page title,
    803  * so that the blog title will be before the page title. This is not good for
    804  * title display, since the blog title shows up on most tabs and not what is
    805  * important, which is the page that the user is looking at.
    806  *
    807  * There are also SEO benefits to having the blog title after or to the 'right'
    808  * or the page title. However, it is mostly common sense to have the blog title
    809  * to the right with most browsers supporting tabs. You can achieve this by
    810  * using the seplocation parameter and setting the value to 'right'. This change
    811  * was introduced around 2.5.0, in case backwards compatibility of themes is
    812  * important.
    813  *
    814  * @since 1.0.0
    815  *
    816  * @global WP_Locale $wp_locale
    817  * @global int       $page
    818  * @global int       $paged
    819  *
    820  * @param string $sep         Optional, default is '&raquo;'. How to separate the various items within the page title.
    821  * @param bool   $display     Optional, default is true. Whether to display or retrieve title.
    822  * @param string $seplocation Optional. Direction to display title, 'right'.
    823  * @return string|void String on retrieve.
    824  */
    825 function wp_title( $sep = '&raquo;', $display = true, $seplocation = '' ) {
    826         global $wp_locale, $page, $paged;
     808        $title = array(
     809                'title' => '',
     810        );
    827811
    828         $m = get_query_var('m');
    829         $year = get_query_var('year');
    830         $monthnum = get_query_var('monthnum');
    831         $day = get_query_var('day');
    832         $search = get_query_var('s');
    833         $title = '';
     812        if ( is_home() && is_front_page() ) {
     813                $title['title'] = get_bloginfo( 'name', 'display' );
    834814
    835         $t_sep = '%WP_TITILE_SEP%'; // Temporary separator, for accurate flipping, if necessary
     815                // If we're on the blog page and that page is not the homepage, use the container page's title.
     816        } elseif ( is_home() && ! is_front_page() ) {
     817                $posts_page = get_post( get_option( 'page_for_posts' ) );
     818                if ( $posts_page instanceof WP_Post ) {
     819                        $title['title'] = $posts_page->post_title;
     820                }
    836821
    837         // If there is a post
    838         if ( is_single() || ( is_home() && !is_front_page() ) || ( is_page() && !is_front_page() ) ) {
    839                 $title = single_post_title( '', false );
    840         }
     822                // If it's a single page, that is designated as the homepage.
     823        } elseif ( ! is_home() && is_front_page() ) {
     824                $title['title'] = single_post_title( '', false );
    841825
    842         // If there's a post type archive
    843         if ( is_post_type_archive() ) {
    844                 $post_type = get_query_var( 'post_type' );
    845                 if ( is_array( $post_type ) )
    846                         $post_type = reset( $post_type );
    847                 $post_type_object = get_post_type_object( $post_type );
    848                 if ( ! $post_type_object->has_archive )
    849                         $title = post_type_archive_title( '', false );
    850         }
     826                // If we're on a post / page.
     827        } elseif ( is_singular() ) {
     828                $title['title'] = single_post_title( '', false );
    851829
    852         // If there's a category or tag
    853         if ( is_category() || is_tag() ) {
    854                 $title = single_term_title( '', false );
    855         }
     830                // If we're on a category or tag or taxonomy archive.
     831        } elseif ( is_category() || is_tag() || is_tax() ) {
     832                $title['title'] = single_term_title( '', false );
    856833
    857         // If there's a taxonomy
    858         if ( is_tax() ) {
    859                 $term = get_queried_object();
    860                 if ( $term ) {
    861                         $tax = get_taxonomy( $term->taxonomy );
    862                         $title = single_term_title( $tax->labels->name . $t_sep, false );
    863                 }
    864         }
     834                // If it's a search.
     835        } elseif ( is_search() ) {
     836                /* translators: 1: search phrase */
     837                $title['title'] = sprintf( __( 'Search Results for &#8220;%1$s&#8221;' ), get_search_query() );
    865838
    866         // If there's an author
    867         if ( is_author() && ! is_post_type_archive() ) {
    868                 $author = get_queried_object();
    869                 if ( $author )
    870                         $title = $author->display_name;
    871         }
     839                // If we're on an author archive.
     840        } elseif ( is_author() && $author = get_queried_object() ) {
     841                $title['title'] = $author->display_name;
    872842
    873         // Post type archives with has_archive should override terms.
    874         if ( is_post_type_archive() && $post_type_object->has_archive )
    875                 $title = post_type_archive_title( '', false );
     843                // If we're on a post type archive.
     844        } elseif ( is_post_type_archive() ) {
     845                $title['title'] = post_type_archive_title( '', false );
    876846
    877         // If there's a month
    878         if ( is_archive() && !empty($m) ) {
    879                 $my_year = substr($m, 0, 4);
    880                 $my_month = $wp_locale->get_month(substr($m, 4, 2));
    881                 $my_day = intval(substr($m, 6, 2));
    882                 $title = $my_year . ( $my_month ? $t_sep . $my_month : '' ) . ( $my_day ? $t_sep . $my_day : '' );
    883         }
     847        } elseif ( is_year() ) {
     848                $title['title'] = get_the_date( _x( 'Y', 'yearly archives date format' ) );
    884849
    885         // If there's a year
    886         if ( is_archive() && !empty($year) ) {
    887                 $title = $year;
    888                 if ( !empty($monthnum) )
    889                         $title .= $t_sep . $wp_locale->get_month($monthnum);
    890                 if ( !empty($day) )
    891                         $title .= $t_sep . zeroise($day, 2);
    892         }
     850        } elseif ( is_month() ) {
     851                $title['title'] = get_the_date( _x( 'F Y', 'monthly archives date format' ) );
     852
     853                // If it's a date archive.
     854        } elseif ( is_day() ) {
     855                $title['title'] = get_the_date();
    893856
    894         // If it's a search
    895         if ( is_search() ) {
    896                 /* translators: 1: separator, 2: search phrase */
    897                 $title = sprintf(__('Search Results %1$s %2$s'), $t_sep, strip_tags($search));
     857                // If it's a 404 page.
     858        } elseif ( is_404() ) {
     859                $title['title'] = __( 'Page not found' );
    898860        }
    899861
    900         // If it's a 404 page
    901         if ( is_404() ) {
    902                 $title = __('Page not found');
     862        // Add a page number if necessary:
     863        if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
     864                $title['page'] = sprintf( __( 'Page %s' ), max( $paged, $page ) );
    903865        }
    904866
    905         $prefix = '';
    906         if ( !empty($title) )
    907                 $prefix = " $sep ";
     867        // Append the description or site title to give context.
     868        if ( is_home() && is_front_page() ) {
     869                $title['tagline'] = get_bloginfo( 'description', 'display' );
     870        } else {
     871                $title['site'] = get_bloginfo( 'name', 'display' );
     872        }
    908873
    909874        /**
    910          * Filter the parts of the page title.
     875         * Filters the separator for the document title.
    911876         *
    912          * @since 4.0.0
     877         * @since 4.4.0
    913878         *
    914          * @param array $title_array Parts of the page title.
     879         * @param string $sep The separator.
    915880         */
    916         $title_array = apply_filters( 'wp_title_parts', explode( $t_sep, $title ) );
    917 
    918         // Determines position of the separator and direction of the breadcrumb
    919         if ( 'right' == $seplocation ) { // sep on right, so reverse the order
    920                 $title_array = array_reverse( $title_array );
    921                 $title = implode( " $sep ", $title_array ) . $prefix;
    922         } else {
    923                 $title = $prefix . implode( " $sep ", $title_array );
    924         }
    925 
    926         if ( current_theme_supports( 'title-tag' ) && ! is_feed() ) {
    927                 $title .= get_bloginfo( 'name', 'display' );
    928 
    929                 $site_description = get_bloginfo( 'description', 'display' );
    930                 if ( $site_description && ( is_home() || is_front_page() ) ) {
    931                         $title .= " $sep $site_description";
    932                 }
    933 
    934                 if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
    935                         $title .= " $sep " . sprintf( __( 'Page %s' ), max( $paged, $page ) );
    936                 }
    937         }
     881        $sep = apply_filters( 'title_tag_separator', '-' );
    938882
    939883        /**
    940          * Filter the text of the page title.
     884         * Filters the parts of the document title.
    941885         *
    942          * @since 2.0.0
     886         * @since 4.4.0
     887         *
     888         * @param array $title {
     889         *     The document title parts.
    943890         *
    944          * @param string $title       Page title.
    945          * @param string $sep         Title separator.
    946          * @param string $seplocation Location of the separator (left or right).
     891         *     @type string $title   Title of the viewed page.
     892         *     @type string $page    Optional. Page number if paginated.
     893         *     @type string $tagline Optional. Site description when on home page.
     894         *     @type string $site    Optional. Site title when not on home page.
     895         * }
    947896         */
    948         $title = apply_filters( 'wp_title', $title, $sep, $seplocation );
     897        $title = apply_filters( 'title_tag_parts', $title );
    949898
    950         // Send it out
    951         if ( $display )
    952                 echo $title;
    953         else
    954                 return $title;
     899        foreach ( array( 'wptexturize', 'convert_chars', 'esc_html', 'capital_P_dangit' ) as $filter ) {
     900                $title = array_map( $filter, $title );
     901        }
     902        $title = implode( " $sep ", array_filter( $title ) );
    955903
     904        echo "<title>$title</title>\n";
    956905}
    957906
    958907/**
  • src/wp-includes/theme.php

     
    17521752        if ( !isset( $_wp_theme_features[$feature] ) )
    17531753                return false;
    17541754
    1755         if ( 'title-tag' == $feature ) {
    1756                 // Don't confirm support unless called internally.
    1757                 $trace = debug_backtrace();
    1758                 if ( ! in_array( $trace[1]['function'], array( '_wp_render_title_tag', 'wp_title' ) ) ) {
    1759                         return false;
    1760                 }
    1761         }
    1762 
    17631755        // If no args passed then no extra checks need be performed
    17641756        if ( func_num_args() <= 1 )
    17651757                return true;
  • tests/phpunit/tests/template/document-title.php

     
     1<?php
     2
     3/**
     4 * A set of unit tests for functions in wp-includes/general-template.php
     5 *
     6 * @group template
     7 * @group document-title
     8 */
     9class Tests_Document_Title extends WP_UnitTestCase {
     10
     11        function setUp() {
     12                parent::setUp();
     13
     14                add_action( 'after_setup_theme', array( $this, '_add_title_tag_support' ) );
     15
     16                $this->category_id = $this->factory->category->create( array(
     17                        'name' => 'test_category',
     18                ) );
     19
     20                $this->author_id = $this->factory->user->create( array(
     21                        'role'        => 'author',
     22                        'user_login'  => 'test_author',
     23                        'description' => 'test_author',
     24                ) );
     25
     26                $this->post_id = $this->factory->post->create( array(
     27                        'post_author'  => $this->author_id,
     28                        'post_status'  => 'publish',
     29                        'post_content' => rand_str(),
     30                        'post_title'   => 'test_title',
     31                        'post_type'    => 'post',
     32                        'post_date'    => '2015-09-22 18:52:17',
     33                        'category'     => $this->category_id,
     34                ) );
     35
     36                setup_postdata( get_post( $this->post_id ) );
     37        }
     38
     39        function tearDown() {
     40                wp_reset_postdata();
     41                parent::tearDown();
     42        }
     43
     44        function _add_title_tag_support() {
     45                add_theme_support( 'title-tag' );
     46        }
     47
     48        function test_short_circuiting_title() {
     49                $this->go_to( '/' );
     50
     51                add_filter( 'title_tag_pre', array( $this, '_short_circuit_title' ) );
     52
     53                $this->expectOutputString( "<title>A Wild Title</title>\n" );
     54                _wp_render_title_tag();
     55        }
     56
     57        function _short_circuit_title( $title ) {
     58                return 'A Wild Title';
     59        }
     60
     61        function test_front_page_title() {
     62                $this->go_to( '/' );
     63
     64                add_filter( 'title_tag_parts', array( $this, '_home_title_parts' ) );
     65
     66                $this->expectOutputString( "<title>Test Blog - Just another WordPress site</title>\n" );
     67                _wp_render_title_tag();
     68        }
     69
     70        function _home_title_parts( $parts ) {
     71                $this->assertFalse( array_key_exists( 'site', $parts ) );
     72                $this->assertTrue( array_key_exists( 'tagline', $parts ) );
     73
     74                return $parts;
     75        }
     76
     77        function test_singular_title() {
     78                $this->go_to( '?p=' . $this->post_id );
     79
     80                $this->expectOutputString( "<title>test_title - Test Blog</title>\n" );
     81                _wp_render_title_tag();
     82        }
     83
     84        function test_category_title() {
     85                $this->go_to( '?cat=' . $this->category_id );
     86
     87                $this->expectOutputString( "<title>test_category - Test Blog</title>\n" );
     88                _wp_render_title_tag();
     89        }
     90
     91        function test_search_title() {
     92                $this->go_to( '?s=test_title' );
     93
     94                $this->expectOutputString( "<title>Search Results for &#8220;test_title&#8221; - Test Blog</title>\n" );
     95                _wp_render_title_tag();
     96        }
     97
     98        function test_author_title() {
     99                $this->go_to( '?author=' . $this->author_id );
     100
     101                $this->expectOutputString( "<title>test_author - Test Blog</title>\n" );
     102                _wp_render_title_tag();
     103        }
     104
     105        function test_post_type_title() {
     106                register_post_type( 'cpt', array(
     107                        'public'      => true,
     108                        'has_archive' => true,
     109                        'labels'      => array(
     110                                'name' => 'test_cpt',
     111                        ),
     112                ) );
     113
     114                $this->factory->post->create( array(
     115                        'post_type' => 'cpt',
     116                ) );
     117
     118                $this->go_to( '?post_type=cpt' );
     119
     120                $this->expectOutputString( "<title>test_cpt - Test Blog</title>\n" );
     121                _wp_render_title_tag();
     122        }
     123
     124        function test_year_title() {
     125                $this->go_to( '?year=2015' );
     126
     127                $this->expectOutputString( "<title>2015 - Test Blog</title>\n" );
     128                _wp_render_title_tag();
     129        }
     130
     131        function test_month_title() {
     132                $this->go_to( '?monthnum=09' );
     133
     134                $this->expectOutputString( "<title>September 2015 - Test Blog</title>\n" );
     135                _wp_render_title_tag();
     136        }
     137
     138        function test_day_title() {
     139                $this->go_to( '?day=22' );
     140
     141                $this->expectOutputString( "<title>September 22, 2015 - Test Blog</title>\n" );
     142                _wp_render_title_tag();
     143        }
     144
     145        function test_404_title() {
     146                $this->go_to( '?m=404' );
     147
     148                $this->expectOutputString( "<title>Page not found - Test Blog</title>\n" );
     149                _wp_render_title_tag();
     150        }
     151
     152        function test_paged_title() {
     153                $this->go_to( '?page=4' );
     154
     155                $this->expectOutputString( "<title>Test Blog - Page 4 - Just another WordPress site</title>\n" );
     156                _wp_render_title_tag();
     157        }
     158
     159        function test_paged_post_title() {
     160                $this->go_to( '?paged=4&p=' . $this->post_id );
     161
     162                $this->expectOutputString( "<title>test_title - Page 4 - Test Blog</title>\n" );
     163                _wp_render_title_tag();
     164        }
     165
     166        function test_rearrange_title_parts() {
     167                $this->go_to( '?p=' . $this->post_id );
     168
     169                add_filter( 'title_tag_parts', array( $this, '_rearrange_title_parts' ) );
     170
     171                $this->expectOutputString( "<title>Test Blog - test_title</title>\n" );
     172                _wp_render_title_tag();
     173        }
     174
     175        function _rearrange_title_parts( $parts ) {
     176                $parts = array(
     177                        $parts['site'],
     178                        $parts['title'],
     179                );
     180
     181                return $parts;
     182        }
     183
     184        function test_change_title_separator() {
     185                $this->go_to( '?p=' . $this->post_id );
     186
     187                add_filter( 'title_tag_separator', array( $this, '_change_title_separator' ) );
     188
     189                $this->expectOutputString( "<title>test_title %% Test Blog</title>\n" );
     190                _wp_render_title_tag();
     191        }
     192
     193        function _change_title_separator( $sep ) {
     194                return '%%';
     195        }
     196}