From c1cf2a6be8c0001f86f9b44b5714fa40d145e9a2 Mon Sep 17 00:00:00 2001 From: Sascha Nitsch Date: Wed, 11 Jun 2025 03:21:51 +0200 Subject: [PATCH] added manual tests, WIP --- php/federator/test.php | 193 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 php/federator/test.php diff --git a/php/federator/test.php b/php/federator/test.php new file mode 100644 index 0000000..bfaab0c --- /dev/null +++ b/php/federator/test.php @@ -0,0 +1,193 @@ +getTotalItems(); + echo "total items: " . $total; + echo " first: " . $ap->getFirst() . "\n"; + + $page = $ap->getFirst(); + $count = 0; + + while ($count < $total) { + echo "query $page\n"; + // query pages + [$response, $info] = \Federator\Main::getFromRemote($page, $headers); + if ($info['http_code'] != 200) { + return; + } + // echo "'$response'\n"; + $json = json_decode($response, true); + $ap = \Federator\Data\ActivityPub\Factory::newFromJson($json, $response); + if (!($ap instanceof \Federator\Data\ActivityPub\Common\OrderedCollectionPage)) { + echo "unsupport reply from $page\n"; + return; + } + $thisCount = $ap->getCount(); + echo "count: " . $thisCount . "\n"; + for ($i = 0; $i < $thisCount; ++$i) { + $entry = $ap->get($i); + if ($entry instanceof \Federator\Data\ActivityPub\Common\APObject) { + echo $entry->getID() . " " . $entry->getPublished() . "\n"; + } + } + $count += $thisCount; + $page = $ap->getNext(); + } + //print_r($ap); + } + /** + * run test + * + * @param int $argc number of arguments + * @param string[] $argv arguments + * @return void + */ + public static function run($argc, $argv) + { + date_default_timezone_set("Europe/Berlin"); + spl_autoload_register(static function (string $className) { + include PROJECT_ROOT . '/php/' . str_replace("\\", "/", strtolower($className)) . '.php'; + }); + if ($argc < 2) { + self::printUsage(); + } + // pretend that we are running from web directory + define('PROJECT_ROOT', dirname(__DIR__, 2)); + $api = new \Federator\Api(); + for ($i = 1; $i < $argc; ++$i) { + switch ($argv[$i]) { + case 'fetchoutbox': + self::fetchOutbox($argv[$i + 1]); + ++$i; + break; + case 'upvote': + self::upvote($api, $argv[$i + 1]); + ++$i; + break; + default: + self::printUsage(); + } + } + } + + + /** + * print usage of maintenance tool + * + * @return void + */ + public static function printUsage() + { + echo "usage php maintenance.php [ ...]\n"; + echo "command can be one of:\n"; + echo " fetchoutbox - fetch users outbox. parameter: username\n"; + echo " upvote - upvote. parameter: URL to upvote\n"; + echo " downvote - downvote. parameter: URL to downvote\n"; + echo " comment - downvote. parameter: URL to comment, text to comment\n"; + echo " Run this after you updated the program files\n"; + exit(); + } + + /** + * upvote given URL + * + * @param \Federator\Api $api api instance + * @param string $_url URL to upvote + * @note uses hardcoded source + * @return void + */ + public static function upvote($api, $_url) + { + $dbh = $api->getDatabase(); + $inboxActivity = new \Federator\Data\ActivityPub\Common\Create(); + \Federator\Api\FedUsers\Inbox::postForUser( + $dbh, + $api->getConnector(), + null, + 'grumpydevelop', + $_url, + $inboxActivity + ); + } + + /** + * do a webfinger request + * @param string $_name name to query + */ + private static function webfinger($_name): mixed + { + // make webfinger request + if (preg_match("/^([^@]+)@(.*)$/", $_name, $matches) != 1) { + echo "username is malformed"; + return false; + } + $remoteURL = 'https://' . $matches[2] . '/.well-known/webfinger?resource=acct:' . urlencode($_name); + $headers = ['Accept: application/activity+json']; + [$response, $info] = \Federator\Main::getFromRemote($remoteURL, $headers); + if ($info['http_code'] != 200) { + return false; + } + $r = json_decode($response, true); + if (isset($r['links'])) { + foreach ($r['links'] as $link) { + if (isset($link['rel']) && $link['rel'] === 'self') { + $remoteURL = $link['href']; + break; + } + } + } + if (!isset($remoteURL)) { + echo "FedUser::getUserByName Failed to find self link in webfinger for " . $_name . "\n"; + return false; + } + // fetch the user + $headers = ['Accept: application/activity+json']; + [$response, $info] = \Federator\Main::getFromRemote($remoteURL, $headers); + if ($info['http_code'] != 200) { + echo "FedUser::getUserByName Failed to fetch user from remoteUrl for " . $_name . "\n"; + return false; + } + $r = json_decode($response, true); + return $r; + } +} + + +Test::run($argc, $argv);