|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once dirname(__FILE__) . '/TinyTestCase.php'; |
| 4 | + |
| 5 | +class Tiny_Picture_Test extends Tiny_TestCase |
| 6 | +{ |
| 7 | + public function set_up() |
| 8 | + { |
| 9 | + parent::set_up(); |
| 10 | + } |
| 11 | + |
| 12 | + /** |
| 13 | + * img tags with a absolute url should be processed correctly |
| 14 | + */ |
| 15 | + public function test_replace_image_with_absolute_url_with_picture_tag() |
| 16 | + { |
| 17 | + $this->wp->createImage(37857, '2025/01', 'test.avif'); |
| 18 | + |
| 19 | + $tiny_picture = new Tiny_Picture(); |
| 20 | + $output = $tiny_picture->replace_img_with_picture_tag('<img src="https://www.tinifytest.com/wp-content/uploads/2025/01/test.png">'); |
| 21 | + $expected_output = '<picture><source type="image/avif" srcset="https://www.tinifytest.com/wp-content/uploads/2025/01/test.avif"><img src="https://www.tinifytest.com/wp-content/uploads/2025/01/test.png"></picture>'; |
| 22 | + $this->assertEquals($expected_output, $output); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * img tags with a relative url should be processed correctly |
| 27 | + */ |
| 28 | + public function test_replace_image_with_relative_url_with_picture_tag() |
| 29 | + { |
| 30 | + $this->wp->createImage(37857, '2025/01', 'test.webp'); |
| 31 | + |
| 32 | + $tiny_picture = new Tiny_Picture(); |
| 33 | + $output = $tiny_picture->replace_img_with_picture_tag('<img src="/wp-content/uploads/2025/01/test.png">'); |
| 34 | + $expected_output = '<picture><source type="image/webp" srcset="/wp-content/uploads/2025/01/test.webp"><img src="/wp-content/uploads/2025/01/test.png"></picture>'; |
| 35 | + $this->assertEquals($expected_output, $output); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * HTML containing a picture element with a img tag nested within, |
| 40 | + * should not be replaced |
| 41 | + */ |
| 42 | + public function test_will_not_replace_images_within_picture_tag() |
| 43 | + { |
| 44 | + $tiny_picture = new Tiny_Picture(); |
| 45 | + $current_picture = '<picture><source type="image/webp" srcset="/wp-content/uploads/2025/01/test.webp"><img src="/wp-content/uploads/2025/01/test.png"></picture>'; |
| 46 | + $output = $tiny_picture->replace_img_with_picture_tag($current_picture); |
| 47 | + $this->assertEquals($current_picture, $output); |
| 48 | + } |
| 49 | + |
| 50 | + public function test_img_with_no_alternate_format_should_not_change() |
| 51 | + { |
| 52 | + $tiny_picture = new Tiny_Picture(); |
| 53 | + $output = $tiny_picture->replace_img_with_picture_tag('<img src="/wp-content/uploads/2025/01/missing.png">'); |
| 54 | + $expected_output = '<img src="/wp-content/uploads/2025/01/missing.png">'; |
| 55 | + $this->assertEquals($expected_output, $output); |
| 56 | + } |
| 57 | + |
| 58 | + public function test_img_with_query_string_keeps_query() |
| 59 | + { |
| 60 | + $this->wp->createImage(37857, '2025/01', 'test.avif'); |
| 61 | + |
| 62 | + $tiny_picture = new Tiny_Picture(); |
| 63 | + $output = $tiny_picture->replace_img_with_picture_tag('<img src="/wp-content/uploads/2025/01/test.png?ver=123">'); |
| 64 | + $expected_output = '<picture><source type="image/avif" srcset="/wp-content/uploads/2025/01/test.avif"><img src="/wp-content/uploads/2025/01/test.png?ver=123"></picture>'; |
| 65 | + $this->assertEquals($expected_output, $output); |
| 66 | + } |
| 67 | + |
| 68 | + public function test_multiple_img_tags() |
| 69 | + { |
| 70 | + $this->wp->createImage(1000, '2025/01', 'first.webp'); |
| 71 | + $this->wp->createImage(1000, '2025/01', 'second.webp'); |
| 72 | + |
| 73 | + $tiny_picture = new Tiny_Picture(); |
| 74 | + $input = '<img src="/wp-content/uploads/2025/01/first.png"><p>Hello</p><img src="/wp-content/uploads/2025/01/second.png">'; |
| 75 | + $expected = '<picture><source type="image/webp" srcset="/wp-content/uploads/2025/01/first.webp"><img src="/wp-content/uploads/2025/01/first.png"></picture><p>Hello</p><picture><source type="image/webp" srcset="/wp-content/uploads/2025/01/second.webp"><img src="/wp-content/uploads/2025/01/second.png"></picture>'; |
| 76 | + |
| 77 | + $this->assertEquals($expected, $tiny_picture->replace_img_with_picture_tag($input)); |
| 78 | + } |
| 79 | + |
| 80 | + public function test_img_with_additional_attributes() |
| 81 | + { |
| 82 | + $this->wp->createImage(1000, '2025/01', 'test.webp'); |
| 83 | + |
| 84 | + $tiny_picture = new Tiny_Picture(); |
| 85 | + $input = '<img src="/wp-content/uploads/2025/01/test.png" class="lazy" alt="Test" loading="lazy">'; |
| 86 | + $expected = '<picture><source type="image/webp" srcset="/wp-content/uploads/2025/01/test.webp"><img src="/wp-content/uploads/2025/01/test.png" class="lazy" alt="Test" loading="lazy"></picture>'; |
| 87 | + $this->assertEquals($expected, $tiny_picture->replace_img_with_picture_tag($input)); |
| 88 | + } |
| 89 | + |
| 90 | + public function test_uppercase_img_tag_is_handled() |
| 91 | + { |
| 92 | + $this->wp->createImage(37857, '2025/01', 'test.webp'); |
| 93 | + |
| 94 | + $tiny_picture = new Tiny_Picture(); |
| 95 | + $input = '<IMG SRC="/wp-content/uploads/2025/01/test.png">'; |
| 96 | + $expected = '<picture><source type="image/webp" srcset="/wp-content/uploads/2025/01/test.webp"><IMG SRC="/wp-content/uploads/2025/01/test.png"></picture>'; |
| 97 | + $this->assertEquals($expected, $tiny_picture->replace_img_with_picture_tag($input)); |
| 98 | + } |
| 99 | + |
| 100 | + public function test_img_inside_anchor_is_wrapped() |
| 101 | + { |
| 102 | + $this->wp->createImage(1000, '2025/01', 'test.webp'); |
| 103 | + |
| 104 | + $tiny_picture = new Tiny_Picture(); |
| 105 | + $input = '<a href="/something"><img src="/wp-content/uploads/2025/01/test.png"></a>'; |
| 106 | + $expected = '<a href="/something"><picture><source type="image/webp" srcset="/wp-content/uploads/2025/01/test.webp"><img src="/wp-content/uploads/2025/01/test.png"></picture></a>'; |
| 107 | + |
| 108 | + $this->assertEquals($expected, $tiny_picture->replace_img_with_picture_tag($input)); |
| 109 | + } |
| 110 | +} |
0 commit comments