WordPress.org

Make WordPress Core

Changeset 1239 in tests


Ignore:
Timestamp:
03/06/2013 08:09:10 PM (9 years ago)
Author:
nacin
Message:

Add shortcode_atts() filter tests. see #15155. props coffee2code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/shortcode.php

    r1189 r1239  
    3030        $this->content = $content;
    3131        $this->tagname = $tagname;
     32        $this->filter_atts_out = null;
     33        $this->filter_atts_pairs = null;
     34        $this->filter_atts_atts = null;
    3235    }
    3336
     
    4245            'foo' => 'no foo',
    4346            'baz' => 'default baz',
    44         ), $atts));
     47        ), $atts, 'bartag'));
    4548
    4649        return "foo = {$foo}";
     
    325328        $this->assertEquals('beforeafter', strip_shortcodes('before[gallery]after'));
    326329    }
     330
     331
     332    // Store passed in shortcode_atts_{$shortcode} args
     333    function _filter_atts( $out, $pairs, $atts ) {
     334        $this->filter_atts_out = $out;
     335        $this->filter_atts_pairs = $pairs;
     336        $this->filter_atts_atts = $atts;
     337        return $out;
     338    }
     339
     340    // Filter shortcode atts in various ways
     341    function _filter_atts2( $out, $pairs, $atts ) {
     342        // If foo attribute equals "foo1", change it to be default value
     343        if ( isset( $out['foo'] ) && 'foo1' == $out['foo'] )
     344            $out['foo'] = $pairs['foo'];
     345
     346        // If baz attribute is set, remove it
     347        if ( isset( $out['baz'] ) )
     348            unset( $out['baz'] );
     349
     350        $this->filter_atts_out = $out;
     351        return $out;
     352    }
     353
     354    function test_shortcode_atts_filter_passes_original_arguments() {
     355        add_filter( 'shortcode_atts_bartag', array( $this, '_filter_atts' ), 10, 3 );
     356
     357        do_shortcode('[bartag foo="foo1" /]');
     358        $this->assertEquals( array( 'foo' => 'foo1', 'baz' => 'default baz' ), $this->filter_atts_out );
     359        $this->assertEquals( array( 'foo' => 'no foo', 'baz' => 'default baz' ), $this->filter_atts_pairs );
     360        $this->assertEquals( array( 'foo' => 'foo1' ), $this->filter_atts_atts );
     361
     362        remove_filter( 'shortcode_atts_bartag', array( $this, '_filter_atts' ), 10, 3 );
     363    }
     364
     365    function test_shortcode_atts_filtering() {
     366        add_filter( 'shortcode_atts_bartag', array( $this, '_filter_atts2' ), 10, 3 );
     367
     368        $out = do_shortcode('[bartag foo="foo1" baz="baz1" /]');
     369        $this->assertEquals( array( 'foo' => 'no foo' ), $this->filter_atts_out );
     370        $this->assertEquals( 'foo = no foo', $out );
     371
     372        $out = do_shortcode('[bartag foo="foo2" /]');
     373        $this->assertEquals( 'foo = foo2', $out );
     374
     375        remove_filter( 'shortcode_atts_bartag', array( $this, '_filter_atts2' ), 10, 3 );
     376    }
     377
    327378}
Note: See TracChangeset for help on using the changeset viewer.