WordPress.org

Make WordPress Core


Ignore:
Timestamp:
06/29/2014 09:25:21 PM (5 years ago)
Author:
nacin
Message:

Add index key support for wp_list_pluck(), à la array_column().

props trepmal.
fixes #28666.

File:
1 edited

Legend:

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

    r25002 r28900  
    1212    function setUp() {
    1313        parent::setUp();
    14         $this->array_list['foo'] = array( 'name' => 'foo', 'field1' => true, 'field2' => true, 'field3' => true, 'field4' => array( 'red' ) );
    15         $this->array_list['bar'] = array( 'name' => 'bar', 'field1' => true, 'field2' => true, 'field3' => false, 'field4' => array( 'green' ) );
    16         $this->array_list['baz'] = array( 'name' => 'baz', 'field1' => true, 'field2' => false, 'field3' => false, 'field4' => array( 'blue' ) );
     14        $this->array_list['foo'] = array( 'name' => 'foo', 'id' => 'f', 'field1' => true, 'field2' => true, 'field3' => true, 'field4' => array( 'red' ) );
     15        $this->array_list['bar'] = array( 'name' => 'bar', 'id' => 'b', 'field1' => true, 'field2' => true, 'field3' => false, 'field4' => array( 'green' ) );
     16        $this->array_list['baz'] = array( 'name' => 'baz', 'id' => 'z', 'field1' => true, 'field2' => false, 'field3' => false, 'field4' => array( 'blue' ) );
    1717        foreach ( $this->array_list as $key => $value ) {
    1818            $this->object_list[ $key ] = (object) $value;
     
    6767    }
    6868
     69    /**
     70     * @ticket 28666
     71     */
     72    function test_wp_list_pluck_index_key() {
     73        $list = wp_list_pluck( $this->array_list, 'name', 'id' );
     74        $this->assertEquals( array( 'f' => 'foo', 'b' => 'bar', 'z' => 'baz' ), $list );
     75    }
     76
     77    /**
     78     * @ticket 28666
     79     */
     80    function test_wp_list_pluck_object_index_key() {
     81        $list = wp_list_pluck( $this->object_list, 'name', 'id' );
     82        $this->assertEquals( array( 'f' => 'foo', 'b' => 'bar', 'z' => 'baz' ), $list );
     83    }
     84
     85    /**
     86     * @ticket 28666
     87     */
     88    function test_wp_list_pluck_missing_index_key() {
     89        $list = wp_list_pluck( $this->array_list, 'name', 'nonexistent' );
     90        $this->assertEquals( array( 0 => 'foo', 1 => 'bar', 2 => 'baz' ), $list );
     91    }
     92
     93    /**
     94     * @ticket 28666
     95     */
     96    function test_wp_list_pluck_partial_missing_index_key() {
     97        $array_list = $this->array_list;
     98        unset( $array_list[ 'bar']['id'] );
     99        $list = wp_list_pluck( $array_list, 'name', 'id' );
     100        $this->assertEquals( array( 'f' => 'foo', 0 => 'bar', 'z' => 'baz' ), $list );
     101    }
     102
     103    /**
     104     * @ticket 28666
     105     */
     106    function test_wp_list_pluck_mixed_index_key() {
     107        $mixed_list = $this->array_list;
     108        $mixed_list['bar'] = (object) $mixed_list['bar'];
     109        $list = wp_list_pluck( $mixed_list, 'name', 'id' );
     110        $this->assertEquals( array( 'f' => 'foo', 'b' => 'bar', 'z' => 'baz' ), $list );
     111    }
     112
    69113    function test_filter_object_list_nested_array_and() {
    70114        $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'blue' ) ), 'AND' );
Note: See TracChangeset for help on using the changeset viewer.