Skip to content

Commit

Permalink
Merge pull request #7227 from David-Guillot/fix/gh7211-should-not-be-…
Browse files Browse the repository at this point in the history
…able-to-delete-used-payment-method

Fixed #7211: now can't delete a used PaymentMethod
  • Loading branch information
michalmarcinkowski committed Jan 12, 2017
2 parents 7fa5baa + 280f6f7 commit 881b3b1
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
36 changes: 36 additions & 0 deletions app/migrations/Version20170110120125.php
@@ -0,0 +1,36 @@
<?php

namespace Sylius\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20170110120125 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE sylius_payment DROP FOREIGN KEY FK_D9191BD419883967');
$this->addSql('ALTER TABLE sylius_payment ADD CONSTRAINT FK_D9191BD419883967 FOREIGN KEY (method_id) REFERENCES sylius_payment_method (id)');
}

/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE sylius_payment DROP FOREIGN KEY FK_D9191BD419883967');
$this->addSql('ALTER TABLE sylius_payment ADD CONSTRAINT FK_D9191BD419883967 FOREIGN KEY (method_id) REFERENCES sylius_payment_method (id) ON DELETE SET NULL');
}
}
@@ -0,0 +1,21 @@
@managing_payment_methods
Feature: Prevent deletion of used payment method
In order to maintain proper order history
As an Administrator
I want to be prevented from deleting used payment method

Background:
Given the store operates on a single channel in "United States"
And the store has a product "PHP T-Shirt"
And the store allows shipping with "DHL Express"
And the store allows paying with "Cash on Delivery"
And there is a customer "john.doe@gmail.com" that placed an order "#00000022"
And the customer bought a single "PHP T-Shirt"
And the customer chose "DHL Express" shipping method to "United States" with "Cash on Delivery" payment
And I am logged in as an administrator

@ui
Scenario: Being unable to delete a payment method which is in use
When I try to delete the "Cash on Delivery" payment method
Then I should be notified that it is in use
And this payment method should still be in the registry
Expand Up @@ -12,9 +12,11 @@
namespace Sylius\Behat\Context\Ui\Admin;

use Behat\Behat\Context\Context;
use Sylius\Behat\NotificationType;
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
use Sylius\Behat\Page\Admin\PaymentMethod\CreatePageInterface;
use Sylius\Behat\Page\Admin\PaymentMethod\UpdatePageInterface;
use Sylius\Behat\Service\NotificationCheckerInterface;
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
use Sylius\Component\Payment\Model\PaymentMethodInterface;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -45,6 +47,11 @@ final class ManagingPaymentMethodsContext implements Context
*/
private $currentPageResolver;

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

/**
* @param CreatePageInterface $createPage
* @param IndexPageInterface $indexPage
Expand All @@ -55,12 +62,14 @@ public function __construct(
CreatePageInterface $createPage,
IndexPageInterface $indexPage,
UpdatePageInterface $updatePage,
CurrentPageResolverInterface $currentPageResolver
CurrentPageResolverInterface $currentPageResolver,
NotificationCheckerInterface $notificationChecker
) {
$this->createPage = $createPage;
$this->indexPage = $indexPage;
$this->updatePage = $updatePage;
$this->currentPageResolver = $currentPageResolver;
$this->notificationChecker = $notificationChecker;
}

/**
Expand Down Expand Up @@ -118,13 +127,22 @@ public function iSaveMyChanges()

/**
* @When I delete the :paymentMethod payment method
* @When I try to delete the :paymentMethod payment method
*/
public function iDeletePaymentMethod(PaymentMethodInterface $paymentMethod)
{
$this->indexPage->open();
$this->indexPage->deleteResourceOnPage(['code' => $paymentMethod->getCode(), 'name' => $paymentMethod->getName()]);
}

/**
* @Then I should be notified that it is in use
*/
public function iShouldBeNotifiedThatItIsInUse()
{
$this->notificationChecker->checkNotification('Cannot delete, the payment method is in use.', NotificationType::failure());
}

/**
* @When I choose :gatewayName gateway
*/
Expand Down Expand Up @@ -210,6 +228,14 @@ public function thePaymentMethodShouldAppearInTheRegistry($paymentMethodName)
);
}

/**
* @Given /^(this payment method) should still be in the registry$/
*/
public function thisPaymentMethodShouldStillBeInTheRegistry(PaymentMethodInterface $paymentMethod)
{
$this->thePaymentMethodShouldAppearInTheRegistry($paymentMethod->getName());
}

/**
* @Given I am browsing payment methods
* @When I browse payment methods
Expand Down
1 change: 1 addition & 0 deletions src/Sylius/Behat/Resources/config/services/contexts/ui.xml
Expand Up @@ -114,6 +114,7 @@
<argument type="service" id="sylius.behat.page.admin.payment_method.index" />
<argument type="service" id="sylius.behat.page.admin.payment_method.update" />
<argument type="service" id="sylius.behat.current_page_resolver" />
<argument type="service" id="sylius.behat.notification_checker" />
<tag name="fob.context_service" />
</service>

Expand Down
Expand Up @@ -8,13 +8,23 @@ default:
- sylius.behat.context.hook.doctrine_orm

- sylius.behat.context.setup.channel
- sylius.behat.context.setup.currency
- sylius.behat.context.setup.locale
- sylius.behat.context.setup.order
- sylius.behat.context.setup.payment
- sylius.behat.context.setup.product
- sylius.behat.context.setup.admin_security
- sylius.behat.context.setup.shipping
- sylius.behat.context.setup.user
- sylius.behat.context.setup.zone

- sylius.behat.context.transform.address
- sylius.behat.context.transform.customer
- sylius.behat.context.transform.locale
- sylius.behat.context.transform.payment
- sylius.behat.context.transform.product
- sylius.behat.context.transform.shared_storage
- sylius.behat.context.transform.shipping_method

- sylius.behat.context.ui.admin.managing_payment_methods
- sylius.behat.context.ui.admin.notification
Expand Down
Expand Up @@ -20,7 +20,7 @@
</id>

<many-to-one field="method" target-entity="Sylius\Component\Payment\Model\PaymentMethodInterface">
<join-column name="method_id" referenced-column-name="id" nullable="true" on-delete="SET NULL" />
<join-column name="method_id" referenced-column-name="id" nullable="true" />
</many-to-one>

<field name="currencyCode" column="currency_code" length="3" type="string" />
Expand Down

0 comments on commit 881b3b1

Please sign in to comment.