11
11
use Activitypub \Collection \Actors ;
12
12
use Activitypub \Collection \Extra_Fields ;
13
13
use Activitypub \Collection \Followers ;
14
+ use Activitypub \Collection \Following ;
14
15
use Activitypub \Collection \Outbox ;
15
16
use Activitypub \Collection \Remote_Actors ;
16
17
use Activitypub \Comment ;
@@ -35,6 +36,11 @@ class Test_Migration extends \WP_UnitTestCase {
35
36
* Set up the test.
36
37
*/
37
38
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
+
38
44
\remove_action ( 'wp_after_insert_post ' , array ( \Activitypub \Scheduler \Post::class, 'schedule_post_activity ' ), 33 );
39
45
\remove_action ( 'transition_comment_status ' , array ( \Activitypub \Scheduler \Comment::class, 'schedule_comment_activity ' ), 20 );
40
46
\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
1097
1103
// Clean up.
1098
1104
\wp_delete_post ( $ post_id , true );
1099
1105
}
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
+ }
1100
1176
}
0 commit comments