|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * LazyLoad tests |
| 4 | + */ |
| 5 | + |
| 6 | +namespace PoweredCache; |
| 7 | + |
| 8 | +class LazyLoad_Tests extends TestCase { |
| 9 | + |
| 10 | + protected $testFiles = [ |
| 11 | + 'utils.php', |
| 12 | + 'constants.php', |
| 13 | + ]; |
| 14 | + |
| 15 | + public function setUp(): void { |
| 16 | + parent::setUp(); |
| 17 | + |
| 18 | + global $is_apache; |
| 19 | + |
| 20 | + $is_apache = false; |
| 21 | + |
| 22 | + \WP_Mock::passthruFunction( 'esc_attr' ); |
| 23 | + \WP_Mock::userFunction( |
| 24 | + 'get_option', |
| 25 | + [ |
| 26 | + 'return' => [ |
| 27 | + 'lazy_load_exclusions' => 'skip-lazy', |
| 28 | + ], |
| 29 | + ] |
| 30 | + ); |
| 31 | + \WP_Mock::userFunction( |
| 32 | + 'wp_parse_args', |
| 33 | + [ |
| 34 | + 'return' => function ( $args, $defaults ) { |
| 35 | + return array_merge( $defaults, $args ); |
| 36 | + }, |
| 37 | + ] |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + public function test_filter_images_rewrites_supported_images(): void { |
| 42 | + $html = '<p><img src="https://example.test/image.jpg" class="alignwide" srcset="https://example.test/image-2x.jpg 2x"></p>'; |
| 43 | + |
| 44 | + $filtered = LazyLoad::filter_images( $html ); |
| 45 | + |
| 46 | + $this->assertStringContainsString( 'data-lazy-type="image"', $filtered ); |
| 47 | + $this->assertStringContainsString( 'data-lazy-src="https://example.test/image.jpg"', $filtered ); |
| 48 | + $this->assertStringContainsString( 'data-lazy-srcset="https://example.test/image-2x.jpg 2x"', $filtered ); |
| 49 | + $this->assertStringContainsString( 'class="lazy lazy-hidden alignwide"', $filtered ); |
| 50 | + $this->assertStringContainsString( '<noscript><img src="https://example.test/image.jpg"', $filtered ); |
| 51 | + } |
| 52 | + |
| 53 | + public function test_filter_images_skips_data_uri_and_configured_exclusions(): void { |
| 54 | + $html = '<img src="data:image/png;base64,abc"><img src="https://example.test/skip.jpg" class="skip-lazy">'; |
| 55 | + |
| 56 | + $filtered = LazyLoad::filter_images( $html ); |
| 57 | + |
| 58 | + $this->assertSame( $html, $filtered ); |
| 59 | + } |
| 60 | + |
| 61 | + public function test_filter_iframes_rewrites_regular_iframes_and_skips_gravity_forms(): void { |
| 62 | + $html = '<iframe src="https://player.example.test/embed"></iframe><iframe id="gform_ajax_frame_1" src="about:blank"></iframe>'; |
| 63 | + |
| 64 | + $filtered = LazyLoad::filter_iframes( $html ); |
| 65 | + |
| 66 | + $this->assertStringContainsString( 'data-lazy-type="iframe"', $filtered ); |
| 67 | + $this->assertStringContainsString( 'data-lazy-src="<iframe src="https://player.example.test/embed"></iframe>"', $filtered ); |
| 68 | + $this->assertStringContainsString( 'id="gform_ajax_frame_1"', $filtered ); |
| 69 | + } |
| 70 | + |
| 71 | + public function test_replace_youtube_iframe_with_thumbnail(): void { |
| 72 | + $html = '<iframe src="https://www.youtube.com/embed/abc123_XY?start=30"></iframe>'; |
| 73 | + |
| 74 | + $filtered = LazyLoad::replace_youtube_iframe_with_thumbnail( $html ); |
| 75 | + |
| 76 | + $this->assertStringContainsString( 'class="pcll-youtube-player"', $filtered ); |
| 77 | + $this->assertStringContainsString( 'data-src="https://www.youtube.com/embed/abc123_XY?start=30"', $filtered ); |
| 78 | + $this->assertStringContainsString( 'https://img.youtube.com/vi/abc123_XY/0.jpg', $filtered ); |
| 79 | + $this->assertStringNotContainsString( '<iframe', $filtered ); |
| 80 | + } |
| 81 | +} |
0 commit comments