Make WordPress Core

Changeset 54421


Ignore:
Timestamp:
10/08/2022 08:44:45 AM (4 years ago)
Author:
audrasjb
Message:

Build/Test Tools: Add tests for WP_List_Util::pluck and WP_List_Util::sort.

This changeset adds additional unit tests for these two functions.

Props pbearne, costdev, afragen.
Fixes #55300.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/functions/wpListUtil.php

    r51667 r54421  
    5353                $this->assertEqualSets( $expected, $util->get_output() );
    5454        }
     55
     56        /**
     57         * @ticket 55300
     58         *
     59         * @dataProvider data_wp_list_util_pluck
     60         *
     61         * @covers WP_List_Util::pluck
     62         * @covers ::wp_list_pluck
     63         *
     64         * @param array  $target_array The array to create the list from.
     65         * @param string $target_key   The key to pluck.
     66         * @param array  $expected     The expected array.
     67         * @param string $index_key    Optional. Field from the element to use as keys for the new array. Default null.
     68         */
     69        public function test_wp_list_util_pluck( $target_array, $target_key, $expected, $index_key = null ) {
     70                $util   = new WP_List_Util( $target_array );
     71                $actual = $util->pluck( $target_key, $index_key );
     72
     73                $this->assertEqualSetsWithIndex(
     74                        $expected,
     75                        $actual,
     76                        'The plucked value did not match the expected value.'
     77                );
     78
     79                $this->assertEqualSetsWithIndex(
     80                        $expected,
     81                        $util->get_output(),
     82                        '::get_output() did not return the expected value.'
     83                );
     84        }
     85
     86        /**
     87         * Data provider for test_wp_list_util_pluck_simple().
     88         *
     89         * @return array[]
     90         */
     91        public function data_wp_list_util_pluck() {
     92                return array(
     93                        'simple'        => array(
     94                                'target_array' => array(
     95                                        0 => array( 'foo' => 'bar' ),
     96                                ),
     97                                'target_key'   => 'foo',
     98                                'expected'     => array( 'bar' ),
     99                        ),
     100                        'simple_object' => array(
     101                                'target_array' => array(
     102                                        0 => (object) array( 'foo' => 'bar' ),
     103                                ),
     104                                'target_key'   => 'foo',
     105                                'expected'     => array( 'bar' ),
     106                        ),
     107                );
     108        }
     109
     110        /**
     111         * @ticket 55300
     112         *
     113         * @covers WP_List_Util::sort
     114         * @covers ::wp_list_sort
     115         */
     116        public function test_wp_list_util_sort_simple() {
     117                $expected     = array(
     118                        1 => 'one',
     119                        2 => 'two',
     120                        3 => 'three',
     121                        4 => 'four',
     122                );
     123                $target_array = array(
     124                        4 => 'four',
     125                        2 => 'two',
     126                        3 => 'three',
     127                        1 => 'one',
     128                );
     129
     130                $util   = new WP_List_Util( $target_array );
     131                $actual = $util->sort();
     132
     133                $this->assertEqualSets(
     134                        $expected,
     135                        $actual,
     136                        'The sorted value did not match the expected value.'
     137                );
     138
     139                $this->assertEqualSets(
     140                        $expected,
     141                        $util->get_output(),
     142                        '::get_output() did not return the expected value.'
     143                );
     144        }
     145
     146        /**
     147         * @ticket 55300
     148         *
     149         * @dataProvider data_wp_list_util_sort_string_arrays
     150         * @dataProvider data_wp_list_util_sort_int_arrays
     151         * @dataProvider data_wp_list_util_sort_arrays_of_arrays
     152         * @dataProvider data_wp_list_util_sort_object_arrays
     153         *
     154         * @covers WP_List_Util::sort
     155         * @covers ::wp_list_sort
     156         *
     157         * @param array  $expected      The expected array.
     158         * @param array  $target_array  The array to create a list from.
     159         * @param array  $orderby       Optional. Either the field name to order by or an array of multiple orderby fields as $orderby => $order.
     160         *                              Default empty array.
     161         * @param string $order         Optional. Either 'ASC' or 'DESC'. Only used if $orderby is a string. Default 'ASC'.
     162         * @param bool   $preserve_keys Optional. Whether to preserve keys. Default false.
     163         */
     164        public function test_wp_list_util_sort( $expected, $target_array, $orderby = array(), $order = 'ASC', $preserve_keys = false ) {
     165                $util   = new WP_List_Util( $target_array );
     166                $actual = $util->sort( $orderby, $order, $preserve_keys );
     167
     168                $this->assertEqualSetsWithIndex(
     169                        $expected,
     170                        $actual,
     171                        'The sorted value did not match the expected value.'
     172                );
     173
     174                $this->assertEqualSetsWithIndex(
     175                        $expected,
     176                        $util->get_output(),
     177                        '::get_output() did not return the expected value.'
     178                );
     179        }
     180
     181        /**
     182         * Data provider that provides string arrays to test_wp_list_util_sort().
     183         *
     184         * @return array[]
     185         */
     186        public function data_wp_list_util_sort_string_arrays() {
     187                return array(
     188                        'string[], no keys, no ordering'     => array(
     189                                'expected'     => array( 'four', 'two', 'three', 'one' ),
     190                                'target_array' => array( 'four', 'two', 'three', 'one' ),
     191                        ),
     192                        'string[], int keys, no ordering'    => array(
     193                                'expected'     => array(
     194                                        4 => 'four',
     195                                        2 => 'two',
     196                                        3 => 'three',
     197                                        1 => 'one',
     198                                ),
     199                                'target_array' => array(
     200                                        4 => 'four',
     201                                        2 => 'two',
     202                                        3 => 'three',
     203                                        1 => 'one',
     204                                ),
     205                        ),
     206                        'string[], int keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
     207                                'expected'      => array(
     208                                        4 => 'four',
     209                                        2 => 'two',
     210                                        3 => 'three',
     211                                        1 => 'one',
     212                                ),
     213                                'target_array'  => array(
     214                                        4 => 'four',
     215                                        2 => 'two',
     216                                        3 => 'three',
     217                                        1 => 'one',
     218                                ),
     219                                'orderby'       => 'id',
     220                                'order'         => 'DESC',
     221                                'preserve_keys' => true,
     222                        ),
     223                        'string[], string keys, no ordering' => array(
     224                                'expected'     => array(
     225                                        'four'  => 'four',
     226                                        'two'   => 'two',
     227                                        'three' => 'three',
     228                                        'one'   => 'one',
     229                                ),
     230                                'target_array' => array(
     231                                        'four'  => 'four',
     232                                        'two'   => 'two',
     233                                        'three' => 'three',
     234                                        'one'   => 'one',
     235                                ),
     236                        ),
     237                        'string[], string keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
     238                                'expected'      => array(
     239                                        'four'  => 'four',
     240                                        'two'   => 'two',
     241                                        'three' => 'three',
     242                                        'one'   => 'one',
     243                                ),
     244                                'target_array'  => array(
     245                                        'four'  => 'four',
     246                                        'two'   => 'two',
     247                                        'three' => 'three',
     248                                        'one'   => 'one',
     249                                ),
     250                                'orderby'       => 'id',
     251                                'order'         => 'DESC',
     252                                'preserve_keys' => true,
     253                        ),
     254                );
     255        }
     256
     257        /**
     258         * Data provider that provides int arrays for test_wp_list_util_sort().
     259         *
     260         * @return array[]
     261         */
     262        public function data_wp_list_util_sort_int_arrays() {
     263                return array(
     264                        'int[], no keys, no ordering'     => array(
     265                                'expected'     => array( 4, 2, 3, 1 ),
     266                                'target_array' => array( 4, 2, 3, 1 ),
     267                        ),
     268                        'int[], int keys, no ordering'    => array(
     269                                'expected'     => array(
     270                                        4 => 4,
     271                                        2 => 2,
     272                                        3 => 3,
     273                                        1 => 1,
     274                                ),
     275                                'target_array' => array(
     276                                        4 => 4,
     277                                        2 => 2,
     278                                        3 => 3,
     279                                        1 => 1,
     280                                ),
     281                        ),
     282                        'int[], int keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
     283                                'expected'      => array(
     284                                        4 => 4,
     285                                        2 => 2,
     286                                        3 => 3,
     287                                        1 => 1,
     288                                ),
     289                                'target_array'  => array(
     290                                        4 => 4,
     291                                        2 => 2,
     292                                        3 => 3,
     293                                        1 => 1,
     294                                ),
     295                                'orderby'       => 'id',
     296                                'order'         => 'DESC',
     297                                'preserve_keys' => true,
     298                        ),
     299                        'int[], string keys, no ordering' => array(
     300                                'expected'     => array(
     301                                        'four'  => 4,
     302                                        'two'   => 2,
     303                                        'three' => 3,
     304                                        'one'   => 1,
     305                                ),
     306                                'target_array' => array(
     307                                        'four'  => 4,
     308                                        'two'   => 2,
     309                                        'three' => 3,
     310                                        'one'   => 1,
     311                                ),
     312                        ),
     313                        'int[], string keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
     314                                'expected'      => array(
     315                                        'four'  => 4,
     316                                        'two'   => 2,
     317                                        'three' => 3,
     318                                        'one'   => 1,
     319                                ),
     320                                'target_array'  => array(
     321                                        'four'  => 4,
     322                                        'two'   => 2,
     323                                        'three' => 3,
     324                                        'one'   => 1,
     325                                ),
     326                                'orderby'       => 'id',
     327                                'order'         => 'DESC',
     328                                'preserve_keys' => true,
     329                        ),
     330                );
     331        }
     332
     333        /**
     334         * Data provider that provides arrays of arrays for test_wp_list_util_sort().
     335         *
     336         * @return array[]
     337         */
     338        public function data_wp_list_util_sort_arrays_of_arrays() {
     339                return array(
     340                        'array[], no keys, no ordering'     => array(
     341                                'expected'     => array(
     342                                        array( 'four' ),
     343                                        array( 'two' ),
     344                                        array( 'three' ),
     345                                        array( 'one' ),
     346                                ),
     347                                'target_array' => array(
     348                                        array( 'four' ),
     349                                        array( 'two' ),
     350                                        array( 'three' ),
     351                                        array( 'one' ),
     352                                ),
     353                        ),
     354                        'array[], int keys, no ordering'    => array(
     355                                'expected'     => array(
     356                                        4 => array( 'four' ),
     357                                        2 => array( 'two' ),
     358                                        3 => array( 'three' ),
     359                                        1 => array( 'one' ),
     360                                ),
     361                                'target_array' => array(
     362                                        4 => array( 'four' ),
     363                                        2 => array( 'two' ),
     364                                        3 => array( 'three' ),
     365                                        1 => array( 'one' ),
     366                                ),
     367                        ),
     368                        'array[], int keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
     369                                'expected'      => array(
     370                                        4 => array( 'value' => 'four' ),
     371                                        2 => array( 'value' => 'two' ),
     372                                        3 => array( 'value' => 'three' ),
     373                                        1 => array( 'value' => 'one' ),
     374                                ),
     375                                'target_array'  => array(
     376                                        4 => array( 'value' => 'four' ),
     377                                        2 => array( 'value' => 'two' ),
     378                                        3 => array( 'value' => 'three' ),
     379                                        1 => array( 'value' => 'one' ),
     380                                ),
     381                                'orderby'       => 'id',
     382                                'order'         => 'DESC',
     383                                'preserve_keys' => true,
     384                        ),
     385                        'array[], int keys, $orderby an existing field, $order = ASC and $preserve_keys = false' => array(
     386                                'expected'      => array(
     387                                        array(
     388                                                'id'    => 1,
     389                                                'value' => 'one',
     390                                        ),
     391                                        array(
     392                                                'id'    => 2,
     393                                                'value' => 'two',
     394                                        ),
     395                                        array(
     396                                                'id'    => 3,
     397                                                'value' => 'three',
     398                                        ),
     399                                        array(
     400                                                'id'    => 4,
     401                                                'value' => 'four',
     402                                        ),
     403                                ),
     404                                'target_array'  => array(
     405                                        4 => array(
     406                                                'id'    => 4,
     407                                                'value' => 'four',
     408                                        ),
     409                                        2 => array(
     410                                                'id'    => 2,
     411                                                'value' => 'two',
     412                                        ),
     413                                        3 => array(
     414                                                'id'    => 3,
     415                                                'value' => 'three',
     416                                        ),
     417                                        1 => array(
     418                                                'id'    => 1,
     419                                                'value' => 'one',
     420                                        ),
     421                                ),
     422                                'orderby'       => 'id',
     423                                'order'         => 'ASC',
     424                                'preserve_keys' => false,
     425                        ),
     426                        'array[], int keys, $orderby an existing field, $order = DESC and $preserve_keys = true' => array(
     427                                'expected'      => array(
     428                                        3 => array(
     429                                                'id'    => 4,
     430                                                'value' => 'four',
     431                                        ),
     432                                        2 => array(
     433                                                'id'    => 3,
     434                                                'value' => 'three',
     435                                        ),
     436                                        1 => array(
     437                                                'id'    => 2,
     438                                                'value' => 'two',
     439                                        ),
     440                                        0 => array(
     441                                                'id'    => 1,
     442                                                'value' => 'one',
     443                                        ),
     444                                ),
     445                                'target_array'  => array(
     446                                        array(
     447                                                'id'    => 1,
     448                                                'value' => 'one',
     449                                        ),
     450                                        array(
     451                                                'id'    => 2,
     452                                                'value' => 'two',
     453                                        ),
     454                                        array(
     455                                                'id'    => 3,
     456                                                'value' => 'three',
     457                                        ),
     458                                        array(
     459                                                'id'    => 4,
     460                                                'value' => 'four',
     461                                        ),
     462                                ),
     463                                'orderby'       => 'id',
     464                                'order'         => 'DESC',
     465                                'preserve_keys' => true,
     466                        ),
     467                        'array[], string keys, no ordering' => array(
     468                                'expected'     => array(
     469                                        'four'  => array( 'value' => 'four' ),
     470                                        'two'   => array( 'value' => 'two' ),
     471                                        'three' => array( 'value' => 'three' ),
     472                                        'one'   => array( 'value' => 'one' ),
     473                                ),
     474                                'target_array' => array(
     475                                        'four'  => array( 'value' => 'four' ),
     476                                        'two'   => array( 'value' => 'two' ),
     477                                        'three' => array( 'value' => 'three' ),
     478                                        'one'   => array( 'value' => 'one' ),
     479                                ),
     480                        ),
     481                        'array[], string keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
     482                                'expected'      => array(
     483                                        'four'  => array( 'value' => 'four' ),
     484                                        'two'   => array( 'value' => 'two' ),
     485                                        'three' => array( 'value' => 'three' ),
     486                                        'one'   => array( 'value' => 'one' ),
     487                                ),
     488                                'target_array'  => array(
     489                                        'four'  => array( 'value' => 'four' ),
     490                                        'two'   => array( 'value' => 'two' ),
     491                                        'three' => array( 'value' => 'three' ),
     492                                        'one'   => array( 'value' => 'one' ),
     493                                ),
     494                                'orderby'       => 'id',
     495                                'order'         => 'DESC',
     496                                'preserve_keys' => true,
     497                        ),
     498                        'array[], string keys, $orderby an existing field, $order = ASC and $preserve_keys = false' => array(
     499                                'expected'      => array(
     500                                        array(
     501                                                'id'    => 1,
     502                                                'value' => 'one',
     503                                        ),
     504                                        array(
     505                                                'id'    => 2,
     506                                                'value' => 'two',
     507                                        ),
     508                                        array(
     509                                                'id'    => 3,
     510                                                'value' => 'three',
     511                                        ),
     512                                        array(
     513                                                'id'    => 4,
     514                                                'value' => 'four',
     515                                        ),
     516                                ),
     517                                'target_array'  => array(
     518                                        'four'  => array(
     519                                                'id'    => 4,
     520                                                'value' => 'four',
     521                                        ),
     522                                        'two'   => array(
     523                                                'id'    => 2,
     524                                                'value' => 'two',
     525                                        ),
     526                                        'three' => array(
     527                                                'id'    => 3,
     528                                                'value' => 'three',
     529                                        ),
     530                                        'one'   => array(
     531                                                'id'    => 1,
     532                                                'value' => 'one',
     533                                        ),
     534                                ),
     535                                'orderby'       => 'id',
     536                                'order'         => 'ASC',
     537                                'preserve_keys' => false,
     538                        ),
     539                        'array[], string keys, $orderby an existing field, $order = DESC and $preserve_keys = true' => array(
     540                                'expected'      => array(
     541                                        'four'  => array(
     542                                                'id'    => 4,
     543                                                'value' => 'four',
     544                                        ),
     545                                        'three' => array(
     546                                                'id'    => 3,
     547                                                'value' => 'three',
     548                                        ),
     549                                        'two'   => array(
     550                                                'id'    => 2,
     551                                                'value' => 'two',
     552                                        ),
     553                                        'one'   => array(
     554                                                'id'    => 1,
     555                                                'value' => 'one',
     556                                        ),
     557                                ),
     558                                'target_array'  => array(
     559                                        'one'   => array(
     560                                                'id'    => 1,
     561                                                'value' => 'one',
     562                                        ),
     563                                        'two'   => array(
     564                                                'id'    => 2,
     565                                                'value' => 'two',
     566                                        ),
     567                                        'three' => array(
     568                                                'id'    => 3,
     569                                                'value' => 'three',
     570                                        ),
     571                                        'four'  => array(
     572                                                'id'    => 4,
     573                                                'value' => 'four',
     574                                        ),
     575                                ),
     576                                'orderby'       => 'id',
     577                                'order'         => 'DESC',
     578                                'preserve_keys' => true,
     579                        ),
     580                        'array[], string keys, $orderby an existing field, $order = asc (lowercase) and $preserve_keys = false' => array(
     581                                'expected'      => array(
     582                                        array(
     583                                                'id'    => 1,
     584                                                'value' => 'one',
     585                                        ),
     586                                        array(
     587                                                'id'    => 2,
     588                                                'value' => 'two',
     589                                        ),
     590                                        array(
     591                                                'id'    => 3,
     592                                                'value' => 'three',
     593                                        ),
     594                                        array(
     595                                                'id'    => 4,
     596                                                'value' => 'four',
     597                                        ),
     598                                ),
     599                                'target_array'  => array(
     600                                        'four'  => array(
     601                                                'id'    => 4,
     602                                                'value' => 'four',
     603                                        ),
     604                                        'two'   => array(
     605                                                'id'    => 2,
     606                                                'value' => 'two',
     607                                        ),
     608                                        'three' => array(
     609                                                'id'    => 3,
     610                                                'value' => 'three',
     611                                        ),
     612                                        'one'   => array(
     613                                                'id'    => 1,
     614                                                'value' => 'one',
     615                                        ),
     616                                ),
     617                                'orderby'       => 'id',
     618                                'order'         => 'asc',
     619                                'preserve_keys' => false,
     620                        ),
     621                        'array[], string keys, $orderby an existing field, no order and $preserve_keys = false' => array(
     622                                'expected'      => array(
     623                                        'four'  => array(
     624                                                'id'    => 4,
     625                                                'value' => 'four',
     626                                        ),
     627                                        'three' => array(
     628                                                'id'    => 3,
     629                                                'value' => 'three',
     630                                        ),
     631                                        'two'   => array(
     632                                                'id'    => 2,
     633                                                'value' => 'two',
     634                                        ),
     635                                        'one'   => array(
     636                                                'id'    => 1,
     637                                                'value' => 'one',
     638                                        ),
     639                                ),
     640                                'target_array'  => array(
     641                                        'one'   => array(
     642                                                'id'    => 1,
     643                                                'value' => 'one',
     644                                        ),
     645                                        'two'   => array(
     646                                                'id'    => 2,
     647                                                'value' => 'two',
     648                                        ),
     649                                        'three' => array(
     650                                                'id'    => 3,
     651                                                'value' => 'three',
     652                                        ),
     653                                        'four'  => array(
     654                                                'id'    => 4,
     655                                                'value' => 'four',
     656                                        ),
     657                                ),
     658                                'orderby'       => array( 'id' ),
     659                                'order'         => null,
     660                                'preserve_keys' => true,
     661                        ),
     662                        'array[], string keys, $orderby two existing fields, differing orders and $preserve_keys = false' => array(
     663                                'expected'      => array(
     664                                        array(
     665                                                'id'    => 1,
     666                                                'value' => 'one',
     667                                        ),
     668                                        array(
     669                                                'id'    => 2,
     670                                                'value' => 'two',
     671                                        ),
     672                                        array(
     673                                                'id'    => 3,
     674                                                'value' => 'three',
     675                                        ),
     676                                        array(
     677                                                'id'    => 4,
     678                                                'value' => 'four',
     679                                        ),
     680                                ),
     681                                'target_array'  => array(
     682                                        'four'  => array(
     683                                                'id'    => 4,
     684                                                'value' => 'four',
     685                                        ),
     686                                        'two'   => array(
     687                                                'id'    => 2,
     688                                                'value' => 'two',
     689                                        ),
     690                                        'three' => array(
     691                                                'id'    => 3,
     692                                                'value' => 'three',
     693                                        ),
     694                                        'one'   => array(
     695                                                'id'    => 1,
     696                                                'value' => 'one',
     697                                        ),
     698                                ),
     699                                'orderby'       => array(
     700                                        'id'    => 'asc',
     701                                        'value' => 'DESC',
     702                                ),
     703                                'order'         => null,
     704                                'preserve_keys' => false,
     705                        ),
     706                );
     707        }
     708
     709        /**
     710         * Data provider that provides object arrays for test_wp_list_util_sort().
     711         *
     712         * @return array[]
     713         */
     714        public function data_wp_list_util_sort_object_arrays() {
     715                return array(
     716                        'object[], no keys, no ordering'     => array(
     717                                'expected'     => array(
     718                                        (object) array( 'four' ),
     719                                        (object) array( 'two' ),
     720                                        (object) array( 'three' ),
     721                                        (object) array( 'one' ),
     722                                ),
     723                                'target_array' => array(
     724                                        (object) array( 'four' ),
     725                                        (object) array( 'two' ),
     726                                        (object) array( 'three' ),
     727                                        (object) array( 'one' ),
     728                                ),
     729                        ),
     730                        'object[], int keys, no ordering'    => array(
     731                                'expected'     => array(
     732                                        4 => (object) array( 'four' ),
     733                                        2 => (object) array( 'two' ),
     734                                        3 => (object) array( 'three' ),
     735                                        1 => (object) array( 'one' ),
     736                                ),
     737                                'target_array' => array(
     738                                        4 => (object) array( 'four' ),
     739                                        2 => (object) array( 'two' ),
     740                                        3 => (object) array( 'three' ),
     741                                        1 => (object) array( 'one' ),
     742                                ),
     743                        ),
     744                        'object[], int keys, $orderby an existing field, $order = ASC and $preserve_keys = false' => array(
     745                                'expected'      => array(
     746                                        (object) array(
     747                                                'id'    => 1,
     748                                                'value' => 'one',
     749                                        ),
     750                                        (object) array(
     751                                                'id'    => 2,
     752                                                'value' => 'two',
     753                                        ),
     754                                        (object) array(
     755                                                'id'    => 3,
     756                                                'value' => 'three',
     757                                        ),
     758                                        (object) array(
     759                                                'id'    => 4,
     760                                                'value' => 'four',
     761                                        ),
     762                                ),
     763                                'target_array'  => array(
     764                                        4 => (object) array(
     765                                                'id'    => 4,
     766                                                'value' => 'four',
     767                                        ),
     768                                        2 => (object) array(
     769                                                'id'    => 2,
     770                                                'value' => 'two',
     771                                        ),
     772                                        3 => (object) array(
     773                                                'id'    => 3,
     774                                                'value' => 'three',
     775                                        ),
     776                                        1 => (object) array(
     777                                                'id'    => 1,
     778                                                'value' => 'one',
     779                                        ),
     780                                ),
     781                                'orderby'       => 'id',
     782                                'order'         => 'ASC',
     783                                'preserve_keys' => false,
     784                        ),
     785                        'object[], int keys, $orderby an existing field, $order = DESC and $preserve_keys = true' => array(
     786                                'expected'      => array(
     787                                        3 => (object) array(
     788                                                'id'    => 4,
     789                                                'value' => 'four',
     790                                        ),
     791                                        2 => (object) array(
     792                                                'id'    => 3,
     793                                                'value' => 'three',
     794                                        ),
     795                                        1 => (object) array(
     796                                                'id'    => 2,
     797                                                'value' => 'two',
     798                                        ),
     799                                        0 => (object) array(
     800                                                'id'    => 1,
     801                                                'value' => 'one',
     802                                        ),
     803                                ),
     804                                'target_array'  => array(
     805                                        (object) array(
     806                                                'id'    => 1,
     807                                                'value' => 'one',
     808                                        ),
     809                                        (object) array(
     810                                                'id'    => 2,
     811                                                'value' => 'two',
     812                                        ),
     813                                        (object) array(
     814                                                'id'    => 3,
     815                                                'value' => 'three',
     816                                        ),
     817                                        (object) array(
     818                                                'id'    => 4,
     819                                                'value' => 'four',
     820                                        ),
     821                                ),
     822                                'orderby'       => 'id',
     823                                'order'         => 'DESC',
     824                                'preserve_keys' => true,
     825                        ),
     826                        'object[], string keys, no ordering' => array(
     827                                'expected'     => array(
     828                                        'four'  => (object) array( 'value' => 'four' ),
     829                                        'two'   => (object) array( 'value' => 'two' ),
     830                                        'three' => (object) array( 'value' => 'three' ),
     831                                        'one'   => (object) array( 'value' => 'one' ),
     832                                ),
     833                                'target_array' => array(
     834                                        'four'  => (object) array( 'value' => 'four' ),
     835                                        'two'   => (object) array( 'value' => 'two' ),
     836                                        'three' => (object) array( 'value' => 'three' ),
     837                                        'one'   => (object) array( 'value' => 'one' ),
     838                                ),
     839                        ),
     840                        'object[], string keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
     841                                'expected'      => array(
     842                                        'four'  => (object) array( 'value' => 'four' ),
     843                                        'two'   => (object) array( 'value' => 'two' ),
     844                                        'three' => (object) array( 'value' => 'three' ),
     845                                        'one'   => (object) array( 'value' => 'one' ),
     846                                ),
     847                                'target_array'  => array(
     848                                        'four'  => (object) array( 'value' => 'four' ),
     849                                        'two'   => (object) array( 'value' => 'two' ),
     850                                        'three' => (object) array( 'value' => 'three' ),
     851                                        'one'   => (object) array( 'value' => 'one' ),
     852                                ),
     853                                'orderby'       => 'id',
     854                                'order'         => 'DESC',
     855                                'preserve_keys' => true,
     856                        ),
     857                        'object[], string keys, $orderby an existing field, $order = ASC and $preserve_keys = false' => array(
     858                                'expected'      => array(
     859                                        (object) array(
     860                                                'id'    => 1,
     861                                                'value' => 'one',
     862                                        ),
     863                                        (object) array(
     864                                                'id'    => 2,
     865                                                'value' => 'two',
     866                                        ),
     867                                        (object) array(
     868                                                'id'    => 3,
     869                                                'value' => 'three',
     870                                        ),
     871                                        (object) array(
     872                                                'id'    => 4,
     873                                                'value' => 'four',
     874                                        ),
     875                                ),
     876                                'target_array'  => array(
     877                                        'four'  => (object) array(
     878                                                'id'    => 4,
     879                                                'value' => 'four',
     880                                        ),
     881                                        'two'   => (object) array(
     882                                                'id'    => 2,
     883                                                'value' => 'two',
     884                                        ),
     885                                        'three' => (object) array(
     886                                                'id'    => 3,
     887                                                'value' => 'three',
     888                                        ),
     889                                        'one'   => (object) array(
     890                                                'id'    => 1,
     891                                                'value' => 'one',
     892                                        ),
     893                                ),
     894                                'orderby'       => 'id',
     895                                'order'         => 'ASC',
     896                                'preserve_keys' => false,
     897                        ),
     898                        'object[], string keys, $orderby an existing field, $order = DESC and $preserve_keys = true' => array(
     899                                'expected'      => array(
     900                                        'four'  => (object) array(
     901                                                'id'    => 4,
     902                                                'value' => 'four',
     903                                        ),
     904                                        'three' => (object) array(
     905                                                'id'    => 3,
     906                                                'value' => 'three',
     907                                        ),
     908                                        'two'   => (object) array(
     909                                                'id'    => 2,
     910                                                'value' => 'two',
     911                                        ),
     912                                        'one'   => (object) array(
     913                                                'id'    => 1,
     914                                                'value' => 'one',
     915                                        ),
     916                                ),
     917                                'target_array'  => array(
     918                                        'one'   => (object) array(
     919                                                'id'    => 1,
     920                                                'value' => 'one',
     921                                        ),
     922                                        'two'   => (object) array(
     923                                                'id'    => 2,
     924                                                'value' => 'two',
     925                                        ),
     926                                        'three' => (object) array(
     927                                                'id'    => 3,
     928                                                'value' => 'three',
     929                                        ),
     930                                        'four'  => (object) array(
     931                                                'id'    => 4,
     932                                                'value' => 'four',
     933                                        ),
     934                                ),
     935                                'orderby'       => 'id',
     936                                'order'         => 'DESC',
     937                                'preserve_keys' => true,
     938                        ),
     939                );
     940        }
     941
     942        /**
     943         * Tests non-existent '$orderby' fields.
     944         *
     945         * In PHP < 7.0.0, the sorting behaviour is different, which Core does not
     946         * currently handle. Until this is fixed, or the minimum PHP version is
     947         * raised to PHP 7.0.0+, these tests will be skipped on PHP < 7.0.0.
     948         *
     949         * @ticket 55300
     950         *
     951         * @dataProvider data_wp_list_util_sort_php_7_or_greater
     952         *
     953         * @covers WP_List_Util::sort
     954         * @covers ::wp_list_sort
     955         *
     956         * @param array  $expected      The expected array.
     957         * @param array  $target_array  The array to create a list from.
     958         * @param array  $orderby       Optional. Either the field name to order by or an array of multiple orderby fields as $orderby => $order.
     959         *                              Default empty array.
     960         * @param string $order         Optional. Either 'ASC' or 'DESC'. Only used if $orderby is a string. Default 'ASC'.
     961         * @param bool   $preserve_keys Optional. Whether to preserve keys. Default false.
     962         */
     963        public function test_wp_list_util_sort_php_7_or_greater( $expected, $target_array, $orderby = array(), $order = 'ASC', $preserve_keys = false ) {
     964                if ( version_compare( PHP_VERSION, '7.0.0', '<' ) ) {
     965                        $this->markTestSkipped( 'This test can only run on PHP 7.0 or greater due to an unstable sort order.' );
     966                }
     967
     968                $util   = new WP_List_Util( $target_array );
     969                $actual = $util->sort( $orderby, $order, $preserve_keys );
     970
     971                $this->assertEqualSetsWithIndex(
     972                        $expected,
     973                        $actual,
     974                        'The sorted value did not match the expected value.'
     975                );
     976                $this->assertEqualSetsWithIndex(
     977                        $expected,
     978                        $util->get_output(),
     979                        '::get_output() did not return the expected value.'
     980                );
     981        }
     982
     983        /**
     984         * Data provider for test_wp_list_util_sort_php_7_or_greater().
     985         *
     986         * @return array[]
     987         */
     988        public function data_wp_list_util_sort_php_7_or_greater() {
     989                return array(
     990                        'int[], int keys, $orderby a non-existent field, $order = ASC and $preserve_keys = false' => array(
     991                                'expected'      => array( 4, 2, 3, 1 ),
     992                                'target_array'  => array(
     993                                        4 => 4,
     994                                        2 => 2,
     995                                        3 => 3,
     996                                        1 => 1,
     997                                ),
     998                                'orderby'       => 'id',
     999                                'order'         => 'ASC',
     1000                                'preserve_keys' => false,
     1001                        ),
     1002                        'int[], string keys, $orderby a non-existent field, $order = ASC and $preserve_keys = false' => array(
     1003                                'expected'      => array( 4, 2, 3, 1 ),
     1004                                'target_array'  => array(
     1005                                        'four'  => 4,
     1006                                        'two'   => 2,
     1007                                        'three' => 3,
     1008                                        'one'   => 1,
     1009                                ),
     1010                                'orderby'       => 'id',
     1011                                'order'         => 'ASC',
     1012                                'preserve_keys' => false,
     1013                        ),
     1014                        'string[], int keys, $orderby a non-existent field, $order = ASC and $preserve_keys = false' => array(
     1015                                'expected'      => array( 'four', 'two', 'three', 'one' ),
     1016                                'target_array'  => array(
     1017                                        4 => 'four',
     1018                                        2 => 'two',
     1019                                        3 => 'three',
     1020                                        1 => 'one',
     1021                                ),
     1022                                'orderby'       => 'id',
     1023                                'order'         => 'ASC',
     1024                                'preserve_keys' => false,
     1025                        ),
     1026                        'string[], string keys, $orderby a non-existent field, $order = ASC and $preserve_keys = false' => array(
     1027                                'expected'      => array( 'four', 'two', 'three', 'one' ),
     1028                                'target_array'  => array(
     1029                                        'four'  => 'four',
     1030                                        'two'   => 'two',
     1031                                        'three' => 'three',
     1032                                        'one'   => 'one',
     1033                                ),
     1034                                'orderby'       => 'id',
     1035                                'order'         => 'ASC',
     1036                                'preserve_keys' => false,
     1037                        ),
     1038                        'array[], int keys, $orderby a non-existent field, $order = ASC and $preserve_keys = false' => array(
     1039                                'expected'      => array(
     1040                                        array( 'value' => 'four' ),
     1041                                        array( 'value' => 'two' ),
     1042                                        array( 'value' => 'three' ),
     1043                                        array( 'value' => 'one' ),
     1044                                ),
     1045                                'target_array'  => array(
     1046                                        4 => array( 'value' => 'four' ),
     1047                                        2 => array( 'value' => 'two' ),
     1048                                        3 => array( 'value' => 'three' ),
     1049                                        1 => array( 'value' => 'one' ),
     1050                                ),
     1051                                'orderby'       => 'id',
     1052                                'order'         => 'ASC',
     1053                                'preserve_keys' => false,
     1054                        ),
     1055                        'array[], string keys, $orderby a non-existent field, $order = ASC and $preserve_keys = false' => array(
     1056                                'expected'      => array(
     1057                                        array( 'value' => 'four' ),
     1058                                        array( 'value' => 'two' ),
     1059                                        array( 'value' => 'three' ),
     1060                                        array( 'value' => 'one' ),
     1061                                ),
     1062                                'target_array'  => array(
     1063                                        'four'  => array( 'value' => 'four' ),
     1064                                        'two'   => array( 'value' => 'two' ),
     1065                                        'three' => array( 'value' => 'three' ),
     1066                                        'one'   => array( 'value' => 'one' ),
     1067                                ),
     1068                                'orderby'       => 'id',
     1069                                'order'         => 'ASC',
     1070                                'preserve_keys' => false,
     1071                        ),
     1072                        'object[], int keys, $orderby a non-existent field, $order = ASC and $preserve_keys = false' => array(
     1073                                'expected'      => array(
     1074                                        (object) array( 'value' => 'four' ),
     1075                                        (object) array( 'value' => 'two' ),
     1076                                        (object) array( 'value' => 'three' ),
     1077                                        (object) array( 'value' => 'one' ),
     1078                                ),
     1079                                'target_array'  => array(
     1080                                        4 => (object) array( 'value' => 'four' ),
     1081                                        2 => (object) array( 'value' => 'two' ),
     1082                                        3 => (object) array( 'value' => 'three' ),
     1083                                        1 => (object) array( 'value' => 'one' ),
     1084                                ),
     1085                                'orderby'       => 'id',
     1086                                'order'         => 'ASC',
     1087                                'preserve_keys' => false,
     1088                        ),
     1089                        'object[], int keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
     1090                                'expected'      => array(
     1091                                        4 => (object) array( 'value' => 'four' ),
     1092                                        2 => (object) array( 'value' => 'two' ),
     1093                                        3 => (object) array( 'value' => 'three' ),
     1094                                        1 => (object) array( 'value' => 'one' ),
     1095                                ),
     1096                                'target_array'  => array(
     1097                                        4 => (object) array( 'value' => 'four' ),
     1098                                        2 => (object) array( 'value' => 'two' ),
     1099                                        3 => (object) array( 'value' => 'three' ),
     1100                                        1 => (object) array( 'value' => 'one' ),
     1101                                ),
     1102                                'orderby'       => 'id',
     1103                                'order'         => 'DESC',
     1104                                'preserve_keys' => true,
     1105                        ),
     1106                        'object[], string keys, $orderby a non-existent field, $order = ASC and $preserve_keys = false' => array(
     1107                                'expected'      => array(
     1108                                        (object) array( 'value' => 'four' ),
     1109                                        (object) array( 'value' => 'two' ),
     1110                                        (object) array( 'value' => 'three' ),
     1111                                        (object) array( 'value' => 'one' ),
     1112                                ),
     1113                                'target_array'  => array(
     1114                                        'four'  => (object) array( 'value' => 'four' ),
     1115                                        'two'   => (object) array( 'value' => 'two' ),
     1116                                        'three' => (object) array( 'value' => 'three' ),
     1117                                        'one'   => (object) array( 'value' => 'one' ),
     1118                                ),
     1119                                'orderby'       => 'id',
     1120                                'order'         => 'ASC',
     1121                                'preserve_keys' => false,
     1122                        ),
     1123                        'object[], string keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
     1124                                'expected'      => array(
     1125                                        'four'  => (object) array( 'value' => 'four' ),
     1126                                        'two'   => (object) array( 'value' => 'two' ),
     1127                                        'three' => (object) array( 'value' => 'three' ),
     1128                                        'one'   => (object) array( 'value' => 'one' ),
     1129                                ),
     1130                                'target_array'  => array(
     1131                                        'four'  => (object) array( 'value' => 'four' ),
     1132                                        'two'   => (object) array( 'value' => 'two' ),
     1133                                        'three' => (object) array( 'value' => 'three' ),
     1134                                        'one'   => (object) array( 'value' => 'one' ),
     1135                                ),
     1136                                'orderby'       => 'id',
     1137                                'order'         => 'DESC',
     1138                                'preserve_keys' => true,
     1139                        ),
     1140                );
     1141        }
     1142
    551143}
Note: See TracChangeset for help on using the changeset viewer.