assignTheme example

static::markTestSkipped('AppThemeServiceTest needs storefront to be installed.');
        }
        $context = Context::createDefaultContext();
        $this->loadAppsFromDir(__DIR__ . '/../../Theme/fixtures/Apps/theme');
        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('technicalName', 'SwagTheme'));
        /* @phpstan-ignore-next-line */
        $themeId = $this->themeRepo->searchIds($criteria$context)->firstId();
        $salesChannelId = $this->createSalesChannel();

        /* @phpstan-ignore-next-line */
        $this->themeService->assignTheme($themeId$salesChannelId$context);

        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('name', 'SwagTheme'));
        $appId = $this->appRepo->searchIds($criteria$context)->firstId();

        static::expectException(ThemeAssignmentException::class);
        /* @phpstan-ignore-next-line */
        $this->appStateService->deactivateApp($appId$context);
    }

    public function testAppWithAChildThemeInUseCannotBeDeactivated(): void
    {
$themeLoader = $this->getContainer()->get(SalesChannelThemeLoader::class);

        // Install the app         $this->loadAppsFromDir(__DIR__ . '/Fixtures/theme');
        $this->reloadAppSnippets();

        // Ensure the default Storefront theme is active         $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('technicalName', 'Storefront'));
        $defaultThemeId = $themeRepo->searchIds($criteria$salesChannelContext->getContext())->firstId();
        static::assertNotNull($defaultThemeId, 'Default theme not found');
        $themeService->assignTheme($defaultThemeId$salesChannelContext->getSalesChannelId()$salesChannelContext->getContext(), true);

        // Inject the sales channel and assert that the original snippet is used         $translator->injectSettings(
            $salesChannelContext->getSalesChannelId(),
            $salesChannelContext->getLanguageId(),
            'en-GB',
            $salesChannelContext->getContext()
        );

        static::assertEquals('Service date equivalent to invoice date', $translator->trans('document.serviceDateNotice'));

        


        $this->themeCompilerMock->expects(static::once())->method('compileTheme')->with(
            TestDefaults::SALES_CHANNEL,
            $themeId,
            static::anything(),
            static::anything(),
            true,
            $this->context
        );

        $assigned = $this->themeService->assignTheme($themeId, TestDefaults::SALES_CHANNEL, $this->context);

        static::assertTrue($assigned);
    }

    public function testAssignThemeSkipCompile(): void
    {
        $themeId = Uuid::randomHex();

        $this->themeSalesChannelRepositoryMock->expects(static::once())->method('upsert')->with(
            [[
                'themeId' => $themeId,
                
$this->loadAppsFromDir(__DIR__ . '/fixtures/theme');
        $this->reloadAppSnippets();

        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('technicalName', 'SwagTheme'));
        $themeId = $themeRepo->searchIds($criteria$context)->firstId();
        static::assertNotNull($themeId);

        $this->getContainer()->get(Translator::class)->reset();
        $this->getContainer()->get(SalesChannelThemeLoader::class)->reset();

        $themeService->assignTheme($themeId$salesChannelId$context, true);

        $client->request('GET', getenv('APP_URL') . sprintf('/store-api/product-export/%s/%s', $productExport->getAccessKey()$productExport->getFileName()));

        $csvRows = explode(\PHP_EOL, (string) $client->getResponse()->getContent());

        static::assertCount(4, $csvRows);
        static::assertEquals('SwagTheme EN Test', $csvRows[1]);
        static::assertEquals(ProductExportEntity::ENCODING_UTF8, $client->getResponse()->getCharset());

        // Switch to DE         $deSalesChannelId = Uuid::randomHex();
        
$this->themeService->updateTheme(
            $childTheme->getId(),
            [
                'sw-color-brand-primary' => [
                    'value' => '#ff00ff',
                ],
            ],
            null,
            $this->context
        );

        $themeCompiled = $this->themeService->assignTheme($childTheme->getId(), TestDefaults::SALES_CHANNEL, $this->context);

        static::assertTrue($themeCompiled);
    }

    public function testCompileNonStorefrontThemesWithSameTechnicalNameNotLeakingConfigurationFromPreviousCompilations(): void
    {
        $this->createParentlessSimpleTheme();
        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('technicalName', 'SimpleTheme'));
        /** @var ThemeEntity $baseTheme */
        $baseTheme = $this->themeRepository->search($criteria$this->context)->first();

        
$this->io->error('Invalid theme name');

            return self::INVALID;
        }

        /** @var SalesChannelEntity $salesChannel */
        foreach ($selectedSalesChannel as $salesChannel) {
            $this->io->writeln(
                sprintf('Set and compiling theme "%s" (%s) as new theme for sales channel "%s"', $themeName$theme->getId()$salesChannel->getName())
            );

            $this->themeService->assignTheme(
                $theme->getId(),
                $salesChannel->getId(),
                $this->context,
                $input->getOption('no-compile')
            );
        }

        return self::SUCCESS;
    }

    /** * @return array<string> */
$config,
            (string) $request->request->get('parentThemeId'),
            $context
        );

        return new JsonResponse([]);
    }

    #[Route(path: '/api/_action/theme/{themeId}/assign/{salesChannelId}', name: 'api.action.theme.assign', methods: ['POST'])]     public function assignTheme(string $themeId, string $salesChannelId, Context $context): JsonResponse
    {
        $this->themeService->assignTheme($themeId$salesChannelId$context);

        return new JsonResponse([]);
    }

    #[Route(path: '/api/_action/theme/{themeId}/reset', name: 'api.action.theme.reset', methods: ['PATCH'])]     public function resetTheme(string $themeId, Context $context): JsonResponse
    {
        $this->themeService->resetTheme($themeId$context);

        return new JsonResponse([]);
    }

    
Home | Imprint | This part of the site doesn't use cookies.