|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for webp_uploads_filter_block_background_images(). |
| 4 | + * |
| 5 | + * @group background-image |
| 6 | + * |
| 7 | + * @package webp-uploads |
| 8 | + */ |
| 9 | + |
| 10 | +use WebP_Uploads\Tests\TestCase; |
| 11 | + |
| 12 | +class Test_WebP_Uploads_Block_Background_Images extends TestCase { |
| 13 | + |
| 14 | + /** |
| 15 | + * Temporary attachment IDs created during tests. |
| 16 | + * |
| 17 | + * @var array<int> |
| 18 | + */ |
| 19 | + private $temp_attachment_ids = array(); |
| 20 | + |
| 21 | + /** |
| 22 | + * Set up test environment. |
| 23 | + */ |
| 24 | + public function set_up(): void { |
| 25 | + parent::set_up(); |
| 26 | + |
| 27 | + // Ensure we can generate both JPEG and WebP variants. |
| 28 | + $this->opt_in_to_jpeg_and_webp(); |
| 29 | + update_option( 'perflab_modern_image_format', 'webp' ); |
| 30 | + update_option( 'perflab_generate_webp_and_jpeg', '1' ); |
| 31 | + |
| 32 | + // Satisfy webp_uploads_in_frontend_body() conditions. |
| 33 | + $this->mock_frontend_body_hooks(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Tear down and clean created artifacts. |
| 38 | + */ |
| 39 | + public function tear_down(): void { |
| 40 | + foreach ( $this->temp_attachment_ids as $id ) { |
| 41 | + wp_delete_attachment( $id, true ); |
| 42 | + } |
| 43 | + $this->temp_attachment_ids = array(); |
| 44 | + |
| 45 | + parent::tear_down(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Helper: Create a JPEG attachment and return its ID. |
| 50 | + */ |
| 51 | + private function create_jpeg_attachment(): int { |
| 52 | + $attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg' ); |
| 53 | + $this->assertNotWPError( $attachment_id ); |
| 54 | + $this->temp_attachment_ids[] = $attachment_id; |
| 55 | + return $attachment_id; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Generates block content markup and block array for a given block type using an attachment background image. |
| 60 | + * |
| 61 | + * @param string $block_name Block name (e.g. 'core/cover' or 'core/group'). |
| 62 | + * @param int $attachment_id Attachment ID whose URL will be used as original background image. |
| 63 | + * @param string $original_url The original full size image URL. |
| 64 | + * @return array{block_content:string,block:array<string,mixed>} Array containing 'block_content' HTML and 'block' parsed block structure. |
| 65 | + */ |
| 66 | + private function generate_block_with_background( string $block_name, int $attachment_id, string $original_url ): array { |
| 67 | + if ( 'core/cover' === $block_name ) { |
| 68 | + $block_content = '<div class="wp-block-cover"><div style="background-image:url(' . esc_url( $original_url ) . ')"><span class="wp-block-cover__inner-container">Content</span></div></div>'; |
| 69 | + $block = array( |
| 70 | + 'blockName' => 'core/cover', |
| 71 | + 'attrs' => array( |
| 72 | + 'id' => $attachment_id, |
| 73 | + 'url' => $original_url, |
| 74 | + ), |
| 75 | + ); |
| 76 | + } elseif ( 'core/group' === $block_name ) { |
| 77 | + $block_content = '<div class="wp-block-group" style="background-image:url(' . esc_url( $original_url ) . ')"><div class="wp-block-group__inner-container">Content</div></div>'; |
| 78 | + $block = array( |
| 79 | + 'blockName' => 'core/group', |
| 80 | + 'attrs' => array( |
| 81 | + 'style' => array( |
| 82 | + 'background' => array( |
| 83 | + 'backgroundImage' => array( |
| 84 | + 'id' => $attachment_id, |
| 85 | + 'url' => $original_url, |
| 86 | + ), |
| 87 | + ), |
| 88 | + ), |
| 89 | + ), |
| 90 | + ); |
| 91 | + } else { |
| 92 | + $block_content = ''; |
| 93 | + $block = array( |
| 94 | + 'blockName' => $block_name, |
| 95 | + 'attrs' => array(), |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + return array( |
| 100 | + 'block_content' => $block_content, |
| 101 | + 'block' => $block, |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * It should replace a Cover block background image URL with a WebP variant when available. |
| 107 | + * |
| 108 | + * @covers ::webp_uploads_filter_block_background_images |
| 109 | + */ |
| 110 | + public function test_cover_block_background_image_replaced_with_webp(): void { |
| 111 | + if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { |
| 112 | + $this->markTestSkipped( 'Mime type image/webp is not supported.' ); |
| 113 | + } |
| 114 | + |
| 115 | + $attachment_id = $this->create_jpeg_attachment(); |
| 116 | + $original_url = wp_get_attachment_image_url( $attachment_id, 'full' ); |
| 117 | + $this->assertIsString( $original_url ); |
| 118 | + |
| 119 | + $generated = $this->generate_block_with_background( 'core/cover', $attachment_id, $original_url ); |
| 120 | + $filtered = webp_uploads_filter_block_background_images( $generated['block_content'], $generated['block'] ); |
| 121 | + |
| 122 | + $webp_url = webp_uploads_get_mime_type_image( $attachment_id, $original_url, 'image/webp' ); |
| 123 | + $this->assertNotNull( $webp_url, 'Expected a generated WebP URL.' ); |
| 124 | + |
| 125 | + if ( $webp_url !== $original_url ) { |
| 126 | + $this->assertStringContainsString( $webp_url, $filtered, 'Filtered markup should reference WebP background image.' ); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * It should not change background when no WebP source exists. |
| 132 | + * |
| 133 | + * @covers ::webp_uploads_filter_block_background_images |
| 134 | + */ |
| 135 | + public function test_cover_block_background_image_not_changed_without_webp_source(): void { |
| 136 | + // Remove any transform filters added during set_up so we start clean. |
| 137 | + remove_all_filters( 'webp_uploads_upload_image_mime_transforms' ); |
| 138 | + |
| 139 | + // Restrict transforms to only JPEG so no WebP is produced for this test. |
| 140 | + $filter = static function ( $transforms ) { |
| 141 | + $transforms['image/jpeg'] = array( 'image/jpeg' ); |
| 142 | + return $transforms; |
| 143 | + }; |
| 144 | + add_filter( 'webp_uploads_upload_image_mime_transforms', $filter ); |
| 145 | + |
| 146 | + $attachment_id = $this->create_jpeg_attachment(); |
| 147 | + $original_url = wp_get_attachment_image_url( $attachment_id, 'full' ); |
| 148 | + $this->assertIsString( $original_url ); |
| 149 | + |
| 150 | + $generated = $this->generate_block_with_background( 'core/cover', $attachment_id, $original_url ); |
| 151 | + $filtered = webp_uploads_filter_block_background_images( $generated['block_content'], $generated['block'] ); |
| 152 | + |
| 153 | + remove_filter( 'webp_uploads_upload_image_mime_transforms', $filter ); |
| 154 | + |
| 155 | + $this->assertSame( $generated['block_content'], $filtered, 'Background image should remain unchanged when no WebP source exists.' ); |
| 156 | + $this->assertStringNotContainsString( '-jpg.webp', $filtered ); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * It should replace a Group block background image URL with a WebP variant when available. |
| 161 | + * |
| 162 | + * @covers ::webp_uploads_filter_block_background_images |
| 163 | + */ |
| 164 | + public function test_group_block_background_image_replaced_with_webp(): void { |
| 165 | + if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { |
| 166 | + $this->markTestSkipped( 'Mime type image/webp is not supported.' ); |
| 167 | + } |
| 168 | + |
| 169 | + $attachment_id = $this->create_jpeg_attachment(); |
| 170 | + $original_url = wp_get_attachment_image_url( $attachment_id, 'full' ); |
| 171 | + $this->assertIsString( $original_url ); |
| 172 | + |
| 173 | + $generated = $this->generate_block_with_background( 'core/group', $attachment_id, $original_url ); |
| 174 | + $filtered = webp_uploads_filter_block_background_images( $generated['block_content'], $generated['block'] ); |
| 175 | + |
| 176 | + $webp_url = webp_uploads_get_mime_type_image( $attachment_id, $original_url, 'image/webp' ); |
| 177 | + $this->assertNotNull( $webp_url, 'Expected a generated WebP URL.' ); |
| 178 | + |
| 179 | + if ( $webp_url !== $original_url ) { |
| 180 | + $this->assertStringContainsString( $webp_url, $filtered, 'Filtered group block should reference WebP background image.' ); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments