Make WordPress Core


Ignore:
Timestamp:
09/12/2025 09:16:41 PM (3 months ago)
Author:
westonruter
Message:

Posts, Post Types: Fix placement of WP_Error check in get_adjacent_post().

Props kitchin, mindctrl, westonruter, SirLouen, SergeyBiryukov, pmbaldha.
Fixes #63920.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/link/getAdjacentPost.php

    r55085 r60733  
    353353
    354354    /**
     355     * @ticket 63920
     356     */
     357    public function test_get_adjacent_post_returns_empty_string_when_wp_get_object_terms_returns_wp_error() {
     358        register_taxonomy( 'wptests_error_tax', 'post' );
     359
     360        $term1_id = self::factory()->term->create(
     361            array(
     362                'taxonomy' => 'wptests_error_tax',
     363            )
     364        );
     365
     366        $post1_id = self::factory()->post->create(
     367            array(
     368                'post_title' => 'First',
     369                'post_date'  => '2025-09-01 12:00:00',
     370            )
     371        );
     372
     373        $post2_id = self::factory()->post->create(
     374            array(
     375                'post_title' => 'Second',
     376                'post_date'  => '2025-09-02 12:00:00',
     377            )
     378        );
     379
     380        wp_set_post_terms( $post1_id, array( $term1_id ), 'wptests_error_tax' );
     381        wp_set_post_terms( $post2_id, array( $term1_id ), 'wptests_error_tax' );
     382
     383        $this->go_to( get_permalink( $post2_id ) );
     384
     385        add_filter(
     386            'wp_get_object_terms',
     387            static function () {
     388                return new WP_Error( 'test_error', 'Test error from wp_get_object_terms' );
     389            }
     390        );
     391        $result = get_adjacent_post( true, '', true, 'wptests_error_tax' );
     392        $this->assertSame( '', $result );
     393    }
     394
     395    /**
     396     * @ticket 63920
     397     */
     398    public function test_get_adjacent_post_empty_term_array_after_exclusions() {
     399        register_taxonomy( 'wptests_tax', 'post' );
     400
     401        $term1_id = self::factory()->term->create(
     402            array(
     403                'taxonomy' => 'wptests_tax',
     404            )
     405        );
     406
     407        $post1_id = self::factory()->post->create(
     408            array(
     409                'post_title' => 'First',
     410                'post_date'  => '2025-01-01 12:00:00',
     411            )
     412        );
     413
     414        $post2_id = self::factory()->post->create(
     415            array(
     416                'post_title' => 'Second',
     417                'post_date'  => '2025-02-01 12:00:00',
     418            )
     419        );
     420
     421        wp_set_post_terms( $post1_id, array( $term1_id ), 'wptests_tax' );
     422        wp_set_post_terms( $post2_id, array( $term1_id ), 'wptests_tax' );
     423
     424        $this->go_to( get_permalink( $post2_id ) );
     425        $result = get_adjacent_post( true, array( $term1_id ), true, 'wptests_tax' );
     426        $this->assertSame( '', $result );
     427    }
     428
     429    /**
     430     * @ticket 63920
     431     */
     432    public function test_get_adjacent_post_term_array_processing_order() {
     433        register_taxonomy( 'wptests_tax', 'post' );
     434
     435        $term1_id = self::factory()->term->create(
     436            array(
     437                'taxonomy' => 'wptests_tax',
     438            )
     439        );
     440        $term2_id = self::factory()->term->create(
     441            array(
     442                'taxonomy' => 'wptests_tax',
     443            )
     444        );
     445
     446        $post1_id = self::factory()->post->create(
     447            array(
     448                'post_title' => 'First',
     449                'post_date'  => '2025-01-01 12:00:00',
     450            )
     451        );
     452
     453        $post2_id = self::factory()->post->create(
     454            array(
     455                'post_title' => 'Second',
     456                'post_date'  => '2025-02-01 12:00:00',
     457            )
     458        );
     459
     460        $post3_id = self::factory()->post->create(
     461            array(
     462                'post_title' => 'Third',
     463                'post_date'  => '2025-03-01 12:00:00',
     464            )
     465        );
     466
     467        // All posts have term1. post_two has term1 and term2.
     468        wp_set_post_terms( $post1_id, array( $term1_id ), 'wptests_tax' );
     469        wp_set_post_terms( $post2_id, array( $term1_id, $term2_id ), 'wptests_tax' );
     470        wp_set_post_terms( $post3_id, array( $term1_id ), 'wptests_tax' );
     471
     472        // Set the current post to post_two.
     473        $this->go_to( get_permalink( $post2_id ) );
     474
     475        // When we exclude term2, we should still get adjacent posts that share term1.
     476        $result = get_adjacent_post( true, array( $term2_id ), true, 'wptests_tax' );
     477
     478        // Should find post_one (previous post that shares term1).
     479        $this->assertInstanceOf( WP_Post::class, $result );
     480        $this->assertEquals( $post1_id, $result->ID );
     481
     482        // Test next post.
     483        $result = get_adjacent_post( true, array( $term2_id ), false, 'wptests_tax' );
     484
     485        // Should find post_three (next post that shares term1).
     486        $this->assertInstanceOf( WP_Post::class, $result );
     487        $this->assertEquals( $post3_id, $result->ID );
     488    }
     489
     490    /**
     491     * @ticket 63920
     492     */
     493    public function test_get_adjacent_post_invalid_taxonomy() {
     494        self::factory()->post->create(
     495            array(
     496                'post_title' => 'First',
     497                'post_date'  => '2025-01-01 12:00:00',
     498            )
     499        );
     500
     501        $post2_id = self::factory()->post->create(
     502            array(
     503                'post_title' => 'Second',
     504                'post_date'  => '2025-02-01 12:00:00',
     505            )
     506        );
     507
     508        $this->go_to( get_permalink( $post2_id ) );
     509        $result = get_adjacent_post( true, '', true, 'invalid_taxonomy' );
     510        $this->assertNull( $result );
     511    }
     512
     513    /**
    355514     * @ticket 41131
    356515     */
Note: See TracChangeset for help on using the changeset viewer.