diff --git a/lib/Controller/APIv2Controller.php b/lib/Controller/APIv2Controller.php index 2270bef69..e0444a0c3 100644 --- a/lib/Controller/APIv2Controller.php +++ b/lib/Controller/APIv2Controller.php @@ -28,29 +28,14 @@ use OCP\Notification\IManager as INotificationManager; class APIv2Controller extends OCSController { - /** @var string */ - protected $filter; - - /** @var int */ - protected $since; - - /** @var int */ - protected $limit; - - /** @var string */ - protected $sort; - - /** @var string */ - protected $objectType; - - /** @var int */ - protected $objectId; - - /** @var string */ - protected $user; - - /** @var bool */ - protected $loadPreviews; + protected string $filter = 'all'; + protected int $since = 0; + protected int $limit = 50; + protected string $sort = 'desc'; + protected string $objectType = ''; + protected int $objectId = 0; + protected string $user = ''; + protected bool $loadPreviews = false; public function __construct( $appName, @@ -71,26 +56,19 @@ public function __construct( } /** - * @param string $filter - * @param int $since - * @param int $limit - * @param bool $previews - * @param string $objectType - * @param int $objectId - * @param string $sort * @throws InvalidFilterException when the filter is invalid * @throws \OutOfBoundsException when no user is given */ - protected function validateParameters($filter, $since, $limit, $previews, $objectType, $objectId, $sort) { - $this->filter = \is_string($filter) ? $filter : 'all'; + protected function validateParameters(string $filter, int $since, int $limit, bool $previews, string $objectType, int $objectId, string $sort): void { + $this->filter = $filter; if ($this->filter !== $this->data->validateFilter($this->filter)) { throw new InvalidFilterException('Invalid filter'); } - $this->since = (int)$since; - $this->limit = (int)$limit; - $this->loadPreviews = (bool)$previews; - $this->objectType = (string)$objectType; - $this->objectId = (int)$objectId; + $this->since = $since; + $this->limit = $limit; + $this->loadPreviews = $previews; + $this->objectType = $objectType; + $this->objectId = $objectId; $this->sort = \in_array($sort, ['asc', 'desc'], true) ? $sort : 'desc'; if (($this->objectType !== '' && $this->objectId === 0) || ($this->objectType === '' && $this->objectId !== 0)) { @@ -110,32 +88,15 @@ protected function validateParameters($filter, $since, $limit, $previews, $objec /** * @NoAdminRequired - * - * @param int $since - * @param int $limit - * @param bool $previews - * @param string $object_type - * @param int $object_id - * @param string $sort - * @return DataResponse */ - public function getDefault($since = 0, $limit = 50, $previews = false, $object_type = '', $object_id = 0, $sort = 'desc'): DataResponse { + public function getDefault(int $since = 0, int $limit = 50, bool $previews = false, string $object_type = '', int $object_id = 0, string $sort = 'desc'): DataResponse { return $this->get('all', $since, $limit, $previews, $object_type, $object_id, $sort); } /** * @NoAdminRequired - * - * @param string $filter - * @param int $since - * @param int $limit - * @param bool $previews - * @param string $object_type - * @param int $object_id - * @param string $sort - * @return DataResponse */ - public function getFilter($filter, $since = 0, $limit = 50, $previews = false, $object_type = '', $object_id = 0, $sort = 'desc'): DataResponse { + public function getFilter(string $filter, int $since = 0, int $limit = 50, bool $previews = false, string $object_type = '', int $object_id = 0, string $sort = 'desc'): DataResponse { return $this->get($filter, $since, $limit, $previews, $object_type, $object_id, $sort); } @@ -191,17 +152,7 @@ public function listFilters(): DataResponse { return new DataResponse($filters); } - /** - * @param string $filter - * @param int $since - * @param int $limit - * @param bool $previews - * @param string $filterObjectType - * @param int $filterObjectId - * @param string $sort - * @return DataResponse - */ - protected function get($filter, $since, $limit, $previews, $filterObjectType, $filterObjectId, $sort): DataResponse { + protected function get(string $filter, int $since, int $limit, bool $previews, string $filterObjectType, int $filterObjectId, string $sort): DataResponse { try { $this->validateParameters($filter, $since, $limit, $previews, $filterObjectType, $filterObjectId, $sort); } catch (InvalidFilterException $e) { diff --git a/lib/Data.php b/lib/Data.php index 3262175df..06e7867af 100644 --- a/lib/Data.php +++ b/lib/Data.php @@ -226,7 +226,7 @@ public function storeMail(IEvent $event, int $latestSendTime): bool { * @return array * */ - public function get(GroupHelper $groupHelper, UserSettings $userSettings, $user, $since, $limit, $sort, $filter, $objectType = '', $objectId = 0, bool $returnEvents = false) { + public function get(GroupHelper $groupHelper, UserSettings $userSettings, string $user, int $since, int $limit, string $sort, string $filter, string $objectType = '', int $objectId = 0, bool $returnEvents = false): array { // get current user if ($user === '') { throw new \OutOfBoundsException('Invalid user', 1); @@ -323,7 +323,7 @@ public function get(GroupHelper $groupHelper, UserSettings $userSettings, $user, * * @throws \OutOfBoundsException If $since is not owned by $user */ - protected function setOffsetFromSince(IQueryBuilder $query, $user, $since, $sort) { + protected function setOffsetFromSince(IQueryBuilder $query, string $user, int $since, string $sort): array { if (!$since) { return $this->getFirstKnownActivityHeader($user, $sort); } @@ -331,7 +331,7 @@ protected function setOffsetFromSince(IQueryBuilder $query, $user, $since, $sort $queryBuilder = $this->connection->getQueryBuilder(); $queryBuilder->select(['affecteduser', 'timestamp']) ->from('activity') - ->where($queryBuilder->expr()->eq('activity_id', $queryBuilder->createNamedParameter((int)$since))); + ->where($queryBuilder->expr()->eq('activity_id', $queryBuilder->createNamedParameter($since))); $result = $queryBuilder->executeQuery(); $activity = $result->fetch(); $result->closeCursor(); diff --git a/tests/Controller/APIv2ControllerTest.php b/tests/Controller/APIv2ControllerTest.php index ebdb1a147..b133a48db 100644 --- a/tests/Controller/APIv2ControllerTest.php +++ b/tests/Controller/APIv2ControllerTest.php @@ -140,9 +140,11 @@ public function testValidateParametersFilter(string $param, string $filter): voi ->method('validateFilter') ->with($param) ->willReturn($filter); + $userMock = $this->createMock(IUser::class); + $userMock->method('getUID')->willReturn('testuser'); $this->userSession->expects($this->once()) ->method('getUser') - ->willReturn($this->createMock(IUser::class)); + ->willReturn($userMock); self::invokePrivate($this->controller, 'validateParameters', [$param, 0, 0, false, '', 0, 'desc']); $this->assertSame($filter, self::invokePrivate($this->controller, 'filter')); @@ -173,19 +175,21 @@ public static function dataValidateParametersObject(): array { return [ ['type', 42, 'type', 42], ['type', '42', 'type', 42], - [null, '42', '', 0], - ['type', null, '', 0], + ['', 42, '', 0], + ['type', 0, '', 0], ]; } #[DataProvider('dataValidateParametersObject')] - public function testValidateParametersObject(?string $type, mixed $id, string $expectedType, int $expectedId): void { + public function testValidateParametersObject(string $type, mixed $id, string $expectedType, int $expectedId): void { $this->data->expects($this->once()) ->method('validateFilter') ->willReturnArgument(0); + $userMock = $this->createMock(IUser::class); + $userMock->method('getUID')->willReturn('testuser'); $this->userSession->expects($this->once()) ->method('getUser') - ->willReturn($this->createMock(IUser::class)); + ->willReturn($userMock); self::invokePrivate($this->controller, 'validateParameters', ['all', 0, 0, false, $type, $id, 'desc']); $this->assertSame($expectedType, self::invokePrivate($this->controller, 'objectType')); @@ -229,9 +233,11 @@ public function testValidateParameters(string $param, mixed $value, string $memb $this->data->expects($this->once()) ->method('validateFilter') ->willReturnArgument(0); + $userMock = $this->createMock(IUser::class); + $userMock->method('getUID')->willReturn('testuser'); $this->userSession->expects($this->once()) ->method('getUser') - ->willReturn($this->createMock(IUser::class)); + ->willReturn($userMock); self::invokePrivate($this->controller, 'validateParameters', $params); $this->assertSame($expectedValue, self::invokePrivate($this->controller, $memberName));