'no foo', 'baz' => 'default baz', ), $atts)); return "foo = {$foo}"; } add_shortcode('bartag', 'bartag_func'); // [baztag]content[/baztag] function baztag_func($atts, $content='') { return 'content = '.do_shortcode($content); } add_shortcode('baztag', 'baztag_func'); function dumptag_func($atts) { $out = ''; foreach ($atts as $k=>$v) $out .= "$k = $v\n"; return $out; } add_shortcode('dumptag', 'dumptag_func'); // suggested by markj for testing p-wrapping of shortcode output function paragraph_func($atts, $content='') { extract(shortcode_atts(array( 'class' => 'graf', ), $atts)); return "
$content
\n"; } add_shortcode('paragraph', 'paragraph_func'); class TestShortcode extends WPTestCase { function setUp() { parent::setUp(); add_shortcode('test-shortcode-tag', array(&$this, '_shortcode_tag')); #error_reporting(E_ALL); #ini_set('display_errors', '1'); $this->atts = null; $this->content = null; $this->tagname = null; } function _shortcode_tag($atts, $content=NULL, $tagname=NULL) { $this->atts = $atts; $this->content = $content; $this->tagname = $tagname; } function test_noatts() { do_shortcode('[test-shortcode-tag /]'); $this->assertEquals( '', $this->atts ); $this->assertEquals( 'test-shortcode-tag', $this->tagname ); } function test_one_att() { do_shortcode('[test-shortcode-tag foo="asdf" /]'); $this->assertEquals( array('foo' => 'asdf'), $this->atts ); $this->assertEquals( 'test-shortcode-tag', $this->tagname ); } function test_not_a_tag() { $out = do_shortcode('[not-a-shortcode-tag]'); $this->assertEquals( '[not-a-shortcode-tag]', $out ); } function test_two_atts() { do_shortcode('[test-shortcode-tag foo="asdf" bar="bing" /]'); $this->assertEquals( array('foo' => 'asdf', 'bar' => 'bing'), $this->atts ); $this->assertEquals( 'test-shortcode-tag', $this->tagname ); } function test_noatts_enclosing() { do_shortcode('[test-shortcode-tag]content[/test-shortcode-tag]'); $this->assertEquals( '', $this->atts ); $this->assertEquals( 'content', $this->content ); $this->assertEquals( 'test-shortcode-tag', $this->tagname ); } function test_one_att_enclosing() { do_shortcode('[test-shortcode-tag foo="bar"]content[/test-shortcode-tag]'); $this->assertEquals( array('foo' => 'bar'), $this->atts ); $this->assertEquals( 'content', $this->content ); $this->assertEquals( 'test-shortcode-tag', $this->tagname ); } function test_two_atts_enclosing() { do_shortcode('[test-shortcode-tag foo="bar" baz="bing"]content[/test-shortcode-tag]'); $this->assertEquals( array('foo' => 'bar', 'baz' => 'bing'), $this->atts ); $this->assertEquals( 'content', $this->content ); $this->assertEquals( 'test-shortcode-tag', $this->tagname ); } function test_unclosed() { $out = do_shortcode('[test-shortcode-tag]'); $this->assertEquals( '', $out ); $this->assertEquals( '', $this->atts ); $this->assertEquals( 'test-shortcode-tag', $this->tagname ); } function test_positional_atts_num() { $out = do_shortcode('[test-shortcode-tag 123]'); $this->assertEquals( '', $out ); $this->assertEquals( array(0=>'123'), $this->atts ); $this->assertEquals( 'test-shortcode-tag', $this->tagname ); } function test_positional_atts_url() { $out = do_shortcode('[test-shortcode-tag http://www.youtube.com/watch?v=eBGIQ7ZuuiU]'); $this->assertEquals( '', $out ); $this->assertEquals( array(0=>'http://www.youtube.com/watch?v=eBGIQ7ZuuiU'), $this->atts ); $this->assertEquals( 'test-shortcode-tag', $this->tagname ); } function test_positional_atts_quotes() { $out = do_shortcode('[test-shortcode-tag "something in quotes" "something else"]'); $this->assertEquals( '', $out ); $this->assertEquals( array(0=>'something in quotes', 1=>'something else'), $this->atts ); $this->assertEquals( 'test-shortcode-tag', $this->tagname ); } function test_positional_atts_mixed() { $out = do_shortcode('[test-shortcode-tag 123 http://wordpress.com/ 0 "foo" bar]'); $this->assertEquals( '', $out ); $this->assertEquals( array(0=>'123', 1=>'http://wordpress.com/', 2=>'0', 3=>'foo', 4=>'bar'), $this->atts ); $this->assertEquals( 'test-shortcode-tag', $this->tagname ); } function test_positional_and_named_atts() { $out = do_shortcode('[test-shortcode-tag 123 url=http://wordpress.com/ foo bar="baz"]'); $this->assertEquals( '', $out ); $this->assertEquals( array(0=>'123', 'url' => 'http://wordpress.com/', 1=>'foo', 'bar' => 'baz'), $this->atts ); $this->assertEquals( 'test-shortcode-tag', $this->tagname ); } function test_footag_default() { $out = do_shortcode('[footag]'); $this->assertEquals('foo = ', $out); } function test_footag_val() { $val = rand_str(); $out = do_shortcode('[footag foo="'.$val.'"]'); $this->assertEquals('foo = '.$val, $out); } function test_nested_tags() { $out = do_shortcode('[baztag][dumptag abc="foo" def=123 http://wordpress.com/][/baztag]'); $expected = <<