From e4977b8a866f84f65f08c55c050a62f40170fdbf Mon Sep 17 00:00:00 2001 From: Jeremy Date: Sun, 22 Feb 2015 09:30:25 +0100 Subject: [PATCH] Adding new user --- .../Controller/ConfigController.php | 20 +++++ .../Form/Type/ChangePasswordType.php | 2 +- .../CoreBundle/Form/Type/NewUserType.php | 40 +++++++++ .../Resources/views/Config/index.html.twig | 32 +++++++ .../Tests/Controller/ConfigControllerTest.php | 89 ++++++++++++++++++- 5 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Form/Type/NewUserType.php diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php index b3236e3ce..aedbc9999 100644 --- a/src/Wallabag/CoreBundle/Controller/ConfigController.php +++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php @@ -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(), )); } diff --git a/src/Wallabag/CoreBundle/Form/Type/ChangePasswordType.php b/src/Wallabag/CoreBundle/Form/Type/ChangePasswordType.php index de0ad5378..e141789f1 100644 --- a/src/Wallabag/CoreBundle/Form/Type/ChangePasswordType.php +++ b/src/Wallabag/CoreBundle/Form/Type/ChangePasswordType.php @@ -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(), ), diff --git a/src/Wallabag/CoreBundle/Form/Type/NewUserType.php b/src/Wallabag/CoreBundle/Form/Type/NewUserType.php new file mode 100644 index 000000000..313a9aae8 --- /dev/null +++ b/src/Wallabag/CoreBundle/Form/Type/NewUserType.php @@ -0,0 +1,40 @@ +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'; + } +} diff --git a/src/Wallabag/CoreBundle/Resources/views/Config/index.html.twig b/src/Wallabag/CoreBundle/Resources/views/Config/index.html.twig index 7a45ec1f1..051dafd6b 100644 --- a/src/Wallabag/CoreBundle/Resources/views/Config/index.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/Config/index.html.twig @@ -102,4 +102,36 @@ {{ form_rest(pwdForm) }} + +

{% trans %}Add a user{% endtrans %}

+ +
+ {{ form_errors(newUserForm) }} + +
+
+ {{ form_label(newUserForm.username) }} + {{ form_errors(newUserForm.username) }} + {{ form_widget(newUserForm.username) }} +
+
+ +
+
+ {{ form_label(newUserForm.password) }} + {{ form_errors(newUserForm.password) }} + {{ form_widget(newUserForm.password) }} +
+
+ +
+
+ {{ form_label(newUserForm.email) }} + {{ form_errors(newUserForm.email) }} + {{ form_widget(newUserForm.email) }} +
+
+ + {{ form_rest(newUserForm) }} +
{% endblock %} diff --git a/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php index 519b4ba25..9b1a0986e 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php @@ -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]); + } }