mirror of
https://github.com/wallabag/wallabag.git
synced 2025-01-10 17:05:26 +00:00
Implement simple config
This commit is contained in:
parent
7a577c519f
commit
4d85d7e9ba
10 changed files with 347 additions and 156 deletions
|
@ -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)
|
||||
|
|
58
src/Wallabag/CoreBundle/Controller/ConfigController.php
Normal file
58
src/Wallabag/CoreBundle/Controller/ConfigController.php
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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 $value;
|
||||
private $items_per_page;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @Assert\NotBlank()
|
||||
* @ORM\Column(name="language", type="string", nullable=false)
|
||||
*/
|
||||
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->value;
|
||||
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->user;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
41
src/Wallabag/CoreBundle/Form/Type/ConfigType.php
Normal file
41
src/Wallabag/CoreBundle/Form/Type/ConfigType.php
Normal file
|
@ -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';
|
||||
}
|
||||
}
|
9
src/Wallabag/CoreBundle/Repository/ConfigRepository.php
Normal file
9
src/Wallabag/CoreBundle/Repository/ConfigRepository.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Wallabag\CoreBundle\Repository;
|
||||
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
|
||||
class ConfigRepository extends EntityRepository
|
||||
{
|
||||
}
|
|
@ -1,3 +1,7 @@
|
|||
_wllbg:
|
||||
entry:
|
||||
resource: "@WallabagCoreBundle/Controller/EntryController.php"
|
||||
type: annotation
|
||||
|
||||
config:
|
||||
resource: "@WallabagCoreBundle/Controller/ConfigController.php"
|
||||
type: annotation
|
||||
|
|
|
@ -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 %}
|
|
@ -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]);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Reference in a new issue