Skip to content

Commit 050454f

Browse files
Copilottacruc
andcommitted
Fix favorite category linking bug: use comparison instead of assignment
Co-authored-by: tacruc <402891+tacruc@users.noreply.github.com>
1 parent 8c1139e commit 050454f

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

lib/Controller/FavoritesController.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,12 +412,18 @@ public function addShareCategoryToMap(string $category, int $targetMapId, ?int $
412412
}
413413
$data = json_decode($file->getContent(), true);
414414
foreach ($data as $s) {
415-
if ($s->token = $share->token) {
415+
if ($s['token'] === $share->getToken()) {
416416
return new DataResponse($this->l->t('Share was already on map'));
417417
}
418418
}
419-
$share->id = count($data);
420-
$data[] = $share;
419+
$shareData = [
420+
'id' => count($data),
421+
'token' => $share->getToken(),
422+
'category' => $share->getCategory(),
423+
'owner' => $share->getOwner(),
424+
'allowEdits' => $share->getAllowEdits()
425+
];
426+
$data[] = $shareData;
421427
$file->putContent(json_encode($data, JSON_PRETTY_PRINT));
422428
return new DataResponse('Done');
423429
}

tests/Unit/Controller/FavoritesControllerTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,4 +601,67 @@ public function testFavoriteShareIsRenamedCorrectly() {
601601
$this->assertContains($newCategoryName, $shareNames);
602602
$this->assertNotContains($categoryName, $shareNames);
603603
}
604+
605+
public function testAddMultipleSharedCategoriesToMap() {
606+
// Create a custom map folder for testing
607+
$this->mapFolder = $this->createMapFolder();
608+
$targetMapId = $this->mapFolder->getId();
609+
610+
// Create two different categories with favorites
611+
$categoryName1 = 'testcat1_' . uniqid();
612+
$categoryName2 = 'testcat2_' . uniqid();
613+
614+
// Add favorites to both categories
615+
$id1 = $this->favoritesController
616+
->addFavorite('Test Favorite 1', 10.1, 20.1, $categoryName1, 'Comment 1', null)
617+
->getData()['id'];
618+
619+
$id2 = $this->favoritesController
620+
->addFavorite('Test Favorite 2', 10.2, 20.2, $categoryName2, 'Comment 2', null)
621+
->getData()['id'];
622+
623+
// Share both categories
624+
$shareResponse1 = $this->favoritesController->shareCategory($categoryName1);
625+
$shareResponse2 = $this->favoritesController->shareCategory($categoryName2);
626+
627+
$this->assertEquals(200, $shareResponse1->getStatus());
628+
$this->assertEquals(200, $shareResponse2->getStatus());
629+
630+
// Try to add both shared categories to the same custom map
631+
// This should work now that we fixed the bug
632+
$addResponse1 = $this->favoritesController->addShareCategoryToMap($categoryName1, $targetMapId);
633+
$this->assertEquals(200, $addResponse1->getStatus());
634+
$this->assertEquals('Done', $addResponse1->getData());
635+
636+
// The second addition should NOT fail (this was the bug)
637+
$addResponse2 = $this->favoritesController->addShareCategoryToMap($categoryName2, $targetMapId);
638+
$this->assertEquals(200, $addResponse2->getStatus());
639+
$this->assertEquals('Done', $addResponse2->getData());
640+
641+
// Verify that the .favorite_shares.json file contains both categories
642+
$sharesFile = $this->mapFolder->get('.favorite_shares.json');
643+
$sharesData = json_decode($sharesFile->getContent(), true);
644+
$this->assertEquals(2, count($sharesData));
645+
646+
// Verify the tokens are different and correct categories are present
647+
$categoryTokens = [];
648+
foreach ($sharesData as $share) {
649+
$categoryTokens[$share['category']] = $share['token'];
650+
}
651+
652+
$this->assertArrayHasKey($categoryName1, $categoryTokens);
653+
$this->assertArrayHasKey($categoryName2, $categoryTokens);
654+
$this->assertNotEquals($categoryTokens[$categoryName1], $categoryTokens[$categoryName2]);
655+
656+
// Test adding the same category again should return "Share was already on map"
657+
$addResponse3 = $this->favoritesController->addShareCategoryToMap($categoryName1, $targetMapId);
658+
$this->assertEquals(200, $addResponse3->getStatus());
659+
$this->assertEquals('Share was already on map', $addResponse3->getData());
660+
661+
// Clean up
662+
$this->favoritesController->deleteFavorite($id1);
663+
$this->favoritesController->deleteFavorite($id2);
664+
$this->favoritesController->unShareCategory($categoryName1);
665+
$this->favoritesController->unShareCategory($categoryName2);
666+
}
604667
}

0 commit comments

Comments
 (0)