Skip to content

Commit

Permalink
[API][Admin] Updating coupons
Browse files Browse the repository at this point in the history
  • Loading branch information
NoResponseMate committed Nov 17, 2023
1 parent 4ddf7cc commit 62ad80d
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 11 deletions.
18 changes: 13 additions & 5 deletions features/promotion/managing_coupons/editing_coupon.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,39 @@ Feature: Editing promotion coupon
And the store has promotion "Christmas sale" with coupon "SANTA2016"
And I am logged in as an administrator

@ui
@ui @api
Scenario: Changing coupon expires date
When I want to modify the "SANTA2016" coupon for this promotion
And I change expires date to "21.05.2019"
And I change its expiration date to "21.05.2019"
And I save my changes
Then I should be notified that it has been successfully edited
And this coupon should be valid until "21.05.2019"

@ui
@ui @api
Scenario: Changing coupons usage limit
When I want to modify the "SANTA2016" coupon for this promotion
And I change its usage limit to 50
And I save my changes
Then I should be notified that it has been successfully edited
And this coupon should have 50 usage limit

@ui
@ui @api
Scenario: Changing coupons per customer usage limit
When I want to modify the "SANTA2016" coupon for this promotion
And I change its per customer usage limit to 20
And I save my changes
Then I should be notified that it has been successfully edited
And this coupon should have 20 per customer usage limit

@ui
@ui @api
Scenario: Changing whether it can be reused from cancelled orders
When I want to modify the "SANTA2016" coupon for this promotion
And I make it not reusable from cancelled orders
And I save my changes
Then I should be notified that it has been successfully edited
And this coupon should not be reusable from cancelled orders

@ui @no-api
Scenario: Seeing a disabled code field when editing a coupon
When I want to modify the "SANTA2016" coupon for this promotion
Then the code field should be disabled
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public function iWantToCreateANewCouponForPromotion(PromotionInterface $promotio
);
}

/**
* @When /^I want to modify the ("[^"]+" coupon) for this promotion$/
*/
public function iWantToModifyTheCouponOfThisPromotion(PromotionCouponInterface $coupon): void
{
$this->client->buildUpdateRequest(Resources::PROMOTION_COUPONS, $coupon->getCode());
}

/**
* @When /^I delete ("[^"]+" coupon) related to this promotion$/
* @When /^I try to delete ("[^"]+" coupon) related to this promotion$/
Expand All @@ -76,6 +84,7 @@ public function iSpecifyItsCodeAs(string $code): void

/**
* @When I limit its usage to :times times
* @When I change its usage limit to :times
*/
public function iLimitItsUsageToTimes(int $times): void
{
Expand All @@ -84,6 +93,7 @@ public function iLimitItsUsageToTimes(int $times): void

/**
* @When I limit its per customer usage to :times times
* @When I change its per customer usage limit to :times
*/
public function iLimitItsPerCustomerUsageToTimes(int $times): void
{
Expand All @@ -92,12 +102,21 @@ public function iLimitItsPerCustomerUsageToTimes(int $times): void

/**
* @When I make it valid until :date
* @When I change its expiration date to :date
*/
public function iMakeItValidUntil(\DateTime $date): void
{
$this->client->addRequestData('expiresAt', $date->format('d-m-Y'));
}

/**
* @When I make it not reusable from cancelled orders
*/
public function iMakeItNotReusableFromCancelledOrders(): void
{
$this->client->addRequestData('reusableFromCancelledOrders', false);
}

/**
* @When I add it
*/
Expand Down Expand Up @@ -204,6 +223,55 @@ public function iShouldBeNotifiedThatItHasBeenSuccessfullyCreated(): void
);
}

/**
* @Then this coupon should be valid until :date
*/
public function thisCouponShouldBeValidUntil(\DateTime $date): void
{
$actualDate = \DateTime::createFromFormat(
'Y-m-d h:i:s',
$this->responseChecker->getValue($this->client->getLastResponse(), 'expiresAt'),
);

Assert::same(
$actualDate->format('Y-m-d'),
$date->format('Y-m-d'),
);
}

/**
* @Then this coupon should have :limit usage limit
*/
public function thisCouponShouldHaveUsageLimit(int $limit): void
{
Assert::same(
$this->responseChecker->getValue($this->client->getLastResponse(), 'usageLimit'),
$limit,
);
}

/**
* @Then this coupon should have :limit per customer usage limit
*/
public function thisCouponShouldHavePerCustomerUsageLimit(int $limit): void
{
Assert::same(
$this->responseChecker->getValue($this->client->getLastResponse(), 'perCustomerUsageLimit'),
$limit,
);
}

/**
* @Then this coupon should not be reusable from cancelled orders
*/
public function thisCouponShouldNotBeReusableFromCancelledOrders(): void
{
Assert::false($this->responseChecker->getValue(
$this->client->getLastResponse(),
'reusableFromCancelledOrders',
));
}

/**
* @Then I should be notified that it has been successfully deleted
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ public function iChangeItsPerCustomerUsageLimitTo(int $limit)
$this->updatePage->setCustomerUsageLimit($limit);
}

/**
* @When I make it not reusable from cancelled orders
*/
public function iMakeItReusableFromCancelledOrders(): void
{
$this->updatePage->toggleReusableFromCancelledOrders(false);
}

/**
* @When I make it valid until :date
*/
Expand All @@ -173,9 +181,9 @@ public function iMakeItValidUntil(\DateTimeInterface $date)
}

/**
* @When I change expires date to :date
* @When I change its expiration date to :date
*/
public function iChangeExpiresDateTo(\DateTimeInterface $date)
public function iChangeItsExpirationDateTo(\DateTimeInterface $date)
{
$this->updatePage->setExpiresAt($date);
}
Expand Down Expand Up @@ -323,9 +331,9 @@ public function thereShouldBeCouponWithCode($code)
/**
* @Then this coupon should be valid until :date
*/
public function thisCouponShouldBeValidUntil(\DateTimeInterface $date)
public function thisCouponShouldBeValidUntil(\DateTime $date)
{
Assert::true($this->indexPage->isSingleResourceOnPage(['expiresAt' => date('d-m-Y', $date->getTimestamp())]));
Assert::true($this->indexPage->isSingleResourceOnPage(['expiresAt' => $date->format('d-m-Y')]));
}

/**
Expand All @@ -352,6 +360,16 @@ public function thisCouponShouldHavePerCustomerUsageLimit($limit)
Assert::true($this->indexPage->isSingleResourceOnPage(['perCustomerUsageLimit' => $limit]));
}

/**
* @Then /^(this coupon) should not be reusable from cancelled orders$/
*/
public function thisCouponShouldBeReusableFromCancelledOrders(PromotionCouponInterface $coupon): void
{
$this->updatePage->open(['id' => $coupon->getId(), 'promotionId' => $coupon->getPromotion()->getId()]);

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

/**
* @Then the code field should be disabled
*/
Expand Down
16 changes: 16 additions & 0 deletions src/Sylius/Behat/Page/Admin/PromotionCoupon/UpdatePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Behat\Mink\Element\NodeElement;
use Sylius\Behat\Behaviour\ChecksCodeImmutability;
use Sylius\Behat\Page\Admin\Crud\UpdatePage as BaseUpdatePage;
use Webmozart\Assert\Assert;

class UpdatePage extends BaseUpdatePage implements UpdatePageInterface
{
Expand All @@ -38,6 +39,20 @@ public function setUsageLimit(string $limit): void
$this->getDocument()->fillField('Usage limit', $limit);
}

public function isReusableFromCancelledOrders(): bool
{
return $this->getElement('reusable_from_cancelled_orders')->isChecked();
}

public function toggleReusableFromCancelledOrders(bool $reusable): void
{
$toggle = $this->getElement('reusable_from_cancelled_orders');

Assert::notSame($toggle->isChecked(), $reusable);

$reusable ? $toggle->check() : $toggle->uncheck();
}

protected function getCodeElement(): NodeElement
{
return $this->getElement('code');
Expand All @@ -50,6 +65,7 @@ protected function getDefinedElements(): array
'expires_at' => '#sylius_promotion_coupon_expiresAt',
'usage_limit' => '#sylius_promotion_coupon_usageLimit',
'per_customer_usage_limit' => '#sylius_promotion_coupon_perCustomerUsageLimit',
'reusable_from_cancelled_orders' => '#sylius_promotion_coupon_reusableFromCancelledOrders',
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public function setCustomerUsageLimit(int $limit): void;
public function setExpiresAt(\DateTimeInterface $date): void;

public function setUsageLimit(string $limit): void;

public function isReusableFromCancelledOrders(): bool;

public function toggleReusableFromCancelledOrders(bool $reusable): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ default:
- sylius.behat.context.setup.admin_api_security

- sylius.behat.context.api.admin.managing_promotion_coupons
- sylius.behat.context.api.admin.response
- sylius.behat.context.api.admin.save

filters:
tags: "@managing_promotion_coupons&&@api"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@
</attribute>
</itemOperation>

<itemOperation name="admin_put">
<attribute name="method">PUT</attribute>
<attribute name="normalization_context">
<attribute name="groups">admin:promotion_coupon:read</attribute>
</attribute>
<attribute name="denormalization_context">
<attribute name="groups">admin:promotion_coupon:update</attribute>
</attribute>
</itemOperation>

<itemOperation name="admin_delete">
<attribute name="method">DELETE</attribute>
</itemOperation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
<attribute name="usageLimit">
<group>admin:promotion_coupon:read</group>
<group>admin:promotion_coupon:create</group>
<group>admin:promotion_coupon:update</group>
</attribute>

<attribute name="perCustomerUsageLimit">
<group>admin:promotion_coupon:read</group>
<group>admin:promotion_coupon:create</group>
<group>admin:promotion_coupon:update</group>
</attribute>

<attribute name="used">
Expand All @@ -46,6 +48,7 @@
<attribute name="reusableFromCancelledOrders">
<group>admin:promotion_coupon:read</group>
<group>admin:promotion_coupon:create</group>
<group>admin:promotion_coupon:update</group>
</attribute>

<attribute name="promotion">
Expand All @@ -56,6 +59,7 @@
<attribute name="expiresAt">
<group>admin:promotion_coupon:read</group>
<group>admin:promotion_coupon:create</group>
<group>admin:promotion_coupon:update</group>
</attribute>
</class>
</serializer>
30 changes: 29 additions & 1 deletion tests/Api/Admin/PromotionCouponsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function it_creates_a_promotion_coupon(): void
'code' => 'XYZ3',
'usageLimit' => 100,
'perCustomerUsageLimit' => 3,
'reusableFromCancelledOrders' => true,
'reusableFromCancelledOrders' => false,
'expiresAt' => '23-12-2023',
'promotion' => 'api/v2/admin/promotions/' . $promotion->getCode(),
], JSON_THROW_ON_ERROR)
Expand All @@ -90,6 +90,34 @@ public function it_creates_a_promotion_coupon(): void
);
}

/** @test */
public function it_updates_a_promotion_coupon(): void
{
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel.yaml', 'promotion.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);

/** @var PromotionCouponInterface $coupon */
$coupon = $fixtures['promotion_1_off_coupon_1'];

$this->client->request(
method: 'PUT',
uri: '/api/v2/admin/promotion-coupons/' . $coupon->getCode(),
server: $header,
content: json_encode([
'usageLimit' => 1000,
'perCustomerUsageLimit' => 5,
'reusableFromCancelledOrders' => false,
'expiresAt' => '2020-01-01 12:00:00',
], JSON_THROW_ON_ERROR)
);

$this->assertResponse(
$this->client->getResponse(),
'admin/promotion_coupon/put_promotion_coupon_response',
Response::HTTP_OK,
);
}

/** @test */
public function it_removes_a_promotion_coupon(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"usageLimit": 100,
"perCustomerUsageLimit": 3,
"used": 0,
"reusableFromCancelledOrders": true,
"reusableFromCancelledOrders": false,
"promotion": "\/api\/v2\/admin\/promotions\/dollar_off",
"createdAt": @date@,
"updatedAt": @date@,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"@context": "\/api\/v2\/contexts\/PromotionCoupon",
"@id": "\/api\/v2\/admin\/promotion-coupons\/XYZ1",
"@type": "PromotionCoupon",
"code": "XYZ1",
"usageLimit": 1000,
"perCustomerUsageLimit": 5,
"used": 1,
"reusableFromCancelledOrders": false,
"promotion": "\/api\/v2\/admin\/promotions\/dollar_off",
"createdAt": @date@,
"updatedAt": @date@,
"expiresAt": "2020-01-01 12:00:00"
}

0 comments on commit 62ad80d

Please sign in to comment.