From a16f89a42b1c1418d6859537b1abae469b6c8899 Mon Sep 17 00:00:00 2001 From: ildyria Date: Fri, 17 Jan 2025 19:37:27 +0100 Subject: [PATCH] coverage --- app/Legacy/Legacy.php | 4 +++ app/Legacy/V1/Middleware/LoginRequiredV1.php | 2 ++ .../Requests/Sharing/DeleteSharingRequest.php | 3 ++ .../Requests/Sharing/ListSharingRequest.php | 8 +++++ .../Sharing/SetSharesByAlbumRequest.php | 2 ++ .../V1/Requests/View/GetPhotoViewRequest.php | 3 ++ app/Services/Auth/SessionOrTokenGuard.php | 8 +++++ app/SmartAlbums/BaseSmartAlbum.php | 4 +++ tests/Unit/CoverageTest.php | 35 +++++++++++++++++++ 9 files changed, 69 insertions(+) diff --git a/app/Legacy/Legacy.php b/app/Legacy/Legacy.php index 6b738d71d3a..41eb38eb3d4 100644 --- a/app/Legacy/Legacy.php +++ b/app/Legacy/Legacy.php @@ -60,18 +60,22 @@ private static function translateLegacyID(int $id, string $tableName, Request $r ' instead of new ID ' . $newID . ' from ' . $referer; if (!Configs::getValueAsBool('legacy_id_redirection')) { + // @codeCoverageIgnoreStart $msg .= ' (translation disabled by configuration)'; throw new ConfigurationException($msg); + // @codeCoverageIgnoreEnd } Log::warning(__METHOD__ . ':' . __LINE__ . $msg); return $newID; } + // @codeCoverageIgnoreStart return null; } catch (\InvalidArgumentException $e) { throw new QueryBuilderException($e); } + // @codeCoverageIgnoreEnd } /** diff --git a/app/Legacy/V1/Middleware/LoginRequiredV1.php b/app/Legacy/V1/Middleware/LoginRequiredV1.php index e8a641ed66e..897bd24b1bc 100644 --- a/app/Legacy/V1/Middleware/LoginRequiredV1.php +++ b/app/Legacy/V1/Middleware/LoginRequiredV1.php @@ -39,6 +39,8 @@ class LoginRequiredV1 * * @throws ConfigurationException * @throws FrameworkException + * + * @codeCoverageIgnore Legacy stuff we don't care. */ public function handle(Request $request, \Closure $next, string $requiredStatus): mixed { diff --git a/app/Legacy/V1/Requests/Sharing/DeleteSharingRequest.php b/app/Legacy/V1/Requests/Sharing/DeleteSharingRequest.php index 6e35d5d9d00..9a8dbf5d4f3 100644 --- a/app/Legacy/V1/Requests/Sharing/DeleteSharingRequest.php +++ b/app/Legacy/V1/Requests/Sharing/DeleteSharingRequest.php @@ -14,6 +14,9 @@ use App\Rules\IntegerIDRule; use Illuminate\Support\Facades\Gate; +/** + * @codeCoverageIgnore Legacy stuff we don't care. + */ class DeleteSharingRequest extends BaseApiRequest { public const SHARE_IDS_ATTRIBUTE = 'shareIDs'; diff --git a/app/Legacy/V1/Requests/Sharing/ListSharingRequest.php b/app/Legacy/V1/Requests/Sharing/ListSharingRequest.php index 25b06c13016..57365b27b53 100644 --- a/app/Legacy/V1/Requests/Sharing/ListSharingRequest.php +++ b/app/Legacy/V1/Requests/Sharing/ListSharingRequest.php @@ -54,6 +54,8 @@ class ListSharingRequest extends BaseApiRequest implements HasBaseAlbum /** * {@inheritDoc} + * + * @codeCoverageIgnore Legacy stuff we don't care. */ public function authorize(): bool { @@ -96,20 +98,26 @@ public function rules(): array protected function processValidatedValues(array $values, array $files): void { $this->album = key_exists(RequestAttribute::ALBUM_ID_ATTRIBUTE, $values) ? + // @codeCoverageIgnoreStart $this->albumFactory->findBaseAlbumOrFail($values[RequestAttribute::ALBUM_ID_ATTRIBUTE]) : + // @codeCoverageIgnoreEnd null; $this->owner = null; $this->participant = null; if (key_exists(self::OWNER_ID_ATTRIBUTE, $values)) { + // @codeCoverageIgnoreStart /** @var int $ownerID */ $ownerID = $values[self::OWNER_ID_ATTRIBUTE]; $this->owner = User::query()->findOrFail($ownerID); + // @codeCoverageIgnoreEnd } if (key_exists(self::PARTICIPANT_ID_ATTRIBUTE, $values)) { + // @codeCoverageIgnoreStart /** @var int $participantID */ $participantID = $values[self::PARTICIPANT_ID_ATTRIBUTE]; $this->participant = User::query()->findOrFail($participantID); + // @codeCoverageIgnoreEnd } } diff --git a/app/Legacy/V1/Requests/Sharing/SetSharesByAlbumRequest.php b/app/Legacy/V1/Requests/Sharing/SetSharesByAlbumRequest.php index bc19a9849c1..778a27bf5cc 100644 --- a/app/Legacy/V1/Requests/Sharing/SetSharesByAlbumRequest.php +++ b/app/Legacy/V1/Requests/Sharing/SetSharesByAlbumRequest.php @@ -24,6 +24,8 @@ * Represents a request for setting the shares of a specific album. * * Only the owner (or the admin) of the album can set the shares. + * + * @codeCoverageIgnore Legacy stuff we don't care. */ class SetSharesByAlbumRequest extends BaseApiRequest implements HasBaseAlbum, HasUserIDs { diff --git a/app/Legacy/V1/Requests/View/GetPhotoViewRequest.php b/app/Legacy/V1/Requests/View/GetPhotoViewRequest.php index 6b21682eb2b..9678c0b02d8 100644 --- a/app/Legacy/V1/Requests/View/GetPhotoViewRequest.php +++ b/app/Legacy/V1/Requests/View/GetPhotoViewRequest.php @@ -16,6 +16,9 @@ use App\Rules\RandomIDRule; use Illuminate\Support\Facades\Gate; +/** + * @codeCoverageIgnore + */ class GetPhotoViewRequest extends BaseApiRequest implements HasPhoto { use HasPhotoTrait; diff --git a/app/Services/Auth/SessionOrTokenGuard.php b/app/Services/Auth/SessionOrTokenGuard.php index fd8802590d8..e42ce2821be 100644 --- a/app/Services/Auth/SessionOrTokenGuard.php +++ b/app/Services/Auth/SessionOrTokenGuard.php @@ -128,7 +128,9 @@ public static function createGuard(Application $app, string $name, array $config /** @disregard P1013 */ $guard->setRequest($app->refresh('request', $guard, 'setRequest')); if (isset($config['remember'])) { + // @codeCoverageIgnoreStart $guard->setRememberDuration($config['remember']); + // @codeCoverageIgnoreEnd } return $guard; @@ -194,7 +196,9 @@ public function user(): Authenticatable|null // new authentication state explicitly. $this->authState = self::AUTH_STATE_STATELESS; } elseif ($userByRecaller !== null) { + // @codeCoverageIgnoreStart $this->login($userByRecaller, true); + // @codeCoverageIgnoreEnd } else { // In the other cases, `$this->user` has implicitly been set by // `parent::setUser` or `$this->login`. @@ -326,7 +330,9 @@ protected function getUserByToken(): ?Authenticatable // Skip if token starts with Basic: it is not related to Lychee. if (Str::startsWith('Basic', $token)) { + // @codeCoverageIgnoreStart return null; + // @codeCoverageIgnoreEnd } // Check if token starts with Bearer @@ -349,8 +355,10 @@ protected function getUserByToken(): ?Authenticatable return match (true) { $authenticable !== null => $authenticable, + // @codeCoverageIgnoreStart $hasBearer && $configThrow => throw new BadRequestHeaderException('Invalid token'), $hasBearer => null, + // @codeCoverageIgnoreEnd default => throw new BadRequestHeaderException('Invalid token'), }; } diff --git a/app/SmartAlbums/BaseSmartAlbum.php b/app/SmartAlbums/BaseSmartAlbum.php index 38b8955cfe8..cf29841e595 100644 --- a/app/SmartAlbums/BaseSmartAlbum.php +++ b/app/SmartAlbums/BaseSmartAlbum.php @@ -68,9 +68,11 @@ protected function __construct(SmartAlbumType $id, \Closure $smartCondition) /** @var AccessPermission|null $perm */ $perm = AccessPermission::query()->where('base_album_id', '=', $id->value)->first(); $this->publicPermissions = $perm; + // @codeCoverageIgnoreStart } catch (BindingResolutionException $e) { throw new FrameworkException('Laravel\'s service container', $e); } + // @codeCoverageIgnoreEnd } /** @@ -136,7 +138,9 @@ protected function getThumbAttribute(): ?Thumb * user. */ $this->thumb ??= Configs::getValueAsBool('SA_random_thumbs') + // @codeCoverageIgnoreStart ? Thumb::createFromRandomQueryable($this->photos()) + // @codeCoverageIgnoreEnd : $this->thumb = Thumb::createFromQueryable( $this->photos(), PhotoSortingCriterion::createDefault() diff --git a/tests/Unit/CoverageTest.php b/tests/Unit/CoverageTest.php index f5e42b04331..5c361c2a918 100644 --- a/tests/Unit/CoverageTest.php +++ b/tests/Unit/CoverageTest.php @@ -25,6 +25,8 @@ use App\Enum\AspectRatioType; use App\Enum\MapProviders; use App\Enum\SmartAlbumType; +use App\Exceptions\Internal\LycheeInvalidArgumentException; +use App\SmartAlbums\UnsortedAlbum; use Tests\AbstractTestCase; class CoverageTest extends AbstractTestCase @@ -100,4 +102,37 @@ public function testImportEventReport(): void ); self::assertEquals('message', $report->toCLIString()); } + + public function testBaseSmartAlbumException(): void + { + self::expectException(LycheeInvalidArgumentException::class); + + $album = new UnsortedAlbum(); + $album->__get(''); + } + + public function testBaseSmartAlbumException2(): void + { + self::expectException(LycheeInvalidArgumentException::class); + + $album = new UnsortedAlbum(); + $album->__get('something'); + } + + public function testBaseSmartAlbumPhotos(): void + { + $album = new UnsortedAlbum(); + $data = $album->__get('Photos'); + self::assertEmpty($data); + + $data = $album->getPhotos(); + self::assertEmpty($data); + + $album->setPublic(); + $album->setPublic(); + $data = $album->getPhotos(); + self::assertEmpty($data); + $album->setPrivate(); + $album->setPrivate(); + } }