-
Notifications
You must be signed in to change notification settings - Fork 406
Define DB_NAME via auto-prepend instead of rewriting wp-config.php #3458
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,99 +7,89 @@ import { joinPaths } from '@php-wasm/util'; | |
|
|
||
| const documentRoot = '/tmp'; | ||
| const wpConfigPath = joinPaths(documentRoot, 'wp-config.php'); | ||
| const constsJsonPath = '/internal/shared/consts.json'; | ||
|
|
||
| function getDefinedConstants(php: PHP): Record<string, unknown> { | ||
| if (!php.fileExists(constsJsonPath)) { | ||
| return {}; | ||
| } | ||
| return JSON.parse(php.readFileAsText(constsJsonPath)); | ||
| } | ||
|
|
||
| /* | ||
| * Tests below execute the rewritten wp-config.php and assert on | ||
| * the JSON output, not just on define() substrings. This proves | ||
| * the file still parses and runs, constants have the expected | ||
| * runtime values, and no warnings or errors were introduced. | ||
| */ | ||
| describe('ensureWpConfig', () => { | ||
| let php: PHP; | ||
| beforeEach(async () => { | ||
| php = new PHP(await loadNodeRuntime(RecommendedPHPVersion)); | ||
| }); | ||
|
|
||
| it('should define required constants when they are missing', async () => { | ||
| it('should define DB_NAME via defineConstant when wp-config.php does not define it', async () => { | ||
| php.writeFile(wpConfigPath, `<?php`); | ||
| await ensureWpConfig(php, documentRoot); | ||
|
|
||
| // wp-config.php should not be modified. | ||
| expect(php.readFileAsText(wpConfigPath)).toBe(`<?php`); | ||
|
|
||
| // DB_NAME should be defined via the auto-prepend mechanism. | ||
| expect(getDefinedConstants(php)).toHaveProperty('DB_NAME', 'wordpress'); | ||
| }); | ||
|
Comment on lines
+25
to
+41
|
||
|
|
||
| it('should not define DB_NAME when wp-config.php already defines it', async () => { | ||
| php.writeFile( | ||
| wpConfigPath, | ||
| `<?php | ||
| echo json_encode([ | ||
| 'DB_NAME' => DB_NAME, | ||
| ]);` | ||
| define( 'DB_NAME', 'custom-db' );` | ||
| ); | ||
| await ensureWpConfig(php, documentRoot); | ||
|
|
||
| const rewritten = php.readFileAsText(wpConfigPath); | ||
| expect(rewritten).toContain(`define( 'DB_NAME', 'wordpress' );`); | ||
| // wp-config.php should not be modified. | ||
| expect(php.readFileAsText(wpConfigPath)).toContain( | ||
| `define( 'DB_NAME', 'custom-db' );` | ||
| ); | ||
|
|
||
| const response = await php.run({ code: rewritten }); | ||
| expect(response.json).toEqual({ | ||
| DB_NAME: 'wordpress', | ||
| }); | ||
| // DB_NAME should not be in consts.json. | ||
| expect(getDefinedConstants(php)).not.toHaveProperty('DB_NAME'); | ||
| }); | ||
|
|
||
| it('should only define missing constants', async () => { | ||
| it('should not define DB_NAME when wp-config.php defines it conditionally', async () => { | ||
| php.writeFile( | ||
| wpConfigPath, | ||
| `<?php | ||
| define( 'DB_USER', 'unchanged' ); | ||
| define( 'AUTH_KEY', 'unchanged' ); | ||
| define( 'WP_DEBUG', true ); | ||
| echo json_encode([ | ||
| 'DB_NAME' => DB_NAME, | ||
| 'DB_USER' => DB_USER, | ||
| 'AUTH_KEY' => AUTH_KEY, | ||
| 'WP_DEBUG' => WP_DEBUG, | ||
| ]);` | ||
| if(!defined('DB_NAME')) { | ||
| define('DB_NAME','defined-conditionally'); | ||
| }` | ||
| ); | ||
| await ensureWpConfig(php, documentRoot); | ||
|
|
||
| const rewritten = php.readFileAsText(wpConfigPath); | ||
| expect(rewritten).toContain(`define( 'DB_NAME', 'wordpress' );`); | ||
| expect(rewritten).toContain(`define( 'DB_USER', 'unchanged' );`); | ||
| expect(rewritten).not.toContain( | ||
| `define( 'DB_USER', 'username_here' );` | ||
| // wp-config.php should not be modified. | ||
| expect(php.readFileAsText(wpConfigPath)).toContain( | ||
| `define('DB_NAME','defined-conditionally');` | ||
| ); | ||
| expect(rewritten).toContain(`define( 'AUTH_KEY', 'unchanged' );`); | ||
| expect(rewritten).not.toContain( | ||
| `define( 'AUTH_KEY', 'put your unique phrase here' );` | ||
| ); | ||
| expect(rewritten).toContain(`define( 'WP_DEBUG', true );`); | ||
| expect(rewritten).not.toContain(`define( 'WP_DEBUG', false );`); | ||
|
|
||
| const response = await php.run({ code: rewritten }); | ||
| expect(response.json).toEqual({ | ||
| DB_NAME: 'wordpress', | ||
| DB_USER: 'unchanged', | ||
| AUTH_KEY: 'unchanged', | ||
| WP_DEBUG: true, | ||
| }); | ||
| // DB_NAME should not be in consts.json. | ||
| expect(getDefinedConstants(php)).not.toHaveProperty('DB_NAME'); | ||
| }); | ||
|
|
||
| it('should not define required constants when they are already defined conditionally', async () => { | ||
| it('should only define missing constants and preserve pre-existing ones', async () => { | ||
| php.writeFile( | ||
| wpConfigPath, | ||
| `<?php | ||
| if(!defined('DB_NAME')) { | ||
| define('DB_NAME','defined-conditionally'); | ||
| } | ||
| echo json_encode([ | ||
| 'DB_NAME' => DB_NAME, | ||
| ]);` | ||
| define( 'DB_NAME', 'custom-db' );` | ||
| ); | ||
| php.defineConstant('WP_HOME', 'http://example.com'); | ||
| await ensureWpConfig(php, documentRoot); | ||
|
|
||
| const rewritten = php.readFileAsText(wpConfigPath); | ||
| expect(rewritten).not.toContain(`define( 'DB_NAME', 'wordpress' );`); | ||
|
|
||
| const response = await php.run({ code: rewritten }); | ||
| expect(response.json).toEqual({ | ||
| DB_NAME: 'defined-conditionally', | ||
| }); | ||
| const consts = getDefinedConstants(php); | ||
| expect(consts).not.toHaveProperty('DB_NAME'); | ||
| expect(consts).toHaveProperty('WP_HOME', 'http://example.com'); | ||
| }); | ||
| }); | ||
|
|
||
| /* | ||
| * Tests below execute the rewritten wp-config.php and assert on | ||
| * the JSON output, not just on define() substrings. This proves | ||
| * the file still parses and runs, constants have the expected | ||
| * runtime values, and no warnings or errors were introduced. | ||
| */ | ||
| describe('defineWpConfigConstants', () => { | ||
| let php: PHP; | ||
| beforeEach(async () => { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.