1111use Activitypub \Collection \Actors ;
1212use Activitypub \Collection \Extra_Fields ;
1313use Activitypub \Collection \Followers ;
14+ use Activitypub \Collection \Following ;
1415use Activitypub \Collection \Outbox ;
1516use Activitypub \Collection \Remote_Actors ;
1617use Activitypub \Comment ;
@@ -35,6 +36,11 @@ class Test_Migration extends \WP_UnitTestCase {
3536 * Set up the test.
3637 */
3738 public static function set_up_before_class () {
39+ // Mock Jetpack class if it doesn't exist.
40+ if ( ! class_exists ( 'Jetpack ' ) ) {
41+ require_once AP_TESTS_DIR . '/data/class-jetpack.php ' ;
42+ }
43+
3844 \remove_action ( 'wp_after_insert_post ' , array ( \Activitypub \Scheduler \Post::class, 'schedule_post_activity ' ), 33 );
3945 \remove_action ( 'transition_comment_status ' , array ( \Activitypub \Scheduler \Comment::class, 'schedule_comment_activity ' ), 20 );
4046 \remove_action ( 'wp_insert_comment ' , array ( \Activitypub \Scheduler \Comment::class, 'schedule_comment_activity_on_insert ' ) );
@@ -1097,4 +1103,74 @@ public function test_remove_pending_application_user_follow_requests_multiple_en
10971103 // Clean up.
10981104 \wp_delete_post ( $ post_id , true );
10991105 }
1106+
1107+ /**
1108+ * Test sync_jetpack_following_meta triggers actions correctly.
1109+ *
1110+ * @covers ::sync_jetpack_following_meta
1111+ */
1112+ public function test_sync_jetpack_following_meta () {
1113+ // Create test posts with following meta.
1114+ $ posts = self ::factory ()->post ->create_many ( 3 , array ( 'post_type ' => Remote_Actors::POST_TYPE ) );
1115+
1116+ // Add following meta to each post.
1117+ \add_post_meta ( $ posts [0 ], Following::FOLLOWING_META_KEY , '123 ' );
1118+ \add_post_meta ( $ posts [1 ], Following::FOLLOWING_META_KEY , '456 ' );
1119+ \add_post_meta ( $ posts [2 ], Following::FOLLOWING_META_KEY , '789 ' );
1120+
1121+ // Track action calls.
1122+ $ action_calls = array ();
1123+ $ capture_action = function () use ( &$ action_calls ) {
1124+ $ action_calls [] = func_get_args ();
1125+ };
1126+
1127+ \add_action ( 'added_post_meta ' , $ capture_action , 10 , 4 );
1128+
1129+ // Run the migration with Jetpack available.
1130+ Migration::sync_jetpack_following_meta ();
1131+
1132+ // Verify the correct actions were triggered.
1133+ $ this ->assertCount ( 3 , $ action_calls , 'Should trigger action for each following meta entry ' );
1134+
1135+ // Check the first action call structure.
1136+ $ this ->assertCount ( 4 , $ action_calls [0 ], 'Action should be called with 4 parameters ' );
1137+ list ( $ meta_id , $ post_id , $ meta_key , $ meta_value ) = $ action_calls [0 ];
1138+
1139+ $ this ->assertEquals ( Following::FOLLOWING_META_KEY , $ meta_key , 'Meta key should be Following::FOLLOWING_META_KEY ' );
1140+ $ this ->assertIsNumeric ( $ meta_id , 'Meta ID should be numeric ' );
1141+ $ this ->assertIsNumeric ( $ post_id , 'Post ID should be numeric ' );
1142+ $ this ->assertContains ( $ meta_value , array ( '123 ' , '456 ' , '789 ' ), 'Meta value should be one of the test values ' );
1143+
1144+ // Clean up.
1145+ \remove_action ( 'added_post_meta ' , $ capture_action , 10 );
1146+ foreach ( $ posts as $ post ) {
1147+ \wp_delete_post ( $ post , true );
1148+ }
1149+ }
1150+
1151+ /**
1152+ * Test sync_jetpack_following_meta with no following meta.
1153+ *
1154+ * @covers ::sync_jetpack_following_meta
1155+ */
1156+ public function test_sync_jetpack_following_meta_no_entries () {
1157+ // Track action calls for the specific meta key we care about.
1158+ $ following_actions = array ();
1159+ $ capture_action = function ( $ meta_id , $ post_id , $ meta_key , $ meta_value ) use ( &$ following_actions ) {
1160+ if ( Following::FOLLOWING_META_KEY === $ meta_key ) {
1161+ $ following_actions [] = array ( $ meta_id , $ post_id , $ meta_key , $ meta_value );
1162+ }
1163+ };
1164+
1165+ \add_action ( 'added_post_meta ' , $ capture_action , 10 , 4 );
1166+
1167+ // Run migration with no following meta (should not trigger our specific actions).
1168+ Migration::sync_jetpack_following_meta ();
1169+
1170+ // Verify no following-specific actions were triggered.
1171+ $ this ->assertEmpty ( $ following_actions , 'No following-specific actions should be triggered when no following meta exists ' );
1172+
1173+ // Clean up.
1174+ \remove_action ( 'added_post_meta ' , $ capture_action , 10 );
1175+ }
11001176}
0 commit comments