Make WordPress Core

Changeset 54829 for trunk


Ignore:
Timestamp:
11/12/2022 02:49:25 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Tests: Resolve WP_Query test failures on MariaDB due to indeterminate sort order.

[54768] added a few tests to verify that caching within WP_Query is bypassed when the SELECT clause has been modified via a filter, to avoid cache key collisions and the returning of incomplete or unexpected results.

However, creating several posts with the same date/time fields can result in inconsistent sort ordering between MySQL and MariaDB, as each engine refines the order further using a different index.

This commit aims to stabilize the tests by using assertEqualSets() instead of assertEquals(), since testing the order is out of their scope. Includes removing array_unshift() and array_reverse() calls as no longer needed.

This resolves a few test failures on MariaDB along the lines of:

Tests_Query_FieldsClause::test_should_limit_fields_to_id_and_parent_subset
Posts property for first query is not of expected form.
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
 Array (
     0 => stdClass Object (
-        'ID' => 36019
+        'ID' => 36015
         'post_parent' => 0
     )
     1 => stdClass Object (
-        'ID' => 36018
+        'ID' => 36016
         'post_parent' => 0
     )
     2 => stdClass Object (...)
     3 => stdClass Object (
-        'ID' => 36016
+        'ID' => 36018
         'post_parent' => 0
     )
     4 => stdClass Object (
-        'ID' => 36015
+        'ID' => 36019
         'post_parent' => 0
     )
 )

/tmp/wp-test-runner/tests/phpunit/tests/query/fieldsClause.php:67
/tmp/wp-test-runner/phpunit-5.7.phar:598

Follow-up to [54768].

Props peterwilsoncc, SergeyBiryukov.
See #57012.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/query/fieldsClause.php

    r54768 r54829  
    5555                $expected = array();
    5656                foreach ( self::$post_ids as $post_id ) {
    57                         // Use array_shift to populate in the reverse order.
    58                         array_unshift(
    59                                 $expected,
    60                                 (object) array(
    61                                         'ID'          => $post_id,
    62                                         'post_parent' => 0,
    63                                 )
     57                        $expected[] = (object) array(
     58                                'ID'          => $post_id,
     59                                'post_parent' => 0,
    6460                        );
    6561                }
    6662
    67                 $this->assertEquals( $expected, $q->posts, 'Posts property for first query is not of expected form.' );
    68                 $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' );
    69                 $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' );
    70 
    71                 // Test the second query's results match.
    72                 $q2 = new WP_Query( $query_args );
    73                 $this->assertEquals( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' );
     63                $this->assertEqualSets( $expected, $q->posts, 'Posts property for first query is not of expected form.' );
     64                $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' );
     65                $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' );
     66
     67                // Test the second query's results match.
     68                $q2 = new WP_Query( $query_args );
     69                $this->assertEqualSets( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' );
    7470        }
    7571
     
    8783                $q = new WP_Query( $query_args );
    8884
    89                 $expected = array_reverse( self::$post_ids );
    90 
    91                 $this->assertEquals( $expected, $q->posts, 'Posts property for first query is not of expected form.' );
    92                 $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' );
    93                 $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' );
    94 
    95                 // Test the second query's results match.
    96                 $q2 = new WP_Query( $query_args );
    97                 $this->assertEquals( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' );
     85                $expected = self::$post_ids;
     86
     87                $this->assertEqualSets( $expected, $q->posts, 'Posts property for first query is not of expected form.' );
     88                $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' );
     89                $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' );
     90
     91                // Test the second query's results match.
     92                $q2 = new WP_Query( $query_args );
     93                $this->assertEqualSets( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' );
    9894        }
    9995
     
    111107                $q = new WP_Query( $query_args );
    112108
    113                 $expected = array_map( 'get_post', array_reverse( self::$post_ids ) );
    114 
    115                 $this->assertEquals( $expected, $q->posts, 'Posts property for first query is not of expected form.' );
    116                 $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' );
    117                 $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' );
    118 
    119                 // Test the second query's results match.
    120                 $q2 = new WP_Query( $query_args );
    121                 $this->assertEquals( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' );
     109                $expected = array_map( 'get_post', self::$post_ids );
     110
     111                $this->assertEqualSets( $expected, $q->posts, 'Posts property for first query is not of expected form.' );
     112                $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' );
     113                $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' );
     114
     115                // Test the second query's results match.
     116                $q2 = new WP_Query( $query_args );
     117                $this->assertEqualSets( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' );
    122118        }
    123119
     
    140136                $expected = array();
    141137                foreach ( self::$post_ids as $post_id ) {
    142                         // Use array_shift to populate in the reverse order.
    143                         array_unshift(
    144                                 $expected,
    145                                 (object) array(
    146                                         'ID'                => $post_id,
    147                                         'post_parent'       => 0,
    148                                         'test_post_fields'  => 1,
    149                                         'test_post_clauses' => 2,
    150                                 )
     138                        $expected[] = (object) array(
     139                                'ID'                => $post_id,
     140                                'post_parent'       => 0,
     141                                'test_post_fields'  => '1',
     142                                'test_post_clauses' => '2',
    151143                        );
    152144                }
    153145
    154                 $this->assertEquals( $expected, $q->posts, 'Posts property for first query is not of expected form.' );
    155                 $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' );
    156                 $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' );
    157 
    158                 // Test the second query's results match.
    159                 $q2 = new WP_Query( $query_args );
    160                 $this->assertEquals( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' );
     146                $this->assertEqualSets( $expected, $q->posts, 'Posts property for first query is not of expected form.' );
     147                $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' );
     148                $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' );
     149
     150                // Test the second query's results match.
     151                $q2 = new WP_Query( $query_args );
     152                $this->assertEqualSets( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' );
    161153        }
    162154
     
    177169                $q = new WP_Query( $query_args );
    178170
    179                 // Fields => ID does not include the additional fields.
    180                 $expected = array_reverse( self::$post_ids );
    181 
    182                 $this->assertEquals( $expected, $q->posts, 'Posts property for first query is not of expected form.' );
    183                 $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' );
    184                 $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' );
    185 
    186                 // Test the second query's results match.
    187                 $q2 = new WP_Query( $query_args );
    188                 $this->assertEquals( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' );
     171                // `fields => ids` does not include the additional fields.
     172                $expected = self::$post_ids;
     173
     174                $this->assertEqualSets( $expected, $q->posts, 'Posts property for first query is not of expected form.' );
     175                $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' );
     176                $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' );
     177
     178                // Test the second query's results match.
     179                $q2 = new WP_Query( $query_args );
     180                $this->assertEqualSets( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' );
    189181        }
    190182
     
    205197                $q = new WP_Query( $query_args );
    206198
    207                 $expected = array_map( 'get_post', array_reverse( self::$post_ids ) );
     199                $expected = array_map( 'get_post', self::$post_ids );
    208200                foreach ( $expected as $post ) {
    209                         $post->test_post_fields  = 1;
    210                         $post->test_post_clauses = 2;
     201                        $post->test_post_fields  = '1';
     202                        $post->test_post_clauses = '2';
    211203                }
    212204
    213                 $this->assertEquals( $expected, $q->posts, 'Posts property for first query is not of expected form.' );
    214                 $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' );
    215                 $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' );
    216 
    217                 // Test the second query's results match.
    218                 $q2 = new WP_Query( $query_args );
    219                 $this->assertEquals( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' );
     205                $this->assertEqualSets( $expected, $q->posts, 'Posts property for first query is not of expected form.' );
     206                $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' );
     207                $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' );
     208
     209                // Test the second query's results match.
     210                $q2 = new WP_Query( $query_args );
     211                $this->assertEqualSets( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' );
    220212        }
    221213
Note: See TracChangeset for help on using the changeset viewer.