|
| 1 | +import { expect, test } from "@wordpress/e2e-test-utils-playwright"; |
| 2 | +import { HWP_SLUG, TEST_PREVIEW_URL } from "../constants"; |
| 3 | +import { |
| 4 | + createACFPostType, |
| 5 | + getSettingsField, |
| 6 | + goToPluginPage, |
| 7 | + installACF, |
| 8 | + resetPluginSettings, |
| 9 | + saveChanges, |
| 10 | + switchToTab, |
| 11 | + uninstallACF, |
| 12 | +} from "../utils"; |
| 13 | + |
| 14 | +test.describe("HWP Previews ACF Integration Test", () => { |
| 15 | + const acfPostTypes = [ |
| 16 | + { |
| 17 | + key: "book", |
| 18 | + pluralLabel: "Books", |
| 19 | + singularLabel: "Book", |
| 20 | + }, |
| 21 | + ]; |
| 22 | + |
| 23 | + test.beforeAll(async ({ requestUtils }) => { |
| 24 | + await requestUtils.resetPreferences(); |
| 25 | + }); |
| 26 | + |
| 27 | + test.beforeEach(async ({ admin, page, requestUtils }) => { |
| 28 | + await resetPluginSettings(admin); |
| 29 | + await requestUtils.activatePlugin(HWP_SLUG); |
| 30 | + await installACF(admin, page); |
| 31 | + }); |
| 32 | + |
| 33 | + test.afterEach(async ({ admin, page }) => { |
| 34 | + await uninstallACF(admin, page); |
| 35 | + }); |
| 36 | + |
| 37 | + test("ACF custom post types appear in HWP Previews settings", async ({ |
| 38 | + page, |
| 39 | + admin, |
| 40 | + }) => { |
| 41 | + // Create custom post types via ACF |
| 42 | + for (const postType of acfPostTypes) { |
| 43 | + await createACFPostType(admin, page, postType); |
| 44 | + } |
| 45 | + |
| 46 | + // Navigate to HWP Previews settings |
| 47 | + await goToPluginPage(admin); |
| 48 | + |
| 49 | + // Verify each ACF custom post type appears as a tab in settings |
| 50 | + for (const postType of acfPostTypes) { |
| 51 | + const tabLink = page |
| 52 | + .locator("#wpbody-content") |
| 53 | + .getByRole("link", { name: postType.pluralLabel }); |
| 54 | + |
| 55 | + await expect(tabLink).toBeVisible(); |
| 56 | + |
| 57 | + // Click on the tab to verify settings fields are present |
| 58 | + await switchToTab(page, postType.pluralLabel); |
| 59 | + |
| 60 | + // Verify settings fields exist for this custom post type |
| 61 | + const enabledCheckbox = getSettingsField(postType.key).enabledCheckbox; |
| 62 | + const previewUrlInput = getSettingsField(postType.key).previewUrlInput; |
| 63 | + const iframeCheckbox = getSettingsField(postType.key).iframeCheckbox; |
| 64 | + |
| 65 | + await expect(page.locator(enabledCheckbox)).toBeVisible(); |
| 66 | + await expect(page.locator(previewUrlInput)).toBeVisible(); |
| 67 | + await expect(page.locator(iframeCheckbox)).toBeVisible(); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + test("ACF custom post types use HWP preview logic correctly", async ({ |
| 72 | + page, |
| 73 | + admin, |
| 74 | + requestUtils, |
| 75 | + }) => { |
| 76 | + // Create a custom post type |
| 77 | + const testPostType = acfPostTypes[0]; // book |
| 78 | + await createACFPostType(admin, page, testPostType); |
| 79 | + |
| 80 | + // Configure HWP Previews for the custom post type |
| 81 | + await goToPluginPage(admin); |
| 82 | + |
| 83 | + await switchToTab(page, testPostType.pluralLabel); |
| 84 | + await page |
| 85 | + .locator(getSettingsField(testPostType.key).enabledCheckbox) |
| 86 | + .check(); |
| 87 | + await page |
| 88 | + .locator(getSettingsField(testPostType.key).previewUrlInput) |
| 89 | + .fill(TEST_PREVIEW_URL); |
| 90 | + await saveChanges(page); |
| 91 | + |
| 92 | + // Create a post of the custom post type using REST API |
| 93 | + const customPost = await requestUtils.rest({ |
| 94 | + method: "POST", |
| 95 | + path: `/wp/v2/${testPostType.key}`, |
| 96 | + data: { |
| 97 | + title: `Test ${testPostType.singularLabel}`, |
| 98 | + content: `Test content for ${testPostType.singularLabel}`, |
| 99 | + status: "draft", |
| 100 | + }, |
| 101 | + }); |
| 102 | + |
| 103 | + // Navigate to the custom post type list |
| 104 | + await admin.visitAdminPage(`/edit.php?post_type=${testPostType.key}`); |
| 105 | + |
| 106 | + // Verify preview link uses HWP preview URL |
| 107 | + await expect( |
| 108 | + page.locator(`#post-${customPost.id} .view a`, { |
| 109 | + hasText: "Preview", |
| 110 | + exact: true, |
| 111 | + }) |
| 112 | + ).toHaveAttribute("href", TEST_PREVIEW_URL); |
| 113 | + |
| 114 | + // Delete the post to not interfere with other tests |
| 115 | + await requestUtils.rest({ |
| 116 | + method: "DELETE", |
| 117 | + path: `/wp/v2/${testPostType.key}/${customPost.id}`, |
| 118 | + data: { |
| 119 | + force: true, |
| 120 | + }, |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + test("ACF custom post types with iframe preview enabled", async ({ |
| 125 | + page, |
| 126 | + admin, |
| 127 | + requestUtils, |
| 128 | + }) => { |
| 129 | + // Create custom post type |
| 130 | + const testPostType = acfPostTypes[0]; // book |
| 131 | + await createACFPostType(admin, page, testPostType); |
| 132 | + |
| 133 | + await goToPluginPage(admin); |
| 134 | + |
| 135 | + await switchToTab(page, testPostType.pluralLabel); |
| 136 | + await page |
| 137 | + .locator(getSettingsField(testPostType.key).enabledCheckbox) |
| 138 | + .check(); |
| 139 | + await page |
| 140 | + .locator(getSettingsField(testPostType.key).iframeCheckbox) |
| 141 | + .check(); |
| 142 | + await page |
| 143 | + .locator(getSettingsField(testPostType.key).previewUrlInput) |
| 144 | + .fill(TEST_PREVIEW_URL); |
| 145 | + await saveChanges(page); |
| 146 | + |
| 147 | + // Create a post of the custom post type |
| 148 | + const customPost = await requestUtils.rest({ |
| 149 | + method: "POST", |
| 150 | + path: `/wp/v2/${testPostType.key}`, |
| 151 | + data: { |
| 152 | + title: `Test ${testPostType.singularLabel}`, |
| 153 | + content: `Test content for ${testPostType.singularLabel}`, |
| 154 | + status: "draft", |
| 155 | + }, |
| 156 | + }); |
| 157 | + |
| 158 | + // Navigate to the custom post type list and click preview |
| 159 | + await admin.visitAdminPage(`/edit.php?post_type=${testPostType.key}`); |
| 160 | + const previewLink = page.locator(`#post-${customPost.id} .view a`, { |
| 161 | + hasText: "Preview", |
| 162 | + exact: true, |
| 163 | + }); |
| 164 | + await previewLink.focus(); |
| 165 | + await previewLink.click(); |
| 166 | + await page.waitForLoadState("domcontentloaded"); |
| 167 | + |
| 168 | + // Verify iframe is present with correct URL |
| 169 | + const iframe = page.locator("iframe.headless-preview-frame"); |
| 170 | + await expect(iframe).toHaveAttribute("src", TEST_PREVIEW_URL); |
| 171 | + |
| 172 | + // Delete the post to not interfere with other tests |
| 173 | + await requestUtils.rest({ |
| 174 | + method: "DELETE", |
| 175 | + path: `/wp/v2/${testPostType.key}/${customPost.id}`, |
| 176 | + data: { |
| 177 | + force: true, |
| 178 | + }, |
| 179 | + }); |
| 180 | + }); |
| 181 | +}); |
0 commit comments