diff --git a/tests/phpunit/tests/shortcodes/applyShortcodes.php b/tests/phpunit/tests/shortcodes/applyShortcodes.php new file mode 100644 index 0000000000000..71c5c3631ffca --- /dev/null +++ b/tests/phpunit/tests/shortcodes/applyShortcodes.php @@ -0,0 +1,103 @@ + 'World' ), $atts ); + + return 'Hello ' . $atts['name'] . ( '' !== (string) $content ? ' [' . $content . ']' : '' ); + } + + /** + * Tests that the shortcodes are processed. + * + * @ticket 66500 + * + * @dataProvider data_content + * + * @param string $content Content with shortcodes. + * @param string $expected Expected content. + */ + public function test_should_process_the_shortcodes( $content, $expected ) { + $this->assertSame( $expected, apply_shortcodes( $content ) ); + } + + /** + * Tests that the result is the same as the one of do_shortcode(). + * + * @ticket 66500 + * + * @dataProvider data_content + * + * @param string $content Content with shortcodes. + */ + public function test_should_return_the_same_result_as_do_shortcode( $content ) { + $this->assertSame( do_shortcode( $content ), apply_shortcodes( $content ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_content() { + return array( + 'self closing' => array( 'A [apply-greeting /] B', 'A Hello World B' ), + 'with an attribute' => array( '[apply-greeting name="Bob"]', 'Hello Bob' ), + 'enclosing content' => array( '[apply-greeting]text[/apply-greeting]', 'Hello World [text]' ), + 'escaped shortcode' => array( '[[apply-greeting]]', '[apply-greeting]' ), + 'unregistered shortcode' => array( '[apply-unknown]', '[apply-unknown]' ), + 'content without brackets' => array( 'No shortcodes here', 'No shortcodes here' ), + 'empty string' => array( '', '' ), + ); + } + + /** + * Tests that the $ignore_html argument is passed on, so that shortcodes inside HTML elements are skipped. + * + * @ticket 66500 + */ + public function test_should_skip_the_shortcodes_inside_html_when_ignoring_html() { + $content = '[apply-greeting]'; + + $this->assertSame( 'Hello World', apply_shortcodes( $content, true ) ); + } + + /** + * Tests that the shortcodes inside HTML elements are processed by default. + * + * @ticket 66500 + */ + public function test_should_process_the_shortcodes_inside_html_by_default() { + $content = '[apply-greeting]'; + + $this->assertSame( 'Hello World', apply_shortcodes( $content ) ); + $this->assertSame( 'Hello World', apply_shortcodes( $content, false ) ); + } +} diff --git a/tests/phpunit/tests/shortcodes/getShortcodeTagsInContent.php b/tests/phpunit/tests/shortcodes/getShortcodeTagsInContent.php new file mode 100644 index 0000000000000..88e77af55d3d1 --- /dev/null +++ b/tests/phpunit/tests/shortcodes/getShortcodeTagsInContent.php @@ -0,0 +1,158 @@ +original_shortcode_tags = $shortcode_tags; + + remove_all_shortcodes(); + + add_shortcode( 'tags-alpha', '__return_empty_string' ); + add_shortcode( 'tags-beta', '__return_empty_string' ); + add_shortcode( 'tags-gamma', '__return_empty_string' ); + } + + public function tear_down() { + global $shortcode_tags; + $shortcode_tags = $this->original_shortcode_tags; + + parent::tear_down(); + } + + /** + * Tests that the registered shortcodes found in the content are returned, in the order they appear. + * + * @ticket 66500 + * + * @dataProvider data_content + * + * @param string $content Content to check. + * @param string[] $expected Expected tags. + */ + public function test_should_return_the_registered_tags_found_in_the_content( $content, $expected ) { + $this->assertSame( $expected, get_shortcode_tags_in_content( $content ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_content() { + return array( + 'one self closing shortcode' => array( '[tags-alpha /]', array( 'tags-alpha' ) ), + 'one shortcode without a slash' => array( '[tags-alpha]', array( 'tags-alpha' ) ), + 'one enclosing shortcode' => array( '[tags-alpha]text[/tags-alpha]', array( 'tags-alpha' ) ), + 'shortcode with attributes' => array( '[tags-alpha a="1" b=2 /]', array( 'tags-alpha' ) ), + 'shortcode in the middle of text' => array( 'Before [tags-beta] after', array( 'tags-beta' ) ), + 'tags in the order they appear' => array( '[tags-gamma] [tags-alpha] [tags-beta]', array( 'tags-gamma', 'tags-alpha', 'tags-beta' ) ), + 'repeated tags are repeated' => array( '[tags-alpha] [tags-alpha]', array( 'tags-alpha', 'tags-alpha' ) ), + 'unregistered shortcodes are ignored' => array( '[tags-alpha] [tags-unknown] [tags-beta /]', array( 'tags-alpha', 'tags-beta' ) ), + 'only unregistered shortcodes' => array( '[tags-unknown] [tags-other /]', array() ), + 'a tag that only starts like a registered tag' => array( '[tags-alphabet] [tags-alpha-b]', array() ), + 'a space after the bracket' => array( '[ tags-alpha]', array() ), + 'brackets that are not shortcodes' => array( 'An [array] of [1, 2] items', array() ), + 'content without brackets' => array( 'No shortcodes here', array() ), + 'empty string' => array( '', array() ), + ); + } + + /** + * Tests that the shortcodes inside the content of another shortcode are returned too. + * + * @ticket 66500 + * + * @dataProvider data_nested_content + * + * @param string $content Content to check. + * @param string[] $expected Expected tags. + */ + public function test_should_return_the_tags_nested_in_the_content_of_a_shortcode( $content, $expected ) { + $this->assertSame( $expected, get_shortcode_tags_in_content( $content ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_nested_content() { + return array( + 'one level' => array( + '[tags-alpha]x [tags-beta /] y[/tags-alpha]', + array( 'tags-alpha', 'tags-beta' ), + ), + 'two levels' => array( + '[tags-alpha][tags-beta][tags-gamma /][/tags-beta][/tags-alpha]', + array( 'tags-alpha', 'tags-beta', 'tags-gamma' ), + ), + 'nested tags before the next sibling' => array( + '[tags-alpha][tags-beta /][/tags-alpha] [tags-gamma /]', + array( 'tags-alpha', 'tags-beta', 'tags-gamma' ), + ), + 'the same tag nested in itself' => array( + '[tags-alpha][tags-alpha /][/tags-alpha]', + array( 'tags-alpha', 'tags-alpha' ), + ), + 'an unregistered shortcode around a registered one' => array( + '[tags-unknown][tags-beta /][/tags-unknown]', + array( 'tags-beta' ), + ), + 'an unregistered shortcode inside a registered one' => array( + '[tags-alpha][tags-unknown][tags-beta /][/tags-alpha]', + array( 'tags-alpha', 'tags-beta' ), + ), + 'a shortcode in the attributes is not returned' => array( + '[tags-alpha text="[tags-beta]" /]', + array( 'tags-alpha' ), + ), + ); + } + + /** + * Tests that only the shortcodes that are registered when the function is called are returned. + * + * @ticket 66500 + */ + public function test_should_use_the_shortcodes_registered_at_the_time_of_the_call() { + $content = '[tags-alpha] [tags-late]'; + + $this->assertSame( array( 'tags-alpha' ), get_shortcode_tags_in_content( $content ), 'The shortcode that is not registered yet should be ignored.' ); + + add_shortcode( 'tags-late', '__return_empty_string' ); + + $this->assertSame( array( 'tags-alpha', 'tags-late' ), get_shortcode_tags_in_content( $content ), 'The shortcode that was registered should be returned.' ); + + remove_shortcode( 'tags-alpha' ); + + $this->assertSame( array( 'tags-late' ), get_shortcode_tags_in_content( $content ), 'The shortcode that was removed should be ignored.' ); + } + + /** + * Tests that no tags are returned when there are no registered shortcodes. + * + * @ticket 66500 + */ + public function test_should_return_an_empty_array_when_no_shortcodes_are_registered() { + remove_all_shortcodes(); + + $this->assertSame( array(), get_shortcode_tags_in_content( '[tags-alpha] [gallery]' ) ); + } +} diff --git a/tests/phpunit/tests/shortcodes/removeAllShortcodes.php b/tests/phpunit/tests/shortcodes/removeAllShortcodes.php new file mode 100644 index 0000000000000..b3ad7fb29361e --- /dev/null +++ b/tests/phpunit/tests/shortcodes/removeAllShortcodes.php @@ -0,0 +1,109 @@ +original_shortcode_tags = $shortcode_tags; + } + + public function tear_down() { + global $shortcode_tags; + $shortcode_tags = $this->original_shortcode_tags; + + parent::tear_down(); + } + + /** + * Tests that all the registered shortcodes are removed. + * + * @ticket 66500 + */ + public function test_should_remove_all_the_registered_shortcodes() { + global $shortcode_tags; + + add_shortcode( 'remove-all-one', '__return_empty_string' ); + add_shortcode( 'remove-all-two', '__return_empty_string' ); + + $this->assertTrue( shortcode_exists( 'remove-all-one' ), 'The first shortcode should be registered.' ); + $this->assertTrue( shortcode_exists( 'remove-all-two' ), 'The second shortcode should be registered.' ); + + remove_all_shortcodes(); + + $this->assertFalse( shortcode_exists( 'remove-all-one' ), 'The first shortcode should be removed.' ); + $this->assertFalse( shortcode_exists( 'remove-all-two' ), 'The second shortcode should be removed.' ); + $this->assertSame( array(), $shortcode_tags, 'The list of shortcodes should be empty.' ); + } + + /** + * Tests that the shortcodes of WordPress are removed too. + * + * @ticket 66500 + */ + public function test_should_remove_the_shortcodes_of_core() { + $this->assertTrue( shortcode_exists( 'gallery' ), 'The gallery shortcode should be registered by default.' ); + + remove_all_shortcodes(); + + $this->assertFalse( shortcode_exists( 'gallery' ) ); + } + + /** + * Tests that the content is no longer processed after the shortcodes are removed. + * + * @ticket 66500 + */ + public function test_should_not_process_the_content_after_removing_the_shortcodes() { + add_shortcode( 'remove-all-hello', static fn() => 'Hello' ); + + $this->assertSame( 'Hello', do_shortcode( '[remove-all-hello]' ), 'The shortcode should be processed before it is removed.' ); + + remove_all_shortcodes(); + + $this->assertSame( '[remove-all-hello]', do_shortcode( '[remove-all-hello]' ), 'The shortcode should be left as it is after it is removed.' ); + } + + /** + * Tests that shortcodes can be registered again after removing all of them. + * + * @ticket 66500 + */ + public function test_should_allow_to_register_shortcodes_again() { + remove_all_shortcodes(); + + add_shortcode( 'remove-all-again', static fn() => 'Again' ); + + $this->assertTrue( shortcode_exists( 'remove-all-again' ) ); + $this->assertSame( 'Again', do_shortcode( '[remove-all-again]' ) ); + } + + /** + * Tests that removing the shortcodes does not fail when there are none. + * + * @ticket 66500 + */ + public function test_should_not_fail_when_there_are_no_shortcodes() { + global $shortcode_tags; + + remove_all_shortcodes(); + remove_all_shortcodes(); + + $this->assertSame( array(), $shortcode_tags ); + } +} diff --git a/tests/phpunit/tests/shortcodes/unescapeInvalidShortcodes.php b/tests/phpunit/tests/shortcodes/unescapeInvalidShortcodes.php new file mode 100644 index 0000000000000..4de8f69d092ef --- /dev/null +++ b/tests/phpunit/tests/shortcodes/unescapeInvalidShortcodes.php @@ -0,0 +1,100 @@ +assertSame( $expected, unescape_invalid_shortcodes( $content ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_content() { + return array( + 'opening bracket' => array( '[', '[' ), + 'closing bracket' => array( ']', ']' ), + 'both brackets' => array( '[shortcode]', '[shortcode]' ), + 'shortcode with attributes' => array( '[gallery ids="1,2"]', '[gallery ids="1,2"]' ), + 'repeated placeholders' => array( '[[a]] [b]', '[[a]] [b]' ), + 'inside html' => array( '[y]', '[y]' ), + 'placeholders of a conditional tag' => array( '"; + + $this->assertSame( "
\n[b]\n
\n", unescape_invalid_shortcodes( $content ) ); + } + + /** + * Tests that the function is the counterpart of the escaping that is done when shortcodes inside HTML are skipped. + * + * @ticket 66500 + */ + public function test_should_restore_the_brackets_that_do_shortcode_leaves_inside_html() { + add_shortcode( 'unescape-skip', static fn() => 'processed' ); + + $content = '[unescape-skip]'; + + try { + $this->assertSame( 'processed', do_shortcode( $content, true ) ); + } finally { + remove_shortcode( 'unescape-skip' ); + } + } +}