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