|
| 1 | +<?php |
| 2 | + |
| 3 | +use Eclipse\World\Filament\Clusters\World\Resources\CountryResource; |
| 4 | +use Eclipse\World\Filament\Clusters\World\Resources\CountryResource\Pages\ListCountries; |
| 5 | +use Eclipse\World\Models\Country; |
| 6 | + |
| 7 | +use function Pest\Livewire\livewire; |
| 8 | + |
| 9 | +beforeEach(function () { |
| 10 | + $this->setUpSuperAdmin(); |
| 11 | +}); |
| 12 | + |
| 13 | +test('unauthorized access can be prevented', function () { |
| 14 | + // Create regular user with no permissions |
| 15 | + $this->setUpCommonUser(); |
| 16 | + |
| 17 | + // Create test country |
| 18 | + $country = Country::factory()->create(); |
| 19 | + |
| 20 | + // View table |
| 21 | + $this->get(CountryResource::getUrl()) |
| 22 | + ->assertForbidden(); |
| 23 | + |
| 24 | + // Add direct permission to view the table, since otherwise any other action below is not available even for testing |
| 25 | + $this->user->givePermissionTo('view_any_country'); |
| 26 | + |
| 27 | + // Create country |
| 28 | + livewire(ListCountries::class) |
| 29 | + ->assertActionDisabled('create'); |
| 30 | + |
| 31 | + // Edit country |
| 32 | + livewire(ListCountries::class) |
| 33 | + ->assertCanSeeTableRecords([$country]) |
| 34 | + ->assertTableActionDisabled('edit', $country); |
| 35 | + |
| 36 | + // Delete country |
| 37 | + livewire(ListCountries::class) |
| 38 | + ->assertTableActionDisabled('delete', $country) |
| 39 | + ->assertTableBulkActionDisabled('delete'); |
| 40 | + |
| 41 | + // Restore and force delete |
| 42 | + $country->delete(); |
| 43 | + $this->assertSoftDeleted($country); |
| 44 | + |
| 45 | + livewire(ListCountries::class) |
| 46 | + ->assertTableActionDisabled('restore', $country) |
| 47 | + ->assertTableBulkActionDisabled('restore') |
| 48 | + ->assertTableActionDisabled('forceDelete', $country) |
| 49 | + ->assertTableBulkActionDisabled('forceDelete'); |
| 50 | +}); |
| 51 | + |
| 52 | +test('countries table can be displayed', function () { |
| 53 | + $this->get(CountryResource::getUrl()) |
| 54 | + ->assertSuccessful(); |
| 55 | +}); |
| 56 | + |
| 57 | +test('form validation works', function () { |
| 58 | + $component = livewire(ListCountries::class); |
| 59 | + |
| 60 | + // Test required fields |
| 61 | + $component->callAction('create') |
| 62 | + ->assertHasActionErrors([ |
| 63 | + 'id' => 'required', |
| 64 | + 'a3_id' => 'required', |
| 65 | + 'name' => 'required', |
| 66 | + ]); |
| 67 | + |
| 68 | + // Test with valid data |
| 69 | + $component->callAction('create', Country::factory()->definition()) |
| 70 | + ->assertHasNoActionErrors(); |
| 71 | +}); |
| 72 | + |
| 73 | +test('new country can be created', function () { |
| 74 | + $data = Country::factory()->definition(); |
| 75 | + |
| 76 | + livewire(ListCountries::class) |
| 77 | + ->callAction('create', $data) |
| 78 | + ->assertHasNoActionErrors(); |
| 79 | + |
| 80 | + $country = Country::where('id', $data['id'])->first(); |
| 81 | + expect($country)->toBeObject(); |
| 82 | + |
| 83 | + foreach ($data as $key => $val) { |
| 84 | + expect($country->$key)->toEqual($val); |
| 85 | + } |
| 86 | +}); |
| 87 | + |
| 88 | +test('existing country can be updated', function () { |
| 89 | + $country = Country::factory()->create([ |
| 90 | + 'id' => 'SI', |
| 91 | + 'a3_id' => 'SVN', |
| 92 | + 'num_code' => 705, |
| 93 | + 'name' => 'Slovenia', |
| 94 | + 'flag' => '🇸🇮', |
| 95 | + ]); |
| 96 | + |
| 97 | + $data = \Illuminate\Support\Arr::except(Country::factory()->definition(), ['id']); |
| 98 | + |
| 99 | + livewire(ListCountries::class) |
| 100 | + ->callTableAction('edit', $country, $data) |
| 101 | + ->assertHasNoTableActionErrors(); |
| 102 | + |
| 103 | + $country->refresh(); |
| 104 | + |
| 105 | + foreach ($data as $key => $val) { |
| 106 | + expect($country->$key)->toEqual($val); |
| 107 | + } |
| 108 | +}); |
| 109 | + |
| 110 | +test('country can be deleted', function () { |
| 111 | + $country = Country::factory()->create(); |
| 112 | + |
| 113 | + livewire(ListCountries::class) |
| 114 | + ->callTableAction('delete', $country) |
| 115 | + ->assertHasNoTableActionErrors(); |
| 116 | + |
| 117 | + $this->assertSoftDeleted($country); |
| 118 | +}); |
| 119 | + |
| 120 | +test('country can be restored', function () { |
| 121 | + $country = Country::factory()->create(); |
| 122 | + $country->delete(); |
| 123 | + |
| 124 | + $this->assertSoftDeleted($country); |
| 125 | + |
| 126 | + livewire(ListCountries::class) |
| 127 | + ->filterTable('trashed') |
| 128 | + ->assertTableActionExists('restore') |
| 129 | + ->assertTableActionEnabled('restore', $country) |
| 130 | + ->assertTableActionVisible('restore', $country) |
| 131 | + ->callTableAction('restore', $country) |
| 132 | + ->assertHasNoTableActionErrors(); |
| 133 | + |
| 134 | + $this->assertNotSoftDeleted($country); |
| 135 | +}); |
| 136 | + |
| 137 | +test('country can be force deleted', function () { |
| 138 | + $country = Country::factory()->create(); |
| 139 | + |
| 140 | + $country->delete(); |
| 141 | + $this->assertSoftDeleted($country); |
| 142 | + |
| 143 | + livewire(ListCountries::class) |
| 144 | + ->filterTable('trashed') |
| 145 | + ->assertTableActionExists('forceDelete') |
| 146 | + ->assertTableActionEnabled('forceDelete', $country) |
| 147 | + ->assertTableActionVisible('forceDelete', $country) |
| 148 | + ->callTableAction('forceDelete', $country) |
| 149 | + ->assertHasNoTableActionErrors(); |
| 150 | + |
| 151 | + $this->assertModelMissing($country); |
| 152 | +}); |
0 commit comments