ContextTokenResponse example

static::assertSame($dataBag$this->controller->renderStorefrontParameters['data']);
        static::assertSame('frontend.account.home.page', $this->controller->renderStorefrontParameters['redirectTo'] ?? '');
        static::assertSame('[]', $this->controller->renderStorefrontParameters['redirectParameters'] ?? '');
        static::assertSame('frontend.account.login.page', $this->controller->renderStorefrontParameters['errorRoute'] ?? '');
        static::assertInstanceOf(AccountLoginPageLoadedHook::class$this->controller->calledHook);
    }

    public function testLoginNewContextIsAdded(): void
    {
        $this->loginRoute
            ->method('login')
            ->willReturn(new ContextTokenResponse('context_token_response'));

        $newSalesChannelContext = Generator::createSalesChannelContext();
        $this->salesChannelContextService
            ->expects(static::once())
            ->method('get')
            ->willReturn($newSalesChannelContext);

        $oldSalesChannelContext = Generator::createSalesChannelContext();
        $oldSalesChannelContext->assign(['customer' => null]);

        $request = new Request();
        
    public function logout(SalesChannelContext $context, RequestDataBag $data): ContextTokenResponse
    {
        /** @var CustomerEntity $customer */
        $customer = $context->getCustomer();
        if ($this->shouldDelete($context)) {
            $this->cartService->deleteCart($context);
            $this->contextPersister->delete($context->getToken()$context->getSalesChannelId());

            $event = new CustomerLogoutEvent($context$customer);
            $this->eventDispatcher->dispatch($event);

            return new ContextTokenResponse($context->getToken());
        }

        $newToken = Random::getAlphanumericString(32);
        if ((bool) $data->get('replace-token')) {
            $newToken = $this->contextPersister->replace($context->getToken()$context);
        }

        $context->assign([
            'token' => $newToken,
        ]);

        
    public function change(RequestDataBag $requestDataBag, SalesChannelContext $context, CustomerEntity $customer): ContextTokenResponse
    {
        $this->validatePasswordFields($requestDataBag$context);

        $customerData = [
            'id' => $customer->getId(),
            'password' => $requestDataBag->get('newPassword'),
        ];

        $this->customerRepository->update([$customerData]$context->getContext());

        return new ContextTokenResponse($context->getToken());
    }

    private function dispatchValidationEvent(DataValidationDefinition $definition, DataBag $data, Context $context): void
    {
        $validationEvent = new BuildValidationEvent($definition$data$context);
        $this->eventDispatcher->dispatch($validationEvent$validationEvent->getName());
    }

    /** * @throws ConstraintViolationException */
    
$parameters,
            $context->getSalesChannel()->getId(),
            $customer && empty($context->getPermissions()) ? $customer->getId() : null
        );

        // Language was switched - Check new Domain         $changeUrl = $this->checkNewDomain($parameters$context);

        $event = new SalesChannelContextSwitchEvent($context$data);
        $this->eventDispatcher->dispatch($event);

        return new ContextTokenResponse($context->getToken()$changeUrl);
    }

    /** * @param array<mixed> $parameters */
    private function checkNewDomain(array $parameters, SalesChannelContext $context): ?string
    {
        if (
            !isset($parameters[self::LANGUAGE_ID])
            || $parameters[self::LANGUAGE_ID] === $context->getLanguageId()
        ) {
            
$changeLangMock = $this->createMock(AbstractChangeLanguageRoute::class);

        $routerMock = $this->createMock(RouterInterface::class);
        $routerMock->expects(static::once())->method('getContext')->willReturn(new RequestContext());
        $routerMock->expects(static::once())->method('generate')->willReturn('http://localhost');
        $requestStackMock = $this->createMock(RequestStack::class);
        $requestStackMock->expects(static::exactly(2))->method('getMainRequest')->willReturn(new Request());

        $contextSwitchRoute = $this->createMock(ContextSwitchRoute::class);
        $contextSwitchRoute->expects(static::once())->method('switchContext')->willReturn(
            new ContextTokenResponse(Uuid::randomHex(), 'http://localhost')
        );

        $controller = new ContextController(
            $contextSwitchRoute,
            $requestStackMock,
            $routerMock
        );

        $contextMock = $this->createMock(SalesChannelContext::class);

        $controller->switchLanguage(
            

#[Package('buyers-experience')] class ContextTokenResponseTest extends TestCase
{
    public function testGetTokenFromResponseBody(): void
    {
        Feature::skipTestIfActive('v6.6.0.0', $this);

        $token = 'sw-token-value';
        $response = new ContextTokenResponse($token);
        static::assertSame($token$response->getToken());
    }

    public function testGetTokenFromHeader(): void
    {
        Feature::skipTestIfInActive('v6.6.0.0', $this);

        $token = 'sw-token-value';
        $response = new ContextTokenResponse($token);
        static::assertSame($token$response->getToken());

        
$this->customerRepository->update([
            [
                'id' => $customer->getId(),
                'lastLogin' => new \DateTimeImmutable(),
                'languageId' => $context->getLanguageId(),
            ],
        ]$context->getContext());

        $event = new CustomerLoginEvent($context$customer$newToken);
        $this->eventDispatcher->dispatch($event);

        return new ContextTokenResponse($newToken);
    }
}
Home | Imprint | This part of the site doesn't use cookies.