170 lines
5.3 KiB
PHP
170 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Hardware;
|
|
use App\Models\Order;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class HardwareTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private User $admin;
|
|
private User $chef;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->admin = User::create([
|
|
'name' => 'Sylvain',
|
|
'email' => 'sylvain@agglo.local',
|
|
'password' => bcrypt('password'),
|
|
'role' => 'admin_reseau',
|
|
]);
|
|
|
|
$this->chef = User::create([
|
|
'name' => 'Jérémy',
|
|
'email' => 'jeremy@agglo.local',
|
|
'password' => bcrypt('password'),
|
|
'role' => 'chef_service',
|
|
]);
|
|
}
|
|
|
|
public function test_guest_cannot_access_hardware_index()
|
|
{
|
|
$response = $this->get(route('materiels.index'));
|
|
$response->assertRedirect('/login');
|
|
}
|
|
|
|
public function test_authenticated_user_can_access_hardware_index()
|
|
{
|
|
$response = $this->actingAs($this->admin)->get(route('materiels.index'));
|
|
$response->assertOk();
|
|
}
|
|
|
|
public function test_can_create_hardware_with_valid_data()
|
|
{
|
|
$order = Order::create([
|
|
'number' => 'CMD-2026-0001',
|
|
'label' => 'Achat Commutateurs',
|
|
'type' => 'Matériel réseau / Serveur',
|
|
'supplier' => 'Cisco',
|
|
'quote_number' => 'DEV-999',
|
|
'amount_ht' => 5000.00,
|
|
'amount_ttc' => 6000.00,
|
|
'requested_by' => 'Sylvain',
|
|
'delivery_deadline' => now()->addDays(30),
|
|
'status' => 'ordered',
|
|
]);
|
|
|
|
$payload = [
|
|
'name' => 'Switch Coeur Informatique',
|
|
'type' => 'switch',
|
|
'brand' => 'Cisco',
|
|
'model' => 'Catalyst 9300',
|
|
'serial_number' => 'CSCO987654321',
|
|
'status' => 'en_stock',
|
|
'purchase_date' => '2026-06-01',
|
|
'commissioning_date' => '2026-06-10',
|
|
'warranty_expiration_date' => '2029-06-01',
|
|
'location' => 'Salle Serveur 1, Baie 3',
|
|
'ip_address' => '10.0.0.1',
|
|
'order_id' => $order->id,
|
|
'notes' => 'Acheté pour le renouvellement du coeur de réseau.',
|
|
];
|
|
|
|
$response = $this->actingAs($this->admin)->post(route('materiels.store'), $payload);
|
|
|
|
$this->assertDatabaseHas('hardwares', [
|
|
'serial_number' => 'CSCO987654321',
|
|
'name' => 'Switch Coeur Informatique',
|
|
'order_id' => $order->id,
|
|
]);
|
|
|
|
$hardware = Hardware::where('serial_number', 'CSCO987654321')->first();
|
|
$response->assertRedirect(route('materiels.show', $hardware->id));
|
|
}
|
|
|
|
public function test_cannot_create_hardware_with_duplicate_serial_number()
|
|
{
|
|
Hardware::create([
|
|
'name' => 'Serveur Test',
|
|
'type' => 'serveur',
|
|
'brand' => 'Dell',
|
|
'model' => 'R740',
|
|
'serial_number' => 'SN-DUPLICATE-123',
|
|
'status' => 'en_service',
|
|
'location' => 'Salle Serveur 1',
|
|
]);
|
|
|
|
$payload = [
|
|
'name' => 'Autre Serveur',
|
|
'type' => 'serveur',
|
|
'brand' => 'Dell',
|
|
'model' => 'R750',
|
|
'serial_number' => 'SN-DUPLICATE-123', // Doublon
|
|
'status' => 'en_stock',
|
|
'location' => 'Salle Serveur 2',
|
|
];
|
|
|
|
$response = $this->actingAs($this->admin)->post(route('materiels.store'), $payload);
|
|
$response->assertSessionHasErrors('serial_number');
|
|
}
|
|
|
|
public function test_can_update_hardware_successfully()
|
|
{
|
|
$hardware = Hardware::create([
|
|
'name' => 'Onduleur Salle A',
|
|
'type' => 'onduleur',
|
|
'brand' => 'APC',
|
|
'model' => 'Smart-UPS',
|
|
'serial_number' => 'APC-9999',
|
|
'status' => 'en_service',
|
|
'location' => 'Salle A',
|
|
]);
|
|
|
|
$payload = [
|
|
'name' => 'Onduleur Salle A Modifié',
|
|
'type' => 'onduleur',
|
|
'brand' => 'APC',
|
|
'model' => 'Smart-UPS XL',
|
|
'serial_number' => 'APC-9999', // Même numéro de série, ne doit pas bloquer
|
|
'status' => 'en_panne',
|
|
'location' => 'Salle A, Baie 1',
|
|
];
|
|
|
|
$response = $this->actingAs($this->admin)->put(route('materiels.update', $hardware->id), $payload);
|
|
|
|
$response->assertRedirect(route('materiels.show', $hardware->id));
|
|
$this->assertDatabaseHas('hardwares', [
|
|
'id' => $hardware->id,
|
|
'name' => 'Onduleur Salle A Modifié',
|
|
'status' => 'en_panne',
|
|
]);
|
|
}
|
|
|
|
public function test_can_delete_hardware()
|
|
{
|
|
$hardware = Hardware::create([
|
|
'name' => 'Poste Sylvain',
|
|
'type' => 'poste_travail',
|
|
'brand' => 'HP',
|
|
'model' => 'EliteBook',
|
|
'serial_number' => 'HP-5555',
|
|
'status' => 'en_service',
|
|
'location' => 'Bureau Sylvain',
|
|
]);
|
|
|
|
$response = $this->actingAs($this->chef)->delete(route('materiels.destroy', $hardware->id));
|
|
|
|
$response->assertRedirect(route('materiels.index'));
|
|
$this->assertDatabaseMissing('hardwares', [
|
|
'id' => $hardware->id,
|
|
]);
|
|
}
|
|
}
|