Skip to content

Commit 95c7da2

Browse files
committed
Add another failing test to ensure preload links are added when parsed stylesheet is cached
1 parent e02b96a commit 95c7da2

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

tests/php/test-amp-style-sanitizer.php

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,37 +1975,40 @@ public function test_font_files_preloading( $theme_slug, $expected_urls, $html =
19751975
$html .= '</head><body></body></html>';
19761976
}
19771977

1978-
$dom = Document::fromHtml( $html, Options::DEFAULTS );
1979-
$error_codes = [];
1980-
$sanitizer = new AMP_Style_Sanitizer(
1981-
$dom,
1982-
[
1983-
'use_document_element' => true,
1984-
]
1985-
);
1986-
$sanitizer->sanitize();
1987-
$this->assertEquals( [], $error_codes );
1978+
// Do the assertions twice in order to ensure that preloads are still added when the CSS is cached.
1979+
foreach ( [ 'false', 'true' ] as $is_cached ) {
1980+
$dom = Document::fromHtml( $html, Options::DEFAULTS );
1981+
$error_codes = [];
1982+
$sanitizer = new AMP_Style_Sanitizer(
1983+
$dom,
1984+
[
1985+
'use_document_element' => true,
1986+
]
1987+
);
1988+
$sanitizer->sanitize();
1989+
$this->assertEquals( [], $error_codes, "Is cached: $is_cached" );
19881990

1989-
$link_elements = $dom->getElementsByTagName( 'link' );
1990-
$link_elements_count = $link_elements->length;
1991+
$link_elements = $dom->getElementsByTagName( 'link' );
1992+
$link_elements_count = $link_elements->length;
19911993

1992-
$actual_urls = array_map(
1993-
static function ( Element $link_element ) {
1994-
return $link_element->getAttribute( 'href' );
1995-
},
1996-
iterator_to_array( $link_elements )
1997-
);
1994+
$actual_urls = array_map(
1995+
static function ( Element $link_element ) {
1996+
return $link_element->getAttribute( 'href' );
1997+
},
1998+
iterator_to_array( $link_elements )
1999+
);
19982000

1999-
$this->assertEqualSets( $expected_urls, $actual_urls );
2001+
$this->assertEqualSets( $expected_urls, $actual_urls, "Is cached: $is_cached" );
20002002

2001-
for ( $i = 0; $i < $link_elements_count; $i++ ) {
2002-
$this->assertStringEndsWith(
2003-
$expected_urls[ $i ],
2004-
$link_elements->item( $i )->getAttribute( 'href' )
2005-
);
2006-
$this->assertEquals( $link_elements->item( $i )->getAttribute( 'rel' ), 'preload' );
2007-
$this->assertEquals( $link_elements->item( $i )->getAttribute( 'as' ), 'font' );
2008-
$this->assertEquals( $link_elements->item( $i )->getAttribute( 'crossorigin' ), '' );
2003+
for ( $i = 0; $i < $link_elements_count; $i ++ ) {
2004+
$this->assertStringEndsWith(
2005+
$expected_urls[ $i ],
2006+
$link_elements->item( $i )->getAttribute( 'href' )
2007+
);
2008+
$this->assertEquals( $link_elements->item( $i )->getAttribute( 'rel' ), 'preload' );
2009+
$this->assertEquals( $link_elements->item( $i )->getAttribute( 'as' ), 'font' );
2010+
$this->assertEquals( $link_elements->item( $i )->getAttribute( 'crossorigin' ), '' );
2011+
}
20092012
}
20102013
}
20112014

0 commit comments

Comments
 (0)