Adding new user

This commit is contained in:
Jeremy 2015-02-22 09:30:25 +01:00
parent c0d9eba07f
commit e4977b8a86
5 changed files with 181 additions and 2 deletions

View file

@ -6,9 +6,11 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Wallabag\CoreBundle\Entity\Config;
use Wallabag\CoreBundle\Entity\User;
use Wallabag\CoreBundle\Form\Type\ConfigType;
use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
use Wallabag\CoreBundle\Form\Type\UserType;
use Wallabag\CoreBundle\Form\Type\NewUserType;
class ConfigController extends Controller
{
@ -72,10 +74,28 @@ class ConfigController extends Controller
return $this->redirect($this->generateUrl('config'));
}
// handle adding new user
$newUser = new User();
$newUserForm = $this->createForm(new NewUserType(), $newUser);
$newUserForm->handleRequest($request);
if ($newUserForm->isValid()) {
$em->persist($newUser);
$em->flush();
$this->get('session')->getFlashBag()->add(
'notice',
sprintf('User "%s" added', $newUser->getUsername())
);
return $this->redirect($this->generateUrl('config'));
}
return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
'configForm' => $configForm->createView(),
'pwdForm' => $pwdForm->createView(),
'userForm' => $userForm->createView(),
'newUserForm' => $newUserForm->createView(),
));
}

View file

@ -23,7 +23,7 @@ class ChangePasswordType extends AbstractType
'constraints' => array(
new Constraints\Length(array(
'min' => 8,
'minMessage' => 'Password should by at least 6 chars long',
'minMessage' => 'Password should by at least 8 chars long',
)),
new Constraints\NotBlank(),
),

View file

@ -0,0 +1,40 @@
<?php
namespace Wallabag\CoreBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints;
class NewUserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', 'text')
->add('password', 'password', array(
'constraints' => array(
new Constraints\Length(array(
'min' => 8,
'minMessage' => 'Password should by at least 8 chars long',
)),
new Constraints\NotBlank(),
),
))
->add('email', 'text')
->add('save', 'submit')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Wallabag\CoreBundle\Entity\User',
));
}
public function getName()
{
return 'new_user';
}
}

View file

@ -102,4 +102,36 @@
{{ form_rest(pwdForm) }}
</form>
<h2>{% trans %}Add a user{% endtrans %}</h2>
<form action="{{ path('config') }}" method="post" {{ form_enctype(newUserForm) }}>
{{ form_errors(newUserForm) }}
<fieldset class="w500p inline">
<div class="row">
{{ form_label(newUserForm.username) }}
{{ form_errors(newUserForm.username) }}
{{ form_widget(newUserForm.username) }}
</div>
</fieldset>
<fieldset class="w500p inline">
<div class="row">
{{ form_label(newUserForm.password) }}
{{ form_errors(newUserForm.password) }}
{{ form_widget(newUserForm.password) }}
</div>
</fieldset>
<fieldset class="w500p inline">
<div class="row">
{{ form_label(newUserForm.email) }}
{{ form_errors(newUserForm.email) }}
{{ form_widget(newUserForm.email) }}
</div>
</fieldset>
{{ form_rest(newUserForm) }}
</form>
{% endblock %}

View file

@ -128,7 +128,7 @@ class ConfigControllerTest extends WallabagTestCase
'change_passwd[new_password][first]' => 'hop',
'change_passwd[new_password][second]' => 'hop',
),
'Password should by at least 6 chars long',
'Password should by at least',
),
);
}
@ -260,4 +260,91 @@ class ConfigControllerTest extends WallabagTestCase
$this->assertGreaterThan(1, $alert = $crawler->filter('div.flash-notice')->extract(array('_text')));
$this->assertContains('Information updated', $alert[0]);
}
public function dataForNewUserFailed()
{
return array(
array(
array(
'new_user[username]' => '',
'new_user[password]' => '',
'new_user[email]' => '',
),
'This value should not be blank.',
),
array(
array(
'new_user[username]' => 'ad',
'new_user[password]' => '',
'new_user[email]' => '',
),
'This value is too short.',
),
array(
array(
'new_user[username]' => 'wallace',
'new_user[password]' => '',
'new_user[email]' => 'test',
),
'This value is not a valid email address.',
),
array(
array(
'new_user[username]' => 'wallace',
'new_user[password]' => 'admin',
'new_user[email]' => 'wallace@wallace.me',
),
'Password should by at least',
),
);
}
/**
* @dataProvider dataForNewUserFailed
*/
public function testNewUserFailed($data, $expectedMessage)
{
$this->logInAs('admin');
$client = $this->getClient();
$crawler = $client->request('GET', '/config');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$form = $crawler->filter('button[id=new_user_save]')->form();
$crawler = $client->submit($form, $data);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
$this->assertContains($expectedMessage, $alert[0]);
}
public function testNewUserCreated()
{
$this->logInAs('admin');
$client = $this->getClient();
$crawler = $client->request('GET', '/config');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$form = $crawler->filter('button[id=new_user_save]')->form();
$data = array(
'new_user[username]' => 'wallace',
'new_user[password]' => 'wallace1',
'new_user[email]' => 'wallace@wallace.me',
);
$client->submit($form, $data);
$this->assertEquals(302, $client->getResponse()->getStatusCode());
$crawler = $client->followRedirect();
$this->assertGreaterThan(1, $alert = $crawler->filter('div.flash-notice')->extract(array('_text')));
$this->assertContains('User "wallace" added', $alert[0]);
}
}