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

[Admin][Promotion] Fix removing taxon used in promotion rule #10365

Merged
merged 8 commits into from
May 13, 2019
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@managing_promotions
Feature: Accessing an edit page of a promotion with a rule that contains a removed taxon
In order to change promotion details
As an Administrator
I want to be able to access an edit page of a promotion with a rule that contains a removed taxon

Background:
Given the store operates on a single channel in "United States"
And the store classifies its products as "T-Shirts" and "Mugs"
And I am logged in as an administrator

@ui
Scenario: Accessing an edit page of a promotion with a rule that contains a removed taxon
Given there is a promotion "Christmas sale" with "Has at least one from taxons" rule configured with "T-Shirts" and "Mugs"
When I remove taxon named "Mugs"
Then I should be notified that "Christmas sale" promotion has been updated
And I should be able to modify a "Christmas sale" promotion
And the "Christmas sale" promotion should have "Has at least one from taxons" rule configured

@ui
Scenario: Accessing an edit page of a promotion with a rule that contains a removed taxon
Given there is a promotion "Christmas sale" with "Total price of items from taxon" rule configured with "Mugs" taxon and $100 amount for "United States" channel
When I remove taxon named "Mugs"
Then I should be notified that "Christmas sale" promotion has been updated
And I should be able to modify a "Christmas sale" promotion
And the "Christmas sale" promotion should not have any rule configured
55 changes: 44 additions & 11 deletions src/Sylius/Behat/Context/Setup/PromotionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,40 @@ public function __construct(
}

/**
* @Given there is (also) a promotion :promotionName
* @Given there is a promotion :promotionName identified by :promotionCode code
* @Given there is (also) a promotion :name
* @Given there is a promotion :name identified by :code code
*/
public function thereIsPromotion(string $promotionName, ?string $promotionCode = null): void
public function thereIsPromotion(string $name, ?string $code = null): void
{
$promotion = $this->testPromotionFactory
->createForChannel($promotionName, $this->sharedStorage->get('channel'))
;
$this->createPromotion($name, $code);
}

if (null !== $promotionCode) {
$promotion->setCode($promotionCode);
}
/**
* @Given /^there is a promotion "([^"]+)" with "Has at least one from taxons" rule (configured with "[^"]+" and "[^"]+")$/
*/
public function thereIsAPromotionWithHasAtLeastOneFromTaxonsRuleConfiguredWith(string $name, array $taxons): void
{
$promotion = $this->createPromotion($name);
$rule = $this->ruleFactory->createHasTaxon([$taxons[0]->getCode(), $taxons[1]->getCode()]);
$promotion->addRule($rule);

$this->promotionRepository->add($promotion);
$this->sharedStorage->set('promotion', $promotion);
$this->objectManager->flush();
}

/**
* @Given /^there is a promotion "([^"]+)" with "Total price of items from taxon" rule configured with ("[^"]+" taxon) and (?:€|£|\$)([^"]+) amount for ("[^"]+" channel)$/
*/
public function thereIsAPromotionWithTotalPriceOfItemsFromTaxonRuleConfiguredWithTaxonAndAmountForChannel(
string $name,
TaxonInterface $taxon,
int $amount,
ChannelInterface $channel
): void {
$promotion = $this->createPromotion($name);
$rule = $this->ruleFactory->createItemsFromTaxonTotal($channel->getCode(), $taxon->getCode(), $amount);
$promotion->addRule($rule);

$this->objectManager->flush();
}

/**
Expand Down Expand Up @@ -730,6 +749,20 @@ private function getPriceRangeFilterConfiguration($minAmount, $maxAmount = null)
return $configuration;
}

private function createPromotion(string $name, ?string $code = null): PromotionInterface
{
$promotion = $this->testPromotionFactory->createForChannel($name, $this->sharedStorage->get('channel'));

if (null !== $code) {
$promotion->setCode($code);
}

$this->promotionRepository->add($promotion);
$this->sharedStorage->set('promotion', $promotion);

return $promotion;
}

/**
* @param int $discount
*/
Expand Down
7 changes: 4 additions & 3 deletions src/Sylius/Behat/Context/Transform/TaxonContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ public function getTaxonByCode($code)

/**
* @Transform /^classified as "([^"]+)" or "([^"]+)"$/
* @Transform /^configured with "([^"]+)" and "([^"]+)"$/
*/
public function getTaxonsByNames($firstTaxon, $secondTaxon)
public function getTaxonsByNames(string $firstTaxonName, string $secondTaxonName): array
{
return [
$this->getTaxonByName($firstTaxon),
$this->getTaxonByName($secondTaxon),
$this->getTaxonByName($firstTaxonName),
$this->getTaxonByName($secondTaxonName),
];
}
}
40 changes: 36 additions & 4 deletions src/Sylius/Behat/Context/Ui/Admin/ManagingPromotionsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,13 @@ public function iSpecifyThatThisActionShouldBeAppliedToItemsFromCategory($taxonN
}

/**
* @When /^I add the "([^"]+)" action configured with a percentage value of (?:|-)([^"]+)% for ("[^"]+" channel)$/
* @When /^I add the "([^"]+)" action configured with a percentage value of (?:|-)([^"]+)% for ("[^"]+") channel$/
*/
public function iAddTheActionConfiguredWithAPercentageValueForChannel($actionType, $percentage = null, $channelName)
{
public function iAddTheActionConfiguredWithAPercentageValueForChannel(
string $actionType,
string $percentage = null,
string $channelName
): void {
$this->createPage->addAction($actionType);
$this->createPage->fillActionOptionForChannel($channelName, 'Percentage', $percentage);
}
Expand Down Expand Up @@ -408,7 +411,6 @@ public function thePromotionShouldBeApplicableForTheChannel(PromotionInterface $
/**
* @Given I want to modify a :promotion promotion
* @Given /^I want to modify (this promotion)$/
* @Then I should be able to modify a :promotion promotion
*/
public function iWantToModifyAPromotion(PromotionInterface $promotion)
{
Expand Down Expand Up @@ -617,6 +619,36 @@ public function thePromotionsShouldHavePriority(PromotionInterface $promotion, i
Assert::same($this->updatePage->getPriority(), $priority);
}

/**
* @Then I should be able to modify a :promotion promotion
*/
public function iShouldBeAbleToModifyAPromotion(PromotionInterface $promotion): void
{
$this->iWantToModifyAPromotion($promotion);
$this->updatePage->saveChanges();
}

/**
* @Then the :promotion promotion should have :ruleName rule configured
*/
public function thePromotionShouldHaveRuleConfigured(PromotionInterface $promotion, string $ruleName): void
{
$this->iWantToModifyAPromotion($promotion);
GSadee marked this conversation as resolved.
Show resolved Hide resolved
$this->updatePage->saveChanges();

Assert::true($this->updatePage->hasRule($ruleName));
}

/**
* @Then the :promotion promotion should not have any rule configured
*/
public function thePromotionShouldNotHaveAnyRuleConfigured(PromotionInterface $promotion): void
{
$this->iWantToModifyAPromotion($promotion);

Assert::false($this->updatePage->hasAnyRule());
}

/**
* @param string $element
* @param string $expectedMessage
Expand Down
9 changes: 0 additions & 9 deletions src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,6 @@ public function iChangeItsParentTaxonTo(TaxonInterface $taxon)
$this->updatePage->chooseParent($taxon);
}

/**
* @When I delete taxon named :name
*/
public function iDeleteTaxonNamed($name)
{
$this->createPage->open();
$this->createPage->deleteTaxonOnPageByName($name);
}

/**
* @When I add it
* @When I try to add it
Expand Down
56 changes: 56 additions & 0 deletions src/Sylius/Behat/Context/Ui/Admin/RemovingTaxonContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* 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\Context\Ui\Admin;

use Behat\Behat\Context\Context;
use Sylius\Behat\NotificationType;
use Sylius\Behat\Page\Admin\Taxon\CreatePageInterface;
use Sylius\Behat\Service\NotificationCheckerInterface;
use Sylius\Component\Core\Model\PromotionInterface;

final class RemovingTaxonContext implements Context
{
/** @var CreatePageInterface */
private $createPage;

/** @var NotificationCheckerInterface */
private $notificationChecker;

public function __construct(CreatePageInterface $createPage, NotificationCheckerInterface $notificationChecker)
{
$this->createPage = $createPage;
$this->notificationChecker = $notificationChecker;
}

/**
* @When I remove taxon named :name
* @When I delete taxon named :name
*/
public function iRemoveTaxonNamed(string $name): void
{
$this->createPage->open();
$this->createPage->deleteTaxonOnPageByName($name);
}

/**
* @Then I should be notified that :promotion promotion has been updated
*/
public function iShouldBeNotifiedThatPromotionHasBeenUpdated(PromotionInterface $promotion): void
{
$this->notificationChecker->checkNotification(
sprintf('Some rules of the promotions with codes %s have been updated.', $promotion->getCode()),
NotificationType::info()
);
}
}
32 changes: 10 additions & 22 deletions src/Sylius/Behat/NotificationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,32 @@ final class NotificationType
/** @var array */
private static $types = [];

/**
* @param string $value
*/
private function __construct($value)
private function __construct(string $value)
{
$this->value = $value;
}

/**
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->value;
}

/**
* @return NotificationType
*/
public static function failure()
public static function failure(): NotificationType
{
return static::getTyped('failure');
}

/**
* @return NotificationType
*/
public static function success()
public static function success(): NotificationType
{
return static::getTyped('success');
}

/**
* @param string $type
*
* @return NotificationType
*/
private static function getTyped($type)
public static function info(): NotificationType
{
return static::getTyped('info');
}

private static function getTyped(string $type): NotificationType
{
if (!isset(static::$types[$type])) {
static::$types[$type] = new self($type);
Expand Down
24 changes: 24 additions & 0 deletions src/Sylius/Behat/Page/Admin/Promotion/UpdatePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ public function hasEndsAt(\DateTimeInterface $dateTime): bool
&& $this->getElement('ends_at_time')->getValue() === date('H:i', $timestamp);
}

public function hasAnyRule(): bool
{
$items = $this->getElement('rules')->findAll('css', 'div[data-form-collection="item"]');

return 0 < count($items);
}

public function hasRule(string $name): bool
{
$items = $this->getElement('rules')->findAll('css', 'div[data-form-collection="item"]');

foreach ($items as $item) {
$selectedOption = $item->find('css', 'option[selected="selected"]');

/** @var NodeElement $selectedOption */
if ($selectedOption->getText() === $name) {
return true;
}
}

return false;
}

protected function getCodeElement(): NodeElement
{
return $this->getElement('code');
Expand All @@ -108,6 +131,7 @@ protected function getDefinedElements(): array
'ends_at_time' => '#sylius_promotion_endsAt_time',
'exclusive' => '#sylius_promotion_exclusive',
'name' => '#sylius_promotion_name',
'rules' => '#rules',
'starts_at' => '#sylius_promotion_startsAt',
'starts_at_date' => '#sylius_promotion_startsAt_date',
'starts_at_time' => '#sylius_promotion_startsAt_time',
Expand Down
4 changes: 4 additions & 0 deletions src/Sylius/Behat/Page/Admin/Promotion/UpdatePageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ public function setEndsAt(\DateTimeInterface $dateTime): void;
public function hasStartsAt(\DateTimeInterface $dateTime): bool;

public function hasEndsAt(\DateTimeInterface $dateTime): bool;

public function hasAnyRule(): bool;

public function hasRule(string $name): bool;
}
6 changes: 6 additions & 0 deletions src/Sylius/Behat/Resources/config/services/contexts/ui.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@
<tag name="fob.context_service" />
</service>

<service id="sylius.behat.context.ui.admin.removing_taxons" class="Sylius\Behat\Context\Ui\Admin\RemovingTaxonContext">
<argument type="service" id="sylius.behat.page.admin.taxon.create" />
<argument type="service" id="sylius.behat.notification_checker" />
<tag name="fob.context_service" />
</service>

<service id="sylius.behat.context.ui.channel" class="Sylius\Behat\Context\Ui\ChannelContext">
<argument type="service" id="sylius.behat.shared_storage" />
<argument type="service" id="sylius.behat.channel_context_setter" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ default:
- sylius.behat.context.hook.doctrine_orm

- sylius.behat.context.transform.address
- sylius.behat.context.transform.channel
- sylius.behat.context.transform.customer
- sylius.behat.context.transform.date_time
- sylius.behat.context.transform.lexical
Expand All @@ -31,6 +32,7 @@ default:
- sylius.behat.context.setup.taxonomy

- sylius.behat.context.ui.admin.managing_promotions
- sylius.behat.context.ui.admin.removing_taxons
- sylius.behat.context.ui.admin.notification
filters:
tags: "@managing_promotions && @ui"
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ default:
- sylius.behat.context.setup.taxonomy

- sylius.behat.context.ui.admin.managing_taxons
- sylius.behat.context.ui.admin.removing_taxons
- sylius.behat.context.ui.admin.notification
filters:
tags: "@managing_taxons && @ui"
Loading