Skip to content

Commit 6cf0154

Browse files
authored
Merge pull request #6791 from ampproject/fix/scan-once-in-wizard
Ensure site is not scanned multiple times in Onboarding Wizard
2 parents fda381f + 8d49e62 commit 6cf0154

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

assets/src/components/site-scan-context-provider/index.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ const INITIAL_STATE = {
6464
currentlyScannedUrlIndexes: [],
6565
forceStandardMode: false,
6666
scannableUrls: [],
67+
scanOnce: false,
6768
status: '',
69+
scansCount: 0,
6870
urlIndexesPendingScan: [],
6971
};
7072

@@ -89,6 +91,7 @@ const CONCURRENT_VALIDATION_REQUESTS_WAIT_MS = 500;
8991
* @param {Object} action Action to call.
9092
* @return {Object} New state.
9193
*/
94+
//eslint-disable-next-line complexity
9295
export function siteScanReducer( state, action ) {
9396
// Bail out early if Site Scan is skipped, i.e. if there is no validation nonce provided meaning the current user
9497
// does not have capabilities for running AMP validation.
@@ -113,9 +116,16 @@ export function siteScanReducer( state, action ) {
113116
};
114117
}
115118
case ACTION_SCANNABLE_URLS_RECEIVE: {
119+
if ( ! action.scannableUrls || action.scannableUrls?.length === 0 ) {
120+
return {
121+
...state,
122+
status: STATUS_COMPLETED,
123+
};
124+
}
125+
116126
return {
117127
...state,
118-
status: action.scannableUrls?.length > 0 ? STATUS_READY : STATUS_COMPLETED,
128+
status: state.scanOnce && state.scansCount > 0 ? STATUS_COMPLETED : STATUS_READY,
119129
scannableUrls: action.scannableUrls,
120130
};
121131
}
@@ -124,10 +134,18 @@ export function siteScanReducer( state, action ) {
124134
return state;
125135
}
126136

137+
if ( state.scanOnce && state.scansCount > 0 ) {
138+
return {
139+
...state,
140+
status: STATUS_COMPLETED,
141+
};
142+
}
143+
127144
return {
128145
...state,
129146
status: STATUS_IDLE,
130147
currentlyScannedUrlIndexes: [],
148+
scansCount: state.scansCount + 1,
131149
urlIndexesPendingScan: state.scannableUrls.map( ( url, index ) => index ),
132150
};
133151
}
@@ -201,8 +219,9 @@ export function siteScanReducer( state, action ) {
201219
* @param {?any} props.children Component children.
202220
* @param {boolean} props.fetchCachedValidationErrors Whether to fetch cached validation errors on mount.
203221
* @param {boolean} props.refetchPluginSuppressionOnScanComplete Whether to refetch plugin suppression data when site scan is complete.
204-
* @param {boolean} props.resetOnOptionsChange Whether to reset scanner and refetch scannable URLs whenever AMPoptions are changed.
222+
* @param {boolean} props.resetOnOptionsChange Whether to reset scanner and refetch scannable URLs whenever AMP options are changed.
205223
* @param {string} props.scannableUrlsRestPath The REST path for interacting with the scannable URL resources.
224+
* @param {boolean} props.scanOnce Whether to scan only once.
206225
* @param {string} props.validateNonce The AMP validate nonce.
207226
*/
208227
export function SiteScanContextProvider( {
@@ -211,6 +230,7 @@ export function SiteScanContextProvider( {
211230
refetchPluginSuppressionOnScanComplete = false,
212231
resetOnOptionsChange = false,
213232
scannableUrlsRestPath,
233+
scanOnce = false,
214234
validateNonce,
215235
} ) {
216236
const {
@@ -221,7 +241,7 @@ export function SiteScanContextProvider( {
221241
refetchPluginSuppression,
222242
} = useContext( Options );
223243
const { setAsyncError } = useAsyncError();
224-
const [ state, dispatch ] = useReducer( siteScanReducer, INITIAL_STATE );
244+
const [ state, dispatch ] = useReducer( siteScanReducer, { ...INITIAL_STATE, scanOnce } );
225245
const {
226246
currentlyScannedUrlIndexes,
227247
forceStandardMode,
@@ -523,5 +543,6 @@ SiteScanContextProvider.propTypes = {
523543
refetchPluginSuppressionOnScanComplete: PropTypes.bool,
524544
resetOnOptionsChange: PropTypes.bool,
525545
scannableUrlsRestPath: PropTypes.string,
546+
scanOnce: PropTypes.bool,
526547
validateNonce: PropTypes.string,
527548
};

assets/src/components/site-scan-context-provider/test/site-scan-reducer.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,33 @@ describe( 'siteScanReducer', () => {
9090
* ACTION_SCANNABLE_URLS_RECEIVE
9191
*/
9292
it( 'returns correct state for ACTION_SCANNABLE_URLS_RECEIVE', () => {
93-
expect( siteScanReducer( {}, {
93+
expect( siteScanReducer( { scanOnce: false, scansCount: 0 }, {
9494
type: ACTION_SCANNABLE_URLS_RECEIVE,
9595
scannableUrls: [],
9696
} ) ).toStrictEqual( {
9797
status: STATUS_COMPLETED,
98-
scannableUrls: [],
98+
scanOnce: false,
99+
scansCount: 0,
99100
} );
100101

101-
expect( siteScanReducer( {}, {
102+
expect( siteScanReducer( { scanOnce: false, scansCount: 2 }, {
102103
type: ACTION_SCANNABLE_URLS_RECEIVE,
103104
scannableUrls: [ 'foo', 'bar' ],
104105
} ) ).toStrictEqual( {
105106
status: STATUS_READY,
106107
scannableUrls: [ 'foo', 'bar' ],
108+
scanOnce: false,
109+
scansCount: 2,
110+
} );
111+
112+
expect( siteScanReducer( { scanOnce: true, scansCount: 1 }, {
113+
type: ACTION_SCANNABLE_URLS_RECEIVE,
114+
scannableUrls: [ 'foo', 'bar' ],
115+
} ) ).toStrictEqual( {
116+
status: STATUS_COMPLETED,
117+
scannableUrls: [ 'foo', 'bar' ],
118+
scanOnce: true,
119+
scansCount: 1,
107120
} );
108121
} );
109122

@@ -129,18 +142,45 @@ describe( 'siteScanReducer', () => {
129142
] )( 'returns correct state for ACTION_SCAN_INITIALIZE when initial status is %s', ( status ) => {
130143
expect( siteScanReducer( {
131144
status,
145+
scanOnce: false,
146+
scansCount: 0,
132147
scannableUrls: [ 'foo', 'bar' ],
133148
urlIndexesPendingScan: [],
134149
}, {
135150
type: ACTION_SCAN_INITIALIZE,
136151
} ) ).toStrictEqual( {
137152
status: STATUS_IDLE,
138153
currentlyScannedUrlIndexes: [],
154+
scanOnce: false,
155+
scansCount: 1,
139156
scannableUrls: [ 'foo', 'bar' ],
140157
urlIndexesPendingScan: [ 0, 1 ],
141158
} );
142159
} );
143160

161+
it.each( [
162+
STATUS_CANCELLED,
163+
STATUS_COMPLETED,
164+
STATUS_FAILED,
165+
STATUS_READY,
166+
] )( 'returns correct state for ACTION_SCAN_INITIALIZE when initial status is %s and scan should be done just once', ( status ) => {
167+
expect( siteScanReducer( {
168+
status,
169+
scanOnce: true,
170+
scansCount: 1,
171+
scannableUrls: [ 'foo', 'bar' ],
172+
urlIndexesPendingScan: [],
173+
}, {
174+
type: ACTION_SCAN_INITIALIZE,
175+
} ) ).toStrictEqual( {
176+
status: STATUS_COMPLETED,
177+
scanOnce: true,
178+
scansCount: 1,
179+
scannableUrls: [ 'foo', 'bar' ],
180+
urlIndexesPendingScan: [],
181+
} );
182+
} );
183+
144184
/**
145185
* ACTION_SCAN_URL
146186
*/

assets/src/onboarding-wizard/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export function Providers( { children } ) {
8282
fetchCachedValidationErrors={ false }
8383
resetOnOptionsChange={ true }
8484
scannableUrlsRestPath={ SCANNABLE_URLS_REST_PATH }
85+
scanOnce={ true }
8586
validateNonce={ VALIDATE_NONCE }
8687
>
8788
<NavigationContextProvider pages={ PAGES }>

0 commit comments

Comments
 (0)