Skip to content

Commit

Permalink
Convert error node_type to constant name in JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwatkins0 committed Oct 21, 2020
1 parent f73e574 commit fe35801
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
29 changes: 25 additions & 4 deletions includes/validation/class-amp-validation-error-taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,30 @@ public static function add_admin_notices() {
}
}

/**
* Returns JSON-formatted error details for an error term.
*
* @param WP_Term $term The term.
* @return string Encoded JSON.
*/
public static function get_error_details_json( $term ) {
$json = json_decode( $term->description, true );

// Convert the numeric constant value of the node_type to its constant name.
$xml_reader_reflection_class = new ReflectionClass( 'XMLReader' );
$constants = $xml_reader_reflection_class->getConstants();
foreach ( $constants as $key => $value ) {
if ( $json['node_type'] === $value ) {
$json['node_type'] = $key;
break;
}
}

$json['status'] = (bool) ( (int) $term->term_group & self::ACCEPTED_VALIDATION_ERROR_BIT_MASK ) ? __( 'Removed', 'amp' ) : __( 'Kept', 'amp' );

return wp_json_encode( $json, JSON_PRETTY_PRINT );
}

/**
* Add row actions.
*
Expand Down Expand Up @@ -1620,13 +1644,10 @@ public static function filter_tag_row_actions( $actions, WP_Term $tag ) {
esc_html__( 'Details', 'amp' )
);

$json = json_decode( $term->description, true );
$json['status'] = (bool) ( (int) $term->term_group & self::ACCEPTED_VALIDATION_ERROR_BIT_MASK ) ? __( 'Removed', 'amp' ) : __( 'Kept', 'amp' );

$actions['copy'] = sprintf(
'<button type="button" aria-label="%s" class="single-url-detail-copy button-link" data-error-json="%s">%s</button>',
esc_attr__( 'Copy to clipboard', 'amp' ),
esc_attr( wp_json_encode( $json, JSON_PRETTY_PRINT ) ),
esc_attr( self::get_error_details_json( $term ) ),
esc_html__( 'Copy to clipboard', 'amp' )
);
} elseif ( 'edit-tags.php' === $pagenow ) {
Expand Down
33 changes: 33 additions & 0 deletions tests/php/validation/test-class-amp-validation-error-taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -1527,4 +1527,37 @@ public function get_mock_error() {
'node_type' => XML_ELEMENT_NODE,
];
}

/**
* Test get_error_details_json.
*
* @covers \AMP_Validation_Error_Taxonomy::get_error_details_json()
*/
public function test_get_error_details_json() {
$term = (object) [
'description' => wp_json_encode(
[
'node_type' => 1,
]
),
'term_group' => 1,
];
$result = json_decode( AMP_Validation_Error_Taxonomy::get_error_details_json( $term ), true );

$this->assertEquals( 'ELEMENT', $result['node_type'] );
$this->assertEquals( 'Removed', $result['status'] );

$term = (object) [
'description' => wp_json_encode(
[
'node_type' => 2,
]
),
'term_group' => 2,
];
$result = json_decode( AMP_Validation_Error_Taxonomy::get_error_details_json( $term ), true );

$this->assertEquals( 'ATTRIBUTE', $result['node_type'] );
$this->assertEquals( 'Kept', $result['status'] );
}
}

0 comments on commit fe35801

Please sign in to comment.