]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Implement simple config
authorJeremy <jeremy.benoist@gmail.com>
Mon, 16 Feb 2015 20:28:49 +0000 (21:28 +0100)
committerJeremy <jeremy.benoist@gmail.com>
Mon, 16 Feb 2015 20:31:58 +0000 (21:31 +0100)
src/Wallabag/CoreBundle/Command/InstallCommand.php
src/Wallabag/CoreBundle/Controller/ConfigController.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Entity/Config.php
src/Wallabag/CoreBundle/Entity/UsersConfig.php [deleted file]
src/Wallabag/CoreBundle/Form/Type/ConfigType.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Repository/ConfigRepository.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Resources/config/routing.yml
src/Wallabag/CoreBundle/Resources/views/Config/index.html.twig [new file with mode: 0644]
src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Tests/WallabagTestCase.php

index bf2f747d81a70b762b3567a28dcf14733b2ccc8c..a43019584d22b87ccc0438257bb9e422dc8b0c4f 100644 (file)
@@ -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 (file)
index 0000000..f48a9cb
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+
+namespace Wallabag\CoreBundle\Controller;
+
+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\Form\Type\ConfigType;
+
+class ConfigController extends Controller
+{
+    /**
+     * @param Request $request
+     *
+     * @Route("/config", name="config")
+     *
+     * @return \Symfony\Component\HttpFoundation\Response
+     */
+    public function indexAction(Request $request)
+    {
+        $config = $this->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;
+    }
+}
index 14977d320e80bc4d1031c648b71b761ccf48e378..7b4464a17cddc1374f8e779b9e6bbeb3a5ce9909 100644 (file)
@@ -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 (file)
index 5212763..0000000
+++ /dev/null
@@ -1,121 +0,0 @@
-<?php
-
-namespace Wallabag\CoreBundle\Entity;
-
-use Doctrine\ORM\Mapping as ORM;
-
-/**
- * UsersConfig
- *
- * @ORM\Table(name="users_config")
- * @ORM\Entity
- */
-class UsersConfig
-{
-    /**
-     * @var integer
-     *
-     * @ORM\Column(name="id", type="integer", nullable=true)
-     * @ORM\Id
-     * @ORM\GeneratedValue(strategy="IDENTITY")
-     */
-    private $id;
-
-    /**
-     * @ORM\ManyToOne(targetEntity="User", inversedBy="config")
-     */
-    private $user;
-
-    /**
-     * @var string
-     *
-     * @ORM\Column(name="name", type="text", nullable=true)
-     */
-    private $name;
-
-    /**
-     * @var string
-     *
-     * @ORM\Column(name="value", type="text", nullable=true)
-     */
-    private $value;
-
-    /**
-     * Get id
-     *
-     * @return integer
-     */
-    public function getId()
-    {
-        return $this->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 (file)
index 0000000..74e2a6f
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+namespace Wallabag\CoreBundle\Form\Type;
+
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\FormBuilderInterface;
+use Symfony\Component\OptionsResolver\OptionsResolverInterface;
+
+class ConfigType extends AbstractType
+{
+    public function buildForm(FormBuilderInterface $builder, array $options)
+    {
+        $builder
+            ->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 (file)
index 0000000..b2b1f62
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+
+namespace Wallabag\CoreBundle\Repository;
+
+use Doctrine\ORM\EntityRepository;
+
+class ConfigRepository extends EntityRepository
+{
+}
index ec1d23cc9f6f0e0d7526e36bcfbfc73d9a6c46f2..f3502e156e6c80e884e9beb89bd42f76ec55bdd7 100644 (file)
@@ -1,3 +1,7 @@
-_wllbg:
+entry:
     resource: "@WallabagCoreBundle/Controller/EntryController.php"
     type:     annotation
+
+config:
+    resource: "@WallabagCoreBundle/Controller/ConfigController.php"
+    type:     annotation
diff --git a/src/Wallabag/CoreBundle/Resources/views/Config/index.html.twig b/src/Wallabag/CoreBundle/Resources/views/Config/index.html.twig
new file mode 100644 (file)
index 0000000..9c04ff2
--- /dev/null
@@ -0,0 +1,41 @@
+{% extends "WallabagCoreBundle::layout.html.twig" %}
+
+{% block title %}{% trans %}Config{% endtrans %}{% endblock %}
+
+{% block menu %}
+    {% include "WallabagCoreBundle::_menu.html.twig" %}
+{% endblock %}
+
+{% block content %}
+    <h2>Basic config</h2>
+
+    <form action="{{ path('config') }}" method="post" {{ form_enctype(form) }}>
+        {{ form_errors(form) }}
+
+        <fieldset class="w500p inline">
+            <div class="row">
+                {{ form_label(form.theme) }}
+                {{ form_errors(form.theme) }}
+                {{ form_widget(form.theme) }}
+            </div>
+        </fieldset>
+
+        <fieldset class="w500p inline">
+            <div class="row">
+                {{ form_label(form.items_per_page) }}
+                {{ form_errors(form.items_per_page) }}
+                {{ form_widget(form.items_per_page) }}
+            </div>
+        </fieldset>
+
+        <fieldset class="w500p inline">
+            <div class="row">
+                {{ form_label(form.language) }}
+                {{ form_errors(form.language) }}
+                {{ form_widget(form.language) }}
+            </div>
+        </fieldset>
+
+        {{ form_rest(form) }}
+    </form>
+{% endblock %}
diff --git a/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php
new file mode 100644 (file)
index 0000000..30809a0
--- /dev/null
@@ -0,0 +1,96 @@
+<?php
+
+namespace Wallabag\CoreBundle\Tests\Controller;
+
+use Wallabag\CoreBundle\Tests\WallabagTestCase;
+
+class ConfigControllerTest extends WallabagTestCase
+{
+    public function testLogin()
+    {
+        $client = $this->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]);
+    }
+}
index a80b8bac426e2093dbdc2ebdec926424851e5eae..397945459e9e85dff8a73f4177e5ba4372da5bf8 100644 (file)
@@ -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;