Fix error with being unable to add product review when the user is logged in by remember me option

This commit is contained in:
TheMilek 2023-08-24 15:24:04 +02:00
parent 65bc7a32e4
commit 8448d136df
No known key found for this signature in database
GPG key ID: 2E44205E7374692F
5 changed files with 54 additions and 3 deletions

View file

@ -7,12 +7,21 @@ Feature: Adding product review as a customer
Background:
Given the store operates on a single channel in "United States"
And the store has a product "Necronomicon"
And I am a logged in customer
@ui @api
Scenario: Adding product reviews as a logged in customer
Given I am a logged in customer
When I want to review product "Necronomicon"
And I leave a comment "Great book for every advanced sorcerer.", titled "Scary but astonishing"
And I rate it with 5 points
And I add it
Then I should be notified that my review is waiting for the acceptation
@ui
Scenario: Adding product reviews as a logged in customer with remember me option
Given I am a logged in customer by using remember me option
When I want to review product "Necronomicon"
And I leave a comment "Great book for every advanced sorcerer.", titled "Scary but astonishing"
And I rate it with 3 points
And I add it
Then I should be notified that my review is waiting for the acceptation

View file

@ -67,4 +67,17 @@ final class ShopSecurityContext implements Context
$this->sharedStorage->set('user', $user);
}
/**
* @Given I am a logged in customer by using remember me option
*/
public function iAmLoggedInCustomerByUsingRememberMeOption(): void
{
$userData = ['email' => 'sylius@example.com', 'password' => 'sylius', 'enabled' => true];
$user = $this->userFactory->create($userData);
$this->userRepository->add($user);
$this->securityService->logInWithRememberMe($user);
}
}

View file

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Behat\Service;
use Sylius\Component\User\Model\UserInterface;
interface RememberMeAwareSecurityServiceInterface extends SecurityServiceInterface
{
public function logInWithRememberMe(UserInterface $user): void;
}

View file

@ -19,11 +19,12 @@ use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionFactoryInterface;
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;
final class SecurityService implements SecurityServiceInterface
final class SecurityService implements RememberMeAwareSecurityServiceInterface
{
private string $sessionTokenVariable;
@ -48,6 +49,13 @@ final class SecurityService implements SecurityServiceInterface
$this->setToken($token);
}
public function logInWithRememberMe(UserInterface $user): void
{
$token = new RememberMeToken($user, $this->firewallContextName, 'secret');
$this->setToken($token);
}
public function logOut(): void
{
try {

View file

@ -12,7 +12,7 @@
{{ form_widget(form.comment, sylius_test_form_attribute('comment')|sylius_merge_recursive( {'attr': {'placeholder': 'sylius.ui.write_your_own_review'|trans}})) }}
{{ form_errors(form.comment) }}
</div>
{% if not is_granted("IS_AUTHENTICATED_FULLY") %}
{% if not is_granted("IS_AUTHENTICATED_REMEMBERED") %}
<div class="field">
{{ form_widget(form.author.email, sylius_test_form_attribute('author-email')|sylius_merge_recursive( {'attr': {'placeholder': 'sylius.ui.email'|trans}})) }}
{{ form_errors(form.author.email) }}