Skip to content

Commit d0acf5b

Browse files
authored
Merge pull request #15356 from snipe/validate_location_parent
Fixed #15341 - validate parent ID
2 parents 09033b1 + 74fbc23 commit d0acf5b

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

app/Models/Location.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Location extends SnipeModel
3333
'country' => 'min:2|max:191|nullable',
3434
'zip' => 'max:10|nullable',
3535
'manager_id' => 'exists:users,id|nullable',
36-
'parent_id' => 'non_circular:locations,id',
36+
'parent_id' => 'nullable|exists:locations,id|non_circular:locations,id',
3737
];
3838

3939
protected $casts = [

tests/Feature/Locations/Ui/CreateLocationsTest.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ public function testUserCanCreateLocations()
3232

3333
$this->assertTrue(Location::where('name', 'Test Location')->exists());
3434
}
35-
35+
36+
public function testUserCannotCreateLocationsWithInvalidParent()
37+
{
38+
$this->assertFalse(Location::where('name', 'Test Location')->exists());
39+
40+
$this->actingAs(User::factory()->superuser()->create())
41+
->from(route('locations.create'))
42+
->post(route('locations.store'), [
43+
'name' => 'Test Location',
44+
'parent_id' => '100000000'
45+
])
46+
->assertRedirect(route('locations.create'));
47+
48+
$this->assertFalse(Location::where('name', 'Test Location')->exists());
49+
}
3650

3751
}

tests/Feature/Locations/Ui/UpdateLocationsTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ public function testUserCannotEditLocationsToMakeThemTheirOwnParent()
5252
$this->assertFalse(Location::where('name', 'Test Location')->exists());
5353
}
5454

55+
public function testUserCannotEditLocationsWithInvalidParent()
56+
{
57+
$location = Location::factory()->create();
58+
$response = $this->actingAs(User::factory()->superuser()->create())
59+
->from(route('locations.edit', ['location' => $location->id]))
60+
->put(route('locations.update', ['location' => $location]), [
61+
'name' => 'Test Location',
62+
'parent_id' => '100000000'
63+
])
64+
->assertRedirect(route('locations.edit', ['location' => $location->id]));
65+
66+
$this->followRedirects($response)->assertSee(trans('general.error'));
67+
$this->assertFalse(Location::where('name', 'Test Location')->exists());
68+
}
69+
5570

5671

5772
}

0 commit comments

Comments
 (0)