Skip to content

Commit

Permalink
Add tests for Local importer
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekn committed Aug 7, 2024
1 parent 77a342a commit 1df6154
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 6 deletions.
103 changes: 103 additions & 0 deletions src/lib/import-export/tests/import/importer/local-importer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// To run tests, execute `npm run test -- src/lib/import-export/tests/import/importer/local-importer.test.ts`
import * as fs from 'fs/promises';
import { lstat, rename } from 'fs-extra';
import { SiteServer } from '../../../../../site-server';
import { LocalImporter } from '../../../import/importers';
import { BackupContents } from '../../../import/types';

jest.mock( 'fs/promises' );
jest.mock( '../../../../../site-server' );
jest.mock( 'fs-extra' );

describe( 'localImporter', () => {
const mockBackupContents: BackupContents = {
extractionDirectory: '/tmp/extracted',
sqlFiles: [ '/tmp/extracted/app/sql/local.sql', '/tmp/extracted/app/sql/local.sql' ],
wpContent: {
uploads: [ '/tmp/extracted/app/public/wp-content/uploads/2023/image.jpg' ],
plugins: [ '/tmp/extracted/app/public/wp-content/plugins/jetpack/jetpack.php' ],
themes: [ '/tmp/extracted/app/public/wp-content/themes/twentytwentyone/style.css' ],
},
metaFile: '/tmp/extracted/local-site.json',
};

const mockStudioSitePath = '/path/to/studio/site';
const mockStudioSiteId = '123';

beforeEach( () => {
jest.clearAllMocks();

( SiteServer.get as jest.Mock ).mockReturnValue( {
details: { path: '/path/to/site' },
executeWpCliCommand: jest.fn().mockReturnValue( { stderr: null } ),
} );

// mock rename
( rename as jest.Mock ).mockResolvedValue( null );

jest.useFakeTimers();
jest.setSystemTime( new Date( '2024-08-01T12:00:00Z' ) );

( lstat as jest.Mock ).mockResolvedValue( {
isDirectory: jest.fn().mockReturnValue( false ),
} );
} );

afterAll( () => {
jest.useRealTimers();
} );

describe( 'import', () => {
it( 'should copy wp-content files and read meta file', async () => {
const importer = new LocalImporter( mockBackupContents );
( fs.mkdir as jest.Mock ).mockResolvedValue( undefined );
( fs.copyFile as jest.Mock ).mockResolvedValue( undefined );
( fs.readFile as jest.Mock ).mockResolvedValue(
JSON.stringify( {
services: {
php: {
version: '8.2.23',
},
},
} )
);

const result = await importer.import( mockStudioSitePath, mockStudioSiteId );

expect( result?.meta?.phpVersion ).toBe( '8.2' );

expect( fs.mkdir ).toHaveBeenCalled();
expect( fs.copyFile ).toHaveBeenCalledTimes( 3 ); // One for each wp-content file
expect( fs.readFile ).toHaveBeenCalledWith( '/tmp/extracted/local-site.json', 'utf-8' );
} );

it( 'should handle missing meta file', async () => {
const importer = new LocalImporter( { ...mockBackupContents, metaFile: undefined } );
( fs.mkdir as jest.Mock ).mockResolvedValue( undefined );
( fs.copyFile as jest.Mock ).mockResolvedValue( undefined );

const result = await importer.import( mockStudioSitePath, mockStudioSiteId );

expect( result?.meta?.phpVersion ).toBe( undefined );

expect( fs.mkdir ).toHaveBeenCalled();
expect( fs.copyFile ).toHaveBeenCalledTimes( 3 );
expect( fs.readFile ).not.toHaveBeenCalled();
} );

it( 'should handle JSON parse error in meta file', async () => {
const importer = new LocalImporter( mockBackupContents );
( fs.mkdir as jest.Mock ).mockResolvedValue( undefined );
( fs.copyFile as jest.Mock ).mockResolvedValue( undefined );
( fs.readFile as jest.Mock ).mockResolvedValue( 'Invalid JSON' );

await expect(
importer.import( mockStudioSitePath, mockStudioSiteId )
).resolves.not.toThrow();

expect( fs.mkdir ).toHaveBeenCalled();
expect( fs.copyFile ).toHaveBeenCalledTimes( 3 );
expect( fs.readFile ).toHaveBeenCalledWith( '/tmp/extracted/local-site.json', 'utf-8' );
} );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe( 'LocalValidator', () => {
describe( 'canHandle', () => {
it( 'should return true for valid Local backup structure', () => {
const fileList = [
'app/sql/wp_options.sql',
'app/sql/local.sql',
'app/public/wp-content/uploads/2023/image.jpg',
'app/public/wp-content/plugins/jetpack/jetpack.php',
'app/public/wp-content/themes/twentytwentyone/style.css',
Expand All @@ -17,7 +17,7 @@ describe( 'LocalValidator', () => {

it( 'should not fail if core files exists.', () => {
const fileList = [
'app/sql/wp_options.sql',
'app/sql/local.sql',
'app/public/wp-admin/wp-admin.php',
'app/public/wp-admin/about.php',
'app/public/wp-includes/test.php',
Expand All @@ -37,7 +37,7 @@ describe( 'LocalValidator', () => {
describe( 'parseBackupContents', () => {
it( 'should correctly parse backup contents', () => {
const fileList = [
'app/sql/wp_options.sql',
'app/sql/local.sql',
'app/public/wp-content/uploads/2023/image.jpg',
'app/public/wp-content/plugins/jetpack/jetpack.php',
'app/public/wp-content/themes/twentytwentyone/style.css',
Expand All @@ -48,7 +48,7 @@ describe( 'LocalValidator', () => {

expect( result ).toEqual( {
extractionDirectory,
sqlFiles: [ '/tmp/extracted/app/sql/wp_options.sql' ],
sqlFiles: [ '/tmp/extracted/app/sql/local.sql' ],
wpContent: {
uploads: [ '/tmp/extracted/app/public/wp-content/uploads/2023/image.jpg' ],
plugins: [ '/tmp/extracted/app/public/wp-content/plugins/jetpack/jetpack.php' ],
Expand All @@ -60,7 +60,7 @@ describe( 'LocalValidator', () => {

it( 'should ignore files that not needed', () => {
const fileList = [
'app/sql/wp_options.sql',
'app/sql/local.sql',
'app/public/wp-admin/wp-admin.php',
'app/public/wp-admin/about.php',
'app/public/wp-includes/test.php',
Expand All @@ -75,7 +75,7 @@ describe( 'LocalValidator', () => {

expect( result ).toEqual( {
extractionDirectory,
sqlFiles: [ '/tmp/extracted/app/sql/wp_options.sql' ],
sqlFiles: [ '/tmp/extracted/app/sql/local.sql' ],
wpContent: {
uploads: [ '/tmp/extracted/app/public/wp-content/uploads/2023/image.jpg' ],
plugins: [ '/tmp/extracted/app/public/wp-content/plugins/jetpack/jetpack.php' ],
Expand Down

0 comments on commit 1df6154

Please sign in to comment.