From 59a99c13ace7bf1698cd2a7922817f0b2b7089ec Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Tue, 5 Jul 2022 00:46:54 +0000 Subject: [PATCH] Media: Add tests for `wp_img_tag_add_decoding_attr()`. Add new tests skipped in original commit. Follow up to [53480]. Fixes #53232. git-svn-id: https://develop.svn.wordpress.org/trunk@53652 602fd350-edb4-49c9-b593-d223f7449a82 --- .../tests/media/wpImageTagAddDecodingAttr.php | 164 ++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 tests/phpunit/tests/media/wpImageTagAddDecodingAttr.php diff --git a/tests/phpunit/tests/media/wpImageTagAddDecodingAttr.php b/tests/phpunit/tests/media/wpImageTagAddDecodingAttr.php new file mode 100644 index 0000000000..76fa203b7e --- /dev/null +++ b/tests/phpunit/tests/media/wpImageTagAddDecodingAttr.php @@ -0,0 +1,164 @@ +assertSame( $expected, wp_img_tag_add_decoding_attr( $image, $context ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_should_add_decoding_attr() { + return array( + 'default' => array( + 'image' => '', + 'context' => '', + 'decoding' => 'no value', + 'expected' => '', + ), + 'async' => array( + 'image' => '', + 'context' => '', + 'decoding' => 'async', + 'expected' => '', + ), + 'sync' => array( + 'image' => '', + 'context' => '', + 'decoding' => 'sync', + 'expected' => '', + ), + 'auto' => array( + 'image' => '', + 'context' => '', + 'decoding' => 'auto', + 'expected' => '', + ), + ); + } + + /** + * Tests that the `wp_img_tag_add_decoding_attr()` function should not add + * the 'decoding' attribute. + * + * @ticket 53232 + * + * @dataProvider data_should_not_add_decoding_attr + * + * @param string $image The HTML `img` tag where the attribute should be added. + * @param string $context Additional context to pass to the filters. + * @param mixed $decoding The value for the 'decoding' attribute. 'no value' for default. + * @param string $expected The expected `img` tag. + */ + public function test_should_not_add_decoding_attr( $image, $context, $decoding, $expected ) { + // Falsey values are allowed in the filter, cannot use `null` or `false` here. + if ( 'no value' !== $decoding ) { + add_filter( + 'wp_img_tag_add_decoding_attr', + static function( $value ) use ( $decoding ) { + return $decoding; + } + ); + } + + $this->assertSame( $expected, wp_img_tag_add_decoding_attr( $image, $context, $expected ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_should_not_add_decoding_attr() { + return array( + // Unhappy paths. + 'lazy (unaccepted value)' => array( + 'image' => '', + 'context' => '', + 'decoding' => 'lazy', + 'expected' => '', + ), + 'a non-string value' => array( + 'image' => '', + 'context' => '', + 'decoding' => array( 'sync' ), + 'expected' => '', + ), + + // Falsey values. + 'false' => array( + 'image' => '', + 'context' => '', + 'decoding' => false, + 'expected' => '', + ), + 'null' => array( + 'image' => '', + 'context' => '', + 'decoding' => null, + 'expected' => '', + ), + 'empty string' => array( + 'image' => '', + 'context' => '', + 'decoding' => '', + 'expected' => '', + ), + 'empty array' => array( + 'image' => '', + 'context' => '', + 'decoding' => array(), + 'expected' => '', + ), + '0 int' => array( + 'image' => '', + 'context' => '', + 'decoding' => 0, + 'expected' => '', + ), + '0 string' => array( + 'image' => '', + 'context' => '', + 'decoding' => '0', + 'expected' => '', + ), + '0.0 float' => array( + 'image' => '', + 'context' => '', + 'decoding' => 0.0, + 'expected' => '', + ), + ); + } +}