Make WordPress Core


Ignore:
Timestamp:
12/28/2022 02:07:16 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Tests: Bring some consistency to creating and updating objects in factory classes.

In various unit test factory classes, some of the create_object() and update_object() methods returned a WP_Error object on failure, while a few others were documented to do so, but did not in practice, instead returning the value 0 or false, or not accounting for a failure at all.

This commit aims to handle this in a consistent way by updating the methods to always return the object ID on success and a WP_Error object on failure.

Includes:

  • Updating and correcting the relevant documentation parts.
  • Adding missing documentation and @since tags in some classes.
  • Renaming some variables to clarify that it is the object ID which is passed around, not the object itself.

Follow-up to [760/tests], [838/tests], [922/tests], [948/tests], [985/tests], [27178], [32659], [34855], [37563], [40968], [44497], [46262].

See #56793.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-term.php

    r51298 r55019  
    77 * as a way to indicate expected return values from the given factory methods.
    88 *
    9  * @method int create( $args = array(), $generation_definitions = null )
    10  * @method WP_Term create_and_get( $args = array(), $generation_definitions = null )
    11  * @method int[] create_many( $count, $args = array(), $generation_definitions = null )
     9 * @method int|WP_Error          create( $args = array(), $generation_definitions = null )
     10 * @method WP_Term|WP_Error|null create_and_get( $args = array(), $generation_definitions = null )
     11 * @method (int|WP_Error)[]      create_many( $count, $args = array(), $generation_definitions = null )
    1212 */
    1313class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
     
    2929         * Creates a term object.
    3030         *
    31          * @param array $args Array or string of arguments for inserting a term.
     31         * @since UT (3.7.0)
    3232         *
    33          * @return array|WP_Error
     33         * @param array $args Array of arguments for inserting a term.
     34         *
     35         * @return int|WP_Error The term ID on success, WP_Error object on failure.
    3436         */
    3537        public function create_object( $args ) {
    3638                $args         = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
    3739                $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
     40
    3841                if ( is_wp_error( $term_id_pair ) ) {
    3942                        return $term_id_pair;
    4043                }
     44
    4145                return $term_id_pair['term_id'];
    4246        }
     
    4549         * Updates the term.
    4650         *
    47          * @param int|object   $term   The term to update.
    48          * @param array|string $fields The context in which to relate the term to the object.
     51         * @since UT (3.7.0)
     52         * @since 6.2.0 Returns a WP_Error object on failure.
    4953         *
    50          * @return int The term ID.
     54         * @param int|object $term   The term to update.
     55         * @param array      $fields Array of arguments for updating a term.
     56         *
     57         * @return int|WP_Error The term ID on success, WP_Error object on failure.
    5158         */
    5259        public function update_object( $term, $fields ) {
    5360                $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
     61
    5462                if ( is_object( $term ) ) {
    5563                        $taxonomy = $term->taxonomy;
    5664                }
     65
    5766                $term_id_pair = wp_update_term( $term, $taxonomy, $fields );
     67
     68                if ( is_wp_error( $term_id_pair ) ) {
     69                        return $term_id_pair;
     70                }
     71
    5872                return $term_id_pair['term_id'];
    5973        }
     
    6175        /**
    6276         * Attach terms to the given post.
     77         *
     78         * @since UT (3.7.0)
    6379         *
    6480         * @param int          $post_id  The post ID.
     
    8096         * Create a term and returns it as an object.
    8197         *
     98         * @since 4.3.0
     99         *
    82100         * @param array $args                   Array or string of arguments for inserting a term.
    83101         * @param null  $generation_definitions The default values.
     
    93111
    94112                $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
     113
    95114                return get_term( $term_id, $taxonomy );
    96115        }
     
    98117        /**
    99118         * Retrieves the term by a given ID.
     119         *
     120         * @since UT (3.7.0)
    100121         *
    101122         * @param int $term_id ID of the term to retrieve.
Note: See TracChangeset for help on using the changeset viewer.