diff --git a/includes/embeds/class-amp-base-embed-handler.php b/includes/embeds/class-amp-base-embed-handler.php index f46874b1974..a3d9ee7a9e8 100644 --- a/includes/embeds/class-amp-base-embed-handler.php +++ b/includes/embeds/class-amp-base-embed-handler.php @@ -11,18 +11,52 @@ * Class AMP_Base_Embed_Handler */ abstract class AMP_Base_Embed_Handler { + /** + * Default width. + * + * @var int + */ protected $DEFAULT_WIDTH = 600; + + /** + * Default height. + * + * @var int + */ protected $DEFAULT_HEIGHT = 480; + /** + * Default arguments. + * + * @var array + */ protected $args = array(); + + /** + * Whether or not conversion was completed. + * + * @var boolean + */ protected $did_convert_elements = false; - abstract function register_embed(); - abstract function unregister_embed(); + /** + * Register embed. + */ + abstract public function register_embed(); - function __construct( $args = array() ) { + /** + * Unregister embed. + */ + abstract public function unregister_embed(); + + /** + * AMP_Base_Embed_Handler constructor. + * + * @param array $args Height and width for embed. + */ + public function __construct( $args = array() ) { $this->args = wp_parse_args( $args, array( - 'width' => $this->DEFAULT_WIDTH, + 'width' => $this->DEFAULT_WIDTH, 'height' => $this->DEFAULT_HEIGHT, ) ); } diff --git a/includes/embeds/class-amp-dailymotion-embed.php b/includes/embeds/class-amp-dailymotion-embed.php index 7c58905948f..19db158f8db 100644 --- a/includes/embeds/class-amp-dailymotion-embed.php +++ b/includes/embeds/class-amp-dailymotion-embed.php @@ -13,31 +13,59 @@ class AMP_DailyMotion_Embed_Handler extends AMP_Base_Embed_Handler { const URL_PATTERN = '#https?:\/\/(www\.)?dailymotion\.com\/video\/.*#i'; - const RATIO = 0.5625; + const RATIO = 0.5625; + /** + * Default width. + * + * @var int + */ protected $DEFAULT_WIDTH = 600; + + /** + * Default height. + * + * @var int + */ protected $DEFAULT_HEIGHT = 338; - function __construct( $args = array() ) { + /** + * AMP_DailyMotion_Embed_Handler constructor. + * + * @param array $args Height, width and maximum width for embed. + */ + public function __construct( $args = array() ) { parent::__construct( $args ); if ( isset( $this->args['content_max_width'] ) ) { - $max_width = $this->args['content_max_width']; - $this->args['width'] = $max_width; + $max_width = $this->args['content_max_width']; + $this->args['width'] = $max_width; $this->args['height'] = round( $max_width * self::RATIO ); } } - function register_embed() { + /** + * Register embed. + */ + public function register_embed() { wp_embed_register_handler( 'amp-dailymotion', self::URL_PATTERN, array( $this, 'oembed' ), -1 ); add_shortcode( 'dailymotion', array( $this, 'shortcode' ) ); } + /** + * Unregister embed. + */ public function unregister_embed() { wp_embed_unregister_handler( 'amp-dailymotion', -1 ); remove_shortcode( 'dailymotion' ); } + /** + * Gets AMP-compliant markup for the Dailymotion shortcode. + * + * @param array $attr The Dailymotion attributes. + * @return string Dailymotion shortcode markup. + */ public function shortcode( $attr ) { $video_id = false; @@ -53,25 +81,53 @@ public function shortcode( $attr ) { return ''; } - return $this->render( array( - 'video_id' => $video_id, - ) ); + return $this->render( + array( + 'video_id' => $video_id, + ) + ); } + /** + * Render oEmbed. + * + * @see \WP_Embed::shortcode() + * + * @param array $matches URL pattern matches. + * @param array $attr Shortcode attribues. + * @param string $url URL. + * @param string $rawattr Unmodified shortcode attributes. + * @return string Rendered oEmbed. + */ public function oembed( $matches, $attr, $url, $rawattr ) { $video_id = $this->get_video_id_from_url( $url ); - return $this->render( array( - 'video_id' => $video_id, - ) ); + return $this->render( + array( + 'video_id' => $video_id, + ) + ); } + /** + * Render. + * + * @param array $args Args. + * @return string Rendered. + */ public function render( $args ) { - $args = wp_parse_args( $args, array( - 'video_id' => false, - ) ); + $args = wp_parse_args( + $args, array( + 'video_id' => false, + ) + ); if ( empty( $args['video_id'] ) ) { - return AMP_HTML_Utils::build_tag( 'a', array( 'href' => esc_url( $args['url'] ), 'class' => 'amp-wp-embed-fallback' ), esc_html( $args['url'] ) ); + return AMP_HTML_Utils::build_tag( + 'a', array( + 'href' => esc_url( $args['url'] ), + 'class' => 'amp-wp-embed-fallback', + ), esc_html( $args['url'] ) + ); } $this->did_convert_elements = true; @@ -80,18 +136,24 @@ public function render( $args ) { 'amp-dailymotion', array( 'data-videoid' => $args['video_id'], - 'layout' => 'responsive', - 'width' => $this->args['width'], - 'height' => $this->args['height'], + 'layout' => 'responsive', + 'width' => $this->args['width'], + 'height' => $this->args['height'], ) ); } + /** + * Determine the video ID from the URL. + * + * @param string $url URL. + * @return integer Video ID. + */ private function get_video_id_from_url( $url ) { $parsed_url = AMP_WP_Utils::parse_url( $url ); parse_str( $parsed_url['path'], $path ); - $tok = explode( '/', $parsed_url['path'] ); - $tok = explode( '_', $tok[2] ); + $tok = explode( '/', $parsed_url['path'] ); + $tok = explode( '_', $tok[2] ); $video_id = $tok[0]; return $video_id; diff --git a/includes/embeds/class-amp-twitter-embed.php b/includes/embeds/class-amp-twitter-embed.php index 8642b5e55d4..735384a16fe 100644 --- a/includes/embeds/class-amp-twitter-embed.php +++ b/includes/embeds/class-amp-twitter-embed.php @@ -13,17 +13,29 @@ class AMP_Twitter_Embed_Handler extends AMP_Base_Embed_Handler { const URL_PATTERN = '#http(s|):\/\/twitter\.com(\/\#\!\/|\/)([a-zA-Z0-9_]{1,20})\/status(es)*\/(\d+)#i'; + /** + * Register embed. + */ public function register_embed() { add_shortcode( 'tweet', array( $this, 'shortcode' ) ); wp_embed_register_handler( 'amp-twitter', self::URL_PATTERN, array( $this, 'oembed' ), -1 ); } + /** + * Unregister embed. + */ public function unregister_embed() { remove_shortcode( 'tweet' ); wp_embed_unregister_handler( 'amp-twitter', -1 ); } - function shortcode( $attr ) { + /** + * Gets AMP-compliant markup for the Twitter shortcode. + * + * @param array $attr The Twitter attributes. + * @return string Twitter shortcode markup. + */ + public function shortcode( $attr ) { $attr = wp_parse_args( $attr, array( 'tweet' => false, ) ); @@ -52,14 +64,25 @@ function shortcode( $attr ) { 'amp-twitter', array( 'data-tweetid' => $id, - 'layout' => 'responsive', - 'width' => $this->args['width'], - 'height' => $this->args['height'], + 'layout' => 'responsive', + 'width' => $this->args['width'], + 'height' => $this->args['height'], ) ); } - function oembed( $matches, $attr, $url, $rawattr ) { + /** + * Render oEmbed. + * + * @see \WP_Embed::shortcode() + * + * @param array $matches URL pattern matches. + * @param array $attr Shortcode attribues. + * @param string $url URL. + * @param string $rawattr Unmodified shortcode attributes. + * @return string Rendered oEmbed. + */ + public function oembed( $matches, $attr, $url, $rawattr ) { $id = false; if ( isset( $matches[5] ) && is_numeric( $matches[5] ) ) { diff --git a/includes/embeds/class-amp-vimeo-embed.php b/includes/embeds/class-amp-vimeo-embed.php index 6b334ce6441..f475b894e48 100644 --- a/includes/embeds/class-amp-vimeo-embed.php +++ b/includes/embeds/class-amp-vimeo-embed.php @@ -16,39 +16,67 @@ class AMP_Vimeo_Embed_Handler extends AMP_Base_Embed_Handler { const RATIO = 0.5625; + /** + * Default width. + * + * @var int + */ protected $DEFAULT_WIDTH = 600; + + /** + * Default height. + * + * @var int + */ protected $DEFAULT_HEIGHT = 338; - function __construct( $args = array() ) { + /** + * AMP_Vimeo_Embed_Handler constructor. + * + * @param array $args Height, width and maximum width for embed. + */ + public function __construct( $args = array() ) { parent::__construct( $args ); if ( isset( $this->args['content_max_width'] ) ) { - $max_width = $this->args['content_max_width']; - $this->args['width'] = $max_width; + $max_width = $this->args['content_max_width']; + $this->args['width'] = $max_width; $this->args['height'] = round( $max_width * self::RATIO ); } } - function register_embed() { + /** + * Register embed. + */ + public function register_embed() { wp_embed_register_handler( 'amp-vimeo', self::URL_PATTERN, array( $this, 'oembed' ), -1 ); add_shortcode( 'vimeo', array( $this, 'shortcode' ) ); add_filter( 'wp_video_shortcode_override', array( $this, 'video_override' ), 10, 2 ); } + /** + * Unregister embed. + */ public function unregister_embed() { wp_embed_unregister_handler( 'amp-vimeo', -1 ); remove_shortcode( 'vimeo' ); } + /** + * Gets AMP-compliant markup for the Vimeo shortcode. + * + * @param array $attr The Vimeo attributes. + * @return string Vimeo shortcode markup. + */ public function shortcode( $attr ) { $video_id = false; if ( isset( $attr['id'] ) ) { $video_id = $attr['id']; } elseif ( isset( $attr['url'] ) ) { - $video_id = $this->get_video_id_from_url($attr['url']); - }elseif ( isset( $attr[0] ) ) { - $video_id = $this->get_video_id_from_url($attr[0]); + $video_id = $this->get_video_id_from_url( $attr['url'] ); + } elseif ( isset( $attr[0] ) ) { + $video_id = $this->get_video_id_from_url( $attr[0] ); } elseif ( function_exists( 'shortcode_new_to_old_params' ) ) { $video_id = shortcode_new_to_old_params( $attr ); } @@ -57,27 +85,55 @@ public function shortcode( $attr ) { return ''; } - return $this->render( array( - 'video_id' => $video_id, - ) ); + return $this->render( + array( + 'video_id' => $video_id, + ) + ); } + /** + * Render oEmbed. + * + * @see \WP_Embed::shortcode() + * + * @param array $matches URL pattern matches. + * @param array $attr Shortcode attribues. + * @param string $url URL. + * @param string $rawattr Unmodified shortcode attributes. + * @return string Rendered oEmbed. + */ public function oembed( $matches, $attr, $url, $rawattr ) { $video_id = $this->get_video_id_from_url( $url ); - return $this->render( array( - 'url' => $url, - 'video_id' => $video_id, - ) ); + return $this->render( + array( + 'url' => $url, + 'video_id' => $video_id, + ) + ); } + /** + * Render. + * + * @param array $args Args. + * @return string Rendered. + */ public function render( $args ) { - $args = wp_parse_args( $args, array( - 'video_id' => false, - ) ); + $args = wp_parse_args( + $args, array( + 'video_id' => false, + ) + ); if ( empty( $args['video_id'] ) ) { - return AMP_HTML_Utils::build_tag( 'a', array( 'href' => esc_url( $args['url'] ), 'class' => 'amp-wp-embed-fallback' ), esc_html( $args['url'] ) ); + return AMP_HTML_Utils::build_tag( + 'a', array( + 'href' => esc_url( $args['url'] ), + 'class' => 'amp-wp-embed-fallback', + ), esc_html( $args['url'] ) + ); } $this->did_convert_elements = true; @@ -86,24 +142,27 @@ public function render( $args ) { 'amp-vimeo', array( 'data-videoid' => $args['video_id'], - 'layout' => 'responsive', - 'width' => $this->args['width'], - 'height' => $this->args['height'], + 'layout' => 'responsive', + 'width' => $this->args['width'], + 'height' => $this->args['height'], ) ); } - // get_video_id_from_url() - // Takes the last component of a Vimeo URL - // and returns it as the associated video id + /** + * Determine the video ID from the URL. + * + * @param string $url URL. + * @return integer Video ID. + */ private function get_video_id_from_url( $url ) { $parsed_url = parse_url( $url ); parse_str( $parsed_url['path'], $path ); - $video_id = ""; + $video_id = ''; if ( $path ) { - $tok = explode( '/', $parsed_url['path'] ); - $video_id = end($tok); + $tok = explode( '/', $parsed_url['path'] ); + $video_id = end( $tok ); } return $video_id; diff --git a/includes/embeds/class-amp-youtube-embed.php b/includes/embeds/class-amp-youtube-embed.php index a019a6a6334..d8d5bc2d6c2 100644 --- a/includes/embeds/class-amp-youtube-embed.php +++ b/includes/embeds/class-amp-youtube-embed.php @@ -14,37 +14,65 @@ class AMP_YouTube_Embed_Handler extends AMP_Base_Embed_Handler { const SHORT_URL_HOST = 'youtu.be'; // Only handling single videos. Playlists are handled elsewhere. const URL_PATTERN = '#https?://(?:www\.)?(?:youtube.com/(?:v/|e/|embed/|watch[/\#?])|youtu\.be/).*#i'; - const RATIO = 0.5625; + const RATIO = 0.5625; + /** + * Default width. + * + * @var int + */ protected $DEFAULT_WIDTH = 600; + + /** + * Default height. + * + * @var int + */ protected $DEFAULT_HEIGHT = 338; - function __construct( $args = array() ) { + /** + * AMP_YouTube_Embed_Handler constructor. + * + * @param array $args Height, width and maximum width for embed. + */ + public function __construct( $args = array() ) { parent::__construct( $args ); if ( isset( $this->args['content_max_width'] ) ) { - $max_width = $this->args['content_max_width']; + $max_width = $this->args['content_max_width']; $this->args['width'] = $max_width; $this->args['height'] = round( $max_width * self::RATIO ); } } - function register_embed() { + /** + * Register embed. + */ + public function register_embed() { wp_embed_register_handler( 'amp-youtube', self::URL_PATTERN, array( $this, 'oembed' ), -1 ); add_shortcode( 'youtube', array( $this, 'shortcode' ) ); add_filter( 'wp_video_shortcode_override', array( $this, 'video_override' ), 10, 2 ); } + /** + * Unregister embed. + */ public function unregister_embed() { wp_embed_unregister_handler( 'amp-youtube', -1 ); remove_shortcode( 'youtube' ); } + /** + * Gets AMP-compliant markup for the YouTube shortcode. + * + * @param array $attr The YouTube attributes. + * @return string YouTube shortcode markup. + */ public function shortcode( $attr ) { - $url = false; + $url = false; $video_id = false; if ( isset( $attr[0] ) ) { - $url = ltrim( $attr[0] , '=' ); + $url = ltrim( $attr[0], '=' ); } elseif ( function_exists( 'shortcode_new_to_old_params' ) ) { $url = shortcode_new_to_old_params( $attr ); } @@ -55,23 +83,49 @@ public function shortcode( $attr ) { $video_id = $this->get_video_id_from_url( $url ); - return $this->render( array( - 'url' => $url, - 'video_id' => $video_id, - ) ); + return $this->render( + array( + 'url' => $url, + 'video_id' => $video_id, + ) + ); } + /** + * Render oEmbed. + * + * @see \WP_Embed::shortcode() + * + * @param array $matches URL pattern matches. + * @param array $attr Shortcode attribues. + * @param string $url URL. + * @param string $rawattr Unmodified shortcode attributes. + * @return string Rendered oEmbed. + */ public function oembed( $matches, $attr, $url, $rawattr ) { return $this->shortcode( array( $url ) ); } + /** + * Render. + * + * @param array $args Args. + * @return string Rendered. + */ public function render( $args ) { - $args = wp_parse_args( $args, array( - 'video_id' => false, - ) ); + $args = wp_parse_args( + $args, array( + 'video_id' => false, + ) + ); if ( empty( $args['video_id'] ) ) { - return AMP_HTML_Utils::build_tag( 'a', array( 'href' => esc_url( $args['url'] ), 'class' => 'amp-wp-embed-fallback' ), esc_html( $args['url'] ) ); + return AMP_HTML_Utils::build_tag( + 'a', array( + 'href' => esc_url( $args['url'] ), + 'class' => 'amp-wp-embed-fallback', + ), esc_html( $args['url'] ) + ); } $this->did_convert_elements = true; @@ -80,25 +134,31 @@ public function render( $args ) { 'amp-youtube', array( 'data-videoid' => $args['video_id'], - 'layout' => 'responsive', - 'width' => $this->args['width'], - 'height' => $this->args['height'], + 'layout' => 'responsive', + 'width' => $this->args['width'], + 'height' => $this->args['height'], ) ); } + /** + * Determine the video ID from the URL. + * + * @param string $url URL. + * @return integer Video ID. + */ private function get_video_id_from_url( $url ) { - $video_id = false; + $video_id = false; $parsed_url = AMP_WP_Utils::parse_url( $url ); if ( self::SHORT_URL_HOST === substr( $parsed_url['host'], -strlen( self::SHORT_URL_HOST ) ) ) { - // youtu.be/{id} + /* youtu.be/{id} */ $parts = explode( '/', $parsed_url['path'] ); if ( ! empty( $parts ) ) { $video_id = $parts[1]; } } else { - // ?v={id} or ?list={id} + /* The query looks like ?v={id} or ?list={id} */ parse_str( $parsed_url['query'], $query_args ); if ( isset( $query_args['v'] ) ) { @@ -107,7 +167,7 @@ private function get_video_id_from_url( $url ) { } if ( empty( $video_id ) ) { - // /(v|e|embed)/{id} + /* The path looks like /(v|e|embed)/{id} */ $parts = explode( '/', $parsed_url['path'] ); if ( in_array( $parts[1], array( 'v', 'e', 'embed' ), true ) ) { @@ -118,8 +178,14 @@ private function get_video_id_from_url( $url ) { return $video_id; } + /** + * Sanitize the v= argument in the URL. + * + * @param string $value query parameters. + * @return string First set of query parameters. + */ private function sanitize_v_arg( $value ) { - // Deal with broken params like `?v=123?rel=0` + // Deal with broken params like `?v=123?rel=0`. if ( false !== strpos( $value, '?' ) ) { $value = strtok( $value, '?' ); } diff --git a/includes/utils/class-amp-image-dimension-extractor.php b/includes/utils/class-amp-image-dimension-extractor.php index e659b4fc860..a67f04099e5 100644 --- a/includes/utils/class-amp-image-dimension-extractor.php +++ b/includes/utils/class-amp-image-dimension-extractor.php @@ -198,7 +198,7 @@ private static function fetch_images_via_faster_image( $urls_to_fetch, &$images $urls = array_keys( $urls_to_fetch ); if ( ! function_exists( 'amp_get_fasterimage_client' ) ) { - require_once( AMP__DIR__ . '/includes/lib/fasterimage/amp-fasterimage.php' ); + require_once AMP__DIR__ . '/includes/lib/fasterimage/amp-fasterimage.php'; } $user_agent = apply_filters( 'amp_extract_image_dimensions_get_user_agent', self::get_default_user_agent() ); diff --git a/jetpack-helper.php b/jetpack-helper.php index 20b4680e7ba..f6394197c01 100644 --- a/jetpack-helper.php +++ b/jetpack-helper.php @@ -63,7 +63,7 @@ function jetpack_amp_build_stats_pixel_url() { $data = compact( 'v', 'j', 'blog', 'post', 'tz', 'srv' ); } - $data['host'] = isset( $_SERVER['HTTP_HOST'] ) ? rawurlencode( $_SERVER['HTTP_HOST'] ) : ''; // input var ok + $data['host'] = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : ''; // input var ok. $data['rand'] = 'RANDOM'; // amp placeholder $data['ref'] = 'DOCUMENT_REFERRER'; // amp placeholder $data = array_map( 'rawurlencode' , $data ); diff --git a/phpcs.xml b/phpcs.xml index 16c6f31e59f..fb1137e950a 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -28,7 +28,7 @@ - + diff --git a/tests/amp-tag-and-attribute-sanitizer-private-methods-tests.php b/tests/amp-tag-and-attribute-sanitizer-private-methods-tests.php index a2115bc7e36..ab9fcee3dd1 100644 --- a/tests/amp-tag-and-attribute-sanitizer-private-methods-tests.php +++ b/tests/amp-tag-and-attribute-sanitizer-private-methods-tests.php @@ -409,7 +409,7 @@ public function test_validate_attr_spec_rules( $data, $expected ) { $got = $this->invoke_method( $sanitizer, $data['func_name'], array( $node, $data['attribute_name'], $attr_spec_rule ) ); - if ( $expected != $got ) { + if ( $expected !== $got ) { printf( 'using source: %s' . PHP_EOL, $source ); var_dump( $data ); } @@ -566,7 +566,7 @@ public function test_is_allowed_attribute( $data, $expected ) { $got = $this->invoke_method( $sanitizer, $data['func_name'], array( $data['attribute_name'], $attr_spec_list ) ); - if ( $expected != $got ) { + if ( $expected !== $got ) { printf( 'using source: %s' . PHP_EOL, $source ); var_dump( $data ); } @@ -662,7 +662,7 @@ public function test_remove_node( $data, $expected ) { $got = AMP_DOM_Utils::get_content_from_dom( $dom ); - if ( $expected != $got ) { + if ( $expected !== $got ) { printf( 'using source: %s' . PHP_EOL, $data['source'] ); var_dump( $data ); printf( 'got = %s' .PHP_EOL, $got ); @@ -803,7 +803,7 @@ public function test_replace_node_with_children( $data, $expected ) { $got = AMP_DOM_Utils::get_content_from_dom( $dom ); $got = preg_replace( '/(?<=>)\s+(?=<)/', '', $got ); - if ( $expected != $got ) { + if ( $expected !== $got ) { printf( 'using source: %s' . PHP_EOL, $data['source'] ); var_dump( $data ); printf( 'got = %s' .PHP_EOL, $got ); @@ -865,7 +865,7 @@ public function test_get_ancestor_with_tag_name( $data, $expected ) { $got = $this->invoke_method( $sanitizer, 'get_ancestor_with_tag_name', array( $node, $data['ancestor_tag_name'] ) ); - if ( $ancestor_node != $got ) { + if ( $ancestor_node !== $got ) { printf( 'using source: %s' . PHP_EOL, $data['source'] ); var_dump( $data ); } @@ -1071,7 +1071,7 @@ public function test_validate_attr_spec_list_for_node( $data, $expected ) { $got = $this->invoke_method( $sanitizer, 'validate_attr_spec_list_for_node', array( $node, $data['attr_spec_list'] ) ); - if ( $expected != $got ) { + if ( $expected !== $got ) { printf( 'using source: %s' . PHP_EOL, $data['source'] ); var_dump( $data ); } @@ -1198,7 +1198,7 @@ public function test_check_attr_spec_rule_value( $data, $expected ) { $got = $this->invoke_method( $sanitizer, 'check_attr_spec_rule_value', array( $node, $data['attr_name'], $data['attr_spec_rule'] ) ); - if ( $expected != $got ) { + if ( $expected !== $got ) { printf( 'using source: %s' . PHP_EOL, $data['source'] ); var_dump( $data ); } @@ -1350,7 +1350,7 @@ public function test_check_attr_spec_rule_value_casei( $data, $expected ) { $got = $this->invoke_method( $sanitizer, 'check_attr_spec_rule_value_casei', array( $node, $data['attr_name'], $data['attr_spec_rule'] ) ); - if ( $expected != $got ) { + if ( $expected !== $got ) { printf( 'using source: %s' . PHP_EOL, $data['source'] ); var_dump( $data ); } @@ -1444,7 +1444,7 @@ public function test_check_attr_spec_rule_blacklisted_value_regex( $data, $expec $got = $this->invoke_method( $sanitizer, 'check_attr_spec_rule_blacklisted_value_regex', array( $node, $data['attr_name'], $data['attr_spec_rule'] ) ); - if ( $expected != $got ) { + if ( $expected !== $got ) { printf( 'using source: %s' . PHP_EOL, $data['source'] ); var_dump( $data ); } @@ -1579,7 +1579,7 @@ public function test_check_attr_spec_rule_allowed_protocol( $data, $expected ) { $got = $this->invoke_method( $sanitizer, 'check_attr_spec_rule_allowed_protocol', array( $node, $data['attr_name'], $data['attr_spec_rule'] ) ); - if ( $expected != $got ) { + if ( $expected !== $got ) { printf( 'using source: %s' . PHP_EOL, $data['source'] ); var_dump( $data ); } @@ -1728,7 +1728,7 @@ public function test_check_attr_spec_rule_disallowed_relative( $data, $expected $got = $this->invoke_method( $sanitizer, 'check_attr_spec_rule_disallowed_relative', array( $node, $data['attr_name'], $data['attr_spec_rule'] ) ); - if ( $expected != $got ) { + if ( $expected !== $got ) { printf( 'using source: %s' . PHP_EOL, $data['source'] ); var_dump( $data ); } diff --git a/tests/test-amp-analytics-options.php b/tests/test-amp-analytics-options.php index 274934ffc45..4b99936833c 100644 --- a/tests/test-amp-analytics-options.php +++ b/tests/test-amp-analytics-options.php @@ -82,7 +82,7 @@ private function insert_one_option( $type, $config ) { /** * Test that nothing is added if no analytics option defined in the DB */ - function test_no_options() { + public function test_no_options() { $options = $this->get_options(); $this->assertEmpty( $options ); } @@ -90,7 +90,7 @@ function test_no_options() { /** * Test that exactly one analytics component is inserted into the DB */ - function test_one_option_inserted() { + public function test_one_option_inserted() { $this->insert_one_option( $this->vendor, $this->config_one @@ -103,7 +103,7 @@ function test_one_option_inserted() { /** * Test that two analytics components are inserted into the DB */ - function test_two_options_inserted() { + public function test_two_options_inserted() { /* Insert analytics option one */ $this->insert_one_option( @@ -124,7 +124,7 @@ function test_two_options_inserted() { /** * Test that the analytics JS is added to the page */ - function test_analytics_js_added() { + public function test_analytics_js_added() { /* Insert analytics option */ $this->insert_one_option( @@ -137,7 +137,7 @@ function test_analytics_js_added() { $libxml_previous_state = libxml_use_internal_errors( true ); // Create a new DOM document - $dom = new DOMDocument; + $dom = new DOMDocument(); // Load the rendered page into it $dom->loadHTML( $amp_rendered ); @@ -158,7 +158,7 @@ function test_analytics_js_added() { /** * Test that exactly one analytics component are added to the page */ - function test_one_analytics_component_added() { + public function test_one_analytics_component_added() { /* Insert analytics option */ $this->insert_one_option( @@ -171,7 +171,7 @@ function test_one_analytics_component_added() { $libxml_previous_state = libxml_use_internal_errors( true ); - $dom = new DOMDocument; + $dom = new DOMDocument(); $dom->loadHTML( $amp_rendered ); $components = $dom->getElementsByTagName( 'amp-analytics' ); @@ -186,7 +186,7 @@ function test_one_analytics_component_added() { /** * Test that two analytics components are added to the page */ - function test_two_analytics_components_added() { + public function test_two_analytics_components_added() { $this->insert_one_option( $this->vendor, @@ -202,7 +202,7 @@ function test_two_analytics_components_added() { $libxml_previous_state = libxml_use_internal_errors( true ); - $dom = new DOMDocument; + $dom = new DOMDocument(); $dom->loadHTML( $amp_rendered ); $components = $dom->getElementsByTagName( 'amp-analytics' ); // Two amp-analytics components should be in the page diff --git a/tests/test-amp-image-dimension-extractor.php b/tests/test-amp-image-dimension-extractor.php index f1010ba618d..6dc14c7a5d7 100644 --- a/tests/test-amp-image-dimension-extractor.php +++ b/tests/test-amp-image-dimension-extractor.php @@ -1,4 +1,9 @@ false, - '//example.com/no-protocol' => false, - '/absolute-url/no-host' => false, + $expected = array( + 'https://example.com' => false, + '//example.com/no-protocol' => false, + '/absolute-url/no-host' => false, 'data:image/gif;base64,R0lGODl...' => false, ); @@ -38,18 +56,21 @@ public function test__should_return_original_urls() { $this->assertEquals( $expected, $actual ); } -} -class AMP_Image_Dimension_Extractor__Normalize_URL__Test extends WP_UnitTestCase { - function get_data() { + /** + * Data strings for testing converter. + * + * @return array + */ + public function get_data() { $site_url = site_url(); return array( - 'empty_url' => array( + 'empty_url' => array( '', false, ), - 'data_url' => array( + 'data_url' => array( 'data:image/gif;base64,R0lGODl...', false, ), @@ -57,19 +78,19 @@ function get_data() { '//example.com/file.jpg', 'http://example.com/file.jpg', ), - 'path_only' => array( + 'path_only' => array( '/path/to/file.png', $site_url . '/path/to/file.png', ), - 'query_only' => array( + 'query_only' => array( '?file=file.png', $site_url . '/?file=file.png', ), - 'path_and_query' => array( + 'path_and_query' => array( '/path/file.jpg?query=1', $site_url . '/path/file.jpg?query=1', ), - 'normal_url' => array( + 'normal_url' => array( 'https://example.com/path/to/file.jpg', 'https://example.com/path/to/file.jpg', ), @@ -77,26 +98,32 @@ function get_data() { } /** + * Test normalizing a URL + * + * @param string $source_url Source. + * @param string $expected_url Expected result. + * * @dataProvider get_data */ - function test__normalize_url( $source_url, $expected_url ) { + public function test__normalize_url( $source_url, $expected_url ) { $result_url = AMP_Image_Dimension_Extractor::normalize_url( $source_url ); $this->assertEquals( $expected_url, $result_url ); } -} -// TODO: tests for transients, errors, lock -// TODO: mocked tests -class AMP_Image_Dimension_Extractor__By_Downloading__Test extends WP_UnitTestCase { - - function test__valid_image_file() { - $sources = array( - IMG_350 => false, + /** + * Test a valid image file. + * + * TODO: tests for transients, errors, lock + * TODO: mocked tests + */ + public function test__valid_image_file() { + $sources = array( + IMG_350 => false, ); $expected = array( - IMG_350 => array( - 'width' => 350, + IMG_350 => array( + 'width' => 350, 'height' => 150, ), ); @@ -106,13 +133,16 @@ function test__valid_image_file() { $this->assertEquals( $expected, $dimensions ); } - function test__valid_image_file_synchronous() { - $sources = array( + /** + * Test a valid image file synchronously. + */ + public function test__valid_image_file_synchronous() { + $sources = array( IMG_350 => false, ); $expected = array( IMG_350 => array( - 'width' => 350, + 'width' => 350, 'height' => 150, ), ); @@ -122,18 +152,21 @@ function test__valid_image_file_synchronous() { $this->assertEquals( $expected, $dimensions ); } - function test__multiple_valid_image_files() { - $sources = array( - IMG_350 => false, + /** + * Test multiple valid image files. + */ + public function test__multiple_valid_image_files() { + $sources = array( + IMG_350 => false, IMG_1024 => false, ); $expected = array( - IMG_350 => array( - 'width' => 350, + IMG_350 => array( + 'width' => 350, 'height' => 150, ), IMG_1024 => array( - 'width' => 1024, + 'width' => 1024, 'height' => 768, ), ); @@ -143,18 +176,21 @@ function test__multiple_valid_image_files() { $this->assertEquals( $expected, $dimensions ); } - function test__multiple_valid_image_files_synchronous() { - $sources = array( - IMG_350 => false, + /** + * Test multiple valid image files synchronously. + */ + public function test__multiple_valid_image_files_synchronous() { + $sources = array( + IMG_350 => false, IMG_1024 => false, ); $expected = array( - IMG_350 => array( - 'width' => 350, + IMG_350 => array( + 'width' => 350, 'height' => 150, ), IMG_1024 => array( - 'width' => 1024, + 'width' => 1024, 'height' => 768, ), ); @@ -164,8 +200,11 @@ function test__multiple_valid_image_files_synchronous() { $this->assertEquals( $expected, $dimensions ); } - function test__invalid_image_file() { - $sources = array( + /** + * Test an invalid image file. + */ + public function test__invalid_image_file() { + $sources = array( AMP_IMG_DIMENSION_TEST_INVALID_FILE => false, ); $expected = array( @@ -177,8 +216,11 @@ function test__invalid_image_file() { $this->assertEquals( $expected, $dimensions ); } - function test__invalid_image_file_synchronous() { - $sources = array( + /** + * Test an invalid image file synchronously. + */ + public function test__invalid_image_file_synchronous() { + $sources = array( AMP_IMG_DIMENSION_TEST_INVALID_FILE => false, ); $expected = array( @@ -190,20 +232,23 @@ function test__invalid_image_file_synchronous() { $this->assertEquals( $expected, $dimensions ); } - function test__mix_of_valid_and_invalid_image_file() { - $sources = array( - IMG_350 => false, + /** + * Test a mix of valid and invalid image files. + */ + public function test__mix_of_valid_and_invalid_image_file() { + $sources = array( + IMG_350 => false, AMP_IMG_DIMENSION_TEST_INVALID_FILE => false, - IMG_1024 => false, + IMG_1024 => false, ); $expected = array( - IMG_350 => array( - 'width' => 350, + IMG_350 => array( + 'width' => 350, 'height' => 150, ), AMP_IMG_DIMENSION_TEST_INVALID_FILE => false, - IMG_1024 => array( - 'width' => 1024, + IMG_1024 => array( + 'width' => 1024, 'height' => 768, ), ); @@ -213,20 +258,23 @@ function test__mix_of_valid_and_invalid_image_file() { $this->assertEquals( $expected, $dimensions ); } - function test__mix_of_valid_and_invalid_image_file_synchronous() { - $sources = array( - IMG_350 => false, + /** + * Test a mix of valid and invalid image files synchronously. + */ + public function test__mix_of_valid_and_invalid_image_file_synchronous() { + $sources = array( + IMG_350 => false, AMP_IMG_DIMENSION_TEST_INVALID_FILE => false, - IMG_1024 => false, + IMG_1024 => false, ); $expected = array( - IMG_350 => array( - 'width' => 350, + IMG_350 => array( + 'width' => 350, 'height' => 150, ), AMP_IMG_DIMENSION_TEST_INVALID_FILE => false, - IMG_1024 => array( - 'width' => 1024, + IMG_1024 => array( + 'width' => 1024, 'height' => 768, ), ); @@ -236,8 +284,13 @@ function test__mix_of_valid_and_invalid_image_file_synchronous() { $this->assertEquals( $expected, $dimensions ); } - function test__amp_wp_user_agent() { - $expected = 'amp-wp, v' . AMP__VERSION . ', '; + /** + * Test get_default_user_agent() + * + * @covers get_default_user_agent() + */ + public function test__amp_wp_user_agent() { + $expected = 'amp-wp, v' . AMP__VERSION . ', '; $user_agent = AMP_Image_Dimension_Extractor::get_default_user_agent( '' ); $user_agent = substr( $user_agent, 0, strlen( $expected ) ); diff --git a/tests/test-amp-wp-utils.php b/tests/test-amp-wp-utils.php index 43f3b3a572e..5ab5d1277ec 100644 --- a/tests/test-amp-wp-utils.php +++ b/tests/test-amp-wp-utils.php @@ -1,25 +1,41 @@ array( + 'valid__no_component' => array( 'https://example.com/path', array( 'scheme' => 'https', - 'host' => 'example.com', - 'path' => '/path', + 'host' => 'example.com', + 'path' => '/path', ), -1, ), - 'valid__with_component' => array( + 'valid__with_component' => array( 'https://example.com/path', 'example.com', PHP_URL_HOST, ), - 'valid__schemaless__no_component' => array( + 'valid__schemaless__no_component' => array( '//example.com/path', array( 'host' => 'example.com', @@ -37,9 +53,15 @@ function get_test_data() { } /** + * Test URL parsing. + * + * @param string $url The URL. + * @param string $expected Expected value. + * @param string $component Derived value. + * * @dataProvider get_test_data */ - function test__method( $url, $expected, $component ) { + public function test__method( $url, $expected, $component ) { $actual = AMP_WP_Utils::parse_url( $url, $component ); $this->assertEquals( $expected, $actual );