|
| 1 | +Feature: Replace WordPress attachment files |
| 2 | + |
| 3 | + Background: |
| 4 | + Given a WP install |
| 5 | + |
| 6 | + Scenario: Replace an attachment file with a local file |
| 7 | + Given download: |
| 8 | + | path | url | |
| 9 | + | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | |
| 10 | + | {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg | |
| 11 | + And I run `wp option update uploads_use_yearmonth_folders 0` |
| 12 | + |
| 13 | + When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain` |
| 14 | + Then save STDOUT as {ATTACHMENT_ID} |
| 15 | + |
| 16 | + When I run `wp media replace {ATTACHMENT_ID} {CACHE_DIR}/canola.jpg` |
| 17 | + Then STDOUT should contain: |
| 18 | + """ |
| 19 | + Replaced file for attachment ID {ATTACHMENT_ID} |
| 20 | + """ |
| 21 | + And STDOUT should contain: |
| 22 | + """ |
| 23 | + Success: Replaced 1 of 1 attachments. |
| 24 | + """ |
| 25 | + |
| 26 | + Scenario: Replace an attachment file from a URL |
| 27 | + Given download: |
| 28 | + | path | url | |
| 29 | + | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | |
| 30 | + And I run `wp option update uploads_use_yearmonth_folders 0` |
| 31 | + |
| 32 | + When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain` |
| 33 | + Then save STDOUT as {ATTACHMENT_ID} |
| 34 | + |
| 35 | + When I run `wp media replace {ATTACHMENT_ID} 'http://wp-cli.org/behat-data/canola.jpg'` |
| 36 | + Then STDOUT should contain: |
| 37 | + """ |
| 38 | + Replaced file for attachment ID {ATTACHMENT_ID} |
| 39 | + """ |
| 40 | + And STDOUT should contain: |
| 41 | + """ |
| 42 | + Success: Replaced 1 of 1 attachments. |
| 43 | + """ |
| 44 | + |
| 45 | + Scenario: Replace an attachment file and output only the attachment ID in porcelain mode |
| 46 | + Given download: |
| 47 | + | path | url | |
| 48 | + | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | |
| 49 | + | {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg | |
| 50 | + And I run `wp option update uploads_use_yearmonth_folders 0` |
| 51 | + |
| 52 | + When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain` |
| 53 | + Then save STDOUT as {ATTACHMENT_ID} |
| 54 | + |
| 55 | + When I run `wp media replace {ATTACHMENT_ID} {CACHE_DIR}/canola.jpg --porcelain` |
| 56 | + Then STDOUT should be: |
| 57 | + """ |
| 58 | + {ATTACHMENT_ID} |
| 59 | + """ |
| 60 | + |
| 61 | + Scenario: Preserve attachment metadata after replacing the file |
| 62 | + Given download: |
| 63 | + | path | url | |
| 64 | + | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | |
| 65 | + | {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg | |
| 66 | + And I run `wp option update uploads_use_yearmonth_folders 0` |
| 67 | + |
| 68 | + When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My Image Title" --porcelain` |
| 69 | + Then save STDOUT as {ATTACHMENT_ID} |
| 70 | + |
| 71 | + When I run `wp media replace {ATTACHMENT_ID} {CACHE_DIR}/canola.jpg` |
| 72 | + Then STDOUT should contain: |
| 73 | + """ |
| 74 | + Success: Replaced 1 of 1 attachments. |
| 75 | + """ |
| 76 | + |
| 77 | + When I run `wp post get {ATTACHMENT_ID} --field=post_title` |
| 78 | + Then STDOUT should be: |
| 79 | + """ |
| 80 | + My Image Title |
| 81 | + """ |
| 82 | + |
| 83 | + Scenario: Error when replacing with a non-existent local file |
| 84 | + Given download: |
| 85 | + | path | url | |
| 86 | + | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | |
| 87 | + |
| 88 | + When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain` |
| 89 | + Then save STDOUT as {ATTACHMENT_ID} |
| 90 | + |
| 91 | + When I try `wp media replace {ATTACHMENT_ID} /tmp/nonexistent-file.jpg` |
| 92 | + Then STDERR should contain: |
| 93 | + """ |
| 94 | + Error: Unable to replace attachment |
| 95 | + """ |
| 96 | + And STDERR should contain: |
| 97 | + """ |
| 98 | + File doesn't exist. |
| 99 | + """ |
| 100 | + And the return code should be 1 |
| 101 | + |
| 102 | + Scenario: Error when replacing with an invalid attachment ID |
| 103 | + When I try `wp media replace 999999 /tmp/fake.jpg` |
| 104 | + Then STDERR should contain: |
| 105 | + """ |
| 106 | + Error: Invalid attachment ID 999999. |
| 107 | + """ |
| 108 | + And the return code should be 1 |
| 109 | + |
| 110 | + Scenario: Skip deletion of old thumbnails when --skip-delete flag is used |
| 111 | + Given download: |
| 112 | + | path | url | |
| 113 | + | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | |
| 114 | + | {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg | |
| 115 | + And I run `wp option update uploads_use_yearmonth_folders 0` |
| 116 | + |
| 117 | + When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain` |
| 118 | + Then save STDOUT as {ATTACHMENT_ID} |
| 119 | + |
| 120 | + When I run `wp post meta get {ATTACHMENT_ID} _wp_attached_file` |
| 121 | + Then save STDOUT as {OLD_FILE} |
| 122 | + |
| 123 | + When I run `wp media replace {ATTACHMENT_ID} {CACHE_DIR}/canola.jpg --skip-delete` |
| 124 | + Then STDOUT should contain: |
| 125 | + """ |
| 126 | + Success: Replaced 1 of 1 attachments. |
| 127 | + """ |
| 128 | + |
| 129 | + And the wp-content/uploads/{OLD_FILE} file should exist |
0 commit comments