Skip to content

Commit

Permalink
Handle empty values & update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Jan 30, 2024
1 parent fc4ef15 commit a4960cc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/src/Controller/Role/RoleUpdateFieldAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected function handle(
if (isset($put[$fieldName])) {
$fieldData = $put[$fieldName];
} elseif ($fieldName === 'permissions') {
$fieldData = $put['value'];
$fieldData = $put['value'] ?? [];
} else {
$e = new MissingRequiredParamException();
$e->setParam($fieldName);
Expand Down
34 changes: 33 additions & 1 deletion app/tests/Controller/Role/RoleUpdateFieldActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,39 @@ public function testPostForPermission(): void
$this->assertSame('Permissions updated for role <strong>' . $role->name . '</strong>', array_reverse($messages)[0]['message']);
}

public function testPostForRemovingRoles(): void
public function testPostForRemovingAllRoles(): void
{
/** @var User */
$user = User::factory()->create();
$this->actAsUser($user, permissions: ['update_role_field']);

/** @var Role */
$role = Role::factory()->has(Permission::factory())->create();
$this->assertCount(1, $role->permissions); // Default role above.

// Create request with method and url and fetch response
// uf-collection will pass no data when removing all roles_id.
$data = ['permissions' => []];
$request = $this->createJsonRequest('PUT', '/api/roles/r/' . $role->slug . '/permissions', $data);
$response = $this->handleRequest($request);

// Assert response status & body
$this->assertJsonResponse([], $response);
$this->assertResponseStatus(200, $response);

// Make sure the user has the new roles.
$role->refresh();
$this->assertCount(0, $role->permissions);

// Test message
/** @var AlertStream */
$ms = $this->ci->get(AlertStream::class);
$messages = $ms->getAndClearMessages();
$this->assertSame('success', array_reverse($messages)[0]['type']);
$this->assertSame('Permissions updated for role <strong>' . $role->name . '</strong>', array_reverse($messages)[0]['message']);
}

public function testPostForMissingValueArgument(): void
{
/** @var User */
$user = User::factory()->create();
Expand Down

0 comments on commit a4960cc

Please sign in to comment.