mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 18:34:04 +00:00
309 lines
8.6 KiB
PHP
309 lines
8.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
|
|
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
|
|
beforeEach(function () {
|
|
BelongsToTenant::$tenantIdColumn = 'tenant_id';
|
|
|
|
Schema::create('posts', function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->string('text');
|
|
|
|
$table->string('tenant_id');
|
|
|
|
$table->foreign('tenant_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade');
|
|
});
|
|
|
|
Schema::create('comments', function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->string('text');
|
|
|
|
$table->unsignedInteger('post_id');
|
|
|
|
$table->foreign('post_id')->references('id')->on('posts')->onUpdate('cascade')->onDelete('cascade');
|
|
});
|
|
|
|
config(['tenancy.tenant_model' => Tenant::class]);
|
|
});
|
|
|
|
test('primary models are scoped to the current tenant', function () {
|
|
// acme context
|
|
tenancy()->initialize($acme = Tenant::create([
|
|
'id' => 'acme',
|
|
]));
|
|
|
|
$post = Post::create(['text' => 'Foo']);
|
|
|
|
expect($post->tenant_id)->toBe('acme');
|
|
expect($post->tenant->id)->toBe('acme');
|
|
|
|
$post = Post::first();
|
|
|
|
expect($post->tenant_id)->toBe('acme');
|
|
expect($post->tenant->id)->toBe('acme');
|
|
|
|
// ======================================
|
|
// foobar context
|
|
tenancy()->initialize($foobar = Tenant::create([
|
|
'id' => 'foobar',
|
|
]));
|
|
|
|
$post = Post::create(['text' => 'Bar']);
|
|
|
|
expect($post->tenant_id)->toBe('foobar');
|
|
expect($post->tenant->id)->toBe('foobar');
|
|
|
|
$post = Post::first();
|
|
|
|
expect($post->tenant_id)->toBe('foobar');
|
|
expect($post->tenant->id)->toBe('foobar');
|
|
|
|
// ======================================
|
|
// acme context again
|
|
|
|
tenancy()->initialize($acme);
|
|
|
|
$post = Post::first();
|
|
expect($post->tenant_id)->toBe('acme');
|
|
expect($post->tenant->id)->toBe('acme');
|
|
|
|
// Assert foobar models are inaccessible in acme context
|
|
expect(Post::count())->toBe(1);
|
|
});
|
|
|
|
test('primary models are not scoped in the central context', function () {
|
|
$this->primary_models_are_scoped_to_the_current_tenant();
|
|
|
|
tenancy()->end();
|
|
|
|
expect(Post::count())->toBe(2);
|
|
});
|
|
|
|
test('secondary models are scoped to the current tenant when accessed via primary model', function () {
|
|
// acme context
|
|
tenancy()->initialize($acme = Tenant::create([
|
|
'id' => 'acme',
|
|
]));
|
|
|
|
$post = Post::create(['text' => 'Foo']);
|
|
$post->comments()->create(['text' => 'Comment text']);
|
|
|
|
// ================
|
|
// foobar context
|
|
tenancy()->initialize($foobar = Tenant::create([
|
|
'id' => 'foobar',
|
|
]));
|
|
|
|
$post = Post::create(['text' => 'Bar']);
|
|
$post->comments()->create(['text' => 'Comment text 2']);
|
|
|
|
// ================
|
|
// acme context again
|
|
tenancy()->initialize($acme);
|
|
expect(Post::count())->toBe(1);
|
|
expect(Post::first()->comments->count())->toBe(1);
|
|
});
|
|
|
|
test('secondary models are n o t scoped to the current tenant when accessed directly', function () {
|
|
$this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model();
|
|
|
|
// We're in acme context
|
|
expect(tenant('id'))->toBe('acme');
|
|
|
|
expect(Comment::count())->toBe(2);
|
|
});
|
|
|
|
test('secondary models a r e scoped to the current tenant when accessed directly a n d p a r e n t r e l a t i o n s h i p t r a i t i s u s e d', function () {
|
|
$acme = Tenant::create([
|
|
'id' => 'acme',
|
|
]);
|
|
|
|
$acme->run(function () {
|
|
$post = Post::create(['text' => 'Foo']);
|
|
$post->scoped_comments()->create(['text' => 'Comment Text']);
|
|
|
|
expect(Post::count())->toBe(1);
|
|
expect(ScopedComment::count())->toBe(1);
|
|
});
|
|
|
|
$foobar = Tenant::create([
|
|
'id' => 'foobar',
|
|
]);
|
|
|
|
$foobar->run(function () {
|
|
expect(Post::count())->toBe(0);
|
|
expect(ScopedComment::count())->toBe(0);
|
|
|
|
$post = Post::create(['text' => 'Bar']);
|
|
$post->scoped_comments()->create(['text' => 'Comment Text 2']);
|
|
|
|
expect(Post::count())->toBe(1);
|
|
expect(ScopedComment::count())->toBe(1);
|
|
});
|
|
|
|
// Global context
|
|
expect(ScopedComment::count())->toBe(2);
|
|
});
|
|
|
|
test('secondary models are n o t scoped in the central context', function () {
|
|
$this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model();
|
|
|
|
tenancy()->end();
|
|
|
|
expect(Comment::count())->toBe(2);
|
|
});
|
|
|
|
test('global models are not scoped at all', function () {
|
|
Schema::create('global_resources', function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->string('text');
|
|
});
|
|
|
|
GlobalResource::create(['text' => 'First']);
|
|
GlobalResource::create(['text' => 'Second']);
|
|
|
|
$acme = Tenant::create([
|
|
'id' => 'acme',
|
|
]);
|
|
|
|
$acme->run(function () {
|
|
expect(GlobalResource::count())->toBe(2);
|
|
|
|
GlobalResource::create(['text' => 'Third']);
|
|
GlobalResource::create(['text' => 'Fourth']);
|
|
});
|
|
|
|
expect(GlobalResource::count())->toBe(4);
|
|
});
|
|
|
|
test('tenant id and relationship is auto added when creating primary resources in tenant context', function () {
|
|
tenancy()->initialize($acme = Tenant::create([
|
|
'id' => 'acme',
|
|
]));
|
|
|
|
$post = Post::create(['text' => 'Foo']);
|
|
|
|
expect($post->tenant_id)->toBe('acme');
|
|
expect($post->relationLoaded('tenant'))->toBeTrue();
|
|
expect($post->tenant)->toBe($acme);
|
|
expect($post->tenant)->toBe(tenant());
|
|
});
|
|
|
|
test('tenant id is not auto added when creating primary resources in central context', function () {
|
|
$this->expectException(QueryException::class);
|
|
|
|
Post::create(['text' => 'Foo']);
|
|
});
|
|
|
|
test('tenant id column name can be customized', function () {
|
|
BelongsToTenant::$tenantIdColumn = 'team_id';
|
|
|
|
Schema::drop('comments');
|
|
Schema::drop('posts');
|
|
Schema::create('posts', function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->string('text');
|
|
|
|
$table->string('team_id');
|
|
|
|
$table->foreign('team_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade');
|
|
});
|
|
|
|
tenancy()->initialize($acme = Tenant::create([
|
|
'id' => 'acme',
|
|
]));
|
|
|
|
$post = Post::create(['text' => 'Foo']);
|
|
|
|
expect($post->team_id)->toBe('acme');
|
|
|
|
// ======================================
|
|
// foobar context
|
|
tenancy()->initialize($foobar = Tenant::create([
|
|
'id' => 'foobar',
|
|
]));
|
|
|
|
$post = Post::create(['text' => 'Bar']);
|
|
|
|
expect($post->team_id)->toBe('foobar');
|
|
|
|
$post = Post::first();
|
|
|
|
expect($post->team_id)->toBe('foobar');
|
|
|
|
// ======================================
|
|
// acme context again
|
|
|
|
tenancy()->initialize($acme);
|
|
|
|
$post = Post::first();
|
|
expect($post->team_id)->toBe('acme');
|
|
|
|
// Assert foobar models are inaccessible in acme context
|
|
expect(Post::count())->toBe(1);
|
|
});
|
|
|
|
test('the model returned by the tenant helper has unique and exists validation rules', function () {
|
|
Schema::table('posts', function (Blueprint $table) {
|
|
$table->string('slug')->nullable();
|
|
$table->unique(['tenant_id', 'slug']);
|
|
});
|
|
|
|
tenancy()->initialize($acme = Tenant::create([
|
|
'id' => 'acme',
|
|
]));
|
|
|
|
Post::create(['text' => 'Foo', 'slug' => 'foo']);
|
|
$data = ['text' => 'Foo 2', 'slug' => 'foo'];
|
|
|
|
$uniqueFails = Validator::make($data, [
|
|
'slug' => 'unique:posts',
|
|
])->fails();
|
|
$existsFails = Validator::make($data, [
|
|
'slug' => 'exists:posts',
|
|
])->fails();
|
|
|
|
// Assert that 'unique' and 'exists' aren't scoped by default
|
|
// $this->assertFalse($uniqueFails); // todo get these two assertions to pass. for some reason, the validator is passing for both 'unique' and 'exists'
|
|
// $this->assertTrue($existsFails); // todo get these two assertions to pass. for some reason, the validator is passing for both 'unique' and 'exists'
|
|
|
|
$uniqueFails = Validator::make($data, [
|
|
'slug' => tenant()->unique('posts'),
|
|
])->fails();
|
|
$existsFails = Validator::make($data, [
|
|
'slug' => tenant()->exists('posts'),
|
|
])->fails();
|
|
|
|
// Assert that tenant()->unique() and tenant()->exists() are scoped
|
|
expect($uniqueFails)->toBeTrue();
|
|
expect($existsFails)->toBeFalse();
|
|
});
|
|
|
|
// Helpers
|
|
function comments()
|
|
{
|
|
return test()->hasMany(Comment::class);
|
|
}
|
|
|
|
function scoped_comments()
|
|
{
|
|
return test()->hasMany(Comment::class);
|
|
}
|
|
|
|
function post()
|
|
{
|
|
return test()->belongsTo(Post::class);
|
|
}
|
|
|
|
function getRelationshipToPrimaryModel(): string
|
|
{
|
|
return 'post';
|
|
}
|