From b26d99c0596d9773285e6fb173d34eab78540e6a Mon Sep 17 00:00:00 2001 From: jkindly Date: Fri, 23 Aug 2024 09:09:27 +0200 Subject: [PATCH] OP-496: Fix for finding media when translation is empty + remove dead code --- spec/Resolver/MediaResourceResolverSpec.php | 13 +- src/Entity/MediaTranslation.php | 6 +- .../PagesCollectionContentElementRenderer.php | 4 +- src/Repository/MediaRepository.php | 46 ------ src/Repository/MediaRepositoryInterface.php | 13 -- src/Resolver/MediaResourceResolver.php | 3 - src/Resources/config/routing/shop/media.yml | 42 ------ src/Resources/config/services/resolver.xml | 1 - .../Behat/Context/Transform/MediaContext.php | 1 - tests/Functional/Api/MediaTest.php | 2 +- ...test_it_finds_media_by_collection_code.yml | 77 ---------- .../test_it_finds_media_by_product_code.yml | 132 ------------------ .../Repository/MediaRepositoryTest.php | 17 +-- 13 files changed, 11 insertions(+), 346 deletions(-) delete mode 100644 tests/Integration/DataFixtures/ORM/MediaRepositoryTest/test_it_finds_media_by_collection_code.yml delete mode 100644 tests/Integration/DataFixtures/ORM/MediaRepositoryTest/test_it_finds_media_by_product_code.yml diff --git a/spec/Resolver/MediaResourceResolverSpec.php b/spec/Resolver/MediaResourceResolverSpec.php index a3fb1a139..aa1d4a3c2 100644 --- a/spec/Resolver/MediaResourceResolverSpec.php +++ b/spec/Resolver/MediaResourceResolverSpec.php @@ -24,11 +24,10 @@ final class MediaResourceResolverSpec extends ObjectBehavior { public function let( MediaRepositoryInterface $mediaRepository, - LocaleContextInterface $localeContext, ChannelContextInterface $channelContext, LoggerInterface $logger, ) { - $this->beConstructedWith($mediaRepository, $localeContext, $channelContext, $logger); + $this->beConstructedWith($mediaRepository, $channelContext, $logger); } public function it_is_initializable(): void @@ -43,40 +42,34 @@ public function it_implements_media_resource_resolver_interface(): void public function it_returns_media_when_found( MediaRepositoryInterface $mediaRepository, - LocaleContextInterface $localeContext, ChannelContextInterface $channelContext, MediaInterface $media, ChannelInterface $channel, ) { $code = 'media_code'; - $localeCode = 'en_US'; $channelCode = 'ecommerce'; $channelContext->getChannel()->willReturn($channel); $channel->getCode()->willReturn($channelCode); - $localeContext->getLocaleCode()->willReturn($localeCode); - $mediaRepository->findOneEnabledByCode($code, $localeCode, $channelCode)->willReturn($media); + $mediaRepository->findOneEnabledByCode($code, $channelCode)->willReturn($media); $this->findOrLog($code)->shouldReturn($media); } public function it_logs_warning_and_returns_null_when_media_not_found( MediaRepositoryInterface $mediaRepository, - LocaleContextInterface $localeContext, ChannelContextInterface $channelContext, LoggerInterface $logger, ChannelInterface $channel, ) { $code = 'non_existing_code'; - $localeCode = 'en_US'; $channelCode = 'ecommerce'; $channelContext->getChannel()->willReturn($channel); $channel->getCode()->willReturn($channelCode); - $localeContext->getLocaleCode()->willReturn($localeCode); - $mediaRepository->findOneEnabledByCode($code, $localeCode, $channelCode)->willReturn(null); + $mediaRepository->findOneEnabledByCode($code, $channelCode)->willReturn(null); $logger->warning(sprintf('Media with "%s" code was not found in the database.', $code))->shouldBeCalled(); diff --git a/src/Entity/MediaTranslation.php b/src/Entity/MediaTranslation.php index 7853b754a..06c685a58 100755 --- a/src/Entity/MediaTranslation.php +++ b/src/Entity/MediaTranslation.php @@ -16,11 +16,11 @@ class MediaTranslation extends AbstractTranslation implements MediaTranslationIn { protected ?int $id; - protected ?string $content; + protected ?string $content = null; - protected ?string $alt; + protected ?string $alt = null; - protected ?string $link; + protected ?string $link = null; public function getId(): ?int { diff --git a/src/Renderer/ContentElement/PagesCollectionContentElementRenderer.php b/src/Renderer/ContentElement/PagesCollectionContentElementRenderer.php index a0447596c..543d18d0a 100644 --- a/src/Renderer/ContentElement/PagesCollectionContentElementRenderer.php +++ b/src/Renderer/ContentElement/PagesCollectionContentElementRenderer.php @@ -36,12 +36,12 @@ public function render(ContentConfigurationInterface $contentConfiguration): str return ''; } - /** @var CollectionInterface $collection */ + /** @var CollectionInterface|null $collection */ $collection = $this->collectionRepository->findOneBy(['code' => $code]); return $this->twig->render('@BitBagSyliusCmsPlugin/Shop/ContentElement/index.html.twig', [ 'content_element' => '@BitBagSyliusCmsPlugin/Shop/ContentElement/_pages_collection.html.twig', - 'collection' => $collection->getPages(), + 'collection' => $collection?->getPages(), ]); } } diff --git a/src/Repository/MediaRepository.php b/src/Repository/MediaRepository.php index 8b3fd5460..f8b45ede8 100755 --- a/src/Repository/MediaRepository.php +++ b/src/Repository/MediaRepository.php @@ -26,66 +26,20 @@ public function createListQueryBuilder(string $locale): QueryBuilder public function findOneEnabledByCode( string $code, - string $localeCode, string $channelCode, ): ?MediaInterface { return $this->createQueryBuilder('o') - ->leftJoin('o.translations', 'translation') ->innerJoin('o.channels', 'channels') - ->where('translation.locale = :localeCode') ->andWhere('o.code = :code') ->andWhere('o.enabled = true') ->andWhere('channels.code = :channelCode') ->setParameter('code', $code) - ->setParameter('localeCode', $localeCode) ->setParameter('channelCode', $channelCode) ->getQuery() ->getOneOrNullResult() ; } - public function findByCollectionCode( - string $collectionCode, - string $localeCode, - string $channelCode, - ): array { - return $this->createQueryBuilder('o') - ->leftJoin('o.translations', 'translation') - ->innerJoin('o.collections', 'collection') - ->innerJoin('o.channels', 'channels') - ->andWhere('translation.locale = :localeCode') - ->andWhere('collection.code = :collectionCode') - ->andWhere('o.enabled = true') - ->andWhere('channels.code = :channelCode') - ->setParameter('localeCode', $localeCode) - ->setParameter('collectionCode', $collectionCode) - ->setParameter('channelCode', $channelCode) - ->getQuery() - ->getResult() - ; - } - - public function findByProductCode( - string $productCode, - string $localeCode, - string $channelCode, - ): array { - return $this->createQueryBuilder('o') - ->leftJoin('o.translations', 'translation') - ->innerJoin('o.products', 'product') - ->innerJoin('o.channels', 'channels') - ->andWhere('translation.locale = :localeCode') - ->andWhere('product.code = :productCode') - ->andWhere('o.enabled = true') - ->andWhere('channels.code = :channelCode') - ->setParameter('localeCode', $localeCode) - ->setParameter('productCode', $productCode) - ->setParameter('channelCode', $channelCode) - ->getQuery() - ->getResult() - ; - } - public function findByNamePart(string $phrase, array $mediaType): array { return $this->createQueryBuilder('o') diff --git a/src/Repository/MediaRepositoryInterface.php b/src/Repository/MediaRepositoryInterface.php index 30944bc0e..bfaf6a715 100755 --- a/src/Repository/MediaRepositoryInterface.php +++ b/src/Repository/MediaRepositoryInterface.php @@ -20,21 +20,8 @@ public function createListQueryBuilder(string $locale): QueryBuilder; public function findOneEnabledByCode( string $code, - string $localeCode, string $channelCode, ): ?MediaInterface; - public function findByCollectionCode( - string $collectionCode, - string $localeCode, - string $channelCode, - ): array; - - public function findByProductCode( - string $productCode, - string $localeCode, - string $channelCode, - ): array; - public function findByNamePart(string $phrase, array $mediaType): array; } diff --git a/src/Resolver/MediaResourceResolver.php b/src/Resolver/MediaResourceResolver.php index c396aa6d4..68fb9f773 100755 --- a/src/Resolver/MediaResourceResolver.php +++ b/src/Resolver/MediaResourceResolver.php @@ -14,14 +14,12 @@ use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface; use Psr\Log\LoggerInterface; use Sylius\Component\Channel\Context\ChannelContextInterface; -use Sylius\Component\Locale\Context\LocaleContextInterface; use Webmozart\Assert\Assert; final class MediaResourceResolver implements MediaResourceResolverInterface { public function __construct( private MediaRepositoryInterface $mediaRepository, - private LocaleContextInterface $localeContext, private ChannelContextInterface $channelContext, private LoggerInterface $logger, ) { @@ -32,7 +30,6 @@ public function findOrLog(string $code): ?MediaInterface Assert::notNull($this->channelContext->getChannel()->getCode()); $media = $this->mediaRepository->findOneEnabledByCode( $code, - $this->localeContext->getLocaleCode(), $this->channelContext->getChannel()->getCode(), ); diff --git a/src/Resources/config/routing/shop/media.yml b/src/Resources/config/routing/shop/media.yml index 5f2cad417..36826fb59 100755 --- a/src/Resources/config/routing/shop/media.yml +++ b/src/Resources/config/routing/shop/media.yml @@ -4,20 +4,6 @@ bitbag_sylius_cms_plugin_shop_media_render: defaults: _controller: bitbag_sylius_cms_plugin.controller.media.overriden::renderMediaAction -bitbag_sylius_cms_plugin_shop_media_render_template: - path: /media-template/{code} - methods: [GET] - defaults: - _controller: bitbag_sylius_cms_plugin.controller.media.overriden::showAction - _sylius: - template: $template - repository: - method: findOneEnabledByCode - arguments: - - $code - - "expr:service('sylius.context.locale').getLocaleCode()" - - "expr:service('sylius.context.channel').getChannel().getCode()" - bitbag_sylius_cms_plugin_shop_media_download: path: /media/download/{code} methods: [GET] @@ -31,31 +17,3 @@ bitbag_sylius_cms_plugin_shop_media_inline: defaults: _controller: bitbag_sylius_cms_plugin.controller.media.overriden::downloadMediaAction disposition: !php/const Symfony\Component\HttpFoundation\ResponseHeaderBag::DISPOSITION_INLINE - -bitbag_sylius_cms_plugin_shop_media_index_by_collection_code: - path: /media/collection/{collectionCode} - methods: [GET] - defaults: - _controller: bitbag_sylius_cms_plugin.controller.media.overriden::indexAction - _sylius: - template: $template - repository: - method: findByCollectionCode - arguments: - - $collectionCode - - "expr:service('sylius.context.locale').getLocaleCode()" - - "expr:service('sylius.context.channel').getChannel().getCode()" - -bitbag_sylius_cms_plugin_shop_media_index_by_product_code: - path: /media/product/{productCode} - methods: [GET] - defaults: - _controller: bitbag_sylius_cms_plugin.controller.media.overriden::indexAction - _sylius: - template: $template - repository: - method: findByProductCode - arguments: - - $productCode - - "expr:service('sylius.context.locale').getLocaleCode()" - - "expr:service('sylius.context.channel').getChannel().getCode()" diff --git a/src/Resources/config/services/resolver.xml b/src/Resources/config/services/resolver.xml index 651791039..3c0b800a0 100644 --- a/src/Resources/config/services/resolver.xml +++ b/src/Resources/config/services/resolver.xml @@ -48,7 +48,6 @@ - diff --git a/tests/Behat/Context/Transform/MediaContext.php b/tests/Behat/Context/Transform/MediaContext.php index 896d88b41..e8f945c2a 100644 --- a/tests/Behat/Context/Transform/MediaContext.php +++ b/tests/Behat/Context/Transform/MediaContext.php @@ -34,7 +34,6 @@ public function getMediaByCode(string $mediaCode): MediaInterface { $media = $this->mediaRepositoryInterface->findOneEnabledByCode( $mediaCode, - $this->sharedStorage->get('locale')->getCode(), $this->sharedStorage->get('channel')->getCode(), ); diff --git a/tests/Functional/Api/MediaTest.php b/tests/Functional/Api/MediaTest.php index e235d4d7d..37e98aa2a 100644 --- a/tests/Functional/Api/MediaTest.php +++ b/tests/Functional/Api/MediaTest.php @@ -28,7 +28,7 @@ public function setUp(): void public function test_media_response(): void { /** @var MediaInterface $media */ - $media = $this->getRepository()->findOneEnabledByCode('media1-code', 'en_US', 'code'); + $media = $this->getRepository()->findOneEnabledByCode('media1-code', 'code'); $this->client->request('GET', '/api/v2/shop/cms-plugin/media/' . $media->getId(), [], [], self::CONTENT_TYPE_HEADER); $response = $this->client->getResponse(); diff --git a/tests/Integration/DataFixtures/ORM/MediaRepositoryTest/test_it_finds_media_by_collection_code.yml b/tests/Integration/DataFixtures/ORM/MediaRepositoryTest/test_it_finds_media_by_collection_code.yml deleted file mode 100644 index 13e62b363..000000000 --- a/tests/Integration/DataFixtures/ORM/MediaRepositoryTest/test_it_finds_media_by_collection_code.yml +++ /dev/null @@ -1,77 +0,0 @@ -Sylius\Component\Addressing\Model\Country: - USA: - code: 'US' -Sylius\Component\Currency\Model\Currency: - dollar: - code: 'USD' -Sylius\Component\Locale\Model\Locale: - locale: - createdAt: '' - code: 'en_US' -Sylius\Component\Core\Model\Channel: - channel: - code: "code" - name: "name" - locales: - - '@locale' - default_locale: '@locale' - tax_calculation_strategy: 'order_items_based' - base_currency: '@dollar' -BitBag\SyliusCmsPlugin\Entity\MediaTranslation: - media1_translation: - locale: 'en_US' - content: 'translation_content_en_US' - link: 'translation_link_en_US' - media2_translation: - locale: 'en_US' - content: 'translation_content_en_US' - link: 'translation_link_en_US' - media3_translation: - locale: 'en_US' - content: 'translation_content_en_US' - link: 'translation_link_en_US' -BitBag\SyliusCmsPlugin\Entity\Media: - media1: - code: 'media1-code' - name: 'media1-name' - enabled: true - type: 'image' - path: '/path/to/media1' - collections: - - '@collection1' - channels: - - '@channel' - translations: - - '@media1_translation' - media2: - code: 'media2-code' - name: 'media2-name' - enabled: true - type: 'image' - path: '/path/to/media2' - collections: - - '@collection2' - channels: - - '@channel' - translations: - - '@media2_translation' - media3: - code: 'media3-code' - name: 'media3-name' - enabled: false - type: 'image' - path: '/path/to/media3' - collections: - - '@collection3' - channels: - - '@channel' - translations: - - '@media3_translation' -BitBag\SyliusCmsPlugin\Entity\Collection: - collection1: - code: 'collection1-code' - collection2: - code: 'collection2-code' - collection3: - code: 'collection3-code' - diff --git a/tests/Integration/DataFixtures/ORM/MediaRepositoryTest/test_it_finds_media_by_product_code.yml b/tests/Integration/DataFixtures/ORM/MediaRepositoryTest/test_it_finds_media_by_product_code.yml deleted file mode 100644 index fb393aa25..000000000 --- a/tests/Integration/DataFixtures/ORM/MediaRepositoryTest/test_it_finds_media_by_product_code.yml +++ /dev/null @@ -1,132 +0,0 @@ -Sylius\Component\Addressing\Model\Country: - USA: - code: 'US' -Sylius\Component\Currency\Model\Currency: - dollar: - code: 'USD' -Sylius\Component\Locale\Model\Locale: - locale: - createdAt: '' - code: 'en_US' -Sylius\Component\Core\Model\Channel: - channel: - code: "code" - name: "name" - locales: - - '@locale' - default_locale: '@locale' - tax_calculation_strategy: 'order_items_based' - base_currency: '@dollar' -Sylius\Component\Taxonomy\Model\TaxonTranslation: - taxon_translation: - locale: 'en_US' - name: "taxon-trans" - slug: "taxon-slug" -Sylius\Component\Core\Model\Taxon: - taxon: - code: "menu_category" - translations: - - '@taxon_translation' - enabled: true -Sylius\Component\Core\Model\ProductTranslation: - first_product_US_translation: - name: 'test_name1' - slug: 'test_slug1' - locale: 'en_US' - second_product_US_translation: - name: 'test_name2' - slug: 'test_slug2' - locale: 'en_US' - third_product_US_translation: - name: 'test_name3' - slug: 'test_slug3' - locale: 'en_US' -Sylius\Component\Core\Model\Product: - product1: - fallbackLocale: "en_US" - currentLocale: "en_US" - code: "MUG_SW" - translations: - - "@first_product_US_translation" - product2: - fallbackLocale: "en_US" - currentLocale: "en_US" - code: "MUG_SW2" - translations: - - "@second_product_US_translation" - product3: - fallbackLocale: "en_US" - currentLocale: "en_US" - code: "MUG_SW3" - translations: - - "@third_product_US_translation" -Sylius\Component\Core\Model\ProductTaxon: - first_relation: - taxon: '@taxon' - product: '@product1' - second_relation: - taxon: '@taxon' - product: '@product2' -Sylius\Component\Core\Model\ProductVariant: - product_variant1: - product: '@product1' - code: "code1" - -BitBag\SyliusCmsPlugin\Entity\MediaTranslation: - media1_translation: - locale: 'en_US' - content: 'translation_content_en_US' - link: 'translation_link_en_US' - media2_translation: - locale: 'en_US' - content: 'translation_content_en_US' - link: 'translation_link_en_US' - media3_translation: - locale: 'en_US' - content: 'translation_content_en_US' - link: 'translation_link_en_US' -BitBag\SyliusCmsPlugin\Entity\Media: - media1: - code: 'media1-code' - name: 'media1-name' - enabled: true - type: 'image' - path: '/path/to/media1' - collections: - - '@collection1' - channels: - - '@channel' - translations: - - '@media1_translation' - media2: - code: 'media2-code' - name: 'media2-name' - enabled: true - type: 'image' - path: '/path/to/media2' - collections: - - '@collection2' - channels: - - '@channel' - translations: - - '@media2_translation' - media3: - code: 'media3-code' - name: 'media3-name' - enabled: false - type: 'image' - path: '/path/to/media3' - collections: - - '@collection3' - channels: - - '@channel' - translations: - - '@media3_translation' -BitBag\SyliusCmsPlugin\Entity\Collection: - collection1: - code: 'collection1-code' - collection2: - code: 'collection2-code' - collection3: - code: 'collection3-code' - diff --git a/tests/Integration/Repository/MediaRepositoryTest.php b/tests/Integration/Repository/MediaRepositoryTest.php index 71e89ee43..b7f3c8f2f 100644 --- a/tests/Integration/Repository/MediaRepositoryTest.php +++ b/tests/Integration/Repository/MediaRepositoryTest.php @@ -28,26 +28,13 @@ public function test_it_finds_enabled_media_by_code(): void $mediaRepository = $this->getRepository(); - $media1 = $mediaRepository->findOneEnabledByCode('media1-code', 'en_US', 'code'); - $media3 = $mediaRepository->findOneEnabledByCode('media3-code', 'en_US', 'code'); + $media1 = $mediaRepository->findOneEnabledByCode('media1-code', 'code'); + $media3 = $mediaRepository->findOneEnabledByCode('media3-code', 'code'); self::assertNotNull($media1); self::assertNull($media3); } - public function test_it_finds_enabled_media_by_collection_code(): void - { - $this->loadFixturesFromFile('MediaRepositoryTest/test_it_finds_media_by_collection_code.yml'); - - $mediaRepository = $this->getRepository(); - - $media1 = $mediaRepository->findByCollectionCode('collection1-code', 'en_US', 'code'); - $media3 = $mediaRepository->findByCollectionCode('collection3-code', 'en_US', 'code'); - - self::assertNotEmpty($media1); - self::assertEmpty($media3); - } - public function test_it_finds_media_by_name_part(): void { $this->loadFixturesFromFile('MediaRepositoryTest/test_it_finds_media_by_name.yml');