130 lines
5.7 KiB
PHP
130 lines
5.7 KiB
PHP
<?php
|
|
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use IriksIT\Manager\Models\Media;
|
|
use IriksIT\Manager\Models\Post;
|
|
use IriksIT\Manager\Support\MediaUsage;
|
|
use IriksIT\Manager\Tests\TestCase;
|
|
|
|
uses(TestCase::class);
|
|
|
|
function png(string $name = 'My Photo.png'): UploadedFile
|
|
{
|
|
return UploadedFile::fake()->createWithContent($name, file_get_contents(__DIR__.'/../Fixtures/pixel.png'));
|
|
}
|
|
|
|
beforeEach(function () {
|
|
Storage::fake('public');
|
|
$this->actingAs(makeUser(['media.delete', 'media.edit', 'media.upload']));
|
|
});
|
|
|
|
describe('uploading', function () {
|
|
it('stores a file on the disk and records it', function () {
|
|
$item = $this->postJson('/manager/media', ['file' => png(), 'focus_x' => 0.3, 'focus_y' => 0.65])->assertCreated()->json('item');
|
|
|
|
$media = Media::sole();
|
|
Storage::disk('public')->assertExists($media->location);
|
|
expect($item['url'])->toBe("/media-files/{$media->id}/my-photo.png/view")
|
|
->and($item)->not->toHaveKeys(['disk', 'location'])
|
|
->and($media->name)->toBe('My Photo')
|
|
->and($media->width)->toBe(1)
|
|
->and($media->checksum)->toBe(hash_file('sha256', __DIR__.'/../Fixtures/pixel.png'))
|
|
->and($media->focus_x)->toBe(0.3)
|
|
->and(Media::focusFor($item['url']))->toBe('30% 65%')
|
|
->and(Media::focusFor('/somewhere/else'))->toBeNull();
|
|
});
|
|
|
|
it('refuses what is not allowed: svg, too big, no room', function () {
|
|
$svg = UploadedFile::fake()->createWithContent('x.svg', '<svg xmlns="http://www.w3.org/2000/svg"/>');
|
|
$this->postJson('/manager/media', ['file' => $svg])->assertJsonValidationErrors('file');
|
|
|
|
config(['manager.media.max_file_mb' => 1]);
|
|
$this->postJson('/manager/media', ['file' => UploadedFile::fake()->create('big.pdf', 2048, 'application/pdf')])->assertJsonValidationErrors('file');
|
|
|
|
config(['manager.media.max_total_gb' => 0.000001]);
|
|
$this->postJson('/manager/media', ['file' => UploadedFile::fake()->create('a.pdf', 5, 'application/pdf')])->assertStatus(422)->assertJsonPath('storage.limit', 1000);
|
|
expect(Media::count())->toBe(0);
|
|
});
|
|
|
|
it('needs the permission', function () {
|
|
$this->actingAs(makeUser(['media.view']));
|
|
|
|
$this->postJson('/manager/media', ['file' => png()])->assertForbidden();
|
|
$this->getJson('/manager/media/items')->assertOk();
|
|
});
|
|
});
|
|
|
|
describe('changing and deleting', function () {
|
|
it('renames and keeps the focus point unless one is sent', function () {
|
|
$id = $this->postJson('/manager/media', ['file' => png(), 'focus_x' => 0.1, 'focus_y' => 0.2])->json('item.id');
|
|
|
|
$this->putJson("/manager/media/{$id}", ['name' => 'New', 'alt' => ' A cat '])->assertOk()->assertJsonPath('item.alt', 'A cat');
|
|
expect(Media::find($id)->focus_y)->toBe(0.2);
|
|
|
|
$this->putJson("/manager/media/{$id}", ['name' => 'New', 'focus_x' => 0.5])->assertJsonValidationErrors('focus_y');
|
|
});
|
|
|
|
it('deletes the file and the row', function () {
|
|
$this->postJson('/manager/media', ['file' => png()]);
|
|
$media = Media::sole();
|
|
|
|
$this->deleteJson("/manager/media/{$media->id}")->assertOk();
|
|
|
|
Storage::disk('public')->assertMissing($media->location);
|
|
expect(Media::count())->toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('usage', function () {
|
|
it('counts where a file is used, through the registered sources of the modules that are on', function () {
|
|
$id = $this->postJson('/manager/media', ['file' => png()])->json('item.id');
|
|
Post::create(['slug' => 'a', 'title' => 'A', 'body' => "See "]);
|
|
|
|
expect(collect($this->getJson('/manager/media/items')->json('items'))->firstWhere('id', $id)['usage_count'])->toBe(1);
|
|
});
|
|
|
|
});
|
|
|
|
describe('the file for visitors', function () {
|
|
beforeEach(function () {
|
|
$this->postJson('/manager/media', ['file' => png()]);
|
|
$this->media = Media::sole();
|
|
auth()->logout();
|
|
$this->app['auth']->forgetGuards();
|
|
});
|
|
|
|
it('is served without logging in, with caching headers', function () {
|
|
$response = $this->get($this->media->url())->assertOk()->assertHeader('Content-Type', 'image/png');
|
|
|
|
expect($response->headers->get('Cache-Control'))->toContain('immutable')
|
|
->and($response->headers->get('X-Content-Type-Options'))->toBe('nosniff')
|
|
->and($response->headers->has('Set-Cookie'))->toBeFalse()
|
|
->and($response->streamedContent())->toBe(file_get_contents(__DIR__.'/../Fixtures/pixel.png'));
|
|
});
|
|
|
|
it('answers 304 for a known ETag and a part for a range', function () {
|
|
$etag = '"'.$this->media->checksum.'"';
|
|
$this->get($this->media->url(), ['If-None-Match' => $etag])->assertStatus(304);
|
|
|
|
$part = $this->get($this->media->url(), ['Range' => 'bytes=0-9'])->assertStatus(206)->assertHeader('Content-Range', "bytes 0-9/{$this->media->size}");
|
|
expect($part->streamedContent())->toBe(substr(file_get_contents(__DIR__.'/../Fixtures/pixel.png'), 0, 10));
|
|
|
|
$this->get($this->media->url(), ['Range' => 'bytes=9999-'])->assertStatus(416);
|
|
});
|
|
|
|
it('offers anything that is not a safe type as a download', function () {
|
|
$this->media->update(['mime_type' => 'text/html']);
|
|
|
|
$this->get($this->media->url())->assertHeader('Content-Type', 'application/octet-stream')
|
|
->assertHeader('Content-Disposition', 'attachment; filename="my-photo.png"');
|
|
});
|
|
|
|
it('is 404 when the row or the file is gone', function () {
|
|
$this->get('/media-files/9999/x.png/view')->assertNotFound();
|
|
|
|
Storage::disk('public')->delete($this->media->location);
|
|
$this->get($this->media->url())->assertNotFound();
|
|
});
|
|
});
|