From 008ac063854f9d8758aedd905765bec12867fa2e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 26 Jun 2024 22:18:43 +0200 Subject: [PATCH] Update or remove tests --- .../interactivity-api/wpInteractivityAPI.php | 260 ++++++------------ 1 file changed, 87 insertions(+), 173 deletions(-) diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php index 25c8c2dc3abce..032836fcf8499 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php @@ -184,273 +184,187 @@ public function test_state_and_config_existing_indexed_arrays_are_replaced() { } /** - * Invokes the private `print_client_interactivity` method of - * WP_Interactivity_API class. - * - * @return array|null The content of the JSON object printed on the client-side or null if nothings was printed. - */ - private function print_client_interactivity_data() { - $interactivity_data_markup = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) ); - preg_match( '/ - -SCRIPT_TAG; + $filter = $this->get_script_data_filter_result( + function () { + $this->interactivity->state( 'pluginWithEmptyState_prune', array() ); + $this->interactivity->state( 'pluginWithState_include', array( 'value' => 'excellent' ) ); + } + ); - $this->assertSame( $expected, $printed_script ); + $this->assertEquals( array( 'state' => array( 'pluginWithState_include' => array( 'value' => 'excellent' ) ) ), $filter->get_args()[0][0] ); } /** * Tests that data consisting of only empty state objects is not printed. * * @ticket 60761 + * @ticket 61512 * * @covers ::print_client_interactivity_data */ public function test_state_not_printed_when_only_empty_arrays() { - $this->interactivity->state( 'pluginWithEmptyState_prune', array() ); - $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) ); - $this->assertSame( '', $printed_script ); + $filter = $this->get_script_data_filter_result( + function () { + $this->interactivity->state( 'pluginWithEmptyState_prune', array() ); + } + ); + + $this->assertEquals( array(), $filter->get_args()[0][0] ); } /** * Tests that nested empty state objects are printed correctly. * * @ticket 60761 + * @ticket 61512 * * @covers ::print_client_interactivity_data */ public function test_state_printed_correctly_with_nested_empty_array() { - $this->interactivity->state( 'myPlugin', array( 'emptyArray' => array() ) ); - $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) ); - $expected = <<<'SCRIPT_TAG' - - -SCRIPT_TAG; + $filter = $this->get_script_data_filter_result( + function () { + $this->interactivity->state( 'myPlugin', array( 'emptyArray' => array() ) ); + } + ); - $this->assertSame( $expected, $printed_script ); + $this->assertEquals( array( 'state' => array( 'myPlugin' => array( 'emptyArray' => array() ) ) ), $filter->get_args()[0][0] ); } /** * Tests that empty config objects are pruned from printed data. * * @ticket 60761 + * @ticket 61512 * * @covers ::print_client_interactivity_data */ public function test_config_not_printed_when_empty_array() { - $this->interactivity->config( 'pluginWithEmptyConfig_prune', array() ); - $this->interactivity->config( 'pluginWithConfig_include', array( 'value' => 'excellent' ) ); - $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) ); - $expected = <<<'SCRIPT_TAG' - - -SCRIPT_TAG; + $filter = $this->get_script_data_filter_result( + function () { + $this->interactivity->config( 'pluginWithEmptyConfig_prune', array() ); + $this->interactivity->config( 'pluginWithConfig_include', array( 'value' => 'excellent' ) ); + } + ); - $this->assertSame( $expected, $printed_script ); + $this->assertEquals( array( 'config' => array( 'pluginWithConfig_include' => array( 'value' => 'excellent' ) ) ), $filter->get_args()[0][0] ); } /** * Tests that data consisting of only empty config objects is not printed. * * @ticket 60761 + * @ticket 61512 * * @covers ::print_client_interactivity_data */ public function test_config_not_printed_when_only_empty_arrays() { - $this->interactivity->config( 'pluginWithEmptyConfig_prune', array() ); - $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) ); - $this->assertSame( '', $printed_script ); + $filter = $this->get_script_data_filter_result( + function () { + $this->interactivity->config( 'pluginWithEmptyConfig_prune', array() ); + } + ); + + $this->assertEquals( array(), $filter->get_args()[0][0] ); } /** * Tests that nested empty config objects are printed correctly. * * @ticket 60761 + * @ticket 61512 * * @covers ::print_client_interactivity_data */ public function test_config_printed_correctly_with_nested_empty_array() { - $this->interactivity->config( 'myPlugin', array( 'emptyArray' => array() ) ); - $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) ); - $expected = <<<'SCRIPT_TAG' - - -SCRIPT_TAG; - - $this->assertSame( $expected, $printed_script ); - } - - /** - * Tests that special characters in the initial state and configuration are - * properly escaped. - * - * @ticket 60356 - * @ticket 61170 - * - * @covers ::state - * @covers ::config - * @covers ::print_client_interactivity_data - */ - public function test_state_and_config_escape_special_characters() { - $this->interactivity->state( - 'myPlugin', - array( - 'ampersand' => '&', - 'less-than sign' => '<', - 'greater-than sign' => '>', - 'solidus' => '/', - 'line separator' => "\u{2028}", - 'paragraph separator' => "\u{2029}", - 'flag of england' => "\u{1F3F4}\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}", - 'malicious script closer' => '', - 'entity-encoded malicious script closer' => '</script>', - ) + $filter = $this->get_script_data_filter_result( + function () { + $this->interactivity->config( 'myPlugin', array( 'emptyArray' => array() ) ); + } ); - $this->interactivity->config( 'myPlugin', array( 'chars' => '&<>/' ) ); - - $interactivity_data_markup = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) ); - preg_match( '~~s', $interactivity_data_markup, $interactivity_data_string ); - - $expected = <<<"JSON" -{"config":{"myPlugin":{"chars":"&\\u003C\\u003E/"}},"state":{"myPlugin":{"ampersand":"&","less-than sign":"\\u003C","greater-than sign":"\\u003E","solidus":"/","line separator":"\u{2028}","paragraph separator":"\u{2029}","flag of england":"\u{1F3F4}\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}","malicious script closer":"\\u003C/script\\u003E","entity-encoded malicious script closer":"</script>"}}} -JSON; - $this->assertEquals( $expected, $interactivity_data_string[1] ); - } - - /** - * Tests that special characters in the initial state and configuration are - * properly escaped when the blog_charset is not UTF-8 (unicode compatible). - * - * This this test, unicode and line terminators should be escaped to their - * JSON unicode sequences. - * - * @ticket 61170 - * - * @covers ::state - * @covers ::config - * @covers ::print_client_interactivity_data - */ - public function test_state_and_config_escape_special_characters_non_utf8() { - add_filter( 'pre_option_blog_charset', array( $this, 'charset_iso_8859_1' ) ); - $this->interactivity->state( - 'myPlugin', - array( - 'ampersand' => '&', - 'less-than sign' => '<', - 'greater-than sign' => '>', - 'solidus' => '/', - 'line separator' => "\u{2028}", - 'paragraph separator' => "\u{2029}", - 'flag of england' => "\u{1F3F4}\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}", - 'malicious script closer' => '', - 'entity-encoded malicious script closer' => '</script>', - ) - ); - $this->interactivity->config( 'myPlugin', array( 'chars' => '&<>/' ) ); - - $interactivity_data_markup = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) ); - preg_match( '~~s', $interactivity_data_markup, $interactivity_data_string ); - $expected = <<<"JSON" -{"config":{"myPlugin":{"chars":"&\\u003C\\u003E/"}},"state":{"myPlugin":{"ampersand":"&","less-than sign":"\\u003C","greater-than sign":"\\u003E","solidus":"/","line separator":"\\u2028","paragraph separator":"\\u2029","flag of england":"\\ud83c\\udff4\\udb40\\udc67\\udb40\\udc62\\udb40\\udc65\\udb40\\udc6e\\udb40\\udc67\\udb40\\udc7f","malicious script closer":"\\u003C/script\\u003E","entity-encoded malicious script closer":"</script>"}}} -JSON; - $this->assertEquals( $expected, $interactivity_data_string[1] ); + $this->assertEquals( array( 'config' => array( 'myPlugin' => array( 'emptyArray' => array() ) ) ), $filter->get_args()[0][0] ); } /**