added manual tests, WIP

This commit is contained in:
Sascha Nitsch 2025-06-11 03:21:51 +02:00
parent d99479c188
commit c1cf2a6be8

193
php/federator/test.php Normal file
View file

@ -0,0 +1,193 @@
<?php
/**
* SPDX-FileCopyrightText: 2024 Sascha Nitsch (grumpydeveloper) https://contentnation.net/@grumpydevelop
* SPDX-License-Identifier: GPL-3.0-or-later
*
* @author Sascha Nitsch (grumpydeveloper)
**/
namespace Federator;
/**
* test functions
*/
class Test
{
/**
* fetch outbox
* @param string $_name user handle to fetch
* @return void
*/
public static function fetchOutbox($_name)
{
// make webfinger request
$r = self::webfinger($_name);
$outbox = $r['outbox'];
$headers = ['Accept: application/activity+json'];
[$response, $info] = \Federator\Main::getFromRemote($outbox, $headers);
if ($info['http_code'] != 200) {
return;
}
$json = json_decode($response, true);
if ($json === false) {
return;
}
$ap = \Federator\Data\ActivityPub\Factory::newFromJson($json, $response);
if (!($ap instanceof \Federator\Data\ActivityPub\Common\OrderedCollection)) {
echo "unsupport reply from $outbox\n";
return;
}
// first and total items should be set
$total = $ap->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 <command> <parameter> [<command> <parameter>...]\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);