Skip to content

Commit

Permalink
Fix #1252 - For Permission & Role
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Apr 25, 2024
1 parent d993625 commit 4c5f270
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [5.1.2](https://github.com/userfrosting/sprinkle-account/compare/5.1.1...5.1.2)
- [Fix #1252](https://github.com/userfrosting/UserFrosting/issues/1252) - For Permission & Role

## [5.1.1](https://github.com/userfrosting/sprinkle-account/compare/5.1.0...5.1.1)
- [Fix #1252](https://github.com/userfrosting/UserFrosting/issues/1252) - Column not found when extending the User (or Group) model

Expand Down
1 change: 1 addition & 0 deletions app/src/Database/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public function users(): BelongsToManyThrough
$roleRelation,
firstJoiningTable: 'permission_roles',
secondJoiningTable: 'role_users',
secondRelatedKey: 'user_id',
);
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/Database/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function users(): BelongsToMany
/** @var string */
$relation = static::$ci?->get(UserInterface::class);

return $this->belongsToMany($relation, 'role_users');
return $this->belongsToMany($relation, 'role_users', relatedPivotKey: 'user_id');
}

/**
Expand Down
2 changes: 2 additions & 0 deletions app/tests/Database/Models/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPUnit\Framework\Attributes\TestWith;
use UserFrosting\Sprinkle\Account\Database\Models\Group;
use UserFrosting\Sprinkle\Account\Database\Models\Interfaces\GroupInterface;
use UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface;
use UserFrosting\Sprinkle\Account\Database\Models\User;
use UserFrosting\Sprinkle\Account\Tests\AccountTestCase;
use UserFrosting\Sprinkle\Core\Testing\RefreshDatabase;
Expand Down Expand Up @@ -102,6 +103,7 @@ public function testUserRelation(): void
#[TestWith([ZeGroup::class])]
public function testRelations(string $model): void
{
$this->ci->set(UserInterface::class, Member::class);
$object = new $model([
'slug' => 'testing',
'name' => 'Test Group',
Expand Down
15 changes: 15 additions & 0 deletions app/tests/Database/Models/PermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,19 @@ public function testUserRoleRelation(): void
$this->assertCount(2, $result);
$this->assertSame([$permission->slug, $newPermission->slug], array_keys($result));
}

/**
* Test relations setup are working, even if the class is extended
* @see https://github.com/userfrosting/UserFrosting/issues/1252
*/
public function testUserExtension(): void
{
$this->ci->set(UserInterface::class, Member::class);
$permission = new Permission([
'name' => 'Test',
'slug' => 'test',
'conditions' => 'always()',
]);
$this->assertCount(0, $permission->users()->get());
}
}
14 changes: 14 additions & 0 deletions app/tests/Database/Models/RoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,18 @@ public function testUserScopeForRole(): void
$this->assertSame(1, $users->count());
$this->assertSame([$userBar->id], $users->pluck('id')->all());
}

/**
* Test relations setup are working, even if the class is extended
* @see https://github.com/userfrosting/UserFrosting/issues/1252
*/
public function testUserExtension(): void
{
$this->ci->set(UserInterface::class, Member::class);
$role = new Role([
'name' => 'Test',
'slug' => 'test',
]);
$this->assertCount(0, $role->users()->get());
}
}

0 comments on commit 4c5f270

Please sign in to comment.