diff --git a/app/Couple.php b/app/Couple.php index 6427682..2908730 100644 --- a/app/Couple.php +++ b/app/Couple.php @@ -3,9 +3,17 @@ namespace App; use Illuminate\Database\Eloquent\Model; +use Ramsey\Uuid\Uuid; class Couple extends Model { + /** + * Indicates if the IDs are auto-incrementing. + * + * @var bool + */ + public $incrementing = false; + public function husband() { return $this->belongsTo(User::class); @@ -23,6 +31,7 @@ class Couple extends Model public function addChild(User $user) { + $user->id = Uuid::uuid4()->toString(); $user->parent_id = $this->id; $user->father_id = $this->husband_id; $user->mother_id = $this->wife_id; diff --git a/app/CouplePivot.php b/app/CouplePivot.php new file mode 100644 index 0000000..a190c09 --- /dev/null +++ b/app/CouplePivot.php @@ -0,0 +1,12 @@ + 'string', + ]; +} diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index dedef38..23a0551 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -2,10 +2,11 @@ namespace App\Http\Controllers\Auth; -use App\User; use App\Http\Controllers\Controller; -use Illuminate\Support\Facades\Validator; +use App\User; use Illuminate\Foundation\Auth\RegistersUsers; +use Illuminate\Support\Facades\Validator; +use Ramsey\Uuid\Uuid; class RegisterController extends Controller { @@ -65,6 +66,7 @@ class RegisterController extends Controller protected function create(array $data) { $user = User::create([ + 'id' => Uuid::uuid4()->toString(), 'nickname' => $data['nickname'], 'name' => $data['name'], 'gender_id' => $data['gender_id'], diff --git a/app/Http/Controllers/FamilyActionsController.php b/app/Http/Controllers/FamilyActionsController.php index 1e1dce5..037975e 100644 --- a/app/Http/Controllers/FamilyActionsController.php +++ b/app/Http/Controllers/FamilyActionsController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Couple; use App\User; use Illuminate\Http\Request; +use Ramsey\Uuid\Uuid; class FamilyActionsController extends Controller { @@ -12,7 +13,7 @@ class FamilyActionsController extends Controller { $this->validate($request, [ 'set_father_id' => 'nullable', - 'set_father' => 'required_without:set_father_id|max:255', + 'set_father' => 'required_without:set_father_id|max:255', ]); if ($request->get('set_father_id')) { @@ -20,6 +21,7 @@ class FamilyActionsController extends Controller $user->save(); } else { $father = new User; + $father->id = Uuid::uuid4()->toString(); $father->name = $request->get('set_father'); $father->nickname = $request->get('set_father'); $father->gender_id = 1; @@ -35,7 +37,7 @@ class FamilyActionsController extends Controller { $this->validate($request, [ 'set_mother_id' => 'nullable', - 'set_mother' => 'required_without:set_mother_id|max:255', + 'set_mother' => 'required_without:set_mother_id|max:255', ]); if ($request->get('set_mother_id')) { @@ -43,6 +45,7 @@ class FamilyActionsController extends Controller $user->save(); } else { $mother = new User; + $mother->id = Uuid::uuid4()->toString(); $mother->name = $request->get('set_mother'); $mother->nickname = $request->get('set_mother'); $mother->gender_id = 2; @@ -57,12 +60,13 @@ class FamilyActionsController extends Controller public function addChild(Request $request, User $user) { $this->validate($request, [ - 'add_child_name' => 'required|string|max:255', + 'add_child_name' => 'required|string|max:255', 'add_child_gender_id' => 'required|in:1,2', 'add_child_parent_id' => 'nullable|exists:couples,id', ]); $child = new User; + $child->id = Uuid::uuid4()->toString(); $child->name = $request->get('add_child_name'); $child->nickname = $request->get('add_child_name'); $child->gender_id = $request->get('add_child_gender_id'); @@ -78,10 +82,12 @@ class FamilyActionsController extends Controller $child->mother_id = $couple->wife_id; $child->save(); } else { - if ($user->gender_id == 1) + if ($user->gender_id == 1) { $child->setFather($user); - else + } else { $child->setMother($user); + } + } \DB::commit(); @@ -92,8 +98,8 @@ class FamilyActionsController extends Controller public function addWife(Request $request, User $user) { $this->validate($request, [ - 'set_wife_id' => 'nullable', - 'set_wife' => 'required_without:set_wife_id|max:255', + 'set_wife_id' => 'nullable', + 'set_wife' => 'required_without:set_wife_id|max:255', 'marriage_date' => 'nullable|date|date_format:Y-m-d', ]); @@ -101,6 +107,7 @@ class FamilyActionsController extends Controller $wife = User::findOrFail($request->get('set_wife_id')); } else { $wife = new User; + $wife->id = Uuid::uuid4()->toString(); $wife->name = $request->get('set_wife'); $wife->nickname = $request->get('set_wife'); $wife->gender_id = 2; @@ -116,14 +123,15 @@ class FamilyActionsController extends Controller { $this->validate($request, [ 'set_husband_id' => 'nullable', - 'set_husband' => 'required_without:set_husband_id|max:255', - 'marriage_date' => 'nullable|date|date_format:Y-m-d', + 'set_husband' => 'required_without:set_husband_id|max:255', + 'marriage_date' => 'nullable|date|date_format:Y-m-d', ]); if ($request->get('set_husband_id')) { $husband = User::findOrFail($request->get('set_husband_id')); } else { $husband = new User; + $husband->id = Uuid::uuid4()->toString(); $husband->name = $request->get('set_husband'); $husband->nickname = $request->get('set_husband'); $husband->gender_id = 1; diff --git a/app/User.php b/app/User.php index 362ca85..8255803 100644 --- a/app/User.php +++ b/app/User.php @@ -2,19 +2,28 @@ namespace App; -use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; +use Illuminate\Notifications\Notifiable; +use Ramsey\Uuid\Uuid; class User extends Authenticatable { use Notifiable; /** + * Indicates if the IDs are auto-incrementing. + * + * @var bool + */ + public $incrementing = false; + + /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ + 'id', 'nickname', 'gender_id', 'name', 'email', 'password', 'address', 'phone', @@ -35,6 +44,14 @@ class User extends Authenticatable 'gender', ]; + protected $casts = [ + 'couples.pivot.id' => 'string', + 'wifes.pivot.id' => 'string', + 'husbands.pivot.id' => 'string', + ]; + + // protected $keyType = 'string'; + public function getGenderAttribute() { return $this->gender_id == 1 ? trans('app.male_code') : trans('app.female_code'); @@ -44,8 +61,9 @@ class User extends Authenticatable { if ($father->gender_id == 1) { - if ($father->exists == false) + if ($father->exists == false) { $father->save(); + } $this->father_id = $father->id; $this->save(); @@ -60,8 +78,9 @@ class User extends Authenticatable { if ($mother->gender_id == 2) { - if ($mother->exists == false) + if ($mother->exists == false) { $mother->save(); + } $this->mother_id = $mother->id; $this->save(); @@ -84,8 +103,9 @@ class User extends Authenticatable public function childs() { - if ($this->gender_id == 2) + if ($this->gender_id == 2) { return $this->hasMany(User::class, 'mother_id'); + } return $this->hasMany(User::class, 'father_id'); } @@ -108,13 +128,16 @@ class User extends Authenticatable public function wifes() { - return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->withPivot(['id'])->withTimestamps(); + return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps(); } public function addWife(User $wife, $marriageDate = null) { - if ($this->gender_id == 1 && ! $this->hasBeenMarriedTo($wife)) { - $this->wifes()->save($wife, ['marriage_date' => $marriageDate]); + if ($this->gender_id == 1 && !$this->hasBeenMarriedTo($wife)) { + $this->wifes()->save($wife, [ + 'id' => Uuid::uuid4()->toString(), + 'marriage_date' => $marriageDate, + ]); return $wife; } @@ -123,13 +146,16 @@ class User extends Authenticatable public function husbands() { - return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->withPivot(['id'])->withTimestamps(); + return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps(); } public function addHusband(User $husband, $marriageDate = null) { - if ($this->gender_id == 2 && ! $this->hasBeenMarriedTo($husband)) { - $this->husbands()->save($husband, ['marriage_date' => $marriageDate]); + if ($this->gender_id == 2 && !$this->hasBeenMarriedTo($husband)) { + $this->husbands()->save($husband, [ + 'id' => Uuid::uuid4()->toString(), + 'marriage_date' => $marriageDate, + ]); return $husband; } @@ -143,33 +169,42 @@ class User extends Authenticatable public function couples() { - if ($this->gender_id == 1) - return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->withPivot(['id'])->withTimestamps(); + if ($this->gender_id == 1) { + return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps(); + } - return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->withPivot(['id'])->withTimestamps(); + return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps(); } public function marriages() { - if ($this->gender_id == 1) + if ($this->gender_id == 1) { return $this->hasMany(Couple::class, 'husband_id'); + } return $this->hasMany(Couple::class, 'wife_id'); } public function siblings() { - if (is_null($this->father_id) && is_null($this->mother_id) && is_null($this->parent_id)) + if (is_null($this->father_id) && is_null($this->mother_id) && is_null($this->parent_id)) { return collect([]); + } return User::where('id', '!=', $this->id) ->where(function ($query) { - if (!is_null($this->father_id)) + if (!is_null($this->father_id)) { $query->where('father_id', $this->father_id); - if (!is_null($this->mother_id)) + } + + if (!is_null($this->mother_id)) { $query->orWhere('mother_id', $this->mother_id); - if (!is_null($this->parent_id)) + } + + if (!is_null($this->parent_id)) { $query->orWhere('parent_id', $this->parent_id); + } + }) ->get(); } diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 1ce8110..4e2bf11 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -18,10 +18,11 @@ use App\User; $factory->define(User::class, function (Faker\Generator $faker) { $name = $faker->name; return [ + 'id' => $faker->uuid, 'name' => $name, 'nickname' => $name, 'gender_id' => rand(1, 2), - 'manager_id' => 1, + 'manager_id' => $faker->uuid, ]; }); @@ -34,8 +35,8 @@ $factory->state(User::class, 'female', function (Faker\Generator $faker) { }); $factory->define(Couple::class, function (Faker\Generator $faker) { - $name = $faker->name; return [ + 'id' => $faker->uuid, 'husband_id' => function () { return factory(User::class)->states('male')->create()->id; }, diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index b102aae..a0e407c 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -14,13 +14,13 @@ class CreateUsersTable extends Migration public function up() { Schema::create('users', function (Blueprint $table) { - $table->increments('id'); + $table->uuid('id')->primary(); $table->string('nickname'); $table->string('name')->nullable(); $table->boolean('gender_id')->unsigned(); - $table->unsignedInteger('father_id')->nullable(); - $table->unsignedInteger('mother_id')->nullable(); - $table->unsignedInteger('parent_id')->nullable(); + $table->uuid('father_id')->nullable(); + $table->uuid('mother_id')->nullable(); + $table->uuid('parent_id')->nullable(); $table->date('dob')->nullable(); $table->date('dod')->nullable(); $table->date('yod')->nullable(); @@ -30,7 +30,7 @@ class CreateUsersTable extends Migration $table->string('city')->nullable(); $table->string('phone')->nullable(); $table->string('photo_path')->nullable(); - $table->unsignedInteger('manager_id')->nullable(); + $table->uuid('manager_id')->nullable(); $table->rememberToken(); $table->timestamps(); }); diff --git a/database/migrations/2017_06_27_151536_create_couples_table.php b/database/migrations/2017_06_27_151536_create_couples_table.php index db733f1..5acafcb 100644 --- a/database/migrations/2017_06_27_151536_create_couples_table.php +++ b/database/migrations/2017_06_27_151536_create_couples_table.php @@ -14,12 +14,12 @@ class CreateCouplesTable extends Migration public function up() { Schema::create('couples', function (Blueprint $table) { - $table->increments('id'); - $table->unsignedInteger('husband_id'); - $table->unsignedInteger('wife_id'); + $table->uuid('id')->primary(); + $table->uuid('husband_id'); + $table->uuid('wife_id'); $table->date('marriage_date')->nullable(); $table->date('divorce_date')->nullable(); - $table->unsignedInteger('manager_id')->nullable(); + $table->uuid('manager_id')->nullable(); $table->timestamps(); $table->unique(['husband_id', 'wife_id']); diff --git a/tests/Feature/ManageUserFamiliesTest.php b/tests/Feature/ManageUserFamiliesTest.php index 6d4db9a..506a214 100644 --- a/tests/Feature/ManageUserFamiliesTest.php +++ b/tests/Feature/ManageUserFamiliesTest.php @@ -48,7 +48,7 @@ class ManageUserFamiliesTest extends TestCase ]); $this->seeInDatabase('users', [ - 'nickname' => 'Nama Ibu', + 'nickname' => 'Nama Ibu', 'manager_id' => $user->id, ]); @@ -67,17 +67,17 @@ class ManageUserFamiliesTest extends TestCase $this->seeElement('select', ['name' => 'add_child_parent_id']); $this->submitForm(trans('user.add_child'), [ - 'add_child_name' => 'Nama Anak 1', + 'add_child_name' => 'Nama Anak 1', 'add_child_gender_id' => 1, 'add_child_parent_id' => '', ]); $this->seeInDatabase('users', [ - 'nickname' => 'Nama Anak 1', - 'gender_id' => 1, - 'father_id' => $user->id, - 'mother_id' => null, - 'parent_id' => null, + 'nickname' => 'Nama Anak 1', + 'gender_id' => 1, + 'father_id' => $user->id, + 'mother_id' => null, + 'parent_id' => null, 'manager_id' => $user->id, ]); } @@ -85,12 +85,11 @@ class ManageUserFamiliesTest extends TestCase /** @test */ public function user_can_add_childrens_with_parent_id_if_exist() { - $husband = factory(User::class)->states('male')->create(); - $wife = factory(User::class)->states('female')->create(); + $husband = $this->loginAsUser(['gender_id' => 1]); + $wife = factory(User::class)->states('female')->create(['manager_id' => $husband->id]); $husband->addWife($wife); $marriageId = $husband->fresh()->wifes->first()->pivot->id; - $this->actingAs($husband); $this->visit(route('profile')); $this->seePageIs(route('profile')); @@ -100,16 +99,16 @@ class ManageUserFamiliesTest extends TestCase $this->seeElement('select', ['name' => 'add_child_parent_id']); $this->submitForm(trans('user.add_child'), [ - 'add_child_name' => 'Nama Anak 1', + 'add_child_name' => 'Nama Anak 1', 'add_child_gender_id' => 1, 'add_child_parent_id' => $marriageId, ]); $this->seeInDatabase('users', [ - 'nickname' => 'Nama Anak 1', - 'gender_id' => 1, - 'father_id' => $husband->id, - 'mother_id' => $wife->id, + 'nickname' => 'Nama Anak 1', + 'gender_id' => 1, + 'father_id' => $husband->id, + 'mother_id' => $wife->id, 'manager_id' => $husband->id, ]); } @@ -124,20 +123,23 @@ class ManageUserFamiliesTest extends TestCase $this->seeElement('input', ['name' => 'set_wife']); $this->submitForm('set_wife_button', [ - 'set_wife' => 'Nama Istri', + 'set_wife' => 'Nama Istri', 'marriage_date' => '2010-01-01', ]); $this->seeInDatabase('users', [ - 'nickname' => 'Nama Istri', + 'nickname' => 'Nama Istri', 'gender_id' => 2, ]); - $wife = User::orderBy('id', 'desc')->first(); + $wife = User::where([ + 'nickname' => 'Nama Istri', + 'gender_id' => 2, + ])->first(); $this->seeInDatabase('couples', [ - 'husband_id' => $user->id, - 'wife_id' => $wife->id, + 'husband_id' => $user->id, + 'wife_id' => $wife->id, 'marriage_date' => '2010-01-01', ]); } @@ -152,21 +154,24 @@ class ManageUserFamiliesTest extends TestCase $this->seeElement('input', ['name' => 'set_husband']); $this->submitForm('set_husband_button', [ - 'set_husband' => 'Nama Suami', + 'set_husband' => 'Nama Suami', 'marriage_date' => '2010-03-03', ]); $this->seeInDatabase('users', [ - 'nickname' => 'Nama Suami', - 'gender_id' => 1, + 'nickname' => 'Nama Suami', + 'gender_id' => 1, 'manager_id' => $user->id, ]); - $husband = User::orderBy('id', 'desc')->first(); + $husband = User::where([ + 'nickname' => 'Nama Suami', + 'gender_id' => 1, + ])->first(); $this->seeInDatabase('couples', [ - 'husband_id' => $husband->id, - 'wife_id' => $user->id, + 'husband_id' => $husband->id, + 'wife_id' => $user->id, 'marriage_date' => '2010-03-03', ]); } @@ -186,7 +191,7 @@ class ManageUserFamiliesTest extends TestCase $this->seeElement('select', ['name' => 'set_father_id']); $this->submitForm('set_father_button', [ - 'set_father' => '', + 'set_father' => '', 'set_father_id' => $father->id, ]); @@ -208,7 +213,7 @@ class ManageUserFamiliesTest extends TestCase $this->seeElement('select', ['name' => 'set_mother_id']); $this->submitForm('set_mother_button', [ - 'set_mother' => '', + 'set_mother' => '', 'set_mother_id' => $mother->id, ]); @@ -228,14 +233,14 @@ class ManageUserFamiliesTest extends TestCase $this->seeElement('select', ['name' => 'set_wife_id']); $this->submitForm('set_wife_button', [ - 'set_wife' => '', - 'set_wife_id' => $wife->id, + 'set_wife' => '', + 'set_wife_id' => $wife->id, 'marriage_date' => '2010-01-01', ]); $this->seeInDatabase('couples', [ - 'husband_id' => $user->id, - 'wife_id' => $wife->id, + 'husband_id' => $user->id, + 'wife_id' => $wife->id, 'marriage_date' => '2010-01-01', ]); } @@ -253,14 +258,14 @@ class ManageUserFamiliesTest extends TestCase $this->seeElement('select', ['name' => 'set_husband_id']); $this->submitForm('set_husband_button', [ - 'set_husband' => '', + 'set_husband' => '', 'set_husband_id' => $husband->id, - 'marriage_date' => '2010-03-03', + 'marriage_date' => '2010-03-03', ]); $this->seeInDatabase('couples', [ - 'husband_id' => $husband->id, - 'wife_id' => $user->id, + 'husband_id' => $husband->id, + 'wife_id' => $user->id, 'marriage_date' => '2010-03-03', ]); } @@ -284,8 +289,8 @@ class ManageUserFamiliesTest extends TestCase ]); $this->seeInDatabase('users', [ - 'id' => $user->id, - 'parent_id' => $marriageId, + 'id' => $user->id, + 'parent_id' => $marriageId, 'manager_id' => $user->id, ]); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 15a2c3a..4af3bfe 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -14,6 +14,12 @@ abstract class TestCase extends BaseTestCase protected function loginAsUser($overrides = []) { + $userId = \Ramsey\Uuid\Uuid::uuid4()->toString(); + $overrides = array_merge([ + 'id' => $userId, + 'manager_id' => $userId, + ], $overrides); + $user = factory(User::class)->create($overrides); $this->actingAs($user); diff --git a/tests/Unit/Policies/UserPolicyTest.php b/tests/Unit/Policies/UserPolicyTest.php index 8a92d5e..8a1c3f8 100644 --- a/tests/Unit/Policies/UserPolicyTest.php +++ b/tests/Unit/Policies/UserPolicyTest.php @@ -13,8 +13,8 @@ class UserPolicyTest extends TestCase /** @test */ public function admin_can_edit_users_profile() { - $user = factory(User::class)->create(); - $manager = $user->manager; + $manager = factory(User::class)->create(); + $user = factory(User::class)->create(['manager_id' => $manager->id]); $this->assertTrue($manager->can('edit', $user)); } diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index 5585b97..bd441d4 100644 --- a/tests/Unit/UserTest.php +++ b/tests/Unit/UserTest.php @@ -76,7 +76,9 @@ class UserTest extends TestCase /** @test */ public function a_user_have_a_manager() { - $user = factory(User::class)->create(); + $manager = factory(User::class)->create(); + $user = factory(User::class)->create(['manager_id' => $manager->id]); + $this->assertTrue($user->manager instanceof User); } }