Files
glastree/tests/Feature/StorageRepositoryTest.php
T

156 lines
5.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Models\StorageRepository;
use App\Models\User;
use App\Services\StorageRepositoryService;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class StorageRepositoryTest extends TestCase
{
protected User $user;
protected function setUp(): void
{
parent::setUp();
Schema::create('users', function ($table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->boolean('is_admin')->default(false);
$table->text('permissions')->nullable();
$table->string('status')->nullable();
$table->unsignedBigInteger('role_preset_id')->nullable();
$table->timestamps();
});
Schema::create('storage_repositories', function ($table) {
$table->id();
$table->string('nome');
$table->string('tipo', 50);
$table->text('config');
$table->boolean('is_active')->default(true);
$table->integer('ordine')->default(0);
$table->timestamps();
});
Schema::create('documenti', function ($table) {
$table->id();
$table->foreignId('repository_id')->nullable()->constrained('storage_repositories')->nullOnDelete();
$table->timestamps();
});
$this->user = User::factory()->create(['is_admin' => true]);
$this->actingAs($this->user);
}
protected function tearDown(): void
{
Schema::dropIfExists('documenti');
Schema::dropIfExists('storage_repositories');
Schema::dropIfExists('users');
parent::tearDown();
}
public function test_can_create_webdav_repository(): void
{
$response = $this->post('/storage-repositories', [
'nome' => 'Test WebDAV',
'tipo' => 'webdav',
'config' => [
'base_uri' => 'https://example.com/webdav/',
'username' => 'testuser',
'password' => 'testpass',
'root' => '/',
],
'is_active' => true,
]);
$response->assertRedirect('/impostazioni#repository');
$response->assertSessionHas('success');
$this->assertDatabaseHas('storage_repositories', [
'nome' => 'Test WebDAV',
'tipo' => 'webdav',
]);
}
public function test_can_create_google_drive_repository(): void
{
$response = $this->post('/storage-repositories', [
'nome' => 'Test Google Drive',
'tipo' => 'google_drive',
'config' => [
'client_id' => 'test-client-id',
'client_secret' => 'test-client-secret',
'refresh_token' => 'test-refresh-token',
'root_folder_id' => 'root',
],
'is_active' => true,
]);
$response->assertRedirect('/impostazioni#repository');
$this->assertDatabaseHas('storage_repositories', [
'nome' => 'Test Google Drive',
'tipo' => 'google_drive',
]);
}
public function test_requires_nome_and_tipo(): void
{
$response = $this->post('/storage-repositories', [
'config' => ['base_uri' => 'https://example.com'],
]);
$response->assertSessionHasErrors(['nome', 'tipo']);
}
public function test_can_delete_repository_with_no_documents(): void
{
$repo = StorageRepository::create([
'nome' => 'To Delete',
'tipo' => 'webdav',
'config' => ['base_uri' => 'https://example.com'],
]);
$response = $this->delete("/storage-repositories/{$repo->id}");
$response->assertStatus(302);
$this->assertDatabaseMissing('storage_repositories', ['id' => $repo->id]);
}
public function test_tipo_labels(): void
{
$this->assertEquals('WebDAV', StorageRepository::etichettaTipo('webdav'));
$this->assertEquals('Google Drive', StorageRepository::etichettaTipo('google_drive'));
}
public function test_tipo_icons(): void
{
$this->assertEquals('fa-server', StorageRepository::iconaTipo('webdav'));
$this->assertEquals('fa-google-drive', StorageRepository::iconaTipo('google_drive'));
}
public function test_can_reorder_repositories(): void
{
$repo1 = StorageRepository::create(['nome' => 'A', 'tipo' => 'webdav', 'config' => [], 'ordine' => 0]);
$repo2 = StorageRepository::create(['nome' => 'B', 'tipo' => 'webdav', 'config' => [], 'ordine' => 1]);
$response = $this->post('/storage-repositories/reorder', [
'order' => [$repo2->id, $repo1->id],
]);
$response->assertJson(['success' => true]);
$this->assertEquals(0, $repo2->fresh()->ordine);
$this->assertEquals(1, $repo1->fresh()->ordine);
}
}