Make WordPress Core


Ignore:
Timestamp:
10/16/2020 02:41:15 AM (4 years ago)
Author:
peterwilsoncc
Message:

Taxonomy: Fix warnings thrown by custom term count callbacks.

Add a use to a closure to avoid an undefined variable throwing a warning. Adds unit tests to ensure the custom callbacks run as expected when defined.

Follow up to [49141].
Props ocean90, dd32.
Fixes #40351.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/taxonomy.php

    r48939 r49171  
    55 */
    66class Tests_Taxonomy extends WP_UnitTestCase {
     7
     8    /**
     9     * Number of times full count callback has been called.
     10     *
     11     * @var int
     12     */
     13    public $full_count_cb_called = 0;
     14
     15    /**
     16     * Number of times partial count callback has been called.
     17     *
     18     * @var int
     19     */
     20    public $partial_count_cb_called = 0;
     21
    722    function test_get_post_taxonomies() {
    823        $this->assertSame( array( 'category', 'post_tag', 'post_format' ), get_object_taxonomies( 'post' ) );
     
    10311046        $this->assertSame( get_option( 'default_term_' . $tax ), false );
    10321047    }
     1048
     1049    /**
     1050     * Ensure custom callbacks are used when registered.
     1051     *
     1052     * @covers register_taxonomy
     1053     * @ticket 40351
     1054     */
     1055    function test_register_taxonomy_counting_callbacks() {
     1056        $post_id = self::factory()->post->create();
     1057
     1058        register_taxonomy(
     1059            'wp_tax_40351_full_only',
     1060            'post',
     1061            array(
     1062                'update_count_callback' => array( $this, 'cb_register_taxonomy_full_count_callback' ),
     1063            )
     1064        );
     1065        $full_term    = self::factory()->term->create_and_get(
     1066            array(
     1067                'taxonomy' => 'wp_tax_40351_full_only',
     1068            )
     1069        );
     1070        $full_term_id = $full_term->term_id;
     1071
     1072        register_taxonomy(
     1073            'wp_tax_40351_partial_only',
     1074            'post',
     1075            array(
     1076                'update_count_by_callback' => array( $this, 'cb_register_taxonomy_partial_count_callback' ),
     1077            )
     1078        );
     1079        $partial_term    = self::factory()->term->create_and_get(
     1080            array(
     1081                'taxonomy' => 'wp_tax_40351_partial_only',
     1082            )
     1083        );
     1084        $partial_term_id = $partial_term->term_id;
     1085
     1086        register_taxonomy(
     1087            'wp_tax_40351_both',
     1088            'post',
     1089            array(
     1090                'update_count_callback'    => array( $this, 'cb_register_taxonomy_full_count_callback' ),
     1091                'update_count_by_callback' => array( $this, 'cb_register_taxonomy_partial_count_callback' ),
     1092            )
     1093        );
     1094        $both_term      = self::factory()->term->create_and_get(
     1095            array(
     1096                'taxonomy' => 'wp_tax_40351_both',
     1097            )
     1098        );
     1099        $both_term_id   = $both_term->term_id;
     1100        $both_term_ttid = $both_term->term_taxonomy_id;
     1101
     1102        wp_set_post_terms( $post_id, $full_term_id, 'wp_tax_40351_full_only' );
     1103        $this->assertSame( 0, $this->partial_count_cb_called );
     1104        $this->assertSame( 1, $this->full_count_cb_called );
     1105
     1106        wp_set_post_terms( $post_id, $partial_term_id, 'wp_tax_40351_partial_only' );
     1107        $this->assertSame( 1, $this->partial_count_cb_called );
     1108        $this->assertSame( 1, $this->full_count_cb_called );
     1109
     1110        wp_set_post_terms( $post_id, $both_term_id, 'wp_tax_40351_both' );
     1111        $this->assertSame( 2, $this->partial_count_cb_called );
     1112        $this->assertSame( 1, $this->full_count_cb_called );
     1113
     1114        // Force a full recount `$both_term` to ensure callback is called.
     1115        wp_update_term_count( $both_term_ttid, 'wp_tax_40351_both' );
     1116        $this->assertSame( 2, $this->full_count_cb_called );
     1117    }
     1118
     1119    /**
     1120     * Custom full count callback for `test_register_taxonomy_counting_callbacks()`.
     1121     *
     1122     * For the purpose of this test no database modifications are required, therefore
     1123     * the parameters passed are unused.
     1124     *
     1125     * @param int|array $tt_ids   The term_taxonomy_id of the terms.
     1126     * @param string    $taxonomy The context of the term.
     1127     */
     1128    function cb_register_taxonomy_full_count_callback( $tt_ids, $taxonomy ) {
     1129        $this->full_count_cb_called++;
     1130    }
     1131
     1132    /**
     1133     * Custom partial count callback for `test_register_taxonomy_counting_callbacks()`.
     1134     *
     1135     * For the purpose of this test no database modifications are required, therefore
     1136     * the parameters passed are unused.
     1137     *
     1138     * @param int|array $tt_ids    The term_taxonomy_id of the terms.
     1139     * @param string    $taxonomy  The context of the term.
     1140     * @param int       $modify_by By how many the term count is to be modified.
     1141     */
     1142    function cb_register_taxonomy_partial_count_callback( $tt_ids, $taxonomy, $modify_by ) {
     1143        $this->partial_count_cb_called++;
     1144    }
    10331145}
Note: See TracChangeset for help on using the changeset viewer.