Make WordPress Core

Ticket #54751: 54751.2.diff

File 54751.2.diff, 4.1 KB (added by mkox, 20 months ago)

Corrected diff direction

  • src/wp-includes/class-wp-list-util.php

    diff --git a/src/wp-includes/class-wp-list-util.php b/src/wp-includes/class-wp-list-util.php
    index 849807dc86..368c46bd01 100644
    a b class WP_List_Util { 
    144144         *
    145145         * This has the same functionality and prototype of
    146146         * array_column() (PHP 5.5) but also supports objects.
     147         *
     148         * Displays a `_doing_it_wrong()` notice when object is not an array.
    147149         *
    148150         * @since 4.7.0
    149151         *
    150152         * @param int|string $field     Field to fetch from the object or array.
    151153         * @param int|string $index_key Optional. Field from the element to use as keys for the new array.
    152154         *                              Default null.
    153          * @return array Array of found values. If `$index_key` is set, an array of found values with keys
    154          *               corresponding to `$index_key`. If `$index_key` is null, array keys from the original
    155          *               `$list` will be preserved in the results.
     155         * @return array|WP_Error               Array of found values. If `$index_key` is set, an array of found
     156         *                                                              values with keys corresponding to `$index_key`. If `$index_key` is
     157         *                                                              null, array keys from the original `$list` will be preserved in the
     158         *                                                              results on success,
     159         *                                                              WP_Error object on failure.
    156160         */
    157161        public function pluck( $field, $index_key = null ) {
     162               
     163                if ( ! is_array( $this->output ) ) {
     164                        /*
     165                         * Check if given object is an array.
     166                         * Return an error if target is no array.
     167                         */
     168                        _doing_it_wrong(
     169                                __METHOD__,
     170                                __( 'Input object must be an array.' ),
     171                                '6.0.0'
     172                        );
     173                        return new WP_Error( 'input_object_is_no_array', __( 'List input object to pluck from must be an array.' ) );
     174                }
     175
    158176                $newlist = array();
    159177
    160178                if ( ! $index_key ) {
  • tests/phpunit/tests/functions/wpListUtil.php

    diff --git a/tests/phpunit/tests/functions/wpListUtil.php b/tests/phpunit/tests/functions/wpListUtil.php
    index 45cad6627a..02f4d3c25d 100644
    a b class Tests_Functions_wpListUtil extends WP_UnitTestCase { 
    5252                $this->assertEqualSets( $expected, $actual );
    5353                $this->assertEqualSets( $expected, $util->get_output() );
    5454        }
     55
     56        /**
     57         * @covers WP_List_Util::pluck
     58         */
     59        public function test_wp_list_util_pluck() {
     60                $expected = array(
     61                        0 => 'buz',
     62                        1 => 'baz',
     63                );
     64
     65                $util   = new WP_List_Util(
     66                        array(
     67                                (object) array(
     68                                        'foo' => 'bar',
     69                                        'bar' => 'buz',
     70                                ),
     71                                (object) array( 'bar' => 'baz' ),
     72                        )
     73                );
     74                $actual = $util->pluck( 'bar' );
     75
     76                $this->assertEqualSets( $expected, $actual );
     77                $this->assertEqualSets( $expected, $util->get_output() );
     78        }
     79
     80        /**
     81         * @covers WP_List_Util::pluck
     82         */
     83        public function test_wp_list_util_pluck_with_index_key() {
     84                $expected = array(
     85                        'bar1' => 'buz',
     86                        'bar2' => 'baz',
     87                );
     88
     89                $util   = new WP_List_Util(
     90                        array(
     91                                (object) array(
     92                                        'test_key' => 'bar1',
     93                                        'test_field' => 'buz',
     94                                ),
     95                                (object) array(
     96                                        'test_key' => 'bar2',
     97                                        'test_field' => 'baz',
     98                                ),
     99                        )
     100                );
     101                $actual = $util->pluck( 'test_field' , 'test_key' );
     102
     103                $this->assertEqualSets( $expected, $actual );
     104                $this->assertEqualSets( $expected, $util->get_output() );
     105        }
     106
     107        /**
     108         * Test for doing it wrong on invalid argument, type string.
     109         *
     110         * @covers WP_List_Util::pluck
     111         * @expectedIncorrectUsage WP_List_Util::pluck
     112         * @ticket 54751
     113         */
     114        public function test_wp_list_util_pluck_doing_it_wrong_when_string_is_given() {
     115
     116                $this->setExpectedIncorrectUsage( 'WP_List_Util::pluck' );
     117
     118                $util   = new WP_List_Util(
     119                        'invalid'
     120                );
     121
     122                $actual = $util->pluck( 'bar' );
     123
     124                $this->assertInstanceOf( 'WP_Error', $actual );
     125        }
     126
     127        /**
     128         * Test for doing it wrong on invalid argument, type integer.
     129         *
     130         * @covers WP_List_Util::pluck
     131         * @expectedIncorrectUsage WP_List_Util::pluck
     132         * @ticket 54751
     133         */
     134        public function test_wp_list_util_pluck_doing_it_wrong_when_integer_is_given() {
     135
     136                $this->setExpectedIncorrectUsage( 'WP_List_Util::pluck' );
     137
     138                $util   = new WP_List_Util(
     139                        15101994
     140                );
     141
     142                $actual = $util->pluck( 'bar' );
     143
     144                $this->assertInstanceOf( 'WP_Error', $actual );
     145        }
    55146}