From f2ebd11cba2ed5d74d4483609e50139f0245e85d Mon Sep 17 00:00:00 2001 From: Xavier Leune Date: Tue, 17 Nov 2026 15:59:35 +0100 Subject: [PATCH] =?UTF-8?q?Redirige=20en=20301=20les=20talks=20dont=20le?= =?UTF-8?q?=20slug=20a=20chang=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Le slug d'un talk est dérivé de son titre : quand le titre est modifié après publication, /talks/{id}-{ancien-slug} et sa variante /joindin répondaient 404, ce qui cassait les anciens liens et les backlinks. - ShowAction et JoindinAction redirigent en 301 vers leur propre route (talks_show, talks_joindin) avec le slug courant quand le talk existe et est publié mais que le slug diffère - Le 404 est conservé pour un talk inexistant ou non publié (isDisplayedOnHistory, et planning absent pour ShowAction) - Behat : step "I do not follow redirects" (repris de feat/seo) et scénarios 301 / 200 / 404 --- .../Website/Talks/JoindinAction.php | 7 +++- .../Controller/Website/Talks/ShowAction.php | 9 ++++-- tests/behat/bootstrap/FeatureContext.php | 24 ++++++++++++++ tests/behat/features/PublicSite/Talks.feature | 32 +++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) diff --git a/sources/AppBundle/Controller/Website/Talks/JoindinAction.php b/sources/AppBundle/Controller/Website/Talks/JoindinAction.php index 780793a7f..15d5b5cc1 100644 --- a/sources/AppBundle/Controller/Website/Talks/JoindinAction.php +++ b/sources/AppBundle/Controller/Website/Talks/JoindinAction.php @@ -8,6 +8,7 @@ use AppBundle\Joindin\JoindinTalk; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpFoundation\Response; final class JoindinAction extends AbstractController { @@ -20,10 +21,14 @@ public function __invoke(int $id, string $slug): RedirectResponse { $talk = $this->talkRepository->get($id); - if (null === $talk || $talk->getSlug() != $slug || !$talk->isDisplayedOnHistory()) { + if (null === $talk || !$talk->isDisplayedOnHistory()) { throw $this->createNotFoundException(); } + if ($talk->getSlug() !== $slug) { + return $this->redirectToRoute('talks_joindin', ['id' => $id, 'slug' => $talk->getSlug()], Response::HTTP_MOVED_PERMANENTLY); + } + $stub = $this->joindinTalk->getStubFromTalk($talk); if (null === $stub) { diff --git a/sources/AppBundle/Controller/Website/Talks/ShowAction.php b/sources/AppBundle/Controller/Website/Talks/ShowAction.php index deda565b9..55ac08d25 100644 --- a/sources/AppBundle/Controller/Website/Talks/ShowAction.php +++ b/sources/AppBundle/Controller/Website/Talks/ShowAction.php @@ -29,15 +29,20 @@ public function __invoke(int $id, string $slug): Response { $talk = $this->talkRepository->get($id); - if (null === $talk || $talk->getSlug() != $slug || !$talk->isDisplayedOnHistory()) { + if (null === $talk || !$talk->isDisplayedOnHistory()) { throw $this->createNotFoundException(); } - $speakers = $this->speakerRepository->getSpeakersByTalk($talk); $planning = $this->planningRepository->getByTalk($talk); if ($planning === null) { throw $this->createNotFoundException(); } + + if ($talk->getSlug() !== $slug) { + return $this->redirectToRoute('talks_show', ['id' => $id, 'slug' => $talk->getSlug()], Response::HTTP_MOVED_PERMANENTLY); + } + + $speakers = $this->speakerRepository->getSpeakersByTalk($talk); $event = $this->eventRepository->get($planning->eventId); $comments = $this->joindinComments->getCommentsFromTalk($talk); diff --git a/tests/behat/bootstrap/FeatureContext.php b/tests/behat/bootstrap/FeatureContext.php index 4b96c2a1e..ada57e0da 100644 --- a/tests/behat/bootstrap/FeatureContext.php +++ b/tests/behat/bootstrap/FeatureContext.php @@ -8,7 +8,9 @@ use Behat\Behat\Context\Context; use Behat\Behat\Hook\Scope\BeforeScenarioScope; use Behat\Gherkin\Node\PyStringNode; +use Behat\Hook\AfterScenario; use Behat\Hook\BeforeScenario; +use Behat\Mink\Driver\BrowserKitDriver; use Behat\Mink\Driver\PantherDriver; use Behat\Mink\Element\NodeElement; use Behat\Mink\Exception\ExpectationException; @@ -50,6 +52,28 @@ public function beforeScenarioReloadDatabase(): void $this->databaseManager->reloadDatabase(); } + #[When('I do not follow redirects')] + public function doNotFollowRedirects(): void + { + $driver = $this->minkContext->getSession()->getDriver(); + + if (!$driver instanceof BrowserKitDriver) { + throw new \LogicException('This step requires a non-@javascript scenario.'); + } + + $driver->getClient()->followRedirects(false); + } + + #[AfterScenario] + public function restoreFollowRedirects(): void + { + $driver = $this->minkContext->getSession()->getDriver(); + + if ($driver instanceof BrowserKitDriver) { + $driver->getClient()->followRedirects(true); + } + } + #[Then('simulate the Paybox callback')] public function simulateThePayboxCallback(): void { diff --git a/tests/behat/features/PublicSite/Talks.feature b/tests/behat/features/PublicSite/Talks.feature index db8a89bac..688640229 100644 --- a/tests/behat/features/PublicSite/Talks.feature +++ b/tests/behat/features/PublicSite/Talks.feature @@ -35,3 +35,35 @@ Feature: Site Public - Talks When I go to "/talks/?q=&hPP=7&idx=afup_talks&p=0&fR[event.title][0]=AFUP Day 2019 Lyon&fR[speakers.label][0]=Un Speaker" Then I should see "Les vidéos de Un Speaker" And the response should contain "" + + @reloadDbWithTestData + Scenario: Un talk avec le bon slug répond 200 + Given I do not follow redirects + When I go to "/talks/2-rest-ou-graphql-exemples-illustres-avec-symfony-et-api-platform" + Then the response status code should be 200 + + @reloadDbWithTestData + Scenario: Un talk avec un mauvais slug redirige en 301 vers le slug courant + Given I do not follow redirects + When I go to "/talks/2-mauvais-slug" + Then the response status code should be 301 + And the response header "Location" should match "#^(https?://[^/]+)?/talks/2-rest-ou-graphql-exemples-illustres-avec-symfony-et-api-platform$#" + + @reloadDbWithTestData + Scenario: Le lien joind.in d'un talk avec un mauvais slug redirige en 301 vers le slug courant + Given I do not follow redirects + When I go to "/talks/2-mauvais-slug/joindin" + Then the response status code should be 301 + And the response header "Location" should match "#^(https?://[^/]+)?/talks/2-rest-ou-graphql-exemples-illustres-avec-symfony-et-api-platform/joindin$#" + + @reloadDbWithTestData + Scenario: Un talk inexistant ou non publié répond 404, quel que soit le slug + Given I do not follow redirects + When I go to "/talks/999999-mauvais-slug" + Then the response status code should be 404 + When I go to "/talks/999999-mauvais-slug/joindin" + Then the response status code should be 404 + When I go to "/talks/4-mauvais-slug" + Then the response status code should be 404 + When I go to "/talks/4-mauvais-slug/joindin" + Then the response status code should be 404