From a8ddb2f44a07b42d28e1db72ba1156d6efc9d1c9 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 11:29:30 +0100 Subject: [PATCH] Added non-circular tests Signed-off-by: snipe --- .../Locations/Api/CreateLocationsTest.php | 20 +++++++ .../Locations/Ui/CreateLocationsTest.php | 37 ++++++++++++ .../Locations/Ui/UpdateLocationsTest.php | 57 +++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 tests/Feature/Locations/Ui/CreateLocationsTest.php create mode 100644 tests/Feature/Locations/Ui/UpdateLocationsTest.php diff --git a/tests/Feature/Locations/Api/CreateLocationsTest.php b/tests/Feature/Locations/Api/CreateLocationsTest.php index 600ae7c61f..171508b725 100644 --- a/tests/Feature/Locations/Api/CreateLocationsTest.php +++ b/tests/Feature/Locations/Api/CreateLocationsTest.php @@ -33,4 +33,24 @@ class CreateLocationsTest extends TestCase } + public function testUserCannotCreateLocationsThatAreTheirOwnParent() + { + $location = Location::factory()->create(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->patchJson(route('api.locations.update', $location), [ + 'parent_id' => $location->id, + ]) + ->assertOk() + ->assertStatusMessageIs('error') + ->assertJson([ + 'messages' => [ + 'parent_id' => ['The parent id must not create a circular reference.'], + ], + ]) + ->json(); + + + } + } diff --git a/tests/Feature/Locations/Ui/CreateLocationsTest.php b/tests/Feature/Locations/Ui/CreateLocationsTest.php new file mode 100644 index 0000000000..5e229f1043 --- /dev/null +++ b/tests/Feature/Locations/Ui/CreateLocationsTest.php @@ -0,0 +1,37 @@ +actingAs(User::factory()->create()) + ->post(route('locations.store'), [ + 'name' => 'Test Location', + 'company_id' => Company::factory()->create()->id + ]) + ->assertForbidden(); + } + + public function testUserCanCreateLocations() + { + $this->assertFalse(Location::where('name', 'Test Location')->exists()); + + $this->actingAs(User::factory()->superuser()->create()) + ->post(route('locations.store'), [ + 'name' => 'Test Location', + 'company_id' => Company::factory()->create()->id + ]) + ->assertRedirect(route('locations.index')); + + $this->assertTrue(Location::where('name', 'Test Location')->exists()); + } + + +} diff --git a/tests/Feature/Locations/Ui/UpdateLocationsTest.php b/tests/Feature/Locations/Ui/UpdateLocationsTest.php new file mode 100644 index 0000000000..5359cd1b71 --- /dev/null +++ b/tests/Feature/Locations/Ui/UpdateLocationsTest.php @@ -0,0 +1,57 @@ +actingAs(User::factory()->create()) + ->post(route('locations.store'), [ + 'name' => 'Test Location', + ]) + ->assertStatus(403) + ->assertForbidden(); + } + + + public function testUserCanEditLocations() + { + $location = Location::factory()->create(['name' => 'Test Location']); + $this->assertTrue(Location::where('name', 'Test Location')->exists()); + + $response = $this->actingAs(User::factory()->superuser()->create()) + ->put(route('locations.update', ['location' => $location]), [ + 'name' => 'Test Location Edited', + ]) + ->assertStatus(302) + ->assertSessionHasNoErrors() + ->assertRedirect(route('locations.index')); + + $this->followRedirects($response)->assertSee('Success'); + $this->assertTrue(Location::where('name', 'Test Location Edited')->exists()); + } + + public function testUserCannotEditLocationsToMakeThemTheirOwnParent() + { + $location = Location::factory()->create(); + + $response = $this->actingAs(User::factory()->superuser()->create()) + ->from(route('locations.edit', ['location' => $location->id])) + ->put(route('locations.update', ['location' => $location]), [ + 'name' => 'Test Location', + 'parent_id' => $location->id, + ]) + ->assertRedirect(route('locations.edit', ['location' => $location])); + + $this->followRedirects($response)->assertSee(trans('general.error')); + $this->assertFalse(Location::where('name', 'Test Location')->exists()); + } + + + +}