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

Ensure site is not scanned multiple times in Onboarding Wizard #6791

Merged
merged 2 commits into from
Dec 15, 2021
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
27 changes: 24 additions & 3 deletions assets/src/components/site-scan-context-provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ const INITIAL_STATE = {
currentlyScannedUrlIndexes: [],
forceStandardMode: false,
scannableUrls: [],
scanOnce: false,
status: '',
scansCount: 0,
urlIndexesPendingScan: [],
};

Expand All @@ -89,6 +91,7 @@ const CONCURRENT_VALIDATION_REQUESTS_WAIT_MS = 500;
* @param {Object} action Action to call.
* @return {Object} New state.
*/
//eslint-disable-next-line complexity
export function siteScanReducer( state, action ) {
// Bail out early if Site Scan is skipped, i.e. if there is no validation nonce provided meaning the current user
// does not have capabilities for running AMP validation.
Expand All @@ -113,9 +116,16 @@ export function siteScanReducer( state, action ) {
};
}
case ACTION_SCANNABLE_URLS_RECEIVE: {
if ( ! action.scannableUrls || action.scannableUrls?.length === 0 ) {
return {
...state,
status: STATUS_COMPLETED,
};
}

return {
...state,
status: action.scannableUrls?.length > 0 ? STATUS_READY : STATUS_COMPLETED,
status: state.scanOnce && state.scansCount > 0 ? STATUS_COMPLETED : STATUS_READY,
scannableUrls: action.scannableUrls,
};
}
Expand All @@ -124,10 +134,18 @@ export function siteScanReducer( state, action ) {
return state;
}

if ( state.scanOnce && state.scansCount > 0 ) {
return {
...state,
status: STATUS_COMPLETED,
};
}

return {
...state,
status: STATUS_IDLE,
currentlyScannedUrlIndexes: [],
scansCount: state.scansCount + 1,
urlIndexesPendingScan: state.scannableUrls.map( ( url, index ) => index ),
};
}
Expand Down Expand Up @@ -201,8 +219,9 @@ export function siteScanReducer( state, action ) {
* @param {?any} props.children Component children.
* @param {boolean} props.fetchCachedValidationErrors Whether to fetch cached validation errors on mount.
* @param {boolean} props.refetchPluginSuppressionOnScanComplete Whether to refetch plugin suppression data when site scan is complete.
* @param {boolean} props.resetOnOptionsChange Whether to reset scanner and refetch scannable URLs whenever AMPoptions are changed.
* @param {boolean} props.resetOnOptionsChange Whether to reset scanner and refetch scannable URLs whenever AMP options are changed.
* @param {string} props.scannableUrlsRestPath The REST path for interacting with the scannable URL resources.
* @param {boolean} props.scanOnce Whether to scan only once.
* @param {string} props.validateNonce The AMP validate nonce.
*/
export function SiteScanContextProvider( {
Expand All @@ -211,6 +230,7 @@ export function SiteScanContextProvider( {
refetchPluginSuppressionOnScanComplete = false,
resetOnOptionsChange = false,
scannableUrlsRestPath,
scanOnce = false,
validateNonce,
} ) {
const {
Expand All @@ -221,7 +241,7 @@ export function SiteScanContextProvider( {
refetchPluginSuppression,
} = useContext( Options );
const { setAsyncError } = useAsyncError();
const [ state, dispatch ] = useReducer( siteScanReducer, INITIAL_STATE );
const [ state, dispatch ] = useReducer( siteScanReducer, { ...INITIAL_STATE, scanOnce } );
const {
currentlyScannedUrlIndexes,
forceStandardMode,
Expand Down Expand Up @@ -523,5 +543,6 @@ SiteScanContextProvider.propTypes = {
refetchPluginSuppressionOnScanComplete: PropTypes.bool,
resetOnOptionsChange: PropTypes.bool,
scannableUrlsRestPath: PropTypes.string,
scanOnce: PropTypes.bool,
validateNonce: PropTypes.string,
};
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,33 @@ describe( 'siteScanReducer', () => {
* ACTION_SCANNABLE_URLS_RECEIVE
*/
it( 'returns correct state for ACTION_SCANNABLE_URLS_RECEIVE', () => {
expect( siteScanReducer( {}, {
expect( siteScanReducer( { scanOnce: false, scansCount: 0 }, {
type: ACTION_SCANNABLE_URLS_RECEIVE,
scannableUrls: [],
} ) ).toStrictEqual( {
status: STATUS_COMPLETED,
scannableUrls: [],
scanOnce: false,
scansCount: 0,
} );

expect( siteScanReducer( {}, {
expect( siteScanReducer( { scanOnce: false, scansCount: 2 }, {
type: ACTION_SCANNABLE_URLS_RECEIVE,
scannableUrls: [ 'foo', 'bar' ],
} ) ).toStrictEqual( {
status: STATUS_READY,
scannableUrls: [ 'foo', 'bar' ],
scanOnce: false,
scansCount: 2,
} );

expect( siteScanReducer( { scanOnce: true, scansCount: 1 }, {
type: ACTION_SCANNABLE_URLS_RECEIVE,
scannableUrls: [ 'foo', 'bar' ],
} ) ).toStrictEqual( {
status: STATUS_COMPLETED,
scannableUrls: [ 'foo', 'bar' ],
scanOnce: true,
scansCount: 1,
} );
} );

Expand All @@ -129,18 +142,45 @@ describe( 'siteScanReducer', () => {
] )( 'returns correct state for ACTION_SCAN_INITIALIZE when initial status is %s', ( status ) => {
expect( siteScanReducer( {
status,
scanOnce: false,
scansCount: 0,
scannableUrls: [ 'foo', 'bar' ],
urlIndexesPendingScan: [],
}, {
type: ACTION_SCAN_INITIALIZE,
} ) ).toStrictEqual( {
status: STATUS_IDLE,
currentlyScannedUrlIndexes: [],
scanOnce: false,
scansCount: 1,
scannableUrls: [ 'foo', 'bar' ],
urlIndexesPendingScan: [ 0, 1 ],
} );
} );

it.each( [
STATUS_CANCELLED,
STATUS_COMPLETED,
STATUS_FAILED,
STATUS_READY,
] )( 'returns correct state for ACTION_SCAN_INITIALIZE when initial status is %s and scan should be done just once', ( status ) => {
expect( siteScanReducer( {
status,
scanOnce: true,
scansCount: 1,
scannableUrls: [ 'foo', 'bar' ],
urlIndexesPendingScan: [],
}, {
type: ACTION_SCAN_INITIALIZE,
} ) ).toStrictEqual( {
status: STATUS_COMPLETED,
scanOnce: true,
scansCount: 1,
scannableUrls: [ 'foo', 'bar' ],
urlIndexesPendingScan: [],
} );
} );

/**
* ACTION_SCAN_URL
*/
Expand Down
1 change: 1 addition & 0 deletions assets/src/onboarding-wizard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export function Providers( { children } ) {
fetchCachedValidationErrors={ false }
resetOnOptionsChange={ true }
scannableUrlsRestPath={ SCANNABLE_URLS_REST_PATH }
scanOnce={ true }
validateNonce={ VALIDATE_NONCE }
>
<NavigationContextProvider pages={ PAGES }>
Expand Down