From 4d85d7e9ba676bd5ac3428976ce9227f460eb542 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Mon, 16 Feb 2015 21:28:49 +0100 Subject: Implement simple config --- src/Wallabag/CoreBundle/Command/InstallCommand.php | 22 ++-- .../CoreBundle/Controller/ConfigController.php | 58 ++++++++++ src/Wallabag/CoreBundle/Entity/Config.php | 107 +++++++++++++++--- src/Wallabag/CoreBundle/Entity/UsersConfig.php | 121 --------------------- src/Wallabag/CoreBundle/Form/Type/ConfigType.php | 41 +++++++ .../CoreBundle/Repository/ConfigRepository.php | 9 ++ .../CoreBundle/Resources/config/routing.yml | 6 +- .../Resources/views/Config/index.html.twig | 41 +++++++ .../Tests/Controller/ConfigControllerTest.php | 96 ++++++++++++++++ src/Wallabag/CoreBundle/Tests/WallabagTestCase.php | 2 +- 10 files changed, 347 insertions(+), 156 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Controller/ConfigController.php delete mode 100644 src/Wallabag/CoreBundle/Entity/UsersConfig.php create mode 100644 src/Wallabag/CoreBundle/Form/Type/ConfigType.php create mode 100644 src/Wallabag/CoreBundle/Repository/ConfigRepository.php create mode 100644 src/Wallabag/CoreBundle/Resources/views/Config/index.html.twig create mode 100644 src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php (limited to 'src/Wallabag/CoreBundle') diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php index bf2f747d..a4301958 100644 --- a/src/Wallabag/CoreBundle/Command/InstallCommand.php +++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php @@ -6,7 +6,7 @@ use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Wallabag\CoreBundle\Entity\User; -use Wallabag\CoreBundle\Entity\UsersConfig; +use Wallabag\CoreBundle\Entity\Config; class InstallCommand extends ContainerAwareCommand { @@ -135,21 +135,13 @@ class InstallCommand extends ContainerAwareCommand $em->persist($user); - $pagerConfig = new UsersConfig(); - $pagerConfig->setUser($user); - $pagerConfig->setName('pager'); - $pagerConfig->setValue(10); + $config = new Config(); + $config->setUser($user); + $config->setTheme('baggy'); + $config->setItemsPerPage(10); + $config->setLanguage('en_US'); - $em->persist($pagerConfig); - - $languageConfig = new LanguageConfig(); - $languageConfig->setUser($user); - $languageConfig->setName('language'); - $languageConfig->setValue('en_EN'); - - $em->persist($languageConfig); - - $em->flush(); + $em->persist($config); } protected function runCommand($command, InputInterface $input, OutputInterface $output) diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php new file mode 100644 index 00000000..f48a9cb1 --- /dev/null +++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php @@ -0,0 +1,58 @@ +getConfig(); + + $form = $this->createForm(new ConfigType(), $config); + + $form->handleRequest($request); + + if ($form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->persist($config); + $em->flush(); + + $this->get('session')->getFlashBag()->add( + 'notice', + 'Config saved' + ); + + return $this->redirect($this->generateUrl('config')); + } + + return $this->render('WallabagCoreBundle:Config:index.html.twig', array( + 'form' => $form->createView(), + )); + } + + private function getConfig() + { + $config = $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Config') + ->findOneByUser($this->getUser()); + + if (!$config) { + $config = new Config($this->getUser()); + } + + return $config; + } +} diff --git a/src/Wallabag/CoreBundle/Entity/Config.php b/src/Wallabag/CoreBundle/Entity/Config.php index 14977d32..7b4464a1 100644 --- a/src/Wallabag/CoreBundle/Entity/Config.php +++ b/src/Wallabag/CoreBundle/Entity/Config.php @@ -8,6 +8,7 @@ use Symfony\Component\Validator\Constraints as Assert; /** * Config * + * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\ConfigRepository") * @ORM\Table(name="config") * @ORM\Entity */ @@ -26,16 +27,40 @@ class Config * @var string * * @Assert\NotBlank() - * @ORM\Column(name="name", type="string", nullable=false) + * @ORM\Column(name="theme", type="string", nullable=false) */ - private $name; + private $theme; /** * @var string * - * @ORM\Column(name="value", type="blob", nullable=true) + * @Assert\NotBlank() + * @ORM\Column(name="items_per_page", type="integer", nullable=false) + */ + private $items_per_page; + + /** + * @var string + * + * @Assert\NotBlank() + * @ORM\Column(name="language", type="string", nullable=false) */ - private $value; + private $language; + + /** + * @ORM\ManyToOne(targetEntity="User", inversedBy="config") + */ + private $user; + + /* + * @param User $user + */ + public function __construct(User $user) + { + $this->user = $user; + $this->items_per_page = 12; + $this->language = 'en_US'; + } /** * Get id @@ -48,48 +73,94 @@ class Config } /** - * Set name + * Set theme * - * @param string $name + * @param string $theme * @return Config */ - public function setName($name) + public function setTheme($theme) { - $this->name = $name; + $this->theme = $theme; return $this; } /** - * Get name + * Get theme * * @return string */ - public function getName() + public function getTheme() { - return $this->name; + return $this->theme; } /** - * Set value + * Set items_per_page * - * @param string $value + * @param integer $itemsPerPage * @return Config */ - public function setValue($value) + public function setItemsPerPage($itemsPerPage) { - $this->value = $value; + $this->items_per_page = $itemsPerPage; return $this; } /** - * Get value + * Get items_per_page + * + * @return integer + */ + public function getItemsPerPage() + { + return $this->items_per_page; + } + + /** + * Set language + * + * @param string $language + * @return Config + */ + public function setLanguage($language) + { + $this->language = $language; + + return $this; + } + + /** + * Get language * * @return string */ - public function getValue() + public function getLanguage() + { + return $this->language; + } + + /** + * Set user + * + * @param \Wallabag\CoreBundle\Entity\User $user + * @return Config + */ + public function setUser(\Wallabag\CoreBundle\Entity\User $user = null) + { + $this->user = $user; + + return $this; + } + + /** + * Get user + * + * @return \Wallabag\CoreBundle\Entity\User + */ + public function getUser() { - return $this->value; + return $this->user; } } diff --git a/src/Wallabag/CoreBundle/Entity/UsersConfig.php b/src/Wallabag/CoreBundle/Entity/UsersConfig.php deleted file mode 100644 index 52127631..00000000 --- a/src/Wallabag/CoreBundle/Entity/UsersConfig.php +++ /dev/null @@ -1,121 +0,0 @@ -id; - } - - /** - * Set name - * - * @param string $name - * @return UsersConfig - */ - public function setName($name) - { - $this->name = $name; - - return $this; - } - - /** - * Get name - * - * @return string - */ - public function getName() - { - return $this->name; - } - - /** - * Set value - * - * @param string $value - * @return UsersConfig - */ - public function setValue($value) - { - $this->value = $value; - - return $this; - } - - /** - * Get value - * - * @return string - */ - public function getValue() - { - return $this->value; - } - - /** - * Set user - * - * @param \Wallabag\CoreBundle\Entity\User $user - * @return UsersConfig - */ - public function setUser(\Wallabag\CoreBundle\Entity\User $user = null) - { - $this->user = $user; - - return $this; - } - - /** - * Get user - * - * @return \Wallabag\CoreBundle\Entity\User - */ - public function getUser() - { - return $this->user; - } -} diff --git a/src/Wallabag/CoreBundle/Form/Type/ConfigType.php b/src/Wallabag/CoreBundle/Form/Type/ConfigType.php new file mode 100644 index 00000000..74e2a6f1 --- /dev/null +++ b/src/Wallabag/CoreBundle/Form/Type/ConfigType.php @@ -0,0 +1,41 @@ +add('theme', 'choice', array( + 'choices' => array( + 'baggy' => 'Baggy', + 'courgette' => 'Courgette', + 'dark' => 'Dark', + 'default' => 'Default', + 'dmagenta' => 'Dmagenta', + 'solarized' => 'Solarized', + 'solarized_dark' => 'Solarized Dark', + ), + )) + ->add('items_per_page') + ->add('language') + ->add('save', 'submit') + ; + } + + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + $resolver->setDefaults(array( + 'data_class' => 'Wallabag\CoreBundle\Entity\Config', + )); + } + + public function getName() + { + return 'config'; + } +} diff --git a/src/Wallabag/CoreBundle/Repository/ConfigRepository.php b/src/Wallabag/CoreBundle/Repository/ConfigRepository.php new file mode 100644 index 00000000..b2b1f627 --- /dev/null +++ b/src/Wallabag/CoreBundle/Repository/ConfigRepository.php @@ -0,0 +1,9 @@ +Basic config + +
+ {{ form_errors(form) }} + +
+
+ {{ form_label(form.theme) }} + {{ form_errors(form.theme) }} + {{ form_widget(form.theme) }} +
+
+ +
+
+ {{ form_label(form.items_per_page) }} + {{ form_errors(form.items_per_page) }} + {{ form_widget(form.items_per_page) }} +
+
+ +
+
+ {{ form_label(form.language) }} + {{ form_errors(form.language) }} + {{ form_widget(form.language) }} +
+
+ + {{ form_rest(form) }} +
+{% endblock %} diff --git a/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php new file mode 100644 index 00000000..30809a04 --- /dev/null +++ b/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php @@ -0,0 +1,96 @@ +getClient(); + + $client->request('GET', '/new'); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + $this->assertContains('login', $client->getResponse()->headers->get('location')); + } + + public function testIndex() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/config'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $this->assertCount(1, $crawler->filter('input[type=number]')); + $this->assertCount(1, $crawler->filter('button[type=submit]')); + } + + public function testUpdate() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/config'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $form = $crawler->filter('button[type=submit]')->form(); + + $data = array( + 'config[theme]' => 'baggy', + 'config[items_per_page]' => '30', + 'config[language]' => 'fr_FR', + ); + + $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('Config saved', $alert[0]); + } + + public function dataForUpdateFailed() + { + return array( + array(array( + 'config[theme]' => 'baggy', + 'config[items_per_page]' => '', + 'config[language]' => 'fr_FR', + )), + array(array( + 'config[theme]' => 'baggy', + 'config[items_per_page]' => '12', + 'config[language]' => '', + )), + ); + } + + /** + * @dataProvider dataForUpdateFailed + */ + public function testUpdateFailed($data) + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/config'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $form = $crawler->filter('button[type=submit]')->form(); + + $crawler = $client->submit($form, $data); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text'))); + $this->assertContains('This value should not be blank', $alert[0]); + } +} diff --git a/src/Wallabag/CoreBundle/Tests/WallabagTestCase.php b/src/Wallabag/CoreBundle/Tests/WallabagTestCase.php index a80b8bac..39794545 100644 --- a/src/Wallabag/CoreBundle/Tests/WallabagTestCase.php +++ b/src/Wallabag/CoreBundle/Tests/WallabagTestCase.php @@ -4,7 +4,7 @@ namespace Wallabag\CoreBundle\Tests; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; -class WallabagTestCase extends WebTestCase +abstract class WallabagTestCase extends WebTestCase { private $client = null; -- cgit v1.2.3