Throw Not Found exception when $uid doesn't exist in Factory\Api\Twitter\User->createFromUserId

- Contact::getPublicIdByUserId() wrongly returns 0 when $uid doesn't exist, which is an existing albeit invalid record.
This commit is contained in:
Hypolite Petovan 2024-02-24 23:51:27 -05:00
parent ac087749e3
commit d37699bc08
2 changed files with 13 additions and 9 deletions

View File

@ -24,10 +24,9 @@ namespace Friendica\Factory\Api\Twitter;
use Friendica\BaseFactory; use Friendica\BaseFactory;
use Friendica\Model\APContact; use Friendica\Model\APContact;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Network\HTTPException;
use Friendica\Factory\Api\Twitter\Status;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Post; use Friendica\Model\Post;
use Friendica\Network\HTTPException;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
class User extends BaseFactory class User extends BaseFactory
@ -85,9 +84,17 @@ class User extends BaseFactory
* @param bool $include_user_entities * @param bool $include_user_entities
* *
* @return \Friendica\Object\Api\Twitter\User * @return \Friendica\Object\Api\Twitter\User
* @throws HTTPException\InternalServerErrorException
* @throws HTTPException\NotFoundException If the $uid doesn't exist
* @throws \ImagickException
*/ */
public function createFromUserId(int $uid, bool $skip_status = true, bool $include_user_entities = true): \Friendica\Object\Api\Twitter\User public function createFromUserId(int $uid, bool $skip_status = true, bool $include_user_entities = true): \Friendica\Object\Api\Twitter\User
{ {
return $this->createFromContactId(Contact::getPublicIdByUserId($uid), $uid, $skip_status, $include_user_entities); $cid = Contact::getPublicIdByUserId($uid);
if (!$cid) {
throw new HTTPException\NotFoundException();
}
return $this->createFromContactId($cid, $uid, $skip_status, $include_user_entities);
} }
} }

View File

@ -23,6 +23,7 @@ namespace Friendica\Test\src\Factory\Api\Twitter;
use Friendica\DI; use Friendica\DI;
use Friendica\Factory\Api\Twitter\User; use Friendica\Factory\Api\Twitter\User;
use Friendica\Network\HTTPException\NotFoundException;
use Friendica\Test\FixtureTest; use Friendica\Test\FixtureTest;
use Friendica\Test\src\Module\Api\ApiTest; use Friendica\Test\src\Module\Api\ApiTest;
@ -133,15 +134,11 @@ class UserTest extends FixtureTest
*/ */
public function testApiGetUserWithWrongGetId() public function testApiGetUserWithWrongGetId()
{ {
$this->expectException(NotFoundException::class);
$user = (new User(DI::logger(), DI::twitterStatus())) $user = (new User(DI::logger(), DI::twitterStatus()))
->createFromUserId(-1) ->createFromUserId(-1)
->toArray(); ->toArray();
self::assertEquals(0, $user['id']);
self::assertEquals(0, $user['uid']);
self::assertEquals(0, $user['cid']);
self::assertEquals(0, $user['pid']);
self::assertEmpty($user['name']);
} }
/** /**