@@ -11,6 +11,7 @@ import { exec } from 'node:child_process';
11
11
import {
12
12
mkdirSync ,
13
13
readdirSync ,
14
+ readFileSync ,
14
15
writeFileSync ,
15
16
symlinkSync ,
16
17
unlinkSync ,
@@ -131,6 +132,129 @@ describe.each(blueprintVersions)(
131
132
expect ( response . text ) . toContain ( oldestSupportedVersion ) ;
132
133
} ) ;
133
134
135
+ test ( 'should add missing constants to wp-config.php' , async ( ) => {
136
+ const tmpDir = await mkdtemp (
137
+ path . join ( tmpdir ( ) , 'playground-test-' )
138
+ ) ;
139
+
140
+ const args : RunCLIArgs = {
141
+ ...suiteCliArgs ,
142
+ command : 'server' ,
143
+ 'mount-before-install' : [
144
+ {
145
+ hostPath : tmpDir ,
146
+ vfsPath : '/wordpress' ,
147
+ } ,
148
+ ] ,
149
+ mode : 'create-new-site' ,
150
+ } ;
151
+
152
+ const newSiteArgs : RunCLIArgs =
153
+ version === 2
154
+ ? {
155
+ ...args ,
156
+ 'experimental-blueprints-v2-runner' : true ,
157
+ mode : 'create-new-site' ,
158
+ }
159
+ : args ;
160
+
161
+ const existingSiteArgs : RunCLIArgs =
162
+ version === 2
163
+ ? {
164
+ ...args ,
165
+ 'experimental-blueprints-v2-runner' : true ,
166
+ mode : 'apply-to-existing-site' ,
167
+ }
168
+ : {
169
+ ...args ,
170
+ skipWordPressSetup : true ,
171
+ } ;
172
+
173
+ // Create a new site so we can load it as an existing site later.
174
+ cliServer = await runCLI ( newSiteArgs ) ;
175
+ const wpConfigPath = path . join ( tmpDir , 'wp-config.php' ) ;
176
+ let wpConfig = readFileSync ( wpConfigPath , 'utf8' ) ;
177
+ expect ( wpConfig ) . toContain (
178
+ "define( 'DB_NAME', 'database_name_here' );"
179
+ ) ;
180
+ expect ( wpConfig ) . not . toContain (
181
+ 'BEGIN: Added by WordPress Playground.'
182
+ ) ;
183
+ expect ( wpConfig ) . not . toContain (
184
+ 'END: Added by WordPress Playground.'
185
+ ) ;
186
+
187
+ // Remove the "DB_NAME" constant.
188
+ writeFileSync (
189
+ wpConfigPath ,
190
+ wpConfig . replace ( "'DB_NAME'" , "'UNKNOWN_CONSTANT'" )
191
+ ) ;
192
+ wpConfig = readFileSync ( wpConfigPath , 'utf8' ) ;
193
+ expect ( wpConfig ) . not . toContain (
194
+ "define( 'DB_NAME', 'database_name_here' );"
195
+ ) ;
196
+
197
+ // Use the existing site and confirm the missing constant is added.
198
+ cliServer = await runCLI ( existingSiteArgs ) ;
199
+ wpConfig = readFileSync ( wpConfigPath , 'utf8' ) ;
200
+ expect ( wpConfig ) . toContain (
201
+ "define( 'DB_NAME', 'database_name_here' );"
202
+ ) ;
203
+ expect ( wpConfig ) . toContain ( 'BEGIN: Added by WordPress Playground.' ) ;
204
+ expect ( wpConfig ) . toContain ( 'END: Added by WordPress Playground.' ) ;
205
+
206
+ // Ensure the "--wp-config-default-constants" argument works as well.
207
+ try {
208
+ cliServer = await runCLI ( {
209
+ ...existingSiteArgs ,
210
+ wpConfigDefaultConstants : {
211
+ DB_NAME : 'test_database_name' ,
212
+ CUSTOM_CONSTANT : 'test_custom_constant' ,
213
+ } ,
214
+ } ) ;
215
+ } catch ( _ ) {
216
+ // eslint-disable-line @typescript-eslint/no-unused-vars
217
+ // The boot will fail due to incorrect database name,
218
+ // but the wp-config.php file should be updated.
219
+ }
220
+
221
+ wpConfig = readFileSync ( wpConfigPath , 'utf8' ) ;
222
+ expect ( wpConfig ) . not . toContain (
223
+ "define( 'DB_NAME', 'database_name_here' );"
224
+ ) ;
225
+ expect ( wpConfig ) . toContain (
226
+ "define( 'DB_NAME', 'test_database_name' );"
227
+ ) ;
228
+ expect ( wpConfig ) . toContain (
229
+ "define( 'CUSTOM_CONSTANT', 'test_custom_constant' );"
230
+ ) ;
231
+ expect ( wpConfig ) . toContain ( 'BEGIN: Added by WordPress Playground.' ) ;
232
+ expect ( wpConfig ) . toContain ( 'END: Added by WordPress Playground.' ) ;
233
+
234
+ // Ensure the injected constants are removed when no longer needed.
235
+ writeFileSync (
236
+ wpConfigPath ,
237
+ wpConfig . replace ( "'UNKNOWN_CONSTANT'" , "'DB_NAME'" )
238
+ ) ;
239
+ await runCLI ( existingSiteArgs ) ;
240
+ wpConfig = readFileSync ( wpConfigPath , 'utf8' ) ;
241
+ expect ( wpConfig ) . toContain (
242
+ "define( 'DB_NAME', 'database_name_here' );"
243
+ ) ;
244
+ expect ( wpConfig ) . not . toContain (
245
+ "define( 'DB_NAME', 'test_database_name' );"
246
+ ) ;
247
+ expect ( wpConfig ) . not . toContain (
248
+ "define( 'CUSTOM_CONSTANT', 'test_custom_constant' );"
249
+ ) ;
250
+ expect ( wpConfig ) . not . toContain (
251
+ 'BEGIN: Added by WordPress Playground.'
252
+ ) ;
253
+ expect ( wpConfig ) . not . toContain (
254
+ 'END: Added by WordPress Playground.'
255
+ ) ;
256
+ } ) ;
257
+
134
258
test ( 'should run blueprint' , async ( ) => {
135
259
cliServer = await runCLI ( {
136
260
...suiteCliArgs ,
0 commit comments