Make WordPress Core

Changeset 54077


Ignore:
Timestamp:
09/06/2022 12:59:17 AM (3 years ago)
Author:
SergeyBiryukov
Message:

Tests: Correct the cache invalidation tests for old date or slug redirect.

This affects:

  • Tests_Rewrite_OldDateRedirect::test_old_date_redirect_cache_invalidation()
  • Tests_Rewrite_OldSlugRedirect::test_old_slug_redirect_cache_invalidation()

In the former test, the $post_id property is declared as static, so can only be approached as static, even when used within the same class in which the property is declared.

Using non-static access will result in null. See: https://3v4l.org/93HQL

This PHP notice was hidden so far, due to the existence of magic methods in the WP_UnitTestCase_Base class.

All the same, the magic methods as they were, would also return null for this property. All in all, the post being updated for this test would never get the correct post_id.

Fixed by using static access to approach the static property.

On a related note, the described bug fix (using the actual $post_id instead of null) exposed that this test was as a matter of fact failing. This was just hidden by the first bug.

Based on the original commit introducing the test, an adjustment is now made which appears to be what the test actually intended to test. A similar change is made to the cache invalidation test for old slug redirects. While not strictly required, it brings some consistency between the two tests and ensures that both tests use a unique post_name value to avoid collisions with the previous values.

This bug was discovered while fixing (removing) the magic methods in the WP_UnitTestCase_Base class in an effort to improve compatibility with PHP 8.2.

Follow-up to [53549].

Props jrf, costdev, SergeyBiryukov.
See #55652.

Location:
trunk/tests/phpunit/tests/rewrite
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rewrite/oldDateRedirect.php

    r53549 r54077  
    107107
    108108        $this->go_to( $old_permalink );
     109
    109110        wp_old_slug_redirect();
    110111        $num_queries = get_num_queries();
    111112        $this->assertSame( $permalink, $this->old_date_redirect_url );
     113
    112114        wp_old_slug_redirect();
    113115        $this->assertSame( $permalink, $this->old_date_redirect_url );
     
    119121     */
    120122    public function test_old_date_redirect_cache_invalidation() {
    121         global $wpdb;
    122123        $old_permalink = user_trailingslashit( get_permalink( self::$post_id ) );
    123124
     
    141142        wp_update_post(
    142143            array(
    143                 'ID'            => $this->post_id,
    144                 'post_date'     => $time,
    145                 'post_date_gmt' => get_gmt_from_date( $time ),
    146                 'post_name'     => 'bar-baz',
    147             )
    148         );
     144                'ID'            => self::$post_id,
     145                'post_date'     => $time,
     146                'post_date_gmt' => get_gmt_from_date( $time ),
     147                'post_name'     => 'foo-bar-baz',
     148            )
     149        );
     150
     151        $permalink = user_trailingslashit( get_permalink( self::$post_id ) );
    149152
    150153        $num_queries = get_num_queries();
    151         $this->go_to( $permalink );
    152154        wp_old_slug_redirect();
    153155        $this->assertSame( $permalink, $this->old_date_redirect_url );
  • trunk/tests/phpunit/tests/rewrite/oldSlugRedirect.php

    r53549 r54077  
    7474        $num_queries = get_num_queries();
    7575        $this->assertSame( $permalink, $this->old_slug_redirect_url );
     76
    7677        wp_old_slug_redirect();
    7778        $this->assertSame( $permalink, $this->old_slug_redirect_url );
     
    102103            array(
    103104                'ID'        => $this->post_id,
    104                 'post_name' => 'foo-bar',
    105             )
    106         );
     105                'post_name' => 'foo-bar-baz',
     106            )
     107        );
     108
     109        $permalink = user_trailingslashit( get_permalink( $this->post_id ) );
     110
    107111        $num_queries = get_num_queries();
    108112        wp_old_slug_redirect();
Note: See TracChangeset for help on using the changeset viewer.