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-attachment.php

    r47122 r55019  
    11<?php
    22
     3/**
     4 * Unit test factory for attachments.
     5 *
     6 * Note: The below @method notations are defined solely for the benefit of IDEs,
     7 * as a way to indicate expected return values from the given factory methods.
     8 *
     9 * @method int|WP_Error     create( $args = array(), $generation_definitions = null )
     10 * @method WP_Post|WP_Error create_and_get( $args = array(), $generation_definitions = null )
     11 * @method (int|WP_Error)[] create_many( $count, $args = array(), $generation_definitions = null )
     12 */
    313class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
    414
    515        /**
    616         * Create an attachment fixture.
     17         *
     18         * @since UT (3.7.0)
     19         * @since 6.2.0 Returns a WP_Error object on failure.
    720         *
    821         * @param array $args {
    … …  
    1528         * @param array $legacy_args   Deprecated.
    1629         *
    17          * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
     30         * @return int|WP_Error The attachment ID on success, WP_Error object on failure.
    1831         */
    1932        public function create_object( $args, $legacy_parent = 0, $legacy_args = array() ) {
    … …  
    3447                );
    3548
    36                 return wp_insert_attachment( $r, $r['file'], $r['post_parent'] );
     49                return wp_insert_attachment( $r, $r['file'], $r['post_parent'], true );
    3750        }
    3851
    … …  
    4053         * Saves an attachment.
    4154         *
     55         * @since 4.4.0
     56         * @since 6.2.0 Returns a WP_Error object on failure.
     57         *
    4258         * @param string $file   The file name to create attachment object for.
    4359         * @param int    $parent ID of the post to attach the file to.
    4460         *
    45          * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
     61         * @return int|WP_Error The attachment ID on success, WP_Error object on failure.
    4662         */
    4763        public function create_upload_object( $file, $parent = 0 ) {
    … …  
    6985
    7086                // Save the data.
    71                 $id = wp_insert_attachment( $attachment, $upload['file'], $parent );
    72                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
     87                $attachment_id = wp_insert_attachment( $attachment, $upload['file'], $parent, true );
    7388
    74                 return $id;
     89                if ( is_wp_error( $attachment_id ) ) {
     90                        return $attachment_id;
     91                }
     92
     93                wp_update_attachment_metadata(
     94                        $attachment_id,
     95                        wp_generate_attachment_metadata( $attachment_id, $upload['file'] )
     96                );
     97
     98                return $attachment_id;
    7599        }
    76100}
Note: See TracChangeset for help on using the changeset viewer.