Compare commits
No commits in common. "8bf17784cc480b9d5d02d5a6079681c565805187" and "79cb5a8f58cc2d36f8df25d95de3c042a0bb8c80" have entirely different histories.
8bf17784cc
...
79cb5a8f58
|
@ -4,15 +4,9 @@
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
|
||||||
"Zoomyboy\\MedialibraryHelper\\": "src/"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"autoload-dev": {
|
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Zoomyboy\\MedialibraryHelper\\Tests\\": "tests/",
|
"Zoomyboy\\MedialibraryHelper\\Tests\\": "tests/",
|
||||||
"Workbench\\App\\": "tests/workbench/app/",
|
"Zoomyboy\\MedialibraryHelper\\": "src/"
|
||||||
"Database\\Factories\\": "tests/workbench/database/factories/"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"authors": [
|
"authors": [
|
||||||
|
@ -34,20 +28,7 @@
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"post-autoload-dump": [
|
"post-autoload-dump": [
|
||||||
"@clear",
|
|
||||||
"@prepare",
|
|
||||||
"@php vendor/bin/testbench package:discover --ansi"
|
"@php vendor/bin/testbench package:discover --ansi"
|
||||||
],
|
|
||||||
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
|
|
||||||
"prepare": "@php vendor/bin/testbench package:discover --ansi",
|
|
||||||
"build": "@php vendor/bin/testbench workbench:build --ansi",
|
|
||||||
"serve": [
|
|
||||||
"Composer\\Config::disableProcessTimeout",
|
|
||||||
"@build",
|
|
||||||
"@php vendor/bin/testbench serve"
|
|
||||||
],
|
|
||||||
"test": [
|
|
||||||
"@php vendor/bin/pest"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
|
|
|
@ -8682,16 +8682,16 @@
|
||||||
"packages-dev": [
|
"packages-dev": [
|
||||||
{
|
{
|
||||||
"name": "fakerphp/faker",
|
"name": "fakerphp/faker",
|
||||||
"version": "v1.23.1",
|
"version": "v1.23.0",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/FakerPHP/Faker.git",
|
"url": "https://github.com/FakerPHP/Faker.git",
|
||||||
"reference": "bfb4fe148adbf78eff521199619b93a52ae3554b"
|
"reference": "e3daa170d00fde61ea7719ef47bb09bb8f1d9b01"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/FakerPHP/Faker/zipball/bfb4fe148adbf78eff521199619b93a52ae3554b",
|
"url": "https://api.github.com/repos/FakerPHP/Faker/zipball/e3daa170d00fde61ea7719ef47bb09bb8f1d9b01",
|
||||||
"reference": "bfb4fe148adbf78eff521199619b93a52ae3554b",
|
"reference": "e3daa170d00fde61ea7719ef47bb09bb8f1d9b01",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
|
@ -8717,6 +8717,11 @@
|
||||||
"ext-mbstring": "Required for multibyte Unicode string functionality."
|
"ext-mbstring": "Required for multibyte Unicode string functionality."
|
||||||
},
|
},
|
||||||
"type": "library",
|
"type": "library",
|
||||||
|
"extra": {
|
||||||
|
"branch-alias": {
|
||||||
|
"dev-main": "v1.21-dev"
|
||||||
|
}
|
||||||
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Faker\\": "src/Faker/"
|
"Faker\\": "src/Faker/"
|
||||||
|
@ -8739,9 +8744,9 @@
|
||||||
],
|
],
|
||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/FakerPHP/Faker/issues",
|
"issues": "https://github.com/FakerPHP/Faker/issues",
|
||||||
"source": "https://github.com/FakerPHP/Faker/tree/v1.23.1"
|
"source": "https://github.com/FakerPHP/Faker/tree/v1.23.0"
|
||||||
},
|
},
|
||||||
"time": "2024-01-02T13:46:09+00:00"
|
"time": "2023-06-12T08:44:38+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "hamcrest/hamcrest-php",
|
"name": "hamcrest/hamcrest-php",
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
return [
|
|
||||||
'temp_disk' => 'temp',
|
|
||||||
'middleware' => ['web', 'auth:web'],
|
|
||||||
];
|
|
|
@ -1,50 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Zoomyboy\MedialibraryHelper;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Storage;
|
|
||||||
use Illuminate\Support\Str;
|
|
||||||
use Spatie\LaravelData\Attributes\MapInputName;
|
|
||||||
use Spatie\LaravelData\Attributes\MapOutputName;
|
|
||||||
use Spatie\LaravelData\Data;
|
|
||||||
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
|
|
||||||
use Spatie\MediaLibrary\HasMedia;
|
|
||||||
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
|
|
||||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
|
||||||
|
|
||||||
#[MapInputName(SnakeCaseMapper::class)]
|
|
||||||
#[MapOutputName(SnakeCaseMapper::class)]
|
|
||||||
class DeferredMediaData extends Data
|
|
||||||
{
|
|
||||||
public ?int $id;
|
|
||||||
|
|
||||||
public string $originalUrl;
|
|
||||||
|
|
||||||
public int $size;
|
|
||||||
|
|
||||||
public string $name;
|
|
||||||
|
|
||||||
public string $collectionName;
|
|
||||||
|
|
||||||
public string $fileName;
|
|
||||||
|
|
||||||
public string $mimeType;
|
|
||||||
|
|
||||||
public bool $fallback = false;
|
|
||||||
|
|
||||||
public bool $isDeferred = true;
|
|
||||||
|
|
||||||
public static function fromPath(string $path, MediaCollection $collection): self
|
|
||||||
{
|
|
||||||
$file = new MediaFile(Storage::disk(config('media-library.temp_disk'))->path($path));
|
|
||||||
return static::withoutMagicalCreationFrom([
|
|
||||||
'collection_name' => $collection->name,
|
|
||||||
'original_url' => Storage::disk(config('media-library.temp_disk'))->url($path),
|
|
||||||
'size' => $file->getSize(),
|
|
||||||
'name' => $file->getBasename(),
|
|
||||||
'file_name' => $file->getFilename(),
|
|
||||||
'mime_type' => Storage::disk(config('media-library.temp_disk'))->mimeType($path),
|
|
||||||
'path' => $path,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -5,14 +5,16 @@ namespace Zoomyboy\MedialibraryHelper;
|
||||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
use Spatie\Image\Image;
|
use Spatie\Image\Image;
|
||||||
use Spatie\LaravelData\DataCollection;
|
use Spatie\LaravelData\DataCollection;
|
||||||
|
use Spatie\MediaLibrary\HasMedia;
|
||||||
use Spatie\MediaLibrary\MediaCollections\Exceptions\InvalidBase64Data;
|
use Spatie\MediaLibrary\MediaCollections\Exceptions\InvalidBase64Data;
|
||||||
use Spatie\MediaLibrary\MediaCollections\FileAdder;
|
use Spatie\MediaLibrary\MediaCollections\FileAdder;
|
||||||
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
|
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
|
||||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||||
use Zoomyboy\MedialibraryHelper\Rules\ModelRule;
|
use Symfony\Component\HttpFoundation\File\File;
|
||||||
use Illuminate\Support\Facades\Storage;
|
|
||||||
|
|
||||||
class MediaController
|
class MediaController
|
||||||
{
|
{
|
||||||
|
@ -21,15 +23,12 @@ class MediaController
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$request->validate([
|
$request->validate([
|
||||||
'parent' => ['required', new ModelRule()],
|
'model' => ['required', 'string', Rule::in(app('media-library-helpers')->keys())],
|
||||||
|
'id' => 'required',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (is_null($request->input('parent.id'))) {
|
$model = $this->validateModel($request);
|
||||||
return $this->storeDeferred($request);
|
$collection = $model->getMediaCollection($request->input('collection'));
|
||||||
}
|
|
||||||
|
|
||||||
$model = ModelRule::getModel($request->input('parent'));
|
|
||||||
$collection = ModelRule::getCollection($request->input('parent'));
|
|
||||||
$isSingle = 1 === $collection->collectionSizeLimit;
|
$isSingle = 1 === $collection->collectionSizeLimit;
|
||||||
$this->authorize('storeMedia', [$model, $collection->name]);
|
$this->authorize('storeMedia', [$model, $collection->name]);
|
||||||
|
|
||||||
|
@ -66,37 +65,6 @@ class MediaController
|
||||||
return $isSingle ? MediaData::from($medias->first()) : MediaData::collection($medias);
|
return $isSingle ? MediaData::from($medias->first()) : MediaData::collection($medias);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function storeDeferred(Request $request)
|
|
||||||
{
|
|
||||||
$modelName = ModelRule::getModelClassName($request->input('parent'));
|
|
||||||
$collection = ModelRule::getCollection($request->input('parent'));
|
|
||||||
$isSingle = 1 === $collection->collectionSizeLimit;
|
|
||||||
|
|
||||||
$request->validate($isSingle ? [
|
|
||||||
'payload' => 'array',
|
|
||||||
'payload.name' => 'required|string|regex:/\..*$/|max:255',
|
|
||||||
'payload.content' => 'required|string',
|
|
||||||
] : [
|
|
||||||
'payload' => 'required|array|min:1',
|
|
||||||
'payload.*' => 'array',
|
|
||||||
'payload.*.name' => 'required|string|regex:/\..*$/|max:255',
|
|
||||||
'payload.*.content' => 'required|string',
|
|
||||||
]);
|
|
||||||
|
|
||||||
$content = $isSingle ? [$request->input('payload')] : $request->input('payload');
|
|
||||||
|
|
||||||
$tempPaths = collect($content)->map(function ($c) use ($collection, $modelName) {
|
|
||||||
$file = new MediaFile($c['name']);
|
|
||||||
|
|
||||||
$tmpFile = $this->storeTemporaryFile($c['content'], $collection);
|
|
||||||
Storage::disk(config('media-library.temp_disk'))->move($tmpFile, 'media-library/' . $file->getFilename());
|
|
||||||
return 'media-library/' . $file->getFilename();
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->authorize('storeMedia', [$modelName, null, $collection->name]);
|
|
||||||
return $isSingle ? DeferredMediaData::fromPath($tempPaths->first(), $collection) : DeferredMediaData::collectionFromPaths($tempPaths, $collection);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function update(Request $request, Media $media): MediaData
|
public function update(Request $request, Media $media): MediaData
|
||||||
{
|
{
|
||||||
$this->authorize('updateMedia', [$media->model, $media->collection_name]);
|
$this->authorize('updateMedia', [$media->model, $media->collection_name]);
|
||||||
|
@ -148,7 +116,27 @@ class MediaController
|
||||||
return property_exists($collection, $callback);
|
return property_exists($collection, $callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function storeTemporaryFile(string $data, MediaCollection $collection): string
|
protected function validateModel(Request $request): HasMedia
|
||||||
|
{
|
||||||
|
$model = app('media-library-helpers')->get($request->input('model'));
|
||||||
|
|
||||||
|
$request->validate([
|
||||||
|
'collection' => [
|
||||||
|
'required',
|
||||||
|
'string',
|
||||||
|
Rule::in((new $model())->getRegisteredMediaCollections()->pluck('name')),
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$model = $model::find($request->input('id'));
|
||||||
|
if (!$model) {
|
||||||
|
throw ValidationException::withMessages(['model' => 'nicht gefunden']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $model;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function fileAdderFromData($model, $data, $collection): FileAdder
|
||||||
{
|
{
|
||||||
$maxWidth = $collection->runCallback('maxWidth', 9);
|
$maxWidth = $collection->runCallback('maxWidth', 9);
|
||||||
if (str_contains($data, ';base64')) {
|
if (str_contains($data, ';base64')) {
|
||||||
|
@ -168,20 +156,13 @@ class MediaController
|
||||||
throw InvalidBase64Data::create();
|
throw InvalidBase64Data::create();
|
||||||
}
|
}
|
||||||
|
|
||||||
$tmpFile = 'media-library/' . str()->uuid()->toString();
|
$tmpFile = tempnam(sys_get_temp_dir(), 'media-library');
|
||||||
Storage::disk(config('media-library.temp_disk'))->put($tmpFile, $binaryData);
|
file_put_contents($tmpFile, $binaryData);
|
||||||
|
|
||||||
if (null !== $maxWidth && 'image/jpeg' === Storage::disk(config('media-library.temp_disk'))->mimeType($tmpFile)) {
|
if (null !== $maxWidth && 'image/jpeg' === mime_content_type($tmpFile)) {
|
||||||
Image::load(Storage::disk(config('media-library.temp_disk'))->path($tmpFile))->width($maxWidth)->save();
|
Image::load($tmpFile)->width($maxWidth)->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $tmpFile;
|
return $model->addMedia($tmpFile);
|
||||||
}
|
|
||||||
|
|
||||||
protected function fileAdderFromData($model, $data, $collection): FileAdder
|
|
||||||
{
|
|
||||||
$tmpFile = $this->storeTemporaryFile($data, $collection);
|
|
||||||
|
|
||||||
return $model->addMediaFromDisk($tmpFile, config('media-library.temp_disk'));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,14 +37,11 @@ class MediaData extends Data
|
||||||
|
|
||||||
public bool $fallback = false;
|
public bool $fallback = false;
|
||||||
|
|
||||||
public bool $isDeferred = false;
|
|
||||||
|
|
||||||
public static function fromMedia(Media $media): self
|
public static function fromMedia(Media $media): self
|
||||||
{
|
{
|
||||||
$conversions = collect($media->getMediaConversionNames())->flip()->map(
|
$conversions = collect($media->getMediaConversionNames())->flip()->map(fn ($integer, $conversion) => $media->hasGeneratedConversion($conversion)
|
||||||
fn ($integer, $conversion) => $media->hasGeneratedConversion($conversion)
|
? ['original_url' => $media->getFullUrl($conversion)]
|
||||||
? ['original_url' => $media->getFullUrl($conversion)]
|
: null,
|
||||||
: null,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return self::withoutMagicalCreationFrom([
|
return self::withoutMagicalCreationFrom([
|
||||||
|
|
|
@ -1,89 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Zoomyboy\MedialibraryHelper\Rules;
|
|
||||||
|
|
||||||
use Illuminate\Contracts\Validation\InvokableRule;
|
|
||||||
use Spatie\MediaLibrary\HasMedia;
|
|
||||||
use Illuminate\Validation\Factory;
|
|
||||||
use Illuminate\Validation\Rule;
|
|
||||||
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
|
|
||||||
|
|
||||||
class ModelRule implements InvokableRule
|
|
||||||
{
|
|
||||||
|
|
||||||
public ?string $collection;
|
|
||||||
public ?int $id;
|
|
||||||
public ?string $model;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array{?id: int, ?collection: string, ?model: string} $attribute
|
|
||||||
*/
|
|
||||||
public function __invoke($attribute, $value, $fail)
|
|
||||||
{
|
|
||||||
app(Factory::class)->make([$attribute => $value], [
|
|
||||||
"{$attribute}.id" => 'nullable|integer|gt:0',
|
|
||||||
"{$attribute}.model" => ['required', 'string', Rule::in(app('media-library-helpers')->keys())],
|
|
||||||
"{$attribute}.collection" => 'required|string',
|
|
||||||
])->validate();
|
|
||||||
|
|
||||||
|
|
||||||
$this->model = data_get($value, 'model');
|
|
||||||
$this->id = data_get($value, 'id');
|
|
||||||
$this->collection = data_get($value, 'collection');
|
|
||||||
|
|
||||||
if (is_null($this->id)) {
|
|
||||||
$this->validateDeferred($attribute, $value);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$model = app('media-library-helpers')->get($this->model);
|
|
||||||
app(Factory::class)->make([$attribute => $value], [
|
|
||||||
"{$attribute}.collection" => ['required', Rule::in((new $model())->getRegisteredMediaCollections()->pluck('name'))],
|
|
||||||
"{$attribute}.id" => ['required', 'exists:' . (new $model)->getTable() . ',id'],
|
|
||||||
])->validate();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function validateDeferred($attribute, $value): void
|
|
||||||
{
|
|
||||||
app(Factory::class)->make([$attribute => $value], [
|
|
||||||
"{$attribute}.model" => ['required', 'string', Rule::in(app('media-library-helpers')->keys())],
|
|
||||||
"{$attribute}.collection" => 'required|string',
|
|
||||||
])->validate();
|
|
||||||
|
|
||||||
$model = app('media-library-helpers')->get($this->model);
|
|
||||||
|
|
||||||
app(Factory::class)->make([$attribute => $value], [
|
|
||||||
"{$attribute}.collection" => ['required', Rule::in((new $model())->getRegisteredMediaCollections()->pluck('name'))],
|
|
||||||
])->validate();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array{?id: int, ?collection: string, ?model: string} $modelParam
|
|
||||||
*/
|
|
||||||
public static function getModel($modelParam): HasMedia
|
|
||||||
{
|
|
||||||
$model = static::getModelClassName($modelParam);
|
|
||||||
|
|
||||||
return $model::find($modelParam['id']);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array{?id: int, ?collection: string, ?model: string} $modelParam
|
|
||||||
* @return class-string<HasMedia>
|
|
||||||
*/
|
|
||||||
public static function getModelClassName($modelParam): string
|
|
||||||
{
|
|
||||||
return app('media-library-helpers')->get($modelParam['model']);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array{?id: int, ?collection: string, ?model: string} $modelParam
|
|
||||||
*/
|
|
||||||
public static function getCollection($modelParam): MediaCollection
|
|
||||||
{
|
|
||||||
$className = static::getModelClassName($modelParam);
|
|
||||||
|
|
||||||
return (new $className)->getMediaCollection($modelParam['collection']);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -12,8 +12,6 @@ class ServiceProvider extends BaseServiceProvider
|
||||||
{
|
{
|
||||||
app()->bind('media-library-helpers', fn () => collect([]));
|
app()->bind('media-library-helpers', fn () => collect([]));
|
||||||
app()->singleton(CollectionExtension::class, fn () => new CollectionExtension());
|
app()->singleton(CollectionExtension::class, fn () => new CollectionExtension());
|
||||||
|
|
||||||
$this->mergeConfigFrom(__DIR__ . '/../config/media-library.php', 'media-library');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function boot(): void
|
public function boot(): void
|
||||||
|
@ -26,8 +24,8 @@ class ServiceProvider extends BaseServiceProvider
|
||||||
$router->patch('mediaupload/{media}', [MediaController::class, 'update'])->name('media.update');
|
$router->patch('mediaupload/{media}', [MediaController::class, 'update'])->name('media.update');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
MediaCollection::mixin(app(CollectionExtension::class));
|
MediaCollection::mixin(app(CollectionExtension::class));
|
||||||
|
// app(CollectionExtension::class)->boot();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
providers:
|
|
||||||
- Spatie\MediaLibrary\MediaLibraryServiceProvider
|
|
||||||
- Spatie\LaravelData\LaravelDataServiceProvider
|
|
||||||
- Zoomyboy\MedialibraryHelper\ServiceProvider
|
|
||||||
|
|
||||||
migrations:
|
|
||||||
- tests/workbench/database/migrations
|
|
||||||
|
|
||||||
workbench:
|
|
||||||
start: '/'
|
|
||||||
install: false
|
|
||||||
discovers:
|
|
||||||
web: false
|
|
||||||
api: false
|
|
||||||
commands: false
|
|
||||||
components: false
|
|
||||||
views: false
|
|
||||||
build: []
|
|
||||||
assets: []
|
|
||||||
sync: []
|
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Workbench\App\Events;
|
namespace Zoomyboy\MedialibraryHelper\Tests\Events;
|
||||||
|
|
||||||
use Illuminate\Foundation\Events\Dispatchable;
|
use Illuminate\Foundation\Events\Dispatchable;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Workbench\App\Events;
|
namespace Zoomyboy\MedialibraryHelper\Tests\Events;
|
||||||
|
|
||||||
use Illuminate\Foundation\Events\Dispatchable;
|
use Illuminate\Foundation\Events\Dispatchable;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Workbench\App\Events;
|
namespace Zoomyboy\MedialibraryHelper\Tests\Events;
|
||||||
|
|
||||||
use Illuminate\Foundation\Events\Dispatchable;
|
use Illuminate\Foundation\Events\Dispatchable;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
|
@ -1,81 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Storage;
|
|
||||||
|
|
||||||
test('it uploads a deferred file to a collection', function () {
|
|
||||||
$this->auth()->registerModel()->withoutExceptionHandling();
|
|
||||||
$content = base64_encode($this->pdfFile()->getContent());
|
|
||||||
|
|
||||||
$payload = [
|
|
||||||
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => null],
|
|
||||||
'payload' => [
|
|
||||||
'content' => $content,
|
|
||||||
'name' => 'beispiel bild.jpg',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
$this->postJson('/mediaupload', $payload)
|
|
||||||
->assertStatus(201)
|
|
||||||
->assertJson([
|
|
||||||
'is_deferred' => true,
|
|
||||||
'original_url' => Storage::disk('temp')->url('media-library/beispiel bild.jpg'),
|
|
||||||
'name' => 'beispiel bild',
|
|
||||||
'collection_name' => 'defaultSingleFile',
|
|
||||||
'size' => 3028,
|
|
||||||
'file_name' => 'beispiel bild.jpg',
|
|
||||||
'mime_type' => 'application/pdf',
|
|
||||||
]);
|
|
||||||
Storage::disk('temp')->assertExists('media-library/beispiel bild.jpg');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('it handles authorization with collection', function () {
|
|
||||||
$this->auth(['storeMedia' => ['collection' => 'rtrt']])->registerModel();
|
|
||||||
$content = base64_encode($this->pdfFile()->getContent());
|
|
||||||
|
|
||||||
$this->postJson('/mediaupload', [
|
|
||||||
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => null],
|
|
||||||
'payload' => [
|
|
||||||
'content' => $content,
|
|
||||||
'name' => 'beispiel bild.jpg',
|
|
||||||
],
|
|
||||||
])->assertStatus(403);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('it handles authorization with collection correctly', function () {
|
|
||||||
$this->auth(['storeMedia' => ['collection' => 'defaultSingleFile']])->registerModel();
|
|
||||||
$content = base64_encode($this->pdfFile()->getContent());
|
|
||||||
|
|
||||||
$this->postJson('/mediaupload', [
|
|
||||||
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => null],
|
|
||||||
'payload' => [
|
|
||||||
'content' => $content,
|
|
||||||
'name' => 'beispiel bild.jpg',
|
|
||||||
],
|
|
||||||
])->assertStatus(201);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('it needs a collection', function ($key, $value) {
|
|
||||||
$this->auth()->registerModel();
|
|
||||||
$content = base64_encode($this->pdfFile()->getContent());
|
|
||||||
|
|
||||||
$payload = [
|
|
||||||
'parent' => ['model' => 'post', 'collection' => '', 'id' => null],
|
|
||||||
'payload' => [
|
|
||||||
'content' => $content,
|
|
||||||
'name' => 'beispiel bild.jpg',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
data_set($payload, $key, $value);
|
|
||||||
|
|
||||||
$this->postJson('/mediaupload', $payload)->assertJsonValidationErrors($key);
|
|
||||||
})->with(function () {
|
|
||||||
yield ['parent.collection', ''];
|
|
||||||
yield ['parent.collection', -1];
|
|
||||||
yield ['parent.collection', 'missingcollection'];
|
|
||||||
yield ['parent.model', 'lalala'];
|
|
||||||
yield ['parent.model', -1];
|
|
||||||
yield ['parent.model', ''];
|
|
||||||
});
|
|
|
@ -3,8 +3,8 @@
|
||||||
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Workbench\App\Events\MediaChange;
|
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
|
||||||
use Workbench\App\Events\MediaDestroyed;
|
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaDestroyed;
|
||||||
|
|
||||||
test('it deletes multiple media', function () {
|
test('it deletes multiple media', function () {
|
||||||
$this->auth()->registerModel()->withoutExceptionHandling();
|
$this->auth()->registerModel()->withoutExceptionHandling();
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
|
||||||
|
|
||||||
use Illuminate\Foundation\Console\VendorPublishCommand;
|
|
||||||
use Illuminate\Support\Facades\Artisan;
|
|
||||||
use Spatie\MediaLibrary\MediaLibraryServiceProvider;
|
|
||||||
|
|
||||||
afterEach(function () {
|
|
||||||
@unlink(config_path('media-library.php'));
|
|
||||||
});
|
|
||||||
|
|
||||||
test('modifies config file', function () {
|
|
||||||
Artisan::call(VendorPublishCommand::class, ['--provider' => MediaLibraryServiceProvider::class, '--tag' => 'config']);
|
|
||||||
$configContents = file_get_contents(config_path('media-library.php'));
|
|
||||||
$configContents = preg_replace('/\'image_driver\' => env.*/', '\'image_driver\' => "lala",', $configContents);
|
|
||||||
file_put_contents(config_path('media-library.php'), $configContents);
|
|
||||||
|
|
||||||
$this->tearDownTheTestEnvironment();
|
|
||||||
$this->setUpTheTestEnvironment();
|
|
||||||
|
|
||||||
$this->assertEquals('lala', config('media-library.image_driver'));
|
|
||||||
$this->assertEquals('temp', config('media-library.temp_disk'));
|
|
||||||
});
|
|
|
@ -3,7 +3,7 @@
|
||||||
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Workbench\App\Events\MediaChange;
|
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
|
||||||
|
|
||||||
test('it can reorder media', function () {
|
test('it can reorder media', function () {
|
||||||
Event::fake();
|
Event::fake();
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Workbench\App\Events\MediaChange;
|
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
|
||||||
|
|
||||||
test('it updates a single files properties', function () {
|
test('it updates a single files properties', function () {
|
||||||
Event::fake();
|
Event::fake();
|
||||||
|
|
|
@ -4,8 +4,8 @@ namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Workbench\App\Events\MediaChange;
|
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
|
||||||
use Workbench\App\Events\MediaStored;
|
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
|
||||||
|
|
||||||
test('it uploads a single file to a single file collection', function () {
|
test('it uploads a single file to a single file collection', function () {
|
||||||
$this->auth()->registerModel();
|
$this->auth()->registerModel();
|
||||||
|
@ -13,7 +13,9 @@ test('it uploads a single file to a single file collection', function () {
|
||||||
$content = base64_encode($this->pdfFile()->getContent());
|
$content = base64_encode($this->pdfFile()->getContent());
|
||||||
|
|
||||||
$response = $this->postJson('/mediaupload', [
|
$response = $this->postJson('/mediaupload', [
|
||||||
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => $post->id],
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'defaultSingleFile',
|
||||||
'payload' => [
|
'payload' => [
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
|
@ -29,18 +31,92 @@ test('it uploads a single file to a single file collection', function () {
|
||||||
$response->assertJsonPath('name', 'beispiel bild');
|
$response->assertJsonPath('name', 'beispiel bild');
|
||||||
$response->assertJsonPath('collection_name', 'defaultSingleFile');
|
$response->assertJsonPath('collection_name', 'defaultSingleFile');
|
||||||
$response->assertJsonPath('file_name', 'beispiel-bild.jpg');
|
$response->assertJsonPath('file_name', 'beispiel-bild.jpg');
|
||||||
$response->assertJsonPath('is_deferred', false);
|
|
||||||
$response->assertJsonMissingPath('model_type');
|
$response->assertJsonMissingPath('model_type');
|
||||||
$response->assertJsonMissingPath('model_id');
|
$response->assertJsonMissingPath('model_id');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('test validation', function (array $attributes, string $messages) {
|
||||||
|
$this->auth()->registerModel();
|
||||||
|
$post = $this->newPost();
|
||||||
|
$content = base64_encode($this->pdfFile()->getContent());
|
||||||
|
|
||||||
|
$this->postJson('/mediaupload', [
|
||||||
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'defaultSingleFile',
|
||||||
|
'payload' => [
|
||||||
|
'content' => $content,
|
||||||
|
'name' => 'beispiel bild.jpg',
|
||||||
|
],
|
||||||
|
...$attributes
|
||||||
|
])->assertJsonValidationErrors($messages);
|
||||||
|
})->with([
|
||||||
|
'missing collection' => [
|
||||||
|
['collection' => ''],
|
||||||
|
'collection'
|
||||||
|
],
|
||||||
|
'missing id' => [
|
||||||
|
['id' => ''],
|
||||||
|
'id'
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
test('test validation for payload', function () {
|
||||||
|
$this->auth()->registerModel();
|
||||||
|
$post = $this->newPost();
|
||||||
|
|
||||||
|
$this->postJson('/mediaupload', [
|
||||||
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'defaultSingleFile',
|
||||||
|
'payload' => [
|
||||||
|
'content' => '',
|
||||||
|
'name' => 'beispiel bild.jpg',
|
||||||
|
],
|
||||||
|
])->assertJsonValidationErrors('payload.content');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('test validation for name', function () {
|
||||||
|
$this->auth()->registerModel();
|
||||||
|
$post = $this->newPost();
|
||||||
|
$content = base64_encode($this->pdfFile()->getContent());
|
||||||
|
|
||||||
|
$this->postJson('/mediaupload', [
|
||||||
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'defaultSingleFile',
|
||||||
|
'payload' => [
|
||||||
|
'content' => $content,
|
||||||
|
'name' => '',
|
||||||
|
],
|
||||||
|
])->assertJsonValidationErrors('payload.name');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('test validation for extension', function () {
|
||||||
|
$this->auth()->registerModel();
|
||||||
|
$post = $this->newPost();
|
||||||
|
$content = base64_encode($this->pdfFile()->getContent());
|
||||||
|
|
||||||
|
$this->postJson('/mediaupload', [
|
||||||
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'defaultSingleFile',
|
||||||
|
'payload' => [
|
||||||
|
'content' => $content,
|
||||||
|
'name' => 'aaa',
|
||||||
|
],
|
||||||
|
])->assertJsonValidationErrors('payload.name');
|
||||||
|
});
|
||||||
|
|
||||||
test('it uploads a single image to a single file collection', function () {
|
test('it uploads a single image to a single file collection', function () {
|
||||||
$this->auth()->registerModel()->withoutExceptionHandling();
|
$this->auth()->registerModel();
|
||||||
$post = $this->newPost();
|
$post = $this->newPost();
|
||||||
$content = base64_encode($this->jpgFile()->getContent());
|
$content = base64_encode($this->jpgFile()->getContent());
|
||||||
|
|
||||||
$response = $this->postJson('/mediaupload', [
|
$response = $this->postJson('/mediaupload', [
|
||||||
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => $post->id],
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'defaultSingleFile',
|
||||||
'payload' => [
|
'payload' => [
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
|
@ -58,7 +134,9 @@ test('it forces a filename for a single collection', function () {
|
||||||
$content = base64_encode($this->pdfFile()->getContent());
|
$content = base64_encode($this->pdfFile()->getContent());
|
||||||
|
|
||||||
$response = $this->postJson('/mediaupload', [
|
$response = $this->postJson('/mediaupload', [
|
||||||
'parent' => ['model' => 'post', 'collection' => 'singleForced', 'id' => $post->id],
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'singleForced',
|
||||||
'payload' => [
|
'payload' => [
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
|
@ -77,7 +155,9 @@ test('it sets custom title when storing', function () {
|
||||||
$content = base64_encode($this->pdfFile()->getContent());
|
$content = base64_encode($this->pdfFile()->getContent());
|
||||||
|
|
||||||
$response = $this->postJson('/mediaupload', [
|
$response = $this->postJson('/mediaupload', [
|
||||||
'parent' => ['model' => 'post', 'collection' => 'singleStoringHook', 'id' => $post->id],
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'singleStoringHook',
|
||||||
'payload' => [
|
'payload' => [
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
|
@ -97,7 +177,9 @@ test('it sets custom properties from properties method', function () {
|
||||||
$content = base64_encode($this->pdfFile()->getContent());
|
$content = base64_encode($this->pdfFile()->getContent());
|
||||||
|
|
||||||
$response = $this->postJson('/mediaupload', [
|
$response = $this->postJson('/mediaupload', [
|
||||||
'parent' => ['model' => 'post', 'collection' => 'multipleProperties', 'id' => $post->id],
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'multipleProperties',
|
||||||
'payload' => [
|
'payload' => [
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
|
@ -117,7 +199,9 @@ test('it forces a filename for multiple collections', function () {
|
||||||
$content = base64_encode($this->pdfFile()->getContent());
|
$content = base64_encode($this->pdfFile()->getContent());
|
||||||
|
|
||||||
$response = $this->postJson('/mediaupload', [
|
$response = $this->postJson('/mediaupload', [
|
||||||
'parent' => ['model' => 'post', 'collection' => 'multipleForced', 'id' => $post->id],
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'multipleForced',
|
||||||
'payload' => [
|
'payload' => [
|
||||||
[
|
[
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
|
@ -138,7 +222,9 @@ test('it throws event when file has been uploaded', function () {
|
||||||
$content = base64_encode($this->pdfFile()->getContent());
|
$content = base64_encode($this->pdfFile()->getContent());
|
||||||
|
|
||||||
$response = $this->postJson('/mediaupload', [
|
$response = $this->postJson('/mediaupload', [
|
||||||
'parent' => ['model' => 'post', 'collection' => 'singleWithEvent', 'id' => $post->id],
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'singleWithEvent',
|
||||||
'payload' => [
|
'payload' => [
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
|
@ -158,7 +244,9 @@ test('it throws event when multiple files uploaded', function () {
|
||||||
$content = base64_encode($this->pdfFile()->getContent());
|
$content = base64_encode($this->pdfFile()->getContent());
|
||||||
|
|
||||||
$response = $this->postJson('/mediaupload', [
|
$response = $this->postJson('/mediaupload', [
|
||||||
'parent' => ['model' => 'post', 'collection' => 'multipleFilesWithEvent', 'id' => $post->id],
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'multipleFilesWithEvent',
|
||||||
'payload' => [
|
'payload' => [
|
||||||
[
|
[
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
|
@ -183,7 +271,9 @@ test('it uploads multiple files', function () {
|
||||||
$post->addMedia($file->getPathname())->preservingOriginal()->toMediaCollection('images');
|
$post->addMedia($file->getPathname())->preservingOriginal()->toMediaCollection('images');
|
||||||
|
|
||||||
$response = $this->postJson('/mediaupload', [
|
$response = $this->postJson('/mediaupload', [
|
||||||
'parent' => ['model' => 'post', 'collection' => 'images', 'id' => $post->id],
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'images',
|
||||||
'payload' => [
|
'payload' => [
|
||||||
[
|
[
|
||||||
'content' => base64_encode($file->getContent()),
|
'content' => base64_encode($file->getContent()),
|
||||||
|
@ -215,125 +305,80 @@ test('it returns 403 when not authorized', function () {
|
||||||
$this->auth(['storeMedia' => false])->registerModel();
|
$this->auth(['storeMedia' => false])->registerModel();
|
||||||
$post = $this->newPost();
|
$post = $this->newPost();
|
||||||
|
|
||||||
$this->postJson('/mediaupload', [
|
$response = $this->postJson('/mediaupload', [
|
||||||
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => $post->id],
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'defaultSingleFile',
|
||||||
'payload' => [
|
'payload' => [
|
||||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
],
|
],
|
||||||
])->assertStatus(403);
|
]);
|
||||||
|
|
||||||
|
$response->assertStatus(403);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('it checks for model when running authorization', function () {
|
test('it needs validation for single files', function (array $payload, string $invalidFieldName) {
|
||||||
$otherPost = $this->newPost();
|
|
||||||
$post = $this->newPost();
|
|
||||||
$this->auth(['storeMedia' => ['id' => $post->id, 'collection' => 'defaultSingleFile']])->registerModel();
|
|
||||||
|
|
||||||
$this->postJson('/mediaupload', [
|
|
||||||
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => $post->id],
|
|
||||||
'payload' => [
|
|
||||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
|
||||||
'name' => 'beispiel bild.jpg',
|
|
||||||
],
|
|
||||||
])->assertStatus(201);
|
|
||||||
|
|
||||||
$this->postJson('/mediaupload', [
|
|
||||||
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => $otherPost->id],
|
|
||||||
'payload' => [
|
|
||||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
|
||||||
'name' => 'beispiel bild.jpg',
|
|
||||||
],
|
|
||||||
])->assertStatus(403);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('it checks for collection when running authorization', function () {
|
|
||||||
$post = $this->newPost();
|
|
||||||
$this->auth(['storeMedia' => ['id' => $post->id, 'collection' => 'defaultSingleFile']])->registerModel();
|
|
||||||
|
|
||||||
$this->postJson('/mediaupload', [
|
|
||||||
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => $post->id],
|
|
||||||
'payload' => [
|
|
||||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
|
||||||
'name' => 'beispiel bild.jpg',
|
|
||||||
],
|
|
||||||
])->assertStatus(201);
|
|
||||||
|
|
||||||
$this->postJson('/mediaupload', [
|
|
||||||
'parent' => ['model' => 'post', 'collection' => 'singleWithEvent', 'id' => $post->id],
|
|
||||||
'payload' => [
|
|
||||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
|
||||||
'name' => 'beispiel bild.jpg',
|
|
||||||
],
|
|
||||||
])->assertStatus(403);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('it needs validation for single files', function (array $payloadOverwrites, string $invalidFieldName) {
|
|
||||||
$this->auth()->registerModel();
|
$this->auth()->registerModel();
|
||||||
$post = $this->newPost();
|
$post = $this->newPost();
|
||||||
|
|
||||||
$payload = [
|
$response = $this->postJson('/mediaupload', [
|
||||||
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => $post->id],
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'defaultSingleFile',
|
||||||
'payload' => [
|
'payload' => [
|
||||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
],
|
],
|
||||||
];
|
...$payload,
|
||||||
|
]);
|
||||||
foreach ($payloadOverwrites as $key => $value) {
|
|
||||||
data_set($payload, $key, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response = $this->postJson('/mediaupload', $payload);
|
|
||||||
|
|
||||||
$response->assertStatus(422);
|
$response->assertStatus(422);
|
||||||
$response->assertJsonValidationErrors($invalidFieldName);
|
$response->assertJsonValidationErrors($invalidFieldName);
|
||||||
})->with(function () {
|
})->with(function () {
|
||||||
yield [['parent.model' => 'missingmodel'], 'parent.model'];
|
yield [['model' => 'missingmodel'], 'model'];
|
||||||
yield [['parent.id' => -1], 'parent.id'];
|
yield [['id' => -1], 'model'];
|
||||||
yield [['parent.collection' => 'missingcollection'], 'parent.collection'];
|
yield [['collection' => 'missingcollection'], 'collection'];
|
||||||
yield [['payload.content' => []], 'payload.content'];
|
yield [['payload' => ['name' => 'AAA', 'content' => []]], 'payload.content'];
|
||||||
yield [['payload.content' => ['UU']], 'payload.content'];
|
yield [['payload' => ['name' => 'AAA', 'content' => ['UU']]], 'payload.content'];
|
||||||
yield [['payload.content' => null], 'payload.content'];
|
yield [['payload' => ['name' => 'AAA', 'content' => null]], 'payload.content'];
|
||||||
yield [['payload.content' => ''], 'payload.content'];
|
yield [['payload' => ['name' => 'AAA', 'content' => '']], 'payload.content'];
|
||||||
yield [['payload.content' => 1], 'payload.content'];
|
yield [['payload' => ['name' => 'AAA', 'content' => 1]], 'payload.content'];
|
||||||
yield [['payload.name' => ''], 'payload.name'];
|
yield [['payload' => ['name' => '', 'content' => 'aaadfdf']], 'payload.name'];
|
||||||
yield [['payload.name' => ['U']], 'payload.name'];
|
yield [['payload' => ['name' => ['U'], 'content' => 'aaadfdf']], 'payload.name'];
|
||||||
yield [['payload.name' => 1], 'payload.name'];
|
yield [['payload' => ['name' => 1, 'content' => 'aaadfdf']], 'payload.name'];
|
||||||
yield [['payload.name' => null], 'payload.name'];
|
yield [['payload' => ['name' => null, 'content' => 'aaadfdf']], 'payload.name'];
|
||||||
yield [['payload' => 'lalal'], 'payload'];
|
yield [['payload' => 'lalal'], 'payload'];
|
||||||
yield [['payload' => 55], 'payload'];
|
yield [['payload' => 55], 'payload'];
|
||||||
});
|
});
|
||||||
|
|
||||||
test('it needs validation for multiple files', function (array $payloadOverwrites, string $invalidFieldName) {
|
test('it needs validation for multiple files', function (array $payload, string $invalidFieldName) {
|
||||||
$this->auth()->registerModel();
|
$this->auth()->registerModel();
|
||||||
$post = $this->newPost();
|
$post = $this->newPost();
|
||||||
|
|
||||||
$payload = [
|
$response = $this->postJson('/mediaupload', [
|
||||||
'parent' => ['model' => 'post', 'collection' => 'images', 'id' => $post->id],
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'images',
|
||||||
'payload' => [
|
'payload' => [
|
||||||
[
|
[
|
||||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
];
|
...$payload,
|
||||||
|
]);
|
||||||
foreach ($payloadOverwrites as $key => $value) {
|
|
||||||
data_set($payload, $key, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response = $this->postJson('/mediaupload', $payload);
|
|
||||||
|
|
||||||
|
|
||||||
$response->assertStatus(422);
|
$response->assertStatus(422);
|
||||||
$response->assertJsonValidationErrors($invalidFieldName);
|
$response->assertJsonValidationErrors($invalidFieldName);
|
||||||
})->with(function () {
|
})->with(function () {
|
||||||
yield [['parent.model' => 'missingmodel'], 'parent.model'];
|
yield [['model' => 'missingmodel'], 'model'];
|
||||||
yield [['parent.model' => 'post.missingcollection'], 'parent.model'];
|
yield [['id' => -1], 'model'];
|
||||||
yield [['parent.id' => -1], 'parent.id'];
|
yield [['collection' => 'missingcollection'], 'collection'];
|
||||||
yield [['payload' => 'lalal'], 'payload'];
|
yield [['payload' => 'lalal'], 'payload'];
|
||||||
yield [['payload' => []], 'payload'];
|
yield [['payload' => []], 'payload'];
|
||||||
yield [['payload' => 1], 'payload'];
|
yield [['payload' => 1], 'payload'];
|
||||||
|
|
||||||
yield [['payload' => [['name' => 'AAA', 'content' => []]]], 'payload.0.content'];
|
yield [['payload' => [['name' => 'AAA', 'content' => []]]], 'payload.0.content'];
|
||||||
yield [['payload' => [['name' => 'AAA', 'content' => ['UU']]]], 'payload.0.content'];
|
yield [['payload' => [['name' => 'AAA', 'content' => ['UU']]]], 'payload.0.content'];
|
||||||
yield [['payload' => [['name' => 'AAA', 'content' => null]]], 'payload.0.content'];
|
yield [['payload' => [['name' => 'AAA', 'content' => null]]], 'payload.0.content'];
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Workbench\App\Models;
|
namespace Zoomyboy\MedialibraryHelper\Tests\Models;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
|
@ -8,9 +8,9 @@ use Illuminate\Support\Str;
|
||||||
use Spatie\MediaLibrary\HasMedia;
|
use Spatie\MediaLibrary\HasMedia;
|
||||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||||
use Workbench\App\Events\MediaChange;
|
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
|
||||||
use Workbench\App\Events\MediaDestroyed;
|
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaDestroyed;
|
||||||
use Workbench\App\Events\MediaStored;
|
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
|
||||||
|
|
||||||
class Post extends Model implements HasMedia
|
class Post extends Model implements HasMedia
|
||||||
{
|
{
|
|
@ -1,11 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Illuminate\Auth\Access\Gate;
|
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Workbench\App\Models\Post;
|
|
||||||
use Workbench\App\Policies\PostPolicy;
|
|
||||||
|
|
||||||
uses(Zoomyboy\MedialibraryHelper\Tests\TestCase::class)->in('Feature');
|
uses(Zoomyboy\MedialibraryHelper\Tests\TestCase::class)->in('Feature');
|
||||||
uses(Illuminate\Foundation\Testing\RefreshDatabase::class)->in('Feature');
|
uses(Illuminate\Foundation\Testing\RefreshDatabase::class)->in('Feature');
|
||||||
uses()->beforeEach(fn () => Storage::fake('media') && Storage::fake('temp'))->in('Feature');
|
uses()->beforeEach(fn () => Storage::fake('media'))->in('Feature');
|
||||||
uses()->beforeEach(fn () => Gate::policy(Post::class, PostPolicy::class))->in('Feature');
|
|
||||||
|
|
|
@ -4,15 +4,30 @@ namespace Zoomyboy\MedialibraryHelper\Tests;
|
||||||
|
|
||||||
use Illuminate\Http\File;
|
use Illuminate\Http\File;
|
||||||
use Illuminate\Support\Facades\Gate;
|
use Illuminate\Support\Facades\Gate;
|
||||||
use Orchestra\Testbench\Concerns\WithWorkbench;
|
|
||||||
use Orchestra\Testbench\TestCase as BaseTestCase;
|
use Orchestra\Testbench\TestCase as BaseTestCase;
|
||||||
use Workbench\App\Models\Post;
|
use Spatie\LaravelData\LaravelDataServiceProvider;
|
||||||
use Workbench\App\Models\User;
|
use Spatie\MediaLibrary\MediaLibraryServiceProvider;
|
||||||
|
use Zoomyboy\MedialibraryHelper\ServiceProvider;
|
||||||
|
use Zoomyboy\MedialibraryHelper\Tests\Models\Post;
|
||||||
|
|
||||||
class TestCase extends BaseTestCase
|
class TestCase extends BaseTestCase
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Define database migrations.
|
||||||
|
*/
|
||||||
|
protected function defineDatabaseMigrations(): void
|
||||||
|
{
|
||||||
|
$this->loadMigrationsFrom(__DIR__ . '/migrations');
|
||||||
|
}
|
||||||
|
|
||||||
use WithWorkbench;
|
protected function getPackageProviders($app): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
ServiceProvider::class,
|
||||||
|
MediaLibraryServiceProvider::class,
|
||||||
|
LaravelDataServiceProvider::class,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a pdf file with a filename and get path.
|
* Generate a pdf file with a filename and get path.
|
||||||
|
@ -50,8 +65,23 @@ class TestCase extends BaseTestCase
|
||||||
|
|
||||||
protected function auth(array $policies = []): self
|
protected function auth(array $policies = []): self
|
||||||
{
|
{
|
||||||
$this->be(User::factory()->policies($policies)->create());
|
$policies = [
|
||||||
|
'storeMedia' => true,
|
||||||
|
'updateMedia' => true,
|
||||||
|
'destroyMedia' => true,
|
||||||
|
'listMedia' => true,
|
||||||
|
...$policies,
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($policies as $ability => $result) {
|
||||||
|
Gate::define($ability, fn (?string $user, string $collectionName) => $result);
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function defineEnvironment($app)
|
||||||
|
{
|
||||||
|
$app['config']->set('media-library.middleware', ['web']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Workbench\App\Models;
|
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
||||||
|
|
||||||
class User extends Authenticatable
|
|
||||||
{
|
|
||||||
use HasFactory;
|
|
||||||
|
|
||||||
public $guarded = [];
|
|
||||||
|
|
||||||
public $casts = [
|
|
||||||
'policies' => 'json',
|
|
||||||
];
|
|
||||||
}
|
|
|
@ -1,56 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Workbench\App\Policies;
|
|
||||||
|
|
||||||
use Illuminate\Auth\Access\HandlesAuthorization;
|
|
||||||
use Spatie\MediaLibrary\HasMedia;
|
|
||||||
use Workbench\App\Models\User;
|
|
||||||
|
|
||||||
class PostPolicy
|
|
||||||
{
|
|
||||||
use HandlesAuthorization;
|
|
||||||
|
|
||||||
public function listMedia(User $user, HasMedia $model): bool
|
|
||||||
{
|
|
||||||
if (is_bool($user->policies['listMedia'])) {
|
|
||||||
return $user->policies['listMedia'] === true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return data_get($user->policies, 'listMedia.id') === $model->id
|
|
||||||
&& data_get($user->policies, 'listMedia.collection') === $collection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function storeMedia(User $user, ?HasMedia $model, ?string $collection = null): bool
|
|
||||||
{
|
|
||||||
if (is_bool($user->policies['storeMedia'])) {
|
|
||||||
return $user->policies['storeMedia'] === true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_null($model)) {
|
|
||||||
return data_get($user->policies, 'storeMedia.collection') === $collection;
|
|
||||||
}
|
|
||||||
|
|
||||||
return data_get($user->policies, 'storeMedia.id') === $model->id
|
|
||||||
&& data_get($user->policies, 'storeMedia.collection') === $collection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function updateMedia(User $user, HasMedia $model, string $collection): bool
|
|
||||||
{
|
|
||||||
if (is_bool($user->policies['updateMedia'])) {
|
|
||||||
return $user->policies['updateMedia'] === true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return data_get($user->policies, 'updateMedia.id') === $model->id
|
|
||||||
&& data_get($user->policies, 'updateMedia.collection') === $collection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function destroyMedia(User $user, HasMedia $model, string $collection): bool
|
|
||||||
{
|
|
||||||
if (is_bool($user->policies['destroyMedia'])) {
|
|
||||||
return $user->policies['destroyMedia'] === true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return data_get($user->policies, 'destroyMedia.id') === $model->id
|
|
||||||
&& data_get($user->policies, 'destroyMedia.collection') === $collection;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,41 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Database\Factories\Models;
|
|
||||||
|
|
||||||
use Workbench\App\Models\User;
|
|
||||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
||||||
use Illuminate\Support\Facades\Hash;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @extends Factory<User>
|
|
||||||
*/
|
|
||||||
class UserFactory extends Factory
|
|
||||||
{
|
|
||||||
protected $model = User::class;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Define the model's default state.
|
|
||||||
*
|
|
||||||
* @return array<string, mixed>
|
|
||||||
*/
|
|
||||||
public function definition()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'email' => $this->faker->safeEmail,
|
|
||||||
'password' => Hash::make('password'),
|
|
||||||
'name' => $this->faker->firstName,
|
|
||||||
'policies' => [],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function policies(array $policies): self
|
|
||||||
{
|
|
||||||
return $this->state(['policies' => [
|
|
||||||
'storeMedia' => true,
|
|
||||||
'updateMedia' => true,
|
|
||||||
'destroyMedia' => true,
|
|
||||||
'listMedia' => true,
|
|
||||||
...$policies,
|
|
||||||
]]);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,20 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
use Illuminate\Database\Migrations\Migration;
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
|
||||||
use Illuminate\Support\Facades\Schema;
|
|
||||||
|
|
||||||
return new class extends Migration
|
|
||||||
{
|
|
||||||
public function up(): void
|
|
||||||
{
|
|
||||||
Schema::create('users', function (Blueprint $table) {
|
|
||||||
$table->id();
|
|
||||||
$table->string('email');
|
|
||||||
$table->string('name');
|
|
||||||
$table->string('password');
|
|
||||||
$table->json('policies');
|
|
||||||
$table->timestamps();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
Loading…
Reference in New Issue