Skip to content

Commit

Permalink
Merge pull request #1 from Brille24/fix_cart_id_in_cookie
Browse files Browse the repository at this point in the history
Cookies saving token instead of id
  • Loading branch information
bitbager authored May 22, 2018
2 parents 6467357 + 3c0f400 commit a247e9f
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 36 deletions.
8 changes: 4 additions & 4 deletions spec/Context/WishlistContextSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ function it_returns_cookie_wishlist_if_cookie_and_no_user(
WishlistInterface $wishlist
): void {
$request->cookies = $parameterBag;
$parameterBag->get('bitbag_sylius_wishlist')->willReturn(1);
$parameterBag->get('bitbag_sylius_wishlist')->willReturn("Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId");
$tokenStorage->getToken()->willReturn($token);
$token->getUser()->willReturn(null);
$wishlistRepository->find(1)->willReturn($wishlist);
$wishlistRepository->findByToken("Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId")->willReturn($wishlist);

$this->getWishlist($request)->shouldReturn($wishlist);
}
Expand All @@ -85,10 +85,10 @@ function it_returns_new_wishlist_if_cookie_not_found_and_no_user(
WishlistInterface $wishlist
): void {
$request->cookies = $parameterBag;
$parameterBag->get('bitbag_sylius_wishlist')->willReturn(1);
$parameterBag->get('bitbag_sylius_wishlist')->willReturn("Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId");
$tokenStorage->getToken()->willReturn($token);
$token->getUser()->willReturn(null);
$wishlistRepository->find(1)->willReturn(null);
$wishlistRepository->findByToken("Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId")->willReturn(null);
$wishlistFactory->createNew()->willReturn($wishlist);

$this->getWishlist($request)->shouldReturn($wishlist);
Expand Down
4 changes: 2 additions & 2 deletions spec/Controller/Action/AddProductToWishlistActionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function let(
$flashBag,
$translator,
$urlGenerator,
'bitbag_wishlist_id'
'bitbag_wishlist_token'
);
}

Expand Down Expand Up @@ -81,7 +81,7 @@ function it_handles_the_request_and_persist_new_wishlist_if_needed(
$wishlistManager->persist($wishlist)->shouldBeCalled();
$wishlistManager->flush()->shouldBeCalled();
$flashBag->add('success', 'Product has been added to your wishlist.')->shouldBeCalled();
$wishlist->getId()->shouldBeCalled();
$wishlist->getToken()->shouldBeCalled();

$this->__invoke($request)->shouldHaveType(RedirectResponse::class);
}
Expand Down
8 changes: 4 additions & 4 deletions spec/EventListener/MergeUserWishlistItemsListenerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ function it_adds_cookie_items_to_user_items_if_both_exist(
$token->getUser()->willReturn($shopUser);
$interactiveLoginEvent->getRequest()->willReturn($request);
$request->cookies = $parameterBag;
$parameterBag->get('bitbag_sylius_wishlist')->willReturn(1);
$wishlistRepository->find(1)->willReturn($cookieWishlist);
$parameterBag->get('bitbag_sylius_wishlist')->willReturn("Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId");
$wishlistRepository->findByToken("Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId")->willReturn($cookieWishlist);
$wishlistRepository->findByShopUser($shopUser)->willReturn($userWishlist);
$cookieWishlist->getWishlistProducts()->willReturn(new ArrayCollection([$wishlistProduct->getWrappedObject()]));

Expand All @@ -94,8 +94,8 @@ function it_associates_anon_wishlsit_with_a_user_if_user_does_not_have_one(
$token->getUser()->willReturn($shopUser);
$interactiveLoginEvent->getRequest()->willReturn($request);
$request->cookies = $parameterBag;
$parameterBag->get('bitbag_sylius_wishlist')->willReturn(1);
$wishlistRepository->find(1)->willReturn($cookieWishlist);
$parameterBag->get('bitbag_sylius_wishlist')->willReturn("Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId");
$wishlistRepository->findByToken("Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId")->willReturn($cookieWishlist);
$wishlistRepository->findByShopUser($shopUser)->willReturn(null);

$cookieWishlist->setShopUser($shopUser)->shouldBeCalled();
Expand Down
16 changes: 8 additions & 8 deletions src/Context/WishlistContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,33 @@ final class WishlistContext implements WishlistContextInterface
private $wishlistFactory;

/** @var string */
private $wishlistCookieId;
private $wishlistCookieToken;

public function __construct(
TokenStorageInterface $tokenStorage,
WishlistRepositoryInterface $wishlistRepository,
WishlistFactoryInterface $wishlistFactory,
string $wishlistCookieId
string $wishlistCookieToken
) {
$this->tokenStorage = $tokenStorage;
$this->wishlistRepository = $wishlistRepository;
$this->wishlistFactory = $wishlistFactory;
$this->wishlistCookieId = $wishlistCookieId;
$this->wishlistCookieToken = $wishlistCookieToken;
}

public function getWishlist(Request $request): WishlistInterface
{
$cookieWishlistId = $request->cookies->get($this->wishlistCookieId);
$cookieWishlistToken = $request->cookies->get($this->wishlistCookieToken);
$token = $this->tokenStorage->getToken();
$user = $token ? $token->getUser() : null;

if (null === $cookieWishlistId && null === $user) {
if (null === $cookieWishlistToken && null === $user) {
return $this->wishlistFactory->createNew();
}

if (null !== $cookieWishlistId && !$user instanceof ShopUserInterface) {
return $this->wishlistRepository->find($cookieWishlistId) ?
$this->wishlistRepository->find($cookieWishlistId) :
if (null !== $cookieWishlistToken && !$user instanceof ShopUserInterface) {
return $this->wishlistRepository->findByToken($cookieWishlistToken) ?
$this->wishlistRepository->findByToken($cookieWishlistToken) :
$this->wishlistFactory->createNew()
;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Controller/Action/AddProductToWishlistAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class AddProductToWishlistAction
private $urlGenerator;

/** @var string */
private $wishlistCookieId;
private $wishlistCookieToken;

public function __construct(
ProductRepositoryInterface $productRepository,
Expand All @@ -61,14 +61,14 @@ public function __construct(
FlashBagInterface $flashBag,
TranslatorInterface $translator,
UrlGeneratorInterface $urlGenerator,
string $wishlistCookieId
string $wishlistCookieToken
) {
$this->productRepository = $productRepository;
$this->wishlistContext = $wishlistContext;
$this->wishlistProductFactory = $wishlistProductFactory;
$this->wishlistManager = $wishlistManager;
$this->urlGenerator = $urlGenerator;
$this->wishlistCookieId = $wishlistCookieId;
$this->wishlistCookieToken = $wishlistCookieToken;
$this->flashBag = $flashBag;
$this->translator = $translator;
}
Expand All @@ -95,7 +95,7 @@ public function __invoke(Request $request): Response
$this->wishlistManager->flush();
$this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_wishlist_item'));

$cookie = new Cookie($this->wishlistCookieId, $wishlist->getId(), strtotime('+1 year'));
$cookie = new Cookie($this->wishlistCookieToken, $wishlist->getToken(), strtotime('+1 year'));
$response = new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products'));
$response->headers->setCookie($cookie);

Expand Down
15 changes: 15 additions & 0 deletions src/Entity/Wishlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class Wishlist implements WishlistInterface
{
Expand All @@ -28,9 +29,13 @@ class Wishlist implements WishlistInterface
/** @var ShopUserInterface|null */
protected $shopUser;

/** @var TokenInterface|null */
protected $token;

public function __construct()
{
$this->wishlistProducts = new ArrayCollection();
$this->token = new WishlistToken();
}

public function getId(): ?int
Expand Down Expand Up @@ -87,4 +92,14 @@ public function setShopUser(ShopUserInterface $shopUser): void
{
$this->shopUser = $shopUser;
}

public function getToken(): string
{
return (string) $this->token;
}

public function setToken(string $token): void
{
$this->token = new WishlistToken($token);
}
}
4 changes: 4 additions & 0 deletions src/Entity/WishlistInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public function addWishlistProduct(WishlistProductInterface $wishlistProduct): v
public function getShopUser(): ?ShopUserInterface;

public function setShopUser(ShopUserInterface $shopShopUser): void;

public function getToken(): string;

public function setToken(string $token): void;
}
49 changes: 49 additions & 0 deletions src/Entity/WishlistToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace BitBag\SyliusWishlistPlugin\Entity;

class WishlistToken implements WishlistTokenInterface
{
protected $value;

public function __construct(?string $value = null)
{
if ($value === null) {
$this->value = $this->generate(self::VALUE_LENGTH);
} else {
$this->setValue($value);
}
}

public function getValue(): string
{
return $this->value;
}

public function setValue(string $value): void
{
$this->value = $value;
}

public function __toString()
{
return $this->getValue();
}

private function generate($length): string
{
$token = '';
$codeAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$codeAlphabet .= 'abcdefghijklmnopqrstuvwxyz';
$codeAlphabet .= '0123456789';
$max = strlen($codeAlphabet); // edited

for ($i = 0; $i < $length; ++$i) {
$token .= $codeAlphabet[random_int(0, $max - 1)];
}

return $token;
}
}
14 changes: 14 additions & 0 deletions src/Entity/WishlistTokenInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace BitBag\SyliusWishlistPlugin\Entity;

interface WishlistTokenInterface
{
const VALUE_LENGTH = 50;

public function getValue(): string;

public function setValue(string $value): void;
}
10 changes: 5 additions & 5 deletions src/EventListener/MergeUserWishlistItemsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ final class MergeUserWishlistItemsListener
private $wishlistManager;

/** @var string */
private $wishlistCookieId;
private $wishlistCookieToken;

public function __construct(
WishlistRepositoryInterface $wishlistRepository,
WishlistFactoryInterface $wishlistFactory,
EntityManagerInterface $wishlistManager,
string $wishlistCookieId
string $wishlistCookieToken
) {
$this->wishlistRepository = $wishlistRepository;
$this->wishlistFactory = $wishlistFactory;
$this->wishlistManager = $wishlistManager;
$this->wishlistCookieId = $wishlistCookieId;
$this->wishlistCookieToken = $wishlistCookieToken;
}

public function onInteractiveLogin(InteractiveLoginEvent $interactiveLoginEvent): void
Expand All @@ -58,9 +58,9 @@ public function onInteractiveLogin(InteractiveLoginEvent $interactiveLoginEvent)

private function resolveWishlist(Request $request, ShopUserInterface $shopUser): void
{
$cookieWishlistId = $request->cookies->get($this->wishlistCookieId);
$cookieWishlistToken = $request->cookies->get($this->wishlistCookieToken);
/** @var WishlistInterface|null $cookieWishlist */
$cookieWishlist = $this->wishlistRepository->find($cookieWishlistId);
$cookieWishlist = $this->wishlistRepository->findByToken($cookieWishlistToken);
$userWishlist = $this->wishlistRepository->findByShopUser($shopUser);

if (null === $cookieWishlist) {
Expand Down
10 changes: 10 additions & 0 deletions src/Repository/WishlistRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ public function findByShopUser(ShopUserInterface $shopUser): ?WishlistInterface
->getOneOrNullResult()
;
}

public function findByToken(string $token): ?WishlistInterface
{
return $this->createQueryBuilder('o')
->where('o.token = :token')
->setParameter('token', $token)
->getQuery()
->getOneOrNullResult()
;
}
}
2 changes: 2 additions & 0 deletions src/Repository/WishlistRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
interface WishlistRepositoryInterface extends RepositoryInterface
{
public function findByShopUser(ShopUserInterface $shopUser): ?WishlistInterface;

public function findByToken(string $token): ?WishlistInterface;
}
2 changes: 1 addition & 1 deletion src/Resources/config/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
wishlist_cookie_id: bitbag_sylius_wishlist
wishlist_cookie_token: bitbag_sylius_wishlist

imports:
- { resource: "@BitBagSyliusWishlistPlugin/Resources/config/services.yml" }
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/doctrine/Wishlist.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<generator strategy="AUTO" />
</id>

<field name="token" type="string" column="token" unique="true" />

<one-to-many field="wishlistProducts" target-entity="BitBag\SyliusWishlistPlugin\Entity\WishlistProductInterface" mapped-by="wishlist">
<cascade>
<cascade-all/>
Expand Down
7 changes: 4 additions & 3 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
- "@session.flash_bag"
- "@translator"
- "@router"
- "%wishlist_cookie_id%"
- "%wishlist_cookie_token%"

bitbag_sylius_wishlist_plugin.controller.action.remove_product_from_wishlist:
class: BitBag\SyliusWishlistPlugin\Controller\Action\RemoveProductFromWishlistAction
Expand Down Expand Up @@ -45,7 +45,8 @@ services:
- "@security.token_storage"
- "@bitbag_sylius_wishlist_plugin.repository.wishlist"
- "@bitbag_sylius_wishlist_plugin.factory.wishlist"
- "%wishlist_cookie_id%"
- "%wishlist_cookie_token%"
- "%wishlist_cookie_token%"

bitbag_sylius_wishlist_plugin.custom_factory.wishlist:
class: BitBag\SyliusWishlistPlugin\Factory\WishlistFactory
Expand All @@ -67,7 +68,7 @@ services:
- "@bitbag_sylius_wishlist_plugin.repository.wishlist"
- "@bitbag_sylius_wishlist_plugin.factory.wishlist"
- "@bitbag_sylius_wishlist_plugin.manager.wishlist"
- "%wishlist_cookie_id%"
- "%wishlist_cookie_token%"
tags:
- { name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin }

Expand Down
8 changes: 4 additions & 4 deletions tests/Behat/Context/Setup/WishlistContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ final class WishlistContext implements Context
private $cookieSetter;

/** @var string */
private $wishlistCookieId;
private $wishlistCookieToken;

public function __construct(
ProductRepositoryInterface $productRepository,
Expand All @@ -63,7 +63,7 @@ public function __construct(
FactoryInterface $productTaxonFactory,
EntityManagerInterface $productTaxonManager,
CookieSetterInterface $cookieSetter,
string $wishlistCookieId
string $wishlistCookieToken
) {
$this->productRepository = $productRepository;
$this->wishlistContext = $wishlistContext;
Expand All @@ -73,7 +73,7 @@ public function __construct(
$this->productTaxonFactory = $productTaxonFactory;
$this->productTaxonManager = $productTaxonManager;
$this->cookieSetter = $cookieSetter;
$this->wishlistCookieId = $wishlistCookieId;
$this->wishlistCookieToken = $wishlistCookieToken;
}

/**
Expand Down Expand Up @@ -137,6 +137,6 @@ private function addProductToWishlist(ProductInterface $product): void
$this->wishlistManager->persist($wishlist);
$this->wishlistManager->flush();

$this->cookieSetter->setCookie($this->wishlistCookieId, $wishlist->getId());
$this->cookieSetter->setCookie($this->wishlistCookieToken, $wishlist->getToken());
}
}
2 changes: 2 additions & 0 deletions tests/Behat/Page/Shop/ProductIndexPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ProductIndexPage extends IndexPage implements ProductIndexPageInterface
{
public function addProductToWishlist(string $productName): void
{
$this->getSession()->setCookie('MOCKSESSID', 'foo');

$wishlistElements = $this->getDocument()->findAll('css', '.bitbag-add-to-wishlist');

/** @var NodeElement $wishlistElement */
Expand Down
Loading

0 comments on commit a247e9f

Please sign in to comment.