From f84c696925467f4091a6ac2e27f97d6a921c1643 Mon Sep 17 00:00:00 2001 From: Philipp Date: Thu, 9 Dec 2021 21:52:40 +0100 Subject: [PATCH] Reenable Twitter/Favorites tests --- src/Module/Api/Twitter/Favorites.php | 10 +++--- tests/src/Module/Api/ApiTest.php | 8 ++--- .../src/Module/Api/Twitter/FavoritesTest.php | 31 +++++++++++++------ 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/Module/Api/Twitter/Favorites.php b/src/Module/Api/Twitter/Favorites.php index f806b23ee..8f4643f82 100644 --- a/src/Module/Api/Twitter/Favorites.php +++ b/src/Module/Api/Twitter/Favorites.php @@ -45,10 +45,10 @@ class Favorites extends BaseApi Logger::info(BaseApi::LOG_PREFIX . 'for {self}', ['module' => 'api', 'action' => 'favorites']); // params - $since_id = $_REQUEST['since_id'] ?? 0; - $max_id = $_REQUEST['max_id'] ?? 0; - $count = $_GET['count'] ?? 20; - $page = $_REQUEST['page'] ?? 1; + $since_id = $request['since_id'] ?? 0; + $max_id = $request['max_id'] ?? 0; + $count = $request['count'] ?? 20; + $page = $request['page'] ?? 1; $start = max(0, ($page - 1) * $count); @@ -64,7 +64,7 @@ class Favorites extends BaseApi $statuses = Post::selectForUser($uid, [], $condition, $params); - $include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true'); + $include_entities = strtolower(($request['include_entities'] ?? 'false') == 'true'); $ret = []; while ($status = DBA::fetch($statuses)) { diff --git a/tests/src/Module/Api/ApiTest.php b/tests/src/Module/Api/ApiTest.php index 09fbb7bbe..9820061e0 100644 --- a/tests/src/Module/Api/ApiTest.php +++ b/tests/src/Module/Api/ApiTest.php @@ -115,14 +115,14 @@ abstract class ApiTest extends FixtureTest /** * Assert that a status array contains expected keys. * - * @param array $status Status array + * @param \stdClass $status Status * * @return void */ - protected function assertStatus(array $status = []) + protected function assertStatus(\stdClass $status) { - self::assertIsString($status['text'] ?? ''); - self::assertIsInt($status['id'] ?? ''); + self::assertIsString($status->text); + self::assertIsInt($status->id); // We could probably do more checks here. } diff --git a/tests/src/Module/Api/Twitter/FavoritesTest.php b/tests/src/Module/Api/Twitter/FavoritesTest.php index 26729144e..dd95d73f9 100644 --- a/tests/src/Module/Api/Twitter/FavoritesTest.php +++ b/tests/src/Module/Api/Twitter/FavoritesTest.php @@ -2,6 +2,10 @@ namespace Friendica\Test\src\Module\Api\Twitter; +use Friendica\App\Router; +use Friendica\Capabilities\ICanCreateResponses; +use Friendica\DI; +use Friendica\Module\Api\Twitter\Favorites; use Friendica\Test\src\Module\Api\ApiTest; class FavoritesTest extends ApiTest @@ -13,14 +17,17 @@ class FavoritesTest extends ApiTest */ public function testApiFavorites() { - /* - $_REQUEST['page'] = -1; - $_REQUEST['max_id'] = 10; - $result = api_favorites('json'); - foreach ($result['status'] as $status) { - self::assertStatus($status); + $favorites = new Favorites(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]); + $response = $favorites->run([ + 'page' => -1, + 'max_id' => 10, + ]); + + $json = $this->toJson($response); + + foreach ($json as $status) { + $this->assertStatus($status); } - */ } /** @@ -30,8 +37,12 @@ class FavoritesTest extends ApiTest */ public function testApiFavoritesWithRss() { - // $result = api_favorites('rss'); - // self::assertXml($result, 'statuses'); + $favorites = new Favorites(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => ICanCreateResponses::TYPE_RSS]); + $response = $favorites->run(); + + self::assertEquals(ICanCreateResponses::TYPE_RSS, $response->getHeaderLine(ICanCreateResponses::X_HEADER)); + + self::assertXml((string)$response->getBody(), 'statuses'); } /** @@ -41,6 +52,8 @@ class FavoritesTest extends ApiTest */ public function testApiFavoritesWithUnallowedUser() { + self::markTestIncomplete('Needs BasicAuth as dynamic method for overriding first'); + // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class); // BasicAuth::setCurrentUserID(); // api_favorites('json');