WordPress.org

Make WordPress Core

Ticket #34093: 34093-7.diff

File 34093-7.diff, 8.3 KB (added by dwainm, 4 years ago)

Adding basic tests

  • src/wp-includes/general-template.php

     
    18631863 * no posts for the month, then it will not be displayed.
    18641864 *
    18651865 * @since 1.0.0
     1866 * @since 4.7 arguments changed to accept an associative array $args.
     1867 * @since 4.7 `get_calendar_args` filter added.
    18661868 *
    18671869 * @global wpdb      $wpdb
    18681870 * @global int       $m
     
    18711873 * @global WP_Locale $wp_locale
    18721874 * @global array     $posts
    18731875 *
    1874  * @param bool $initial Optional, default is true. Use initial calendar names.
    1875  * @param bool $echo    Optional, default is true. Set to false for return.
    1876  * @return string|void String when retrieving.
     1876 * @param array $args{
     1877 *    Optional. An array of arguments.
     1878 *    @type bool   $initial. Use initial calendar names. Default true.
     1879 *    @type bool   $echo. Should we echo the calendar. Default true.
     1880 *    @type string $post_type. The post type to be used on the calendar. Default `post`.
     1881 * }
     1882 *
     1883 * @return string|void String when $args['echo'] is set to true.
    18771884 */
    1878 function get_calendar( $initial = true, $echo = true ) {
     1885function get_calendar( $args = array() ) {
    18791886        global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
    18801887
     1888        $defaults = array(
     1889                'initial'   => true,
     1890                'echo'      => true,
     1891                'post_type' => 'post',
     1892        );
     1893
     1894        if ( ! is_array( $args ) ) {
     1895                $backwards_compatible_args = func_get_args();
     1896
     1897                if ( isset( $backwards_compatible_args[0] ) && is_bool( $backwards_compatible_args[0] ) ){
     1898                        $defaults['initial'] = $backwards_compatible_args[0];
     1899                }
     1900
     1901                if ( isset( $backwards_compatible_args[1] ) && is_bool( $backwards_compatible_args[1] ) ){
     1902                        $defaults['echo'] = $backwards_compatible_args[1];
     1903                }
     1904        }
     1905
     1906        /**
     1907         * Filter the `get_calander` function arguments before they are used.
     1908         *
     1909         * @since 4.7
     1910         * @param array $args
     1911         */
     1912        $args = apply_filters( 'get_calendar_args', wp_parse_args( $args, $defaults) );
     1913
    18811914        $key = md5( $m . $monthnum . $year );
    18821915        $cache = wp_cache_get( 'get_calendar', 'calendar' );
    18831916
    18841917        if ( $cache && is_array( $cache ) && isset( $cache[ $key ] ) ) {
    18851918                /** This filter is documented in wp-includes/general-template.php */
    1886                 $output = apply_filters( 'get_calendar', $cache[ $key ] );
     1919                $output = apply_filters( 'get_calendar', $cache[ $key ], $args );
    18871920
    1888                 if ( $echo ) {
     1921                if ( $args['echo'] ) {
    18891922                        echo $output;
    18901923                        return;
    18911924                }
     
    18971930                $cache = array();
    18981931        }
    18991932
     1933        $post_type = $args['post_type'];
     1934        if ( ! post_type_exists( $post_type ) ) {
     1935                // set the post type back to 'post' if the filter value is not a valid post type
     1936                $post_type = 'post';
     1937        }
     1938
    19001939        // Quick check. If we have no posts at all, abort!
    19011940        if ( ! $posts ) {
    1902                 $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");
     1941                $prepared_query = $wpdb->prepare( "SELECT 1 as test FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' LIMIT 1", $post_type );
     1942                $gotsome = $wpdb->get_var( $prepared_query );
    19031943                if ( ! $gotsome ) {
    19041944                        $cache[ $key ] = '';
    19051945                        wp_cache_set( 'get_calendar', $cache, 'calendar' );
     
    19401980        $last_day = date( 't', $unixmonth );
    19411981
    19421982        // Get the next and previous month and year with at least one post
    1943         $previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
     1983        $previous_prepared_query = $wpdb->prepare( "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    19441984                FROM $wpdb->posts
    19451985                WHERE post_date < '$thisyear-$thismonth-01'
    1946                 AND post_type = 'post' AND post_status = 'publish'
     1986                AND post_type = %s AND post_status = 'publish'
    19471987                        ORDER BY post_date DESC
    1948                         LIMIT 1");
    1949         $next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
     1988                        LIMIT 1" , $post_type);
     1989        $previous = $wpdb->get_row( $previous_prepared_query );
     1990
     1991        $next_prepared_query = $wpdb->prepare( "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    19501992                FROM $wpdb->posts
    19511993                WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
    1952                 AND post_type = 'post' AND post_status = 'publish'
    1953                         ORDER BY post_date ASC
    1954                         LIMIT 1");
     1994                AND post_type = %s AND post_status = 'publish'
     1995                ORDER BY post_date ASC
     1996                LIMIT 1", $post_type );
     1997        $next = $wpdb->get_row( $next_prepared_query );
    19551998
    19561999        /* translators: Calendar caption: 1: month name, 2: 4-digit year */
    19572000        $calendar_caption = _x('%1$s %2$s', 'calendar caption');
     
    19712014        }
    19722015
    19732016        foreach ( $myweek as $wd ) {
    1974                 $day_name = $initial ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );
     2017                $day_name = $args['initial'] ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );
    19752018                $wd = esc_attr( $wd );
    19762019                $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
    19772020        }
     
    20112054        $daywithpost = array();
    20122055
    20132056        // Get days with posts
    2014         $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
     2057        $dayswithposts_prepared_query = $wpdb->prepare( "SELECT DISTINCT DAYOFMONTH(post_date)
    20152058                FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
    2016                 AND post_type = 'post' AND post_status = 'publish'
    2017                 AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N);
     2059                AND post_type = %s AND post_status = 'publish'
     2060                AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", $post_type);
     2061        $dayswithposts = $wpdb->get_results( $dayswithposts_prepared_query , ARRAY_N);
     2062
    20182063        if ( $dayswithposts ) {
    20192064                foreach ( (array) $dayswithposts as $daywith ) {
    20202065                        $daywithpost[] = $daywith[0];
     
    20732118        $cache[ $key ] = $calendar_output;
    20742119        wp_cache_set( 'get_calendar', $cache, 'calendar' );
    20752120
    2076         if ( $echo ) {
    2077                 /**
    2078                  * Filters the HTML calendar output.
    2079                  *
    2080                  * @since 3.0.0
    2081                  *
    2082                  * @param string $calendar_output HTML output of the calendar.
    2083                  */
    2084                 echo apply_filters( 'get_calendar', $calendar_output );
     2121        /**
     2122         * Filters the HTML calendar output.
     2123         *
     2124         * @since 3.0.0
     2125         * @since 4.7.0 new argument $args added.
     2126         *
     2127         * @param string $calendar_output HTML output of the calendar.
     2128         * @param array $args passed into get_calender function. $args also filtered earlier in get_calender().
     2129         */
     2130        $calendar_output = apply_filters( 'get_calendar', $calendar_output, $args );
     2131
     2132        if ( $args['echo'] ) {
     2133                echo $calendar_output;
    20852134                return;
    20862135        }
    2087         /** This filter is documented in wp-includes/general-template.php */
    2088         return apply_filters( 'get_calendar', $calendar_output );
     2136
     2137        return $calendar_output;
    20892138}
    20902139
    20912140/**
  • tests/phpunit/tests/functions/getCalendar.php

     
     1<?php
     2/*
     3        $defaults = array(
     4                'initial'   => true,
     5                'echo'      => true,
     6                'post_type' => 'post',
     7        );
     8*/
     9class Tests_Get_Calendar extends WP_UnitTestCase {
     10        function setUp() {
     11                parent::setUp();
     12        }
     13        public static function wpSetUpBeforeClass( $factory ) {
     14                $factory->post->create_many( 1, array( 'post_type' => 'post', 'post_author' => '1' ) );
     15        }
     16
     17        public function test_get_calendar_echo() {
     18                $expected['echo'] = '<table id="wp-calendar">';
     19                ob_start();
     20                get_calendar( array( 'echo' => true ) );
     21                $function_output = ob_get_clean();
     22                $this->assertContains( $expected['echo'], $function_output );
     23        }
     24
     25        public function test_get_calendar_initial() {
     26                $expected['initial_false'] = '<th scope="col" title="Monday">Mon</th>';
     27                ob_start();
     28                get_calendar( array( 'initial' => false ) );
     29                $initial_false_function_output = ob_get_clean();
     30                $this->assertContains( $expected['initial_false'], $initial_false_function_output );
     31
     32                //invalidate the cache before the second function call
     33                wp_cache_delete( 'get_calendar', 'calendar' );
     34
     35                $expected['initial_true'] = '<th scope="col" title="Monday">M</th>';
     36                ob_start();
     37                get_calendar( array( 'initial' => true ) );
     38                $initial_true_function_output = ob_get_clean();
     39                $this->assertContains( $expected['initial_true'], $initial_true_function_output );
     40        }
     41}