Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OP-122: Don't create new token if user has wishlists #198

Merged
merged 3 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@

/etc/build/*
!/etc/build/.gitignore

/behat.yml
56 changes: 56 additions & 0 deletions features/assigning_wishlist_to_user.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@wishlist
Feature: Assigning a wishlist to a user
In order to assign a wishlist to a user
As a Visitor
I want to be able to assign a wishlist to my account if I created it as a not logged in user.

Background:
Given the store operates on a single channel in "United States"
And there is a customer account "jdeer@sylius.pl"
And user "jdeer@sylius.pl" has a wishlist named "Wishlist1" with token "123456token"
And user "jdeer@sylius.pl" has a wishlist named "Wishlist2" with token "123456token"

@ui
Scenario: Listing wishlists
When I go to "/"
And I log in as "jdeer@sylius.pl"
And I go to "/wishlists"
Then I should have 3 wishlists
And I should see "Save wishlist"

@ui
Scenario: Assigning a wishlist
When I go to "/"
And I log in as "jdeer@sylius.pl"
And I go to "/wishlists"
And I follow "Save wishlist"
Then I should have 3 wishlists
And I should not see "Save wishlist"

@ui @javascript
Scenario: Assigning a wishlist to a user and logout
When I go to "/"
And I log in as "jdeer@sylius.pl"
And I go to "/wishlists"
And I press "wishlist-edit-button-Wishlist"
And I fill in "edit_wishlist_name" with "Wishlist-assigned"
And I press "edit_wishlist_save"
And I follow "Save wishlist"
And I log out
And I go to "/wishlists"
Then I should have 1 wishlists
And I should not see "Wishlist-assigned"
And I should see "Wishlist"

@ui @javascript
Scenario: Logout without assigning a wishlist to a user
When I go to "/"
And I log in as "jdeer@sylius.pl"
And I go to "/wishlists"
And I press "wishlist-edit-button-Wishlist"
And I fill in "edit_wishlist_name" with "Wishlist-not-assigned"
And I press "edit_wishlist_save"
And I log out
And I go to "/wishlists"
Then I should have 1 wishlists
And I should see "Wishlist-not-assigned"
13 changes: 11 additions & 2 deletions features/listing_wishlists.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ Feature: Listing wishlists

Background:
Given the store operates on a single channel in "United States"
Given I am on "/"

@ui
Scenario: Listing wishlist
Given I am on "/"
And the store has a wishlist named "Wishlist1"
And the store has a wishlist named "Wishlist2"
When I go to "/wishlists"
Then I should have 3 wishlists
Then I should have 3 wishlists

@ui
Scenario: Listing wishlist as user
Given there is a customer account "jdeer@sylius.pl"
And I am logged in as "jdeer@sylius.pl"
And user "jdeer@sylius.pl" has a wishlist named "Wishlist1" with token "123456token"
And user "jdeer@sylius.pl" has a wishlist named "Wishlist2" with token "123456token"
When I go to "/wishlists"
Then I should have 2 wishlists
5 changes: 4 additions & 1 deletion src/EventSubscriber/CreateNewWishlistSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public function onKernelRequest(RequestEvent $event): void

$wishlistCookieToken = $event->getRequest()->cookies->get($this->wishlistCookieToken);

if ($wishlistCookieToken && !empty($wishlists)) {
if (!empty($wishlists)) {
if (null === $wishlistCookieToken) {
$event->getRequest()->attributes->set($this->wishlistCookieToken, reset($wishlists)->getToken());
}
return;
}

Expand Down
29 changes: 28 additions & 1 deletion tests/Behat/Context/Setup/WishlistContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Behat\Behat\Context\Context;
use BitBag\SyliusWishlistPlugin\Context\WishlistContextInterface;
use BitBag\SyliusWishlistPlugin\Entity\Wishlist;
use BitBag\SyliusWishlistPlugin\Entity\WishlistProductInterface;
use BitBag\SyliusWishlistPlugin\Factory\WishlistProductFactoryInterface;
use Doctrine\Common\Collections\Collection;
Expand All @@ -23,7 +24,9 @@
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Webmozart\Assert\Assert;

final class WishlistContext implements Context
{
Expand All @@ -47,6 +50,8 @@ final class WishlistContext implements Context

private ChannelRepositoryInterface $channelRepository;

private UserRepositoryInterface $userRepository;

public function __construct(
ProductRepositoryInterface $productRepository,
WishlistContextInterface $wishlistContext,
Expand All @@ -57,7 +62,8 @@ public function __construct(
EntityManagerInterface $productTaxonManager,
CookieSetterInterface $cookieSetter,
string $wishlistCookieToken,
ChannelRepositoryInterface $channelRepository
ChannelRepositoryInterface $channelRepository,
UserRepositoryInterface $userRepository
) {
$this->productRepository = $productRepository;
$this->wishlistContext = $wishlistContext;
Expand All @@ -69,6 +75,7 @@ public function __construct(
$this->cookieSetter = $cookieSetter;
$this->wishlistCookieToken = $wishlistCookieToken;
$this->channelRepository = $channelRepository;
$this->userRepository = $userRepository;
}

/**
Expand Down Expand Up @@ -141,4 +148,24 @@ private function addProductToWishlist(ProductInterface $product): void

$this->cookieSetter->setCookie($this->wishlistCookieToken, $wishlist->getToken());
}

/**
* @Given user :email has a wishlist named :name with token :token
*/
public function userHasAWishlistNamedWithToken(string $email, string $name, string $token): void
{
$user = $this->userRepository->findOneByEmail($email);
Assert::notNull($user);

$wishlist = new Wishlist();
$channel = $this->channelRepository->findOneByCode('WEB-US');

$wishlist->setName($name);
$wishlist->setChannel($channel);
$wishlist->setToken($token);
$wishlist->setShopUser($user);

$this->wishlistManager->persist($wishlist);
$this->wishlistManager->flush();
}
}
1 change: 1 addition & 0 deletions tests/Behat/Resources/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ services:
- "@sylius.behat.cookie_setter"
- "%bitbag_sylius_wishlist_plugin.parameters.wishlist_cookie_token%"
- '@sylius.repository.channel'
- '@sylius.repository.shop_user'

bitbag_sylius_cms_plugin.behat.context.ui.wishlist:
class: Tests\BitBag\SyliusWishlistPlugin\Behat\Context\Ui\WishlistContext
Expand Down
2 changes: 2 additions & 0 deletions tests/Behat/Resources/suites/ui/ui_wishlist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ default:
- sylius.behat.context.hook.doctrine_orm
- sylius.behat.context.setup.channel
- sylius.behat.context.setup.product
- sylius.behat.context.setup.customer
- sylius.behat.context.setup.shop_security
- sylius.behat.context.transform.lexical
- sylius.behat.context.transform.product
- sylius.behat.context.transform.channel
Expand Down