From f5ed4963d67c8f311489c227372d60290e6965f7 Mon Sep 17 00:00:00 2001 From: gitlost Date: Mon, 2 Oct 2017 23:24:11 +0100 Subject: [PATCH 1/6] Skip non-thumbnailed PDFS & other images rather than fail. Check return values. --- features/media-regenerate.feature | 589 +++++++++++++++++++++++++++++- src/Media_Command.php | 175 +++++++-- 2 files changed, 723 insertions(+), 41 deletions(-) diff --git a/features/media-regenerate.feature b/features/media-regenerate.feature index a4d2b0abc..ca09b40d8 100644 --- a/features/media-regenerate.feature +++ b/features/media-regenerate.feature @@ -10,6 +10,69 @@ Feature: Regenerate WordPress attachments No images found. """ + Scenario: Regenerate all images default behavior + Given download: + | path | url | + | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | + | {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg | + | {CACHE_DIR}/white-150-square.jpg | http://wp-cli.org/behat-data/white-150-square.jpg | + And I run `wp option update uploads_use_yearmonth_folders 0` + + When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My imported large attachment" --porcelain` + Then save STDOUT as {LARGE_ATTACHMENT_ID} + And the wp-content/uploads/large-image.jpg file should exist + And the wp-content/uploads/large-image-150x150.jpg file should exist + And the wp-content/uploads/large-image-300x225.jpg file should exist + And the wp-content/uploads/large-image-1024x768.jpg file should exist + + When I run `wp media import {CACHE_DIR}/canola.jpg --title="My imported medium attachment" --porcelain` + Then save STDOUT as {MEDIUM_ATTACHMENT_ID} + And the wp-content/uploads/canola.jpg file should exist + And the wp-content/uploads/canola-150x150.jpg file should exist + And the wp-content/uploads/canola-300x225.jpg file should exist + And the wp-content/uploads/canola-1024x768.jpg file should not exist + + When I run `wp media import {CACHE_DIR}/white-150-square.jpg --title="My imported small attachment" --porcelain` + Then save STDOUT as {SMALL_ATTACHMENT_ID} + And the wp-content/uploads/white-150-square.jpg file should exist + And the wp-content/uploads/white-150-square-150x150.jpg file should not exist + And the wp-content/uploads/white-150-square-300x300.jpg file should not exist + And the wp-content/uploads/white-150-square-1024x1024.jpg file should not exist + + When I run `wp media regenerate --yes` + Then STDOUT should contain: + """ + Found 3 images to regenerate. + """ + And STDOUT should contain: + """ + /3 Regenerated thumbnails for "My imported large attachment" (ID {LARGE_ATTACHMENT_ID}) + """ + And STDOUT should contain: + """ + /3 Regenerated thumbnails for "My imported medium attachment" (ID {MEDIUM_ATTACHMENT_ID}) + """ + And STDOUT should contain: + """ + /3 Regenerated thumbnails for "My imported small attachment" (ID {SMALL_ATTACHMENT_ID}) + """ + And STDOUT should contain: + """ + Success: Regenerated 3 of 3 images. + """ + And the wp-content/uploads/large-image.jpg file should exist + And the wp-content/uploads/large-image-150x150.jpg file should exist + And the wp-content/uploads/large-image-300x225.jpg file should exist + And the wp-content/uploads/large-image-1024x768.jpg file should exist + And the wp-content/uploads/canola.jpg file should exist + And the wp-content/uploads/canola-150x150.jpg file should exist + And the wp-content/uploads/canola-300x225.jpg file should exist + And the wp-content/uploads/canola-1024x768.jpg file should not exist + And the wp-content/uploads/white-150-square.jpg file should exist + And the wp-content/uploads/white-150-square-150x150.jpg file should not exist + And the wp-content/uploads/white-150-square-300x300.jpg file should not exist + And the wp-content/uploads/white-150-square-1024x1024.jpg file should not exist + Scenario: Delete existing thumbnails when media is regenerated Given download: | path | url | @@ -194,7 +257,7 @@ Feature: Regenerate WordPress attachments Then STDERR should be: """ Warning: Can't find "My imported attachment" (ID {ATTACHMENT_ID}). - Error: No images regenerated. + Error: No images regenerated (1 failed). """ Scenario: Only regenerate images which are missing sizes @@ -780,3 +843,527 @@ Feature: Regenerate WordPress attachments """ Error: Unknown image size "test1". """ + + Scenario: Regenerating SVGs should be marked as skipped and not produce PHP notices + Given an svg.svg file: + """ + + """ + And a wp-content/mu-plugins/media-settings.php file: + """ + + """ + And a wp-content/mu-plugins/media-settings.php file: + """ + add_image_size_filters( $image_size ); } - $errored = false; - $successes = $errors = 0; + $successes = $errors = $skips = 0; foreach ( $images->posts as $number => $id ) { - if ( $this->process_regeneration( $id, $skip_delete, $only_missing, $image_size, ( $number + 1 ) . '/' . $count ) ) { - $successes++; - } else { - $errors++; - } + $this->process_regeneration( $id, $skip_delete, $only_missing, $image_size, ( $number + 1 ) . '/' . $count, $successes, $errors, $skips ); } if ( $image_size ) { $this->remove_image_size_filters( $image_size_filters ); } - Utils\report_batch_operation_results( 'image', 'regenerate', count( $images->posts ), $successes, $errors ); + self::report_batch_operation_results( 'image', 'regenerate', $count, $successes, $errors, $skips ); } /** @@ -477,15 +472,28 @@ private function make_copy( $path ) { return $filename; } - private function process_regeneration( $id, $skip_delete, $only_missing, $image_size, $progress ) { - $fullsizepath = get_attached_file( $id ); + private function process_regeneration( $id, $skip_delete, $only_missing, $image_size, $progress, &$successes, &$errors, &$skips ) { - $att_desc = sprintf( '"%1$s" (ID %2$d)', get_the_title( $id ), $id ); + $title = get_the_title( $id ); + if ( '' === $title ) { + // If audio or video cover art then the id is the sub attachment id, which has no title. + if ( metadata_exists( 'post', $id, '_cover_hash' ) ) { + // Unfortunately the only way to get the attachment title would be to do a non-indexed query against the meta value of `_thumbnail_id`. So don't. + $att_desc = sprintf( 'cover attachment (ID %d)', $id ); + } else { + $att_desc = sprintf( '"(no title)" (ID %d)', $id ); + } + } else { + $att_desc = sprintf( '"%1$s" (ID %2$d)', $title, $id ); + } $thumbnail_desc = $image_size ? sprintf( '"%s" thumbnail', $image_size ) : 'thumbnail'; + $fullsizepath = get_attached_file( $id ); + if ( false === $fullsizepath || !file_exists( $fullsizepath ) ) { WP_CLI::warning( "Can't find $att_desc." ); - return false; + $errors++; + return; } if ( ! $skip_delete ) { @@ -494,41 +502,55 @@ private function process_regeneration( $id, $skip_delete, $only_missing, $image_ $is_pdf = 'application/pdf' === get_post_mime_type( $id ); - if ( ! $only_missing || $this->needs_regeneration( $id, $fullsizepath, $is_pdf, $image_size ) ) { + $needs_regeneration = $this->needs_regeneration( $id, $fullsizepath, $is_pdf, $image_size, $skip_it ); - $metadata = wp_generate_attachment_metadata( $id, $fullsizepath ); - if ( is_wp_error( $metadata ) ) { - WP_CLI::warning( $metadata->get_error_message() ); - return false; - } + if ( $skip_it ) { + WP_CLI::log( "$progress Skipped $thumbnail_desc regeneration for $att_desc." ); + $skips++; + return; + } - // Note it's possible for no metadata to generated for PDFs if restricted to a specific image size. - if ( empty( $metadata ) && ! ( $is_pdf && $image_size ) ) { - WP_CLI::warning( "$progress Couldn't regenerate thumbnails for $att_desc." ); - return false; - } + if ( $only_missing && ! $needs_regeneration ) { + WP_CLI::log( "$progress No $thumbnail_desc regeneration needed for $att_desc." ); + $successes++; + return; + } - if ( $image_size ) { - if ( $this->update_attachment_metadata_for_image_size( $id, $metadata, $image_size ) ) { - WP_CLI::log( "$progress Regenerated $thumbnail_desc for $att_desc." ); - } else { - WP_CLI::log( "$progress No $thumbnail_desc regeneration needed for $att_desc." ); - } - } else { - wp_update_attachment_metadata( $id, $metadata ); + $metadata = wp_generate_attachment_metadata( $id, $fullsizepath ); + if ( is_wp_error( $metadata ) ) { + WP_CLI::warning( $metadata->get_error_message() ); + $errors++; + return; + } - WP_CLI::log( "$progress Regenerated thumbnails for $att_desc." ); + // Note it's possible for no metadata to generated for PDFs if restricted to a specific image size. + if ( empty( $metadata ) && ! ( $is_pdf && $image_size ) ) { + WP_CLI::warning( "$progress Couldn't regenerate thumbnails for $att_desc." ); + $errors++; + return; + } + + if ( $image_size ) { + if ( $this->update_attachment_metadata_for_image_size( $id, $metadata, $image_size ) ) { + WP_CLI::log( "$progress Regenerated $thumbnail_desc for $att_desc." ); + } else { + WP_CLI::log( "$progress No $thumbnail_desc regeneration needed for $att_desc." ); } - return true; } else { - WP_CLI::log( "$progress No $thumbnail_desc regeneration needed for $att_desc." ); - return true; + wp_update_attachment_metadata( $id, $metadata ); + + WP_CLI::log( "$progress Regenerated thumbnails for $att_desc." ); } + $successes++; } private function remove_old_images( $att_id, $fullsizepath, $image_size ) { $metadata = wp_get_attachment_metadata( $att_id ); + if ( ! is_array( $metadata ) ) { + return; + } + if ( empty( $metadata['sizes'] ) ) { return; } @@ -553,14 +575,49 @@ private function remove_old_images( $att_id, $fullsizepath, $image_size ) { } } - private function needs_regeneration( $att_id, $fullsizepath, $is_pdf, $image_size ) { + private function needs_regeneration( $att_id, $fullsizepath, $is_pdf, $image_size, &$skip_it ) { + + // Assume not skipping. + $skip_it = false; $metadata = wp_get_attachment_metadata($att_id); + if ( ! is_array( $metadata ) ) { + if ( $is_pdf ) { + $editor = wp_get_image_editor( $fullsizepath ); + $no_pdf_editor = is_wp_error( $editor ); + unset( $editor ); + if ( $no_pdf_editor ) { + // No PDF thumbnail generation available, so skip. + $skip_it = true; + return false; + } + // Assume it may be possible to regenerate the PDF thumbnails and allow processing to continue and possibly fail. + return true; + } + // Assume it's not a standard image (eg an SVG) and skip. + $skip_it = true; + return false; + } + // Note that an attachment can have no sizes if it's on or below the thumbnail threshold. // Check whether there's new sizes or they've changed. $image_sizes = $this->get_intermediate_image_sizes_for_attachment( $fullsizepath, $is_pdf, $metadata ); + if ( is_wp_error( $image_sizes ) ) { + if ( $is_pdf && 'image_no_editor' === $image_sizes->get_error_code() ) { + // No PDF thumbnail generation available, so skip. + $skip_it = true; + return false; + } + // Warn but assume it may be possible to regenerate and allow processing to continue and possibly fail. + WP_CLI::warning( $image_sizes->get_error_message() ); + return true; + } + if ( ! $image_sizes ) { + // This shouldn't really happen so assume that it may be possible to regenerate and allow processing to continue and possibly fail. + return true; + } if ( $image_size ) { if ( empty( $image_sizes[ $image_size ] ) ) { @@ -614,13 +671,11 @@ private function get_intermediate_image_sizes_for_attachment( $fullsizepath, $is // Need to get width, height of attachment for image_resize_dimensions(). $editor = wp_get_image_editor( $fullsizepath ); if ( is_wp_error( $editor ) ) { - WP_CLI::warning( $editor->get_error_message() ); - return array(); + return $editor; } if ( is_wp_error( $result = $editor->load() ) ) { - WP_CLI::warning( $result->get_error_message() ); unset( $editor ); - return array(); + return $result; } list( $width, $height ) = array_values( $editor->get_size() ); unset( $editor ); @@ -736,6 +791,10 @@ private function remove_image_size_filters( $image_size_filters ) { private function update_attachment_metadata_for_image_size( $id, $new_metadata, $image_size ) { $metadata = wp_get_attachment_metadata( $id ); + if ( ! is_array( $metadata ) ) { + return false; + } + // If have metadata for image_size. if ( ! empty( $new_metadata['sizes'][ $image_size ] ) ) { $metadata['sizes'][ $image_size ] = $new_metadata['sizes'][ $image_size ]; @@ -751,4 +810,40 @@ private function update_attachment_metadata_for_image_size( $id, $new_metadata, } return false; } + + /** + * Report the results of the same operation against multiple resources. + * + * @access public + * @category Input + * + * @param string $noun Resource being affected (e.g. plugin) + * @param string $verb Type of action happening to the noun (e.g. activate) + * @param integer $total Total number of resource being affected. + * @param integer $successes Number of successful operations. + * @param integer $failures Number of failures. + * @param integer $skips Number of skipped operations. + */ + private function report_batch_operation_results( $noun, $verb, $total, $successes, $failures, $skips = 0 ) { + $plural_noun = $noun . 's'; + $past_tense_verb = Utils\past_tense_verb( $verb ); + $past_tense_verb_upper = ucfirst( $past_tense_verb ); + if ( $failures ) { + $failed_skipped_message = " ({$failures} failed" . ( $skips ? ", {$skips} skipped" : '' ) . ')'; + if ( $successes ) { + WP_CLI::error( "Only {$past_tense_verb} {$successes} of {$total} {$plural_noun}{$failed_skipped_message}." ); + } else { + WP_CLI::error( "No {$plural_noun} {$past_tense_verb}{$failed_skipped_message}." ); + } + } else { + $skipped_message = $skips ? " ({$skips} skipped)" : ''; + if ( $successes || $skips ) { + WP_CLI::success( "{$past_tense_verb_upper} {$successes} of {$total} {$plural_noun}{$skipped_message}." ); + } else { + $message = $total > 1 ? ucfirst( $plural_noun ) : ucfirst( $noun ); + WP_CLI::success( "{$message} already {$past_tense_verb}." ); + } + } + } + } From 959e1c26cd265ff7a4589c8a3b2458fa9d7e5beb Mon Sep 17 00:00:00 2001 From: gitlost Date: Tue, 3 Oct 2017 02:55:57 +0100 Subject: [PATCH 2/6] Image sizes can be empty so don't skip. Tests compat. --- features/media-regenerate.feature | 102 +++++++++++++++++++++--------- src/Media_Command.php | 4 -- 2 files changed, 71 insertions(+), 35 deletions(-) diff --git a/features/media-regenerate.feature b/features/media-regenerate.feature index ca09b40d8..b84ac4c2b 100644 --- a/features/media-regenerate.feature +++ b/features/media-regenerate.feature @@ -15,7 +15,6 @@ Feature: Regenerate WordPress attachments | path | url | | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | | {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg | - | {CACHE_DIR}/white-150-square.jpg | http://wp-cli.org/behat-data/white-150-square.jpg | And I run `wp option update uploads_use_yearmonth_folders 0` When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My imported large attachment" --porcelain` @@ -32,33 +31,22 @@ Feature: Regenerate WordPress attachments And the wp-content/uploads/canola-300x225.jpg file should exist And the wp-content/uploads/canola-1024x768.jpg file should not exist - When I run `wp media import {CACHE_DIR}/white-150-square.jpg --title="My imported small attachment" --porcelain` - Then save STDOUT as {SMALL_ATTACHMENT_ID} - And the wp-content/uploads/white-150-square.jpg file should exist - And the wp-content/uploads/white-150-square-150x150.jpg file should not exist - And the wp-content/uploads/white-150-square-300x300.jpg file should not exist - And the wp-content/uploads/white-150-square-1024x1024.jpg file should not exist - When I run `wp media regenerate --yes` Then STDOUT should contain: """ - Found 3 images to regenerate. - """ - And STDOUT should contain: - """ - /3 Regenerated thumbnails for "My imported large attachment" (ID {LARGE_ATTACHMENT_ID}) + Found 2 images to regenerate. """ And STDOUT should contain: """ - /3 Regenerated thumbnails for "My imported medium attachment" (ID {MEDIUM_ATTACHMENT_ID}) + /2 Regenerated thumbnails for "My imported large attachment" (ID {LARGE_ATTACHMENT_ID}) """ And STDOUT should contain: """ - /3 Regenerated thumbnails for "My imported small attachment" (ID {SMALL_ATTACHMENT_ID}) + /2 Regenerated thumbnails for "My imported medium attachment" (ID {MEDIUM_ATTACHMENT_ID}) """ And STDOUT should contain: """ - Success: Regenerated 3 of 3 images. + Success: Regenerated 2 of 2 images. """ And the wp-content/uploads/large-image.jpg file should exist And the wp-content/uploads/large-image-150x150.jpg file should exist @@ -68,6 +56,35 @@ Feature: Regenerate WordPress attachments And the wp-content/uploads/canola-150x150.jpg file should exist And the wp-content/uploads/canola-300x225.jpg file should exist And the wp-content/uploads/canola-1024x768.jpg file should not exist + + # WP < 4.2 produced thumbnails duplicating original, https://core.trac.wordpress.org/ticket/31296 + @require-wp-4.2 + Scenario: Regenerate all images default behavior + Given download: + | path | url | + | {CACHE_DIR}/white-150-square.jpg | http://wp-cli.org/behat-data/white-150-square.jpg | + And I run `wp option update uploads_use_yearmonth_folders 0` + + When I run `wp media import {CACHE_DIR}/white-150-square.jpg --title="My imported small attachment" --porcelain` + Then save STDOUT as {SMALL_ATTACHMENT_ID} + And the wp-content/uploads/white-150-square.jpg file should exist + And the wp-content/uploads/white-150-square-150x150.jpg file should not exist + And the wp-content/uploads/white-150-square-300x300.jpg file should not exist + And the wp-content/uploads/white-150-square-1024x1024.jpg file should not exist + + When I run `wp media regenerate --yes` + Then STDOUT should contain: + """ + Found 1 image to regenerate. + """ + And STDOUT should contain: + """ + 1/1 Regenerated thumbnails for "My imported small attachment" (ID {SMALL_ATTACHMENT_ID}) + """ + And STDOUT should contain: + """ + Success: Regenerated 1 of 1 images. + """ And the wp-content/uploads/white-150-square.jpg file should exist And the wp-content/uploads/white-150-square-150x150.jpg file should not exist And the wp-content/uploads/white-150-square-300x300.jpg file should not exist @@ -499,19 +516,13 @@ Feature: Regenerate WordPress attachments And the wp-content/uploads/minimal-us-letter-pdf-1100x1100.jpg file should not exist And the wp-content/uploads/minimal-us-letter-pdf-1000x1000.jpg file should exist + # WP < 4.2 produced thumbnails duplicating original, https://core.trac.wordpress.org/ticket/31296 + @require-wp-4.2 Scenario: Only regenerate images that are missing if it has thumbnails Given download: | path | url | | {CACHE_DIR}/white-150-square.jpg | http://wp-cli.org/behat-data/white-150-square.jpg | And I run `wp option update uploads_use_yearmonth_folders 0` - And a wp-content/mu-plugins/media-settings.php file: - """ - get_error_message() ); return true; } - if ( ! $image_sizes ) { - // This shouldn't really happen so assume that it may be possible to regenerate and allow processing to continue and possibly fail. - return true; - } if ( $image_size ) { if ( empty( $image_sizes[ $image_size ] ) ) { From 09a25adc6ad736974b924d675bcf668eb0d7d99c Mon Sep 17 00:00:00 2001 From: gitlost Date: Tue, 3 Oct 2017 03:02:47 +0100 Subject: [PATCH 3/6] Test WP 3.9 & 4.2. --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index 923370a29..3de6cb673 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,6 +37,10 @@ matrix: - php: 5.3 dist: precise env: WP_VERSION=latest + - php: 5.6 + env: WP_VERSION=3.9 + - php: 5.6 + env: WP_VERSION=4.2 before_install: - | From dd98b8e70059cd72869a62d0ad8f5cb584031cbc Mon Sep 17 00:00:00 2001 From: gitlost Date: Tue, 3 Oct 2017 10:58:14 +0100 Subject: [PATCH 4/6] Fix and expand batch results melange test. --- features/media-regenerate.feature | 78 +++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 5 deletions(-) diff --git a/features/media-regenerate.feature b/features/media-regenerate.feature index b84ac4c2b..b9293e495 100644 --- a/features/media-regenerate.feature +++ b/features/media-regenerate.feature @@ -1256,7 +1256,7 @@ Feature: Regenerate WordPress attachments And STDERR should be empty @require-wp-4.7.3 @require-extension-imagick - Scenario: Regenerating melange: needing regeneration, not needing regeneration and skipped + Scenario: Regenerating melange with batch results: regenerated (and not needing regeneration), skipped, failed Given download: | path | url | | {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg | @@ -1355,17 +1355,43 @@ Feature: Regenerate WordPress attachments """ /4 Skipped thumbnail regeneration for "My imported SVG attachment" (ID {SVG_ATTACHMENT_ID}). """ - And STDOUT should not contain: + And STDOUT should contain: """ - /4 Regenerated thumbnails for "My imported PDF attachment" (ID {PDF_ATTACHMENT_ID}). + /4 Skipped thumbnail regeneration for "My imported PDF attachment" (ID {PDF_ATTACHMENT_ID}). """ And STDOUT should contain: """ /4 Regenerated thumbnails for cover attachment (ID {VIDEO_SUB_ATTACHMENT_ID}). """ - And STDERR should not contain: + And STDOUT should contain: + """ + Success: Regenerated 2 of 4 images (2 skipped). """ - Warning: + And STDERR should be empty + + # Regenerate canola only. + Given I run `rm wp-content/uploads/canola-150x150.jpg` + + When I run `WP_CLI_TEST_MEDIA_REGENERATE_PDF=0 wp media regenerate --yes --only-missing` + Then STDOUT should contain: + """ + Found 4 images to regenerate. + """ + And STDOUT should contain: + """ + /4 Regenerated thumbnails for "My imported JPG attachment" (ID {JPG_ATTACHMENT_ID}). + """ + And STDOUT should contain: + """ + /4 Skipped thumbnail regeneration for "My imported SVG attachment" (ID {SVG_ATTACHMENT_ID}). + """ + And STDOUT should contain: + """ + /4 Skipped thumbnail regeneration for "My imported PDF attachment" (ID {PDF_ATTACHMENT_ID}). + """ + And STDOUT should contain: + """ + /4 No thumbnail regeneration needed for cover attachment (ID {VIDEO_SUB_ATTACHMENT_ID}). """ And STDOUT should contain: """ @@ -1377,6 +1403,7 @@ Feature: Regenerate WordPress attachments Given a wp-content/uploads/canola.jpg file: """ """ + When I try `WP_CLI_TEST_MEDIA_REGENERATE_PDF=1 wp media regenerate --yes` Then STDOUT should contain: """ @@ -1407,3 +1434,44 @@ Feature: Regenerate WordPress attachments Error: Only regenerated 2 of 4 images (1 failed, 1 skipped). """ And the return code should be 1 + + # Make minimal pdf fail. + Given a wp-content/uploads/minimal-us-letter.pdf file: + """ + %PDF-1.1 + %¥±ë + + %%EOF + """ + And I run `rm wp-content/uploads/minimal-us-letter-pdf-116x150.jpg` + + When I try `WP_CLI_TEST_MEDIA_REGENERATE_PDF=1 wp media regenerate --yes --only-missing` + Then STDOUT should contain: + """ + Found 4 images to regenerate. + """ + And STDOUT should contain: + """ + /4 Skipped thumbnail regeneration for "My imported SVG attachment" (ID {SVG_ATTACHMENT_ID}). + """ + And STDOUT should contain: + """ + /4 No thumbnail regeneration needed for cover attachment (ID {VIDEO_SUB_ATTACHMENT_ID}). + """ + And STDERR should contain: + """ + /4 Couldn't regenerate thumbnails for "My imported JPG attachment" (ID {JPG_ATTACHMENT_ID}). + """ + And STDERR should contain: + """ + /4 Couldn't regenerate thumbnails for "My imported PDF attachment" (ID {PDF_ATTACHMENT_ID}). + """ + And STDERR should contain: + """ + Warning: + """ + And STDERR should contain: + """ + Error: Only regenerated 1 of 4 images (2 failed, 1 skipped). + """ + And the return code should be 1 From 237f7859e01516cb9e012a99e1cb9d2b92f16efc Mon Sep 17 00:00:00 2001 From: gitlost Date: Wed, 11 Oct 2017 22:20:23 +0100 Subject: [PATCH 5/6] Use Utils\report_batch_operation_results(); wp-cli.org/behat. --- features/media-regenerate.feature | 20 ++++++++-------- src/Media_Command.php | 39 ++----------------------------- 2 files changed, 12 insertions(+), 47 deletions(-) diff --git a/features/media-regenerate.feature b/features/media-regenerate.feature index b9293e495..785c852d1 100644 --- a/features/media-regenerate.feature +++ b/features/media-regenerate.feature @@ -1150,9 +1150,9 @@ Feature: Regenerate WordPress attachments @require-wp-3.9 Scenario: Regenerating audio with thumbnail Given download: - | path | url | - | {CACHE_DIR}/audio-with-400x300-cover.mp3 | http://gitlostbonger.com/behat-data/audio-with-400x300-cover.mp3 | - | {CACHE_DIR}/audio-with-no-cover.mp3 | http://gitlostbonger.com/behat-data/audio-with-no-cover.mp3 | + | path | url | + | {CACHE_DIR}/audio-with-400x300-cover.mp3 | http://wp-cli.org/behat-data/audio-with-400x300-cover.mp3 | + | {CACHE_DIR}/audio-with-no-cover.mp3 | http://wp-cli.org/behat-data/audio-with-no-cover.mp3 | And a wp-content/mu-plugins/media-settings.php file: """ diff --git a/src/Media_Command.php b/src/Media_Command.php index 196040253..15bff33c7 100644 --- a/src/Media_Command.php +++ b/src/Media_Command.php @@ -141,7 +141,7 @@ function regenerate( $args, $assoc_args = array() ) { $this->remove_image_size_filters( $image_size_filters ); } - self::report_batch_operation_results( 'image', 'regenerate', $count, $successes, $errors, $skips ); + Utils\report_batch_operation_results( 'image', 'regenerate', $count, $successes, $errors, $skips ); } /** @@ -523,7 +523,7 @@ private function process_regeneration( $id, $skip_delete, $only_missing, $image_ return; } - // Note it's possible for no metadata to generated for PDFs if restricted to a specific image size. + // Note it's possible for no metadata to be generated for PDFs if restricted to a specific image size. if ( empty( $metadata ) && ! ( $is_pdf && $image_size ) ) { WP_CLI::warning( "$progress Couldn't regenerate thumbnails for $att_desc." ); $errors++; @@ -807,39 +807,4 @@ private function update_attachment_metadata_for_image_size( $id, $new_metadata, return false; } - /** - * Report the results of the same operation against multiple resources. - * - * @access public - * @category Input - * - * @param string $noun Resource being affected (e.g. plugin) - * @param string $verb Type of action happening to the noun (e.g. activate) - * @param integer $total Total number of resource being affected. - * @param integer $successes Number of successful operations. - * @param integer $failures Number of failures. - * @param integer $skips Number of skipped operations. - */ - private function report_batch_operation_results( $noun, $verb, $total, $successes, $failures, $skips = 0 ) { - $plural_noun = $noun . 's'; - $past_tense_verb = Utils\past_tense_verb( $verb ); - $past_tense_verb_upper = ucfirst( $past_tense_verb ); - if ( $failures ) { - $failed_skipped_message = " ({$failures} failed" . ( $skips ? ", {$skips} skipped" : '' ) . ')'; - if ( $successes ) { - WP_CLI::error( "Only {$past_tense_verb} {$successes} of {$total} {$plural_noun}{$failed_skipped_message}." ); - } else { - WP_CLI::error( "No {$plural_noun} {$past_tense_verb}{$failed_skipped_message}." ); - } - } else { - $skipped_message = $skips ? " ({$skips} skipped)" : ''; - if ( $successes || $skips ) { - WP_CLI::success( "{$past_tense_verb_upper} {$successes} of {$total} {$plural_noun}{$skipped_message}." ); - } else { - $message = $total > 1 ? ucfirst( $plural_noun ) : ucfirst( $noun ); - WP_CLI::success( "{$message} already {$past_tense_verb}." ); - } - } - } - } From 727cd621b16eb92fd29098c90cc46cabcdd84d4e Mon Sep 17 00:00:00 2001 From: gitlost Date: Thu, 12 Oct 2017 01:03:29 +0100 Subject: [PATCH 6/6] Remove extra WP version jobs from travis and do in scenario outline instead. --- .travis.yml | 4 ---- features/media-regenerate.feature | 16 +++++++++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3de6cb673..923370a29 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,10 +37,6 @@ matrix: - php: 5.3 dist: precise env: WP_VERSION=latest - - php: 5.6 - env: WP_VERSION=3.9 - - php: 5.6 - env: WP_VERSION=4.2 before_install: - | diff --git a/features/media-regenerate.feature b/features/media-regenerate.feature index 785c852d1..4407e244e 100644 --- a/features/media-regenerate.feature +++ b/features/media-regenerate.feature @@ -1147,9 +1147,12 @@ Feature: Regenerate WordPress attachments And STDERR should be empty # Audio/video `_cover_hash` meta, used to determine if sub attachment, added in WP 3.9 - @require-wp-3.9 - Scenario: Regenerating audio with thumbnail - Given download: + # Test on PHP 5.6 latest only, and iterate over various WP versions. + @require-wp-latest @require-php-5.6 @less-than-php-7.0 + Scenario Outline: Regenerating audio with thumbnail + Given I run `wp core download --version= --force` + And I run `wp core update-db` + And download: | path | url | | {CACHE_DIR}/audio-with-400x300-cover.mp3 | http://wp-cli.org/behat-data/audio-with-400x300-cover.mp3 | | {CACHE_DIR}/audio-with-no-cover.mp3 | http://wp-cli.org/behat-data/audio-with-no-cover.mp3 | @@ -1202,6 +1205,13 @@ Feature: Regenerate WordPress attachments """ And STDERR should be empty + Examples: + | version | + | latest | + | trunk | + | 4.2 | + | 3.9 | + # Video cover support requires ID3 library 1.9.9, updated WP 4.3 https://core.trac.wordpress.org/ticket/32806 @require-wp-4.3 Scenario: Regenerating video with thumbnail