Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class="text-red-600 dark:text-red-400"
@if ($this->uploadTypes === [])
<x-ui.text muted class="mt-4">{{ __('No document types yet. Add them on System, then Add-ons.') }}</x-ui.text>
@else
<form wire:submit="upload" class="mt-4 grid gap-3 rounded-lg border border-zinc-200 p-4 sm:grid-cols-2 lg:grid-cols-3 dark:border-zinc-700">
<form wire:submit="saveDocument" class="mt-4 grid gap-3 rounded-lg border border-zinc-200 p-4 sm:grid-cols-2 lg:grid-cols-3 dark:border-zinc-700">
<x-ui.select
wire:model.live="uploadType"
:label="__('Document type')"
Expand Down
2 changes: 1 addition & 1 deletion resources/views/livewire/customer-card/customer-card.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function showNumber(int $id): void
/**
* Stores the file as the customer's document of the chosen type, replacing (hiding) one already there.
*/
public function upload(): void
public function saveDocument(): void
{
$this->authorize(Abilities::UPLOAD);
abort_if($this->account->trashed(), 403);
Expand Down
23 changes: 23 additions & 0 deletions tests/Feature/ActionNamesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
| Livewire's browser side keeps some $wire names for itself (`upload` is its own file upload helper), so
| wire:submit="upload" never reached the component's upload() method. The names come from the installed Livewire.
*/
it('names no button or form action after one of Livewire\'s own $wire helpers', function () {
$livewire = (string) file_get_contents(base_path('vendor/livewire/livewire/dist/livewire.esm.js'));
preg_match('/var aliases = \{(.*?)\};/s', $livewire, $block);
preg_match_all('/"(\w+)":/', $block[1] ?? '', $reserved);

$root = dirname(__DIR__, 2);
$actions = [];
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("{$root}/resources/views", FilesystemIterator::SKIP_DOTS));

foreach ($files as $file) {
preg_match_all('/wire:(?:click|submit)[\w.-]*="\s*([A-Za-z_]\w*)/', (string) file_get_contents($file->getPathname()), $matches);
$actions = [...$actions, ...$matches[1]];
}

expect($reserved[1])->toContain('upload')
->and(array_values(array_intersect(array_unique($actions), $reserved[1])))->toBe([]);
});
16 changes: 8 additions & 8 deletions tests/Feature/CustomerCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ function card(Account $account): Testable

card($this->account)
->set('uploadType', (string) $photo->id)
->set('uploadFile', UploadedFile::fake()->createWithContent('a.png', '<html></html>'))->call('upload')
->set('uploadFile', UploadedFile::fake()->createWithContent('a.png', '<html></html>'))->call('saveDocument')
->assertHasErrors(['uploadFile'])
->set('uploadFile', pngUpload('me.png'))->call('upload')->assertHasNoErrors()
->set('uploadFile', pngUpload('me.png'))->call('saveDocument')->assertHasNoErrors()
->assertSet('uploadType', '')->assertSet('uploadFile', null);

expect(Document::query()->where('document_type_id', $photo->id)->sole()->original_name)->toBe('me.png');
Expand All @@ -86,10 +86,10 @@ function card(Account $account): Testable
it('needs the type and the file in the upload box', function () {
$this->actingAs($this->admin);

card($this->account)->call('upload')
card($this->account)->call('saveDocument')
->assertHasErrors(['uploadType' => __('Choose the document type.'), 'uploadFile' => __('Choose the file.')]);
card($this->account)->set('uploadType', (string) documentType('Old form', ['active' => false])->id)->set('uploadFile', pngUpload())
->call('upload')->assertHasErrors(['uploadType']);
->call('saveDocument')->assertHasErrors(['uploadType']);
});

it('edits the type and number, refusing a taken type', function () {
Expand Down Expand Up @@ -136,7 +136,7 @@ function card(Account $account): Testable
$this->account->delete();

card($this->account)->assertOk()->assertSee('xxxx xxxx 4821')->assertDontSee(__('Choose file'))
->set('uploadType', (string) $photo->id)->set('uploadFile', pngUpload())->call('upload')->assertForbidden();
->set('uploadType', (string) $photo->id)->set('uploadFile', pngUpload())->call('saveDocument')->assertForbidden();
});

it('downloads all current documents as one zip and logs it', function () {
Expand Down Expand Up @@ -165,7 +165,7 @@ function card(Account $account): Testable
card($this->account)->assertSee(__(':type (replaces the current file)', ['type' => 'Aadhaar card']))
->set('uploadType', (string) $this->aadhaar->id)
->assertSeeHtml('wire:confirm="'.e(__('Replace the :type of :customer? The current file can be restored from Deleted documents.', ['type' => 'Aadhaar card', 'customer' => 'Ramesh Kumar'])).'"')
->set('uploadFile', pngUpload('sharper.png'))->call('upload')->assertHasNoErrors();
->set('uploadFile', pngUpload('sharper.png'))->call('saveDocument')->assertHasNoErrors();

expect($this->document->fresh()->trashed())->toBeTrue()
->and(Document::query()->sole()->original_name)->toBe('sharper.png')
Expand Down Expand Up @@ -204,7 +204,7 @@ function card(Account $account): Testable
$this->actingAs($this->admin);
$photo = documentType('Customer photo');

card($this->account)->set('uploadType', (string) $photo->id)->set('uploadFile', 'documents/1/secret')->call('upload')->assertHasErrors(['uploadFile']);
card($this->account)->set('uploadType', (string) $photo->id)->set('uploadFile', 'documents/1/secret')->call('saveDocument')->assertHasErrors(['uploadFile']);
});

it('refuses Upload once the customer is deleted while the card is open', function () {
Expand All @@ -214,7 +214,7 @@ function card(Account $account): Testable

$this->account->delete();

$card->set('uploadType', (string) $photo->id)->set('uploadFile', pngUpload())->call('upload')->assertForbidden();
$card->set('uploadType', (string) $photo->id)->set('uploadFile', pngUpload())->call('saveDocument')->assertForbidden();
expect(Document::query()->where('document_type_id', $photo->id)->count())->toBe(0);
});

Expand Down