@@ -185,8 +185,8 @@ public function test_filter_import_mastodon_post_data_with_paragraphs() {
185185 'post_content ' => '<p>First paragraph</p><p>Second paragraph</p> ' ,
186186 );
187187
188- $ post = ( object ) array (
189- 'object ' => ( object ) array (
188+ $ post = array (
189+ 'object ' => array (
190190 'inReplyTo ' => null ,
191191 ),
192192 );
@@ -207,8 +207,8 @@ public function test_filter_import_mastodon_post_data_with_reply() {
207207 );
208208
209209 $ reply_url = 'https://mastodon.social/@user/123456 ' ;
210- $ post = ( object ) array (
211- 'object ' => ( object ) array (
210+ $ post = array (
211+ 'object ' => array (
212212 'inReplyTo ' => $ reply_url ,
213213 ),
214214 );
@@ -219,6 +219,194 @@ public function test_filter_import_mastodon_post_data_with_reply() {
219219 $ this ->assertStringContainsString ( "<!-- wp:paragraph --> \n<p>This is a reply</p> \n<!-- /wp:paragraph --> " , $ result ['post_content ' ] );
220220 }
221221
222+ /**
223+ * Test filter_import_mastodon_post_data without inReplyTo field.
224+ *
225+ * @covers ::filter_import_mastodon_post_data
226+ */
227+ public function test_filter_import_mastodon_post_data_without_inreplyto () {
228+ $ data = array (
229+ 'post_content ' => '<p>Regular post without reply</p> ' ,
230+ );
231+
232+ $ post = array (
233+ 'object ' => array (
234+ // No inReplyTo field.
235+ ),
236+ );
237+
238+ $ result = Blocks::filter_import_mastodon_post_data ( $ data , $ post );
239+
240+ $ this ->assertStringNotContainsString ( 'wp:activitypub/reply ' , $ result ['post_content ' ], 'Should not add reply block when no inReplyTo ' );
241+ $ this ->assertStringContainsString ( "<!-- wp:paragraph --> \n<p>Regular post without reply</p> \n<!-- /wp:paragraph --> " , $ result ['post_content ' ] );
242+ }
243+
244+ /**
245+ * Test filter_import_mastodon_post_data with multiple paragraphs and a reply.
246+ *
247+ * @covers ::filter_import_mastodon_post_data
248+ */
249+ public function test_filter_import_mastodon_post_data_with_multiple_paragraphs_and_reply () {
250+ $ data = array (
251+ 'post_content ' => '<p>First paragraph</p><p>Second paragraph</p><p>Third paragraph</p> ' ,
252+ );
253+
254+ $ reply_url = 'https://mastodon.social/@alice/789 ' ;
255+ $ post = array (
256+ 'object ' => array (
257+ 'inReplyTo ' => $ reply_url ,
258+ ),
259+ );
260+
261+ $ result = Blocks::filter_import_mastodon_post_data ( $ data , $ post );
262+
263+ // Should have reply block at the start.
264+ $ this ->assertStringStartsWith ( '<!-- wp:activitypub/reply ' , $ result ['post_content ' ], 'Reply block should be at the start ' );
265+
266+ // Should have all three paragraphs as blocks.
267+ $ this ->assertStringContainsString ( '<!-- wp:paragraph --> ' , $ result ['post_content ' ] );
268+ $ this ->assertSame ( 3 , substr_count ( $ result ['post_content ' ], '<!-- wp:paragraph --> ' ), 'Should have 3 paragraph blocks ' );
269+ $ this ->assertSame ( 3 , substr_count ( $ result ['post_content ' ], '<!-- /wp:paragraph --> ' ), 'Should close 3 paragraph blocks ' );
270+ }
271+
272+ /**
273+ * Test filter_import_mastodon_post_data with empty content.
274+ *
275+ * @covers ::filter_import_mastodon_post_data
276+ */
277+ public function test_filter_import_mastodon_post_data_with_empty_content () {
278+ $ data = array (
279+ 'post_content ' => '' ,
280+ );
281+
282+ $ post = array (
283+ 'object ' => array (
284+ 'inReplyTo ' => null ,
285+ ),
286+ );
287+
288+ $ result = Blocks::filter_import_mastodon_post_data ( $ data , $ post );
289+
290+ // Should handle empty content gracefully.
291+ $ this ->assertSame ( '' , $ result ['post_content ' ], 'Should return empty string for empty content ' );
292+ }
293+
294+ /**
295+ * Test filter_import_mastodon_post_data with content but no paragraph tags.
296+ *
297+ * @covers ::filter_import_mastodon_post_data
298+ */
299+ public function test_filter_import_mastodon_post_data_with_non_paragraph_content () {
300+ $ data = array (
301+ 'post_content ' => 'Plain text without paragraph tags ' ,
302+ );
303+
304+ $ post = array (
305+ 'object ' => array (
306+ 'inReplyTo ' => null ,
307+ ),
308+ );
309+
310+ $ result = Blocks::filter_import_mastodon_post_data ( $ data , $ post );
311+
312+ // Should handle content without <p> tags.
313+ $ this ->assertSame ( '' , $ result ['post_content ' ], 'Should return empty string when no paragraphs found ' );
314+ }
315+
316+ /**
317+ * Test filter_import_mastodon_post_data preserves data keys.
318+ *
319+ * @covers ::filter_import_mastodon_post_data
320+ */
321+ public function test_filter_import_mastodon_post_data_preserves_other_data () {
322+ $ data = array (
323+ 'post_content ' => '<p>Test content</p> ' ,
324+ 'post_author ' => 123 ,
325+ 'post_date ' => '2024-01-15T10:30:00Z ' ,
326+ 'post_excerpt ' => 'Test excerpt ' ,
327+ 'meta_input ' => array ( '_source_id ' => 'test-id ' ),
328+ );
329+
330+ $ post = array (
331+ 'object ' => array (
332+ 'inReplyTo ' => null ,
333+ ),
334+ );
335+
336+ $ result = Blocks::filter_import_mastodon_post_data ( $ data , $ post );
337+
338+ // Should preserve all other data keys.
339+ $ this ->assertArrayHasKey ( 'post_author ' , $ result , 'Should preserve post_author ' );
340+ $ this ->assertSame ( 123 , $ result ['post_author ' ], 'Should preserve post_author value ' );
341+ $ this ->assertArrayHasKey ( 'post_date ' , $ result , 'Should preserve post_date ' );
342+ $ this ->assertSame ( '2024-01-15T10:30:00Z ' , $ result ['post_date ' ], 'Should preserve post_date value ' );
343+ $ this ->assertArrayHasKey ( 'post_excerpt ' , $ result , 'Should preserve post_excerpt ' );
344+ $ this ->assertArrayHasKey ( 'meta_input ' , $ result , 'Should preserve meta_input ' );
345+
346+ // Should only modify post_content.
347+ $ this ->assertNotSame ( '<p>Test content</p> ' , $ result ['post_content ' ], 'Should modify post_content ' );
348+ $ this ->assertStringContainsString ( '<!-- wp:paragraph --> ' , $ result ['post_content ' ], 'Should add block markup ' );
349+ }
350+
351+ /**
352+ * Test filter_import_mastodon_post_data with nested HTML in paragraphs.
353+ *
354+ * @covers ::filter_import_mastodon_post_data
355+ */
356+ public function test_filter_import_mastodon_post_data_with_nested_html () {
357+ $ data = array (
358+ 'post_content ' => '<p>Text with <a href="https://example.com">a link</a> and <strong>bold text</strong></p> ' ,
359+ );
360+
361+ $ post = array (
362+ 'object ' => array (
363+ 'inReplyTo ' => null ,
364+ ),
365+ );
366+
367+ $ result = Blocks::filter_import_mastodon_post_data ( $ data , $ post );
368+
369+ // Should preserve nested HTML.
370+ $ this ->assertStringContainsString ( '<a href="https://example.com">a link</a> ' , $ result ['post_content ' ], 'Should preserve links ' );
371+ $ this ->assertStringContainsString ( '<strong>bold text</strong> ' , $ result ['post_content ' ], 'Should preserve strong tags ' );
372+ $ this ->assertStringContainsString ( '<!-- wp:paragraph --> ' , $ result ['post_content ' ], 'Should add block markup ' );
373+ }
374+
375+ /**
376+ * Test filter_import_mastodon_post_data integration with array-based post data.
377+ *
378+ * @covers ::filter_import_mastodon_post_data
379+ */
380+ public function test_filter_import_mastodon_post_data_with_complete_activity () {
381+ $ data = array (
382+ 'post_content ' => '<p>Complete test</p> ' ,
383+ );
384+
385+ // Realistic Mastodon activity structure.
386+ $ post = array (
387+ 'id ' => 'https://mastodon.social/users/example/statuses/123/activity ' ,
388+ 'type ' => 'Create ' ,
389+ 'actor ' => 'https://mastodon.social/users/example ' ,
390+ 'published ' => '2024-01-15T10:30:00Z ' ,
391+ 'to ' => array ( 'https://www.w3.org/ns/activitystreams#Public ' ),
392+ 'object ' => array (
393+ 'id ' => 'https://mastodon.social/users/example/statuses/123 ' ,
394+ 'type ' => 'Note ' ,
395+ 'content ' => '<p>Complete test</p> ' ,
396+ 'published ' => '2024-01-15T10:30:00Z ' ,
397+ 'inReplyTo ' => 'https://mastodon.social/@other/456 ' ,
398+ ),
399+ );
400+
401+ $ result = Blocks::filter_import_mastodon_post_data ( $ data , $ post );
402+
403+ // Should work with complete activity structure.
404+ $ this ->assertIsArray ( $ result , 'Should return array ' );
405+ $ this ->assertArrayHasKey ( 'post_content ' , $ result , 'Should have post_content key ' );
406+ $ this ->assertStringContainsString ( 'wp:activitypub/reply ' , $ result ['post_content ' ], 'Should add reply block ' );
407+ $ this ->assertStringContainsString ( '<!-- wp:paragraph --> ' , $ result ['post_content ' ], 'Should add paragraph block ' );
408+ }
409+
222410 /**
223411 * Test the reactions block with deprecated markup.
224412 */
0 commit comments