From 206480397b7e1b7773244aef088d828a2fa2c03d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Mon, 8 Apr 2024 14:51:13 +0200 Subject: [PATCH 1/3] importWxr step: Call wp_slash() on the imported post content --- .../steps/fixtures/import-wxr-slash-issue.xml | 60 ++++++++++++++++ .../src/lib/steps/import-wxr.spec.ts | 71 +++++++++++++++++++ .../blueprints/src/lib/steps/import-wxr.ts | 7 +- 3 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 packages/playground/blueprints/src/lib/steps/fixtures/import-wxr-slash-issue.xml create mode 100644 packages/playground/blueprints/src/lib/steps/import-wxr.spec.ts 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..b3ba70a374 --- /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]]> + 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..e55b48cb85 --- /dev/null +++ b/packages/playground/blueprints/src/lib/steps/import-wxr.spec.ts @@ -0,0 +1,71 @@ +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 res = await php.run({ + code: `post_content; + `, + env: { + DOCROOT: await php.documentRoot, + }, + }); + + expect(res.text).toContain(expectedPostContent); + }); +}); diff --git a/packages/playground/blueprints/src/lib/steps/import-wxr.ts b/packages/playground/blueprints/src/lib/steps/import-wxr.ts index 7d48362df1..c1c70998d3 100644 --- a/packages/playground/blueprints/src/lib/steps/import-wxr.ts +++ b/packages/playground/blueprints/src/lib/steps/import-wxr.ts @@ -39,9 +39,10 @@ export const importWxr: StepHandler> = async ( data: file, }); const docroot = await playground.documentRoot; - await playground.run({ + const res = await playground.run({ code: ` 'Administrator') )[0]; $importer = new WXR_Importer( array( 'fetch_attachments' => true, @@ -49,7 +50,11 @@ export const importWxr: StepHandler> = async ( ) ); $logger = new WP_Importer_Logger_CLI(); $importer->set_logger( $logger ); + add_filter( 'content_save_pre', function( $content ) { + return wp_slash( $content ); + }); $result = $importer->import( '/tmp/import.wxr' ); `, }); + console.log(res.text); }; From 9beaae859b396e2dbe8b628ca177a4420b59ea8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Mon, 8 Apr 2024 14:59:04 +0200 Subject: [PATCH 2/3] Slash the entire post content --- .../src/lib/steps/fixtures/import-wxr-slash-issue.xml | 2 +- .../blueprints/src/lib/steps/import-wxr.spec.ts | 11 ++++++++--- .../playground/blueprints/src/lib/steps/import-wxr.ts | 7 +++++-- 3 files changed, 14 insertions(+), 6 deletions(-) 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 index b3ba70a374..1772dfbc64 100644 --- 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 @@ -22,7 +22,7 @@ https://wordpress.org/?v=6.5 - <![CDATA[Issue]]> + <![CDATA["Issue\Issue"]]> https://playground.wordpress.net/scope:0.6224554755515266/?p=5 Mon, 08 Apr 2024 06:59:36 +0000 diff --git a/packages/playground/blueprints/src/lib/steps/import-wxr.spec.ts b/packages/playground/blueprints/src/lib/steps/import-wxr.spec.ts index e55b48cb85..1aa6dfa4ea 100644 --- a/packages/playground/blueprints/src/lib/steps/import-wxr.spec.ts +++ b/packages/playground/blueprints/src/lib/steps/import-wxr.spec.ts @@ -55,17 +55,22 @@ describe('Blueprint step importWxr', () => {
is loading ...
`; - const res = await php.run({ + const result = await php.run({ code: `post_content; + echo json_encode([ + 'post_content' => $posts[0]->post_content, + 'post_title' => $posts[0]->post_title, + ]); `, env: { DOCROOT: await php.documentRoot, }, }); + const json = result.json; - expect(res.text).toContain(expectedPostContent); + 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 c1c70998d3..5353d0c6a2 100644 --- a/packages/playground/blueprints/src/lib/steps/import-wxr.ts +++ b/packages/playground/blueprints/src/lib/steps/import-wxr.ts @@ -50,9 +50,12 @@ export const importWxr: StepHandler> = async ( ) ); $logger = new WP_Importer_Logger_CLI(); $importer->set_logger( $logger ); - add_filter( 'content_save_pre', function( $content ) { - return wp_slash( $content ); + + // 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' ); `, }); From 684142cfb29fd646ade4632a5a46dda25ea74c41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Mon, 8 Apr 2024 15:02:19 +0200 Subject: [PATCH 3/3] Remove console.log --- packages/playground/blueprints/src/lib/steps/import-wxr.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/playground/blueprints/src/lib/steps/import-wxr.ts b/packages/playground/blueprints/src/lib/steps/import-wxr.ts index 5353d0c6a2..dc1de5ee5d 100644 --- a/packages/playground/blueprints/src/lib/steps/import-wxr.ts +++ b/packages/playground/blueprints/src/lib/steps/import-wxr.ts @@ -39,7 +39,7 @@ export const importWxr: StepHandler> = async ( data: file, }); const docroot = await playground.documentRoot; - const res = await playground.run({ + await playground.run({ code: `> = async ( $result = $importer->import( '/tmp/import.wxr' ); `, }); - console.log(res.text); };