Make WordPress Core

Ticket #22400: 22400.general-template-w-unit-tests.diff

File 22400.general-template-w-unit-tests.diff, 11.8 KB (added by MikeHansenMe, 9 years ago)

wp_get_archives refresh with unit tests

  • tests/phpunit/tests/general-template.php

     
     1<?php
     2
     3class Tests_Get_Archives extends WP_UnitTestCase {
     4
     5        /*
     6        $defaults = array(
     7                'type' => 'monthly', 'limit' => '',
     8                'format' => 'html', 'before' => '',
     9                'after' => '', 'show_post_count' => false,
     10                'echo' => 1, 'order' => 'DESC',
     11        );
     12        */
     13
     14        function setUp() {
     15                parent::setUp();       
     16                $this->factory->post->create_many( 8, array( 'post_type' => 'post', 'post_author' => '1' ) );
     17        }
     18
     19        function test_wp_get_archives_default() {
     20                $expected['default'] = "<li><a href='http://example.org/?m=" . date( 'Ym' ) . "'>" . date( 'F Y' ) . "</a></li>";
     21                $this->AssertEquals( $expected['default'], trim( wp_get_archives( array( 'echo' => false ) ) ) );
     22        }
     23
     24        function test_wp_get_archives_type() {
     25                $expected['type'] = "<li><a href='http://example.org/?m=" . date( 'Y' ) . "'>" . date( 'Y' ) . "</a></li>";
     26                $this->AssertEquals( $expected['type'], trim( wp_get_archives( array( 'echo' => false, 'type' => 'yearly' ) ) ) );
     27        }
     28
     29        function test_wp_get_archives_limit() {
     30                $expected['limit'] = <<<EOF
     31<li><a href='http://example.org/?p=26'>Post title 8</a></li>
     32        <li><a href='http://example.org/?p=25'>Post title 7</a></li>
     33        <li><a href='http://example.org/?p=24'>Post title 6</a></li>
     34        <li><a href='http://example.org/?p=23'>Post title 5</a></li>
     35        <li><a href='http://example.org/?p=22'>Post title 4</a></li>
     36EOF;
     37                $this->AssertEquals( $expected['limit'], trim( wp_get_archives( array( 'echo' => false, 'type' => 'postbypost', 'limit' => 5 ) ) ) );
     38        }
     39
     40        function test_wp_get_archives_format() {
     41                $expected['format'] = "<option value='http://example.org/?m=" . date( 'Ym' ) . "'> " . date( 'F Y' ) . " </option>";
     42                $this->AssertEquals( $expected['format'], trim( wp_get_archives( array( 'echo' => false, 'format' => 'option' ) ) ) );
     43        }
     44       
     45        function test_wp_get_archives_before_and_after() {
     46                $expected['before_and_after'] = "<div><a href='http://example.org/?m=" . date( 'Ym' ) . "'>" . date( 'F Y' ) . "</a></div>";
     47                $this->AssertEquals( $expected['before_and_after'], trim( wp_get_archives( array( 'echo' => false, 'format' => 'custom', 'before' => '<div>', 'after' => '</div>' ) ) ) );
     48        }
     49       
     50        function test_wp_get_archives_show_post_count() {
     51                $expected['show_post_count'] = "<li><a href='http://example.org/?m=" . date( 'Ym' ) . "'>" . date( 'F Y' ) . "</a>&nbsp;(8)</li>";
     52                $this->AssertEquals( $expected['show_post_count'], trim( wp_get_archives( array( 'echo' => false, 'show_post_count' => 1 ) ) ) );       
     53        }
     54
     55        function test_wp_get_archives_echo() {
     56                $expected['echo'] = "<li><a href='http://example.org/?m=" . date( 'Ym' ) . "'>" . date( 'F Y' ) . "</a></li>";
     57                ob_start();
     58                wp_get_archives( array( 'echo' => true ) );
     59                $actual = ob_get_clean();
     60                $this->AssertEquals( $expected['echo'], trim( $actual ) );
     61        }
     62       
     63        function test_wp_get_archives_order() {
     64                $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => '1', 'post_date' => '2012-10-23 19:34:42' ) );
     65                $date_n = date( "Ym" );
     66                $date_full = date( 'F Y' );
     67                $expected['order_asc'] = <<<EOF
     68<li><a href='http://example.org/?m=201210'>October 2012</a></li>
     69        <li><a href='http://example.org/?m=$date_n'>$date_full</a></li>
     70EOF;
     71                $this->AssertEquals( $expected['order_asc'], trim( wp_get_archives( array( 'echo' => false, 'order' => 'ASC' ) ) ) );
     72
     73                $expected['order_desc'] = <<<EOF
     74<li><a href='http://example.org/?m=$date_n'>$date_full</a></li>
     75        <li><a href='http://example.org/?m=201210'>October 2012</a></li>
     76EOF;
     77                $this->AssertEquals( $expected['order_desc'], trim( wp_get_archives( array( 'echo' => false, 'order' => 'DESC' ) ) ) );
     78        }
     79}
     80 No newline at end of file
  • src/wp-includes/general-template.php

     
    934934 * @param string|array $args Optional. Override defaults.
    935935 * @return string|null String when retrieving, null when displaying.
    936936 */
    937 function wp_get_archives($args = '') {
     937function wp_get_archives( $args = '' ) {
    938938        global $wpdb, $wp_locale;
    939939
    940940        $defaults = array(
     
    945945        );
    946946
    947947        $r = wp_parse_args( $args, $defaults );
    948         extract( $r, EXTR_SKIP );
    949948
    950         if ( '' == $type )
    951                 $type = 'monthly';
     949        if ( '' == $r['type'] )
     950                $r['type'] = 'monthly';
    952951
    953         if ( '' != $limit ) {
    954                 $limit = absint($limit);
    955                 $limit = ' LIMIT '.$limit;
     952        if ( '' != $r['limit'] ) {
     953                $r['limit'] = absint( $r['limit'] );
     954                $r['limit'] = ' LIMIT ' . $r['limit'];
    956955        }
    957956
    958         $order = strtoupper( $order );
     957        $order = strtoupper( $r['order'] );
    959958        if ( $order !== 'ASC' )
    960959                $order = 'DESC';
    961960
     
    973972        $archive_week_end_date_format   = 'Y/m/d';
    974973
    975974        if ( !$archive_date_format_over_ride ) {
    976                 $archive_day_date_format = get_option('date_format');
    977                 $archive_week_start_date_format = get_option('date_format');
    978                 $archive_week_end_date_format = get_option('date_format');
     975                $archive_day_date_format = get_option( 'date_format' );
     976                $archive_week_start_date_format = get_option( 'date_format' );
     977                $archive_week_end_date_format = get_option( 'date_format' );
    979978        }
    980979
    981980        $where = apply_filters( 'getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
     
    989988                wp_cache_set( 'last_changed', $last_changed, 'posts' );
    990989        }
    991990
    992         if ( 'monthly' == $type ) {
     991        $limit = $r['limit'];
     992
     993        if ( 'monthly' == $r['type'] ) {
    993994                $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit";
    994995                $key = md5( $query );
    995996                $key = "wp_get_archives:$key:$last_changed";
     
    998999                        wp_cache_set( $key, $results, 'posts' );
    9991000                }
    10001001                if ( $results ) {
    1001                         $afterafter = $after;
     1002                        $afterafter = $r['after'];
    10021003                        foreach ( (array) $results as $result ) {
    10031004                                $url = get_month_link( $result->year, $result->month );
    10041005                                /* translators: 1: month name, 2: 4-digit year */
    1005                                 $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($result->month), $result->year);
    1006                                 if ( $show_post_count )
    1007                                         $after = '&nbsp;('.$result->posts.')' . $afterafter;
    1008                                 $output .= get_archives_link($url, $text, $format, $before, $after);
     1006                                $text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $result->month ), $result->year );
     1007                                if ( $r['show_post_count'] )
     1008                                        $r['after'] = '&nbsp;('.$result->posts.')' . $afterafter;
     1009                                $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
    10091010                        }
    10101011                }
    1011         } elseif ('yearly' == $type) {
     1012        } elseif ( 'yearly' == $r['type'] ) {
    10121013                $query = "SELECT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date $order $limit";
    10131014                $key = md5( $query );
    10141015                $key = "wp_get_archives:$key:$last_changed";
     
    10171018                        wp_cache_set( $key, $results, 'posts' );
    10181019                }
    10191020                if ( $results ) {
    1020                         $afterafter = $after;
     1021                        $afterafter = $r['after'];
    10211022                        foreach ( (array) $results as $result) {
    1022                                 $url = get_year_link($result->year);
    1023                                 $text = sprintf('%d', $result->year);
    1024                                 if ($show_post_count)
    1025                                         $after = '&nbsp;('.$result->posts.')' . $afterafter;
    1026                                 $output .= get_archives_link($url, $text, $format, $before, $after);
     1023                                $url = get_year_link( $result->year );
     1024                                $text = sprintf( '%d', $result->year );
     1025                                if ( $r['show_post_count'] )
     1026                                        $r['after'] = '&nbsp;('.$result->posts.')' . $afterafter;
     1027                                $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
    10271028                        }
    10281029                }
    1029         } elseif ( 'daily' == $type ) {
     1030        } elseif ( 'daily' == $r['type'] ) {
    10301031                $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date $order $limit";
    10311032                $key = md5( $query );
    10321033                $key = "wp_get_archives:$key:$last_changed";
     
    10361037                        wp_cache_set( $key, $results, 'posts' );
    10371038                }
    10381039                if ( $results ) {
    1039                         $afterafter = $after;
     1040                        $afterafter = $r['after'];
    10401041                        foreach ( (array) $results as $result ) {
    1041                                 $url    = get_day_link($result->year, $result->month, $result->dayofmonth);
    1042                                 $date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth);
    1043                                 $text = mysql2date($archive_day_date_format, $date);
    1044                                 if ($show_post_count)
    1045                                         $after = '&nbsp;('.$result->posts.')'.$afterafter;
    1046                                 $output .= get_archives_link($url, $text, $format, $before, $after);
     1042                                $url    = get_day_link( $result->year, $result->month, $result->dayofmonth );
     1043                                $date = sprintf( '%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth );
     1044                                $text = mysql2date( $archive_day_date_format, $date );
     1045                                if ( $r['show_post_count'] )
     1046                                        $after = '&nbsp;(' . $result->posts . ')' . $afterafter;
     1047                                $output .= get_archives_link( $url, $text, $format, $before, $after );
    10471048                        }
    10481049                }
    1049         } elseif ( 'weekly' == $type ) {
     1050        } elseif ( 'weekly' == $r['type'] ) {
    10501051                $week = _wp_mysql_week( '`post_date`' );
    10511052                $query = "SELECT DISTINCT $week AS `week`, YEAR( `post_date` ) AS `yr`, DATE_FORMAT( `post_date`, '%Y-%m-%d' ) AS `yyyymmdd`, count( `ID` ) AS `posts` FROM `$wpdb->posts` $join $where GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` $order $limit";
    10521053                $key = md5( $query );
     
    10561057                        wp_cache_set( $key, $results, 'posts' );
    10571058                }
    10581059                $arc_w_last = '';
    1059                 $afterafter = $after;
     1060                $afterafter = $r['after'];
    10601061                if ( $results ) {
    10611062                                foreach ( (array) $results as $result ) {
    10621063                                        if ( $result->week != $arc_w_last ) {
    10631064                                                $arc_year = $result->yr;
    10641065                                                $arc_w_last = $result->week;
    1065                                                 $arc_week = get_weekstartend($result->yyyymmdd, get_option('start_of_week'));
    1066                                                 $arc_week_start = date_i18n($archive_week_start_date_format, $arc_week['start']);
    1067                                                 $arc_week_end = date_i18n($archive_week_end_date_format, $arc_week['end']);
    1068                                                 $url  = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&amp;', '=', $result->week);
     1066                                                $arc_week = get_weekstartend( $result->yyyymmdd, get_option( 'start_of_week' ) );
     1067                                                $arc_week_start = date_i18n( $archive_week_start_date_format, $arc_week['start'] );
     1068                                                $arc_week_end = date_i18n( $archive_week_end_date_format, $arc_week['end'] );
     1069                                                $url  = sprintf( '%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&amp;', '=', $result->week );
    10691070                                                $text = $arc_week_start . $archive_week_separator . $arc_week_end;
    1070                                                 if ($show_post_count)
    1071                                                         $after = '&nbsp;('.$result->posts.')'.$afterafter;
    1072                                                 $output .= get_archives_link($url, $text, $format, $before, $after);
     1071                                                if ( $r['show_post_count'] )
     1072                                                        $after = '&nbsp;(' . $result->posts . ')' . $afterafter;
     1073                                                $output .= get_archives_link( $url, $text, $format, $before, $after );
    10731074                                        }
    10741075                                }
    10751076                }
    1076         } elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) {
    1077                 $orderby = ('alpha' == $type) ? 'post_title ASC ' : 'post_date DESC ';
     1077        } elseif ( ( 'postbypost' == $r['type'] ) || ('alpha' == $r['type'] ) ) {
     1078                $orderby = ( 'alpha' == $r['type'] ) ? 'post_title ASC ' : 'post_date DESC ';
    10781079                $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
    10791080                $key = md5( $query );
    10801081                $key = "wp_get_archives:$key:$last_changed";
     
    10921093                                        } else {
    10931094                                                $text = $result->ID;
    10941095                                        }
    1095                                         $output .= get_archives_link($url, $text, $format, $before, $after);
     1096                                        $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
    10961097                                }
    10971098                        }
    10981099                }
    10991100        }
    1100         if ( $echo )
     1101        if ( $r['echo'] )
    11011102                echo $output;
    11021103        else
    11031104                return $output;