|
4 | 4 |
|
5 | 5 | namespace Orchid\Tests\Feature\Platform; |
6 | 6 |
|
| 7 | +use Illuminate\Support\Facades\Gate; |
7 | 8 | use Illuminate\Http\UploadedFile; |
8 | 9 | use Orchid\Attachment\Models\Attachment; |
| 10 | +use Orchid\Platform\Models\User; |
| 11 | +use Orchid\Tests\App\Policies\PolicySortDeny; |
9 | 12 | use Orchid\Tests\TestFeatureCase; |
10 | 13 |
|
11 | 14 | class SortableTest extends TestFeatureCase |
12 | 15 | { |
| 16 | + public function setUp():void |
| 17 | + { |
| 18 | + parent::setUp(); |
| 19 | + Gate::policy(Attachment::class, null); |
| 20 | + } |
| 21 | + |
| 22 | + public function tearDown():void |
| 23 | + { |
| 24 | + Gate::policy(Attachment::class, null); |
| 25 | + parent::tearDown(); |
| 26 | + } |
| 27 | + |
| 28 | + public function testSortingIsForbiddenWhenPolicyDenies(): void |
| 29 | + { |
| 30 | + Gate::policy(Attachment::class, PolicySortDeny::class); |
| 31 | + |
| 32 | + $response = $this |
| 33 | + ->actingAs($this->createAdminUser()) |
| 34 | + ->post(route('orchid.files.upload'), [ |
| 35 | + 'files' => [ |
| 36 | + UploadedFile::fake()->image('first.jpg'), |
| 37 | + UploadedFile::fake()->image('second.png'), |
| 38 | + ], |
| 39 | + ]); |
| 40 | + |
| 41 | + $attachments = $response->decodeResponseJson()->json(); |
| 42 | + |
| 43 | + $ids = array_column($attachments, 'id'); |
| 44 | + $sortBefore = Attachment::whereIn('id', $ids)->pluck('sort', 'id')->toArray(); |
| 45 | + |
| 46 | + $sortItems = collect($ids)->map(fn ($id, $index) => [ |
| 47 | + 'id' => $id, |
| 48 | + 'sortOrder' => $index, |
| 49 | + ])->values()->all(); |
| 50 | + |
| 51 | + $response = $this |
| 52 | + ->actingAs($this->createAdminUser()) |
| 53 | + ->post(route('orchid.sorting'), [ |
| 54 | + 'items' => $sortItems, |
| 55 | + 'model' => Attachment::class, |
| 56 | + ]); |
| 57 | + |
| 58 | + $response->assertForbidden(); |
| 59 | + |
| 60 | + $sortAfter = Attachment::whereIn('id', $ids)->pluck('sort', 'id')->toArray(); |
| 61 | + $this->assertSame($sortBefore, $sortAfter, 'Sort order must not change when policy denies.'); |
| 62 | + } |
| 63 | + |
| 64 | + |
13 | 65 | public function testAttachmentHttpSort(): void |
14 | 66 | { |
15 | 67 | $response = $this |
@@ -43,7 +95,7 @@ public function testAttachmentHttpSort(): void |
43 | 95 | 'model' => Attachment::class, |
44 | 96 | ]); |
45 | 97 |
|
46 | | - $response->isOk(); |
| 98 | + $response->assertOk(); |
47 | 99 |
|
48 | 100 | $attachments = Attachment::whereIn('id', $originalFiles) |
49 | 101 | ->pluck('sort', 'id') |
|
0 commit comments