diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index 8625f70..043193b 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -198,6 +198,18 @@ class UsersController extends Controller 'manager_id' => $attributes['replacement_user_id'], ]); + DB::table('couples')->where('husband_id', $oldUserId)->update([ + 'husband_id' => $attributes['replacement_user_id'], + ]); + + DB::table('couples')->where('wife_id', $oldUserId)->update([ + 'wife_id' => $attributes['replacement_user_id'], + ]); + + DB::table('couples')->where('manager_id', $oldUserId)->update([ + 'manager_id' => $attributes['replacement_user_id'], + ]); + $user->delete(); DB::commit(); diff --git a/tests/Feature/UsersDeletionTest.php b/tests/Feature/UsersDeletionTest.php index 87c49dc..6c4f2e9 100644 --- a/tests/Feature/UsersDeletionTest.php +++ b/tests/Feature/UsersDeletionTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature; use App\User; +use App\Couple; use Tests\TestCase; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -109,7 +110,7 @@ class UsersDeletionTest extends TestCase } /** @test */ - public function manager_can_delete_a_user_the_replace_childs_manager_id() + public function manager_can_delete_a_user_the_replace_users_manager_id() { $manager = $this->loginAsUser(); $oldUser = factory(User::class)->create([ @@ -146,4 +147,113 @@ class UsersDeletionTest extends TestCase 'manager_id' => $replacementUser->id, ]); } + + /** @test */ + public function manager_can_delete_a_user_the_replace_couples_husband_id() + { + $manager = $this->loginAsUser(); + $oldUser = factory(User::class)->create([ + 'gender_id' => 1, + 'manager_id' => $manager->id, + ]); + $oldUserCouple = factory(Couple::class)->create([ + 'husband_id' => $oldUser->id, + ]); + $replacementUser = factory(User::class)->create([ + 'gender_id' => 1, + 'manager_id' => $manager->id, + ]); + + $this->visit(route('users.edit', [$oldUser, 'action' => 'delete'])); + $this->see(__('user.replace_delete_text')); + + $this->submitForm(__('user.replace_delete_button'), [ + 'replacement_user_id' => $replacementUser->id, + ]); + + $this->dontSeeInDatabase('users', [ + 'id' => $oldUser->id, + ]); + + $this->dontSeeInDatabase('couples', [ + 'husband_id' => $oldUser->id, + ]); + + $this->seeInDatabase('couples', [ + 'id' => $oldUserCouple->id, + 'husband_id' => $replacementUser->id, + ]); + } + + /** @test */ + public function manager_can_delete_a_user_the_replace_couples_wife_id() + { + $manager = $this->loginAsUser(); + $oldUser = factory(User::class)->create([ + 'gender_id' => 2, + 'manager_id' => $manager->id, + ]); + $oldUserCouple = factory(Couple::class)->create([ + 'wife_id' => $oldUser->id, + ]); + $replacementUser = factory(User::class)->create([ + 'gender_id' => 2, + 'manager_id' => $manager->id, + ]); + + $this->visit(route('users.edit', [$oldUser, 'action' => 'delete'])); + $this->see(__('user.replace_delete_text')); + + $this->submitForm(__('user.replace_delete_button'), [ + 'replacement_user_id' => $replacementUser->id, + ]); + + $this->dontSeeInDatabase('users', [ + 'id' => $oldUser->id, + ]); + + $this->dontSeeInDatabase('couples', [ + 'wife_id' => $oldUser->id, + ]); + + $this->seeInDatabase('couples', [ + 'id' => $oldUserCouple->id, + 'wife_id' => $replacementUser->id, + ]); + } + + /** @test */ + public function manager_can_delete_a_user_the_replace_couples_manager_id() + { + $manager = $this->loginAsUser(); + $oldUser = factory(User::class)->create([ + 'gender_id' => 1, + 'manager_id' => $manager->id, + ]); + $oldCoupleManagedCouple = factory(Couple::class)->create(['manager_id' => $oldUser->id]); + $replacementUser = factory(User::class)->create([ + 'gender_id' => 1, + 'manager_id' => $manager->id, + ]); + + $this->visit(route('users.edit', [$oldUser, 'action' => 'delete'])); + $this->see(__('user.replace_delete_text')); + + $this->submitForm(__('user.replace_delete_button'), [ + 'replacement_user_id' => $replacementUser->id, + ]); + + $this->dontSeeInDatabase('users', [ + 'id' => $oldUser->id, + ]); + + $this->dontSeeInDatabase('couples', [ + 'manager_id' => $oldUser->id, + ]); + + $this->seeInDatabase('couples', [ + 'id' => $oldCoupleManagedCouple->id, + 'manager_id' => $replacementUser->id, + ]); + } }