Skip to content

Commit 98102a5

Browse files
authored
Adapt to changes in WP 5.3 image handling to fix broken tests (#115)
2 parents 29050e1 + ba9f358 commit 98102a5

5 files changed

+128
-16
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
},
1717
"require-dev": {
1818
"wp-cli/entity-command": "^1.3 || ^2",
19+
"wp-cli/extension-command": "^2.0",
1920
"wp-cli/wp-cli-tests": "^2.1"
2021
},
2122
"config": {

features/media-fix-orientation.feature

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Feature: Fix WordPress attachments orientation
1111
Error: No images found.
1212
"""
1313

14-
@require-extension-exif @require-wp-4.0
14+
@require-extension-exif @require-wp-4.0 @less-than-wp-5.3
1515
Scenario: Fix orientation for all images
1616
Given download:
1717
| path | url |
@@ -94,7 +94,7 @@ Feature: Fix WordPress attachments orientation
9494
Success: Images already fixed.
9595
"""
9696

97-
@require-extension-exif @require-wp-4.0
97+
@require-extension-exif @require-wp-4.0 @less-than-wp-5.3
9898
Scenario: Fix orientation for single image
9999
When I run `wp media import {CACHE_DIR}/portrait-6.jpg --title="Portrait Six" --porcelain`
100100
Then save STDOUT as {PORTRAIT_SIX}

features/media-image-size.feature

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
11
Feature: List image sizes
22

3-
@require-wp-4.8
3+
@require-wp-5.3
44
Scenario: Basic usage
55
Given a WP install
6+
# Differing themes can have differing default image sizes. Let's stick to one.
7+
And I try `wp theme install twentynineteen --activate`
8+
9+
When I run `wp media image-size`
10+
Then STDOUT should be a table containing rows:
11+
| name | width | height | crop | ratio |
12+
| full | | | N/A | N/A |
13+
| 2048x2048 | 2048 | 2048 | soft | N/A |
14+
| post-thumbnail | 1568 | 9999 | soft | N/A |
15+
| large | 1024 | 1024 | soft | N/A |
16+
| medium_large | 768 | 0 | soft | N/A |
17+
| medium | 300 | 300 | soft | N/A |
18+
| thumbnail | 150 | 150 | hard | 1:1 |
19+
And STDERR should be empty
20+
21+
When I run `wp media image-size --skip-themes`
22+
Then STDOUT should be a table containing rows:
23+
| name | width | height | crop | ratio |
24+
| full | | | N/A | N/A |
25+
| large | 1024 | 1024 | soft | N/A |
26+
| medium_large | 768 | 0 | soft | N/A |
27+
| medium | 300 | 300 | soft | N/A |
28+
| thumbnail | 150 | 150 | hard | 1:1 |
29+
And STDERR should be empty
30+
31+
# Behavior changed with WordPress 5.3+, so we're adding separate tests for previous versions.
32+
# Change that impacts this:
33+
# https://core.trac.wordpress.org/ticket/43524
34+
@require-wp-4.8 @less-than-wp-5.3
35+
Scenario: Basic usage (pre-WP-5.3)
36+
Given a WP install
37+
# Differing themes can have differing default image sizes. Let's stick to one.
38+
And I try `wp theme install twentynineteen --activate`
639

740
When I run `wp media image-size`
841
Then STDOUT should be a table containing rows:

features/media-regenerate.feature

+67-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Feature: Regenerate WordPress attachments
22

33
Background:
44
Given a WP install
5+
And I try `wp theme install twentynineteen --activate`
56

67
Scenario: Regenerate all images while none exists
78
When I try `wp media regenerate --yes`
@@ -11,19 +12,23 @@ Feature: Regenerate WordPress attachments
1112
"""
1213
And the return code should be 0
1314

15+
@require-wp-5.3
1416
Scenario: Regenerate all images default behavior
1517
Given download:
1618
| path | url |
1719
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
1820
| {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg |
21+
| {CACHE_DIR}/white-150-square.jpg | http://wp-cli.org/behat-data/white-150-square.jpg |
1922
And I run `wp option update uploads_use_yearmonth_folders 0`
2023

2124
When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My imported large attachment" --porcelain`
2225
Then save STDOUT as {LARGE_ATTACHMENT_ID}
2326
And the wp-content/uploads/large-image.jpg file should exist
27+
And the wp-content/uploads/large-image-2560.jpg file should exist
2428
And the wp-content/uploads/large-image-150x150.jpg file should exist
2529
And the wp-content/uploads/large-image-300x225.jpg file should exist
2630
And the wp-content/uploads/large-image-1024x768.jpg file should exist
31+
And the wp-content/uploads/large-image-2048x1536.jpg file should exist
2732

2833
When I run `wp media import {CACHE_DIR}/canola.jpg --title="My imported medium attachment" --porcelain`
2934
Then save STDOUT as {MEDIUM_ATTACHMENT_ID}
@@ -32,41 +37,76 @@ Feature: Regenerate WordPress attachments
3237
And the wp-content/uploads/canola-300x225.jpg file should exist
3338
And the wp-content/uploads/canola-1024x768.jpg file should not exist
3439

40+
When I run `wp media import {CACHE_DIR}/white-150-square.jpg --title="My imported small attachment" --porcelain`
41+
Then save STDOUT as {SMALL_ATTACHMENT_ID}
42+
And the wp-content/uploads/white-150-square.jpg file should exist
43+
And the wp-content/uploads/white-150-square-150x150.jpg file should not exist
44+
And the wp-content/uploads/white-150-square-300x300.jpg file should not exist
45+
And the wp-content/uploads/white-150-square-1024x1024.jpg file should not exist
46+
3547
When I run `wp media regenerate --yes`
3648
Then STDOUT should contain:
3749
"""
38-
Found 2 images to regenerate.
50+
Found 3 images to regenerate.
3951
"""
4052
And STDOUT should contain:
4153
"""
42-
/2 Regenerated thumbnails for "My imported large attachment" (ID {LARGE_ATTACHMENT_ID})
54+
/3 Regenerated thumbnails for "My imported large attachment" (ID {LARGE_ATTACHMENT_ID})
4355
"""
4456
And STDOUT should contain:
4557
"""
46-
/2 Regenerated thumbnails for "My imported medium attachment" (ID {MEDIUM_ATTACHMENT_ID})
58+
/3 Regenerated thumbnails for "My imported medium attachment" (ID {MEDIUM_ATTACHMENT_ID})
4759
"""
4860
And STDOUT should contain:
4961
"""
50-
Success: Regenerated 2 of 2 images.
62+
/3 Regenerated thumbnails for "My imported small attachment" (ID {SMALL_ATTACHMENT_ID})
63+
"""
64+
And STDOUT should contain:
65+
"""
66+
Success: Regenerated 3 of 3 images.
5167
"""
5268
And the wp-content/uploads/large-image.jpg file should exist
69+
And the wp-content/uploads/large-image-2560.jpg file should exist
5370
And the wp-content/uploads/large-image-150x150.jpg file should exist
5471
And the wp-content/uploads/large-image-300x225.jpg file should exist
5572
And the wp-content/uploads/large-image-1024x768.jpg file should exist
73+
And the wp-content/uploads/large-image-2048x1536.jpg file should exist
5674
And the wp-content/uploads/canola.jpg file should exist
5775
And the wp-content/uploads/canola-150x150.jpg file should exist
5876
And the wp-content/uploads/canola-300x225.jpg file should exist
5977
And the wp-content/uploads/canola-1024x768.jpg file should not exist
78+
And the wp-content/uploads/white-150-square.jpg file should exist
79+
And the wp-content/uploads/white-150-square-150x150.jpg file should not exist
80+
And the wp-content/uploads/white-150-square-300x300.jpg file should not exist
81+
And the wp-content/uploads/white-150-square-1024x1024.jpg file should not exist
6082

61-
# WP < 4.2 produced thumbnails duplicating original, https://core.trac.wordpress.org/ticket/31296
62-
# WP 5.3 alpha contains a bug where duplicate resizes are being stored: https://core.trac.wordpress.org/ticket/32437
63-
@require-wp-4.2 @less-than-wp-5.3
64-
Scenario: Regenerate all images default behavior
83+
# Behavior changed with WordPress 5.3+, so we're adding separate tests for previous versions.
84+
# Changes that impact this:
85+
# https://core.trac.wordpress.org/ticket/43524
86+
# https://core.trac.wordpress.org/ticket/47873
87+
@less-than-wp-5.3
88+
Scenario: Regenerate all images default behavior (pre-WP-5.3)
6589
Given download:
6690
| path | url |
91+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
92+
| {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg |
6793
| {CACHE_DIR}/white-150-square.jpg | http://wp-cli.org/behat-data/white-150-square.jpg |
6894
And I run `wp option update uploads_use_yearmonth_folders 0`
6995

96+
When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My imported large attachment" --porcelain`
97+
Then save STDOUT as {LARGE_ATTACHMENT_ID}
98+
And the wp-content/uploads/large-image.jpg file should exist
99+
And the wp-content/uploads/large-image-150x150.jpg file should exist
100+
And the wp-content/uploads/large-image-300x225.jpg file should exist
101+
And the wp-content/uploads/large-image-1024x768.jpg file should exist
102+
103+
When I run `wp media import {CACHE_DIR}/canola.jpg --title="My imported medium attachment" --porcelain`
104+
Then save STDOUT as {MEDIUM_ATTACHMENT_ID}
105+
And the wp-content/uploads/canola.jpg file should exist
106+
And the wp-content/uploads/canola-150x150.jpg file should exist
107+
And the wp-content/uploads/canola-300x225.jpg file should exist
108+
And the wp-content/uploads/canola-1024x768.jpg file should not exist
109+
70110
When I run `wp media import {CACHE_DIR}/white-150-square.jpg --title="My imported small attachment" --porcelain`
71111
Then save STDOUT as {SMALL_ATTACHMENT_ID}
72112
And the wp-content/uploads/white-150-square.jpg file should exist
@@ -77,16 +117,32 @@ Feature: Regenerate WordPress attachments
77117
When I run `wp media regenerate --yes`
78118
Then STDOUT should contain:
79119
"""
80-
Found 1 image to regenerate.
120+
Found 3 images to regenerate.
81121
"""
82122
And STDOUT should contain:
83123
"""
84-
1/1 Regenerated thumbnails for "My imported small attachment" (ID {SMALL_ATTACHMENT_ID})
124+
/3 Regenerated thumbnails for "My imported large attachment" (ID {LARGE_ATTACHMENT_ID})
85125
"""
86126
And STDOUT should contain:
87127
"""
88-
Success: Regenerated 1 of 1 images.
128+
/3 Regenerated thumbnails for "My imported medium attachment" (ID {MEDIUM_ATTACHMENT_ID})
129+
"""
130+
And STDOUT should contain:
131+
"""
132+
/3 Regenerated thumbnails for "My imported small attachment" (ID {SMALL_ATTACHMENT_ID})
133+
"""
134+
And STDOUT should contain:
89135
"""
136+
Success: Regenerated 3 of 3 images.
137+
"""
138+
And the wp-content/uploads/large-image.jpg file should exist
139+
And the wp-content/uploads/large-image-150x150.jpg file should exist
140+
And the wp-content/uploads/large-image-300x225.jpg file should exist
141+
And the wp-content/uploads/large-image-1024x768.jpg file should exist
142+
And the wp-content/uploads/canola.jpg file should exist
143+
And the wp-content/uploads/canola-150x150.jpg file should exist
144+
And the wp-content/uploads/canola-300x225.jpg file should exist
145+
And the wp-content/uploads/canola-1024x768.jpg file should not exist
90146
And the wp-content/uploads/white-150-square.jpg file should exist
91147
And the wp-content/uploads/white-150-square-150x150.jpg file should not exist
92148
And the wp-content/uploads/white-150-square-300x300.jpg file should not exist

src/Media_Command.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ private function process_regeneration( $id, $skip_delete, $only_missing, $image_
565565
}
566566
$thumbnail_desc = $image_size ? sprintf( '"%s" thumbnail', $image_size ) : 'thumbnail';
567567

568-
$fullsizepath = get_attached_file( $id );
568+
$fullsizepath = $this->get_attached_file( $id );
569569

570570
if ( false === $fullsizepath || ! file_exists( $fullsizepath ) ) {
571571
WP_CLI::warning( "Can't find $att_desc." );
@@ -1059,7 +1059,7 @@ private function process_orientation_fix( $id, $progress, &$successes, &$errors,
10591059
$att_desc = sprintf( '"%1$s" (ID %2$d)', $title, $id );
10601060
}
10611061

1062-
$full_size_path = get_attached_file( $id );
1062+
$full_size_path = $this->get_attached_file( $id );
10631063

10641064
if ( false === $full_size_path || ! file_exists( $full_size_path ) ) {
10651065
WP_CLI::warning( "Can't find {$att_desc}." );
@@ -1183,4 +1183,26 @@ private function calculate_transformation( $orientation ) {
11831183
];
11841184
}
11851185

1186+
/**
1187+
* Add compatibility indirection to get_attached_file().
1188+
*
1189+
* In WordPress 5.3, behavior changed to account for automatic resizing of
1190+
* big image files.
1191+
*
1192+
* @see https://core.trac.wordpress.org/ticket/47873
1193+
*
1194+
* @param int $attachment_id ID of the attachment to get the filepath for.
1195+
* @return string|false Filepath of the attachment, or false if not found.
1196+
*/
1197+
private function get_attached_file( $attachment_id ) {
1198+
if ( function_exists( 'wp_get_original_image_path' ) ) {
1199+
$filepath = wp_get_original_image_path( $attachment_id );
1200+
1201+
if ( false !== $filepath ) {
1202+
return $filepath;
1203+
}
1204+
}
1205+
1206+
return get_attached_file( $attachment_id );
1207+
}
11861208
}

0 commit comments

Comments
 (0)