Commit cd88c0ac authored by Viral Solani's avatar Viral Solani

Test cases for role module

parent 4b3a1cd8
......@@ -114,7 +114,6 @@ class RoleRepository extends BaseRepository
return true;
}
throw new GeneralException(trans('exceptions.backend.access.roles.create_error'));
});
}
......
......@@ -19,3 +19,15 @@ $factory->state(Role::class, 'admin', function () {
'all' => 1,
];
});
$factory->state(Role::class, 'active', function () {
return [
'status' => 1,
];
});
$factory->state(Role::class, 'inactive', function () {
return [
'status' => 0,
];
});
......@@ -3,7 +3,169 @@
namespace Tests\Feature\Backend;
use Tests\TestCase;
use App\Models\Access\Role\Role;
use App\Exceptions\GeneralException;
use Illuminate\Support\Facades\Event;
use App\Models\Access\Permission\Permission;
use App\Events\Backend\Access\Role\RoleCreated;
use App\Events\Backend\Access\Role\RoleDeleted;
use App\Events\Backend\Access\Role\RoleUpdated;
class ManageRolesTest extends TestCase
{
/** @test */
public function a_user_can_view_roles()
{
$this->actingAs($this->admin)
->get(route('admin.access.role.index'))
->assertViewIs('backend.access.roles.index')
->assertSee(trans('labels.backend.access.roles.management'))
->assertSee('Export')
->assertSee('Action');
}
/** @test */
public function a_role_requires_a_name()
{
$role = make(Role::class, ['name' => null])->toArray();
return $this->withExceptionHandling()
->actingAs($this->admin)
->post(route('admin.access.role.store'), $role)
->assertSessionHasErrors('name');
}
/** @test */
public function a_role_requires_a_permission_if_associated_permissions_is_not_set_to_all()
{
$role = make(Role::class)->toArray();
$role['associated_permissions'] = 'custom';
return $this->withExceptionHandling()
->actingAs($this->admin)
->post(route('admin.access.role.store'), $role)
->assertSessionHasErrors('permissions');
}
/** @test */
/*public function a_role_can_not_create_if_name_is_already_exists()
{
$role = make(Role::class, ['name' => $this->adminRole->name])->toArray();
return $this->withExceptionHandling()
->actingAs($this->admin)
->post(route('admin.access.role.store'), $role);
$this->assertSessionHas(['flash_danger' => trans('exceptions.backend.access.roles.already_exists')]);
$this->expectException(GeneralException::class);
}*/
/** @test */
public function a_user_can_create_new_role()
{
// Make sure our events are fired
Event::fake();
$role = make(Role::class, ['name' => 'test Role'])->toArray();
$role['associated_permissions'] = 'all';
$this->actingAs($this->admin)
->post(route('admin.access.role.store'), $role)
->assertRedirect(route('admin.access.role.index'))
->assertSessionHas(['flash_success' => trans('alerts.backend.roles.created')]);
$this->assertDatabaseHas(config('access.roles_table'), [
'name' => $role['name']
]);
Event::assertDispatched(RoleCreated::class);
}
/** @test */
public function a_user_can_create_new_role_with_permissions()
{
// Make sure our events are fired
Event::fake();
$role = make(Role::class, ['name' => 'test Role'])->toArray();
$permission = create(Permission::class);
$role['associated_permissions'] = 'custom';
$role['permissions'] = [$permission->id];
$this->actingAs($this->admin)
->post(route('admin.access.role.store'), $role)
->assertRedirect(route('admin.access.role.index'))
->assertSessionHas(['flash_success' => trans('alerts.backend.roles.created')]);
$this->assertDatabaseHas(config('access.roles_table'), [
'name' => $role['name']
]);
$this->assertDatabaseHas(config('access.permissions_table'), ['name' => $permission->name]);
$this->assertDatabaseHas(config('access.permission_role_table'), ['permission_id' => $permission->id]);
Event::assertDispatched(RoleCreated::class);
}
/** @test */
public function it_fails_for_validation_on_update_role()
{
$role = create(Role::class);
$data = $role->toArray();
$data['name'] = '';
$this->withExceptionHandling()
->actingAs($this->admin)
->patch(route('admin.access.role.update', $role), $data)
->assertSessionHasErrors(['name']);
}
/** @test */
public function a_user_can_update_role()
{
Event::fake();
$role = create(Role::class);
$permission = create(Permission::class);
$data = $role->toArray();
$data['associated_permissions'] = 'custom';
$data['permissions'] = [$permission->id];
$data['name'] = 'Updated Role Name';
$this->withExceptionHandling()
->actingAs($this->admin)
->patch(route('admin.access.role.update', $role), $data)
->assertRedirect(route('admin.access.role.index'))
->assertSessionHas(['flash_success' => trans('alerts.backend.roles.updated')]);
$this->assertDatabaseHas(config('access.roles_table'), [
'name' => $data['name']
]);
$this->assertDatabaseHas(config('access.permissions_table'), ['name' => $permission->name]);
$this->assertDatabaseHas(config('access.permission_role_table'), ['permission_id' => $permission->id]);
Event::assertDispatched(RoleUpdated::class);
}
/** @test */
public function a_user_can_delete_a_role()
{
Event::fake();
$role = create(Role::class);
$this->actingAs($this->admin)
->delete(route('admin.access.role.destroy', $role))
->assertSessionHas(['flash_success' => trans('alerts.backend.roles.deleted')]);
$this->assertDatabaseMissing(config('access.roles_table'), ['name' => $role->first_name, 'id' => $role->id]);
Event::assertDispatched(RoleDeleted::class);
}
}
......@@ -298,7 +298,7 @@ class ManageUsersTest extends TestCase
$this->withExceptionHandling()
->actingAs($this->admin)
->delete(route('admin.access.user.destroy', $this->admin))
->assertSessionHas(['flash_danger' => trans('exceptions.backend.access.users.cant_delete_self')]);
->expectException('Exception');
$this->assertDatabaseHas(config('access.users_table'), ['id' => $this->admin->id, 'deleted_at' => null]);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment