|
1 | 1 | <?php |
2 | 2 |
|
3 | | -namespace Tests\Feature\Filament\Resources; |
4 | | - |
5 | 3 | use Eclipse\Cms\Admin\Filament\Resources\SectionResource; |
6 | 4 | use Eclipse\Cms\Models\Section; |
7 | 5 | use Filament\Actions\DeleteAction; |
8 | 6 | use Livewire\Livewire; |
9 | | -use Tests\TestCase; |
10 | | - |
11 | | -class SectionResourceTest extends TestCase |
12 | | -{ |
13 | | - public function test_authorized_access_can_view_sections_list(): void |
14 | | - { |
15 | | - $this->migrate() |
16 | | - ->set_up_super_admin_and_tenant(); |
17 | | - |
18 | | - $response = $this->get(SectionResource::getUrl('index')); |
19 | | - |
20 | | - $response->assertSuccessful(); |
21 | | - } |
22 | | - |
23 | | - public function test_create_section_screen_can_be_rendered(): void |
24 | | - { |
25 | | - $this->migrate() |
26 | | - ->set_up_super_admin_and_tenant(); |
27 | | - |
28 | | - $response = $this->get(SectionResource::getUrl('create')); |
29 | | - |
30 | | - $response->assertSuccessful(); |
31 | | - } |
32 | | - |
33 | | - public function test_section_form_validation_works(): void |
34 | | - { |
35 | | - $this->migrate() |
36 | | - ->set_up_super_admin_and_tenant(); |
37 | | - |
38 | | - Livewire::test(SectionResource\Pages\CreateSection::class) |
39 | | - ->fillForm([ |
40 | | - 'name' => '', |
41 | | - 'type' => 'pages', |
42 | | - ]) |
43 | | - ->call('create') |
44 | | - ->assertHasFormErrors(['name']); |
45 | | - } |
46 | | - |
47 | | - public function test_section_can_be_created_through_form(): void |
48 | | - { |
49 | | - $this->migrate() |
50 | | - ->set_up_super_admin_and_tenant(); |
51 | | - |
52 | | - $newData = [ |
53 | | - 'name.en' => 'Test Section', |
54 | | - 'name.sl' => 'Test Sekcija', |
55 | | - 'type' => 'pages', |
56 | | - ]; |
57 | 7 |
|
58 | | - Livewire::test(SectionResource\Pages\CreateSection::class) |
59 | | - ->fillForm($newData) |
60 | | - ->call('create') |
61 | | - ->assertHasNoFormErrors(); |
| 8 | +beforeEach(function () { |
| 9 | + $this->setUpSuperAdmin(); |
| 10 | +}); |
62 | 11 |
|
63 | | - $this->assertDatabaseHas('cms_sections', [ |
64 | | - 'type' => 'pages', |
65 | | - ]); |
66 | | - } |
| 12 | +test('authorized access can view sections list', function () { |
| 13 | + $response = $this->get(SectionResource::getUrl('index')); |
67 | 14 |
|
68 | | - public function test_section_can_be_updated(): void |
69 | | - { |
70 | | - $this->migrate() |
71 | | - ->set_up_super_admin_and_tenant(); |
| 15 | + $response->assertSuccessful(); |
| 16 | +}); |
72 | 17 |
|
73 | | - $section = Section::factory()->create(); |
| 18 | +test('create section screen can be rendered', function () { |
| 19 | + $response = $this->get(SectionResource::getUrl('create')); |
74 | 20 |
|
75 | | - $newData = [ |
76 | | - 'name.en' => 'Updated Section', |
77 | | - 'name.sl' => 'Posodobljena Sekcija', |
78 | | - 'type' => 'pages', |
79 | | - ]; |
| 21 | + $response->assertSuccessful(); |
| 22 | +}); |
80 | 23 |
|
81 | | - Livewire::test(SectionResource\Pages\EditSection::class, [ |
82 | | - 'record' => $section->getRouteKey(), |
| 24 | +test('section form validation works', function () { |
| 25 | + Livewire::test(SectionResource\Pages\CreateSection::class) |
| 26 | + ->fillForm([ |
| 27 | + 'name' => '', |
| 28 | + 'type' => 'pages', |
83 | 29 | ]) |
84 | | - ->fillForm($newData) |
85 | | - ->call('save') |
86 | | - ->assertHasNoFormErrors(); |
| 30 | + ->call('create') |
| 31 | + ->assertHasFormErrors(['name']); |
| 32 | +}); |
87 | 33 |
|
88 | | - $this->assertTrue(true); |
89 | | - } |
| 34 | +test('section can be created through form', function () { |
| 35 | + $newData = [ |
| 36 | + 'name.en' => 'Test Section', |
| 37 | + 'name.sl' => 'Test Sekcija', |
| 38 | + 'type' => 'pages', |
| 39 | + ]; |
90 | 40 |
|
91 | | - public function test_section_can_be_deleted(): void |
92 | | - { |
93 | | - $this->migrate() |
94 | | - ->set_up_super_admin_and_tenant(); |
| 41 | + Livewire::test(SectionResource\Pages\CreateSection::class) |
| 42 | + ->fillForm($newData) |
| 43 | + ->call('create') |
| 44 | + ->assertHasNoFormErrors(); |
95 | 45 |
|
96 | | - $section = Section::factory()->create(); |
| 46 | + expect(Section::where('type', 'pages')->exists())->toBeTrue(); |
| 47 | +}); |
97 | 48 |
|
98 | | - Livewire::test(SectionResource\Pages\EditSection::class, [ |
99 | | - 'record' => $section->getRouteKey(), |
100 | | - ]) |
101 | | - ->callAction(DeleteAction::class); |
| 49 | +test('section can be updated', function () { |
| 50 | + $section = Section::factory()->create(); |
102 | 51 |
|
103 | | - $this->assertSoftDeleted($section); |
104 | | - } |
| 52 | + $newData = [ |
| 53 | + 'name.en' => 'Updated Section', |
| 54 | + 'name.sl' => 'Posodobljena Sekcija', |
| 55 | + 'type' => 'pages', |
| 56 | + ]; |
105 | 57 |
|
106 | | - public function test_unauthorized_access_can_be_prevented(): void |
107 | | - { |
108 | | - $this->migrate() |
109 | | - ->set_up_user_without_permissions(); |
| 58 | + Livewire::test(SectionResource\Pages\EditSection::class, [ |
| 59 | + 'record' => $section->getRouteKey(), |
| 60 | + ]) |
| 61 | + ->fillForm($newData) |
| 62 | + ->call('save') |
| 63 | + ->assertHasNoFormErrors(); |
110 | 64 |
|
111 | | - $response = $this->get(SectionResource::getUrl('index')); |
| 65 | + expect(true)->toBeTrue(); |
| 66 | +}); |
112 | 67 |
|
113 | | - $response->assertForbidden(); |
114 | | - } |
| 68 | +test('section can be deleted', function () { |
| 69 | + $section = Section::factory()->create(); |
115 | 70 |
|
116 | | - public function test_user_with_create_permission_can_create_sections(): void |
117 | | - { |
118 | | - $this->migrate() |
119 | | - ->set_up_user_with_permissions(['view_any_section', 'create_section']); |
| 71 | + Livewire::test(SectionResource\Pages\EditSection::class, [ |
| 72 | + 'record' => $section->getRouteKey(), |
| 73 | + ]) |
| 74 | + ->callAction(DeleteAction::class); |
120 | 75 |
|
121 | | - $response = $this->get(SectionResource::getUrl('create')); |
| 76 | + expect($section->fresh()->trashed())->toBeTrue(); |
| 77 | +}); |
122 | 78 |
|
123 | | - $response->assertSuccessful(); |
124 | | - } |
| 79 | +test('unauthorized access can be prevented', function () { |
| 80 | + $this->setUpUserWithoutPermissions(); |
125 | 81 |
|
126 | | - public function test_user_with_update_permission_can_edit_sections(): void |
127 | | - { |
128 | | - $this->migrate() |
129 | | - ->set_up_user_with_permissions(['view_any_section', 'view_section', 'update_section']); |
| 82 | + $response = $this->get(SectionResource::getUrl('index')); |
130 | 83 |
|
131 | | - $section = Section::factory()->create(); |
| 84 | + $response->assertForbidden(); |
| 85 | +}); |
132 | 86 |
|
133 | | - $response = $this->get(SectionResource::getUrl('edit', [ |
134 | | - 'record' => $section, |
135 | | - ])); |
| 87 | +test('user with create permission can create sections', function () { |
| 88 | + $this->setUpUserWithPermissions(['view_any_section', 'create_section']); |
136 | 89 |
|
137 | | - $response->assertSuccessful(); |
138 | | - } |
| 90 | + $response = $this->get(SectionResource::getUrl('create')); |
139 | 91 |
|
140 | | - public function test_user_with_delete_permission_can_delete_sections(): void |
141 | | - { |
142 | | - $this->migrate() |
143 | | - ->set_up_user_with_permissions(['view_any_section', 'view_section', 'update_section', 'delete_section']); |
| 92 | + $response->assertSuccessful(); |
| 93 | +}); |
144 | 94 |
|
145 | | - $section = Section::factory()->create(); |
| 95 | +test('user with update permission can edit sections', function () { |
| 96 | + $this->setUpUserWithPermissions(['view_any_section', 'view_section', 'update_section']); |
146 | 97 |
|
147 | | - Livewire::test(SectionResource\Pages\EditSection::class, [ |
148 | | - 'record' => $section->getRouteKey(), |
149 | | - ]) |
150 | | - ->callAction('delete'); |
| 98 | + $section = Section::factory()->create(); |
| 99 | + |
| 100 | + $response = $this->get(SectionResource::getUrl('edit', [ |
| 101 | + 'record' => $section, |
| 102 | + ])); |
| 103 | + |
| 104 | + $response->assertSuccessful(); |
| 105 | +}); |
| 106 | + |
| 107 | +test('user with delete permission can delete sections', function () { |
| 108 | + $this->setUpUserWithPermissions(['view_any_section', 'view_section', 'update_section', 'delete_section']); |
| 109 | + |
| 110 | + $section = Section::factory()->create(); |
| 111 | + |
| 112 | + Livewire::test(SectionResource\Pages\EditSection::class, [ |
| 113 | + 'record' => $section->getRouteKey(), |
| 114 | + ]) |
| 115 | + ->callAction('delete'); |
151 | 116 |
|
152 | | - $this->assertSoftDeleted($section); |
153 | | - } |
154 | | -} |
| 117 | + expect($section->fresh()->trashed())->toBeTrue(); |
| 118 | +}); |
0 commit comments