@@ -1216,4 +1216,210 @@ public function test_get_avatar_url_empty() {
12161216
12171217 // Clean up.
12181218 }
1219+
1220+ /**
1221+ * Test that webfinger acct is stored when creating an actor.
1222+ *
1223+ * @covers ::create
1224+ */
1225+ public function test_webfinger_acct_stored_on_create () {
1226+ // Create an actor with webfinger.
1227+ $ actor = array (
1228+ 'id ' => 'https://example.com/users/webfinger-store ' ,
1229+ 'type ' => 'Person ' ,
1230+ 'url ' => 'https://example.com/users/webfinger-store ' ,
1231+ 'inbox ' => 'https://example.com/users/webfinger-store/inbox ' ,
1232+ 'name ' => 'Webfinger Store ' ,
1233+ 'preferredUsername ' => 'webfinger ' ,
1234+ );
1235+
1236+ // Mock webfinger resolution.
1237+ $ webfinger_callback = function ( $ preempt , $ parsed_args , $ url ) {
1238+ if ( strpos ( $ url , '.well-known/webfinger ' ) !== false ) {
1239+ return array (
1240+ 'response ' => array ( 'code ' => 200 ),
1241+ 'body ' => wp_json_encode (
1242+ array (
1243+ 'subject ' =>
'acct:[email protected] ' ,
1244+ 'links ' => array (
1245+ array (
1246+ 'rel ' => 'self ' ,
1247+ 'type ' => 'application/activity+json ' ,
1248+ 'href ' => 'https://example.com/users/webfinger-store ' ,
1249+ ),
1250+ ),
1251+ )
1252+ ),
1253+ );
1254+ }
1255+ return $ preempt ;
1256+ };
1257+ \add_filter ( 'pre_http_request ' , $ webfinger_callback , 10 , 3 );
1258+
1259+ $ post_id = Remote_Actors::create ( $ actor );
1260+ $ this ->assertIsInt ( $ post_id );
1261+
1262+ // Verify acct was stored.
1263+ $ stored_acct = \get_post_meta ( $ post_id , '_activitypub_acct ' , true );
1264+ $ this ->
assertEquals (
'[email protected] ' ,
$ stored_acct );
1265+
1266+ \remove_filter ( 'pre_http_request ' , $ webfinger_callback );
1267+ }
1268+
1269+ /**
1270+ * Test that webfinger is populated when loading an actor from database.
1271+ *
1272+ * @covers ::get_actor
1273+ */
1274+ public function test_webfinger_populated_on_load () {
1275+ // Create an actor.
1276+ $ actor = array (
1277+ 'id ' => 'https://example.com/users/webfinger-load ' ,
1278+ 'type ' => 'Person ' ,
1279+ 'url ' => 'https://example.com/users/webfinger-load ' ,
1280+ 'inbox ' => 'https://example.com/users/webfinger-load/inbox ' ,
1281+ 'name ' => 'Webfinger Load ' ,
1282+ 'preferredUsername ' => 'webfingerload ' ,
1283+ );
1284+
1285+ $ post_id = Remote_Actors::create ( $ actor );
1286+ $ this ->assertIsInt ( $ post_id );
1287+
1288+ // Store acct manually.
1289+ \update_post_meta (
$ post_id,
'_activitypub_acct ' ,
'[email protected] ' );
1290+
1291+ // Load the actor.
1292+ $ actor_obj = Remote_Actors::get_actor ( $ post_id );
1293+
1294+ // Verify webfinger was populated.
1295+ $ this ->
assertEquals (
'[email protected] ' ,
$ actor_obj->
get_webfinger () );
1296+ }
1297+
1298+ /**
1299+ * Test that webfinger is generated from actor URL when not available.
1300+ *
1301+ * @covers ::get_actor
1302+ */
1303+ public function test_webfinger_generated_from_url () {
1304+ // Create an actor without stored webfinger.
1305+ $ actor = array (
1306+ 'id ' => 'https://example.com/users/generate-webfinger ' ,
1307+ 'type ' => 'Person ' ,
1308+ 'url ' => 'https://example.com/users/generate-webfinger ' ,
1309+ 'inbox ' => 'https://example.com/users/generate-webfinger/inbox ' ,
1310+ 'name ' => 'Generate Webfinger ' ,
1311+ 'preferredUsername ' => 'generatewf ' ,
1312+ );
1313+
1314+ $ post_id = Remote_Actors::create ( $ actor );
1315+ $ this ->assertIsInt ( $ post_id );
1316+
1317+ // Don't store acct meta.
1318+ \delete_post_meta ( $ post_id , '_activitypub_acct ' );
1319+
1320+ // Mock webfinger resolution failure (will fall back to guess).
1321+ $ webfinger_callback = function ( $ preempt , $ parsed_args , $ url ) {
1322+ if ( strpos ( $ url , '.well-known/webfinger ' ) !== false ) {
1323+ return array (
1324+ 'response ' => array ( 'code ' => 404 ),
1325+ 'body ' => 'Not Found ' ,
1326+ );
1327+ }
1328+ return $ preempt ;
1329+ };
1330+ \add_filter ( 'pre_http_request ' , $ webfinger_callback , 10 , 3 );
1331+
1332+ // Load the actor.
1333+ $ actor_obj = Remote_Actors::get_actor ( $ post_id );
1334+
1335+ // Verify webfinger was guessed from URL.
1336+ 1337+ $ this ->assertEquals ( $ expected , $ actor_obj ->get_webfinger () );
1338+
1339+ \remove_filter ( 'pre_http_request ' , $ webfinger_callback );
1340+ }
1341+
1342+ /**
1343+ * Test that webfinger acct is updated when actor is updated.
1344+ *
1345+ * @covers ::update
1346+ */
1347+ public function test_webfinger_acct_updated_on_update () {
1348+ // Create an actor.
1349+ $ actor = array (
1350+ 'id ' => 'https://example.com/users/webfinger-update ' ,
1351+ 'type ' => 'Person ' ,
1352+ 'url ' => 'https://example.com/users/webfinger-update ' ,
1353+ 'inbox ' => 'https://example.com/users/webfinger-update/inbox ' ,
1354+ 'name ' => 'Webfinger Update ' ,
1355+ 'preferredUsername ' => 'webfingerupdate ' ,
1356+ );
1357+
1358+ // Mock webfinger resolution.
1359+ $ webfinger_callback = function ( $ preempt , $ parsed_args , $ url ) {
1360+ if ( strpos ( $ url , '.well-known/webfinger ' ) !== false ) {
1361+ return array (
1362+ 'response ' => array ( 'code ' => 200 ),
1363+ 'body ' => wp_json_encode (
1364+ array (
1365+ 'subject ' =>
'acct:[email protected] ' ,
1366+ 'links ' => array (
1367+ array (
1368+ 'rel ' => 'self ' ,
1369+ 'type ' => 'application/activity+json ' ,
1370+ 'href ' => 'https://example.com/users/webfinger-update ' ,
1371+ ),
1372+ ),
1373+ )
1374+ ),
1375+ );
1376+ }
1377+ return $ preempt ;
1378+ };
1379+ \add_filter ( 'pre_http_request ' , $ webfinger_callback , 10 , 3 );
1380+
1381+ $ post_id = Remote_Actors::create ( $ actor );
1382+ $ this ->assertIsInt ( $ post_id );
1383+
1384+ // Verify initial acct.
1385+ $ stored_acct = \get_post_meta ( $ post_id , '_activitypub_acct ' , true );
1386+ $ this ->
assertEquals (
'[email protected] ' ,
$ stored_acct );
1387+
1388+ // Update the actor with modified name.
1389+ $ updated_actor = $ actor ;
1390+ $ updated_actor ['name ' ] = 'Webfinger Updated Name ' ;
1391+
1392+ Remote_Actors::update ( $ post_id , $ updated_actor );
1393+
1394+ // Verify acct is still stored correctly after update.
1395+ $ updated_acct = \get_post_meta ( $ post_id , '_activitypub_acct ' , true );
1396+ $ this ->
assertEquals (
'[email protected] ' ,
$ updated_acct );
1397+
1398+ \remove_filter ( 'pre_http_request ' , $ webfinger_callback );
1399+ }
1400+
1401+ /**
1402+ * Test that webfinger acct is stored when provided in actor data.
1403+ *
1404+ * @covers ::create
1405+ */
1406+ public function test_webfinger_from_actor_data () {
1407+ // Create an actor with webfinger in the data.
1408+ $ actor = array (
1409+ 'id ' => 'https://example.com/users/actor-data-wf ' ,
1410+ 'type ' => 'Person ' ,
1411+ 'url ' => 'https://example.com/users/actor-data-wf ' ,
1412+ 'inbox ' => 'https://example.com/users/actor-data-wf/inbox ' ,
1413+ 'name ' => 'Actor Data Webfinger ' ,
1414+ 'preferredUsername ' => 'actordatawf ' ,
1415+ 'webfinger ' =>
'[email protected] ' ,
1416+ );
1417+
1418+ $ post_id = Remote_Actors::create ( $ actor );
1419+ $ this ->assertIsInt ( $ post_id );
1420+
1421+ // Verify custom webfinger was stored.
1422+ $ stored_acct = \get_post_meta ( $ post_id , '_activitypub_acct ' , true );
1423+ $ this ->
assertEquals (
'[email protected] ' ,
$ stored_acct );
1424+ }
12191425}
0 commit comments