-
Notifications
You must be signed in to change notification settings - Fork 158
Link to image file should point to original upload regardless of fallback setting #2570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
faisalahammad
wants to merge
2
commits into
WordPress:trunk
Choose a base branch
from
faisalahammad:fix/2470-gallery-link-original-image
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+248
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| <?php | ||
| /** | ||
| * Tests for webp-uploads plugin rest-api.php webp_uploads_update_rest_attachment_original_source_url(). | ||
| * | ||
| * @package webp-uploads | ||
| */ | ||
|
|
||
| use WebP_Uploads\Tests\TestCase; | ||
|
|
||
| class Test_WebP_Uploads_Attachment_Url extends TestCase { | ||
|
|
||
| public function set_up(): void { | ||
| parent::set_up(); | ||
|
|
||
| if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { | ||
| $this->markTestSkipped( 'Mime type image/webp is not supported.' ); | ||
| } | ||
|
|
||
| // Default to webp output for tests. | ||
| $this->set_image_output_type( 'webp' ); | ||
| } | ||
|
|
||
| /** | ||
| * Fetches the REST attachment response data for a given attachment. | ||
| * | ||
| * @param int $attachment_id The attachment ID. | ||
| * @return array<string, mixed> The REST response data. | ||
| */ | ||
| private function get_rest_attachment_data( int $attachment_id ): array { | ||
| $request = new WP_REST_Request(); | ||
| $request->set_param( 'id', $attachment_id ); | ||
|
|
||
| $controller = new WP_REST_Attachments_Controller( 'attachment' ); | ||
| $response = $controller->get_item( $request ); | ||
|
|
||
| $this->assertNotWPError( $response ); | ||
|
|
||
| return $response->get_data(); | ||
| } | ||
|
|
||
| /** | ||
| * When the fallback setting is disabled, the attachment's main file is swapped for the | ||
| * modern format and the original is backed up. The REST `source_url` (which the | ||
| * Image/Gallery blocks use for "Link to image file") should still resolve to the original. | ||
| * | ||
| * @covers ::webp_uploads_update_rest_attachment_original_source_url | ||
| */ | ||
| public function test_source_url_points_to_original_when_fallback_disabled(): void { | ||
| update_option( 'perflab_generate_webp_and_jpeg', false ); | ||
|
|
||
| $attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg' ); | ||
| $this->assertNotWPError( $attachment_id ); | ||
|
|
||
| // Sanity check: the attached file itself was swapped to WebP (the post's mime type | ||
| // stays JPEG for compatibility, which is why this can't be detected from that alone). | ||
| $this->assertStringEndsWith( '.webp', get_attached_file( $attachment_id ) ); | ||
|
|
||
| $original_path = wp_get_original_image_path( $attachment_id ); | ||
| $this->assertStringEndsWith( '.jpg', $original_path ); | ||
| $this->assertFileExists( $original_path ); | ||
|
|
||
| $data = $this->get_rest_attachment_data( $attachment_id ); | ||
|
|
||
| $this->assertStringEndsWith( '.jpg', $data['source_url'] ); | ||
| $this->assertSame( wp_basename( $original_path ), wp_basename( $data['source_url'] ) ); | ||
| $this->assertSame( wp_basename( $original_path ), wp_basename( $data['media_details']['sizes']['full']['source_url'] ) ); | ||
|
|
||
| // The <img> src pipeline (wp_get_attachment_url()) must be unaffected: it still needs | ||
| // to resolve to the current (modern-format) file, only the REST `source_url` changes. | ||
| $this->assertStringEndsWith( '.webp', wp_get_attachment_url( $attachment_id ) ); | ||
| } | ||
|
|
||
| /** | ||
| * When the fallback setting is enabled, the main file is not swapped, so `source_url` | ||
| * should be unaffected (still the original, as before this filter was added). | ||
| * | ||
| * @covers ::webp_uploads_update_rest_attachment_original_source_url | ||
| */ | ||
| public function test_source_url_unchanged_when_fallback_enabled(): void { | ||
| update_option( 'perflab_generate_webp_and_jpeg', true ); | ||
|
|
||
| $attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg' ); | ||
| $this->assertNotWPError( $attachment_id ); | ||
|
|
||
| $this->assertSame( 'image/jpeg', get_post_mime_type( $attachment_id ) ); | ||
|
|
||
| $data = $this->get_rest_attachment_data( $attachment_id ); | ||
|
|
||
| $this->assertStringEndsWith( '.jpg', $data['source_url'] ); | ||
| $this->assertSame( wp_basename( get_attached_file( $attachment_id ) ), wp_basename( $data['source_url'] ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Core's own "-scaled" resizing for oversized images keeps the same mime type and is | ||
| * unrelated to this plugin's format swap. It must not be affected by the filter. | ||
| * | ||
| * @covers ::webp_uploads_update_rest_attachment_original_source_url | ||
| */ | ||
| public function test_source_url_unaffected_by_core_scaled_images(): void { | ||
| update_option( 'perflab_generate_webp_and_jpeg', true ); | ||
|
|
||
| add_filter( | ||
| 'big_image_size_threshold', | ||
| static function () { | ||
| return 850; | ||
| } | ||
| ); | ||
|
|
||
| $attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg' ); | ||
| $this->assertNotWPError( $attachment_id ); | ||
|
|
||
| $metadata = wp_get_attachment_metadata( $attachment_id ); | ||
| $this->assertArrayHasKey( 'original_image', $metadata, 'Core should have backed up the pre-scaled original.' ); | ||
|
|
||
| $data = $this->get_rest_attachment_data( $attachment_id ); | ||
|
|
||
| // The URL should still be the "-scaled" file core itself produced, not the pre-scaled original. | ||
| $this->assertSame( wp_basename( get_attached_file( $attachment_id ) ), wp_basename( $data['source_url'] ) ); | ||
| $this->assertStringContainsString( '-scaled', $data['source_url'] ); | ||
| } | ||
|
|
||
| /** | ||
| * The <img> tag optimization must keep showing the modern format regardless of the | ||
| * REST `source_url` filter above. | ||
| * | ||
| * @covers ::webp_uploads_img_tag_update_mime_type | ||
| */ | ||
| public function test_img_tag_still_uses_modern_format_when_fallback_disabled(): void { | ||
| update_option( 'perflab_generate_webp_and_jpeg', false ); | ||
| $this->mock_frontend_body_hooks(); | ||
|
|
||
| $attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg' ); | ||
| $this->assertNotWPError( $attachment_id ); | ||
|
|
||
| $image = wp_get_attachment_image( $attachment_id, 'large', false, array( 'class' => 'wp-image-' . $attachment_id ) ); | ||
| $content = apply_filters( 'the_content', $image ); | ||
|
|
||
| $processor = new WP_HTML_Tag_Processor( $content ); | ||
| $this->assertTrue( $processor->next_tag( array( 'tag_name' => 'IMG' ) ) ); | ||
| $this->assertStringEndsWith( '.webp', $processor->get_attribute( 'src' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * When the fallback setting is disabled and the image is oversized, the plugin swaps | ||
| * the attached file to the scaled modern format (e.g. `leaves-scaled.webp`) and backs | ||
| * up the unscaled original in `original_image`. The REST `source_url` should still | ||
| * resolve to the scaled output-format file, not the unscaled original upload, so the | ||
| * "Link to image file" href stays scaled and in the modern format. | ||
| * | ||
| * @covers ::webp_uploads_update_rest_attachment_original_source_url | ||
| */ | ||
| public function test_source_url_points_to_scaled_output_format_when_fallback_disabled(): void { | ||
| update_option( 'perflab_generate_webp_and_jpeg', false ); | ||
|
|
||
| add_filter( | ||
| 'big_image_size_threshold', | ||
| static function () { | ||
| return 850; | ||
| } | ||
| ); | ||
|
|
||
| $attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg' ); | ||
| $this->assertNotWPError( $attachment_id ); | ||
|
|
||
| // The attached file itself was swapped to the scaled modern format. | ||
| $this->assertStringEndsWith( '-scaled.webp', get_attached_file( $attachment_id ) ); | ||
|
|
||
| $data = $this->get_rest_attachment_data( $attachment_id ); | ||
|
|
||
| // The link must stay scaled and in the output format, not the unscaled original upload. | ||
| $this->assertStringEndsWith( '-scaled.webp', $data['source_url'] ); | ||
| $this->assertSame( wp_basename( get_attached_file( $attachment_id ) ), wp_basename( $data['source_url'] ) ); | ||
| $this->assertSame( wp_basename( get_attached_file( $attachment_id ) ), wp_basename( $data['media_details']['sizes']['full']['source_url'] ) ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.