aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag
diff options
context:
space:
mode:
authorJeremy <jeremy.benoist@gmail.com>2015-02-16 21:28:49 +0100
committerJeremy <jeremy.benoist@gmail.com>2015-02-16 21:31:58 +0100
commit4d85d7e9ba676bd5ac3428976ce9227f460eb542 (patch)
treeacf77c5baced69e689fcfc2d9463a9b75b9b3657 /src/Wallabag
parent7a577c519ffc254b6ddecd75c9ee84f656d759e1 (diff)
downloadwallabag-4d85d7e9ba676bd5ac3428976ce9227f460eb542.tar.gz
wallabag-4d85d7e9ba676bd5ac3428976ce9227f460eb542.tar.zst
wallabag-4d85d7e9ba676bd5ac3428976ce9227f460eb542.zip
Implement simple config
Diffstat (limited to 'src/Wallabag')
-rw-r--r--src/Wallabag/CoreBundle/Command/InstallCommand.php22
-rw-r--r--src/Wallabag/CoreBundle/Controller/ConfigController.php58
-rw-r--r--src/Wallabag/CoreBundle/Entity/Config.php107
-rw-r--r--src/Wallabag/CoreBundle/Entity/UsersConfig.php121
-rw-r--r--src/Wallabag/CoreBundle/Form/Type/ConfigType.php41
-rw-r--r--src/Wallabag/CoreBundle/Repository/ConfigRepository.php9
-rw-r--r--src/Wallabag/CoreBundle/Resources/config/routing.yml6
-rw-r--r--src/Wallabag/CoreBundle/Resources/views/Config/index.html.twig41
-rw-r--r--src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php96
-rw-r--r--src/Wallabag/CoreBundle/Tests/WallabagTestCase.php2
10 files changed, 347 insertions, 156 deletions
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;
6use Symfony\Component\Console\Input\InputInterface; 6use Symfony\Component\Console\Input\InputInterface;
7use Symfony\Component\Console\Output\OutputInterface; 7use Symfony\Component\Console\Output\OutputInterface;
8use Wallabag\CoreBundle\Entity\User; 8use Wallabag\CoreBundle\Entity\User;
9use Wallabag\CoreBundle\Entity\UsersConfig; 9use Wallabag\CoreBundle\Entity\Config;
10 10
11class InstallCommand extends ContainerAwareCommand 11class InstallCommand extends ContainerAwareCommand
12{ 12{
@@ -135,21 +135,13 @@ class InstallCommand extends ContainerAwareCommand
135 135
136 $em->persist($user); 136 $em->persist($user);
137 137
138 $pagerConfig = new UsersConfig(); 138 $config = new Config();
139 $pagerConfig->setUser($user); 139 $config->setUser($user);
140 $pagerConfig->setName('pager'); 140 $config->setTheme('baggy');
141 $pagerConfig->setValue(10); 141 $config->setItemsPerPage(10);
142 $config->setLanguage('en_US');
142 143
143 $em->persist($pagerConfig); 144 $em->persist($config);
144
145 $languageConfig = new LanguageConfig();
146 $languageConfig->setUser($user);
147 $languageConfig->setName('language');
148 $languageConfig->setValue('en_EN');
149
150 $em->persist($languageConfig);
151
152 $em->flush();
153 } 145 }
154 146
155 protected function runCommand($command, InputInterface $input, OutputInterface $output) 147 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 @@
1<?php
2
3namespace Wallabag\CoreBundle\Controller;
4
5use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7use Symfony\Component\HttpFoundation\Request;
8use Wallabag\CoreBundle\Entity\Config;
9use Wallabag\CoreBundle\Form\Type\ConfigType;
10
11class ConfigController extends Controller
12{
13 /**
14 * @param Request $request
15 *
16 * @Route("/config", name="config")
17 *
18 * @return \Symfony\Component\HttpFoundation\Response
19 */
20 public function indexAction(Request $request)
21 {
22 $config = $this->getConfig();
23
24 $form = $this->createForm(new ConfigType(), $config);
25
26 $form->handleRequest($request);
27
28 if ($form->isValid()) {
29 $em = $this->getDoctrine()->getManager();
30 $em->persist($config);
31 $em->flush();
32
33 $this->get('session')->getFlashBag()->add(
34 'notice',
35 'Config saved'
36 );
37
38 return $this->redirect($this->generateUrl('config'));
39 }
40
41 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
42 'form' => $form->createView(),
43 ));
44 }
45
46 private function getConfig()
47 {
48 $config = $this->getDoctrine()
49 ->getRepository('WallabagCoreBundle:Config')
50 ->findOneByUser($this->getUser());
51
52 if (!$config) {
53 $config = new Config($this->getUser());
54 }
55
56 return $config;
57 }
58}
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;
8/** 8/**
9 * Config 9 * Config
10 * 10 *
11 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\ConfigRepository")
11 * @ORM\Table(name="config") 12 * @ORM\Table(name="config")
12 * @ORM\Entity 13 * @ORM\Entity
13 */ 14 */
@@ -26,16 +27,40 @@ class Config
26 * @var string 27 * @var string
27 * 28 *
28 * @Assert\NotBlank() 29 * @Assert\NotBlank()
29 * @ORM\Column(name="name", type="string", nullable=false) 30 * @ORM\Column(name="theme", type="string", nullable=false)
30 */ 31 */
31 private $name; 32 private $theme;
32 33
33 /** 34 /**
34 * @var string 35 * @var string
35 * 36 *
36 * @ORM\Column(name="value", type="blob", nullable=true) 37 * @Assert\NotBlank()
38 * @ORM\Column(name="items_per_page", type="integer", nullable=false)
39 */
40 private $items_per_page;
41
42 /**
43 * @var string
44 *
45 * @Assert\NotBlank()
46 * @ORM\Column(name="language", type="string", nullable=false)
37 */ 47 */
38 private $value; 48 private $language;
49
50 /**
51 * @ORM\ManyToOne(targetEntity="User", inversedBy="config")
52 */
53 private $user;
54
55 /*
56 * @param User $user
57 */
58 public function __construct(User $user)
59 {
60 $this->user = $user;
61 $this->items_per_page = 12;
62 $this->language = 'en_US';
63 }
39 64
40 /** 65 /**
41 * Get id 66 * Get id
@@ -48,48 +73,94 @@ class Config
48 } 73 }
49 74
50 /** 75 /**
51 * Set name 76 * Set theme
52 * 77 *
53 * @param string $name 78 * @param string $theme
54 * @return Config 79 * @return Config
55 */ 80 */
56 public function setName($name) 81 public function setTheme($theme)
57 { 82 {
58 $this->name = $name; 83 $this->theme = $theme;
59 84
60 return $this; 85 return $this;
61 } 86 }
62 87
63 /** 88 /**
64 * Get name 89 * Get theme
65 * 90 *
66 * @return string 91 * @return string
67 */ 92 */
68 public function getName() 93 public function getTheme()
69 { 94 {
70 return $this->name; 95 return $this->theme;
71 } 96 }
72 97
73 /** 98 /**
74 * Set value 99 * Set items_per_page
75 * 100 *
76 * @param string $value 101 * @param integer $itemsPerPage
77 * @return Config 102 * @return Config
78 */ 103 */
79 public function setValue($value) 104 public function setItemsPerPage($itemsPerPage)
80 { 105 {
81 $this->value = $value; 106 $this->items_per_page = $itemsPerPage;
82 107
83 return $this; 108 return $this;
84 } 109 }
85 110
86 /** 111 /**
87 * Get value 112 * Get items_per_page
113 *
114 * @return integer
115 */
116 public function getItemsPerPage()
117 {
118 return $this->items_per_page;
119 }
120
121 /**
122 * Set language
123 *
124 * @param string $language
125 * @return Config
126 */
127 public function setLanguage($language)
128 {
129 $this->language = $language;
130
131 return $this;
132 }
133
134 /**
135 * Get language
88 * 136 *
89 * @return string 137 * @return string
90 */ 138 */
91 public function getValue() 139 public function getLanguage()
140 {
141 return $this->language;
142 }
143
144 /**
145 * Set user
146 *
147 * @param \Wallabag\CoreBundle\Entity\User $user
148 * @return Config
149 */
150 public function setUser(\Wallabag\CoreBundle\Entity\User $user = null)
151 {
152 $this->user = $user;
153
154 return $this;
155 }
156
157 /**
158 * Get user
159 *
160 * @return \Wallabag\CoreBundle\Entity\User
161 */
162 public function getUser()
92 { 163 {
93 return $this->value; 164 return $this->user;
94 } 165 }
95} 166}
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 @@
1<?php
2
3namespace Wallabag\CoreBundle\Entity;
4
5use Doctrine\ORM\Mapping as ORM;
6
7/**
8 * UsersConfig
9 *
10 * @ORM\Table(name="users_config")
11 * @ORM\Entity
12 */
13class UsersConfig
14{
15 /**
16 * @var integer
17 *
18 * @ORM\Column(name="id", type="integer", nullable=true)
19 * @ORM\Id
20 * @ORM\GeneratedValue(strategy="IDENTITY")
21 */
22 private $id;
23
24 /**
25 * @ORM\ManyToOne(targetEntity="User", inversedBy="config")
26 */
27 private $user;
28
29 /**
30 * @var string
31 *
32 * @ORM\Column(name="name", type="text", nullable=true)
33 */
34 private $name;
35
36 /**
37 * @var string
38 *
39 * @ORM\Column(name="value", type="text", nullable=true)
40 */
41 private $value;
42
43 /**
44 * Get id
45 *
46 * @return integer
47 */
48 public function getId()
49 {
50 return $this->id;
51 }
52
53 /**
54 * Set name
55 *
56 * @param string $name
57 * @return UsersConfig
58 */
59 public function setName($name)
60 {
61 $this->name = $name;
62
63 return $this;
64 }
65
66 /**
67 * Get name
68 *
69 * @return string
70 */
71 public function getName()
72 {
73 return $this->name;
74 }
75
76 /**
77 * Set value
78 *
79 * @param string $value
80 * @return UsersConfig
81 */
82 public function setValue($value)
83 {
84 $this->value = $value;
85
86 return $this;
87 }
88
89 /**
90 * Get value
91 *
92 * @return string
93 */
94 public function getValue()
95 {
96 return $this->value;
97 }
98
99 /**
100 * Set user
101 *
102 * @param \Wallabag\CoreBundle\Entity\User $user
103 * @return UsersConfig
104 */
105 public function setUser(\Wallabag\CoreBundle\Entity\User $user = null)
106 {
107 $this->user = $user;
108
109 return $this;
110 }
111
112 /**
113 * Get user
114 *
115 * @return \Wallabag\CoreBundle\Entity\User
116 */
117 public function getUser()
118 {
119 return $this->user;
120 }
121}
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 @@
1<?php
2namespace Wallabag\CoreBundle\Form\Type;
3
4use Symfony\Component\Form\AbstractType;
5use Symfony\Component\Form\FormBuilderInterface;
6use Symfony\Component\OptionsResolver\OptionsResolverInterface;
7
8class ConfigType extends AbstractType
9{
10 public function buildForm(FormBuilderInterface $builder, array $options)
11 {
12 $builder
13 ->add('theme', 'choice', array(
14 'choices' => array(
15 'baggy' => 'Baggy',
16 'courgette' => 'Courgette',
17 'dark' => 'Dark',
18 'default' => 'Default',
19 'dmagenta' => 'Dmagenta',
20 'solarized' => 'Solarized',
21 'solarized_dark' => 'Solarized Dark',
22 ),
23 ))
24 ->add('items_per_page')
25 ->add('language')
26 ->add('save', 'submit')
27 ;
28 }
29
30 public function setDefaultOptions(OptionsResolverInterface $resolver)
31 {
32 $resolver->setDefaults(array(
33 'data_class' => 'Wallabag\CoreBundle\Entity\Config',
34 ));
35 }
36
37 public function getName()
38 {
39 return 'config';
40 }
41}
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 @@
1<?php
2
3namespace Wallabag\CoreBundle\Repository;
4
5use Doctrine\ORM\EntityRepository;
6
7class ConfigRepository extends EntityRepository
8{
9}
diff --git a/src/Wallabag/CoreBundle/Resources/config/routing.yml b/src/Wallabag/CoreBundle/Resources/config/routing.yml
index ec1d23cc..f3502e15 100644
--- a/src/Wallabag/CoreBundle/Resources/config/routing.yml
+++ b/src/Wallabag/CoreBundle/Resources/config/routing.yml
@@ -1,3 +1,7 @@
1_wllbg: 1entry:
2 resource: "@WallabagCoreBundle/Controller/EntryController.php" 2 resource: "@WallabagCoreBundle/Controller/EntryController.php"
3 type: annotation 3 type: annotation
4
5config:
6 resource: "@WallabagCoreBundle/Controller/ConfigController.php"
7 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
index 00000000..9c04ff20
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Resources/views/Config/index.html.twig
@@ -0,0 +1,41 @@
1{% extends "WallabagCoreBundle::layout.html.twig" %}
2
3{% block title %}{% trans %}Config{% endtrans %}{% endblock %}
4
5{% block menu %}
6 {% include "WallabagCoreBundle::_menu.html.twig" %}
7{% endblock %}
8
9{% block content %}
10 <h2>Basic config</h2>
11
12 <form action="{{ path('config') }}" method="post" {{ form_enctype(form) }}>
13 {{ form_errors(form) }}
14
15 <fieldset class="w500p inline">
16 <div class="row">
17 {{ form_label(form.theme) }}
18 {{ form_errors(form.theme) }}
19 {{ form_widget(form.theme) }}
20 </div>
21 </fieldset>
22
23 <fieldset class="w500p inline">
24 <div class="row">
25 {{ form_label(form.items_per_page) }}
26 {{ form_errors(form.items_per_page) }}
27 {{ form_widget(form.items_per_page) }}
28 </div>
29 </fieldset>
30
31 <fieldset class="w500p inline">
32 <div class="row">
33 {{ form_label(form.language) }}
34 {{ form_errors(form.language) }}
35 {{ form_widget(form.language) }}
36 </div>
37 </fieldset>
38
39 {{ form_rest(form) }}
40 </form>
41{% 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 @@
1<?php
2
3namespace Wallabag\CoreBundle\Tests\Controller;
4
5use Wallabag\CoreBundle\Tests\WallabagTestCase;
6
7class ConfigControllerTest extends WallabagTestCase
8{
9 public function testLogin()
10 {
11 $client = $this->getClient();
12
13 $client->request('GET', '/new');
14
15 $this->assertEquals(302, $client->getResponse()->getStatusCode());
16 $this->assertContains('login', $client->getResponse()->headers->get('location'));
17 }
18
19 public function testIndex()
20 {
21 $this->logInAs('admin');
22 $client = $this->getClient();
23
24 $crawler = $client->request('GET', '/config');
25
26 $this->assertEquals(200, $client->getResponse()->getStatusCode());
27
28 $this->assertCount(1, $crawler->filter('input[type=number]'));
29 $this->assertCount(1, $crawler->filter('button[type=submit]'));
30 }
31
32 public function testUpdate()
33 {
34 $this->logInAs('admin');
35 $client = $this->getClient();
36
37 $crawler = $client->request('GET', '/config');
38
39 $this->assertEquals(200, $client->getResponse()->getStatusCode());
40
41 $form = $crawler->filter('button[type=submit]')->form();
42
43 $data = array(
44 'config[theme]' => 'baggy',
45 'config[items_per_page]' => '30',
46 'config[language]' => 'fr_FR',
47 );
48
49 $client->submit($form, $data);
50
51 $this->assertEquals(302, $client->getResponse()->getStatusCode());
52
53 $crawler = $client->followRedirect();
54
55 $this->assertGreaterThan(1, $alert = $crawler->filter('div.flash-notice')->extract(array('_text')));
56 $this->assertContains('Config saved', $alert[0]);
57 }
58
59 public function dataForUpdateFailed()
60 {
61 return array(
62 array(array(
63 'config[theme]' => 'baggy',
64 'config[items_per_page]' => '',
65 'config[language]' => 'fr_FR',
66 )),
67 array(array(
68 'config[theme]' => 'baggy',
69 'config[items_per_page]' => '12',
70 'config[language]' => '',
71 )),
72 );
73 }
74
75 /**
76 * @dataProvider dataForUpdateFailed
77 */
78 public function testUpdateFailed($data)
79 {
80 $this->logInAs('admin');
81 $client = $this->getClient();
82
83 $crawler = $client->request('GET', '/config');
84
85 $this->assertEquals(200, $client->getResponse()->getStatusCode());
86
87 $form = $crawler->filter('button[type=submit]')->form();
88
89 $crawler = $client->submit($form, $data);
90
91 $this->assertEquals(200, $client->getResponse()->getStatusCode());
92
93 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
94 $this->assertContains('This value should not be blank', $alert[0]);
95 }
96}
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;
4 4
5use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; 5use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6 6
7class WallabagTestCase extends WebTestCase 7abstract class WallabagTestCase extends WebTestCase
8{ 8{
9 private $client = null; 9 private $client = null;
10 10