diff --git a/packages/playground/blueprints/src/lib/steps/fixtures/import-wxr-slash-issue.xml b/packages/playground/blueprints/src/lib/steps/fixtures/import-wxr-slash-issue.xml new file mode 100644 index 0000000000..1772dfbc64 --- /dev/null +++ b/packages/playground/blueprints/src/lib/steps/fixtures/import-wxr-slash-issue.xml @@ -0,0 +1,60 @@ + + + + My WordPress Website + https://playground.wordpress.net/scope:0.6224554755515266 + + Mon, 08 Apr 2024 07:03:05 +0000 + en-US + 1.2 + https://playground.wordpress.net/scope:0.6224554755515266 + https://playground.wordpress.net/scope:0.6224554755515266 + + 1 + + + https://wordpress.org/?v=6.5 + + + <![CDATA["Issue\Issue"]]> + https://playground.wordpress.net/scope:0.6224554755515266/?p=5 + Mon, 08 Apr 2024 06:59:36 +0000 + + https://playground.wordpress.net/scope:0.6224554755515266/?p=5 + + +
is loading ...
+]]>
+ + 5 + + + + + + + + + 0 + 0 + + + 0 + + + + + + + + + +
+
+
\ No newline at end of file diff --git a/packages/playground/blueprints/src/lib/steps/import-wxr.spec.ts b/packages/playground/blueprints/src/lib/steps/import-wxr.spec.ts new file mode 100644 index 0000000000..1aa6dfa4ea --- /dev/null +++ b/packages/playground/blueprints/src/lib/steps/import-wxr.spec.ts @@ -0,0 +1,76 @@ +import { NodePHP } from '@php-wasm/node'; +import { + RecommendedPHPVersion, + getWordPressModule, +} from '@wp-playground/wordpress'; +import { importWxr } from './import-wxr'; +import { readFile } from 'fs/promises'; +import { unzip } from './unzip'; +import { installPlugin } from './install-plugin'; + +describe('Blueprint step importWxr', () => { + let php: NodePHP; + beforeEach(async () => { + php = await NodePHP.load(RecommendedPHPVersion, { + requestHandler: { + documentRoot: '/wordpress', + }, + }); + + await unzip(php, { + zipFile: await getWordPressModule(), + extractToPath: '/wordpress', + }); + + // Delete all posts + await php.run({ + code: `ID, true); + } + `, + }); + + // Install the WordPress importer plugin + const pluginZipData = await readFile( + __dirname + '/../../../../website/public/wordpress-importer.zip' + ); + const pluginZipFile = new File([pluginZipData], 'plugin.zip'); + await installPlugin(php, { + pluginZipFile, + }); + }); + + it('Should import a WXR file with JSON-encoded UTF-8 characters', async () => { + const fileData = await readFile( + __dirname + '/fixtures/import-wxr-slash-issue.xml' + ); + const file = new File([fileData], 'import.wxr'); + + await importWxr(php, { file }); + + const expectedPostContent = ` +
is loading ...
+`; + + const result = await php.run({ + code: ` $posts[0]->post_content, + 'post_title' => $posts[0]->post_title, + ]); + `, + env: { + DOCROOT: await php.documentRoot, + }, + }); + const json = result.json; + + expect(json.post_content).toEqual(expectedPostContent); + expect(json.post_title).toEqual(`"Issue\\Issue"`); + }); +}); diff --git a/packages/playground/blueprints/src/lib/steps/import-wxr.ts b/packages/playground/blueprints/src/lib/steps/import-wxr.ts index 7d48362df1..dc1de5ee5d 100644 --- a/packages/playground/blueprints/src/lib/steps/import-wxr.ts +++ b/packages/playground/blueprints/src/lib/steps/import-wxr.ts @@ -42,6 +42,7 @@ export const importWxr: StepHandler> = async ( await playground.run({ code: ` 'Administrator') )[0]; $importer = new WXR_Importer( array( 'fetch_attachments' => true, @@ -49,6 +50,12 @@ export const importWxr: StepHandler> = async ( ) ); $logger = new WP_Importer_Logger_CLI(); $importer->set_logger( $logger ); + + // Slashes from the imported content are lost if we don't call wp_slash here. + add_action( 'wp_insert_post_data', function( $data ) { + return wp_slash($data); + }); + $result = $importer->import( '/tmp/import.wxr' ); `, });