Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions tests/phpunit/tests/shortcodes/applyShortcodes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

/**
* Tests for the apply_shortcodes() function.
*
* @group shortcode
*
* @covers ::apply_shortcodes
*/
class Tests_Shortcodes_ApplyShortcodes extends WP_UnitTestCase {

public function set_up() {
parent::set_up();

add_shortcode( 'apply-greeting', array( $this, 'shortcode_greeting' ) );
}

public function tear_down() {
remove_shortcode( 'apply-greeting' );

parent::tear_down();
}

/**
* Shortcode callback.
*
* @param array|string $atts Attributes.
* @param string|null $content Content.
* @return string
*/
public function shortcode_greeting( $atts, $content = null ) {
$atts = shortcode_atts( array( 'name' => '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 = '<a title="[apply-greeting]">[apply-greeting]</a>';

$this->assertSame( '<a title="[apply-greeting]">Hello World</a>', 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 = '<a title="[apply-greeting]">[apply-greeting]</a>';

$this->assertSame( '<a title="Hello World">Hello World</a>', apply_shortcodes( $content ) );
$this->assertSame( '<a title="Hello World">Hello World</a>', apply_shortcodes( $content, false ) );
}
}
158 changes: 158 additions & 0 deletions tests/phpunit/tests/shortcodes/getShortcodeTagsInContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

/**
* Tests for the get_shortcode_tags_in_content() function.
*
* @group shortcode
*
* @covers ::get_shortcode_tags_in_content
*/
class Tests_Shortcodes_GetShortcodeTagsInContent extends WP_UnitTestCase {

/**
* Registered shortcodes, to restore after each test.
*
* @var array
*/
private $original_shortcode_tags;

public function set_up() {
parent::set_up();

global $shortcode_tags;
$this->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]' ) );
}
}
109 changes: 109 additions & 0 deletions tests/phpunit/tests/shortcodes/removeAllShortcodes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

/**
* Tests for the remove_all_shortcodes() function.
*
* @group shortcode
*
* @covers ::remove_all_shortcodes
*/
class Tests_Shortcodes_RemoveAllShortcodes extends WP_UnitTestCase {

/**
* Registered shortcodes, to restore after each test.
*
* @var array
*/
private $original_shortcode_tags;

public function set_up() {
parent::set_up();

global $shortcode_tags;
$this->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 );
}
}
Loading
Loading