From 305c28baa5fa486773df7aac7b42754aaba83aa3 Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Tue, 29 May 2018 22:50:35 -0500 Subject: [PATCH 01/85] Begin testing of site urls, starting with helper method. Define get_post_permalinks(). This gets permalinks for public posts, other than attachments. Another helper function can call AMP_Validation_Manager::validate_url() for each of these permalinks. --- includes/class-amp-autoloader.php | 1 + .../validation/class-amp-site-validation.php | 42 ++++++++++ tests/test-class-amp-site-validation.php | 79 +++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 includes/validation/class-amp-site-validation.php create mode 100644 tests/test-class-amp-site-validation.php diff --git a/includes/class-amp-autoloader.php b/includes/class-amp-autoloader.php index 1a11d9ba1d0..02dbe12eb8e 100644 --- a/includes/class-amp-autoloader.php +++ b/includes/class-amp-autoloader.php @@ -92,6 +92,7 @@ class AMP_Autoloader { 'AMP_Validation_Manager' => 'includes/validation/class-amp-validation-manager', 'AMP_Invalid_URL_Post_Type' => 'includes/validation/class-amp-invalid-url-post-type', 'AMP_Validation_Error_Taxonomy' => 'includes/validation/class-amp-validation-error-taxonomy', + 'AMP_Site_Validation' => 'includes/validation/class-amp-site-validation', 'AMP_String_Utils' => 'includes/utils/class-amp-string-utils', 'AMP_WP_Utils' => 'includes/utils/class-amp-wp-utils', 'AMP_Widget_Archives' => 'includes/widgets/class-amp-widget-archives', diff --git a/includes/validation/class-amp-site-validation.php b/includes/validation/class-amp-site-validation.php new file mode 100644 index 00000000000..594b94bc2f2 --- /dev/null +++ b/includes/validation/class-amp-site-validation.php @@ -0,0 +1,42 @@ + true ), 'names' ); + $query = new WP_Query( array( + 'posts_per_page' => $number_posts, + 'post_type' => array_values( $post_types ), + 'orderby' => 'ID', + 'order' => 'ASC', + 'post_status' => array( 'publish' ), + ) ); + + $permalinks = array(); + foreach ( $query->posts as $post ) { + $permalinks[] = amp_get_permalink( $post->ID ); + } + + return $permalinks; + } +} diff --git a/tests/test-class-amp-site-validation.php b/tests/test-class-amp-site-validation.php new file mode 100644 index 00000000000..a9d12b157f6 --- /dev/null +++ b/tests/test-class-amp-site-validation.php @@ -0,0 +1,79 @@ +node = $dom_document->createElement( self::TAG_NAME ); + AMP_Validation_Manager::reset_validation_results(); + } + + /** + * Test get_post_permalinks. + * + * @covers AMP_Site_Validation::get_post_permalinks() + */ + public function test_get_post_permalinks() { + $posts = array(); + $permalinks = array(); + $number_posts_each_post_type = 20; + $post_types = get_post_types( array( 'public' => true ), 'names' ); + + /** + * The tested method does not get attachment permalinks. + * It only searches for posts with the status 'publish.' + * Attachments have a default status of 'inherit,' to depend on the status of their parent post. + */ + unset( $post_types['attachment'] ); + foreach ( $post_types as $post_type ) { + for ( $i = 0; $i < $number_posts_each_post_type; $i++ ) { + $post = $this->factory()->post->create_and_get( array( + 'post_type' => $post_type, + 'post_status' => 'publish', + ) ); + $posts[] = $post; + $permalinks[] = amp_get_permalink( $post->ID ); + } + } + $number_of_posts = count( $post_types ) * $number_posts_each_post_type; + $this->assertEquals( $permalinks, AMP_Site_Validation::get_post_permalinks( $number_of_posts ) ); + } +} From 9e9df92533bd75b7ce01d581954aa3770b56ad70 Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Tue, 29 May 2018 23:49:24 -0500 Subject: [PATCH 02/85] Change helper method to output IDs, for use with existing method. validate_queued_posts_on_frontend() uses IDs, which are stored in $posts_pending_frontend_validation. So simply return IDs from the helper function. --- .../validation/class-amp-site-validation.php | 30 ++++++++++++------- tests/test-class-amp-site-validation.php | 21 +++++++------ 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/includes/validation/class-amp-site-validation.php b/includes/validation/class-amp-site-validation.php index 594b94bc2f2..676d4d10b55 100644 --- a/includes/validation/class-amp-site-validation.php +++ b/includes/validation/class-amp-site-validation.php @@ -13,16 +13,16 @@ class AMP_Site_Validation { /** - * Get the post permalinks to check for AMP validity. + * Gets the post IDs of all public post types with the status 'publish,' to check for AMP validity. * * This excludes 'attachment' post types, as it only searches for posts with the status 'publish.' * Attachments have a default status of 'inherit,' so they can depend on the status of their parent like a post. * - * @todo: consider whether this should also return 'attachment' permalinks. - * @param int|null $number_posts The maximum amount of posts to get the permalinks for. - * @return string[] $permalinks The permalinks, as an array of strings. + * @todo: consider whether this should also return 'attachment' IDs. + * @param int $number_posts The maximum amount of posts to get the IDs for (optional). + * @return int[] $post_ids The post IDs in an array. */ - public static function get_post_permalinks( $number_posts ) { + public static function get_post_ids( $number_posts = 200 ) { $post_types = get_post_types( array( 'public' => true ), 'names' ); $query = new WP_Query( array( 'posts_per_page' => $number_posts, @@ -32,11 +32,21 @@ public static function get_post_permalinks( $number_posts ) { 'post_status' => array( 'publish' ), ) ); - $permalinks = array(); - foreach ( $query->posts as $post ) { - $permalinks[] = amp_get_permalink( $post->ID ); - } + return wp_list_pluck( $query->posts, 'ID' ); + } - return $permalinks; + /** + * Validates the URLs on the site. + * + * @todo: Consider wrapping this function with another, as different use cases will probably require a different return value or display. + * For example, the