Make WordPress Core

Ticket #34090: 34090.2.diff

File 34090.2.diff, 2.2 KB (added by miqrogroove, 9 years ago)

Adds unit tests.

  • src/wp-includes/shortcodes.php

     
    8888 */
    8989function add_shortcode($tag, $func) {
    9090        global $shortcode_tags;
     91
     92        if ( '' == $tag || 0 !== preg_match( '@[<>&/\[\]\x00-\x20]@', $tag ) ) {
     93                _doing_it_wrong( __FUNCTION__, __( 'Invalid shortcode name. Do not use reserved chars: & / < > [ ]' ), '4.4' );
     94                return;
     95        }
     96
    9197        $shortcode_tags[ $tag ] = $func;
    9298}
    9399
  • tests/phpunit/tests/shortcode.php

     
    539539                $this->assertTrue( has_shortcode( $content_nested, 'gallery' ) );
    540540                remove_shortcode( 'foo' );
    541541        }
     542
     543        /**
     544         * Make sure invalid shortcode names are not allowed.
     545         *
     546         * @dataProvider data_registration_bad
     547         * @expectedIncorrectUsage add_shortcode
     548         */
     549        function test_registration_bad( $input, $expected ) {
     550                return $this->sub_registration( $input, $expected );
     551        }
     552       
     553        /**
     554         * Make sure valid shortcode names are allowed.
     555         *
     556         * @dataProvider data_registration_good
     557         */
     558        function test_registration_good( $input, $expected ) {
     559                return $this->sub_registration( $input, $expected );
     560        }
     561       
     562        function sub_registration( $input, $expected ) {
     563                add_shortcode( $input, '' );
     564                $actual = shortcode_exists( $input );
     565                $test = $this->assertEquals( $expected, $actual );
     566                if ( $actual ) remove_shortcode( $input );
     567                return $test;
     568        }
     569       
     570        function data_registration_bad() {
     571                return array(
     572                        array(
     573                                '<html>',
     574                                false,
     575                        ),
     576                        array(
     577                                '[shortcode]',
     578                                false,
     579                        ),
     580                        array(
     581                                'bad/',
     582                                false,
     583                        ),
     584                        array(
     585                                '/bad',
     586                                false,
     587                        ),
     588                        array(
     589                                'bad space',
     590                                false,
     591                        ),
     592                        array(
     593                                '&amp;',
     594                                false,
     595                        ),
     596                        array(
     597                                '',
     598                                false,
     599                        ),
     600                );
     601        }
     602
     603        function data_registration_good() {
     604                return array(
     605                        array(
     606                                'good!',
     607                                true,
     608                        ),
     609                        array(
     610                                'plain',
     611                                true,
     612                        ),
     613                        array(
     614                                'unreserved!#$%()*+,-.;?@^_{|}~chars',
     615                                true,
     616                        ),
     617                );
     618        }
    542619}