-
Notifications
You must be signed in to change notification settings - Fork 40
Add wp media replace subcommand
#240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
9
commits into
main
Choose a base branch
from
copilot/add-media-replace-subcommand
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+293
−1
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4a22e54
Initial plan
Copilot 249767e
Add media replace subcommand
Copilot ba8050b
Update src/Media_Command.php
swissspidy 018be36
Update src/Media_Command.php
swissspidy 4f6b949
Update src/Media_Command.php
swissspidy 886d4d2
Update src/Media_Command.php
swissspidy c10d8b1
Update src/Media_Command.php
swissspidy 8fc573a
Address PR review feedback: fix noun, temp file cleanup, error handli…
Copilot d79ce7b
Apply suggestion from @swissspidy
swissspidy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| Feature: Replace WordPress attachment files | ||
|
|
||
| Background: | ||
| Given a WP install | ||
|
|
||
| Scenario: Replace an attachment file with a local file | ||
| 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 | | ||
| And I run `wp option update uploads_use_yearmonth_folders 0` | ||
|
|
||
| When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain` | ||
| Then save STDOUT as {ATTACHMENT_ID} | ||
|
|
||
| When I run `wp media replace {ATTACHMENT_ID} {CACHE_DIR}/canola.jpg` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Replaced file for attachment ID {ATTACHMENT_ID} | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| Success: Replaced 1 of 1 attachments. | ||
| """ | ||
|
|
||
| Scenario: Replace an attachment file from a URL | ||
| Given download: | ||
| | path | url | | ||
| | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | | ||
| And I run `wp option update uploads_use_yearmonth_folders 0` | ||
|
|
||
| When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain` | ||
| Then save STDOUT as {ATTACHMENT_ID} | ||
|
|
||
| When I run `wp media replace {ATTACHMENT_ID} 'http://wp-cli.org/behat-data/canola.jpg'` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Replaced file for attachment ID {ATTACHMENT_ID} | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| Success: Replaced 1 of 1 attachments. | ||
| """ | ||
|
|
||
| Scenario: Replace an attachment file and output only the attachment ID in porcelain mode | ||
| 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 | | ||
| And I run `wp option update uploads_use_yearmonth_folders 0` | ||
|
|
||
| When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain` | ||
| Then save STDOUT as {ATTACHMENT_ID} | ||
|
|
||
| When I run `wp media replace {ATTACHMENT_ID} {CACHE_DIR}/canola.jpg --porcelain` | ||
| Then STDOUT should be: | ||
| """ | ||
| {ATTACHMENT_ID} | ||
| """ | ||
|
|
||
| Scenario: Preserve attachment metadata after replacing the file | ||
| 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 | | ||
| And I run `wp option update uploads_use_yearmonth_folders 0` | ||
|
|
||
| When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My Image Title" --porcelain` | ||
| Then save STDOUT as {ATTACHMENT_ID} | ||
|
|
||
| When I run `wp media replace {ATTACHMENT_ID} {CACHE_DIR}/canola.jpg` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: Replaced 1 of 1 attachments. | ||
| """ | ||
|
|
||
| When I run `wp post get {ATTACHMENT_ID} --field=post_title` | ||
| Then STDOUT should be: | ||
| """ | ||
| My Image Title | ||
| """ | ||
|
|
||
| Scenario: Error when replacing with a non-existent local file | ||
| Given download: | ||
| | path | url | | ||
| | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | | ||
|
|
||
| When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain` | ||
| Then save STDOUT as {ATTACHMENT_ID} | ||
|
|
||
| When I try `wp media replace {ATTACHMENT_ID} /tmp/nonexistent-file.jpg` | ||
| Then STDERR should contain: | ||
| """ | ||
| Error: Unable to replace attachment | ||
| """ | ||
| And STDERR should contain: | ||
| """ | ||
| File doesn't exist. | ||
| """ | ||
| And the return code should be 1 | ||
|
|
||
| Scenario: Error when replacing with an invalid attachment ID | ||
| When I try `wp media replace 999999 /tmp/fake.jpg` | ||
| Then STDERR should contain: | ||
| """ | ||
| Error: Invalid attachment ID 999999. | ||
| """ | ||
| And the return code should be 1 | ||
|
|
||
| Scenario: Skip deletion of old thumbnails when --skip-delete flag is used | ||
| 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 | | ||
| And I run `wp option update uploads_use_yearmonth_folders 0` | ||
|
|
||
| When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain` | ||
| Then save STDOUT as {ATTACHMENT_ID} | ||
|
|
||
| When I run `wp post meta get {ATTACHMENT_ID} _wp_attached_file` | ||
| Then save STDOUT as {OLD_FILE} | ||
|
|
||
| When I run `wp media replace {ATTACHMENT_ID} {CACHE_DIR}/canola.jpg --skip-delete` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: Replaced 1 of 1 attachments. | ||
| """ | ||
|
|
||
| And the wp-content/uploads/{OLD_FILE} file should exist |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.