Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d9543b9
Add rough import / export logic and debug input fields
artemiomorales Jan 12, 2023
53f2ea7
Move export logic to PHP
artemiomorales Jan 17, 2023
c4bdd8f
Migrate import logic to PHP
artemiomorales Jan 20, 2023
4e7813e
Add support for exporting/importing database; remove export of 'wp-in…
artemiomorales Jan 23, 2023
502271d
Remove extra spaces around PHP declarations and extraneous closing tags
artemiomorales Jan 23, 2023
3064736
Move DOCROOT const to config file
artemiomorales Jan 23, 2023
1dcdd0e
Add error handling to custom PHP run() code; rename variables
artemiomorales Jan 23, 2023
03a0a96
Install WordPress Importer by default in WP bundle
artemiomorales Jan 31, 2023
00457a5
Rebuild WordPress versions
artemiomorales Jan 31, 2023
cd720c1
Fix full site editing
artemiomorales Feb 8, 2023
9c997bb
Add UI for migration logic
artemiomorales Feb 10, 2023
fe837c0
Reset build/ folder to HEAD
artemiomorales Feb 11, 2023
e48b217
Rebuild WP bundles to include WordPress Importer
artemiomorales Feb 11, 2023
b505cf5
Add descriptive text for modal
artemiomorales Feb 11, 2023
e9c1400
Add WordPress version to export filename
artemiomorales Feb 11, 2023
6e62b40
Fix bug when using PHP 7.0-7.3
artemiomorales Feb 14, 2023
f92cb88
Move generateZipFile() code to separate PHP file; begin adding tests
artemiomorales Feb 24, 2023
6f9a3b6
Begin moving import code to separate file; add tests
artemiomorales Feb 24, 2023
5333bbd
Move import overwrite logic to separate file; add tests; fix a bug
artemiomorales Feb 24, 2023
0badcd2
Add known limitations regarding media, options/users, and plugins to …
artemiomorales Feb 24, 2023
429f07b
Replace rmSync calls with rmdirSync to remove Typscript warnings
artemiomorales Feb 24, 2023
a969224
Fix code formatting and spacing issues
artemiomorales Feb 24, 2023
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
Prev Previous commit
Next Next commit
Begin moving import code to separate file; add tests
Moved part of the import logic to separate file, modified it to
use parameters, and added many constants in the migration test file
to prevent code duplication.
  • Loading branch information
artemiomorales committed Feb 24, 2023
commit 6f9a3b6ad85907750b4c292e0ee39e65ba8e1610
218 changes: 164 additions & 54 deletions src/wordpress-playground/__tests__/migration-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,93 @@ import { existsSync, rmSync, readFileSync } from 'fs';

const { TextDecoder } = require('util');

// Code to test
const migration = readFileSync(__dirname + '/../migration.php');

// Mock files
const testDirPath = __dirname + '/__test39024';
const fileSystemPath = testDirPath + '/filesystem';
const wpImporterOutputPath = fileSystemPath + '/databaseExport.xml';
const wpPath = fileSystemPath + '/wordpress';
const wpAdminPath = wpPath + '/wp-admin';
const wpContentPath = wpPath + '/wp-content';
const wpIncludesPath = wpPath + '/wp-includes';
const wpSqlDbPath = wpContentPath + '/database';
const wpImporterOutputValue = 'Mock WordPress Importer migration file';

const exportName = `wordpress-playground-export.zip`;
const exportPath = `${testDirPath}/${exportName}`;

// Code to test
const migrationFilePath = __dirname + '/../migration.php';
// ------------------------
// Mock WordPress Directory
// ------------------------

// Root
const wpDir = fileSystemPath + '/wordpress';
const wpDirFilePath = wpDir + '/test-wp-root.php';
const wpDirFileValue = 'Mock WP root file';

// WP Admin
const wpAdminDir = wpDir + '/wp-admin';
const wpAdminFilePath = wpAdminDir + '/test-admin.php';
const wpAdminFileValue = 'Mock WP admin file';

// WP Content
const wpContentDir = wpDir + '/wp-content';
const wpContentFilePath = wpContentDir + '/test-content.php';
const wpContentFileValue = 'Mock WP content file';

// WP Content - SQL DB
const wpSqlDbDir = wpContentDir + '/database';
const wpSqlDbFilePath = wpSqlDbDir + '/test-database.php';
const wpSqlDbFileValue = 'Mock SQL content';

// WP Includes
const wpIncludesDir = wpDir + '/wp-includes';
const wpIncludesFilePath = wpIncludesDir + '/test-includes.php';
const wpIncludesFileValue = 'Mock WP includes';

function createMockStructure(phpRuntime) {
phpRuntime.mkdirTree(testDirPath);
phpRuntime.mkdirTree(fileSystemPath);
phpRuntime.mkdirTree(wpDir);

phpRuntime.writeFile(wpImporterOutputPath, wpImporterOutputValue);

// WP Root
phpRuntime.writeFile(wpDirFilePath, wpDirFileValue);

// WP Admin
phpRuntime.mkdirTree(wpAdminDir);
phpRuntime.writeFile(wpAdminFilePath, wpAdminFileValue);

// WP Content
phpRuntime.mkdirTree(wpContentDir);
phpRuntime.writeFile(wpContentFilePath, wpContentFileValue);

// WP Content - SQL DB
phpRuntime.mkdirTree(wpSqlDbDir);
phpRuntime.writeFile(wpSqlDbFilePath, wpSqlDbFileValue);

// WP Includes
phpRuntime.mkdirTree(wpIncludesDir);
phpRuntime.writeFile(wpIncludesFilePath, wpIncludesFileValue);

const exportWriteRequest = phpRuntime.run({
code:
migration +
` generateZipFile("${exportPath}", "${wpImporterOutputPath}", "${wpDir}");`,
});

if (exportWriteRequest.exitCode !== 0) {
throw exportWriteRequest.errors;
}
}

describe('generateZipFile()', () => {
let php;
let migration;

beforeEach(async () => {
php = await startPHP(phpLoaderModule, 'NODE');
if (existsSync(testDirPath)) {
rmSync(testDirPath, { recursive: true });
}
migration = readFileSync(migrationFilePath);
php.mkdirTree(testDirPath);
php.mkdirTree(fileSystemPath);
php.mkdirTree(wpPath);

php.writeFile(
wpImporterOutputPath,
'Mock WordPress Importer migration file'
);

// WP Root
php.writeFile(`${wpPath}/test-wp-root.php`, 'Mock WP root file');

// WP Admin
php.mkdirTree(wpAdminPath);
php.writeFile(`${wpAdminPath}/test-admin.php`, 'Mock WP admin file');

// WP Content
php.mkdirTree(wpContentPath);
php.writeFile(
`${wpContentPath}/test-content.php`,
'Mock WP content file'
);
php.mkdirTree(wpSqlDbPath);
php.writeFile(`${wpSqlDbPath}/test-database.php`, 'Mock SQL content');

// WP Includes
php.mkdirTree(wpIncludesPath);
php.writeFile(
`${wpIncludesPath}/test-includes.php`,
'Mock WP includes'
);

const exportWriteRequest = php.run({
code:
migration +
` generateZipFile("${exportPath}", "${wpImporterOutputPath}", "${wpPath}");`,
});

if (exportWriteRequest.exitCode !== 0) {
throw exportWriteRequest.errors;
}
createMockStructure(php);
});

afterAll(() => {
Expand All @@ -87,8 +108,8 @@ describe('generateZipFile()', () => {
if ($res === TRUE) {
if(
$zip->getFromName('${wpImporterOutputPath}') &&
$zip->getFromName('${wpPath}/test-wp-root.php') &&
$zip->getFromName('${wpContentPath}/test-content.php')
$zip->getFromName('${wpDirFilePath}') &&
$zip->getFromName('${wpContentFilePath}')
) {
echo 'success';
} else {
Expand All @@ -111,7 +132,7 @@ describe('generateZipFile()', () => {
$res = $zip->open('${exportPath}');
if ($res === TRUE) {
if(
!$zip->getFromName('${wpSqlDbPath}/test-database.php')
!$zip->getFromName('${wpSqlDbFilePath}')
) {
echo 'success';
} else {
Expand All @@ -134,7 +155,7 @@ describe('generateZipFile()', () => {
$res = $zip->open('${exportPath}');
if ($res === TRUE) {
if(
!$zip->getFromName('${wpIncludesPath}/test-includes.php')
!$zip->getFromName('${wpIncludesFilePath}')
) {
echo 'success';
} else {
Expand All @@ -149,3 +170,92 @@ describe('generateZipFile()', () => {
expect(runTestOutput).toEqual('success');
});
});

describe('readFileFromZipArchive()', () => {
let php;

beforeEach(async () => {
php = await startPHP(phpLoaderModule, 'NODE');
if (existsSync(testDirPath)) {
rmSync(testDirPath, { recursive: true });
}
createMockStructure(php);
});

afterAll(() => {
if (existsSync(testDirPath)) {
rmSync(testDirPath, { recursive: true });
}
});

describe("given a path to a generateZipFile() output, it should return a specified file's contents", () => {
it('filesystem root file', () => {
const readFileRequest = php.run({
code:
migration +
` readFileFromZipArchive('${exportPath}', '${wpImporterOutputPath}');`,
});

if (readFileRequest.exitCode !== 0) {
throw readFileRequest.errors;
}

const readFileOutput = new TextDecoder()
.decode(readFileRequest.body)
.trim();

expect(readFileOutput).toEqual(wpImporterOutputValue);
});
it('wp root file', () => {
const readFileRequest = php.run({
code:
migration +
` readFileFromZipArchive('${exportPath}', '${wpDirFilePath}');`,
});

if (readFileRequest.exitCode !== 0) {
throw readFileRequest.errors;
}

const readFileOutput = new TextDecoder()
.decode(readFileRequest.body)
.trim();

expect(readFileOutput).toEqual(wpDirFileValue);
});
it('wp admin file', () => {
const readFileRequest = php.run({
code:
migration +
` readFileFromZipArchive('${exportPath}', '${wpAdminFilePath}');`,
});

if (readFileRequest.exitCode !== 0) {
throw readFileRequest.errors;
}

const readFileOutput = new TextDecoder()
.decode(readFileRequest.body)
.trim();

expect(readFileOutput).toEqual(wpAdminFileValue);
});
it('wp content file', () => {
const readFileRequest = php.run({
code:
migration +
` readFileFromZipArchive('${exportPath}', '${wpContentFilePath}');`,
});

if (readFileRequest.exitCode !== 0) {
throw readFileRequest.errors;
}

const readFileOutput = new TextDecoder()
.decode(readFileRequest.body)
.trim();

expect(readFileOutput).toEqual(wpContentFileValue);
});
});
});
26 changes: 12 additions & 14 deletions src/wordpress-playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const importCloseModalButton = document.querySelector(
'#import-close-modal--btn'
) as HTMLButtonElement;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's about time this entire file gets migrated to (p)react – that's definitely out of scope of this PR, though


const databaseExportName = 'databaseExport.xml';
const databaseExportPath = '/' + databaseExportName;

let workerThread;

let isBooted = false;
Expand Down Expand Up @@ -347,14 +350,13 @@ async function generateZip() {
const databaseExportContent = new TextDecoder().decode(
databaseExportResponse.body
);
const databasePath = '/databaseExport.xml';
await workerThread.writeFile(databasePath, databaseExportContent);
await workerThread.writeFile(databaseExportPath, databaseExportContent);
const exportName = `wordpress-playground--wp${wpVersion}--php${phpVersion}.zip`;
const exportPath = `/${exportName}`;
const exportWriteRequest = await workerThread.run({
code:
migration +
` generateZipFile("${exportPath}", "${databasePath}", "${DOCROOT}");`,
` generateZipFile('${exportPath}', '${databaseExportPath}', '${DOCROOT}');`,
});
if (exportWriteRequest.exitCode !== 0) {
throw exportWriteRequest.errors;
Expand Down Expand Up @@ -384,19 +386,15 @@ async function importFile() {

const fileArrayBuffer = await userUploadedFile.arrayBuffer();
const fileContent = new Uint8Array(fileArrayBuffer);
await workerThread.writeFile('/import.zip', fileContent);
const importPath = '/import.zip';

await workerThread.writeFile(importPath, fileContent);

// Import the database
const databaseFromZipFileReadRequest = await workerThread.run({
code: `<?php
chmod('/import.zip', 0777);
$zip = new ZipArchive;
$res = $zip->open('/import.zip');
if ($res === TRUE) {
$file = $zip->getFromName('/databaseExport.xml');
echo $file;
}
`,
code:
migration +
` readFileFromZipArchive('${importPath}', '${databaseExportPath}');`,
});
if (databaseFromZipFileReadRequest.exitCode !== 0) {
throw databaseFromZipFileReadRequest.errors;
Expand All @@ -408,7 +406,7 @@ async function importFile() {

const databaseFile = new File(
[databaseFromZipFileContent],
'databaseExport.xml'
databaseExportName
);

const importerPageOneResponse = await workerThread.HTTPRequest({
Expand Down
10 changes: 10 additions & 0 deletions src/wordpress-playground/migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ function generateZipFile($exportPath, $databasePath, $docRoot) {
chmod($exportPath, 0777);
}
}

function readFileFromZipArchive($pathToZip, $pathToFile) {
chmod($pathToZip, 0777);
$zip = new ZipArchive;
$res = $zip->open($pathToZip);
if ($res === TRUE) {
$file = $zip->getFromName($pathToFile);
echo $file;
}
}