|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Fieldtypes; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use PHPUnit\Framework\Attributes\Test; |
| 7 | +use Statamic\Facades\User; |
| 8 | +use Statamic\Fields\Field; |
| 9 | +use Statamic\Fieldtypes\UserGroups; |
| 10 | +use Tests\FakesRoles; |
| 11 | +use Tests\FakesUserGroups; |
| 12 | +use Tests\PreventSavingStacheItemsToDisk; |
| 13 | +use Tests\TestCase; |
| 14 | + |
| 15 | +class UserGroupsTest extends TestCase |
| 16 | +{ |
| 17 | + use FakesRoles; |
| 18 | + use FakesUserGroups; |
| 19 | + use PreventSavingStacheItemsToDisk; |
| 20 | + |
| 21 | + #[Test] |
| 22 | + public function it_returns_empty_index_items_without_assign_user_groups_permission() |
| 23 | + { |
| 24 | + $this->actingAs($this->cpUserWithPermissions(['access cp'])); |
| 25 | + |
| 26 | + $items = $this->fieldtype()->getIndexItems(new Request); |
| 27 | + |
| 28 | + $this->assertTrue($items->isEmpty()); |
| 29 | + } |
| 30 | + |
| 31 | + #[Test] |
| 32 | + public function it_returns_groups_in_index_items_with_assign_user_groups_permission() |
| 33 | + { |
| 34 | + $this->setTestUserGroups(['editors' => []]); |
| 35 | + $this->actingAs($this->cpUserWithPermissions(['access cp', 'assign user groups'])); |
| 36 | + |
| 37 | + $items = $this->fieldtype()->getIndexItems(new Request); |
| 38 | + |
| 39 | + $this->assertContains('editors', $items->pluck('id')); |
| 40 | + } |
| 41 | + |
| 42 | + private function fieldtype() |
| 43 | + { |
| 44 | + return (new UserGroups)->setField(new Field('test', ['type' => 'user_groups'])); |
| 45 | + } |
| 46 | + |
| 47 | + private function cpUserWithPermissions(array $permissions) |
| 48 | + { |
| 49 | + $this->setTestRoles(['test' => $permissions]); |
| 50 | + |
| 51 | + return tap(User::make()->id(uniqid())->assignRole('test'))->save(); |
| 52 | + } |
| 53 | +} |
0 commit comments