diff --git a/sources/AppBundle/Controller/Admin/Site/Article/AddArticleAction.php b/sources/AppBundle/Controller/Admin/Site/Article/AddArticleAction.php index afeb2a658..035d4203b 100644 --- a/sources/AppBundle/Controller/Admin/Site/Article/AddArticleAction.php +++ b/sources/AppBundle/Controller/Admin/Site/Article/AddArticleAction.php @@ -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 { @@ -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é'); diff --git a/sources/AppBundle/Controller/Admin/Site/Article/EditArticleAction.php b/sources/AppBundle/Controller/Admin/Site/Article/EditArticleAction.php index 3964ab7ea..39574516e 100644 --- a/sources/AppBundle/Controller/Admin/Site/Article/EditArticleAction.php +++ b/sources/AppBundle/Controller/Admin/Site/Article/EditArticleAction.php @@ -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); diff --git a/sources/AppBundle/Site/Form/ArticleType.php b/sources/AppBundle/Site/Form/ArticleType.php index 8d1a868dc..5e0bfdd7d 100644 --- a/sources/AppBundle/Site/Form/ArticleType.php +++ b/sources/AppBundle/Site/Form/ArticleType.php @@ -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 @@ -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', @@ -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, + ]); } } diff --git a/tests/behat/features/Admin/Site/AdminSiteArticles.feature b/tests/behat/features/Admin/Site/AdminSiteArticles.feature index a41c52960..ee74ed74e 100644 --- a/tests/behat/features/Admin/Site/AdminSiteArticles.feature +++ b/tests/behat/features/Admin/Site/AdminSiteArticles.feature @@ -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" @@ -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 !"