Add feature tests for Doctor-related functionalities
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Http\Admin\Role;
|
||||
|
||||
use App\Models\Permission;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Spatie\Permission\Models\Role;
|
||||
use Tests\Concerns\InteractsWithAdmin;
|
||||
use Tests\TestCase;
|
||||
|
||||
class RoleControllerTest extends TestCase
|
||||
{
|
||||
use InteractsWithAdmin, RefreshDatabase;
|
||||
|
||||
public function test_guest_is_redirected(): void
|
||||
{
|
||||
$this->get(route('admin.roles.index'))->assertRedirect(route('login'));
|
||||
}
|
||||
|
||||
public function test_admin_without_permission_is_forbidden(): void
|
||||
{
|
||||
$admin = $this->adminWithoutPermissions();
|
||||
|
||||
$this->actingAs($admin)->get(route('admin.roles.index'))->assertForbidden();
|
||||
$this->actingAs($admin)->post(route('admin.roles.store'), ['name' => 'x', 'label' => 'y'])->assertForbidden();
|
||||
$this->actingAs($admin)->post('/admin/roles-bulk-delete', ['ids' => []])->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_index_renders_with_search(): void
|
||||
{
|
||||
$admin = $this->adminWithAllPermissions();
|
||||
Role::create(['name' => 'editor', 'label' => 'ویرایشگر']);
|
||||
|
||||
$this->actingAs($admin)->get(route('admin.roles.index'))->assertOk()->assertInertia(fn (Assert $page) => $page->component('Roles/index')->has('roles.data', 2));
|
||||
$this->actingAs($admin)->get(route('admin.roles.index', ['search' => 'edit']))->assertInertia(fn (Assert $page) => $page->has('roles.data', 1));
|
||||
}
|
||||
|
||||
public function test_create_and_edit_pages_render_with_grouped_permissions(): void
|
||||
{
|
||||
$admin = $this->adminWithAllPermissions();
|
||||
$role = Role::where('name', 'admin')->first();
|
||||
$parent = Permission::where('name', 'users')->first();
|
||||
|
||||
$this->actingAs($admin)->get(route('admin.roles.create'))->assertInertia(fn (Assert $page) => $page->component('Roles/create_and_edit')->has('permissions'));
|
||||
$this->actingAs($admin)->get(route('admin.roles.edit', $role))->assertInertia(fn (Assert $page) => $page
|
||||
->component('Roles/create_and_edit')
|
||||
->where('is_edit', true)
|
||||
->where('role.id', $role->id)
|
||||
->has("role.permissions.{$parent->id}"));
|
||||
}
|
||||
|
||||
public function test_store_creates_role_with_permissions(): void
|
||||
{
|
||||
$admin = $this->adminWithAllPermissions();
|
||||
$permissionIds = Permission::where('name', 'like', 'users.%')->pluck('id')->all();
|
||||
|
||||
$this->actingAs($admin)->post(route('admin.roles.store'), ['name' => 'editor', 'label' => 'ویرایشگر', 'selectedPermissionIds' => [$permissionIds]])->assertCreated()->assertSessionHas('success');
|
||||
|
||||
$role = Role::where('name', 'editor')->first();
|
||||
$this->assertNotNull($role);
|
||||
$this->assertEqualsCanonicalizing($permissionIds, $role->permissions->pluck('id')->all());
|
||||
}
|
||||
|
||||
public function test_store_validates_input(): void
|
||||
{
|
||||
$this->actingAs($this->adminWithAllPermissions())->postJson(route('admin.roles.store'), [])->assertUnprocessable()->assertJsonValidationErrors(['name', 'label']);
|
||||
}
|
||||
|
||||
public function test_update_changes_role_and_permissions(): void
|
||||
{
|
||||
$admin = $this->adminWithAllPermissions();
|
||||
$role = Role::create(['name' => 'editor', 'label' => 'x']);
|
||||
$permission = Permission::where('name', 'pets.index')->first();
|
||||
|
||||
$this->actingAs($admin)->put(route('admin.roles.update', $role), ['name' => 'editor2', 'label' => 'y', 'selectedPermissionIds' => [[$permission->id]]])->assertOk();
|
||||
|
||||
$role->refresh();
|
||||
$this->assertSame('editor2', $role->name);
|
||||
$this->assertTrue($role->hasPermissionTo($permission));
|
||||
}
|
||||
|
||||
public function test_destroy_and_bulk_delete_remove_roles(): void
|
||||
{
|
||||
$admin = $this->adminWithAllPermissions();
|
||||
$roles = collect(['a', 'b', 'c'])->map(fn ($name) => Role::create(['name' => $name, 'label' => $name]));
|
||||
|
||||
$this->actingAs($admin)->delete(route('admin.roles.destroy', $roles[0]))->assertOk();
|
||||
$this->actingAs($admin)->post('/admin/roles-bulk-delete', ['ids' => [$roles[1]->id, $roles[2]->id]])->assertOk();
|
||||
|
||||
$this->assertSame(1, Role::count());
|
||||
}
|
||||
|
||||
public function test_export_data_downloads_excel(): void
|
||||
{
|
||||
Excel::fake();
|
||||
|
||||
$this->actingAs($this->adminWithAllPermissions())->get('/admin/roles-export-data')->assertOk();
|
||||
|
||||
Excel::assertDownloaded('roles.xlsx');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user