From 32af72767c6a5a427bfb0708a6300d427c75e4dd Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Wed, 11 Sep 2024 16:41:28 +0300 Subject: [PATCH 1/8] Use resized image in schema when taken from the first content image and that's resized --- src/generators/schema/main-image.php | 2 +- src/helpers/schema/image-helper.php | 47 +++++++++++++++++++--------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/generators/schema/main-image.php b/src/generators/schema/main-image.php index 9a30975ab37..d097494d33e 100644 --- a/src/generators/schema/main-image.php +++ b/src/generators/schema/main-image.php @@ -38,7 +38,7 @@ public function generate() { // The first image in the content. if ( $this->context->main_image_url ) { - return $this->helpers->schema->image->generate_from_url( $image_id, $this->context->main_image_url ); + return $this->helpers->schema->image->generate_from_url( $image_id, $this->context->main_image_url, '', false, true, true ); } return false; diff --git a/src/helpers/schema/image-helper.php b/src/helpers/schema/image-helper.php index 0878b50b66d..095884631b8 100644 --- a/src/helpers/schema/image-helper.php +++ b/src/helpers/schema/image-helper.php @@ -48,18 +48,19 @@ public function __construct( HTML_Helper $html, Language_Helper $language, Main_ /** * Find an image based on its URL and generate a Schema object for it. * - * @param string $schema_id The `@id` to use for the returned image. - * @param string $url The image URL to base our object on. - * @param string $caption An optional caption. - * @param bool $add_hash Whether a hash will be added as a suffix in the @id. - * @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id. + * @param string $schema_id The `@id` to use for the returned image. + * @param string $url The image URL to base our object on. + * @param string $caption An optional caption. + * @param bool $add_hash Whether a hash will be added as a suffix in the @id. + * @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id. + * @param bool $check_for_resized Whether to account for resized images. * * @return array Schema ImageObject array. */ - public function generate_from_url( $schema_id, $url, $caption = '', $add_hash = false, $use_link_table = true ) { + public function generate_from_url( $schema_id, $url, $caption = '', $add_hash = false, $use_link_table = true, $check_for_resized = false ) { $attachment_id = $this->image->get_attachment_by_url( $url, $use_link_table ); if ( $attachment_id > 0 ) { - return $this->generate_from_attachment_id( $schema_id, $attachment_id, $caption, $add_hash ); + return $this->generate_from_attachment_id( $schema_id, $attachment_id, $caption, $add_hash, $check_for_resized, $url ); } return $this->simple_image_object( $schema_id, $url, $caption, $add_hash ); @@ -68,24 +69,40 @@ public function generate_from_url( $schema_id, $url, $caption = '', $add_hash = /** * Retrieve data about an image from the database and use it to generate a Schema object. * - * @param string $schema_id The `@id` to use for the returned image. - * @param int $attachment_id The attachment to retrieve data from. - * @param string $caption The caption string, if there is one. - * @param bool $add_hash Whether a hash will be added as a suffix in the @id. + * @param string $schema_id The `@id` to use for the returned image. + * @param int $attachment_id The attachment to retrieve data from. + * @param string $caption The caption string, if there is one. + * @param bool $add_hash Whether a hash will be added as a suffix in the @id. + * @param bool $check_for_resized Whether to account for resized images. * * @return array Schema ImageObject array. */ - public function generate_from_attachment_id( $schema_id, $attachment_id, $caption = '', $add_hash = false ) { + public function generate_from_attachment_id( $schema_id, $attachment_id, $caption = '', $add_hash = false, $check_for_resized = false, $original_url = '' ) { $data = $this->generate_object(); - $url = $this->image->get_attachment_image_url( $attachment_id, 'full' ); + $use_resized = $check_for_resized && preg_match( '/-\d+x\d+\.(jpg|jpeg|png|gif|webp)$/i', $original_url ); + if ( $use_resized ) { + $url = $original_url; + } else { + $url = $this->image->get_attachment_image_url( $attachment_id, 'full' ); + } + + // @TODO: re-check about hashes. $id_suffix = ( $add_hash ) ? \md5( $url ) : ''; $data['@id'] = $schema_id . $id_suffix; $data['url'] = $url; $data['contentUrl'] = $url; - $data = $this->add_image_size( $data, $attachment_id ); - $data = $this->add_caption( $data, $attachment_id, $caption ); + + if ( $use_resized ) { + preg_match( '/-(\d+)x(\d+)\.(jpg|jpeg|png|gif|webp)/', $original_url, $matches ); + $data['width'] = $matches[1]; + $data['height'] = $matches[2]; + } else { + $data = $this->add_image_size( $data, $attachment_id ); + } + + $data = $this->add_caption( $data, $attachment_id, $caption ); return $data; } From 7d4ca81996156e5cced048205d26b91820259ed3 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Thu, 12 Sep 2024 12:53:06 +0300 Subject: [PATCH 2/8] Improve code structure and CS --- src/helpers/schema/image-helper.php | 74 ++++++++++++++++++----------- 1 file changed, 46 insertions(+), 28 deletions(-) diff --git a/src/helpers/schema/image-helper.php b/src/helpers/schema/image-helper.php index 095884631b8..59065138766 100644 --- a/src/helpers/schema/image-helper.php +++ b/src/helpers/schema/image-helper.php @@ -55,12 +55,17 @@ public function __construct( HTML_Helper $html, Language_Helper $language, Main_ * @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id. * @param bool $check_for_resized Whether to account for resized images. * - * @return array Schema ImageObject array. + * @return string[] Schema ImageObject array. */ public function generate_from_url( $schema_id, $url, $caption = '', $add_hash = false, $use_link_table = true, $check_for_resized = false ) { $attachment_id = $this->image->get_attachment_by_url( $url, $use_link_table ); if ( $attachment_id > 0 ) { - return $this->generate_from_attachment_id( $schema_id, $attachment_id, $caption, $add_hash, $check_for_resized, $url ); + // First check if we should use the resized image and whether it is a resized image indeed. + if ( $check_for_resized && \preg_match( '/-\d+x\d+\.(jpg|jpeg|png|gif|webp)$/i', $url ) ) { + return $this->generate_from_resized_url( $schema_id, $attachment_id, $caption, $add_hash, $url ); + } + + return $this->generate_from_attachment_id( $schema_id, $attachment_id, $caption, $add_hash ); } return $this->simple_image_object( $schema_id, $url, $caption, $add_hash ); @@ -69,40 +74,53 @@ public function generate_from_url( $schema_id, $url, $caption = '', $add_hash = /** * Retrieve data about an image from the database and use it to generate a Schema object. * - * @param string $schema_id The `@id` to use for the returned image. - * @param int $attachment_id The attachment to retrieve data from. - * @param string $caption The caption string, if there is one. - * @param bool $add_hash Whether a hash will be added as a suffix in the @id. - * @param bool $check_for_resized Whether to account for resized images. + * @param string $schema_id The `@id` to use for the returned image. + * @param int $attachment_id The attachment to retrieve data from. + * @param string $caption The caption string, if there is one. + * @param bool $add_hash Whether a hash will be added as a suffix in the @id. + * @param string $resized_url The url of the resized image. * - * @return array Schema ImageObject array. + * @return string[] Schema ImageObject array. */ - public function generate_from_attachment_id( $schema_id, $attachment_id, $caption = '', $add_hash = false, $check_for_resized = false, $original_url = '' ) { + public function generate_from_resized_url( $schema_id, $attachment_id, $caption = '', $add_hash = false, $resized_url = '' ) { $data = $this->generate_object(); - $use_resized = $check_for_resized && preg_match( '/-\d+x\d+\.(jpg|jpeg|png|gif|webp)$/i', $original_url ); - if ( $use_resized ) { - $url = $original_url; - } else { - $url = $this->image->get_attachment_image_url( $attachment_id, 'full' ); - } + $id_suffix = ( $add_hash ) ? \md5( $resized_url ) : ''; + + $data['@id'] = $schema_id . $id_suffix; + $data['url'] = $resized_url; + $data['contentUrl'] = $resized_url; + + \preg_match( '/-(\d+)x(\d+)\.(jpg|jpeg|png|gif|webp)/', $resized_url, $matches ); + $data['width'] = $matches[1]; + $data['height'] = $matches[2]; + + $data = $this->add_caption( $data, $attachment_id, $caption ); + + return $data; + } + + /** + * Retrieve data about an image from the database and use it to generate a Schema object. + * + * @param string $schema_id The `@id` to use for the returned image. + * @param int $attachment_id The attachment to retrieve data from. + * @param string $caption The caption string, if there is one. + * @param bool $add_hash Whether a hash will be added as a suffix in the @id. + * + * @return string[] Schema ImageObject array. + */ + public function generate_from_attachment_id( $schema_id, $attachment_id, $caption = '', $add_hash = false ) { + $data = $this->generate_object(); + $url = $this->image->get_attachment_image_url( $attachment_id, 'full' ); - // @TODO: re-check about hashes. $id_suffix = ( $add_hash ) ? \md5( $url ) : ''; $data['@id'] = $schema_id . $id_suffix; $data['url'] = $url; $data['contentUrl'] = $url; - - if ( $use_resized ) { - preg_match( '/-(\d+)x(\d+)\.(jpg|jpeg|png|gif|webp)/', $original_url, $matches ); - $data['width'] = $matches[1]; - $data['height'] = $matches[2]; - } else { - $data = $this->add_image_size( $data, $attachment_id ); - } - - $data = $this->add_caption( $data, $attachment_id, $caption ); + $data = $this->add_image_size( $data, $attachment_id ); + $data = $this->add_caption( $data, $attachment_id, $caption ); return $data; } @@ -115,7 +133,7 @@ public function generate_from_attachment_id( $schema_id, $attachment_id, $captio * @param string $caption The caption string, if there is one. * @param bool $add_hash Whether a hash will be added as a suffix in the @id. * - * @return array Schema ImageObject array. + * @return string[] Schema ImageObject array. */ public function generate_from_attachment_meta( $schema_id, $attachment_meta, $caption = '', $add_hash = false ) { $data = $this->generate_object(); @@ -142,7 +160,7 @@ public function generate_from_attachment_meta( $schema_id, $attachment_meta, $ca * @param string $caption A caption, if set. * @param bool $add_hash Whether a hash will be added as a suffix in the @id. * - * @return array Schema ImageObject array. + * @return string[] Schema ImageObject array. */ public function simple_image_object( $schema_id, $url, $caption = '', $add_hash = false ) { $data = $this->generate_object(); From 94496bdfe9689f3ba13b97ddac0c8de8dcbb3e42 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Thu, 12 Sep 2024 13:04:08 +0300 Subject: [PATCH 3/8] Fix PHPCS thresholds and unit tests --- composer.json | 2 +- tests/Unit/Generators/Schema/Main_Image_Test.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 05ac91adee3..dcaade866c2 100644 --- a/composer.json +++ b/composer.json @@ -91,7 +91,7 @@ "Yoast\\WP\\SEO\\Composer\\Actions::check_coding_standards" ], "check-cs-thresholds": [ - "@putenv YOASTCS_THRESHOLD_ERRORS=2481", + "@putenv YOASTCS_THRESHOLD_ERRORS=2477", "@putenv YOASTCS_THRESHOLD_WARNINGS=252", "Yoast\\WP\\SEO\\Composer\\Actions::check_cs_thresholds" ], diff --git a/tests/Unit/Generators/Schema/Main_Image_Test.php b/tests/Unit/Generators/Schema/Main_Image_Test.php index 2b660071ac4..90e780404b8 100644 --- a/tests/Unit/Generators/Schema/Main_Image_Test.php +++ b/tests/Unit/Generators/Schema/Main_Image_Test.php @@ -137,7 +137,7 @@ public function test_generate_from_url() { $this->schema_image->expects( 'generate_from_url' ) ->once() - ->with( $image_id, $image_url ) + ->with( $image_id, $image_url, '', false, true, true ) ->andReturn( $image_schema ); self::assertEquals( $image_schema, $this->instance->generate() ); From 55aa767ff6c667551f3dcf7afa178e1ef9de927c Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Thu, 12 Sep 2024 16:11:16 +0300 Subject: [PATCH 4/8] Add unit tests --- .../Unit/Helpers/Schema/Image_Helper_Test.php | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/Unit/Helpers/Schema/Image_Helper_Test.php b/tests/Unit/Helpers/Schema/Image_Helper_Test.php index a7f060f4317..af95183f22f 100644 --- a/tests/Unit/Helpers/Schema/Image_Helper_Test.php +++ b/tests/Unit/Helpers/Schema/Image_Helper_Test.php @@ -88,6 +88,32 @@ public function test_generate_from_url_with_found_attachment_id() { ); } + /** + * Tests generating the schema from url with a found attachment id. + * + * @covers ::generate_from_url + * + * @return void + */ + public function test_generate_from_resized_url_with_found_attachment_id() { + $this->image + ->expects( 'get_attachment_by_url' ) + ->once() + ->with( 'https://example.org/image-300x300.jpg', true ) + ->andReturn( 1337 ); + + $this->instance + ->expects( 'generate_from_resized_url' ) + ->once() + ->with( '#schema-image-ABC', 1337, '', false, 'https://example.org/image-300x300.jpg' ) + ->andReturn( [] ); + + $this->assertEquals( + [], + $this->instance->generate_from_url( '#schema-image-ABC', 'https://example.org/image-300x300.jpg', '', false, true, true ) + ); + } + /** * Tests generating the schema from url no found attachment id. * @@ -168,6 +194,53 @@ public function test_generate_from_attachment_id_with_caption_and_image_dimensio ); } + /** + * Tests the generate_from_resized_url method. + * + * @covers ::generate_from_resized_url + * @covers ::generate_object + * @covers ::add_image_size + * @covers ::add_caption + * + * @return void + */ + public function test_generate_from_resized_url() { + $this->image + ->expects( 'get_attachment_image_url' ) + ->never(); + + $this->image + ->expects( 'get_metadata' ) + ->never(); + + $this->language + ->expects( 'add_piece_language' ) + ->once() + ->andReturnUsing( [ $this, 'set_language' ] ); + + $expected = [ + '@type' => 'ImageObject', + '@id' => 'https://example.com/#/schema/logo/image/', + 'url' => 'https://example.com/logo-300x300.jpg', + 'contentUrl' => 'https://example.com/logo-300x300.jpg', + 'width' => 300, + 'height' => 300, + 'caption' => 'Caption', + 'inLanguage' => 'language', + ]; + + $this->assertEquals( + $expected, + $this->instance->generate_from_resized_url( + 'https://example.com/#/schema/logo/image/', + 1337, + 'Caption', + false, + 'https://example.com/logo-300x300.jpg' + ) + ); + } + /** * Tests the generate_from_attachment_id method. * From 0763608eba68c85dd7a8573e6a93a75c3c841345 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Fri, 13 Sep 2024 10:31:02 +0300 Subject: [PATCH 5/8] Make regex case insensitive and not considering matches in the middle of filenames and unify them --- src/helpers/schema/image-helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers/schema/image-helper.php b/src/helpers/schema/image-helper.php index 59065138766..9ca5d8b56a2 100644 --- a/src/helpers/schema/image-helper.php +++ b/src/helpers/schema/image-helper.php @@ -61,7 +61,7 @@ public function generate_from_url( $schema_id, $url, $caption = '', $add_hash = $attachment_id = $this->image->get_attachment_by_url( $url, $use_link_table ); if ( $attachment_id > 0 ) { // First check if we should use the resized image and whether it is a resized image indeed. - if ( $check_for_resized && \preg_match( '/-\d+x\d+\.(jpg|jpeg|png|gif|webp)$/i', $url ) ) { + if ( $check_for_resized && \preg_match( '/-(\d+)x(\d+)\.(jpg|jpeg|png|gif|webp)$/', $url ) ) { return $this->generate_from_resized_url( $schema_id, $attachment_id, $caption, $add_hash, $url ); } @@ -91,7 +91,7 @@ public function generate_from_resized_url( $schema_id, $attachment_id, $caption $data['url'] = $resized_url; $data['contentUrl'] = $resized_url; - \preg_match( '/-(\d+)x(\d+)\.(jpg|jpeg|png|gif|webp)/', $resized_url, $matches ); + \preg_match( '/-(\d+)x(\d+)\.(jpg|jpeg|png|gif|webp)$/', $resized_url, $matches ); $data['width'] = $matches[1]; $data['height'] = $matches[2]; From 0913cefa807a1f585d4e61a1cf2d854f475f3630 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Fri, 13 Sep 2024 15:24:37 +0300 Subject: [PATCH 6/8] Fix annotation types --- src/helpers/schema/image-helper.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/helpers/schema/image-helper.php b/src/helpers/schema/image-helper.php index 9ca5d8b56a2..287dd0e68e5 100644 --- a/src/helpers/schema/image-helper.php +++ b/src/helpers/schema/image-helper.php @@ -55,7 +55,7 @@ public function __construct( HTML_Helper $html, Language_Helper $language, Main_ * @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id. * @param bool $check_for_resized Whether to account for resized images. * - * @return string[] Schema ImageObject array. + * @return array Schema ImageObject array. */ public function generate_from_url( $schema_id, $url, $caption = '', $add_hash = false, $use_link_table = true, $check_for_resized = false ) { $attachment_id = $this->image->get_attachment_by_url( $url, $use_link_table ); @@ -80,7 +80,7 @@ public function generate_from_url( $schema_id, $url, $caption = '', $add_hash = * @param bool $add_hash Whether a hash will be added as a suffix in the @id. * @param string $resized_url The url of the resized image. * - * @return string[] Schema ImageObject array. + * @return array Schema ImageObject array. */ public function generate_from_resized_url( $schema_id, $attachment_id, $caption = '', $add_hash = false, $resized_url = '' ) { $data = $this->generate_object(); @@ -108,7 +108,7 @@ public function generate_from_resized_url( $schema_id, $attachment_id, $caption * @param string $caption The caption string, if there is one. * @param bool $add_hash Whether a hash will be added as a suffix in the @id. * - * @return string[] Schema ImageObject array. + * @return array Schema ImageObject array. */ public function generate_from_attachment_id( $schema_id, $attachment_id, $caption = '', $add_hash = false ) { $data = $this->generate_object(); @@ -133,7 +133,7 @@ public function generate_from_attachment_id( $schema_id, $attachment_id, $captio * @param string $caption The caption string, if there is one. * @param bool $add_hash Whether a hash will be added as a suffix in the @id. * - * @return string[] Schema ImageObject array. + * @return array Schema ImageObject array. */ public function generate_from_attachment_meta( $schema_id, $attachment_meta, $caption = '', $add_hash = false ) { $data = $this->generate_object(); @@ -160,7 +160,7 @@ public function generate_from_attachment_meta( $schema_id, $attachment_meta, $ca * @param string $caption A caption, if set. * @param bool $add_hash Whether a hash will be added as a suffix in the @id. * - * @return string[] Schema ImageObject array. + * @return array Schema ImageObject array. */ public function simple_image_object( $schema_id, $url, $caption = '', $add_hash = false ) { $data = $this->generate_object(); From 4a034a085f7ff1145712f5bd75c03bed2a4d8cf9 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Fri, 13 Sep 2024 16:25:16 +0300 Subject: [PATCH 7/8] Add unit tests and prevent adding width|height when it's not a resized image --- src/helpers/schema/image-helper.php | 7 +- .../Unit/Helpers/Schema/Image_Helper_Test.php | 126 +++++++++++++++--- 2 files changed, 112 insertions(+), 21 deletions(-) diff --git a/src/helpers/schema/image-helper.php b/src/helpers/schema/image-helper.php index 287dd0e68e5..c990239d24f 100644 --- a/src/helpers/schema/image-helper.php +++ b/src/helpers/schema/image-helper.php @@ -91,9 +91,10 @@ public function generate_from_resized_url( $schema_id, $attachment_id, $caption $data['url'] = $resized_url; $data['contentUrl'] = $resized_url; - \preg_match( '/-(\d+)x(\d+)\.(jpg|jpeg|png|gif|webp)$/', $resized_url, $matches ); - $data['width'] = $matches[1]; - $data['height'] = $matches[2]; + if ( \preg_match( '/-(\d+)x(\d+)\.(jpg|jpeg|png|gif|webp)$/', $resized_url, $matches ) ) { + $data['width'] = $matches[1]; + $data['height'] = $matches[2]; + } $data = $this->add_caption( $data, $attachment_id, $caption ); diff --git a/tests/Unit/Helpers/Schema/Image_Helper_Test.php b/tests/Unit/Helpers/Schema/Image_Helper_Test.php index af95183f22f..da03350191c 100644 --- a/tests/Unit/Helpers/Schema/Image_Helper_Test.php +++ b/tests/Unit/Helpers/Schema/Image_Helper_Test.php @@ -92,28 +92,69 @@ public function test_generate_from_url_with_found_attachment_id() { * Tests generating the schema from url with a found attachment id. * * @covers ::generate_from_url + * @dataProvider provide_generate_from_resized_url_with_found_attachment_id + * + * @param string $url The url of the image to create schema for. + * @param int $generate_from_resized_times The times we generate from resized URL. + * @param int $generate_from_attachment_id_times The times we generate from attachment ID. * * @return void */ - public function test_generate_from_resized_url_with_found_attachment_id() { + public function test_generate_from_resized_url_with_found_attachment_id( $url, $generate_from_resized_times, $generate_from_attachment_id_times ) { $this->image ->expects( 'get_attachment_by_url' ) ->once() - ->with( 'https://example.org/image-300x300.jpg', true ) + ->with( $url, true ) ->andReturn( 1337 ); $this->instance ->expects( 'generate_from_resized_url' ) - ->once() - ->with( '#schema-image-ABC', 1337, '', false, 'https://example.org/image-300x300.jpg' ) + ->times( $generate_from_resized_times ) + ->with( '#schema-image-ABC', 1337, '', false, $url ) + ->andReturn( [] ); + + $this->instance + ->expects( 'generate_from_attachment_id' ) + ->times( $generate_from_attachment_id_times ) + ->with( '#schema-image-ABC', 1337, '', false ) ->andReturn( [] ); $this->assertEquals( [], - $this->instance->generate_from_url( '#schema-image-ABC', 'https://example.org/image-300x300.jpg', '', false, true, true ) + $this->instance->generate_from_url( '#schema-image-ABC', $url, '', false, true, true ) ); } + /** + * Data provider for test_generate_from_resized_url_with_found_attachment_id. + * + * @return array + */ + public static function provide_generate_from_resized_url_with_found_attachment_id() { + return [ + 'Generate for resized image' => [ + 'url' => 'https://example.org/image-300x300.jpg', + 'generate_from_resized_times' => 1, + 'generate_from_attachment_id_times' => 0, + ], + 'Generate for non-resized image' => [ + 'url' => 'https://example.org/image.jpg', + 'generate_from_resized_times' => 0, + 'generate_from_attachment_id_times' => 1, + ], + 'Generate for non-resized image that had the same format with resized in filename' => [ + 'url' => 'https://example.org/image-300x300-1.jpg', + 'generate_from_resized_times' => 0, + 'generate_from_attachment_id_times' => 1, + ], + 'Generate for non-resized image that has a similar format with resized but with capital X' => [ + 'url' => 'https://example.org/image-300X300.jpg', + 'generate_from_resized_times' => 0, + 'generate_from_attachment_id_times' => 1, + ], + ]; + } + /** * Tests generating the schema from url no found attachment id. * @@ -201,10 +242,14 @@ public function test_generate_from_attachment_id_with_caption_and_image_dimensio * @covers ::generate_object * @covers ::add_image_size * @covers ::add_caption + * @dataProvider provide_generate_from_resized_url + * + * @param string $url The url of the image to create schema for. + * @param string|array $expected The times we generate from resized URL. * * @return void */ - public function test_generate_from_resized_url() { + public function test_generate_from_resized_url( $url, $expected ) { $this->image ->expects( 'get_attachment_image_url' ) ->never(); @@ -218,17 +263,6 @@ public function test_generate_from_resized_url() { ->once() ->andReturnUsing( [ $this, 'set_language' ] ); - $expected = [ - '@type' => 'ImageObject', - '@id' => 'https://example.com/#/schema/logo/image/', - 'url' => 'https://example.com/logo-300x300.jpg', - 'contentUrl' => 'https://example.com/logo-300x300.jpg', - 'width' => 300, - 'height' => 300, - 'caption' => 'Caption', - 'inLanguage' => 'language', - ]; - $this->assertEquals( $expected, $this->instance->generate_from_resized_url( @@ -236,11 +270,67 @@ public function test_generate_from_resized_url() { 1337, 'Caption', false, - 'https://example.com/logo-300x300.jpg' + $url ) ); } + /** + * Data provider for test_generate_from_resized_url. + * + * @return array + */ + public static function provide_generate_from_resized_url() { + return [ + 'Generate for resized image' => [ + 'url' => 'https://example.org/image-400x300.jpg', + 'expected' => [ + '@type' => 'ImageObject', + '@id' => 'https://example.com/#/schema/logo/image/', + 'url' => 'https://example.org/image-400x300.jpg', + 'contentUrl' => 'https://example.org/image-400x300.jpg', + 'width' => '400', + 'height' => '300', + 'caption' => 'Caption', + 'inLanguage' => 'language', + ], + ], + 'Generate for non-resized image' => [ + 'url' => 'https://example.org/image.jpg', + 'expected' => [ + '@type' => 'ImageObject', + '@id' => 'https://example.com/#/schema/logo/image/', + 'url' => 'https://example.org/image.jpg', + 'contentUrl' => 'https://example.org/image.jpg', + 'caption' => 'Caption', + 'inLanguage' => 'language', + ], + ], + 'Generate for non-resized image that had the same format with resized in filename' => [ + 'url' => 'https://example.org/image-300x300-1.jpg', + 'expected' => [ + '@type' => 'ImageObject', + '@id' => 'https://example.com/#/schema/logo/image/', + 'url' => 'https://example.org/image-300x300-1.jpg', + 'contentUrl' => 'https://example.org/image-300x300-1.jpg', + 'caption' => 'Caption', + 'inLanguage' => 'language', + ], + ], + 'Generate for non-resized image that has a similar format with resized but with capital X' => [ + 'url' => 'https://example.org/image-300X300.jpg', + 'expected' => [ + '@type' => 'ImageObject', + '@id' => 'https://example.com/#/schema/logo/image/', + 'url' => 'https://example.org/image-300X300.jpg', + 'contentUrl' => 'https://example.org/image-300X300.jpg', + 'caption' => 'Caption', + 'inLanguage' => 'language', + ], + ], + ]; + } + /** * Tests the generate_from_attachment_id method. * From c0218f3b0925edf61047c88add6eac6441dc3eb6 Mon Sep 17 00:00:00 2001 From: Vraja Das Date: Mon, 16 Sep 2024 09:58:33 +0300 Subject: [PATCH 8/8] add test case for url for files that re not image format --- tests/Unit/Helpers/Schema/Image_Helper_Test.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/Unit/Helpers/Schema/Image_Helper_Test.php b/tests/Unit/Helpers/Schema/Image_Helper_Test.php index da03350191c..df7358c0cb4 100644 --- a/tests/Unit/Helpers/Schema/Image_Helper_Test.php +++ b/tests/Unit/Helpers/Schema/Image_Helper_Test.php @@ -152,6 +152,11 @@ public static function provide_generate_from_resized_url_with_found_attachment_i 'generate_from_resized_times' => 0, 'generate_from_attachment_id_times' => 1, ], + 'Generate for other file types that are not image' => [ + 'url' => 'https://example.org/image.pdf', + 'generate_from_resized_times' => 0, + 'generate_from_attachment_id_times' => 1, + ], ]; }