Skip to content

Commit

Permalink
Merge pull request #31 from wp-cli/fetch-maybe_make_link
Browse files Browse the repository at this point in the history
Don't always return clickable link; add 2 new options; misc other fixes.
  • Loading branch information
schlessera authored Jan 15, 2018
2 parents c953bfd + d598de5 commit eac79e0
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 43 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ matrix:
- php: 5.3
dist: precise
env: WP_VERSION=latest
- php: 7.0
env: WP_VERSION=4.5.3
- php: 7.0
env: WP_VERSION=4.3.1

before_install:
- |
Expand Down
250 changes: 233 additions & 17 deletions features/fetch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ Feature: Manage oEmbed cache.
Background:
Given a WP install

# Behavior that's the same for all WP versions.
Scenario: Get HTML embed code for a given URL
# Provider not requiring discovery
# Known provider not requiring discovery.
When I run `wp embed fetch https://www.youtube.com/watch?v=dQw4w9WgXcQ --width=500`
Then STDOUT should contain:
"""
Expand All @@ -15,7 +16,90 @@ Feature: Manage oEmbed cache.
dQw4w9WgXcQ
"""

# Provider requiring discovery
# Unknown provider (taken from https://oembed.com) requiring discovery but returning iframe so not sanitized.
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "try" to cater for these.
When I try `wp embed fetch http://LearningApps.org/259`
Then the return code should be 0
And STDERR should not contain:
"""
Error:
"""
And STDOUT should contain:
"""
LearningApps.org/
"""
And STDOUT should contain:
"""
<iframe
"""

# How unknown provider checked depends on WP version and post_id so recheck with post_id.
When I run `wp post create --post_title="Foo Bar" --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID}
When I try `wp embed fetch http://LearningApps.org/259 --post-id={POST_ID}`
Then the return code should be 0
And STDERR should not contain:
"""
Error:
"""
And STDOUT should contain:
"""
LearningApps.org/
"""
And STDOUT should contain:
"""
<iframe
"""

# Unknown provider requiring discovery but not returning iframe so would be sanitized for WP >= 4.4 without 'skip-sanitization' option.
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "try" to cater for these.
When I try `wp embed fetch https://asciinema.org/a/140798 --skip-sanitization`
Then the return code should be 0
And STDERR should not contain:
"""
Error:
"""
And STDOUT should contain:
"""
asciinema.org/
"""
And STDOUT should contain:
"""
<a
"""
And STDOUT should not contain:
"""
<iframe
"""

# WP 4.9 always returns clickable link even for sanitized oEmbed responses.
@require-wp-4.9
Scenario: Get HTML embed code for a given URL that requires discovery and is sanitized
When I run `wp embed fetch https://asciinema.org/a/140798`
Then STDOUT should contain:
"""
asciinema.org/
"""
And STDOUT should contain:
"""
<a
"""

# `wp_filter_oembed_result` filter introduced WP 4.4 which sanitizes oEmbed responses that don't include an iframe.
@less-than-wp-4.9 @require-wp-4.4
Scenario: Get HTML embed code for a given URL that requires discovery and is sanitized
When I try `wp embed fetch https://asciinema.org/a/140798`
Then the return code should be 1
And STDERR should be:
"""
Error: There was an error fetching the oEmbed data.
"""
And STDOUT should be empty

# No sanitization prior to WP 4.4.
@less-than-wp-4.4
Scenario: Get HTML embed code for a given URL that requires discovery and is sanitized
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "try" to cater for these.
When I try `wp embed fetch https://asciinema.org/a/140798`
Then the return code should be 0
Expand Down Expand Up @@ -43,6 +127,39 @@ Feature: Manage oEmbed cache.
{DEFAULT_STDOUT}
"""

# Raw requests are not sanitized.
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "try" to cater for these.
When I try `wp embed fetch https://asciinema.org/a/140798 --raw`
Then the return code should be 0
And STDERR should not contain:
"""
Error:
"""
And STDOUT should contain:
"""
asciinema.org
"""

Scenario: Fail then succeed when given unknown discoverable provider for a raw request, depending on discover option
When I try `wp embed fetch http://LearningApps.org/259 --raw --no-discover`
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discovery so use "contain" to ignore these.
Then STDERR should contain:
"""
Error: No oEmbed provider found for given URL. Maybe try discovery?
"""

# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "try" to cater for these.
When I try `wp embed fetch http://LearningApps.org/259 --raw`
Then the return code should be 0
And STDERR should not contain:
"""
Error:
"""
And STDOUT should contain:
"""
LearningApps.org
"""

Scenario: Bails when no oEmbed provider is found for a raw request
When I try `wp embed fetch https://foo.example.com --raw`
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discovery so use "contain" to ignore these.
Expand All @@ -51,33 +168,48 @@ Feature: Manage oEmbed cache.
Error: No oEmbed provider found for given URL.
"""

Scenario: Bails when no oEmbed provider is found for a raw request is found and discovery is off
Scenario: Bails when no oEmbed provider is found for a raw request and discovery is off
When I try `wp embed fetch https://foo.example.com --raw --discover=0`
Then STDERR should be:
"""
Error: No oEmbed provider found for given URL. Maybe try discovery?
"""

# WP 4.9 always returns clickable link.
@require-wp-4.9
Scenario: Makes unknown URLs clickable
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "try" to cater for these.
When I try `wp embed fetch https://foo.example.com`
Then the return code should be 0
And STDERR should not contain:
When I run `wp embed fetch https://foo.example.com`
Then STDOUT should contain:
"""
Error:
<a href="https://foo.example.com">https://foo.example.com</a>
"""
And STDOUT should contain:

# WP prior to 4.9 does not return clickable link.
@less-than-wp-4.9
Scenario: Doesn't make unknown URLs clickable
When I try `wp embed fetch https://foo.example.com`
Then the return code should be 1
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "contain" to cater for these.
And STDERR should contain:
"""
<a href="https://foo.example.com">https://foo.example.com</a>
Error: There was an error fetching the oEmbed data.
"""
And STDOUT should be empty

Scenario: Caches oEmbed response data for a given post
When I run `wp post create --post_title="Foo Bar" --porcelain`
# Note need post author for 'unfiltered_html' check to work for WP < 4.4.
When I run `wp post create --post_title="Foo Bar" --post_author=1 --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID}

When I run `wp embed fetch https://foo.example.com --post-id={POST_ID}`
Then STDOUT should contain:
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "try" to cater for these.
When I try `wp embed fetch https://foo.example.com --post-id={POST_ID}`
Then the return code should be 0
And STDERR should not contain:
"""
Error:
"""
And STDOUT should contain:
"""
<a href="https://foo.example.com">https://foo.example.com</a>
"""
Expand All @@ -98,16 +230,16 @@ Feature: Manage oEmbed cache.
# Depends on `oembed_remote_get_args` filter introduced WP 4.0 https://core.trac.wordpress.org/ticket/23442
@require-wp-4.0
Scenario: Get embed code for a URL with limited response size
# Need post_id for caching to work for WP < 4.9
When I run `wp post create --post_title="Foo Bar" --porcelain`
# Need post_id for caching to work for WP < 4.9, and also post_author for caching to work for WP < 4.4 (due to 'unfiltered_html' check).
When I run `wp post create --post_title="Foo Bar" --post_author=1 --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID}

When I run `wp embed fetch https://www.youtube.com/watch?v=dQw4w9WgXcQ --post-id={POST_ID}`
And save STDOUT as {DEFAULT_STDOUT}
Then STDOUT should contain:
"""
iframe
<iframe
"""

# Response limit too small but cached so ignored.
Expand Down Expand Up @@ -149,7 +281,7 @@ Feature: Manage oEmbed cache.
And save STDOUT as {DEFAULT_STDOUT}
Then STDOUT should contain:
"""
iframe
<iframe
"""

# Response limit too small but cached so ignored.
Expand Down Expand Up @@ -199,6 +331,90 @@ Feature: Manage oEmbed cache.
Hello world!
"""

# `wp_embed_handler_youtube` handler introduced WP 4.0.
@require-wp-4.0
Scenario: Invoke built-in YouTube handler
When I run `wp post create --post_title="Foo Bar" --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID}

When I run `wp embed fetch http://www.youtube.com/embed/dQw4w9WgXcQ --post-id={POST_ID}`
Then STDOUT should contain:
"""
youtube
"""
And STDOUT should contain:
"""
<iframe
"""

Scenario: Invoke built-in audio handler
When I run `wp post create --post_title="Foo Bar" --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID}

When I run `wp embed fetch http://www.example.com/never-gonna-give-you-up.mp3 --post-id={POST_ID}`
Then STDOUT should contain:
"""
example.com
"""
And STDOUT should contain:
"""
[audio
"""

When I run `wp embed fetch http://www.example.com/never-gonna-give-you-up.mp3 --post-id={POST_ID} --do-shortcode`
Then STDOUT should contain:
"""
example.com
"""
And STDOUT should contain:
"""
<audio
"""

Scenario: Invoke built-in video handler
When I run `wp post create --post_title="Foo Bar" --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID}

When I run `wp embed fetch http://www.example.com/never-gonna-give-you-up.mp4 --post-id={POST_ID}`
Then STDOUT should contain:
"""
example.com
"""
And STDOUT should contain:
"""
[video
"""

When I run `wp embed fetch http://www.example.com/never-gonna-give-you-up.mp4 --post-id={POST_ID} --do-shortcode`
Then STDOUT should contain:
"""
example.com
"""
And STDOUT should contain:
"""
<video
"""

# `wp_embed_handler_googlevideo` handler deprecated WP 4.6.
@less-than-wp-4.6
Scenario: Invoke built-in Google Video handler
When I run `wp post create --post_title="Foo Bar" --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID}

When I run `wp embed fetch http://video.google.com/videoplay?docid=123456789 --post-id={POST_ID}`
Then STDOUT should contain:
"""
video.google.com
"""
And STDOUT should contain:
"""
<embed
"""

Scenario: Incompatible options
When I try `wp embed fetch https://www.example.com/watch?v=dQw4w9WgXcQ --no-discover --limit-response-size=50000`
Then the return code should be 1
Expand Down
Loading

0 comments on commit eac79e0

Please sign in to comment.