Skip to content

Commit

Permalink
Test actual codes for errors raised in validation error callback
Browse files Browse the repository at this point in the history
Add tests for disallowed_file_extension and file_path_not_found
  • Loading branch information
westonruter committed Apr 17, 2018
1 parent 947cbb3 commit b9a0668
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
5 changes: 3 additions & 2 deletions includes/sanitizers/class-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public function get_validated_url_file_path( $url, $allowed_extensions = array()
$pattern = sprintf( '/\.(%s)$/i', implode( '|', $allowed_extensions ) );
if ( ! preg_match( $pattern, $url ) ) {
/* translators: %s is the file URL */
return new WP_Error( 'amp_disallowed_file_extension', sprintf( __( 'Skipped file which does not have an allowed file extension (%s).', 'amp' ), $url ) );
return new WP_Error( 'disallowed_file_extension', sprintf( __( 'Skipped file which does not have an allowed file extension (%s).', 'amp' ), $url ) );
}
}

Expand All @@ -337,7 +337,7 @@ public function get_validated_url_file_path( $url, $allowed_extensions = array()

if ( ! $file_path || false !== strpos( '../', $file_path ) || 0 !== validate_file( $file_path ) || ! file_exists( $file_path ) ) {
/* translators: %s is file URL */
return new WP_Error( 'amp_file_path_not_found', sprintf( __( 'Unable to locate filesystem path for %s.', 'amp' ), $url ) );
return new WP_Error( 'file_path_not_found', sprintf( __( 'Unable to locate filesystem path for %s.', 'amp' ), $url ) );
}

return $file_path;
Expand Down Expand Up @@ -406,6 +406,7 @@ private function process_link_element( DOMElement $element ) {
$css_file_path = $this->get_validated_url_file_path( $href, array( 'css', 'less', 'scss', 'sass' ) );
if ( is_wp_error( $css_file_path ) ) {
$this->remove_invalid_child( $element, array(
'code' => $css_file_path->get_error_code(),
'message' => $css_file_path->get_error_message(),
) );
return;
Expand Down
43 changes: 27 additions & 16 deletions tests/test-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,12 @@ public function get_stylesheet_urls() {
'amp_disallowed_file_extension' => array(
content_url( 'themes/twentyseventeen/index.php' ),
null,
'amp_disallowed_file_extension',
'disallowed_file_extension',
),
'amp_file_path_not_found' => array(
content_url( 'themes/twentyseventeen/404.css' ),
null,
'amp_file_path_not_found',
'file_path_not_found',
),
);
}
Expand Down Expand Up @@ -581,31 +581,35 @@ public function get_font_urls() {
return array(
'tangerine' => array(
'https://fonts.googleapis.com/css?family=Tangerine',
true,
array(),
),
'tangerine2' => array(
'//fonts.googleapis.com/css?family=Tangerine',
true,
array(),
),
'tangerine3' => array(
'http://fonts.googleapis.com/css?family=Tangerine',
true,
array(),
),
'typekit' => array(
'https://use.typekit.net/abc.css',
true,
array(),
),
'fontscom' => array(
'https://fast.fonts.net/abc.css',
true,
array(),
),
'fontawesome' => array(
'https://maxcdn.bootstrapcdn.com/font-awesome/123/css/font-awesome.min.css',
true,
array(),
),
'fontbad' => array(
'https://bad.example.com/font.css',
false,
'bad_ext' => array(
home_url( '/bad.php' ),
array( 'disallowed_file_extension' ),
),
'bad_file' => array(
home_url( '/bad.css' ),
array( 'file_path_not_found' ),
),
);
}
Expand All @@ -614,19 +618,26 @@ public function get_font_urls() {
* Tests that font URLs get validated.
*
* @dataProvider get_font_urls
* @param string $url Font URL.
* @param bool $pass Whether the font URL is ok.
* @param string $url Font URL.
* @param array $error_codes Error codes.
*/
public function test_font_urls( $url, $pass ) {
public function test_font_urls( $url, $error_codes ) {
$dom = AMP_DOM_Utils::get_dom( sprintf( '<html><head><link rel="stylesheet" href="%s"></head></html>', $url ) ); // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet

$validation_errors = array();

$sanitizer = new AMP_Style_Sanitizer( $dom, array(
'use_document_element' => true,
'use_document_element' => true,
'validation_error_callback' => function( $error ) use ( &$validation_errors ) {
$validation_errors[] = $error;
},
) );
$sanitizer->sanitize();

$this->assertEqualSets( $error_codes, wp_list_pluck( $validation_errors, 'code' ) );

$link = $dom->getElementsByTagName( 'link' )->item( 0 );
if ( $pass ) {
if ( empty( $error_codes ) ) {
$this->assertInstanceOf( 'DOMElement', $link );
$this->assertEquals(
preg_replace( '#^(http:)?(?=//)#', 'https:', $url ),
Expand Down

0 comments on commit b9a0668

Please sign in to comment.