Moved API\Notification tests

This commit is contained in:
Philipp 2021-11-12 21:35:21 +01:00
parent f2ca21935e
commit 6725f05ab2
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
8 changed files with 161 additions and 86 deletions

View file

@ -35,6 +35,29 @@ class ApiResponse
$this->logger = $logger;
}
/**
* Sets header directly
* mainly used to override it for tests
*
* @param string $header
*/
protected function setHeader(string $header)
{
header($header);
}
/**
* Prints output directly to the caller
* mainly used to override it for tests
*
* @param string $output
*/
protected function printOutput(string $output)
{
echo $output;
exit;
}
/**
* Creates the XML from a JSON style array
*
@ -143,7 +166,7 @@ class ApiResponse
'request' => $this->args->getQueryString()
];
header(($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' ' . $code . ' ' . $description);
$this->setHeader(($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' ' . $code . ' ' . $description);
$this->exit('status', ['status' => $error], $format);
}
@ -165,10 +188,10 @@ class ApiResponse
switch ($format) {
case 'xml':
header('Content-Type: text/xml');
$this->setHeader('Content-Type: text/xml');
break;
case 'json':
header('Content-Type: application/json');
$this->setHeader('Content-Type: application/json');
if (!empty($return)) {
$json = json_encode(end($return));
if (!empty($_GET['callback'])) {
@ -178,17 +201,16 @@ class ApiResponse
}
break;
case 'rss':
header('Content-Type: application/rss+xml');
$this->setHeader('Content-Type: application/rss+xml');
$return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
break;
case 'atom':
header('Content-Type: application/atom+xml');
$this->setHeader('Content-Type: application/atom+xml');
$return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
break;
}
echo $return;
exit;
$this->printOutput($return);
}
/**

View file

@ -0,0 +1,54 @@
<?php
namespace Friendica\Test\Util;
use Friendica\Module\Api\ApiResponse;
class ApiResponseDouble extends ApiResponse
{
/**
* The header list
*
* @var string[]
*/
protected static $header = [];
/**
* The printed output
*
* @var string
*/
protected static $output = '';
/**
* @return string[]
*/
public static function getHeader(): array
{
return static::$header;
}
/**
* @return string
*/
public static function getOutput(): string
{
return static::$output;
}
public static function reset()
{
self::$output = '';
self::$header = [];
}
protected function setHeader(string $header)
{
static::$header[] = $header;
}
protected function printOutput(string $output)
{
static::$output .= $output;
}
}

View file

@ -3565,77 +3565,6 @@ class ApiTest extends FixtureTest
$this->markTestIncomplete();
}
/**
* Test the api_friendica_notification() function.
*
* @return void
*/
public function testApiFriendicaNotification()
{
// $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
// api_friendica_notification('json');
}
/**
* Test the api_friendica_notification() function without an authenticated user.
*
* @return void
*/
public function testApiFriendicaNotificationWithoutAuthenticatedUser()
{
// $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
// $_SESSION['authenticated'] = false;
// api_friendica_notification('json');
}
/**
* Test the api_friendica_notification() function with empty result
*
* @return void
*/
public function testApiFriendicaNotificationWithEmptyResult()
{
// DI::args()->setArgv(['api', 'friendica', 'notification']);
// $_SESSION['uid'] = 41;
// $result = api_friendica_notification('json');
// self::assertEquals(['note' => false], $result);
}
/**
* Test the api_friendica_notification() function with an XML result.
*
* @return void
*/
public function testApiFriendicaNotificationWithXmlResult()
{
/*
DI::args()->setArgv(['api', 'friendica', 'notification']);
$result = api_friendica_notification('xml');
$date = DateTimeFormat::local('2020-01-01 12:12:02');
$dateRel = Temporal::getRelativeDate('2020-01-01 07:12:02');
$assertXml=<<<XML
<?xml version="1.0"?>
<notes>
<note date="$date" date_rel="$dateRel" id="1" iid="4" link="http://localhost/notification/1" msg="A test reply from an item" msg_cache="A test reply from an item" msg_html="A test reply from an item" msg_plain="A test reply from an item" name="Reply to" name_cache="Reply to" otype="item" parent="" photo="http://localhost/" seen="false" timestamp="1577880722" type="8" uid="42" url="http://localhost/display/1" verb="http://activitystrea.ms/schema/1.0/post"/>
</notes>
XML;
self::assertXmlStringEqualsXmlString($assertXml, $result);
*/
}
/**
* Test the api_friendica_notification() function with an JSON result.
*
* @return void
*/
public function testApiFriendicaNotificationWithJsonResult()
{
// DI::args()->setArgv(['api', 'friendica', 'notification']);
// $result = json_encode(api_friendica_notification('json'));
// self::assertJson($result);
}
/**
* Test the api_friendica_notification_seen() function.
*

View file

@ -7,8 +7,10 @@ use Friendica\Core\Addon;
use Friendica\Core\Hook;
use Friendica\Database\Database;
use Friendica\DI;
use Friendica\Module\Api\ApiResponse;
use Friendica\Security\Authentication;
use Friendica\Test\FixtureTest;
use Friendica\Test\Util\ApiResponseDouble;
use Friendica\Test\Util\AuthenticationDouble;
class ApiTest extends FixtureTest
@ -18,12 +20,20 @@ class ApiTest extends FixtureTest
parent::setUp(); // TODO: Change the autogenerated stub
$this->dice = $this->dice
->addRule(Authentication::class, ['instanceOf' => AuthenticationDouble::class, 'shared' => true]);
->addRule(Authentication::class, ['instanceOf' => AuthenticationDouble::class, 'shared' => true])
->addRule(ApiResponse::class, ['instanceOf' => ApiResponseDouble::class, 'shared' => true]);
DI::init($this->dice);
$this->installAuthTest();
}
protected function tearDown(): void
{
ApiResponseDouble::reset();
parent::tearDown();
}
/**
* installs auththest.
*

View file

@ -0,0 +1,60 @@
<?php
namespace Friendica\Test\src\Module\Api\Friendica;
use Friendica\DI;
use Friendica\Module\Api\Friendica\Notification;
use Friendica\Network\HTTPException\BadRequestException;
use Friendica\Test\src\Module\Api\ApiTest;
use Friendica\Test\Util\ApiResponseDouble;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Temporal;
class NotificationTest extends ApiTest
{
public function testEmpty()
{
self::markTestIncomplete('Needs BasicAuth as dynamic method for overriding first');
$this->expectException(BadRequestException::class);
DI::session()->set('uid', '');
Notification::rawContent();
}
public function testWithoutAuthenticatedUser()
{
self::markTestIncomplete('Needs BasicAuth as dynamic method for overriding first');
$this->expectException(BadRequestException::class);
DI::session()->set('uid', 41);
Notification::rawContent();
}
public function testWithXmlResult()
{
$date = DateTimeFormat::local('2020-01-01 12:12:02');
$dateRel = Temporal::getRelativeDate('2020-01-01 07:12:02');
$assertXml = <<<XML
<?xml version="1.0"?>
<notes>
<note date="$date" date_rel="$dateRel" id="1" iid="4" link="http://localhost/notification/1" msg="A test reply from an item" msg_cache="A test reply from an item" msg_html="A test reply from an item" msg_plain="A test reply from an item" name="Reply to" name_cache="Reply to" otype="item" parent="" photo="http://localhost/" seen="false" timestamp="1577880722" type="8" uid="42" url="http://localhost/display/1" verb="http://activitystrea.ms/schema/1.0/post"/>
</notes>
XML;
Notification::rawContent(['extension' => 'xml']);
self::assertXmlStringEqualsXmlString($assertXml, ApiResponseDouble::getOutput());
}
public function testWithJsonResult()
{
Notification::rawContent(['parameter' => 'json']);
$result = json_encode(ApiResponseDouble::getOutput());
self::assertJson($result);
}
}

View file

@ -10,7 +10,7 @@ class DeleteTest extends ApiTest
{
public function testEmpty()
{
self::expectException(BadRequestException::class);
$this->expectException(BadRequestException::class);
Delete::rawContent();
}
@ -21,7 +21,7 @@ class DeleteTest extends ApiTest
public function testWrong()
{
self::expectException(BadRequestException::class);
$this->expectException(BadRequestException::class);
Delete::rawContent(['photo_id' => 1]);
}

View file

@ -10,13 +10,13 @@ class DeleteTest extends ApiTest
{
public function testEmpty()
{
self::expectException(BadRequestException::class);
$this->expectException(BadRequestException::class);
Delete::rawContent();
}
public function testWrong()
{
self::expectException(BadRequestException::class);
$this->expectException(BadRequestException::class);
Delete::rawContent(['album' => 'album_name']);
}

View file

@ -10,19 +10,19 @@ class UpdateTest extends ApiTest
{
public function testEmpty()
{
self::expectException(BadRequestException::class);
$this->expectException(BadRequestException::class);
Update::rawContent();
}
public function testTooFewArgs()
{
self::expectException(BadRequestException::class);
$this->expectException(BadRequestException::class);
Update::rawContent(['album' => 'album_name']);
}
public function testWrongUpdate()
{
self::expectException(BadRequestException::class);
$this->expectException(BadRequestException::class);
Update::rawContent(['album' => 'album_name', 'album_new' => 'album_name']);
}