From 0017edfb924002f0cf291d0e75f3325c928040f0 Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Tue, 28 Aug 2018 14:23:05 -0500 Subject: [PATCH 01/29] Add a 'type' to all validation errors Like Weston suggested, add this to the amp_validation_error term description. This will aid in searching that taxonomy page by error type. At the moment, there are only 3 types: HTML, JS, and CSS. --- .../sanitizers/class-amp-base-sanitizer.php | 8 ++++++ .../sanitizers/class-amp-style-sanitizer.php | 19 ++++++++++++++ .../class-amp-validation-error-taxonomy.php | 25 +++++++++++++++++++ tests/test-class-amp-base-sanitizer.php | 4 +++ tests/test-tag-and-attribute-sanitizer.php | 3 ++- 5 files changed, 58 insertions(+), 1 deletion(-) diff --git a/includes/sanitizers/class-amp-base-sanitizer.php b/includes/sanitizers/class-amp-base-sanitizer.php index b98c645b88b..8d77b6e6a62 100644 --- a/includes/sanitizers/class-amp-base-sanitizer.php +++ b/includes/sanitizers/class-amp-base-sanitizer.php @@ -436,6 +436,11 @@ public function prepare_validation_error( array $error = array(), array $data = if ( ! isset( $error['code'] ) ) { $error['code'] = AMP_Validation_Error_Taxonomy::INVALID_ELEMENT_CODE; } + + if ( ! isset( $error['type'] ) ) { + $error['type'] = 'script' === $node->nodeName ? AMP_Validation_Error_Taxonomy::JS_ERROR_TYPE : AMP_Validation_Error_Taxonomy::HTML_ERROR_TYPE; + } + $error['node_attributes'] = array(); foreach ( $node->attributes as $attribute ) { $error['node_attributes'][ $attribute->nodeName ] = $attribute->nodeValue; @@ -456,6 +461,9 @@ public function prepare_validation_error( array $error = array(), array $data = if ( ! isset( $error['code'] ) ) { $error['code'] = AMP_Validation_Error_Taxonomy::INVALID_ATTRIBUTE_CODE; } + if ( ! isset( $error['type'] ) ) { + $error['type'] = AMP_Validation_Error_Taxonomy::HTML_ERROR_TYPE; + } $error['element_attributes'] = array(); if ( $node->parentNode && $node->parentNode->hasAttributes() ) { foreach ( $node->parentNode->attributes as $attribute ) { diff --git a/includes/sanitizers/class-amp-style-sanitizer.php b/includes/sanitizers/class-amp-style-sanitizer.php index 050c24c69e1..6a7703f2dcf 100644 --- a/includes/sanitizers/class-amp-style-sanitizer.php +++ b/includes/sanitizers/class-amp-style-sanitizer.php @@ -673,6 +673,7 @@ private function process_link_element( DOMElement $element ) { $this->remove_invalid_child( $element, array( 'code' => $css_file_path->get_error_code(), 'message' => $css_file_path->get_error_message(), + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ) ); return; } else { @@ -682,6 +683,7 @@ private function process_link_element( DOMElement $element ) { $this->remove_invalid_child( $element, array( 'code' => $css_file_path->get_error_code(), 'message' => $css_file_path->get_error_message(), + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ) ); return; } else { @@ -691,6 +693,7 @@ private function process_link_element( DOMElement $element ) { if ( false === $stylesheet ) { $this->remove_invalid_child( $element, array( 'code' => 'stylesheet_file_missing', + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ) ); return; } @@ -887,6 +890,7 @@ private function parse_import_stylesheet( Import $item, CSSList $css_list, $opti $error = array( 'code' => $contents->get_error_code(), 'message' => $contents->get_error_message(), + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ); $sanitized = $this->should_sanitize_validation_error( $error ); if ( $sanitized ) { @@ -901,6 +905,7 @@ private function parse_import_stylesheet( Import $item, CSSList $css_list, $opti $error = array( 'code' => $css_file_path->get_error_code(), 'message' => $css_file_path->get_error_message(), + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ); $sanitized = $this->should_sanitize_validation_error( $error ); if ( $sanitized ) { @@ -988,6 +993,7 @@ function ( $value ) { $error = array( 'code' => 'css_parse_error', 'message' => $exception->getMessage(), + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ); /* @@ -1239,6 +1245,7 @@ private function process_css_list( CSSList $css_list, $options ) { $error = array( 'code' => 'illegal_css_at_rule', 'at_rule' => $css_item->atRuleName(), + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ); $sanitized = $this->should_sanitize_validation_error( $error ); $results[] = compact( 'error', 'sanitized' ); @@ -1259,6 +1266,7 @@ private function process_css_list( CSSList $css_list, $options ) { $error = array( 'code' => 'illegal_css_at_rule', 'at_rule' => $css_item->atRuleName(), + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ); $sanitized = $this->should_sanitize_validation_error( $error ); $results[] = compact( 'error', 'sanitized' ); @@ -1275,6 +1283,7 @@ private function process_css_list( CSSList $css_list, $options ) { $error = array( 'code' => 'illegal_css_at_rule', 'at_rule' => $css_item->atRuleName(), + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ); $sanitized = $this->should_sanitize_validation_error( $error ); $results[] = compact( 'error', 'sanitized' ); @@ -1290,6 +1299,7 @@ private function process_css_list( CSSList $css_list, $options ) { $error = array( 'code' => 'illegal_css_at_rule', 'at_rule' => $css_item->atRuleName(), + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ); $sanitized = $this->should_sanitize_validation_error( $error ); $results[] = compact( 'error', 'sanitized' ); @@ -1297,6 +1307,7 @@ private function process_css_list( CSSList $css_list, $options ) { $error = array( 'code' => 'unrecognized_css', 'item' => get_class( $css_item ), + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ); $sanitized = $this->should_sanitize_validation_error( $error ); $results[] = compact( 'error', 'sanitized' ); @@ -1382,6 +1393,7 @@ private function process_css_declaration_block( RuleSet $ruleset, CSSList $css_l 'code' => 'illegal_css_property', 'property_name' => $property->getRule(), 'property_value' => $property->getValue(), + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ); $sanitized = $this->should_sanitize_validation_error( $error ); if ( $sanitized ) { @@ -1398,6 +1410,7 @@ private function process_css_declaration_block( RuleSet $ruleset, CSSList $css_l 'code' => 'illegal_css_property', 'property_name' => $property->getRule(), 'property_value' => (string) $property->getValue(), + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ); $sanitized = $this->should_sanitize_validation_error( $error ); if ( $sanitized ) { @@ -1556,6 +1569,7 @@ private function process_css_keyframes( KeyFrame $css_list, $options ) { $error = array( 'code' => 'unrecognized_css', 'item' => get_class( $rules ), + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ); $sanitized = $this->should_sanitize_validation_error( $error ); if ( $sanitized ) { @@ -1578,6 +1592,7 @@ private function process_css_keyframes( KeyFrame $css_list, $options ) { 'code' => 'illegal_css_property', 'property_name' => $property->getRule(), 'property_value' => (string) $property->getValue(), + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ); $sanitized = $this->should_sanitize_validation_error( $error ); if ( $sanitized ) { @@ -1623,6 +1638,7 @@ private function transform_important_qualifiers( RuleSet $ruleset, CSSList $css_ } else { $error = array( 'code' => 'illegal_css_important', + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ); $sanitized = $this->should_sanitize_validation_error( $error ); if ( $sanitized ) { @@ -1901,6 +1917,7 @@ private function finalize_styles() { if ( ! $body ) { $this->should_sanitize_validation_error( array( 'code' => 'missing_body_element', + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ) ); } else { $style_element = $this->dom->createElement( 'style' ); @@ -1997,6 +2014,7 @@ private function finalize_stylesheet_set( $stylesheet_set ) { if ( $is_too_much_css && $should_tree_shake && empty( $this->args['accept_tree_shaking'] ) ) { $should_tree_shake = $this->should_sanitize_validation_error( array( 'code' => self::TREE_SHAKING_ERROR_CODE, + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ) ); } @@ -2066,6 +2084,7 @@ private function finalize_stylesheet_set( $stylesheet_set ) { if ( $final_size + $sheet_size > $stylesheet_set['cdata_spec']['max_bytes'] ) { $validation_error = array( 'code' => 'excessive_css', + 'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, ); if ( isset( $pending_stylesheet['sources'] ) ) { $validation_error['sources'] = $pending_stylesheet['sources']; diff --git a/includes/validation/class-amp-validation-error-taxonomy.php b/includes/validation/class-amp-validation-error-taxonomy.php index 9c8414dc7b7..4a2cc0a5403 100644 --- a/includes/validation/class-amp-validation-error-taxonomy.php +++ b/includes/validation/class-amp-validation-error-taxonomy.php @@ -75,6 +75,31 @@ class AMP_Validation_Error_Taxonomy { */ const INVALID_ATTRIBUTE_CODE = 'invalid_attribute'; + /** + * The 'type' of error that applies to most errors with the 'code' of 'invalid_element' and 'invalid_attribute'. + * + * Except for 'invalid_element' errors for a + assertEquals( 10, has_action( 'redirect_term_location', array( self::TESTED_CLASS, 'add_term_filter_query_var' ) ) ); $this->assertEquals( 10, has_action( 'load-edit-tags.php', array( self::TESTED_CLASS, 'add_group_terms_clauses_filter' ) ) ); $this->assertEquals( 10, has_action( 'load-edit-tags.php', array( self::TESTED_CLASS, 'add_error_type_clauses_filter' ) ) ); + $this->assertEquals( 10, has_action( sprintf( 'after-%s-table', AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG, array( self::TESTED_CLASS, 'render_taxonomy_filter' ) ) ) ); $this->assertEquals( 10, has_filter( 'user_has_cap', array( self::TESTED_CLASS, 'filter_user_has_cap_for_hiding_term_list_table_checkbox' ) ) ); $this->assertEquals( 10, has_filter( 'terms_clauses', array( self::TESTED_CLASS, 'filter_terms_clauses_for_description_search' ) ) ); $this->assertEquals( 10, has_action( 'admin_notices', array( self::TESTED_CLASS, 'add_admin_notices' ) ) ); @@ -444,6 +445,35 @@ public function test_add_error_type_clauses_filter() { $this->assertEquals( $initial_clauses, apply_filters( $tested_filter, $initial_clauses, $taxonomies ) ); } + /** + * Test render_taxonomy_filter. + * + * @covers \AMP_Validation_Error_Taxonomy::render_taxonomy_filter() + */ + public function test_render_taxonomy_filter() { + // When passing the wrong $taxonomy_name to the method, it should not output anything. + ob_start(); + AMP_Validation_Error_Taxonomy::render_taxonomy_filter( 'category' ); + $this->assertEmpty( ob_get_clean() ); + + // With the correct taxonomy name, the strings below should be present. + ob_start(); + AMP_Validation_Error_Taxonomy::render_taxonomy_filter( AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG ); + $markup = ob_get_clean(); + + $expected_to_contain = array( + AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_TYPE_QUERY_VAR, + AMP_Validation_Error_Taxonomy::HTML_ERROR_TYPE, + AMP_Validation_Error_Taxonomy::JS_ERROR_TYPE, + AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE, + ' %s', + esc_attr( add_query_arg( + 'post_type', + AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG, + admin_url( 'edit.php' ) + ) ), + esc_attr( self::ID_LINK_ERRORS_BY_URL ), + esc_html__( 'View errors by URL', 'amp' ) + ); + } + /** * Renders the error status filter - - - - - + + + + + text should not have 'With', like 'With JS Errors'. + $this->assertNotContains( 'With', $markup ); + + // On the edit.php page (Errors by URL), the