mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:04:04 +00:00
HasScopedValidationRules trait
This commit is contained in:
parent
208eb2fdd3
commit
d92277a236
2 changed files with 69 additions and 1 deletions
19
src/Database/Concerns/HasScopedValidationRules.php
Normal file
19
src/Database/Concerns/HasScopedValidationRules.php
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Database\Concerns;
|
||||||
|
|
||||||
|
use Illuminate\Validation\Rules\Exists;
|
||||||
|
use Illuminate\Validation\Rules\Unique;
|
||||||
|
|
||||||
|
trait HasScopedValidationRules
|
||||||
|
{
|
||||||
|
public function unique($table, $column = 'NULL')
|
||||||
|
{
|
||||||
|
return (new Unique($table, $column))->where(BelongsToTenant::$tenantIdColumn, $this->getTenantKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function exists($table, $column = 'NULL')
|
||||||
|
{
|
||||||
|
return (new Exists($table, $column))->where(BelongsToTenant::$tenantIdColumn, $this->getTenantKey());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,9 +7,13 @@ use Illuminate\Database\QueryException;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
use Illuminate\Validation\Rules\Unique;
|
||||||
use Stancl\Tenancy\Database\Concerns\BelongsToPrimaryModel;
|
use Stancl\Tenancy\Database\Concerns\BelongsToPrimaryModel;
|
||||||
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Database\Concerns\HasScopedValidationRules;
|
||||||
|
use Stancl\Tenancy\Tests\Etc\Tenant as TestTenant;
|
||||||
|
|
||||||
class SingleDatabaseTenancyTest extends TestCase
|
class SingleDatabaseTenancyTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
@ -36,6 +40,8 @@ class SingleDatabaseTenancyTest extends TestCase
|
||||||
|
|
||||||
$table->foreign('post_id')->references('id')->on('posts')->onUpdate('cascade')->onDelete('cascade');
|
$table->foreign('post_id')->references('id')->on('posts')->onUpdate('cascade')->onDelete('cascade');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
config(['tenancy.tenant_model' => Tenant::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
|
@ -273,6 +279,49 @@ class SingleDatabaseTenancyTest extends TestCase
|
||||||
// Assert foobar models are inaccessible in acme context
|
// Assert foobar models are inaccessible in acme context
|
||||||
$this->assertSame(1, Post::count());
|
$this->assertSame(1, Post::count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function the_model_returned_by_the_tenant_helper_has_unique_and_exists_validation_rules()
|
||||||
|
{
|
||||||
|
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
|
||||||
|
$this->assertTrue($uniqueFails);
|
||||||
|
$this->assertFalse($existsFails);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Tenant extends TestTenant
|
||||||
|
{
|
||||||
|
use HasScopedValidationRules;
|
||||||
}
|
}
|
||||||
|
|
||||||
class Post extends Model
|
class Post extends Model
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue