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.

git-svn-id: https://develop.svn.wordpress.org/trunk@54077 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-09-06 00:59:17 +00:00
parent b48ec1408e
commit 7771a7c7a9
2 changed files with 11 additions and 5 deletions

View File

@ -106,9 +106,11 @@ class Tests_Rewrite_OldDateRedirect extends WP_UnitTestCase {
$permalink = user_trailingslashit( get_permalink( self::$post_id ) );
$this->go_to( $old_permalink );
wp_old_slug_redirect();
$num_queries = get_num_queries();
$this->assertSame( $permalink, $this->old_date_redirect_url );
wp_old_slug_redirect();
$this->assertSame( $permalink, $this->old_date_redirect_url );
$this->assertSame( $num_queries, get_num_queries() );
@ -118,7 +120,6 @@ class Tests_Rewrite_OldDateRedirect extends WP_UnitTestCase {
* @ticket 36723
*/
public function test_old_date_redirect_cache_invalidation() {
global $wpdb;
$old_permalink = user_trailingslashit( get_permalink( self::$post_id ) );
$time = '2004-01-03 00:00:00';
@ -140,15 +141,16 @@ class Tests_Rewrite_OldDateRedirect extends WP_UnitTestCase {
$time = '2014-02-01 00:00:00';
wp_update_post(
array(
'ID' => $this->post_id,
'ID' => self::$post_id,
'post_date' => $time,
'post_date_gmt' => get_gmt_from_date( $time ),
'post_name' => 'bar-baz',
'post_name' => 'foo-bar-baz',
)
);
$permalink = user_trailingslashit( get_permalink( self::$post_id ) );
$num_queries = get_num_queries();
$this->go_to( $permalink );
wp_old_slug_redirect();
$this->assertSame( $permalink, $this->old_date_redirect_url );
$this->assertGreaterThan( $num_queries, get_num_queries() );

View File

@ -73,6 +73,7 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase {
wp_old_slug_redirect();
$num_queries = get_num_queries();
$this->assertSame( $permalink, $this->old_slug_redirect_url );
wp_old_slug_redirect();
$this->assertSame( $permalink, $this->old_slug_redirect_url );
$this->assertSame( $num_queries, get_num_queries() );
@ -101,9 +102,12 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase {
wp_update_post(
array(
'ID' => $this->post_id,
'post_name' => 'foo-bar',
'post_name' => 'foo-bar-baz',
)
);
$permalink = user_trailingslashit( get_permalink( $this->post_id ) );
$num_queries = get_num_queries();
wp_old_slug_redirect();
$this->assertSame( $permalink, $this->old_slug_redirect_url );