Skip to content

Commit f472ae0

Browse files
committed
add bats tests
1 parent be63d0c commit f472ae0

File tree

3 files changed

+153
-5
lines changed

3 files changed

+153
-5
lines changed

.github/tests/2-rest-url-fix.bats

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ jobs:
7777
env:
7878
CI: 1
7979
run: |
80-
bats -p -t .github/tests
80+
bats -p -t .github/tests/1-test-update-php.bats
8181
8282
- name: Create failure status artifact
8383
if: failure()
Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: WordPress Composer Playwright Tests
1+
name: WordPress Composer Tests
22
on:
33
pull_request:
44
paths-ignore:
@@ -29,13 +29,16 @@ permissions:
2929

3030
jobs:
3131

32-
playwright-single:
32+
test-single:
3333
name: Single site
3434
runs-on: ubuntu-latest
3535

3636
steps:
3737
- uses: actions/checkout@v4
3838

39+
- name: Install Bats
40+
uses: bats-core/bats-action@2.0.0
41+
3942
- name: Wait for status artifacts
4043
env:
4144
GH_TOKEN: ${{ github.token }}
@@ -154,18 +157,31 @@ jobs:
154157
SITE_URL: ${{ env.SITE_URL }}
155158
run: npm run test .github/tests/wpcm.spec.ts
156159

160+
- name: Run Bats tests for URL fixes (Single Site)
161+
env:
162+
SITE_ID: wpcm-playwright-tests
163+
TERMINUS_TOKEN: ${{ secrets.TERMINUS_TOKEN }}
164+
run: |
165+
echo "Running REST URL Bats tests..."
166+
terminus auth:login --machine-token="${TERMINUS_TOKEN}" || echo "Terminus already logged in."
167+
# Corrected path to the test file
168+
bats -p -t .github/tests/rest-url-fix.bats
169+
157170
- name: Delete Site
158171
if: success()
159172
shell: bash
160173
run: terminus site:delete wpcm-playwright-tests -y
161174

162-
playwright-subdir:
175+
test-subdir:
163176
name: Subdirectory multisite
164177
runs-on: ubuntu-latest
165178

166179
steps:
167180
- uses: actions/checkout@v4
168181

182+
- name: Install Bats
183+
uses: bats-core/bats-action@2.0.0
184+
169185
- name: Wait for status artifacts
170186
env:
171187
GH_TOKEN: ${{ github.token }}
@@ -287,18 +303,35 @@ jobs:
287303
echo "Running Playwright tests on WordPress subdirectory subsite"
288304
npm run test .github/tests/wpcm.spec.ts
289305
306+
- name: Run Bats tests for URL fixes
307+
# This step runs *after* the site setup, including the initial permalink flush.
308+
# The Bats test itself handles permalink changes for specific test cases.
309+
# We need to pass the SITE_ID to the Bats test environment so WP-CLI commands work via Terminus.
310+
env:
311+
SITE_ID: wpcm-subdir-playwright-tests
312+
SUBSITE: foo
313+
TERMINUS_TOKEN: ${{ secrets.TERMINUS_TOKEN }}
314+
run: |
315+
echo "Running REST URL Bats tests..."
316+
# Ensure Terminus is logged in if needed by Bats WP-CLI calls
317+
terminus auth:login --machine-token="${TERMINUS_TOKEN}" || echo "Terminus already logged in."
318+
bats -p -t .github/tests/2-rest-url-fix.bats
319+
290320
- name: Delete Site
291321
if: success()
292322
shell: bash
293323
run: terminus site:delete wpcm-subdir-playwright-tests -y
294324

295-
playwright-subdom:
325+
test-subdom:
296326
name: Subdomain multisite
297327
runs-on: ubuntu-latest
298328

299329
steps:
300330
- uses: actions/checkout@v4
301331

332+
- name: Install Bats
333+
uses: bats-core/bats-action@2.0.0
334+
302335
- name: Wait for status artifacts
303336
env:
304337
GH_TOKEN: ${{ github.token }}
@@ -464,3 +497,20 @@ jobs:
464497
SITE_URL: ${{ env.SUBDOMAIN_URL }}
465498
GRAPHQL_ENDPOINT: ${{ env.SUBDOMAIN_URL }}/wp/graphql
466499
run: npm run test .github/tests/wpcm.spec.ts
500+
501+
- name: Run Bats tests for URL fixes (Subdomain)
502+
env:
503+
SITE_ID: wpcm-subdom-playwright-tests
504+
SUBSITE: foo
505+
TERMINUS_TOKEN: ${{ secrets.TERMINUS_TOKEN }}
506+
run: |
507+
echo "Running REST URL Bats tests..."
508+
terminus auth:login --machine-token="${TERMINUS_TOKEN}" || echo "Terminus already logged in."
509+
# Corrected path to the test file
510+
bats -p -t .github/tests/rest-url-fix.bats
511+
512+
- name: Delete Site
513+
# Run always to ensure cleanup
514+
if: always()
515+
shell: bash
516+
run: terminus site:delete wpcm-subdom-playwright-tests -y

0 commit comments

Comments
 (0)