183 lines
7.9 KiB
PHP
183 lines
7.9 KiB
PHP
<?php
|
|
|
|
use App\Models\User;
|
|
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
|
|
use App\Modules\Jobs\Infrastructure\Files\GdTaskImageFiles;
|
|
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
|
|
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskImageModel;
|
|
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Livewire\Livewire;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
beforeEach(function () {
|
|
Storage::fake('local');
|
|
$this->employee = User::factory()->create(['name' => 'Eetu Esimerkki']);
|
|
$this->actingAs($this->employee);
|
|
$this->job = CustomerJobModel::factory()->create(['status' => 'order', 'responsible_user_id' => $this->employee->id]);
|
|
$workType = WorkTypeModel::query()->where('name', 'graafinen työ')->sole();
|
|
$this->task = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'work_type_id' => $workType->id, 'title' => 'Mainoskuvat']);
|
|
$this->params = ['jobId' => $this->job->id, 'taskId' => $this->task->id, 'taskTitle' => 'Mainoskuvat'];
|
|
});
|
|
|
|
/**
|
|
* Sends an image through the page as the signed-in user.
|
|
*/
|
|
function uploadTaskImage(array $params, UploadedFile $file, string $caption = ''): TaskImageModel
|
|
{
|
|
Livewire::test('jobs::task-images', $params)
|
|
->set('photo', $file)
|
|
->set('caption', $caption)
|
|
->call('add')
|
|
->assertHasNoErrors();
|
|
|
|
return TaskImageModel::query()->latest('id')->firstOrFail();
|
|
}
|
|
|
|
/**
|
|
* A JPEG whose EXIF says it must be turned 90° clockwise (orientation 6),
|
|
* as phone cameras save upright photos.
|
|
*/
|
|
function sidewaysJpeg(int $width, int $height): string
|
|
{
|
|
$image = imagecreatetruecolor($width, $height);
|
|
ob_start();
|
|
imagejpeg($image);
|
|
$jpeg = (string) ob_get_clean();
|
|
|
|
$tiff = 'II*'."\0".pack('V', 8).pack('v', 1).pack('vvVv', 0x0112, 3, 1, 6)."\0\0".pack('V', 0);
|
|
$exif = 'Exif'."\0\0".$tiff;
|
|
$path = tempnam(sys_get_temp_dir(), 'exif').'.jpg';
|
|
file_put_contents($path, substr($jpeg, 0, 2)."\xFF\xE1".pack('n', strlen($exif) + 2).$exif.substr($jpeg, 2));
|
|
|
|
return $path;
|
|
}
|
|
|
|
it('stores a downscaled image and a thumbnail without metadata and shows them on the job page', function () {
|
|
$image = uploadTaskImage($this->params, UploadedFile::fake()->image('valot.jpg', 4000, 3000), 'Valot paikallaan');
|
|
|
|
[$width, $height] = getimagesizefromstring(Storage::disk('local')->get($image->path));
|
|
[$thumbWidth] = getimagesizefromstring(Storage::disk('local')->get($image->thumbnail_path));
|
|
|
|
expect([$width, $height])->toBe([2560, 1920])
|
|
->and([$image->width, $image->height])->toBe([2560, 1920])
|
|
->and($thumbWidth)->toBe(400)
|
|
->and($image->caption)->toBe('Valot paikallaan')
|
|
->and($image->original_name)->toBe('valot.jpg')
|
|
->and($image->user_id)->toBe($this->employee->id);
|
|
|
|
$this->get(route('jobs.show', $this->job->id))
|
|
->assertOk()
|
|
->assertSee('Kuvat:')
|
|
->assertSee('alt="Valot paikallaan"', false)
|
|
->assertSee(route('jobs.tasks.images.show', [$this->job->id, $this->task->id, $image->id, 'thumbnail']));
|
|
});
|
|
|
|
it('turns a sideways phone photo upright and leaves out its EXIF data', function () {
|
|
$stored = app(GdTaskImageFiles::class)->store(sidewaysJpeg(40, 20), $this->task->id);
|
|
$bytes = Storage::disk('local')->get($stored->path);
|
|
|
|
expect(getimagesizefromstring($bytes)[0])->toBe(20)
|
|
->and(getimagesizefromstring($bytes)[1])->toBe(40)
|
|
->and(str_contains($bytes, 'Exif'))->toBeFalse();
|
|
});
|
|
|
|
it('shows Finnish errors for a missing, non-image and too large file', function () {
|
|
Livewire::test('jobs::task-images', $this->params)
|
|
->call('add')
|
|
->assertSee('Valitse kuva.')
|
|
->set('photo', UploadedFile::fake()->create('muistio.pdf', 10, 'application/pdf'))
|
|
->call('add')
|
|
->assertSee('Tiedosto ei ole kuva.')
|
|
->set('photo', UploadedFile::fake()->image('iso.jpg')->size(13000))
|
|
->call('add')
|
|
->assertSee('Kuva voi olla enintään 12 Mt.')
|
|
->set('photo', UploadedFile::fake()->image('kuva.jpg'))
|
|
->set('caption', str_repeat('a', 256))
|
|
->call('add')
|
|
->assertSee('Kuvateksti voi olla enintään 255 merkkiä.');
|
|
|
|
expect(TaskImageModel::query()->count())->toBe(0)
|
|
->and(Storage::disk('local')->allFiles('task-images'))->toBe([]);
|
|
});
|
|
|
|
it('serves the image to those who see the job, and downloads it under its own name', function () {
|
|
$image = uploadTaskImage($this->params, UploadedFile::fake()->image('valot.png', 50, 50));
|
|
|
|
$this->get(route('jobs.tasks.images.show', [$this->job->id, $this->task->id, $image->id, 'full']))
|
|
->assertOk()
|
|
->assertHeader('Content-Type', 'image/jpeg');
|
|
$this->get(route('jobs.tasks.images.show', [$this->job->id, $this->task->id, $image->id, 'full']).'?download=1')
|
|
->assertDownload('valot.jpg');
|
|
|
|
$otherJob = CustomerJobModel::factory()->create();
|
|
$this->get(route('jobs.tasks.images.show', [$otherJob->id, $this->task->id, $image->id, 'full']))->assertNotFound();
|
|
|
|
$this->actingAs(User::factory()->subcontractor()->create());
|
|
$this->get(route('jobs.tasks.images.show', [$this->job->id, $this->task->id, $image->id, 'thumbnail']))->assertForbidden();
|
|
});
|
|
|
|
it('lets the uploader change the caption and remove the image with its files', function () {
|
|
$image = uploadTaskImage($this->params, UploadedFile::fake()->image('valot.jpg'));
|
|
|
|
Livewire::test('jobs::task-images', $this->params)
|
|
->call('startEdit', $image->id)
|
|
->set('editingCaption', 'Lavasteet')
|
|
->call('saveEdit')
|
|
->assertSee('Kuvateksti tallennettu.')
|
|
->assertSet('editingImageId', null)
|
|
->call('confirmRemoval', $image->id)
|
|
->assertSet('removalImageId', $image->id)
|
|
->call('remove')
|
|
->assertSee('Kuva poistettu.')
|
|
->assertDispatched('task-images-changed');
|
|
|
|
expect(TaskImageModel::query()->count())->toBe(0)
|
|
->and(Storage::disk('local')->allFiles('task-images'))->toBe([]);
|
|
});
|
|
|
|
it('lets management remove but not recaption another user\'s image, and others neither', function () {
|
|
$image = uploadTaskImage($this->params, UploadedFile::fake()->image('valot.jpg'));
|
|
|
|
$this->actingAs(User::factory()->create());
|
|
Livewire::test('jobs::task-images', $this->params)
|
|
->assertDontSee('Poista kuva')
|
|
->call('confirmRemoval', $image->id)
|
|
->assertSet('removalImageId', null)
|
|
->assertSee('Voit muokata ja poistaa vain itse lähettämiäsi kuvia.');
|
|
|
|
$this->actingAs(User::factory()->management()->create());
|
|
Livewire::test('jobs::task-images', $this->params)
|
|
->assertDontSee('Muokkaa kuvatekstiä')
|
|
->call('confirmRemoval', $image->id)
|
|
->call('remove')
|
|
->assertSee('Kuva poistettu.');
|
|
|
|
expect(TaskImageModel::query()->count())->toBe(0);
|
|
});
|
|
|
|
it('lets an attached subcontractor send images but forbids a job they are not attached to', function () {
|
|
$subcontractor = User::factory()->subcontractor()->create();
|
|
TaskModel::factory()->assignedTo([$subcontractor->id])->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->task->work_type_id]);
|
|
$this->actingAs($subcontractor);
|
|
|
|
expect(uploadTaskImage($this->params, UploadedFile::fake()->image('valmis.jpg'))->user_id)->toBe($subcontractor->id);
|
|
|
|
$this->actingAs(User::factory()->subcontractor()->create());
|
|
Livewire::test('jobs::task-images', $this->params)->assertForbidden();
|
|
});
|
|
|
|
it('removes the image files with their task', function () {
|
|
uploadTaskImage($this->params, UploadedFile::fake()->image('valot.jpg'));
|
|
|
|
Livewire::test('jobs::show', ['job' => $this->job->id])
|
|
->call('confirmTaskRemoval', $this->task->id)
|
|
->call('removeTask');
|
|
|
|
expect(TaskImageModel::query()->count())->toBe(0)
|
|
->and(Storage::disk('local')->allFiles('task-images'))->toBe([]);
|
|
});
|