--wip-- [skip ci]

This commit is contained in:
philipp lang 2024-12-20 02:22:36 +01:00
parent e9c5b1d789
commit 9493c66914
3 changed files with 163 additions and 21 deletions

View File

@ -0,0 +1,107 @@
<?php
namespace Modules\Fileshare\Components;
use App\Fileshare\ConnectionTypes\ConnectionType;
use App\Fileshare\Models\Fileshare;
use Modules\Mailgateway\Models\Mailgateway;
use Modules\Mailgateway\Types\Type;
use Illuminate\Support\Collection;
use Illuminate\Validation\ValidationException;
use Livewire\Attributes\On;
use Livewire\Attributes\Validate;
use Livewire\Component;
class Form extends Component
{
public string $name = '';
public array $type = [];
#[Validate('required|string|max:255|exclude')]
public ?string $typeClass = null;
public array $types;
public ?Mailgateway $model = null;
public function rules()
{
return [
'name' => 'required|string|max:255',
'type' => 'array',
];
}
public function validationAttributes(): array
{
return [
'type' => 'Typ',
'name' => 'Bezeichnung',
];
}
public function mount(?string $id = null): void
{
$this->types = ConnectionType::forSelect();
// if ($id) {
// $this->model = Mailgateway::find($id);
// $this->name = $this->model->name;
// $this->type = $this->model->type;
// $this->typeClass = get_class($this->model->type);
// }
}
public function fields(): array
{
return $this->type ? $this->typeClass::fields() : [];
}
public function updatedTypeClass(?string $type): void
{
if (!$type) {
return;
}
$this->type = $type::defaults();
}
#[On('onStoreFromModal')]
public function onSave(): void
{
$type = $this->typeClass::from($this->type);
if (!$type->check()) {
throw ValidationException::withMessages(['typeClass' => 'Verbindung fehlgeschlagen']);
}
Fileshare::create([
...$this->validate(),
'type' => $type,
]);
$this->dispatch('closeModal');
$this->dispatch('refresh-page');
$this->dispatch('success', 'Verbindung erstellt.');
}
public function render()
{
return <<<'HTML'
<div>
<form class="grid grid-cols-2 gap-3">
<x-form::text name="name" wire:model="name" label="Bezeichnung" required />
<x-form::select name="typeClass" wire:model.live="typeClass" label="Typ" :options="$types" required />
@foreach($this->fields() as $index => $field)
<x-form::text
wire:key="$index"
wire:model="type.{{$field['key']}}"
:label="$field['label']"
:type="$field['type']"
:name="'type.'.$field['key']"
required
></x-form::text>
@endforeach
</form>
</div>
HTML;
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace Modules\Fileshare\Components;
use App\Fileshare\ConnectionTypes\OwncloudConnection;
use App\Fileshare\Models\Fileshare;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Livewire\Livewire;
use Tests\FileshareTestCase;
uses(FileshareTestCase::class);
uses(DatabaseTransactions::class);
it('it displays values', function () {
test()->withoutExceptionHandling()->login()->loginNami();
Livewire::test(Form::class)
->assertSet('name', '')
->assertSee('Bezeichnung')
->assertSee('Owncloud')
->assertSee('Nextcloud')
->assertSet('type', []);
});
it('it displays owncloud connection values', function () {
test()->withoutExceptionHandling()->login()->loginNami();
Livewire::test(Form::class)
->set('typeClass', OwncloudConnection::class)
->assertSet('typeClass', OwncloudConnection::class)
->assertSee('Benutzer')
->assertSee('URL')
->assertSet('type.user', '')
->assertSet('type.base_url', '')
->assertSet('type.password', '');
});
it('it saves owncloud connection', function () {
test()->login()->loginNami()->withUser('badenpowell', 'secret');
Livewire::test(Form::class)
->set('name', 'lala')
->set('typeClass', OwncloudConnection::class)
->set('type.user', 'badenpowell')
->set('type.password', 'secret')
->set('type.base_url', env('TEST_OWNCLOUD_DOMAIN'))
->call('onSave')
->assertDispatched('success', 'Verbindung erstellt.')
->assertDispatched('refresh-page');
$connection = Fileshare::firstOrFail();
$this->assertEquals('badenpowell', $connection->type->user);
$this->assertEquals('secret', $connection->type->password);
$this->assertEquals(env('TEST_OWNCLOUD_DOMAIN'), $connection->type->baseUrl);
$this->assertEquals('lala', $connection->name);
});

View File

@ -9,27 +9,6 @@ use Tests\FileshareTestCase;
class FileshareStoreActionTest extends FileshareTestCase
{
public function testItStoresAConnection(): void
{
$this->withoutExceptionHandling()->login()->loginNami()->withUser('badenpowell', 'secret');
$this->post(route('fileshare.store'), [
'name' => 'Lala',
'type' => OwncloudConnection::class,
'config' => [
'user' => 'badenpowell',
'password' => 'secret',
'base_url' => env('TEST_OWNCLOUD_DOMAIN'),
]
])->assertOk();
$connection = Fileshare::firstOrFail();
$this->assertEquals('badenpowell', $connection->type->user);
$this->assertEquals('secret', $connection->type->password);
$this->assertEquals(env('TEST_OWNCLOUD_DOMAIN'), $connection->type->baseUrl);
$this->assertEquals('Lala', $connection->name);
}
public function testItChecksConnection(): void
{
$this->withExceptionHandling()->login()->loginNami();