@@ -436,4 +436,110 @@ public function test_handle_create_malformed_object() {
436436 // Should not create objects with malformed data.
437437 $ this ->assertEquals ( count ( $ objects_before ), count ( $ objects_after ) );
438438 }
439+
440+ /**
441+ * Test create_post returns false when activitypub_create_posts option is disabled.
442+ *
443+ * @covers ::create_post
444+ */
445+ public function test_create_post_disabled_by_option () {
446+ // Ensure option is not set.
447+ \delete_option ( 'activitypub_create_posts ' );
448+
449+ // Mock HTTP request for Remote_Actors::fetch_by_uri.
450+ $ mock_callback = function ( $ pre , $ url_or_object ) {
451+ $ url = \Activitypub \object_to_uri ( $ url_or_object );
452+ if ( 'https://example.com/users/testuser ' === $ url ) {
453+ return array (
454+ 'id ' => 'https://example.com/users/testuser ' ,
455+ 'type ' => 'Person ' ,
456+ 'name ' => 'Test Actor ' ,
457+ 'preferredUsername ' => 'testuser ' ,
458+ 'url ' => 'https://example.com/users/testuser ' ,
459+ 'inbox ' => 'https://example.com/users/testuser/inbox ' ,
460+ );
461+ }
462+ return $ pre ;
463+ };
464+ \add_filter ( 'activitypub_pre_http_get_remote_object ' , $ mock_callback , 10 , 2 );
465+
466+ $ activity = array (
467+ 'id ' => 'https://example.com/activities/create_disabled ' ,
468+ 'type ' => 'Create ' ,
469+ 'actor ' => 'https://example.com/users/testuser ' ,
470+ 'to ' => array ( 'https://www.w3.org/ns/activitystreams#Public ' ),
471+ 'object ' => array (
472+ 'id ' => 'https://example.com/objects/note_disabled ' ,
473+ 'type ' => 'Note ' ,
474+ 'content ' => '<p>This should not be created</p> ' ,
475+ 'attributedTo ' => 'https://example.com/users/testuser ' ,
476+ 'published ' => '2023-01-01T12:00:00Z ' ,
477+ 'to ' => array ( 'https://www.w3.org/ns/activitystreams#Public ' ),
478+ ),
479+ );
480+
481+ $ result = Create::create_post ( $ activity , array ( $ this ->user_id ) );
482+
483+ $ this ->assertFalse ( $ result );
484+
485+ // Verify no post was created.
486+ $ created_object = Posts::get_by_guid ( 'https://example.com/objects/note_disabled ' );
487+ $ this ->assertTrue ( \is_wp_error ( $ created_object ) );
488+
489+ \remove_filter ( 'activitypub_pre_http_get_remote_object ' , $ mock_callback );
490+ }
491+
492+ /**
493+ * Test create_post works when activitypub_create_posts option is enabled.
494+ *
495+ * @covers ::create_post
496+ */
497+ public function test_create_post_enabled_by_option () {
498+ // Enable the option.
499+ \update_option ( 'activitypub_create_posts ' , '1 ' );
500+
501+ // Mock HTTP request for Remote_Actors::fetch_by_uri.
502+ $ mock_callback = function ( $ pre , $ url_or_object ) {
503+ $ url = \Activitypub \object_to_uri ( $ url_or_object );
504+ if ( 'https://example.com/users/testuser2 ' === $ url ) {
505+ return array (
506+ 'id ' => 'https://example.com/users/testuser2 ' ,
507+ 'type ' => 'Person ' ,
508+ 'name ' => 'Test Actor 2 ' ,
509+ 'preferredUsername ' => 'testuser2 ' ,
510+ 'url ' => 'https://example.com/users/testuser2 ' ,
511+ 'inbox ' => 'https://example.com/users/testuser2/inbox ' ,
512+ );
513+ }
514+ return $ pre ;
515+ };
516+ \add_filter ( 'activitypub_pre_http_get_remote_object ' , $ mock_callback , 10 , 2 );
517+
518+ $ activity = array (
519+ 'id ' => 'https://example.com/activities/create_enabled ' ,
520+ 'type ' => 'Create ' ,
521+ 'actor ' => 'https://example.com/users/testuser2 ' ,
522+ 'to ' => array ( 'https://www.w3.org/ns/activitystreams#Public ' ),
523+ 'object ' => array (
524+ 'id ' => 'https://example.com/objects/note_enabled ' ,
525+ 'type ' => 'Note ' ,
526+ 'content ' => '<p>This should be created</p> ' ,
527+ 'attributedTo ' => 'https://example.com/users/testuser2 ' ,
528+ 'published ' => '2023-01-01T12:00:00Z ' ,
529+ 'to ' => array ( 'https://www.w3.org/ns/activitystreams#Public ' ),
530+ ),
531+ );
532+
533+ $ result = Create::create_post ( $ activity , array ( $ this ->user_id ) );
534+
535+ $ this ->assertInstanceOf ( 'WP_Post ' , $ result );
536+
537+ // Verify post was created.
538+ $ created_object = Posts::get_by_guid ( 'https://example.com/objects/note_enabled ' );
539+ $ this ->assertNotNull ( $ created_object );
540+ $ this ->assertStringContainsString ( 'This should be created ' , $ created_object ->post_content );
541+
542+ \remove_filter ( 'activitypub_pre_http_get_remote_object ' , $ mock_callback );
543+ \delete_option ( 'activitypub_create_posts ' );
544+ }
439545}
0 commit comments