diff --git a/app/Legacy/V1/Requests/Traits/Authorize/AuthorizeCanEditAlbumAlbumsTrait.php b/app/Legacy/V1/Requests/Traits/Authorize/AuthorizeCanEditAlbumAlbumsTrait.php index 6045066ad46..49c97a58934 100644 --- a/app/Legacy/V1/Requests/Traits/Authorize/AuthorizeCanEditAlbumAlbumsTrait.php +++ b/app/Legacy/V1/Requests/Traits/Authorize/AuthorizeCanEditAlbumAlbumsTrait.php @@ -30,7 +30,7 @@ public function authorize(): bool if (!Gate::check(AlbumPolicy::CAN_EDIT, $album)) { // @codeCoverageIgnoreStart return false; - // @codecoverageignoreend + // @codeCoverageIgnoreEnd } } diff --git a/app/Legacy/V1/Requests/Traits/HasAlbumIDTrait.php b/app/Legacy/V1/Requests/Traits/HasAlbumIDTrait.php deleted file mode 100644 index 7d32b60b377..00000000000 --- a/app/Legacy/V1/Requests/Traits/HasAlbumIDTrait.php +++ /dev/null @@ -1,22 +0,0 @@ -albumID; - } -} diff --git a/app/Models/AccessPermission.php b/app/Models/AccessPermission.php index edc7a973de9..173b9efd3b5 100644 --- a/app/Models/AccessPermission.php +++ b/app/Models/AccessPermission.php @@ -11,7 +11,6 @@ use App\Constants\AccessPermissionConstants as APC; use App\Exceptions\ConfigurationKeyMissingException; use App\Models\Builders\AccessPermissionBuilder; -use App\Models\Extensions\HasAttributesPatch; use App\Models\Extensions\ThrowsConsistentExceptions; use App\Models\Extensions\UTCBasedTimes; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -65,7 +64,6 @@ class AccessPermission extends Model { use UTCBasedTimes; - use HasAttributesPatch; use ThrowsConsistentExceptions; /** @phpstan-use HasFactory<\Database\Factories\AccessPermissionFactory> */ use HasFactory; diff --git a/app/Models/BaseAlbumImpl.php b/app/Models/BaseAlbumImpl.php index beec5db3ff3..46709662096 100644 --- a/app/Models/BaseAlbumImpl.php +++ b/app/Models/BaseAlbumImpl.php @@ -17,7 +17,6 @@ use App\Enum\PhotoLayoutType; use App\Enum\TimelinePhotoGranularity; use App\Models\Builders\BaseAlbumImplBuilder; -use App\Models\Extensions\HasAttributesPatch; use App\Models\Extensions\HasBidirectionalRelationships; use App\Models\Extensions\HasRandomIDAndLegacyTimeBasedID; use App\Models\Extensions\ThrowsConsistentExceptions; @@ -142,7 +141,6 @@ */ class BaseAlbumImpl extends Model implements HasRandomID { - use HasAttributesPatch; /** @phpstan-use HasRandomIDAndLegacyTimeBasedID */ use HasRandomIDAndLegacyTimeBasedID; use ThrowsConsistentExceptions; diff --git a/app/Models/Extensions/HasAttributesPatch.php b/app/Models/Extensions/HasAttributesPatch.php deleted file mode 100644 index 98941f3bff1..00000000000 --- a/app/Models/Extensions/HasAttributesPatch.php +++ /dev/null @@ -1,37 +0,0 @@ -hasGetMutator($key)) { - $value = $this->mutateAttribute($key, $value); - } - if ($this->isClassCastable($key)) { - $value = $this->getClassCastableAttributeValue($key, $value); - } - - return $value instanceof Arrayable ? $value->toArray() : $value; - } -} diff --git a/app/Models/Extensions/SizeVariants.php b/app/Models/Extensions/SizeVariants.php index 5b40ea9e7f9..a8cb6c8205a 100644 --- a/app/Models/Extensions/SizeVariants.php +++ b/app/Models/Extensions/SizeVariants.php @@ -233,6 +233,8 @@ public function getPlaceholder(): ?SizeVariant * * @throws IllegalOrderOfOperationException * @throws ModelDBException + * + * @disregard P1006 */ public function create(SizeVariantType $sizeVariantType, string $shortPath, ImageDimension $dim, int $filesize): SizeVariant { diff --git a/app/Models/Extensions/ThrowsConsistentExceptions.php b/app/Models/Extensions/ThrowsConsistentExceptions.php index f21aee99a70..b4739efb15b 100644 --- a/app/Models/Extensions/ThrowsConsistentExceptions.php +++ b/app/Models/Extensions/ThrowsConsistentExceptions.php @@ -8,11 +8,8 @@ namespace App\Models\Extensions; -use App\Exceptions\Internal\LycheeLogicException; use App\Exceptions\ModelDBException; -use Illuminate\Database\Eloquent\JsonEncodingException; use Illuminate\Support\Str; -use function Safe\json_encode; /** * Fixed Eloquent model for all Lychee models. @@ -125,39 +122,4 @@ public function jsonSerialize(): array throw new \JsonException(get_class($this) . '::toArray() failed', 0, $e); } } - - /** - * Convert the model instance to JSON. - * - * The error message is inspired by {@link JsonEncodingException::forModel()}. - * - * @param int $options - * - * @return string - * - * @throws JsonEncodingException - */ - public function toJson($options = 0): string - { - // See if we can delete this. - throw new LycheeLogicException('Error encoding model [' . get_class($this) . '] to JSON'); - try { - // Note, we must not use the option `JSON_THROW_ON_ERROR` here, - // because this does not clear `json_last_error()` from any - // previous, stalled error message. - // But `\Illuminate\Http\JsonResponse::setData()` falsy assumes - // that this method does so. - // Hence, we call `json_encode` _without_ specifying - // `JSON_THROW_ON_ERROR` and then mimic that behaviour. - // TODO: VERIFY THIS - $json = json_encode($this->jsonSerialize(), $options); - if (json_last_error() !== JSON_ERROR_NONE) { - throw new \JsonException(json_last_error_msg(), json_last_error()); - } - - return $json; - } catch (\JsonException $e) { - throw new JsonEncodingException('Error encoding model [' . get_class($this) . '] to JSON', 0, $e); - } - } } diff --git a/app/Models/OauthCredential.php b/app/Models/OauthCredential.php index b036bfe4fb1..0629e9046a9 100644 --- a/app/Models/OauthCredential.php +++ b/app/Models/OauthCredential.php @@ -10,7 +10,6 @@ use App\Enum\OauthProvidersType; use App\Models\Builders\OauthCredentialBuilder; -use App\Models\Extensions\HasAttributesPatch; use App\Models\Extensions\ThrowsConsistentExceptions; use App\Models\Extensions\UTCBasedTimes; use Illuminate\Database\Eloquent\Model; @@ -30,7 +29,6 @@ class OauthCredential extends Model { use UTCBasedTimes; - use HasAttributesPatch; use ThrowsConsistentExceptions; protected $fillable = [ diff --git a/app/Models/Photo.php b/app/Models/Photo.php index 17028ea7285..2e4bb9aeec9 100644 --- a/app/Models/Photo.php +++ b/app/Models/Photo.php @@ -23,7 +23,6 @@ use App\Facades\Helpers; use App\Image\Files\BaseMediaFile; use App\Models\Builders\PhotoBuilder; -use App\Models\Extensions\HasAttributesPatch; use App\Models\Extensions\HasBidirectionalRelationships; use App\Models\Extensions\HasRandomIDAndLegacyTimeBasedID; use App\Models\Extensions\SizeVariants; @@ -130,7 +129,6 @@ class Photo extends Model /** @phpstan-use HasFactory<\Database\Factories\PhotoFactory> */ use HasFactory; use UTCBasedTimes; - use HasAttributesPatch; /** @phpstan-use HasRandomIDAndLegacyTimeBasedID */ use HasRandomIDAndLegacyTimeBasedID; use ThrowsConsistentExceptions; @@ -260,10 +258,12 @@ protected function getShutterAttribute(?string $shutter): ?string } return $shutter; + // @codeCoverageIgnoreStart } catch (ZeroModuloException $e) { // this should not happen as we covered the case $b = 0; throw LycheeAssertionError::createFromUnexpectedException($e); } + // @codeCoverageIgnoreEnd } /** diff --git a/app/Models/SizeVariant.php b/app/Models/SizeVariant.php index abf11b648e8..9c2ee6c5cd8 100644 --- a/app/Models/SizeVariant.php +++ b/app/Models/SizeVariant.php @@ -18,7 +18,6 @@ use App\Http\Resources\Models\SizeVariantResource; use App\Image\Files\FlysystemFile; use App\Models\Builders\SizeVariantBuilder; -use App\Models\Extensions\HasAttributesPatch; use App\Models\Extensions\HasBidirectionalRelationships; use App\Models\Extensions\ThrowsConsistentExceptions; use App\Models\Extensions\ToArrayThrowsNotImplemented; @@ -75,7 +74,6 @@ class SizeVariant extends Model { use UTCBasedTimes; - use HasAttributesPatch; use HasBidirectionalRelationships; use ThrowsConsistentExceptions; use ToArrayThrowsNotImplemented; @@ -207,7 +205,9 @@ public function getUrlAttribute(): string return $this->getSymLinkUrl(); } + // @codeCoverageIgnoreStart throw new ConfigurationException('the chosen storage adapter "' . get_class($storageAdapter) . '" does not support the symbolic linking feature'); + // @codeCoverageIgnoreEnd } /** diff --git a/app/Models/SymLink.php b/app/Models/SymLink.php index df878d7c94b..1e11b38df16 100644 --- a/app/Models/SymLink.php +++ b/app/Models/SymLink.php @@ -14,7 +14,6 @@ use App\Exceptions\ModelDBException; use App\Image\Files\FlysystemFile; use App\Models\Builders\SymLinkBuilder; -use App\Models\Extensions\HasAttributesPatch; use App\Models\Extensions\ThrowsConsistentExceptions; use App\Models\Extensions\UTCBasedTimes; use Carbon\Exceptions\InvalidTimeZoneException; @@ -61,7 +60,6 @@ class SymLink extends Model { use UTCBasedTimes; - use HasAttributesPatch; use ThrowsConsistentExceptions { ThrowsConsistentExceptions::delete as private internalDelete; } @@ -136,9 +134,11 @@ protected function getUrlAttribute(): string try { /** @disregard P1013 */ return Storage::disk(self::DISK_NAME)->url($this->short_path); + // @codeCoverageIgnoreStart } catch (\RuntimeException $e) { throw new FrameworkException('Laravel\'s storage component', $e); } + // @codeCoverageIgnoreEnd } /** @@ -167,9 +167,11 @@ protected function performInsert(Builder $query): bool unlink($symAbsolutePath); } symlink($origRealPath, $symAbsolutePath); + // @codeCoverageIgnoreStart } catch (FilesystemException $e) { throw new MediaFileOperationException($e->getMessage(), $e); } + // @codeCoverageIgnoreEnd $this->short_path = $symShortPath; return parent::performInsert($query); diff --git a/tests/Unit/Models/AccessPermissionTest.php b/tests/Unit/Models/AccessPermissionTest.php new file mode 100644 index 00000000000..876fb03e499 --- /dev/null +++ b/tests/Unit/Models/AccessPermissionTest.php @@ -0,0 +1,38 @@ +user_id); + self::assertTrue($ap->{APC::GRANTS_FULL_PHOTO_ACCESS}); // @phpstan-ignore-line: Variable property access on App\Models\AccessPermission + self::assertTrue($ap->{APC::GRANTS_DOWNLOAD}); // @phpstan-ignore-line: Variable property access on App\Models\AccessPermission + self::assertTrue($ap->{APC::GRANTS_UPLOAD}); // @phpstan-ignore-line: Variable property access on App\Models\AccessPermission + self::assertTrue($ap->{APC::GRANTS_EDIT}); // @phpstan-ignore-line: Variable property access on App\Models\AccessPermission + self::assertTrue($ap->{APC::GRANTS_DELETE}); // @phpstan-ignore-line: Variable property access on App\Models\AccessPermission + } +} diff --git a/tests/Unit/Models/ConfigsTest.php b/tests/Unit/Models/ConfigsTest.php new file mode 100644 index 00000000000..b9b6de4e3df --- /dev/null +++ b/tests/Unit/Models/ConfigsTest.php @@ -0,0 +1,57 @@ + 'key', + 'value' => '', + 'type_range' => ConfigType::STRING_REQ->value, + ] + ); + self::assertEquals('Error: key empty or not set', $config->sanity('')); + self::assertEquals('Error: key empty or not set', $config->sanity(null)); + + $config->type_range = ConfigType::POSTIIVE->value; + self::assertEquals('Error: Wrong property for key, expected strictly positive integer, got a.', $config->sanity('a')); + self::assertEquals('Error: Wrong property for key, expected strictly positive integer, got -1.', $config->sanity('-1')); + self::assertEquals('Error: Wrong property for key, expected strictly positive integer, got 0.', $config->sanity('0')); + + $config->type_range = ConfigType::MAP_PROVIDER->value; + self::assertEquals('Error: Wrong property for key, expected a valid map provider, got something.', $config->sanity('something')); + + $config->type_range = '1|2|3|4'; + self::assertEquals('Error: Wrong property for key, expected 1 or 2 or 3 or 4, got 5.', $config->sanity('5')); + } +} \ No newline at end of file diff --git a/tests/Unit/Models/PhotoTest.php b/tests/Unit/Models/PhotoTest.php new file mode 100644 index 00000000000..2c47a6700c8 --- /dev/null +++ b/tests/Unit/Models/PhotoTest.php @@ -0,0 +1,53 @@ +shutter = '10/5 s'; + self::assertEquals('2 s', $p->shutter); + $p->shutter = '1/1000 s'; + self::assertEquals('1/1000 s', $p->shutter); + $p->shutter = '1/1 s'; + self::assertEquals('1 s', $p->shutter); + } + + public function testShutterException(): void + { + $p = new Photo(); + $p->shutter = '10/0 s'; + self::assertEquals('10/0 s', $p->shutter); + } + + public function testAspectRatio(): void + { + $p = new Photo(); + $p->type = 'video/mp4'; + self::assertEquals('1', $p->aspect_ratio); + + $p->type = 'image/jpeg'; + self::assertEquals('1', $p->aspect_ratio); + } +}