Skip to content

Commit

Permalink
Backup: do not consider discarded backups as successful backups (#39385)
Browse files Browse the repository at this point in the history
* Backup: do not consider discarded backups as successful backups

* changelog

* Update tests to consider discarded backups
  • Loading branch information
Initsogar authored Sep 13, 2024
1 parent 9a2f447 commit 88750ed
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Stop considering discarded backups as successful backups on the admin page
112 changes: 112 additions & 0 deletions projects/packages/backup/src/js/hooks/test/useBackupsState.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,31 @@ const fixtures = {
is_scan: 0,
},
],
discarded: [
{
id: 381971090,
started: '2023-01-01 02:16:32',
last_updated: '2023-01-01 02:16:34',
status: 'finished',
period: 1672530000,
percent: 100,
is_backup: 1,
is_scan: 0,
has_warnings: false,
discarded: '1',
stats: {
prefix: 'wp_',
plugins: { count: 100 },
themes: { count: 100 },
uploads: { count: 100 },
tables: {
wp_posts: {
post_published: 100,
},
},
}, // full stats details are not required currently
},
],
complete: [
{
id: 381971090,
Expand All @@ -28,6 +53,7 @@ const fixtures = {
is_backup: 1,
is_scan: 0,
has_warnings: false,
discarded: '0',
stats: {
prefix: 'wp_',
plugins: { count: 100 },
Expand All @@ -53,6 +79,54 @@ const fixtures = {
is_scan: 0,
},
],
complete_and_discarded: [
{
id: 234567,
started: '2024-01-02 01:00:00',
last_updated: '2024-01-02 01:05:00',
status: 'finished',
period: 1704157200,
percent: 100,
is_backup: 1,
is_scan: 0,
has_warnings: false,
discarded: '1', // Discarded backup
stats: {
prefix: 'wp_',
plugins: { count: 100 },
themes: { count: 100 },
uploads: { count: 100 },
tables: {
wp_posts: {
post_published: 100,
},
},
},
},
{
id: 123456,
started: '2024-01-01 01:00:00',
last_updated: '2024-01-01 01:05:00',
status: 'finished',
period: 1704070800,
percent: 100,
is_backup: 1,
is_scan: 0,
has_warnings: false,
discarded: '0', // Complete backup
stats: {
prefix: 'wp_',
plugins: { count: 100 },
themes: { count: 100 },
uploads: { count: 100 },
tables: {
wp_posts: {
post_published: 100,
},
},
},
},
],
};

jest.mock( '@wordpress/data', () => ( {
Expand Down Expand Up @@ -151,4 +225,42 @@ describe( 'useBackupsState', () => {
expect( result.current.backupState ).toBe( BACKUP_STATE.NO_GOOD_BACKUPS );
} );
} );

it( 'backupState should be NO_GOOD_BACKUPS when last backup finished as discarded', async () => {
useSelect.mockImplementation( selector => {
if ( typeof selector === 'function' ) {
return selector( () => ( {
getBackups: () => fixtures.discarded,
isFetchingBackups: () => false,
hasLoadedBackups: () => true,
} ) );
}
return [];
} );

const { result } = renderHook( () => useBackupsState() );

await waitFor( () => {
expect( result.current.backupState ).toBe( BACKUP_STATE.NO_GOOD_BACKUPS );
} );
} );

it( 'backupState should be COMPLETE by selecting the latest non-discarded finished backup', async () => {
useSelect.mockImplementation( selector => {
if ( typeof selector === 'function' ) {
return selector( () => ( {
getBackups: () => fixtures.complete_and_discarded,
isFetchingBackups: () => false,
hasLoadedBackups: () => true,
} ) );
}
return [];
} );

const { result } = renderHook( () => useBackupsState() );

await waitFor( () => {
expect( result.current.backupState ).toBe( BACKUP_STATE.COMPLETE );
} );
} );
} );
2 changes: 1 addition & 1 deletion projects/packages/backup/src/js/hooks/useBackupsState.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const useBackupsState = ( shouldPoll = false ) => {
return;
}

if ( 'finished' === backup.status && backup.stats ) {
if ( 'finished' === backup.status && backup.stats && '0' === backup.discarded ) {
latestBackup = backup;
setBackupState( BACKUP_STATE.COMPLETE );
}
Expand Down

0 comments on commit 88750ed

Please sign in to comment.