|
| 1 | +import { loadNodeRuntime } from '@php-wasm/node'; |
| 2 | +import { RecommendedPHPVersion } from '@wp-playground/common'; |
| 3 | +import { |
| 4 | + bootRequestHandler, |
| 5 | + bootWordPress, |
| 6 | + bootWordPressAndRequestHandler, |
| 7 | +} from '@wp-playground/wordpress'; |
| 8 | +import { dirname } from '@php-wasm/util'; |
| 9 | +import { |
| 10 | + getSqliteDriverModule, |
| 11 | + getWordPressModule, |
| 12 | +} from '@wp-playground/wordpress-builds'; |
| 13 | +import { getSqliteDatabasePath } from './get-sqlite-database-path'; |
| 14 | + |
| 15 | +describe('getSqliteDatabasePath', () => { |
| 16 | + it('reads the active database from WordPress environment metadata', async () => { |
| 17 | + await using handler = await bootWordPressAndRequestHandler({ |
| 18 | + createPhpRuntime: () => loadNodeRuntime(RecommendedPHPVersion), |
| 19 | + siteUrl: 'http://playground.test', |
| 20 | + wordPressZip: await getWordPressModule(), |
| 21 | + sqliteIntegrationPluginZip: await getSqliteDriverModule(), |
| 22 | + documentRoot: '/custom-root', |
| 23 | + dataSqlPath: '/custom/database.sqlite', |
| 24 | + }); |
| 25 | + const php = await handler.getPrimaryPhp(); |
| 26 | + const path = await getSqliteDatabasePath(php); |
| 27 | + expect(php.isFile(path)).toBe(true); |
| 28 | + expect(path).toBe('/custom/database.sqlite'); |
| 29 | + await php.run({ |
| 30 | + code: `<?php |
| 31 | +require getenv('DOCUMENT_ROOT') . '/wp-load.php'; |
| 32 | +update_option('playground_database_path_test', 'active database'); |
| 33 | +`, |
| 34 | + env: { DOCUMENT_ROOT: php.documentRoot }, |
| 35 | + }); |
| 36 | + const response = await php.run({ |
| 37 | + code: `<?php |
| 38 | +$pdo = new PDO('sqlite:' . getenv('DATABASE_PATH')); |
| 39 | +echo $pdo->query("SELECT option_value FROM wp_options WHERE option_name = 'playground_database_path_test'")->fetchColumn(); |
| 40 | +`, |
| 41 | + env: { DATABASE_PATH: path }, |
| 42 | + }); |
| 43 | + expect(response.errors).toBe(''); |
| 44 | + expect(response.text).toBe('active database'); |
| 45 | + }, 30_000); |
| 46 | + |
| 47 | + it.each([undefined, '/custom/database.sqlite'])( |
| 48 | + 'keeps the database accessible after a failed boot with dataSqlPath=%s', |
| 49 | + async (dataSqlPath) => { |
| 50 | + const options = { |
| 51 | + createPhpRuntime: () => loadNodeRuntime(RecommendedPHPVersion), |
| 52 | + siteUrl: 'http://playground.test', |
| 53 | + wordPressZip: await getWordPressModule(), |
| 54 | + sqliteIntegrationPluginZip: await getSqliteDriverModule(), |
| 55 | + dataSqlPath, |
| 56 | + }; |
| 57 | + await using saved = await bootWordPressAndRequestHandler(options); |
| 58 | + const source = await saved.getPrimaryPhp(); |
| 59 | + const path = await getSqliteDatabasePath(source); |
| 60 | + const database = source.readFileAsBuffer(path); |
| 61 | + await using reopened = await bootRequestHandler(options); |
| 62 | + await expect( |
| 63 | + bootWordPress(reopened, { |
| 64 | + ...options, |
| 65 | + wordpressInstallMode: |
| 66 | + 'install-from-existing-files-if-needed', |
| 67 | + hooks: { |
| 68 | + beforeDatabaseSetup: async (php) => { |
| 69 | + php.mkdir(dirname(path)); |
| 70 | + php.writeFile(path, database); |
| 71 | + php.mkdir('/wordpress/wp-content/mu-plugins'); |
| 72 | + php.writeFile( |
| 73 | + '/wordpress/wp-content/mu-plugins/broken.php', |
| 74 | + '<?php throw new Exception("A saved plugin failed");' |
| 75 | + ); |
| 76 | + }, |
| 77 | + }, |
| 78 | + }) |
| 79 | + ).rejects.toThrow('A saved plugin failed'); |
| 80 | + |
| 81 | + const php = await reopened.getPrimaryPhp(); |
| 82 | + expect(await getSqliteDatabasePath(php)).toBe(path); |
| 83 | + expect(php.readFileAsBuffer(path).byteLength).toBeGreaterThan(0); |
| 84 | + |
| 85 | + // An unrelated PHP request must not overwrite the saved database metadata. |
| 86 | + await php.run({ code: '<?php echo "No WordPress loaded";' }); |
| 87 | + expect(await getSqliteDatabasePath(php)).toBe(path); |
| 88 | + }, |
| 89 | + 30_000 |
| 90 | + ); |
| 91 | +}); |
0 commit comments