-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Blueprints: Add ifAlreadyInstalled to installPlugin and installTheme steps #1244
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 133 additions & 87 deletions
220
packages/playground/blueprints/src/lib/steps/install-plugin.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,159 @@ | ||
import { NodePHP } from '@php-wasm/node'; | ||
import { compileBlueprint, runBlueprintSteps } from '../compile'; | ||
import { RecommendedPHPVersion } from '@wp-playground/wordpress'; | ||
import { installPlugin } from './install-plugin'; | ||
import { phpVar } from '@php-wasm/util'; | ||
|
||
async function zipFiles( | ||
php: NodePHP, | ||
fileName: string, | ||
files: Record<string, string> | ||
) { | ||
const zipFileName = 'test.zip'; | ||
const zipFilePath = `/${zipFileName}`; | ||
|
||
await php.run({ | ||
code: `<?php $zip = new ZipArchive(); | ||
$zip->open("${zipFileName}", ZIPARCHIVE::CREATE); | ||
$files = ${phpVar(files)}; | ||
foreach($files as $path => $content) { | ||
$zip->addFromString($path, $content); | ||
} | ||
$zip->close();`, | ||
}); | ||
|
||
describe('Blueprint step installPlugin', () => { | ||
let php: NodePHP; | ||
beforeEach(async () => { | ||
php = await NodePHP.load(RecommendedPHPVersion, { | ||
const zip = await php.readFileAsBuffer(zipFilePath); | ||
php.unlink(zipFilePath); | ||
return new File([zip], fileName); | ||
} | ||
|
||
describe('Blueprint step installPlugin – without a root-level folder', () => { | ||
it('should install a plugin even when it is zipped directly without a root-level folder', async () => { | ||
const php = await NodePHP.load(RecommendedPHPVersion, { | ||
requestHandler: { | ||
documentRoot: '/wordpress', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should install a plugin', async () => { | ||
// Create test plugin | ||
const pluginName = 'test-plugin'; | ||
|
||
php.mkdir(`/${pluginName}`); | ||
php.writeFile( | ||
`/${pluginName}/index.php`, | ||
`/**\n * Plugin Name: Test Plugin` | ||
); | ||
|
||
// Note the package name is different from plugin folder name | ||
const zipFileName = `${pluginName}-0.0.1.zip`; | ||
|
||
await php.run({ | ||
code: `<?php $zip = new ZipArchive(); | ||
$zip->open("${zipFileName}", ZIPARCHIVE::CREATE); | ||
$zip->addFile("/${pluginName}/index.php"); | ||
$zip->close();`, | ||
}); | ||
|
||
php.rmdir(`/${pluginName}`); | ||
|
||
expect(php.fileExists(zipFileName)).toBe(true); | ||
|
||
// Create plugins folder | ||
const rootPath = await php.documentRoot; | ||
const rootPath = php.documentRoot; | ||
const pluginsPath = `${rootPath}/wp-content/plugins`; | ||
|
||
php.mkdir(pluginsPath); | ||
|
||
await runBlueprintSteps( | ||
compileBlueprint({ | ||
steps: [ | ||
{ | ||
step: 'installPlugin', | ||
pluginZipFile: { | ||
resource: 'vfs', | ||
path: zipFileName, | ||
}, | ||
options: { | ||
activate: false, | ||
}, | ||
}, | ||
], | ||
}), | ||
php | ||
); | ||
// Create test plugin | ||
const pluginName = 'test-plugin'; | ||
|
||
php.unlink(zipFileName); | ||
await installPlugin(php, { | ||
pluginZipFile: await zipFiles( | ||
php, | ||
// Note the ZIP filename is different from plugin folder name | ||
`${pluginName}-0.0.1.zip`, | ||
{ | ||
'index.php': `/**\n * Plugin Name: Test Plugin`, | ||
} | ||
), | ||
ifAlreadyInstalled: 'overwrite', | ||
options: { | ||
activate: false, | ||
}, | ||
}); | ||
|
||
expect(php.fileExists(`${pluginsPath}/${pluginName}`)).toBe(true); | ||
expect(php.fileExists(`${pluginsPath}/${pluginName}-0.0.1`)).toBe(true); | ||
}); | ||
}); | ||
|
||
it('should install a plugin even when it is zipped directly without a root-level folder', async () => { | ||
// Create test plugin | ||
const pluginName = 'test-plugin'; | ||
|
||
php.writeFile('/index.php', `/**\n * Plugin Name: Test Plugin`); | ||
describe('Blueprint step installPlugin', () => { | ||
let php: NodePHP; | ||
// Create plugins folder | ||
let rootPath = ''; | ||
let installedPluginPath = ''; | ||
const pluginName = 'test-plugin'; | ||
const zipFileName = `${pluginName}-0.0.1.zip`; | ||
beforeEach(async () => { | ||
php = await NodePHP.load(RecommendedPHPVersion, { | ||
requestHandler: { | ||
documentRoot: '/wordpress', | ||
}, | ||
}); | ||
rootPath = php.documentRoot; | ||
php.mkdir(`${rootPath}/wp-content/plugins`); | ||
installedPluginPath = `${rootPath}/wp-content/plugins/${pluginName}`; | ||
}); | ||
|
||
// Note the package name is different from plugin folder name | ||
const zipFileName = `${pluginName}-0.0.1.zip`; | ||
afterEach(() => { | ||
php.exit(); | ||
}); | ||
|
||
await php.run({ | ||
code: `<?php $zip = new ZipArchive(); | ||
$zip->open("${zipFileName}", ZIPARCHIVE::CREATE); | ||
$zip->addFile("/index.php"); | ||
$zip->close();`, | ||
it('should install a plugin', async () => { | ||
await installPlugin(php, { | ||
pluginZipFile: await zipFiles(php, zipFileName, { | ||
[`${pluginName}/index.php`]: `/**\n * Plugin Name: Test Plugin`, | ||
}), | ||
ifAlreadyInstalled: 'overwrite', | ||
options: { | ||
activate: false, | ||
}, | ||
}); | ||
expect(php.fileExists(installedPluginPath)).toBe(true); | ||
}); | ||
|
||
expect(php.fileExists(zipFileName)).toBe(true); | ||
describe('ifAlreadyInstalled option', () => { | ||
beforeEach(async () => { | ||
await installPlugin(php, { | ||
pluginZipFile: await zipFiles(php, zipFileName, { | ||
[`${pluginName}/index.php`]: `/**\n * Plugin Name: Test Plugin`, | ||
}), | ||
ifAlreadyInstalled: 'overwrite', | ||
options: { | ||
activate: false, | ||
}, | ||
}); | ||
}); | ||
|
||
// Create plugins folder | ||
const rootPath = await php.documentRoot; | ||
const pluginsPath = `${rootPath}/wp-content/plugins`; | ||
it('ifAlreadyInstalled=overwrite should overwrite the plugin if it already exists', async () => { | ||
// Install the plugin | ||
await installPlugin(php, { | ||
pluginZipFile: await zipFiles(php, zipFileName, { | ||
[`${pluginName}/index.php`]: `/**\n * Plugin Name: A different Plugin`, | ||
}), | ||
ifAlreadyInstalled: 'overwrite', | ||
options: { | ||
activate: false, | ||
}, | ||
}); | ||
expect( | ||
php.readFileAsText(`${installedPluginPath}/index.php`) | ||
).toContain('Plugin Name: A different Plugin'); | ||
}); | ||
|
||
php.mkdir(pluginsPath); | ||
it('ifAlreadyInstalled=skip should skip the plugin if it already exists', async () => { | ||
// Install the plugin | ||
await installPlugin(php, { | ||
pluginZipFile: await zipFiles(php, zipFileName, { | ||
[`${pluginName}/index.php`]: `/**\n * Plugin Name: A different Plugin`, | ||
}), | ||
ifAlreadyInstalled: 'skip', | ||
options: { | ||
activate: false, | ||
}, | ||
}); | ||
expect( | ||
php.readFileAsText(`${installedPluginPath}/index.php`) | ||
).toContain('Plugin Name: Test Plugin'); | ||
}); | ||
|
||
await runBlueprintSteps( | ||
compileBlueprint({ | ||
steps: [ | ||
{ | ||
step: 'installPlugin', | ||
pluginZipFile: { | ||
resource: 'vfs', | ||
path: zipFileName, | ||
}, | ||
options: { | ||
activate: false, | ||
}, | ||
it('ifAlreadyInstalled=error should throw an error if the plugin already exists', async () => { | ||
// Install the plugin | ||
await expect( | ||
installPlugin(php, { | ||
pluginZipFile: await zipFiles(php, zipFileName, { | ||
[`${pluginName}/index.php`]: `/**\n * Plugin Name: A different Plugin`, | ||
}), | ||
ifAlreadyInstalled: 'error', | ||
options: { | ||
activate: false, | ||
}, | ||
], | ||
}), | ||
php | ||
); | ||
|
||
php.unlink(zipFileName); | ||
expect(php.fileExists(`${pluginsPath}/${pluginName}-0.0.1`)).toBe(true); | ||
}) | ||
).rejects.toThrowError(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plugins can be a file, for example, Hello Dolly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good spot!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, that's not a big deal in this case because we always assume a folder name when unzipping a plugin. If the zip contains just a
hello-dolly.php
file, we'll make up a folder name. It's not perfect, but that's how it works today and changing it is outside of scope of this PR. I'm happy to find a better solution in Blueprints v2.