|
| 1 | +#!/usr/bin/env bats |
| 2 | + |
| 3 | +# wp wrapper function |
| 4 | +_wp() { |
| 5 | + terminus wp -- ${SITE_ID}.dev "$@" |
| 6 | +} |
| 7 | + |
| 8 | +# Helper function to get REST URL via WP-CLI |
| 9 | +get_rest_url() { |
| 10 | + _wp eval 'echo get_rest_url();' |
| 11 | +} |
| 12 | + |
| 13 | +# Helper function to get home_url path via WP-CLI |
| 14 | +get_home_url_path() { |
| 15 | + _wp eval 'echo rtrim(parse_url(home_url(), PHP_URL_PATH) ?: "", "/");' |
| 16 | +} |
| 17 | + |
| 18 | +setup_suite() { |
| 19 | + # Ensure WP is installed and we are in the right directory |
| 20 | + _wp core is-installed || (echo "WordPress not installed. Run setup script first." && exit 1) |
| 21 | +} |
| 22 | + |
| 23 | +@test "Check REST URL with default (pretty) permalinks (after setup script flush)" { |
| 24 | + run get_rest_url |
| 25 | + assert_success |
| 26 | + # Default setup script sets /%postname%/ and flushes. |
| 27 | + # Expecting /wp/wp-json/ because home_url path should be /wp |
| 28 | + assert_output --partial "/wp/wp-json/" |
| 29 | +} |
| 30 | + |
| 31 | +@test "Check REST URL with plain permalinks" { |
| 32 | + # Set plain permalinks and flush |
| 33 | + _wp option update permalink_structure '' --quiet |
| 34 | + _wp rewrite flush --hard --quiet |
| 35 | + run get_rest_url |
| 36 | + assert_success |
| 37 | + # With plain permalinks, expect ?rest_route= based on home_url |
| 38 | + # Check if it contains the problematic /wp-json/wp/ segment (it shouldn't) |
| 39 | + refute_output --partial "/wp-json/wp/" |
| 40 | + # Check if it contains the expected ?rest_route= |
| 41 | + assert_output --partial "?rest_route=/" |
| 42 | + |
| 43 | + # Restore pretty permalinks for subsequent tests |
| 44 | + _wp option update permalink_structure '/%postname%/' --quiet |
| 45 | + _wp rewrite flush --hard --quiet |
| 46 | +} |
| 47 | + |
| 48 | +@test "Check REST URL with pretty permalinks *before* flush (Simulates new site)" { |
| 49 | + # Set pretty permalinks *without* flushing |
| 50 | + _wp option update permalink_structure '/%postname%/' --quiet |
| 51 | + # DO NOT FLUSH HERE |
| 52 | + |
| 53 | + # Check home_url path to confirm /wp setup |
| 54 | + run get_home_url_path |
| 55 | + assert_success |
| 56 | + assert_output "/wp" |
| 57 | + |
| 58 | + # Now check get_rest_url() - this is where the original issue might occur |
| 59 | + run get_rest_url |
| 60 | + assert_success |
| 61 | + # Assert that the output *should* be the correct /wp/wp-json/ even before flush, |
| 62 | + # assuming the fix (either integrated or separate filter) is in place. |
| 63 | + # If the fix is NOT in place, this might output /wp-json/ and fail. |
| 64 | + # If the plain permalink fix was active, it might output /wp/wp-json/wp/ and fail. |
| 65 | + assert_output --partial "/wp/wp-json/" |
| 66 | + refute_output --partial "/wp-json/wp/" # Ensure the bad structure isn't present |
| 67 | + |
| 68 | + # Clean up: Flush permalinks |
| 69 | + _wp rewrite flush --hard --quiet |
| 70 | +} |
| 71 | + |
| 72 | +@test "Access pretty REST API path directly with plain permalinks active" { |
| 73 | + # Set plain permalinks and flush |
| 74 | + _wp option update permalink_structure '' --quiet |
| 75 | + _wp rewrite flush --hard --quiet |
| 76 | + |
| 77 | + # Get the full home URL to construct the test URL |
| 78 | + SITE_URL=$( _wp option get home ) |
| 79 | + # Construct the pretty-style REST API URL |
| 80 | + # Note: home_url() includes /wp, so we append /wp-json/... directly |
| 81 | + TEST_URL="${SITE_URL}/wp-json/wp/v2/posts" |
| 82 | + |
| 83 | + # Make a curl request to the pretty URL |
| 84 | + # -s: silent, -o /dev/null: discard body, -w '%{http_code}': output only HTTP code |
| 85 | + # -L: follow redirects (we expect NO redirect, so this helps verify) |
| 86 | + # We expect a 200 OK if the internal handling works, or maybe 404 if not found, |
| 87 | + # but crucially NOT a 301/302 redirect. |
| 88 | + run curl -s -o /dev/null -w '%{http_code}' -L "${TEST_URL}" |
| 89 | + assert_success |
| 90 | + # Assert that the final HTTP status code is 200 (OK) |
| 91 | + # If it were redirecting, -L would follow, but the *initial* code wouldn't be 200. |
| 92 | + # If the internal handling fails, it might be 404 or other error. |
| 93 | + assert_output "200" |
| 94 | + |
| 95 | + # Restore pretty permalinks for subsequent tests |
| 96 | + _wp option update permalink_structure '/%postname%/' --quiet |
| 97 | + _wp rewrite flush --hard --quiet |
| 98 | +} |
0 commit comments