mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 20:14:04 +00:00
correct method name
This commit is contained in:
parent
01a3a5fbab
commit
eae2bd607c
5 changed files with 26 additions and 26 deletions
|
|
@ -14,7 +14,7 @@ use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||||
*/
|
*/
|
||||||
interface SyncMaster extends Syncable
|
interface SyncMaster extends Syncable
|
||||||
{
|
{
|
||||||
public function resources(): MorphToMany;
|
public function tenants(): MorphToMany;
|
||||||
|
|
||||||
public function getTenantModelName(): string;
|
public function getTenantModelName(): string;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ trait ResourceSyncing
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resources(): MorphToMany
|
public function tenants(): MorphToMany
|
||||||
{
|
{
|
||||||
return $this->morphToMany($this->getResourceTenantModelName(), 'tenant_resources', 'tenant_resources', 'resource_global_id', 'tenant_id', 'global_id')
|
return $this->morphToMany($this->getResourceTenantModelName(), 'tenant_resources', 'tenant_resources', 'resource_global_id', 'tenant_id', 'global_id')
|
||||||
->using(TenantPivot::class);
|
->using(TenantPivot::class);
|
||||||
|
|
|
||||||
|
|
@ -46,10 +46,10 @@ class UpdateSyncedResource extends QueueableListener
|
||||||
|
|
||||||
// Since this model is "dirty" (taken by reference from the event), it might have the tenants
|
// Since this model is "dirty" (taken by reference from the event), it might have the tenants
|
||||||
// relationship already loaded and cached. For this reason, we refresh the relationship.
|
// relationship already loaded and cached. For this reason, we refresh the relationship.
|
||||||
$centralModel->load('resources');
|
$centralModel->load('tenants');
|
||||||
|
|
||||||
/** @var TenantCollection $tenants */
|
/** @var TenantCollection $tenants */
|
||||||
$tenants = $centralModel->resources;
|
$tenants = $centralModel->tenants;
|
||||||
|
|
||||||
return $tenants;
|
return $tenants;
|
||||||
}
|
}
|
||||||
|
|
@ -80,7 +80,7 @@ class UpdateSyncedResource extends QueueableListener
|
||||||
return ((string) $model->pivot->tenant_id) === ((string) $tenant->getTenantKey());
|
return ((string) $model->pivot->tenant_id) === ((string) $tenant->getTenantKey());
|
||||||
};
|
};
|
||||||
|
|
||||||
$mappingExists = $centralModel->resources->contains($currentTenantMapping);
|
$mappingExists = $centralModel->tenants->contains($currentTenantMapping);
|
||||||
|
|
||||||
if (! $mappingExists) {
|
if (! $mappingExists) {
|
||||||
// Here we should call TenantPivot, but we call general Pivot, so that this works
|
// Here we should call TenantPivot, but we call general Pivot, so that this works
|
||||||
|
|
@ -89,12 +89,12 @@ class UpdateSyncedResource extends QueueableListener
|
||||||
/** @var Tenant */
|
/** @var Tenant */
|
||||||
$tenant = $event->tenant;
|
$tenant = $event->tenant;
|
||||||
|
|
||||||
$centralModel->resources()->attach($tenant->getTenantKey());
|
$centralModel->tenants()->attach($tenant->getTenantKey());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var TenantCollection $tenants */
|
/** @var TenantCollection $tenants */
|
||||||
$tenants = $centralModel->resources->filter(function ($model) use ($currentTenantMapping) {
|
$tenants = $centralModel->tenants->filter(function ($model) use ($currentTenantMapping) {
|
||||||
// Remove the mapping for the current tenant.
|
// Remove the mapping for the current tenant.
|
||||||
return ! $currentTenantMapping($model);
|
return ! $currentTenantMapping($model);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,7 @@ test('polymorphic relationship works for every model when syncing resources from
|
||||||
});
|
});
|
||||||
|
|
||||||
// When central model provides nothing/null, the resource model will be created as a 1:1 copy of central model
|
// When central model provides nothing/null, the resource model will be created as a 1:1 copy of central model
|
||||||
$centralUser->resources()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
||||||
$tenant1->run(function () use ($centralUser) {
|
$tenant1->run(function () use ($centralUser) {
|
||||||
$resourceUser = ResourceUserForPolymorphic::first()->only(['name', 'email', 'password', 'role']);
|
$resourceUser = ResourceUserForPolymorphic::first()->only(['name', 'email', 'password', 'role']);
|
||||||
|
|
@ -95,7 +95,7 @@ test('polymorphic relationship works for every model when syncing resources from
|
||||||
});
|
});
|
||||||
|
|
||||||
// When central model provides nothing/null, the resource model will be created as a 1:1 copy of central model
|
// When central model provides nothing/null, the resource model will be created as a 1:1 copy of central model
|
||||||
$centralCompany->resources()->attach('t2');
|
$centralCompany->tenants()->attach('t2');
|
||||||
|
|
||||||
$tenant2->run(function () use ($centralCompany) {
|
$tenant2->run(function () use ($centralCompany) {
|
||||||
$resourceCompany = ResourceCompanyForPolymorphic::first()->only(['name', 'email']);
|
$resourceCompany = ResourceCompanyForPolymorphic::first()->only(['name', 'email']);
|
||||||
|
|
|
||||||
|
|
@ -150,7 +150,7 @@ test('sync resource creation works when central model provides attributes and re
|
||||||
});
|
});
|
||||||
|
|
||||||
// When central model provides the list of attributes, resource model will be created from the provided list of attributes' values
|
// When central model provides the list of attributes, resource model will be created from the provided list of attributes' values
|
||||||
$centralUser->resources()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
||||||
$tenant1->run(function () {
|
$tenant1->run(function () {
|
||||||
$resourceUser = ResourceUserProvidingDefaultValues::all();
|
$resourceUser = ResourceUserProvidingDefaultValues::all();
|
||||||
|
|
@ -206,7 +206,7 @@ test('sync resource creation works when central model provides default values an
|
||||||
});
|
});
|
||||||
|
|
||||||
// When central model provides the list of default values, resource model will be created from the provided list of default values
|
// When central model provides the list of default values, resource model will be created from the provided list of default values
|
||||||
$centralUser->resources()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
||||||
$tenant1->run(function () {
|
$tenant1->run(function () {
|
||||||
// Assert resource user was created using the list of default values
|
// Assert resource user was created using the list of default values
|
||||||
|
|
@ -258,7 +258,7 @@ test('sync resource creation works when central model provides mixture and resou
|
||||||
});
|
});
|
||||||
|
|
||||||
// When central model provides the list of a mixture (attributes and default values), resource model will be created from the provided list of mixture (attributes and default values)
|
// When central model provides the list of a mixture (attributes and default values), resource model will be created from the provided list of mixture (attributes and default values)
|
||||||
$centralUser->resources()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
||||||
$tenant1->run(function () {
|
$tenant1->run(function () {
|
||||||
$resourceUser = ResourceUser::first();
|
$resourceUser = ResourceUser::first();
|
||||||
|
|
@ -312,7 +312,7 @@ test('sync resource creation works when central model provides nothing and resou
|
||||||
});
|
});
|
||||||
|
|
||||||
// When central model provides nothing/null, the resource model will be created as a 1:1 copy of central model
|
// When central model provides nothing/null, the resource model will be created as a 1:1 copy of central model
|
||||||
$centralUser->resources()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
||||||
expect($centralUser->getSyncedCreationAttributes())->toBeNull();
|
expect($centralUser->getSyncedCreationAttributes())->toBeNull();
|
||||||
$tenant1->run(function () use ($centralUser) {
|
$tenant1->run(function () use ($centralUser) {
|
||||||
|
|
@ -377,7 +377,7 @@ test('attaching a tenant to the central resource triggers a pull from the tenant
|
||||||
expect(ResourceUser::all())->toHaveCount(0);
|
expect(ResourceUser::all())->toHaveCount(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
$centralUser->resources()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
||||||
$tenant->run(function () {
|
$tenant->run(function () {
|
||||||
expect(ResourceUser::all())->toHaveCount(1);
|
expect(ResourceUser::all())->toHaveCount(1);
|
||||||
|
|
@ -433,8 +433,8 @@ test('resources are synced only to workspaces that have the resource', function
|
||||||
]);
|
]);
|
||||||
migrateUsersTableForTenants();
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
$centralUser->resources()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
$centralUser->resources()->attach('t2');
|
$centralUser->tenants()->attach('t2');
|
||||||
// t3 is not attached
|
// t3 is not attached
|
||||||
|
|
||||||
$t1->run(function () {
|
$t1->run(function () {
|
||||||
|
|
@ -472,7 +472,7 @@ test('when a resource exists in other tenant dbs but is created in a tenant db t
|
||||||
migrateUsersTableForTenants();
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
// Copy (cascade) user to t1 DB
|
// Copy (cascade) user to t1 DB
|
||||||
$centralUser->resources()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
||||||
$t2->run(function () {
|
$t2->run(function () {
|
||||||
// Create user with the same global ID in t2 database
|
// Create user with the same global ID in t2 database
|
||||||
|
|
@ -520,9 +520,9 @@ test('the synced columns are updated in other tenant dbs where the resource exis
|
||||||
migrateUsersTableForTenants();
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
// Copy (cascade) user to t1 DB
|
// Copy (cascade) user to t1 DB
|
||||||
$centralUser->resources()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
$centralUser->resources()->attach('t2');
|
$centralUser->tenants()->attach('t2');
|
||||||
$centralUser->resources()->attach('t3');
|
$centralUser->tenants()->attach('t3');
|
||||||
|
|
||||||
$t3->run(function () {
|
$t3->run(function () {
|
||||||
ResourceUser::first()->update([
|
ResourceUser::first()->update([
|
||||||
|
|
@ -574,7 +574,7 @@ test('when the resource doesnt exist in the tenant db non synced columns will ca
|
||||||
|
|
||||||
migrateUsersTableForTenants();
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
$centralUser->resources()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
||||||
$t1->run(function () {
|
$t1->run(function () {
|
||||||
expect(ResourceUser::first()->role)->toBe('employee');
|
expect(ResourceUser::first()->role)->toBe('employee');
|
||||||
|
|
@ -650,17 +650,17 @@ test('an event is fired for all touched resources', function () {
|
||||||
migrateUsersTableForTenants();
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
// Copy (cascade) user to t1 DB
|
// Copy (cascade) user to t1 DB
|
||||||
$centralUser->resources()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
Event::assertDispatched(SyncedResourceChangedInForeignDatabase::class, function (SyncedResourceChangedInForeignDatabase $event) {
|
Event::assertDispatched(SyncedResourceChangedInForeignDatabase::class, function (SyncedResourceChangedInForeignDatabase $event) {
|
||||||
return $event->tenant->getTenantKey() === 't1';
|
return $event->tenant->getTenantKey() === 't1';
|
||||||
});
|
});
|
||||||
|
|
||||||
$centralUser->resources()->attach('t2');
|
$centralUser->tenants()->attach('t2');
|
||||||
Event::assertDispatched(SyncedResourceChangedInForeignDatabase::class, function (SyncedResourceChangedInForeignDatabase $event) {
|
Event::assertDispatched(SyncedResourceChangedInForeignDatabase::class, function (SyncedResourceChangedInForeignDatabase $event) {
|
||||||
return $event->tenant->getTenantKey() === 't2';
|
return $event->tenant->getTenantKey() === 't2';
|
||||||
});
|
});
|
||||||
|
|
||||||
$centralUser->resources()->attach('t3');
|
$centralUser->tenants()->attach('t3');
|
||||||
Event::assertDispatched(SyncedResourceChangedInForeignDatabase::class, function (SyncedResourceChangedInForeignDatabase $event) {
|
Event::assertDispatched(SyncedResourceChangedInForeignDatabase::class, function (SyncedResourceChangedInForeignDatabase $event) {
|
||||||
return $event->tenant->getTenantKey() === 't3';
|
return $event->tenant->getTenantKey() === 't3';
|
||||||
});
|
});
|
||||||
|
|
@ -748,7 +748,7 @@ function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase()
|
||||||
expect(CentralUser::first()->role)->toBe('commenter');
|
expect(CentralUser::first()->role)->toBe('commenter');
|
||||||
|
|
||||||
// Assert mapping was created
|
// Assert mapping was created
|
||||||
expect(CentralUser::first()->resources)->toHaveCount(1);
|
expect(CentralUser::first()->tenants)->toHaveCount(1);
|
||||||
|
|
||||||
// Assert role change doesn't cascade
|
// Assert role change doesn't cascade
|
||||||
CentralUser::first()->update(['role' => 'central superadmin']);
|
CentralUser::first()->update(['role' => 'central superadmin']);
|
||||||
|
|
@ -785,7 +785,7 @@ test('resources are synced only when sync is enabled', function (bool $enabled)
|
||||||
'role' => 'commenter',
|
'role' => 'commenter',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$centralUser->resources()->attach('t2');
|
$centralUser->tenants()->attach('t2');
|
||||||
|
|
||||||
$tenant2->run(function () use ($enabled) {
|
$tenant2->run(function () use ($enabled) {
|
||||||
expect(TenantUserWithConditionalSync::all())->toHaveCount($enabled ? 1 : 0);
|
expect(TenantUserWithConditionalSync::all())->toHaveCount($enabled ? 1 : 0);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue