diff --git a/projects/packages/backup/changelog/fix-backup-discarded-as-successful b/projects/packages/backup/changelog/fix-backup-discarded-as-successful new file mode 100644 index 0000000000000..ab7cac4e82011 --- /dev/null +++ b/projects/packages/backup/changelog/fix-backup-discarded-as-successful @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Stop considering discarded backups as successful backups on the admin page diff --git a/projects/packages/backup/src/js/hooks/test/useBackupsState.js b/projects/packages/backup/src/js/hooks/test/useBackupsState.js index 91cdc18da682e..0df42c2b6a28c 100644 --- a/projects/packages/backup/src/js/hooks/test/useBackupsState.js +++ b/projects/packages/backup/src/js/hooks/test/useBackupsState.js @@ -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, @@ -28,6 +53,7 @@ const fixtures = { is_backup: 1, is_scan: 0, has_warnings: false, + discarded: '0', stats: { prefix: 'wp_', plugins: { count: 100 }, @@ -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', () => ( { @@ -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 ); + } ); + } ); } ); diff --git a/projects/packages/backup/src/js/hooks/useBackupsState.js b/projects/packages/backup/src/js/hooks/useBackupsState.js index 2f9f4b09604c6..0307eb36f3045 100644 --- a/projects/packages/backup/src/js/hooks/useBackupsState.js +++ b/projects/packages/backup/src/js/hooks/useBackupsState.js @@ -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 ); }