|
| 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("Basic Logging Usage", () => { |
| 12 | + test.beforeEach(async ({ admin, page }) => { |
| 13 | + await resetPluginSettings(admin); |
| 14 | + |
| 15 | + // Go to settings page |
| 16 | + await goToLoggingSettingsPage(admin); |
| 17 | + await expect(page.locator("h1")).toHaveText("WPGraphQL Logging Settings"); |
| 18 | + }); |
| 19 | + |
| 20 | + test("enables logging and logs GraphQL queries", async ({ |
| 21 | + page, |
| 22 | + admin, |
| 23 | + request, |
| 24 | + }) => { |
| 25 | + await configureLogging(page, { |
| 26 | + enabled: true, |
| 27 | + dataSampling: "100", |
| 28 | + eventLogSelection: ["graphql_request_results"], |
| 29 | + }); |
| 30 | + |
| 31 | + const response = await executeGraphQLQuery(request, GET_POSTS_QUERY); |
| 32 | + expect(response.ok()).toBeTruthy(); |
| 33 | + |
| 34 | + // Check that the log appears in the logs list |
| 35 | + await goToLogsListPage(admin); |
| 36 | + await expect(page.locator("h1")).toContainText("WPGraphQL Logs"); |
| 37 | + |
| 38 | + const logRow = page |
| 39 | + .locator("#the-list tr") |
| 40 | + .filter({ hasText: "GetPosts" }) |
| 41 | + .first(); |
| 42 | + await expect(logRow).toBeVisible({ timeout: 10000 }); |
| 43 | + |
| 44 | + // View log details |
| 45 | + const viewLink = logRow.locator(".row-actions .view a"); |
| 46 | + await expect(viewLink).toBeVisible(); |
| 47 | + await viewLink.focus(); |
| 48 | + await viewLink.click(); |
| 49 | + |
| 50 | + await expect(page.locator("h1")).toContainText("Log Entry"); |
| 51 | + |
| 52 | + const logTable = page.locator(".widefat.striped"); |
| 53 | + await expect(logTable).toBeVisible(); |
| 54 | + |
| 55 | + const queryRow = logTable |
| 56 | + .locator("tr") |
| 57 | + .filter({ has: page.locator("th", { hasText: "Query" }) }); |
| 58 | + await expect(queryRow).toBeVisible(); |
| 59 | + await expect(queryRow.locator("td pre")).toContainText("query GetPosts"); |
| 60 | + |
| 61 | + // Go back to logs list |
| 62 | + const backLink = page |
| 63 | + .locator("p a.button") |
| 64 | + .filter({ hasText: "Back to Logs" }); |
| 65 | + await expect(backLink).toBeVisible(); |
| 66 | + |
| 67 | + await backLink.click(); |
| 68 | + await expect(page.locator("h1")).toContainText("WPGraphQL Logs"); |
| 69 | + }); |
| 70 | + |
| 71 | + test("does not log when disabled", async ({ page, admin, request }) => { |
| 72 | + await configureLogging(page, { |
| 73 | + enabled: false, |
| 74 | + dataSampling: "100", |
| 75 | + }); |
| 76 | + |
| 77 | + // Make sure there are no logs |
| 78 | + await goToLogsListPage(admin); |
| 79 | + await expect( |
| 80 | + page.locator('td.colspanchange:has-text("No items found.")') |
| 81 | + ).toBeVisible(); |
| 82 | + |
| 83 | + await executeGraphQLQuery(request, GET_POSTS_QUERY); |
| 84 | + |
| 85 | + // Navigate to logs and verify no new logs were created |
| 86 | + await goToLogsListPage(admin); |
| 87 | + await expect( |
| 88 | + page.locator('td.colspanchange:has-text("No items found.")') |
| 89 | + ).toBeVisible(); |
| 90 | + }); |
| 91 | + |
| 92 | + test("downloads log as CSV with correct content", async ({ |
| 93 | + page, |
| 94 | + admin, |
| 95 | + request, |
| 96 | + }) => { |
| 97 | + await configureLogging(page, { |
| 98 | + enabled: true, |
| 99 | + dataSampling: "100", |
| 100 | + eventLogSelection: ["graphql_request_results"], |
| 101 | + }); |
| 102 | + |
| 103 | + // Execute a GraphQL query |
| 104 | + const response = await executeGraphQLQuery(request, GET_POSTS_QUERY); |
| 105 | + expect(response.ok()).toBeTruthy(); |
| 106 | + |
| 107 | + // Check that the log appears in the logs list |
| 108 | + await goToLogsListPage(admin); |
| 109 | + await expect(page.locator("h1")).toContainText("WPGraphQL Logs"); |
| 110 | + |
| 111 | + const logRow = page |
| 112 | + .locator("#the-list tr") |
| 113 | + .filter({ hasText: "GetPosts" }) |
| 114 | + .first(); |
| 115 | + await expect(logRow).toBeVisible({ timeout: 10000 }); |
| 116 | + |
| 117 | + // View log details |
| 118 | + const downloadButton = logRow.locator(".row-actions .download a"); |
| 119 | + await expect(downloadButton).toBeVisible(); |
| 120 | + |
| 121 | + const downloadPromise = page.waitForEvent("download"); |
| 122 | + await downloadButton.focus(); |
| 123 | + await downloadButton.click(); |
| 124 | + const download = await downloadPromise; |
| 125 | + |
| 126 | + // Verify download properties |
| 127 | + expect(download.suggestedFilename()).toMatch(/graphql_log_\d+\.csv/); |
| 128 | + expect(download.suggestedFilename()).toContain(".csv"); |
| 129 | + |
| 130 | + // Optionally save and verify the content |
| 131 | + const path = await download.path(); |
| 132 | + const fs = require("fs"); |
| 133 | + const content = fs.readFileSync(path, "utf8"); |
| 134 | + |
| 135 | + // Verify CSV contains expected data |
| 136 | + expect(content).toContain("ID"); |
| 137 | + expect(content).toContain("Date"); |
| 138 | + expect(content).toContain("Level"); |
| 139 | + expect(content).toContain("Message"); |
| 140 | + expect(content).toContain("GetPosts"); |
| 141 | + }); |
| 142 | + |
| 143 | + test("should set data sampling to 10% and verify only 1 log is created", async ({ |
| 144 | + page, |
| 145 | + admin, |
| 146 | + request, |
| 147 | + }) => { |
| 148 | + const QUERY_COUNT = 5; |
| 149 | + |
| 150 | + await configureLogging(page, { |
| 151 | + enabled: true, |
| 152 | + dataSampling: "25", |
| 153 | + eventLogSelection: ["graphql_request_results"], |
| 154 | + }); |
| 155 | + |
| 156 | + // Execute a GraphQL queries |
| 157 | + const responses = await Promise.all( |
| 158 | + Array.from({ length: QUERY_COUNT }, async () => |
| 159 | + executeGraphQLQuery(request, GET_POSTS_QUERY) |
| 160 | + ) |
| 161 | + ); |
| 162 | + await Promise.all( |
| 163 | + responses.map(async (response) => { |
| 164 | + return expect(response.ok()).toBeTruthy(); |
| 165 | + }) |
| 166 | + ); |
| 167 | + |
| 168 | + // Navigate to logs and verify no new logs were created |
| 169 | + await goToLogsListPage(admin); |
| 170 | + await expect(page.locator("h1")).toContainText("WPGraphQL Logs"); |
| 171 | + |
| 172 | + const logRow = page.locator("#the-list tr").filter({ hasText: "GetPosts" }); |
| 173 | + |
| 174 | + const logCount = await logRow.count(); |
| 175 | + expect(logCount).toBeLessThan(QUERY_COUNT); |
| 176 | + }); |
| 177 | +}); |
0 commit comments