From 8c1139eb31b05f49c84d6f8b7db2d5926c339f77 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 8 Jul 2025 19:56:35 +0000 Subject: [PATCH 1/5] Initial plan From 050454f615656a6ace8d55f2b44325b772dbe0c3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 8 Jul 2025 20:11:17 +0000 Subject: [PATCH 2/5] Fix favorite category linking bug: use comparison instead of assignment Co-authored-by: tacruc <402891+tacruc@users.noreply.github.com> --- lib/Controller/FavoritesController.php | 12 +++- .../Controller/FavoritesControllerTest.php | 63 +++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/lib/Controller/FavoritesController.php b/lib/Controller/FavoritesController.php index a9ebff87b..c68289068 100644 --- a/lib/Controller/FavoritesController.php +++ b/lib/Controller/FavoritesController.php @@ -412,12 +412,18 @@ public function addShareCategoryToMap(string $category, int $targetMapId, ?int $ } $data = json_decode($file->getContent(), true); foreach ($data as $s) { - if ($s->token = $share->token) { + if ($s['token'] === $share->getToken()) { return new DataResponse($this->l->t('Share was already on map')); } } - $share->id = count($data); - $data[] = $share; + $shareData = [ + 'id' => count($data), + 'token' => $share->getToken(), + 'category' => $share->getCategory(), + 'owner' => $share->getOwner(), + 'allowEdits' => $share->getAllowEdits() + ]; + $data[] = $shareData; $file->putContent(json_encode($data, JSON_PRETTY_PRINT)); return new DataResponse('Done'); } diff --git a/tests/Unit/Controller/FavoritesControllerTest.php b/tests/Unit/Controller/FavoritesControllerTest.php index 26daca5af..1dcd0b15a 100644 --- a/tests/Unit/Controller/FavoritesControllerTest.php +++ b/tests/Unit/Controller/FavoritesControllerTest.php @@ -601,4 +601,67 @@ public function testFavoriteShareIsRenamedCorrectly() { $this->assertContains($newCategoryName, $shareNames); $this->assertNotContains($categoryName, $shareNames); } + + public function testAddMultipleSharedCategoriesToMap() { + // Create a custom map folder for testing + $this->mapFolder = $this->createMapFolder(); + $targetMapId = $this->mapFolder->getId(); + + // Create two different categories with favorites + $categoryName1 = 'testcat1_' . uniqid(); + $categoryName2 = 'testcat2_' . uniqid(); + + // Add favorites to both categories + $id1 = $this->favoritesController + ->addFavorite('Test Favorite 1', 10.1, 20.1, $categoryName1, 'Comment 1', null) + ->getData()['id']; + + $id2 = $this->favoritesController + ->addFavorite('Test Favorite 2', 10.2, 20.2, $categoryName2, 'Comment 2', null) + ->getData()['id']; + + // Share both categories + $shareResponse1 = $this->favoritesController->shareCategory($categoryName1); + $shareResponse2 = $this->favoritesController->shareCategory($categoryName2); + + $this->assertEquals(200, $shareResponse1->getStatus()); + $this->assertEquals(200, $shareResponse2->getStatus()); + + // Try to add both shared categories to the same custom map + // This should work now that we fixed the bug + $addResponse1 = $this->favoritesController->addShareCategoryToMap($categoryName1, $targetMapId); + $this->assertEquals(200, $addResponse1->getStatus()); + $this->assertEquals('Done', $addResponse1->getData()); + + // The second addition should NOT fail (this was the bug) + $addResponse2 = $this->favoritesController->addShareCategoryToMap($categoryName2, $targetMapId); + $this->assertEquals(200, $addResponse2->getStatus()); + $this->assertEquals('Done', $addResponse2->getData()); + + // Verify that the .favorite_shares.json file contains both categories + $sharesFile = $this->mapFolder->get('.favorite_shares.json'); + $sharesData = json_decode($sharesFile->getContent(), true); + $this->assertEquals(2, count($sharesData)); + + // Verify the tokens are different and correct categories are present + $categoryTokens = []; + foreach ($sharesData as $share) { + $categoryTokens[$share['category']] = $share['token']; + } + + $this->assertArrayHasKey($categoryName1, $categoryTokens); + $this->assertArrayHasKey($categoryName2, $categoryTokens); + $this->assertNotEquals($categoryTokens[$categoryName1], $categoryTokens[$categoryName2]); + + // Test adding the same category again should return "Share was already on map" + $addResponse3 = $this->favoritesController->addShareCategoryToMap($categoryName1, $targetMapId); + $this->assertEquals(200, $addResponse3->getStatus()); + $this->assertEquals('Share was already on map', $addResponse3->getData()); + + // Clean up + $this->favoritesController->deleteFavorite($id1); + $this->favoritesController->deleteFavorite($id2); + $this->favoritesController->unShareCategory($categoryName1); + $this->favoritesController->unShareCategory($categoryName2); + } } From 2de5313ba9592445258fcf556f993b48a0f210cf Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 10 Jul 2025 12:33:31 +0000 Subject: [PATCH 3/5] Simplify serialization approach: use Entity instead of manual array Co-authored-by: tacruc <402891+tacruc@users.noreply.github.com> --- lib/Controller/FavoritesController.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/Controller/FavoritesController.php b/lib/Controller/FavoritesController.php index c68289068..2326ee04e 100644 --- a/lib/Controller/FavoritesController.php +++ b/lib/Controller/FavoritesController.php @@ -416,14 +416,8 @@ public function addShareCategoryToMap(string $category, int $targetMapId, ?int $ return new DataResponse($this->l->t('Share was already on map')); } } - $shareData = [ - 'id' => count($data), - 'token' => $share->getToken(), - 'category' => $share->getCategory(), - 'owner' => $share->getOwner(), - 'allowEdits' => $share->getAllowEdits() - ]; - $data[] = $shareData; + $share->id = count($data); + $data[] = $share; $file->putContent(json_encode($data, JSON_PRETTY_PRINT)); return new DataResponse('Done'); } From bee63f4e5c0c73e5a4194f89fbc51662e59c37cb Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 10 Jul 2025 14:49:38 +0000 Subject: [PATCH 4/5] Fix Entity serialization for addShareCategoryToMap Co-authored-by: tacruc <402891+tacruc@users.noreply.github.com> --- lib/Controller/FavoritesController.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/Controller/FavoritesController.php b/lib/Controller/FavoritesController.php index 2326ee04e..ca8a325cd 100644 --- a/lib/Controller/FavoritesController.php +++ b/lib/Controller/FavoritesController.php @@ -416,8 +416,12 @@ public function addShareCategoryToMap(string $category, int $targetMapId, ?int $ return new DataResponse($this->l->t('Share was already on map')); } } - $share->id = count($data); - $data[] = $share; + $shareData = [ + 'id' => count($data), + 'token' => $share->getToken(), + 'category' => $share->getCategory() + ]; + $data[] = $shareData; $file->putContent(json_encode($data, JSON_PRETTY_PRINT)); return new DataResponse('Done'); } From 0515cfce8372f4c41553bb0f541139208bc31b33 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 10 Jul 2025 20:18:13 +0000 Subject: [PATCH 5/5] Fix comparison bug with minimal changes only Co-authored-by: tacruc <402891+tacruc@users.noreply.github.com> --- lib/Controller/FavoritesController.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/Controller/FavoritesController.php b/lib/Controller/FavoritesController.php index ca8a325cd..2326ee04e 100644 --- a/lib/Controller/FavoritesController.php +++ b/lib/Controller/FavoritesController.php @@ -416,12 +416,8 @@ public function addShareCategoryToMap(string $category, int $targetMapId, ?int $ return new DataResponse($this->l->t('Share was already on map')); } } - $shareData = [ - 'id' => count($data), - 'token' => $share->getToken(), - 'category' => $share->getCategory() - ]; - $data[] = $shareData; + $share->id = count($data); + $data[] = $share; $file->putContent(json_encode($data, JSON_PRETTY_PRINT)); return new DataResponse('Done'); }