Skip to content
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

Fix: Ensure Accurate Detection of WordPress Previews via URL Query Parameters #1911

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heavy-rats-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@faustwp/core': patch
---

Bug: Fixes issue with review detection via query string is too greedy and catches non WP previews
4 changes: 2 additions & 2 deletions packages/faustwp-core/src/components/WordPressTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useAuth } from '../hooks/useAuth.js';
import { SEED_QUERY, SeedNode } from '../queries/seedQuery.js';
import { FaustContext, FaustQueries } from '../store/FaustContext.js';
import { getQueryParam } from '../utils/convert.js';
import { isWordPressPreview } from '../utils/isWordPressPreview.js';

export type FaustProps = {
__SEED_NODE__?: SeedNode | null;
Expand Down Expand Up @@ -204,9 +205,8 @@ export function WordPressTemplate(props: WordPressTemplateProps) {
return;
}

setIsPreview(window.location.search.includes('preview=true'));
setIsPreview(isWordPressPreview(window.location.search));
}, []);

/**
* If we are on a preview route and there is no authenticated user, redirect
* them to the login page
Expand Down
5 changes: 5 additions & 0 deletions packages/faustwp-core/src/utils/isWordPressPreview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Helper function to check if the URL is a WordPress preview
export function isWordPressPreview(search: string) {
const params = new URLSearchParams(search);
return params.has('preview') && params.get('preview') === 'true';
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,56 +27,6 @@ describe('<WordPressTemplate />', () => {
);
});

test('Properly determines whether or not the given URL is a preview or not', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problematic unit test with lots of testing anti-patterns. I removed this since we already test the logic of isWordPressPreview

const getConfigSpy = jest.spyOn(getConfig, 'getConfig').mockReturnValue({
templates: {},
});

const useAuthSpy = jest.spyOn(useAuth, 'useAuth').mockReturnValue({
isAuthenticated: false,
isReady: true,
loginUrl: null,
});

delete (window as any).location;
window.location = new URL('http://localhost:3000') as any as Location;

const stringIncludesSpy = jest.spyOn(String.prototype, 'includes');

await act(async () => {
render(
<WordPressTemplate.WordPressTemplate
__SEED_NODE__={{ databaseId: '1' }}
__TEMPLATE_QUERY_DATA__={{ fakeData: true }}
/>,
);
});

expect(window.location.search.includes).toHaveBeenLastCalledWith(
'preview=true',
);
expect(window.location.search.includes).toReturnWith(false);

delete (window as any).location;
window.location = new URL(
'http://localhost:3000?preview=true&p=40',
) as any as Location;

await act(async () => {
render(
<WordPressTemplate.WordPressTemplate
__SEED_NODE__={{ databaseId: '1' }}
__TEMPLATE_QUERY_DATA__={{ fakeData: true }}
/>,
);
});

expect(window.location.search.includes).toHaveBeenLastCalledWith(
'preview=true',
);
expect(window.location.search.includes).toReturnWith(true);
});

test('Properly redirects to login URL on preview route with no logged in user', async () => {
const getConfigSpy = jest.spyOn(getConfig, 'getConfig').mockReturnValue({
templates: {},
Expand Down
23 changes: 23 additions & 0 deletions packages/faustwp-core/tests/utils/isWordPressPreview.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isWordPressPreview } from '../../src/utils/isWordPressPreview';

describe('isWordPressPreview', () => {
it('returns true if the search string contains preview=true', () => {
expect(isWordPressPreview('?preview=true')).toBe(true);
expect(isWordPressPreview('?foo=bar&preview=true')).toBe(true);
});

it('returns false if the search string does not contain preview=true', () => {
expect(isWordPressPreview('?preview=false')).toBe(false);
expect(isWordPressPreview('?foo=bar')).toBe(false);
expect(isWordPressPreview('?otpreview=true')).toBe(false);
});

it('returns false if the search string is empty', () => {
expect(isWordPressPreview('')).toBe(false);
});

it('returns false if the preview parameter is not exactly true', () => {
expect(isWordPressPreview('?preview=1')).toBe(false);
expect(isWordPressPreview('?preview=yes')).toBe(false);
});
});
Loading