diff --git a/module/Core/src/ShortUrl/Entity/ShortUrl.php b/module/Core/src/ShortUrl/Entity/ShortUrl.php index 9b266b4a..d0e9cba4 100644 --- a/module/Core/src/ShortUrl/Entity/ShortUrl.php +++ b/module/Core/src/ShortUrl/Entity/ShortUrl.php @@ -174,12 +174,13 @@ class ShortUrl extends AbstractEntity $this->deviceLongUrls->remove($deviceType->value); } foreach ($shortUrlEdit->deviceLongUrls as $deviceLongUrlPair) { - $deviceLongUrl = $this->deviceLongUrls->get($deviceLongUrlPair->deviceType->value); + $key = $deviceLongUrlPair->deviceType->value; + $deviceLongUrl = $this->deviceLongUrls->get($key); if ($deviceLongUrl !== null) { $deviceLongUrl->updateLongUrl($deviceLongUrlPair->longUrl); } else { - $this->deviceLongUrls->add(DeviceLongUrl::fromShortUrlAndPair($this, $deviceLongUrlPair)); + $this->deviceLongUrls->set($key, DeviceLongUrl::fromShortUrlAndPair($this, $deviceLongUrlPair)); } } } diff --git a/module/Core/test/ShortUrl/Entity/ShortUrlTest.php b/module/Core/test/ShortUrl/Entity/ShortUrlTest.php index d83ff3d7..2d950d5f 100644 --- a/module/Core/test/ShortUrl/Entity/ShortUrlTest.php +++ b/module/Core/test/ShortUrl/Entity/ShortUrlTest.php @@ -7,8 +7,10 @@ namespace ShlinkioTest\Shlink\Core\ShortUrl\Entity; use Cake\Chronos\Chronos; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\Core\Exception\ShortCodeCannotBeRegeneratedException; +use Shlinkio\Shlink\Core\Model\DeviceType; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation; +use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlEdition; use Shlinkio\Shlink\Core\ShortUrl\Model\Validation\ShortUrlInputFilter; use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl; use Shlinkio\Shlink\Importer\Sources\ImportSource; @@ -89,4 +91,46 @@ class ShortUrlTest extends TestCase yield [null, DEFAULT_SHORT_CODES_LENGTH]; yield from map(range(4, 10), fn (int $value) => [$value, $value]); } + + /** @test */ + public function deviceLongUrlsAreUpdated(): void + { + $shortUrl = ShortUrl::withLongUrl('foo'); + + $shortUrl->update(ShortUrlEdition::fromRawData([ + ShortUrlInputFilter::DEVICE_LONG_URLS => [ + DeviceType::ANDROID->value => 'android', + DeviceType::IOS->value => 'ios', + ], + ])); + self::assertEquals([ + DeviceType::ANDROID->value => 'android', + DeviceType::IOS->value => 'ios', + DeviceType::DESKTOP->value => null, + ], $shortUrl->deviceLongUrls()); + + $shortUrl->update(ShortUrlEdition::fromRawData([ + ShortUrlInputFilter::DEVICE_LONG_URLS => [ + DeviceType::ANDROID->value => null, + DeviceType::DESKTOP->value => 'desktop', + ], + ])); + self::assertEquals([ + DeviceType::ANDROID->value => null, + DeviceType::IOS->value => 'ios', + DeviceType::DESKTOP->value => 'desktop', + ], $shortUrl->deviceLongUrls()); + + $shortUrl->update(ShortUrlEdition::fromRawData([ + ShortUrlInputFilter::DEVICE_LONG_URLS => [ + DeviceType::ANDROID->value => null, + DeviceType::IOS->value => null, + ], + ])); + self::assertEquals([ + DeviceType::ANDROID->value => null, + DeviceType::IOS->value => null, + DeviceType::DESKTOP->value => 'desktop', + ], $shortUrl->deviceLongUrls()); + } } diff --git a/module/Rest/test-api/Action/CreateShortUrlTest.php b/module/Rest/test-api/Action/CreateShortUrlTest.php index 4300c852..0bb02c9e 100644 --- a/module/Rest/test-api/Action/CreateShortUrlTest.php +++ b/module/Rest/test-api/Action/CreateShortUrlTest.php @@ -178,7 +178,7 @@ class CreateShortUrlTest extends ApiTestCase 'tags' => ['boo', 'far'], 'validSince' => Chronos::now()->toAtomString(), 'maxVisits' => 7, - ] + ], ]; }