From de1431bd47b00f15ee4c857ffef911fbbd2b7346 Mon Sep 17 00:00:00 2001 From: Michal Date: Wed, 28 Feb 2024 22:04:59 +0000 Subject: [PATCH] fix: parsing map details when none present --- app/Parsers/Files/GPXParser.php | 20 +++++++++++++++-- tests/Unit/MarkerTest.php | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/app/Parsers/Files/GPXParser.php b/app/Parsers/Files/GPXParser.php index c5fbf68..7b5c526 100644 --- a/app/Parsers/Files/GPXParser.php +++ b/app/Parsers/Files/GPXParser.php @@ -129,9 +129,25 @@ public function parseMapDetailsFromFile(string $filepath): array $mapDetails = $array['metadata']; } + $mapTitle = null; + $mapDescription = null; + + // Depending what is isset, we will return the name and desc of the map + if (isset($mapDetails['name'])) { + $mapTitle = $mapDetails['name']; + } elseif (isset($array['name'])) { + $mapTitle = (string) $array['name']; + } + + if (isset($mapDetails['desc'])) { + $mapDescription = $mapDetails['desc']; + } elseif (isset($array['desc'])) { + $mapDescription = (string) $array['desc']; + } + return [ - 'title' => $mapDetails['name'] ?? (string) $array['name'] ?? null, - 'description' => $mapDetails['desc'] ?? (string) $array['desc'] ?? null, + 'title' => $mapTitle, + 'description' => $mapDescription, ]; } diff --git a/tests/Unit/MarkerTest.php b/tests/Unit/MarkerTest.php index e740452..6cba388 100644 --- a/tests/Unit/MarkerTest.php +++ b/tests/Unit/MarkerTest.php @@ -499,6 +499,45 @@ public function testCreateMarkerInBulkWithGpxFile() $this->assertEquals(59, $map->markers()->orderBy('id', 'desc')->first()->locations()->count()); } + /** + * Test creating markers in bulk by uploading a GPX file + * + * @return void + */ + public function testCreateMarkerInBulkWithSecondGpxFile() + { + $this->withoutExceptionHandling(); + + // Skip any dispatched jobs + $this->expectsJobs([ + FillMissingMarkerElevation::class, + FillMissingLocationGeocodes::class, + ]); + + // We need to clean up the database before we start + DB::table('markers')->delete(); + DB::table('marker_locations')->delete(); + + $map = new \App\Models\Map(); + $map->users_can_create_markers = 'yes'; + $map->options = ['links' => 'optional']; + $map->save(); + + $user = User::factory()->create(); + + /** + * @var \Illuminate\Contracts\Auth\Authenticatable + */ + $user = $user->givePermissionTo('create markers in bulk'); + + $this->actingAs($user, 'api'); + + $file = new UploadedFile(base_path('tests/fixtures/fells_loop.gpx'), 'fells_loop.gpx', 'application/gpx+xml', null, true); + + $response = $this->postJson('/api/maps/' . $map->uuid . '/markers/file', ['file' => $file]); + $response->assertStatus(200); + } + public function testCreateMarkerInBulkWithGeoJSONFile() { $this->withoutExceptionHandling();