Skip to content

Commit

Permalink
Merge pull request #386 from sfadschm/cache-null-checks
Browse files Browse the repository at this point in the history
Fix cache() return value checks.
  • Loading branch information
lonnieezell authored Jun 28, 2021
2 parents 7983635 + b89bab8 commit 5690788
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Authorization/GroupModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function removeUserFromAllGroups(int $userId)
*/
public function getGroupsForUser(int $userId)
{
if (! $found = cache("{$userId}_groups"))
if (null === $found = cache("{$userId}_groups"))
{
$found = $this->builder()
->select('auth_groups_users.*, auth_groups.name, auth_groups.description')
Expand All @@ -115,7 +115,7 @@ public function getGroupsForUser(int $userId)
*/
public function getUsersForGroup(int $groupId)
{
if (! $found = cache("{$groupId}_users"))
if (null === $found = cache("{$groupId}_users"))
{
$found = $this->builder()
->select('auth_groups_users.*, users.*')
Expand Down
2 changes: 1 addition & 1 deletion src/Authorization/PermissionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function removePermissionFromUser(int $permissionId, int $userId)
*/
public function getPermissionsForUser(int $userId): array
{
if (! $found = cache("{$userId}_permissions"))
if (null === $found = cache("{$userId}_permissions"))
{
$fromUser = $this->db->table('auth_users_permissions')
->select('id, auth_permissions.name')
Expand Down
87 changes: 86 additions & 1 deletion tests/authorization/GroupModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,56 @@ public function testGetGroupsForUser()
]);
$this->assertEquals($result[1], [
'group_id' => $group2->id,
'user_id' => $user->id,
'user_id' => $user->id,
'name' => $group2->name,
'description' => $group2->description
]);
}

public function testGetGroupsForUserFromCache()
{
$user = $this->createUser();
$group1 = $this->createGroup();
$group2 = $this->createGroup();

$this->hasInDatabase('auth_groups_users', [
'user_id' => $user->id,
'group_id' => $group1->id
]);

$cacheGroups = [
[
'group_id' => $group2->id,
'user_id' => $user->id,
'name' => 'notemptygroup',
'description' => 'This group can only be loaded from cache.'
]
];
cache()->save("{$user->id}_groups", $cacheGroups, 300);

$result = $this->model->getGroupsForUser($user->id);

$this->assertSame($result, $cacheGroups);
}

public function testGetEmptyGroupsForUserFromCache()
{
$user = $this->createUser();
$group1 = $this->createGroup();

$this->hasInDatabase('auth_groups_users', [
'user_id' => $user->id,
'group_id' => $group1->id
]);

$cacheGroups = [];
cache()->save("{$user->id}_groups", $cacheGroups, 300);

$result = $this->model->getGroupsForUser($user->id);

$this->assertSame($result, $cacheGroups);
}

public function testGetUsersForGroup()
{
$group = $this->createGroup();
Expand All @@ -142,6 +186,47 @@ public function testGetUsersForGroup()
$this->assertEquals($user2->id, $result[1]['id']);
}

public function testGetUsersForGroupFromCache()
{
$group = $this->createGroup();
$user1 = fake(UserFaker::class);
$user2 = fake(UserFaker::class);

$this->hasInDatabase('auth_groups_users', [
'user_id' => $user1->id,
'group_id' => $group->id
]);

$cacheUsers = [
'group_id' => $group->id,
'user_id' => $user2->id,
'email' => 'gonnaSkip@theOtherProperties.lazy'
];
cache()->save("{$group->id}_users", $cacheUsers, 300);

$result = $this->model->getUsersForGroup($group->id);

$this->assertSame($result, $cacheUsers);
}

public function testGetEmptyUsersForGroupFromCache()
{
$group = $this->createGroup();
$user = fake(UserFaker::class);

$this->hasInDatabase('auth_groups_users', [
'user_id' => $user->id,
'group_id' => $group->id
]);

$cacheUsers = [];
cache()->save("{$group->id}_users", $cacheUsers, 300);

$result = $this->model->getUsersForGroup($group->id);

$this->assertSame($result, $cacheUsers);
}

public function testAddPermissionToGroup()
{
$group = $this->createGroup();
Expand Down
35 changes: 35 additions & 0 deletions tests/authorization/PermissionModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,41 @@ public function testGetPermissionsForUser()
$this->assertEquals($expected, $this->model->getPermissionsForUser($user->id));
}

public function testGetPermissionsForUserFromCache()
{
$user = $this->createUser();
$permission1 = $this->createPermission(['name' => 'first']);
$permission2 = $this->createPermission(['name' => 'second']);

$this->hasInDatabase('auth_users_permissions', [
'user_id' => $user->id,
'permission_id' => $permission1->id
]);

$cachePermissions = [
$permission2->id => $permission2->name
];
cache()->save("{$user->id}_permissions", $cachePermissions, 300);

$this->assertEquals($cachePermissions, $this->model->getPermissionsForUser($user->id));
}

public function testGetEmptyPermissionsForUserFromCache()
{
$user = $this->createUser();
$permission1 = $this->createPermission(['name' => 'first']);

$this->hasInDatabase('auth_users_permissions', [
'user_id' => $user->id,
'permission_id' => $permission1->id
]);

$cachePermissions = [];
cache()->save("{$user->id}_permissions", $cachePermissions, 300);

$this->assertEquals($cachePermissions, $this->model->getPermissionsForUser($user->id));
}

public function testDoesUserHavePermissionByGroupAssign()
{
$user = $this->createUser();
Expand Down

0 comments on commit 5690788

Please sign in to comment.