|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests the adding of fetchpriority to img tags in the content. |
| 4 | + * |
| 5 | + * @covers ::fetchpriority_img_tag_add_attr |
| 6 | + */ |
| 7 | +class Fetchpriority_Test extends WP_UnitTestCase { |
| 8 | + protected static $post; |
| 9 | + protected static $attachment_id; |
| 10 | + protected static $attachment_id_2; |
| 11 | + |
| 12 | + protected $current_size_filter_data = null; |
| 13 | + protected $current_size_filter_result = null; |
| 14 | + |
| 15 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 16 | + self::$post = $factory->post->create_and_get(); |
| 17 | + $file = DIR_TESTDATA . '/images/canola.jpg'; |
| 18 | + self::$attachment_id = $factory->attachment->create_upload_object( |
| 19 | + $file, |
| 20 | + self::$post->ID, |
| 21 | + array( |
| 22 | + 'post_mime_type' => 'image/jpeg', |
| 23 | + ) |
| 24 | + ); |
| 25 | + self::$attachment_id_2 = $factory->attachment->create_upload_object( |
| 26 | + $file, |
| 27 | + self::$post->ID, |
| 28 | + array( |
| 29 | + 'post_mime_type' => 'image/jpeg', |
| 30 | + ) |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + public static function tear_down_after_class() { |
| 35 | + wp_delete_attachment( self::$attachment_id, true ); |
| 36 | + wp_delete_attachment( self::$attachment_id_2, true ); |
| 37 | + parent::tear_down_after_class(); |
| 38 | + } |
| 39 | + |
| 40 | + public function test_fetchpriority_img_tag_add_attr_based_on_context_and_loading_lazy() { |
| 41 | + $img = get_image_tag( self::$attachment_id, '', '', '', 'large' ); |
| 42 | + |
| 43 | + $this->assertStringContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'the_content' ) ); |
| 44 | + $this->assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'not_content' ) ); |
| 45 | + |
| 46 | + $img = str_replace( '<img ', '<img loading="lazy" ', $img ); |
| 47 | + $this->assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'the_content' ) ); |
| 48 | + } |
| 49 | + |
| 50 | + public function test_fetchpriority_img_tag_add_in_wp_filter_content_tags() { |
| 51 | + global $wp_query; |
| 52 | + global $wp_the_query; |
| 53 | + $img = get_image_tag( self::$attachment_id, '', '', '', 'large' ); |
| 54 | + $img_2 = get_image_tag( self::$attachment_id_2, '', '', '', 'large' ); |
| 55 | + |
| 56 | + $img = '<!-- wp:image {"id":' . self::$attachment_id . ',"sizeSlug":"large","linkDestination":"none"} --> |
| 57 | +<figure class="wp-block-image size-large">' . $img . '</figure> |
| 58 | +<!-- /wp:image --> |
| 59 | +<!-- wp:paragraph --> |
| 60 | +<p>This is an example page.</p> |
| 61 | +<!-- /wp:paragraph --> |
| 62 | +<!-- wp:image {"id":' . self::$attachment_id_2 . ',"sizeSlug":"large","linkDestination":"none"} --> |
| 63 | +<figure class="wp-block-image size-large">' . $img_2 . '</figure> |
| 64 | +<!-- /wp:image --> |
| 65 | +'; |
| 66 | + // Ensure image filtering occurs 'in_the_loop', is_main_query. |
| 67 | + $wp_the_query = $wp_query; |
| 68 | + $wp_query->in_the_loop = true; |
| 69 | + $content = wp_filter_content_tags( $img, 'the_content' ); |
| 70 | + $this->assertStringContainsString( 'fetchpriority="high"', $content ); |
| 71 | + $this->assertStringContainsString( 'loading="lazy"', $content ); |
| 72 | + $this->assertTrue( strpos( $content, 'fetchpriority="high"' ) < strpos( $content, 'loading="lazy"' ) ); |
| 73 | + |
| 74 | + $this->assertEquals( 1, substr_count( $content, 'fetchpriority' ) ); |
| 75 | + |
| 76 | + // Disable lazy loading and verify fetchpriority isn't added. |
| 77 | + add_filter( 'wp_lazy_loading_enabled', '__return_false' ); |
| 78 | + $content = wp_filter_content_tags( $img, 'the_content' ); |
| 79 | + $this->assertStringNotContainsString( 'fetchpriority="high"', $content ); |
| 80 | + |
| 81 | + } |
| 82 | +} |
0 commit comments