From 76da7cbeb33d5dcf68459d828fa041c1af3efd76 Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Wed, 11 Jan 2017 13:40:31 +0100 Subject: [PATCH] [Payum][Admin][Behat] Validation and paypal improvements --- .../payment_method_validation.feature | 24 ++++--- .../Behat/Context/Setup/PaymentContext.php | 16 +++-- .../Admin/ManagingPaymentMethodsContext.php | 22 ++++++ .../Page/Admin/PaymentMethod/CreatePage.php | 2 + .../PaypalGatewayConfigurationTransformer.php | 46 +++++++++++++ .../Type/PaypalGatewayConfigurationType.php | 27 ++++++++ .../config/validation/GatewayConfig.xml | 28 ++++++++ .../config/validation/PaymentMethod.xml | 20 ++++++ .../Resources/translations/validators.en.yml | 12 ++++ ...palGatewayConfigurationTransformerSpec.php | 67 +++++++++++++++++++ 10 files changed, 252 insertions(+), 12 deletions(-) create mode 100644 src/Sylius/Bundle/PayumBundle/Form/DataTransformer/PaypalGatewayConfigurationTransformer.php create mode 100644 src/Sylius/Bundle/PayumBundle/Resources/config/validation/GatewayConfig.xml create mode 100644 src/Sylius/Bundle/PayumBundle/Resources/config/validation/PaymentMethod.xml create mode 100644 src/Sylius/Bundle/PayumBundle/Resources/translations/validators.en.yml create mode 100644 src/Sylius/Bundle/PayumBundle/spec/Form/DataTransformer/PaypalGatewayConfigurationTransformerSpec.php diff --git a/features/payment/managing_payment_methods/payment_method_validation.feature b/features/payment/managing_payment_methods/payment_method_validation.feature index 14ad5b598d..48ddd2d6e2 100644 --- a/features/payment/managing_payment_methods/payment_method_validation.feature +++ b/features/payment/managing_payment_methods/payment_method_validation.feature @@ -11,34 +11,42 @@ Feature: Payment method validation @ui Scenario: Trying to add a new payment method without specifying its code - Given I want to create a new payment method + Given I want to create a new payment method with "Paypal Express Checkout" gateway factory When I name it "Paypal Express Checkout" in "English (United States)" But I do not specify its code - And I choose "Paypal Express Checkout" gateway And I add it Then I should be notified that code is required And the payment method with name "Paypal Express Checkout" should not be added @ui Scenario: Trying to add a new payment method without specifying its name - Given I want to create a new payment method + Given I want to create a new payment method with "Paypal Express Checkout" gateway factory When I specify its code as "PEC" - And I choose "Paypal Express Checkout" gateway But I do not name it And I add it Then I should be notified that name is required And the payment method with code "PEC" should not be added @ui - Scenario: Trying to add a new payment method without specifying required configuration - Given I want to create a new payment method + Scenario: Trying to add a new paypal payment method without specifying required configuration + Given I want to create a new payment method with "Paypal Express Checkout" gateway factory When I name it "Paypal Express Checkout" in "English (United States)" And I specify its code as "PEC" - And I choose "Paypal Express Checkout" gateway And I configure it for username "TEST" with "TEST" signature But I do not specify configuration password And I add it - Then I should be notified that password is required + Then I should be notified that I must specify paypal password + And the payment method with code "PEC" should not be added + + @ui + Scenario: Trying to add a new paypal payment method with wrong name + Given I want to create a new payment method with "Paypal Express Checkout" gateway factory + When I name it "Paypal Express Checkout" in "English (United States)" + And I specify its code as "PEC" + And I configure it for username "TEST", with "TEST" password and "TEST" signature + And I name the gateway "Paypal express checkout" + And I add it + Then I should be notified that gateway name should contain only letters and underscores And the payment method with code "PEC" should not be added @ui diff --git a/src/Sylius/Behat/Context/Setup/PaymentContext.php b/src/Sylius/Behat/Context/Setup/PaymentContext.php index 29961f40d7..68e3986ae2 100644 --- a/src/Sylius/Behat/Context/Setup/PaymentContext.php +++ b/src/Sylius/Behat/Context/Setup/PaymentContext.php @@ -124,12 +124,18 @@ final class PaymentContext implements Context $password, $signature ) { - $paymentMethod->getGatewayConfig()->setConfig([ + $config = [ 'username' => $username, 'password' => $password, 'signature' => $signature, 'sandbox' => true, - ]); + ]; + + if ('paypal_express_checkout' === $paymentMethod->getGatewayConfig()->getFactoryName()) { + $config = array_merge($config, ['payum.http_client' => '@sylius.payum.http_client']); + } + + $paymentMethod->getGatewayConfig()->setConfig($config); $this->objectManager->flush(); } @@ -196,9 +202,11 @@ final class PaymentContext implements Context $addForCurrentChannel = true, $position = null ) { + $gatewayFactory = array_search($gatewayFactory, $this->gatewayFactories); + /** @var PaymentMethodInterface $paymentMethod */ - $paymentMethod = $this->paymentMethodFactory->createWithGateway(array_search($gatewayFactory, $this->gatewayFactories)); - $paymentMethod->getGatewayConfig()->setGatewayName(array_search($gatewayFactory, $this->gatewayFactories)); + $paymentMethod = $this->paymentMethodFactory->createWithGateway($gatewayFactory); + $paymentMethod->getGatewayConfig()->setGatewayName($gatewayFactory); $paymentMethod->setName(ucfirst($name)); $paymentMethod->setCode($code); $paymentMethod->setPosition($position); diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingPaymentMethodsContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingPaymentMethodsContext.php index b539e55413..4777543454 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingPaymentMethodsContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingPaymentMethodsContext.php @@ -295,6 +295,28 @@ final class ManagingPaymentMethodsContext implements Context $this->assertFieldValidationMessage($element, sprintf('Please enter payment method %s.', $element)); } + /** + * @Then I should be notified that I must specify paypal :element + */ + public function iShouldBeNotifiedThatIMustSpecifyPaypal($element) + { + Assert::same( + $this->createPage->getValidationMessage('paypal_'.$element), + sprintf('Please enter paypal %s.', $element) + ); + } + + /** + * @Then I should be notified that gateway name should contain only letters and underscores + */ + public function iShouldBeNotifiedThatGatewayNameShouldContainOnlyLettersAndUnderscores() + { + Assert::same( + $this->createPage->getValidationMessage('gateway_name'), + 'Gateway name should contain only letters and underscores.' + ); + } + /** * @Then the payment method with :element :value should not be added */ diff --git a/src/Sylius/Behat/Page/Admin/PaymentMethod/CreatePage.php b/src/Sylius/Behat/Page/Admin/PaymentMethod/CreatePage.php index 1b2b58ed6e..57d0dc4a1a 100644 --- a/src/Sylius/Behat/Page/Admin/PaymentMethod/CreatePage.php +++ b/src/Sylius/Behat/Page/Admin/PaymentMethod/CreatePage.php @@ -128,7 +128,9 @@ class CreatePage extends BaseCreatePage implements CreatePageInterface 'code' => '#sylius_payment_method_code', 'enabled' => '#sylius_payment_method_enabled', 'gateway' => '#sylius_payment_method_gateway', + 'gateway_name' => '#sylius_payment_method_gatewayConfig_gatewayName', 'name' => '#sylius_payment_method_translations_en_US_name', + 'paypal_password' => '#sylius_payment_method_gatewayConfig_config_password', ]); } } diff --git a/src/Sylius/Bundle/PayumBundle/Form/DataTransformer/PaypalGatewayConfigurationTransformer.php b/src/Sylius/Bundle/PayumBundle/Form/DataTransformer/PaypalGatewayConfigurationTransformer.php new file mode 100644 index 0000000000..55926c4cf5 --- /dev/null +++ b/src/Sylius/Bundle/PayumBundle/Form/DataTransformer/PaypalGatewayConfigurationTransformer.php @@ -0,0 +1,46 @@ + + */ +final class PaypalGatewayConfigurationTransformer implements DataTransformerInterface +{ + /** + * {@inheritdoc} + */ + public function transform($value) + { + if (empty($value)) { + return $value; + } + + $value['payum_http_client'] = $value['payum.http_client']; + unset($value['payum.http_client']); + + return $value; + } + + /** + * {@inheritdoc} + */ + public function reverseTransform($value) + { + $value['payum.http_client'] = $value['payum_http_client']; + unset($value['payum_http_client']); + + return $value; + } +} \ No newline at end of file diff --git a/src/Sylius/Bundle/PayumBundle/Form/Type/PaypalGatewayConfigurationType.php b/src/Sylius/Bundle/PayumBundle/Form/Type/PaypalGatewayConfigurationType.php index 602e873442..907d8a4565 100755 --- a/src/Sylius/Bundle/PayumBundle/Form/Type/PaypalGatewayConfigurationType.php +++ b/src/Sylius/Bundle/PayumBundle/Form/Type/PaypalGatewayConfigurationType.php @@ -11,10 +11,13 @@ namespace Sylius\Bundle\PayumBundle\Form\Type; +use Sylius\Bundle\PayumBundle\Form\DataTransformer\PaypalGatewayConfigurationTransformer; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\PasswordType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Validator\Constraints\NotBlank; /** * @author Mateusz Zalewski @@ -29,13 +32,37 @@ final class PaypalGatewayConfigurationType extends AbstractType implements Gatew $builder ->add('username', TextType::class, [ 'label' => 'sylius.form.gateway_configuration.paypal.username', + 'constraints' => [ + new NotBlank([ + 'message' => 'sylius.gateway_config.paypal.username.not_blank', + 'groups' => 'sylius', + ]) + ], ]) ->add('password', PasswordType::class, [ 'label' => 'sylius.form.gateway_configuration.paypal.password', + 'constraints' => [ + new NotBlank([ + 'message' => 'sylius.gateway_config.paypal.password.not_blank', + 'groups' => 'sylius', + ]) + ], ]) ->add('signature', TextType::class, [ 'label' => 'sylius.form.gateway_configuration.paypal.signature', + 'constraints' => [ + new NotBlank([ + 'message' => 'sylius.gateway_config.paypal.signature.not_blank', + 'groups' => 'sylius', + ]) + ], ]) + ->add('payum_http_client', HiddenType::class, [ + 'label' => false, + // Hardcoded, as its the only payum client used now in sylius, could be extended to support many clients + 'data' => '@sylius.payum.http_client', + ]) + ->addModelTransformer(new PaypalGatewayConfigurationTransformer()) ; } } diff --git a/src/Sylius/Bundle/PayumBundle/Resources/config/validation/GatewayConfig.xml b/src/Sylius/Bundle/PayumBundle/Resources/config/validation/GatewayConfig.xml new file mode 100644 index 0000000000..ec8a1d4829 --- /dev/null +++ b/src/Sylius/Bundle/PayumBundle/Resources/config/validation/GatewayConfig.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + diff --git a/src/Sylius/Bundle/PayumBundle/Resources/config/validation/PaymentMethod.xml b/src/Sylius/Bundle/PayumBundle/Resources/config/validation/PaymentMethod.xml new file mode 100644 index 0000000000..f22baacada --- /dev/null +++ b/src/Sylius/Bundle/PayumBundle/Resources/config/validation/PaymentMethod.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + diff --git a/src/Sylius/Bundle/PayumBundle/Resources/translations/validators.en.yml b/src/Sylius/Bundle/PayumBundle/Resources/translations/validators.en.yml new file mode 100644 index 0000000000..ce44c9a040 --- /dev/null +++ b/src/Sylius/Bundle/PayumBundle/Resources/translations/validators.en.yml @@ -0,0 +1,12 @@ +sylius: + gateway_config: + gateway_name: + not_blank: Please enter gateway name. + regex: Gateway name should contain only letters and underscores. + paypal: + password: + not_blank: Please enter paypal password. + signature: + not_blank: Please enter paypal signature. + username: + not_blank: Please enter paypal username. \ No newline at end of file diff --git a/src/Sylius/Bundle/PayumBundle/spec/Form/DataTransformer/PaypalGatewayConfigurationTransformerSpec.php b/src/Sylius/Bundle/PayumBundle/spec/Form/DataTransformer/PaypalGatewayConfigurationTransformerSpec.php new file mode 100644 index 0000000000..d546768b46 --- /dev/null +++ b/src/Sylius/Bundle/PayumBundle/spec/Form/DataTransformer/PaypalGatewayConfigurationTransformerSpec.php @@ -0,0 +1,67 @@ + + */ +final class PaypalGatewayConfigurationTransformerSpec extends ObjectBehavior +{ + function it_is_initializable() + { + $this->shouldHaveType(PaypalGatewayConfigurationTransformer::class); + } + + function it_implements_data_transformer_interface() + { + $this->shouldImplement(DataTransformerInterface::class); + } + + function it_changes_paypal_gateway_configuration_to_be_handled_well_by_payum() + { + $this->reverseTransform([ + 'username' => 'TEST', + 'password' => 'TEST', + 'signature' => 'TEST', + 'payum_http_client' => '@sylius.payum.http_client' + ])->shouldReturn([ + 'username' => 'TEST', + 'password' => 'TEST', + 'signature' => 'TEST', + 'payum.http_client' => '@sylius.payum.http_client' + ]); + } + + function it_changes_stored_configuration_to_be_properly_handled_by_form() + { + $this->transform([ + 'username' => 'TEST', + 'password' => 'TEST', + 'signature' => 'TEST', + 'payum.http_client' => '@sylius.payum.http_client' + ])->shouldReturn([ + 'username' => 'TEST', + 'password' => 'TEST', + 'signature' => 'TEST', + 'payum_http_client' => '@sylius.payum.http_client' + ]); + } + + function it_does_not_transform_empty_array() + { + $this->transform([])->shouldReturn([]); + } +}