|
25 | 25 | namespace Tests\Feature\Services; |
26 | 26 |
|
27 | 27 | use Illuminate\Validation\ValidationException; |
| 28 | +use Pterodactyl\Exceptions\DisplayException; |
| 29 | +use Pterodactyl\Models\Node; |
28 | 30 | use Tests\TestCase; |
29 | 31 | use Pterodactyl\Models\Location; |
30 | 32 | use Pterodactyl\Services\LocationService; |
@@ -201,4 +203,46 @@ public function testShouldThrowExceptionIfInvalidModelIdIsProvided() |
201 | 203 | { |
202 | 204 | $this->service->update(0, []); |
203 | 205 | } |
| 206 | + |
| 207 | + /** |
| 208 | + * Test that a location can be deleted normally when no nodes are attached. |
| 209 | + */ |
| 210 | + public function testShouldDeleteExistingLocation() |
| 211 | + { |
| 212 | + $location = factory(Location::class)->create(); |
| 213 | + |
| 214 | + $this->assertDatabaseHas('locations', [ |
| 215 | + 'id' => $location->id, |
| 216 | + ]); |
| 217 | + |
| 218 | + $model = $this->service->delete($location); |
| 219 | + |
| 220 | + $this->assertTrue($model); |
| 221 | + $this->assertDatabaseMissing('locations', [ |
| 222 | + 'id' => $location->id, |
| 223 | + ]); |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Test that a location cannot be deleted if a node is attached to it. |
| 228 | + * |
| 229 | + * @expectedException \Pterodactyl\Exceptions\DisplayException |
| 230 | + */ |
| 231 | + public function testShouldFailToDeleteExistingLocationWithAttachedNodes() |
| 232 | + { |
| 233 | + $location = factory(Location::class)->create(); |
| 234 | + $node = factory(Node::class)->create(['location_id' => $location->id]); |
| 235 | + |
| 236 | + $this->assertDatabaseHas('locations', ['id' => $location->id]); |
| 237 | + $this->assertDatabaseHas('nodes', ['id' => $node->id]); |
| 238 | + |
| 239 | + try { |
| 240 | + $this->service->delete($location->id); |
| 241 | + } catch (\Exception $ex) { |
| 242 | + $this->assertInstanceOf(DisplayException::class, $ex); |
| 243 | + $this->assertNotEmpty($ex->getMessage()); |
| 244 | + |
| 245 | + throw $ex; |
| 246 | + } |
| 247 | + } |
204 | 248 | } |
0 commit comments