-
Notifications
You must be signed in to change notification settings - Fork 1
Add WP Rocket settings no-broken-links check #329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1a4e041
add WP Rocket settings no-broken-links check
hanna-meda d26453a
add copilot suggestions
hanna-meda a697925
refactor broken-links: improve robustness and debugging
hanna-meda 279af04
add better error handling, documentation, improve type safety, move s…
hanna-meda 139edae
add skip external 401/403 gated content
hanna-meda c58695a
simplify error handling and improved logic
hanna-meda cddfb5d
move collectLinks and validateLinks under helpers.ts
hanna-meda 7be6267
wrap tab URL construction in try-catch to handle malformed hrefs
hanna-meda 932ceeb
update to assert instead of throwing error
hanna-meda 7381d20
make network errors fail the test
hanna-meda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| @setup @smoke @brokenlinks | ||
| Feature: Broken links in WP Rocket settings UI | ||
|
|
||
| Scenario: WP Rocket settings links are not broken | ||
| Given I am logged in | ||
| And plugin is installed 'new_release' | ||
| And plugin is activated | ||
| Then WP Rocket settings links are not broken | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /** | ||
| * @fileoverview | ||
| * This module contains Cucumber step definitions for validating links in WP Rocket settings UI. | ||
| * | ||
| * @requires {@link ../../common/custom-world} | ||
| * @requires {@link @playwright/test} | ||
| * @requires {@link @cucumber/cucumber} | ||
| */ | ||
| import { expect } from '@playwright/test'; | ||
| import { Then } from '@cucumber/cucumber'; | ||
hanna-meda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import { ICustomWorld } from '../../common/custom-world'; | ||
|
|
||
| /** | ||
| * Executes the step to verify that WP Rocket settings links are not broken. | ||
hanna-meda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @function | ||
| * @async | ||
| * @param {ICustomWorld} this - The Cucumber world context for the current scenario. | ||
| * @return {Promise<void>} - A Promise that resolves when the check is completed. | ||
| */ | ||
hanna-meda marked this conversation as resolved.
Show resolved
Hide resolved
hanna-meda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Then('WP Rocket settings links are not broken', async function (this: ICustomWorld) { | ||
| const hrefs = new Set<string>(); | ||
|
|
||
| // Visit WP Rocket settings. | ||
| await this.utils.visitPage('wp-admin/options-general.php?page=wprocket'); | ||
|
|
||
| const collectLinks = async (): Promise<void> => { | ||
hanna-meda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const links = await this.page.$$eval('#wpbody-content a[href]', (elements) => | ||
| elements | ||
| .map((element) => element.getAttribute('href')) | ||
| .filter(Boolean) | ||
| ); | ||
|
|
||
| for (const href of links) { | ||
| hrefs.add(href as string); | ||
hanna-meda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }; | ||
|
|
||
| // Collect links from the base settings page first. | ||
| await collectLinks(); | ||
|
|
||
| // Collect tab links from within the WP Rocket settings content. | ||
| const tabHrefs = await this.page.$$eval('#wpbody-content a[href*="page=wprocket#"]', (links) => | ||
| links | ||
| .map((link) => link.getAttribute('href')) | ||
| .filter(Boolean) | ||
| ); | ||
|
|
||
| const tabUrls = Array.from(new Set(tabHrefs)).map((href) => { | ||
| return new URL(href as string, this.page.url()).toString(); | ||
hanna-meda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| for (const tabUrl of tabUrls) { | ||
| await this.page.goto(tabUrl); | ||
| await this.page.waitForLoadState('load'); | ||
| await collectLinks(); | ||
| } | ||
hanna-meda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const normalizedUrls = new Set<string>(); | ||
| const skipProtocols = ['mailto:', 'tel:', 'javascript:']; | ||
|
|
||
| for (const href of hrefs) { | ||
| const lowerHref = href.toLowerCase(); | ||
| if (lowerHref.startsWith('#')) { | ||
| continue; | ||
| } | ||
|
|
||
| if (skipProtocols.some((protocol) => lowerHref.startsWith(protocol))) { | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| const url = new URL(href, this.page.url()); | ||
hanna-meda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!['http:', 'https:'].includes(url.protocol)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (url.pathname.endsWith('/wp-admin/admin-post.php')) { | ||
| // Avoid triggering admin-post actions. | ||
| continue; | ||
| } | ||
|
|
||
| url.hash = ''; | ||
| normalizedUrls.add(url.toString()); | ||
| } catch { | ||
hanna-meda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| continue; | ||
| } | ||
| } | ||
|
|
||
| for (const url of normalizedUrls) { | ||
| try { | ||
| const response = await this.page.request.get(url, { maxRedirects: 5, timeout: 30000 }); | ||
| expect(response.status(), `Expected ${url} not to return 404`).not.toBe(404); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| throw new Error(`Network error while requesting ${url}: ${message}`); | ||
| } | ||
| } | ||
hanna-meda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.