Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (8 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/filters.php

    r39430 r42343  
    99
    1010    function test_simple_filter() {
    11         $a = new MockAction();
    12         $tag = __FUNCTION__;
    13         $val = __FUNCTION__ . '_val';
    14 
    15         add_filter($tag, array($a, 'filter'));
    16         $this->assertEquals($val, apply_filters($tag, $val));
     11        $a   = new MockAction();
     12        $tag = __FUNCTION__;
     13        $val = __FUNCTION__ . '_val';
     14
     15        add_filter( $tag, array( $a, 'filter' ) );
     16        $this->assertEquals( $val, apply_filters( $tag, $val ) );
    1717
    1818        // only one event occurred for the hook, with empty args
    19         $this->assertEquals(1, $a->get_call_count());
     19        $this->assertEquals( 1, $a->get_call_count() );
    2020        // only our hook was called
    21         $this->assertEquals(array($tag), $a->get_tags());
     21        $this->assertEquals( array( $tag ), $a->get_tags() );
    2222
    2323        $argsvar = $a->get_args();
    24         $args = array_pop( $argsvar );
    25         $this->assertEquals(array($val), $args);
     24        $args    = array_pop( $argsvar );
     25        $this->assertEquals( array( $val ), $args );
    2626    }
    2727
    2828    function test_remove_filter() {
    29         $a = new MockAction();
    30         $tag = __FUNCTION__;
    31         $val = __FUNCTION__ . '_val';
    32 
    33         add_filter($tag, array($a, 'filter'));
    34         $this->assertEquals($val, apply_filters($tag, $val));
     29        $a   = new MockAction();
     30        $tag = __FUNCTION__;
     31        $val = __FUNCTION__ . '_val';
     32
     33        add_filter( $tag, array( $a, 'filter' ) );
     34        $this->assertEquals( $val, apply_filters( $tag, $val ) );
    3535
    3636        // make sure our hook was called correctly
    37         $this->assertEquals(1, $a->get_call_count());
    38         $this->assertEquals(array($tag), $a->get_tags());
     37        $this->assertEquals( 1, $a->get_call_count() );
     38        $this->assertEquals( array( $tag ), $a->get_tags() );
    3939
    4040        // now remove the filter, do it again, and make sure it's not called this time
    41         remove_filter($tag, array($a, 'filter'));
    42         $this->assertEquals($val, apply_filters($tag, $val));
    43         $this->assertEquals(1, $a->get_call_count());
    44         $this->assertEquals(array($tag), $a->get_tags());
     41        remove_filter( $tag, array( $a, 'filter' ) );
     42        $this->assertEquals( $val, apply_filters( $tag, $val ) );
     43        $this->assertEquals( 1, $a->get_call_count() );
     44        $this->assertEquals( array( $tag ), $a->get_tags() );
    4545
    4646    }
     
    5050            $func = __FUNCTION__ . '_func';
    5151
    52             $this->assertFalse( has_filter($tag, $func) );
    53             $this->assertFalse( has_filter($tag) );
    54             add_filter($tag, $func);
    55             $this->assertEquals( 10, has_filter($tag, $func) );
    56             $this->assertTrue( has_filter($tag) );
    57             remove_filter($tag, $func);
    58             $this->assertFalse( has_filter($tag, $func) );
    59             $this->assertFalse( has_filter($tag) );
     52            $this->assertFalse( has_filter( $tag, $func ) );
     53            $this->assertFalse( has_filter( $tag ) );
     54            add_filter( $tag, $func );
     55            $this->assertEquals( 10, has_filter( $tag, $func ) );
     56            $this->assertTrue( has_filter( $tag ) );
     57            remove_filter( $tag, $func );
     58            $this->assertFalse( has_filter( $tag, $func ) );
     59            $this->assertFalse( has_filter( $tag ) );
    6060    }
    6161
    6262    // one tag with multiple filters
    6363    function test_multiple_filters() {
    64         $a1 = new MockAction();
    65         $a2 = new MockAction();
     64        $a1  = new MockAction();
     65        $a2  = new MockAction();
    6666        $tag = __FUNCTION__;
    6767        $val = __FUNCTION__ . '_val';
    6868
    6969        // add both filters to the hook
    70         add_filter($tag, array($a1, 'filter'));
    71         add_filter($tag, array($a2, 'filter'));
    72 
    73         $this->assertEquals($val, apply_filters($tag, $val));
     70        add_filter( $tag, array( $a1, 'filter' ) );
     71        add_filter( $tag, array( $a2, 'filter' ) );
     72
     73        $this->assertEquals( $val, apply_filters( $tag, $val ) );
    7474
    7575        // both filters called once each
    76         $this->assertEquals(1, $a1->get_call_count());
    77         $this->assertEquals(1, $a2->get_call_count());
     76        $this->assertEquals( 1, $a1->get_call_count() );
     77        $this->assertEquals( 1, $a2->get_call_count() );
    7878    }
    7979
    8080    function test_filter_args_1() {
    81         $a = new MockAction();
     81        $a    = new MockAction();
    8282        $tag  = __FUNCTION__;
    8383        $val  = __FUNCTION__ . '_val';
    8484        $arg1 = __FUNCTION__ . '_arg1';
    8585
    86         add_filter($tag, array($a, 'filter'), 10, 2);
     86        add_filter( $tag, array( $a, 'filter' ), 10, 2 );
    8787        // call the filter with a single argument
    88         $this->assertEquals($val, apply_filters($tag, $val, $arg1));
    89 
    90         $this->assertEquals(1, $a->get_call_count());
     88        $this->assertEquals( $val, apply_filters( $tag, $val, $arg1 ) );
     89
     90        $this->assertEquals( 1, $a->get_call_count() );
    9191        $argsvar = $a->get_args();
    9292        $this->assertEquals( array( $val, $arg1 ), array_pop( $argsvar ) );
     
    9494
    9595    function test_filter_args_2() {
    96         $a1 = new MockAction();
    97         $a2 = new MockAction();
     96        $a1   = new MockAction();
     97        $a2   = new MockAction();
    9898        $tag  = __FUNCTION__;
    9999        $val  = __FUNCTION__ . '_val';
     
    102102
    103103        // a1 accepts two arguments, a2 doesn't
    104         add_filter($tag, array($a1, 'filter'), 10, 3);
    105         add_filter($tag, array($a2, 'filter'));
     104        add_filter( $tag, array( $a1, 'filter' ), 10, 3 );
     105        add_filter( $tag, array( $a2, 'filter' ) );
    106106        // call the filter with two arguments
    107         $this->assertEquals($val, apply_filters($tag, $val, $arg1, $arg2));
     107        $this->assertEquals( $val, apply_filters( $tag, $val, $arg1, $arg2 ) );
    108108
    109109        // a1 should be called with both args
    110         $this->assertEquals(1, $a1->get_call_count());
     110        $this->assertEquals( 1, $a1->get_call_count() );
    111111        $argsvar1 = $a1->get_args();
    112112        $this->assertEquals( array( $val, $arg1, $arg2 ), array_pop( $argsvar1 ) );
    113113
    114114        // a2 should be called with one only
    115         $this->assertEquals(1, $a2->get_call_count());
     115        $this->assertEquals( 1, $a2->get_call_count() );
    116116        $argsvar2 = $a2->get_args();
    117117        $this->assertEquals( array( $val ), array_pop( $argsvar2 ) );
     
    119119
    120120    function test_filter_priority() {
    121         $a = new MockAction();
     121        $a   = new MockAction();
    122122        $tag = __FUNCTION__;
    123123        $val = __FUNCTION__ . '_val';
    124124
    125125        // make two filters with different priorities
    126         add_filter($tag, array($a, 'filter'), 10);
    127         add_filter($tag, array($a, 'filter2'), 9);
    128         $this->assertEquals($val, apply_filters($tag, $val));
     126        add_filter( $tag, array( $a, 'filter' ), 10 );
     127        add_filter( $tag, array( $a, 'filter2' ), 9 );
     128        $this->assertEquals( $val, apply_filters( $tag, $val ) );
    129129
    130130        // there should be two events, one per filter
    131         $this->assertEquals(2, $a->get_call_count());
    132 
    133         $expected = array (
     131        $this->assertEquals( 2, $a->get_call_count() );
     132
     133        $expected = array(
    134134            // filter2 is called first because it has priority 9
    135             array (
     135            array(
    136136                'filter' => 'filter2',
    137                 'tag' => $tag,
    138                 'args' => array($val)
     137                'tag'    => $tag,
     138                'args'   => array( $val ),
    139139            ),
    140140            // filter 1 is called second
    141             array (
     141            array(
    142142                'filter' => 'filter',
    143                 'tag' => $tag,
    144                 'args' => array($val)
     143                'tag'    => $tag,
     144                'args'   => array( $val ),
    145145            ),
    146146        );
    147147
    148         $this->assertEquals($expected, $a->get_events());
     148        $this->assertEquals( $expected, $a->get_events() );
    149149    }
    150150
    151151    function test_all_filter() {
    152         $a = new MockAction();
     152        $a    = new MockAction();
    153153        $tag1 = __FUNCTION__ . '_1';
    154154        $tag2 = __FUNCTION__ . '_2';
     
    156156
    157157        // add an 'all' filter
    158         add_filter('all', array($a, 'filterall'));
     158        add_filter( 'all', array( $a, 'filterall' ) );
    159159        // do some filters
    160         $this->assertEquals($val, apply_filters($tag1, $val));
    161         $this->assertEquals($val, apply_filters($tag2, $val));
    162         $this->assertEquals($val, apply_filters($tag1, $val));
    163         $this->assertEquals($val, apply_filters($tag1, $val));
     160        $this->assertEquals( $val, apply_filters( $tag1, $val ) );
     161        $this->assertEquals( $val, apply_filters( $tag2, $val ) );
     162        $this->assertEquals( $val, apply_filters( $tag1, $val ) );
     163        $this->assertEquals( $val, apply_filters( $tag1, $val ) );
    164164
    165165        // our filter should have been called once for each apply_filters call
    166         $this->assertEquals(4, $a->get_call_count());
     166        $this->assertEquals( 4, $a->get_call_count() );
    167167        // the right hooks should have been called in order
    168         $this->assertEquals(array($tag1, $tag2, $tag1, $tag1), $a->get_tags());
    169 
    170         remove_filter('all', array($a, 'filterall'));
    171         $this->assertFalse( has_filter('all', array($a, 'filterall')) );
     168        $this->assertEquals( array( $tag1, $tag2, $tag1, $tag1 ), $a->get_tags() );
     169
     170        remove_filter( 'all', array( $a, 'filterall' ) );
     171        $this->assertFalse( has_filter( 'all', array( $a, 'filterall' ) ) );
    172172
    173173    }
    174174
    175175    function test_remove_all_filter() {
    176         $a = new MockAction();
    177         $tag = __FUNCTION__;
    178         $val = __FUNCTION__ . '_val';
    179 
    180         add_filter('all', array($a, 'filterall'));
    181         $this->assertTrue( has_filter('all') );
    182         $this->assertEquals( 10, has_filter('all', array($a, 'filterall')) );
    183         $this->assertEquals($val, apply_filters($tag, $val));
     176        $a   = new MockAction();
     177        $tag = __FUNCTION__;
     178        $val = __FUNCTION__ . '_val';
     179
     180        add_filter( 'all', array( $a, 'filterall' ) );
     181        $this->assertTrue( has_filter( 'all' ) );
     182        $this->assertEquals( 10, has_filter( 'all', array( $a, 'filterall' ) ) );
     183        $this->assertEquals( $val, apply_filters( $tag, $val ) );
    184184
    185185        // make sure our hook was called correctly
    186         $this->assertEquals(1, $a->get_call_count());
    187         $this->assertEquals(array($tag), $a->get_tags());
     186        $this->assertEquals( 1, $a->get_call_count() );
     187        $this->assertEquals( array( $tag ), $a->get_tags() );
    188188
    189189        // now remove the filter, do it again, and make sure it's not called this time
    190         remove_filter('all', array($a, 'filterall'));
    191         $this->assertFalse( has_filter('all', array($a, 'filterall')) );
    192         $this->assertFalse( has_filter('all') );
    193         $this->assertEquals($val, apply_filters($tag, $val));
     190        remove_filter( 'all', array( $a, 'filterall' ) );
     191        $this->assertFalse( has_filter( 'all', array( $a, 'filterall' ) ) );
     192        $this->assertFalse( has_filter( 'all' ) );
     193        $this->assertEquals( $val, apply_filters( $tag, $val ) );
    194194        // call cound should remain at 1
    195         $this->assertEquals(1, $a->get_call_count());
    196         $this->assertEquals(array($tag), $a->get_tags());
     195        $this->assertEquals( 1, $a->get_call_count() );
     196        $this->assertEquals( array( $tag ), $a->get_tags() );
    197197    }
    198198
     
    201201     */
    202202    function test_remove_all_filters_should_respect_the_priority_argument() {
    203         $a = new MockAction();
     203        $a   = new MockAction();
    204204        $tag = __FUNCTION__;
    205205        $val = __FUNCTION__ . '_val';
     
    221221    function test_filter_ref_array() {
    222222        $obj = new stdClass();
    223         $a = new MockAction();
    224         $tag = __FUNCTION__;
    225 
    226         add_action($tag, array($a, 'filter'));
    227 
    228         apply_filters_ref_array($tag, array(&$obj));
     223        $a   = new MockAction();
     224        $tag = __FUNCTION__;
     225
     226        add_action( $tag, array( $a, 'filter' ) );
     227
     228        apply_filters_ref_array( $tag, array( &$obj ) );
    229229
    230230        $args = $a->get_args();
    231         $this->assertSame($args[0][0], $obj);
     231        $this->assertSame( $args[0][0], $obj );
    232232        // just in case we don't trust assertSame
    233233        $obj->foo = true;
    234         $this->assertFalse( empty($args[0][0]->foo) );
     234        $this->assertFalse( empty( $args[0][0]->foo ) );
    235235    }
    236236
     
    240240    function test_filter_ref_array_result() {
    241241        $obj = new stdClass();
    242         $a = new MockAction();
    243         $b = new MockAction();
    244         $tag = __FUNCTION__;
    245 
    246         add_action($tag, array($a, 'filter_append'), 10, 2);
    247         add_action($tag, array($b, 'filter_append'), 10, 2);
    248 
    249         $result = apply_filters_ref_array($tag, array('string', &$obj));
    250 
    251         $this->assertEquals($result, 'string_append_append');
     242        $a   = new MockAction();
     243        $b   = new MockAction();
     244        $tag = __FUNCTION__;
     245
     246        add_action( $tag, array( $a, 'filter_append' ), 10, 2 );
     247        add_action( $tag, array( $b, 'filter_append' ), 10, 2 );
     248
     249        $result = apply_filters_ref_array( $tag, array( 'string', &$obj ) );
     250
     251        $this->assertEquals( $result, 'string_append_append' );
    252252
    253253        $args = $a->get_args();
    254         $this->assertSame($args[0][1], $obj);
     254        $this->assertSame( $args[0][1], $obj );
    255255        // just in case we don't trust assertSame
    256256        $obj->foo = true;
    257         $this->assertFalse( empty($args[0][1]->foo) );
     257        $this->assertFalse( empty( $args[0][1]->foo ) );
    258258
    259259        $args = $b->get_args();
    260         $this->assertSame($args[0][1], $obj);
     260        $this->assertSame( $args[0][1], $obj );
    261261        // just in case we don't trust assertSame
    262262        $obj->foo = true;
    263         $this->assertFalse( empty($args[0][1]->foo) );
    264 
    265     }
    266 
    267     function _self_removal($tag) {
    268         remove_action( $tag, array($this, '_self_removal'), 10, 1 );
     263        $this->assertFalse( empty( $args[0][1]->foo ) );
     264
     265    }
     266
     267    function _self_removal( $tag ) {
     268        remove_action( $tag, array( $this, '_self_removal' ), 10, 1 );
    269269        return $tag;
    270270    }
     
    274274     */
    275275    function test_has_filter_after_remove_all_filters() {
    276         $a = new MockAction();
     276        $a   = new MockAction();
    277277        $tag = __FUNCTION__;
    278278        $val = __FUNCTION__ . '_val';
     
    379379    public function _other_priority_action() {
    380380        global $wp_filter;
    381         $this->current_priority = $wp_filter[ 'the_content' ]->current_priority();
     381        $this->current_priority = $wp_filter['the_content']->current_priority();
    382382    }
    383383}
Note: See TracChangeset for help on using the changeset viewer.