-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathinstall-plugin.spec.ts
225 lines (206 loc) · 6.25 KB
/
install-plugin.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { PHP } from '@php-wasm/universal';
import { RecommendedPHPVersion } from '@wp-playground/common';
import { installPlugin } from './install-plugin';
import { phpVar } from '@php-wasm/util';
import { PHPRequestHandler } from '@php-wasm/universal';
import { loadNodeRuntime } from '@php-wasm/node';
async function zipFiles(
php: PHP,
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();`,
});
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 handler = new PHPRequestHandler({
phpFactory: async () =>
new PHP(await loadNodeRuntime(RecommendedPHPVersion)),
documentRoot: '/wordpress',
});
const php = await handler.getPrimaryPhp();
// Create plugins folder
const rootPath = php.documentRoot;
const pluginsPath = `${rootPath}/wp-content/plugins`;
php.mkdir(pluginsPath);
// Create test plugin
const pluginName = 'test-plugin';
await installPlugin(php, {
pluginData: 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}-0.0.1`)).toBe(true);
});
});
describe('Blueprint step installPlugin', () => {
let php: PHP;
// Create plugins folder
let rootPath = '';
let pluginsPath = '';
let installedPluginPath = '';
const pluginName = 'test-plugin';
const zipFileName = `${pluginName}-0.0.1.zip`;
beforeEach(async () => {
const handler = new PHPRequestHandler({
phpFactory: async () =>
new PHP(await loadNodeRuntime(RecommendedPHPVersion)),
documentRoot: '/wordpress',
});
php = await handler.getPrimaryPhp();
rootPath = php.documentRoot;
pluginsPath = `${rootPath}/wp-content/plugins`;
php.mkdir(pluginsPath);
installedPluginPath = `${pluginsPath}/${pluginName}`;
});
it('should install a plugin', async () => {
await installPlugin(php, {
pluginData: await zipFiles(php, zipFileName, {
[`${pluginName}/index.php`]: `/**\n * Plugin Name: Test Plugin`,
}),
ifAlreadyInstalled: 'overwrite',
options: {
activate: false,
},
});
expect(php.fileExists(installedPluginPath)).toBe(true);
});
it('should install a single PHP file as a plugin', async () => {
const rawPluginContent = `<?php\n/**\n * Plugin Name: Test Plugin`;
await installPlugin(php, {
pluginData: new File(
[new TextEncoder().encode(rawPluginContent)],
'test-plugin.php'
),
ifAlreadyInstalled: 'overwrite',
options: {
activate: false,
},
});
const pluginFilePath = `${pluginsPath}/test-plugin.php`;
expect(php.fileExists(pluginFilePath)).toBe(true);
expect(php.readFileAsText(pluginFilePath)).toBe(rawPluginContent);
});
it('should install a plugin using the deprecated pluginZipFile option', async () => {
// @ts-ignore
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);
});
it('should install a plugin from a directory resource', async () => {
await installPlugin(php, {
pluginData: {
name: pluginName,
files: {
'index.php': `/**\n * Plugin Name: Test Plugin`,
},
},
ifAlreadyInstalled: 'overwrite',
options: {
activate: false,
},
});
expect(php.fileExists(installedPluginPath)).toBe(true);
});
describe('ifAlreadyInstalled option', () => {
beforeEach(async () => {
await installPlugin(php, {
pluginData: await zipFiles(php, zipFileName, {
[`${pluginName}/index.php`]: `/**\n * Plugin Name: Test Plugin`,
}),
ifAlreadyInstalled: 'overwrite',
options: {
activate: false,
},
});
});
it('ifAlreadyInstalled=overwrite should overwrite the plugin if it already exists', async () => {
// Install the plugin
await installPlugin(php, {
pluginData: 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');
});
it('ifAlreadyInstalled=skip should skip the plugin if it already exists', async () => {
// Install the plugin
await installPlugin(php, {
pluginData: 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');
});
it('ifAlreadyInstalled=error should throw an error if the plugin already exists', async () => {
// Install the plugin
await expect(
installPlugin(php, {
pluginData: await zipFiles(php, zipFileName, {
[`${pluginName}/index.php`]: `/**\n * Plugin Name: A different Plugin`,
}),
ifAlreadyInstalled: 'error',
options: {
activate: false,
},
})
).rejects.toThrowError();
});
});
describe('targetFolderName option', () => {
it('should install a plugin to expected path', async () => {
await installPlugin(php, {
pluginZipFile: await zipFiles(php, zipFileName, {
[`unexpected-path/index.php`]: `/**\n * Plugin Name: Test Plugin`,
}),
ifAlreadyInstalled: 'overwrite',
options: {
activate: false,
targetFolderName: pluginName,
},
});
expect(php.fileExists(installedPluginPath)).toBe(true);
});
});
});