Skip to content

Commit

Permalink
Add two test cases for ilo_construct_preload_links and remove integri…
Browse files Browse the repository at this point in the history
…ty attr lifting
  • Loading branch information
westonruter committed Dec 12, 2023
1 parent 3e77988 commit 46080ef
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
4 changes: 2 additions & 2 deletions modules/images/image-loading-optimization/optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function ilo_construct_preload_links( array $lcp_elements_by_minimum_viewport_wi
$minimum_viewport_widths = array_keys( $lcp_elements_by_minimum_viewport_widths );
for ( $i = 0, $len = count( $minimum_viewport_widths ); $i < $len; $i++ ) {
$lcp_element = $lcp_elements_by_minimum_viewport_widths[ $minimum_viewport_widths[ $i ] ];
if ( false === $lcp_element || empty( $lcp_element['attributes'] ) ) {
if ( false === $lcp_element ) {
// No supported LCP element at this breakpoint, so nothing to preload.
continue;
}
Expand Down Expand Up @@ -173,7 +173,7 @@ function ilo_optimize_template_output_buffer( string $buffer ): string {
// Capture the attributes from the LCP elements to use in preload links.
if ( isset( $lcp_element_minimum_viewport_width_by_xpath[ $xpath ] ) ) {
$attributes = array();
foreach ( array( 'src', 'srcset', 'sizes', 'crossorigin', 'integrity' ) as $attr_name ) {
foreach ( array( 'src', 'srcset', 'sizes', 'crossorigin' ) as $attr_name ) {
$attributes[ $attr_name ] = $processor->get_attribute( $attr_name );
}
foreach ( $lcp_element_minimum_viewport_width_by_xpath[ $xpath ] as $minimum_viewport_width ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,58 @@ public function test_ilo_maybe_add_template_output_buffer_filter() {
$this->assertSame( 10, has_filter( 'ilo_template_output_buffer', 'ilo_optimize_template_output_buffer' ) );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_provider_test_ilo_construct_preload_links(): array {
return array(
'no-lcp-image' => array(
'lcp_elements_by_minimum_viewport_widths' => array(
0 => false,
),
'expected' => '',
),
'one-non-responsive-lcp-image' => array(
'lcp_elements_by_minimum_viewport_widths' => array(
0 => array(
'attributes' => array(
'src' => 'https://example.com/image.jpg',
),
),
),
'expected' => '
<link data-ilo-added-tag rel="preload" fetchpriority="high" as="image" href="https://example.com/image.jpg">
',
),
'one-responsive-lcp-image' => array(
'lcp_elements_by_minimum_viewport_widths' => array(
0 => array(
'attributes' => array(
'src' => 'elva-fairy-800w.jpg',
'srcset' => 'elva-fairy-480w.jpg 480w, elva-fairy-800w.jpg 800w',
'sizes' => '(max-width: 600px) 480px, 800px',
'crossorigin' => 'anonymous',
),
),
),
'expected' => '
<link data-ilo-added-tag rel="preload" fetchpriority="high" as="image" imagesrcset="elva-fairy-480w.jpg 480w, elva-fairy-800w.jpg 800w" imagesizes="(max-width: 600px) 480px, 800px" crossorigin="anonymous">
',
),
);
}

/**
* Test ilo_construct_preload_links().
*
* @test
* @covers ::ilo_construct_preload_links
* @dataProvider data_provider_test_ilo_construct_preload_links
*/
public function test_ilo_construct_preload_links() {
$this->markTestIncomplete();
public function test_ilo_construct_preload_links( array $lcp_elements_by_minimum_viewport_widths, string $expected ) {
$this->assertSame( trim( $expected ), trim( ilo_construct_preload_links( $lcp_elements_by_minimum_viewport_widths ) ) );
}

/**
Expand Down

0 comments on commit 46080ef

Please sign in to comment.