|
| 1 | +import { expect, test } from "@wordpress/e2e-test-utils-playwright"; |
| 2 | +import { |
| 3 | + goToLoggingSettingsPage, |
| 4 | + goToLogsListPage, |
| 5 | + configureLogging, |
| 6 | + executeGraphQLQuery, |
| 7 | + resetPluginSettings, |
| 8 | +} from "../utils"; |
| 9 | +import { GET_POSTS_QUERY } from "../constants"; |
| 10 | + |
| 11 | +test.describe("Configure WPGraphQL Logging Plugin and Verify Logging Works", () => { |
| 12 | + test.beforeEach(async ({ admin }) => { |
| 13 | + await resetPluginSettings(admin); |
| 14 | + }); |
| 15 | + |
| 16 | + test("should enable logging and verify GraphQL queries are being logged", async ({ |
| 17 | + page, |
| 18 | + admin, |
| 19 | + request, |
| 20 | + }) => { |
| 21 | + // Set up logging settings |
| 22 | + await goToLoggingSettingsPage(admin); |
| 23 | + await expect(page.locator("h1")).toHaveText("WPGraphQL Logging Settings"); |
| 24 | + |
| 25 | + await configureLogging(page, { |
| 26 | + enabled: true, |
| 27 | + dataSampling: "100", |
| 28 | + eventLogSelection: ["graphql_request_results"], |
| 29 | + }); |
| 30 | + |
| 31 | + await expect(page.locator(".notice.notice-success")).toBeVisible(); |
| 32 | + |
| 33 | + // Execute a GraphQL query |
| 34 | + const response = await executeGraphQLQuery(request, GET_POSTS_QUERY); |
| 35 | + expect(response.ok()).toBeTruthy(); |
| 36 | + |
| 37 | + // Check that the log appears in the logs list |
| 38 | + await goToLogsListPage(admin); |
| 39 | + await expect(page.locator("h1")).toContainText("WPGraphQL Logs"); |
| 40 | + |
| 41 | + const logRow = page |
| 42 | + .locator("#the-list tr") |
| 43 | + .filter({ hasText: "GetPosts" }) |
| 44 | + .first(); |
| 45 | + await expect(logRow).toBeVisible({ timeout: 10000 }); |
| 46 | + |
| 47 | + // View log details |
| 48 | + const viewLink = logRow.locator(".row-actions .view a"); |
| 49 | + await expect(viewLink).toBeVisible(); |
| 50 | + await viewLink.focus(); |
| 51 | + await viewLink.click(); |
| 52 | + |
| 53 | + await expect(page.locator("h1")).toContainText("Log Entry"); |
| 54 | + |
| 55 | + const logTable = page.locator(".widefat.striped"); |
| 56 | + await expect(logTable).toBeVisible(); |
| 57 | + |
| 58 | + const queryRow = logTable |
| 59 | + .locator("tr") |
| 60 | + .filter({ has: page.locator("th", { hasText: "Query" }) }); |
| 61 | + await expect(queryRow).toBeVisible(); |
| 62 | + await expect(queryRow.locator("td pre")).toContainText("query GetPosts"); |
| 63 | + |
| 64 | + // Go back to logs list |
| 65 | + const backLink = page |
| 66 | + .locator("p a.button") |
| 67 | + .filter({ hasText: "Back to Logs" }); |
| 68 | + await expect(backLink).toBeVisible(); |
| 69 | + |
| 70 | + await backLink.click(); |
| 71 | + await expect(page.locator("h1")).toContainText("WPGraphQL Logs"); |
| 72 | + }); |
| 73 | + |
| 74 | + test("should not log queries when logging is disabled", async ({ |
| 75 | + page, |
| 76 | + admin, |
| 77 | + request, |
| 78 | + }) => { |
| 79 | + // Make sure there are no logs |
| 80 | + await goToLogsListPage(admin); |
| 81 | + await expect( |
| 82 | + page.locator('td.colspanchange:has-text("No items found.")') |
| 83 | + ).toBeVisible(); |
| 84 | + |
| 85 | + // Disable logging |
| 86 | + await goToLoggingSettingsPage(admin); |
| 87 | + await configureLogging(page, { |
| 88 | + enabled: false, |
| 89 | + dataSampling: "100", |
| 90 | + }); |
| 91 | + |
| 92 | + await executeGraphQLQuery(request, GET_POSTS_QUERY); |
| 93 | + |
| 94 | + // Navigate to logs and verify no new logs were created |
| 95 | + await goToLogsListPage(admin); |
| 96 | + await expect( |
| 97 | + page.locator('td.colspanchange:has-text("No items found.")') |
| 98 | + ).toBeVisible(); |
| 99 | + }); |
| 100 | +}); |
0 commit comments