Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\String\Slugger\AsciiSlugger;

final class AddArticleAction extends AbstractController
{
Expand All @@ -27,6 +28,10 @@ public function __invoke(Request $request): Response
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
if ($article->titre && $article->raccourci === null) {
$article->raccourci = (new AsciiSlugger())->slug($article->titre)->lower()->toString();
}

$this->articleRepository->save($article);
$this->audit->log('Ajout de l\'article ' . $article->titre);
$this->addFlash('notice', 'L\'article ' . $article->titre . ' a été ajouté');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __invoke(int $id, Request $request): Response
throw $this->createNotFoundException();
}

$form = $this->createForm(ArticleType::class, $article);
$form = $this->createForm(ArticleType::class, $article, ['is_new' => false]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->articleRepository->save($article);
Expand Down
44 changes: 30 additions & 14 deletions sources/AppBundle/Site/Form/ArticleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;

class ArticleType extends AbstractType
Expand Down Expand Up @@ -78,20 +79,6 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
new Assert\Type('string'),
],
])
->add('raccourci', TextType::class, [
'required' => true,
'label' => 'Raccourci',
'attr' => [
'maxlength' => 255,
'size' => 60,
],
'constraints' => [
new Assert\Length(max: 255),
new Assert\NotBlank(),
new Assert\Type('string'),
new Assert\Regex('/(\s)/', 'Ne doit pas contenir d\'espaces', null, false),
],
])
->add('rubrique', EntityType::class, [
'required' => true,
'label' => 'Rubrique',
Expand Down Expand Up @@ -144,5 +131,34 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
])
;
$builder->get('datePublication')->addModelTransformer(new DateTimeToTimestampTransformer());

$raccourciOption = [
'required' => $options['is_new'] === false,
'label' => 'Raccourci',
'attr' => [
'maxlength' => 255,
'size' => 60,
],
'constraints' => [
new Assert\Length(max: 255),
new Assert\Type('string'),
new Assert\Regex('/(\s)/', 'Ne doit pas contenir d\'espaces', null, false),
],
];

if ($options['is_new'] === true) {
$raccourciOption['help'] = 'Si ce champ est vide, la valeur sera générée automatiquement';
} else {
$raccourciOption['constraints'][] = new Assert\NotBlank();
}

$builder->add('raccourci', TextType::class, $raccourciOption);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'is_new' => true,
]);
}
}
33 changes: 33 additions & 0 deletions tests/behat/features/Admin/Site/AdminSiteArticles.feature
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ Feature: Administration - Partie Site
And I should see "Liste des article"
Then the ".content table" element should contain "Le titre de l'article modifié"

# vérification que le raccourci n'a pas changé avec le titre
When I go to "/news/17-url-article"
Then I should see "Le titre de l'article"
Then I should see "Le chapeau de l'article"
Then I should see "Le contenu de l'article"

# suppression d'un article
When I follow "Administration"
When I follow "Articles"
And I follow "supprimer_2"
Then I should see "Liste des articles"
Expand Down Expand Up @@ -111,3 +118,29 @@ Feature: Administration - Partie Site
And the "article[titre]" field should contain "Le titre markdown"
And the "article[chapeau]" field should contain "Le *chapeau* markdown"
And the "article[contenu]" field should contain "Un peu de **contenu** avec de la *mise* en page et un [lien](https://afup.org)."

@reloadDbWithTestData
Scenario: Génération du raccourci d'un article
Given I am logged in as admin and on the Administration
And I follow "Articles"
Then I should see "Liste des articles"
And I should see "Actualités"

# ajout d'un article
When I follow "Ajouter"
Then I should see "Ajouter un article"
And I fill in "article[titre]" with "Mon super 123 article !"
And I fill in "article[chapeau]" with "Le chapeau de l'article"
And I fill in "article[contenu]" with "Le contenu de l'article"
And I select "Actualités" from "article[rubrique]"
And I select "9" from "article[position]"
And I select "En ligne" from "article[etat]"
And I select "Associatif" from "article[theme]"
And I select "forum" from "article[idEvent]"
And I press "Ajouter"
When I should see "Liste des articles"
Then the ".content table" element should contain "Mon super 123 article !"

# vérification de l'article sur le site publique
When I go to "/news/17-mon-super-123-article"
Then I should see "Mon super 123 article !"
Loading