diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPIFunctions.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPIFunctions.php index f5089dd04b86e..7c933109154a4 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPIFunctions.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPIFunctions.php @@ -354,27 +354,43 @@ public function test_process_directives_when_block_is_filtered() { * * @ticket 60356 * - * @covers wp_interactivity_data_wp_context + * @covers wp_interactivity_data_wp_context + * @dataProvider data_wp_interactivity_data_wp_context_with_different_arrays + * + * @param array $context Context to encode. + * @param string $expected Expected function output. + */ + public function test_wp_interactivity_data_wp_context_with_different_arrays( $context, $expected ) { + $this->assertSame( $expected, wp_interactivity_data_wp_context( $context ) ); + } + + /** + * Data provider. + * + * @return array */ - public function test_wp_interactivity_data_wp_context_with_different_arrays() { - $this->assertSame( 'data-wp-context=\'{}\'', wp_interactivity_data_wp_context( array() ) ); - $this->assertSame( - 'data-wp-context=\'{"a":1,"b":"2","c":true}\'', - wp_interactivity_data_wp_context( - array( + public function data_wp_interactivity_data_wp_context_with_different_arrays() { + return array( + 'empty array' => array( + 'context' => array(), + 'expected' => 'data-wp-context=\'{}\'', + ), + 'associative array with mixed values' => array( + 'context' => array( 'a' => 1, 'b' => '2', 'c' => true, - ) - ) - ); - $this->assertSame( - 'data-wp-context=\'{"a":[1,2]}\'', - wp_interactivity_data_wp_context( array( 'a' => array( 1, 2 ) ) ) - ); - $this->assertSame( - 'data-wp-context=\'[1,2]\'', - wp_interactivity_data_wp_context( array( 1, 2 ) ) + ), + 'expected' => 'data-wp-context=\'{"a":1,"b":"2","c":true}\'', + ), + 'associative array with nested array as value' => array( + 'context' => array( 'a' => array( 1, 2 ) ), + 'expected' => 'data-wp-context=\'{"a":[1,2]}\'', + ), + 'array without keys, integer values' => array( + 'context' => array( 1, 2 ), + 'expected' => 'data-wp-context=\'[1,2]\'', + ), ); } @@ -384,28 +400,48 @@ public function test_wp_interactivity_data_wp_context_with_different_arrays() { * * @ticket 60356 * - * @covers wp_interactivity_data_wp_context + * @covers wp_interactivity_data_wp_context + * @dataProvider data_wp_interactivity_data_wp_context_with_different_arrays_and_a_namespace + * + * @param array $context Context to encode. + * @param string $store Store namespace. + * @param string $expected Expected function output. + */ + public function test_wp_interactivity_data_wp_context_with_different_arrays_and_a_namespace( $context, $store, $expected ) { + $this->assertSame( $expected, wp_interactivity_data_wp_context( $context, $store ) ); + } + + /** + * Data provider. + * + * @return array */ - public function test_wp_interactivity_data_wp_context_with_different_arrays_and_a_namespace() { - $this->assertSame( 'data-wp-context=\'myPlugin::{}\'', wp_interactivity_data_wp_context( array(), 'myPlugin' ) ); - $this->assertSame( - 'data-wp-context=\'myPlugin::{"a":1,"b":"2","c":true}\'', - wp_interactivity_data_wp_context( - array( + public function data_wp_interactivity_data_wp_context_with_different_arrays_and_a_namespace() { + return array( + 'empty array' => array( + 'context' => array(), + 'store' => 'myPlugin', + 'expected' => 'data-wp-context=\'myPlugin::{}\'', + ), + 'associative array with mixed values' => array( + 'context' => array( 'a' => 1, 'b' => '2', 'c' => true, ), - 'myPlugin' - ) - ); - $this->assertSame( - 'data-wp-context=\'myPlugin::{"a":[1,2]}\'', - wp_interactivity_data_wp_context( array( 'a' => array( 1, 2 ) ), 'myPlugin' ) - ); - $this->assertSame( - 'data-wp-context=\'myPlugin::[1,2]\'', - wp_interactivity_data_wp_context( array( 1, 2 ), 'myPlugin' ) + 'store' => 'myPlugin', + 'expected' => 'data-wp-context=\'myPlugin::{"a":1,"b":"2","c":true}\'', + ), + 'associative array with nested array as value' => array( + 'context' => array( 'a' => array( 1, 2 ) ), + 'store' => 'myPlugin', + 'expected' => 'data-wp-context=\'myPlugin::{"a":[1,2]}\'', + ), + 'array without keys, integer values' => array( + 'context' => array( 1, 2 ), + 'store' => 'myPlugin', + 'expected' => 'data-wp-context=\'myPlugin::[1,2]\'', + ), ); } @@ -417,13 +453,40 @@ public function test_wp_interactivity_data_wp_context_with_different_arrays_and_ * * @ticket 60356 * - * @covers wp_interactivity_data_wp_context + * @covers wp_interactivity_data_wp_context + * @dataProvider data_wp_interactivity_data_wp_context_with_json_flags + * + * @param array $context Context to encode. + * @param string $expected Expected function output. */ - public function test_wp_interactivity_data_wp_context_with_json_flags() { - $this->assertSame( 'data-wp-context=\'{"tag":"\u003Cfoo\u003E"}\'', wp_interactivity_data_wp_context( array( 'tag' => '' ) ) ); - $this->assertSame( 'data-wp-context=\'{"apos":"\u0027bar\u0027"}\'', wp_interactivity_data_wp_context( array( 'apos' => "'bar'" ) ) ); - $this->assertSame( 'data-wp-context=\'{"quot":"\u0022baz\u0022"}\'', wp_interactivity_data_wp_context( array( 'quot' => '"baz"' ) ) ); - $this->assertSame( 'data-wp-context=\'{"amp":"T\u0026T"}\'', wp_interactivity_data_wp_context( array( 'amp' => 'T&T' ) ) ); + public function test_wp_interactivity_data_wp_context_with_json_flags( $context, $expected ) { + $this->assertSame( $expected, wp_interactivity_data_wp_context( $context ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_wp_interactivity_data_wp_context_with_json_flags() { + return array( + 'value contains <> brackets' => array( + 'context' => array( 'tag' => '' ), + 'expected' => 'data-wp-context=\'{"tag":"\u003Cfoo\u003E"}\'', + ), + 'value contains single quote chars' => array( + 'context' => array( 'apos' => "'bar'" ), + 'expected' => 'data-wp-context=\'{"apos":"\u0027bar\u0027"}\'', + ), + 'value contains double quote chars' => array( + 'context' => array( 'quot' => '"baz"' ), + 'expected' => 'data-wp-context=\'{"quot":"\u0022baz\u0022"}\'', + ), + 'value contains & ampersand' => array( + 'context' => array( 'amp' => 'T&T' ), + 'expected' => 'data-wp-context=\'{"amp":"T\u0026T"}\'', + ), + ); } /**