Merge pull request #69 from skowi/fix/user-register-checkout

User register in checkout on security step
This commit is contained in:
Paweł Jędrzejewski 2013-05-14 02:55:50 -07:00
commit cb715d6252
5 changed files with 163 additions and 12 deletions

View file

@ -16,11 +16,20 @@ Feature: Checkout security
| username | password | enabled |
| john | foo | yes |
| rick | bar | yes |
And the following zones are defined:
| name | type | members |
| UK | country | United Kingdom |
And the following shipping methods exist:
| zone | name |
| UK | DHL Express |
And the following payment methods exist:
| name | gateway | enabled |
| Credit Card | stripe | yes |
And I added product "PHP Top" to cart
And I go to the checkout start page
Scenario: Trying to sign in with bad credentials
during the checkout
Given I added product "PHP Top" to cart
And I go to the checkout start page
When I fill in the following:
| Username | john |
| Password | habababa |
@ -28,8 +37,6 @@ Feature: Checkout security
Then I should see "Bad credentials"
Scenario: Signing in during the checkout
Given I added product "PHP Top" to cart
And I go to the checkout start page
When I fill in the following:
| Username | john |
| Password | foo |
@ -37,12 +44,27 @@ Feature: Checkout security
Then I should be redirected to the checkout addressing step
Scenario: Creating account during the checkout
Given I added product "PHP Top" to cart
And I go to the checkout start page
When I fill in the following:
| Username | mike |
| Email | mike@example.com |
| Password | mikepass |
| Verification | mikepass |
| fos_user_registration_form_username | mike |
| fos_user_registration_form_email | mike@example.com |
| fos_user_registration_form_plainPassword_first | mikepass |
| fos_user_registration_form_plainPassword_second | mikepass |
And I press "Register"
Then I should be redirected to the checkout addressing step
Scenario: Creating account during the whole checkout
When I fill in the following:
| fos_user_registration_form_username | mike |
| fos_user_registration_form_email | mike@example.com |
| fos_user_registration_form_plainPassword_first | mikepass |
| fos_user_registration_form_plainPassword_second | mikepass |
And I press "Register"
And I fill in the shipping address to United Kingdom
And I press "Continue"
And I select the "DHL Express" radio button
And I press "Continue"
And I select the "Credit Card" radio button
And I press "Continue"
And I click "Place order"
Then I should be on the store homepage
And I should see "Thank you for your order!"

View file

@ -30,4 +30,4 @@ Feature: User registration
| Verification | foo |
And I press "Register"
Then I should be on registration page
And I should see "The entered passwords don't match"
And I should see "The entered passwords don't match"

View file

@ -13,6 +13,8 @@ namespace Sylius\Bundle\CoreBundle\Checkout\Step;
use Sylius\Bundle\FlowBundle\Process\Context\ProcessContextInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Security step.
@ -44,7 +46,19 @@ class SecurityStep extends CheckoutStep
*/
public function forwardAction(ProcessContextInterface $context)
{
return $this->complete();
$form = $this->getRegistrationForm();
$request = $this->getRequest();
if ($request->isMethod('POST') && $form->bind($request)->isValid()) {
$user = $form->getData();
$this->saveUser($user);
$this->authenticateUser($user);
return $this->complete();
}
return $this->renderStep($context, $this->getRegistrationForm());
}
/**
@ -81,4 +95,22 @@ class SecurityStep extends CheckoutStep
$this->get('session')->set('_security.'.$providerKey.'.target_path', $url);
}
/**
* @param \Sylius\Bundle\CoreBundle\Entity\User $user
*/
private function saveUser($user)
{
$user->setEnabled(true);
$this->get('fos_user.user_manager')->updateUser($user);
}
private function authenticateUser(UserInterface $user)
{
$providerKey = $this->container->getParameter('fos_user.firewall_name');
$token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
$this->container->get('security.context')->setToken($token);
}
}

View file

@ -0,0 +1,96 @@
<?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.
*/
namespace spec\Sylius\Bundle\CoreBundle\Checkout\Step;
use PHPSpec2\ObjectBehavior;
/**
* Checkout security step spec.
*/
class SecurityStep extends ObjectBehavior
{
/**
* @param Symfony\Component\DependencyInjection\ContainerInterface $container
* @param Symfony\Component\Form\Form $form
* @param Symfony\Component\HttpFoundation\Request $request
* @param Symfony\Bundle\FrameworkBundle\Templating\EngineInterface $templating
* @param FOS\UserBundle\Model\UserManagerInterface $userManager
* @param Symfony\Component\Security\SecurityContextInterface $security
*/
function let($container, $form, $request, $templating, $userManager, $security)
{
$container->get('fos_user.registration.form')->willReturn($form);
$container->get('request')->willReturn($request);
$container->get('templating')->willReturn($templating);
$container->get('fos_user.user_manager')->willReturn($userManager);
$container->get('security.context')->willReturn($security);
$this->setContainer($container);
}
function it_is_initializable()
{
$this->shouldHaveType('Sylius\Bundle\CoreBundle\Checkout\Step\SecurityStep');
}
function it_extends_checkout_step()
{
$this->shouldHaveType('Sylius\Bundle\CoreBundle\Checkout\Step\SecurityStep');
}
/**
* @param Sylius\Bundle\FlowBundle\Process\Context\ProcessContextInterface $context
*/
function its_forwardAction_render_step_without_register($context, $templating, $request)
{
$request->isMethod('POST')->willReturn(false);
$templating->renderResponse(ANY_ARGUMENTS)->shouldBeCalled();
$this->forwardAction($context);
}
/**
* @param Sylius\Bundle\FlowBundle\Process\Context\ProcessContextInterface $context
*/
function its_forwardAction_render_step_with_wrong_register($context, $templating, $request, $form)
{
$request->isMethod('POST')->willReturn(true);
$form->bind($request)->willReturn($form);
$form->isValid()->willReturn(false);
$templating->renderResponse(ANY_ARGUMENTS)->shouldBeCalled();
$this->forwardAction($context);
}
/**
* @param Sylius\Bundle\FlowBundle\Process\Context\ProcessContextInterface $context
* @param Sylius\Bundle\CoreBundle\Entity\User $user
*/
function its_forwardAction_complete_and_register_new_user($context, $templating, $request, $form, $user, $userManager, $container, $security)
{
$request->isMethod('POST')->willReturn(true);
$form->bind($request)->willReturn($form);
$form->isValid()->willReturn(true);
$form->getData()->shouldBeCalled()->willReturn($user);
$user->getRoles()->willReturn(array());
$userManager->updateUser($user)->shouldBeCalled();
$container->getParameter('fos_user.firewall_name')->shouldBeCalled()->willReturn('provider_KEY');
$security->setToken(ANY_ARGUMENT)->shouldBeCalled();
$templating->renderResponse(ANY_ARGUMENTS)->shouldNotBeCalled();
$response = $this->forwardAction($context);
$response->shouldHaveType('Sylius\Bundle\FlowBundle\Process\Step\ActionResult');
}
}

View file

@ -13,6 +13,7 @@ namespace Sylius\Bundle\WebBundle\Behat;
use Behat\Behat\Context\Step;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Exception\ElementNotFoundException;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Exception\ExpectationException;