Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix nesting tests #2364

Merged
merged 1 commit into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ public function __construct(array $attributes = [])
parent::__construct($attributes);

$this->guarded[] = $this->primaryKey;
}

public function getTable()
{
return config('permission.table_names.permissions', parent::getTable());
$this->table = config('permission.table_names.permissions') ?: parent::getTable();
}

public static function create(array $attributes = [])
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(array $attributes = [])
parent::__construct($attributes);

$this->guarded[] = $this->primaryKey;
$this->table = config('permission.table_names.roles', parent::getTable());
$this->table = config('permission.table_names.roles') ?: parent::getTable();
}

public static function create(array $attributes = [])
Expand Down
26 changes: 26 additions & 0 deletions tests/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,32 @@ class Role extends \Spatie\Permission\Models\Role
'name',
];

const HIERARCHY_TABLE = 'roles_hierarchy';

/**
* @return BelongsToMany
*/
public function parents()
{
return $this->belongsToMany(
static::class,
static::HIERARCHY_TABLE,
'child_id',
'parent_id');
}

/**
* @return BelongsToMany
*/
public function children()
{
return $this->belongsToMany(
static::class,
static::HIERARCHY_TABLE,
'parent_id',
'child_id');
}

protected static function boot()
{
parent::boot();
Expand Down
36 changes: 0 additions & 36 deletions tests/RoleWithNesting.php

This file was deleted.

77 changes: 36 additions & 41 deletions tests/RoleWithNestingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,58 @@

class RoleWithNestingTest extends TestCase
{
private static $old_migration;
/** @var bool */
protected $useCustomModels = true;

/**
* @var RoleWithNesting[]
*/
/** @var Role[] */
protected $parent_roles = [];

/**
* @var RoleWithNesting[]
*/
/** @var Role[] */
protected $child_roles = [];

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
self::$old_migration = self::$migration;
self::$migration = self::getMigration();
}

public function setUp(): void
{
parent::setUp();
$this->parent_roles = [];
$this->child_roles = [];
$this->parent_roles['has_no_children'] = RoleWithNesting::create(['name' => 'has_no_children']);
$this->parent_roles['has_1_child'] = RoleWithNesting::create(['name' => 'has_1_child']);
$this->parent_roles['has_3_children'] = RoleWithNesting::create(['name' => 'has_3_children']);

$this->child_roles['has_no_parents'] = RoleWithNesting::create(['name' => 'has_no_parents']);
$this->child_roles['has_1_parent'] = RoleWithNesting::create(['name' => 'has_1_parent']);
$this->child_roles['has_2_parents'] = RoleWithNesting::create(['name' => 'has_2_parents']);
$this->child_roles['third_child'] = RoleWithNesting::create(['name' => 'third_child']);
$this->parent_roles = [
'has_no_children' => Role::create(['name' => 'has_no_children']),
'has_1_child' => Role::create(['name' => 'has_1_child']),
'has_3_children' => Role::create(['name' => 'has_3_children']),
];
$this->child_roles = [
'has_no_parents' => Role::create(['name' => 'has_no_parents']),
'has_1_parent' => Role::create(['name' => 'has_1_parent']),
'has_2_parents' => Role::create(['name' => 'has_2_parents']),
'third_child' => Role::create(['name' => 'third_child']),
];

$this->parent_roles['has_1_child']->children()->attach($this->child_roles['has_2_parents']);
$this->parent_roles['has_3_children']->children()->attach($this->child_roles['has_2_parents']);
$this->parent_roles['has_3_children']->children()->attach($this->child_roles['has_1_parent']);
$this->parent_roles['has_3_children']->children()->attach($this->child_roles['third_child']);
$this->parent_roles['has_3_children']->children()->attach([
$this->child_roles['has_2_parents']->getKey(),
$this->child_roles['has_1_parent']->getKey(),
$this->child_roles['third_child']->getKey()
]);
}

public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
self::$migration = self::$old_migration;
}

protected function getEnvironmentSetUp($app)
/**
* Set up the database.
*
* @param \Illuminate\Foundation\Application $app
*/
protected function setUpDatabase($app)
{
parent::getEnvironmentSetUp($app);
$app['config']->set('permission.models.role', RoleWithNesting::class);
$app['config']->set('permission.table_names.roles', 'nesting_role');
}
parent::setUpDatabase($app);

protected static function getMigration()
{
require_once __DIR__.'/customMigrations/roles_with_nesting_migration.php.stub';
$tableRoles = $app['config']->get('permission.table_names.roles');

return new \CreatePermissionTablesWithNested();
$app['db']->connection()->getSchemaBuilder()->create(Role::HIERARCHY_TABLE, function ($table) use ($tableRoles) {
$table->id();
$table->uuid("parent_id");
$table->uuid("child_id");
$table->foreign("parent_id")->references("role_test_id")->on($tableRoles);
$table->foreign("child_id")->references("role_test_id")->on($tableRoles);
});
}

/** @test
Expand All @@ -71,7 +66,7 @@ public function it_returns_correct_withCount_of_nested_roles($role_group, $index
$role = $this->$role_group[$index];
$count_field_name = sprintf('%s_count', $relation);

$actualCount = intval(RoleWithNesting::query()->withCount($relation)->find($role->id)->$count_field_name);
$actualCount = intval(Role::withCount($relation)->find($role->getKey())->$count_field_name);

$this->assertSame(
$expectedCount,
Expand Down
40 changes: 0 additions & 40 deletions tests/customMigrations/roles_with_nesting_migration.php.stub

This file was deleted.