From c20360fe836870c985969d9374a65c1713f7fe6a Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sat, 18 May 2019 20:59:25 -0700 Subject: [PATCH] Add test for stylesheet prioritization --- tests/test-amp-style-sanitizer.php | 129 +++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/tests/test-amp-style-sanitizer.php b/tests/test-amp-style-sanitizer.php index 679d025ee4f..01893c7b1f2 100644 --- a/tests/test-amp-style-sanitizer.php +++ b/tests/test-amp-style-sanitizer.php @@ -12,6 +12,26 @@ */ class AMP_Style_Sanitizer_Test extends WP_UnitTestCase { + /** + * Set up. + */ + public function setUp() { + parent::setUp(); + global $wp_styles, $wp_scripts; + $wp_styles = null; + $wp_scripts = null; + } + + /** + * Tear down. + */ + public function tearDown() { + parent::tearDown(); + global $wp_styles, $wp_scripts; + $wp_styles = null; + $wp_scripts = null; + } + /** * Get data for tests. * @@ -2124,4 +2144,113 @@ public function test_style_element_cdata() { $expected = "body{color:red}body{color:green}body{color:blue}\n\n/*# sourceURL=amp-custom.css */"; $this->assertEquals( $expected, $style->nodeValue ); } + + /** + * Get prioritization test data. + * + * @return array + */ + public function get_prioritization_data() { + add_filter( + 'theme_root', + function () { + return ABSPATH . 'wp-content/themes'; + } + ); + add_filter( + 'theme_root_uri', + function () { + return site_url( 'wp-content/themes' ); + } + ); + + $render_template = function () { + ob_start(); + ?> + + array( + function () use ( $render_template ) { + show_admin_bar( true ); + _wp_admin_bar_init(); + switch_theme( 'twentyten' ); + require_once get_template_directory() . '/functions.php'; + add_action( + 'wp_head', + function() { + printf( '', esc_html( str_repeat( 'a', 10000 ) ) ); + }, + -1000 + ); + add_action( 'wp_enqueue_scripts', 'twentyten_scripts_styles' ); + add_action( 'wp_enqueue_scripts', 'AMP_Theme_Support::enqueue_assets' ); + wp_add_inline_style( 'admin-bar', '.admin-bar-inline-style{ color:red }' ); + wp_set_current_user( $this->factory()->user->create( array( 'role' => 'administrator' ) ) ); + + return $render_template(); + }, + function( $original_dom, $original_source, $amphtml_dom, $amphtml_source ) { + /** + * Vars. + * + * @var DOMDocument $original_dom + * @var string $original_source + * @var DOMDocument $amphtml_dom + * @var string $amphtml_source + */ + $this->assertInstanceOf( 'DOMElement', $original_dom->getElementById( 'wpadminbar' ), 'Expected admin bar element to be present originally.' ); + $this->assertInstanceOf( 'DOMElement', $original_dom->getElementById( 'admin-bar-css' ), 'Expected admin bar CSS to be present originally.' ); + $this->assertContains( 'earlyprintstyle', $original_source, 'Expected early print style to not be present.' ); + + $this->assertContains( '.is-style-outline .wp-block-button__link', $amphtml_source, 'Expected block-library/style.css' ); + $this->assertContains( '[class^="wp-block-"]:not(.wp-block-gallery) figcaption', $amphtml_source, 'Expected twentyten/blocks.css' ); + $this->assertContains( 'amp-img.amp-wp-enforced-sizes', $amphtml_source, 'Expected amp-default.css' ); + $this->assertNotContains( 'ab-empty-item', $amphtml_source, 'Expected admin-bar.css to not be present.' ); + $this->assertNotContains( 'earlyprintstyle', $amphtml_source, 'Expected early print style to not be present.' ); + $this->assertNotContains( 'admin-bar-inline-style', $amphtml_source, 'Expected admin-bar.css inline style to not be present.' ); + $this->assertEmpty( $amphtml_dom->getElementById( 'wpadminbar' ) ); + }, + ), + ); + } + + /** + * Test stylesheet prioritization. + * + * @dataProvider get_prioritization_data + * @covers \AMP_Style_Sanitizer::finalize_stylesheet_group() + * @covers \AMP_Style_Sanitizer::get_stylesheet_priority() + * + * @param callable $html_generator Generator of HTML. + * @param callable $assert Function which runs assertions. + */ + public function test_prioritized_stylesheets( $html_generator, $assert ) { + add_theme_support( 'amp' ); + $this->go_to( home_url() ); + $html = $html_generator(); + + $original_dom = AMP_DOM_Utils::get_dom( $html ); + $amphtml_dom = clone $original_dom; + + $error_codes = array(); + $args = array( + 'use_document_element' => true, + 'remove_unused_rules' => 'never', + 'validation_error_callback' => function( $error ) use ( &$error_codes ) { + $error_codes[] = $error['code']; + }, + ); + + $sanitizer = new AMP_Style_Sanitizer( $amphtml_dom, $args ); + $sanitizer->sanitize(); + + $whitelist_sanitizer = new AMP_Tag_And_Attribute_Sanitizer( $amphtml_dom, $args ); + $whitelist_sanitizer->sanitize(); + + $assert( $original_dom, $html, $amphtml_dom, $amphtml_dom->saveHTML(), $sanitizer->get_stylesheets() ); + } }