Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[4.x] Test that global scopes on syncable models can break resource syncing #1285

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
85 changes: 85 additions & 0 deletions tests/ResourceSyncingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
use Stancl\Tenancy\Tests\Etc\ResourceSyncing\CentralUser as BaseCentralUser;
use Stancl\Tenancy\ResourceSyncing\CentralResourceNotAvailableInPivotException;
use Stancl\Tenancy\ResourceSyncing\Events\SyncedResourceSavedInForeignDatabase;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use Illuminate\Database\QueryException;

beforeEach(function () {
config(['tenancy.bootstrappers' => [
Expand Down Expand Up @@ -107,6 +110,28 @@
UpdateOrCreateSyncedResource::$scopeGetModelQuery = null;
});

test('multiple tenants can have users synced to a central resource', function() {
$tenants = [Tenant::create(), Tenant::create(), Tenant::create()];
migrateUsersTableForTenants();

tenancy()->runForMultiple($tenants, function () {
// Create a user in tenant DB
TenantUser::create([
'global_id' => 'acme',
'name' => Str::random(),
'email' => 'john@localhost',
'password' => 'secret',
'role' => 'commenter',
]);
});

// Create the same user in tenant DB
$users = CentralUser::where(['global_id' => 'acme'])->get();

expect($users)->toHaveCount(1);
expect($users->first()->global_id)->toBe('acme');
});

test('SyncedResourceSaved event gets triggered when resource gets created or when its synced attributes get updated', function () {
Event::fake(SyncedResourceSaved::class);

Expand Down Expand Up @@ -1172,6 +1197,53 @@
expect($centralUser->foo)->toBe('bar');
});

test('resource syncing works correctly when using a global scope on a tenant model', function(bool $scopeGetModelQuery) {
if ($scopeGetModelQuery) {
UpdateOrCreateSyncedResource::$scopeGetModelQuery = function (Builder $query) {
if ($query->getModel()->hasGlobalScope(TestingScope::class)) {
$query->withoutGlobalScope(TestingScope::class);
}
};
}

$centralUser = CentralUser::firstOrCreate(
['email' => '[email protected]'],
['name' => 'User', 'password' => bcrypt('****'), 'role' => 'admin']
);

[$tenant1, $tenant2] = createTenantsAndRunMigrations();

tenancy()->initialize($tenant1);

TenantUserWithScope::create([
'global_id' => $centralUser->global_id,
'name' => $centralUser->name,
'email' => $centralUser->email,
'password' => $centralUser->password,
'role' => 'admin'
]);

tenancy()->end();

tenancy()->initialize($tenant2);

if (! $scopeGetModelQuery) {
pest()->expectException(QueryException::class);
pest()->expectExceptionMessage('Duplicate entry');
}

TenantUserWithScope::create([
'global_id' => $centralUser->global_id,
'name' => $centralUser->name,
'email' => $centralUser->email,
'password' => $centralUser->password,
'role' => 'admin'
]);
})->with([
true,
false,
]);

/**
* Create two tenants and run migrations for those tenants.
*
Expand Down Expand Up @@ -1243,6 +1315,11 @@ public function tenants(): BelongsToMany
}
}

#[ScopedBy([TestingScope::class])]
class TenantUserWithScope extends TenantUser
{
}

class TenantPivot extends BasePivot
{
public $table = 'tenant_users';
Expand Down Expand Up @@ -1344,3 +1421,11 @@ public function getSyncedAttributeNames(): array
];
}
}

class TestingScope implements Scope
{
public function apply(Builder $builder, Model $model): void
{
$builder->whereNull('name');
}
}
Loading