From c84847b21cc6e621b32d0f9f055d6bd9c6995364 Mon Sep 17 00:00:00 2001
From: Fabrice Gangler <fabrice.gangler@adullact.org>
Date: Tue, 18 Mar 2025 13:37:30 +0100
Subject: [PATCH] test(organization): add first tests (change locale in url &
 variation)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

 ✔ All organizations page allow to change locale in url
 ✔ One organization page allow to truncate url to redirect to all organizations page
 ✔ One organization page allow to change locale in url
---
 .../OrganizationDisplayController.php         | 67 ++++++++++++-
 .../FunctionalTestOrganizationPagesTest.php   | 97 +++++++++++++++++++
 .../User/FunctionalTestCreateAccountTest.php  |  2 +-
 3 files changed, 164 insertions(+), 2 deletions(-)
 create mode 100644 webapp/tests/Functional/Organization/FunctionalTestOrganizationPagesTest.php

diff --git a/webapp/src/Controller/Organization/OrganizationDisplayController.php b/webapp/src/Controller/Organization/OrganizationDisplayController.php
index 56babae..49a976d 100644
--- a/webapp/src/Controller/Organization/OrganizationDisplayController.php
+++ b/webapp/src/Controller/Organization/OrganizationDisplayController.php
@@ -40,6 +40,32 @@
 )]
 class OrganizationDisplayController extends AbstractController
 {
+    #[Route(
+        path: [
+            'en' => '/{_locale}/organisation/{id}/{slug}/',
+            'fr' => '/{_locale}/organization/{id}/{slug}/',
+        ],
+        name: 'app_anonymous_organization_display_one_allow_to_change_locale_in_url',
+        methods: ['GET', 'HEAD']
+    )]
+    public function displayOneOrganizationPageAllowToChangeLocaleInUrl(
+        string $_locale,
+        Organization $organization,
+    ): RedirectResponse {
+        return new RedirectResponse(
+            url: $this->generateUrl(
+                route: 'app_anonymous_organization_display_one_organization',
+                parameters: [
+                    '_locale' => $_locale,
+                    'id' => $organization->getId(),
+                    'slug' => $organization->getSlug(),
+                ],
+            ),
+            status: Response::HTTP_PERMANENTLY_REDIRECT,  // 308 Permanent Redirect
+        );
+    }
+
+
     #[Route(
         path: [
             'en' => '/{_locale}/organization/{id}/{slug}/',
@@ -48,7 +74,7 @@ class OrganizationDisplayController extends AbstractController
         name: 'app_anonymous_organization_display_one_organization',
         methods: ['GET', 'HEAD']
     )]
-    public function displayOneOrganization(
+    public function displayOneOrganizationPage(
         string $_locale,
         string $slug,
         Organization $organization,
@@ -84,6 +110,45 @@ public function displayOneOrganization(
     }
 
 
+    #[Route(
+        path: [
+            'en' => '/{_locale}/organisations/',
+            'fr' => '/{_locale}/organizations/',
+        ],
+        name: 'app_anonymous_organization_display_all_allow_to_change_locale_in_url',
+        methods: ['GET', 'HEAD']
+    )]
+    public function allOrganizationsPageAllowToChangeLocaleInUrl(string $_locale): RedirectResponse
+    {
+        return new RedirectResponse(
+            url: $this->generateUrl(
+                route: 'app_anonymous_organization_display_all_organization',
+                parameters: ['_locale' => "$_locale"],
+            ),
+            status: Response::HTTP_PERMANENTLY_REDIRECT,
+        );
+    }
+
+    #[Route(
+        path: [
+            'en' => '/{_locale}/organization/',
+            'fr' => '/{_locale}/organisation/',
+        ],
+        name: 'app_anonymous_organization_display_all_fix_url_typo',
+        methods: ['GET', 'HEAD']
+    )]
+    public function allOrganizationsPageFixUrlTypo(string $_locale): RedirectResponse
+    {
+        return new RedirectResponse(
+            url: $this->generateUrl(
+                route: 'app_anonymous_organization_display_all_organization',
+                parameters: ['_locale' => "$_locale"],
+            ),
+            status: Response::HTTP_PERMANENTLY_REDIRECT,
+        );
+    }
+
+
     #[Route(
         path: [
             'en' => '/{_locale}/organizations/',
diff --git a/webapp/tests/Functional/Organization/FunctionalTestOrganizationPagesTest.php b/webapp/tests/Functional/Organization/FunctionalTestOrganizationPagesTest.php
new file mode 100644
index 0000000..e4b4fac
--- /dev/null
+++ b/webapp/tests/Functional/Organization/FunctionalTestOrganizationPagesTest.php
@@ -0,0 +1,97 @@
+<?php
+
+/*
+ * This file is part of the Comptoir-du-Libre software.
+ * <https://gitlab.adullact.net/Comptoir/comptoir-du-libre>
+ *
+ * Copyright (c) ADULLACT   <https://adullact.org>
+ *               Association des Développeurs et Utilisateurs de Logiciels Libres
+ *               pour les Administrations et les Collectivités Territoriales
+ *
+ * Comptoir-du-Libre is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public Organization as published
+ * by the Free Software Foundation, either version 3 of the Organization, or
+ * (at your option) any later version.
+ *
+ * You should have received a copy of the GNU Affero General Public Organization
+ * along with this software. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>.
+ */
+
+declare(strict_types=1);
+
+namespace App\Tests\Functional\Organization;
+
+use App\DataFixtures\AppOrganizationFixtures;
+use App\Tests\Functional\TestHelperBreadcrumbTrait;
+use App\Tests\Functional\TestHelperTrait;
+use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
+
+/**
+ * @group gogogo
+ */
+class FunctionalTestOrganizationPagesTest extends WebTestCase
+{
+    use TestHelperTrait;
+    use TestHelperBreadcrumbTrait;
+
+
+    public function testAllOrganizationsPageAllowToChangeLocaleInUrl(): void
+    {
+        $client = static::createClient();
+        $this->checkRedirectionToNewUrl(
+            client: $client,
+            currentUrl: "/en/organisations/",
+            expectedNewUrl: "/en/organizations/",
+            expectedCurrentRoute: 'app_anonymous_organization_display_all_allow_to_change_locale_in_url',
+            expectedNewRoute: 'app_anonymous_organization_display_all_organization',
+        );
+        $this->checkRedirectionToNewUrl(
+            client: $client,
+            currentUrl: "/fr/organizations/",
+            expectedNewUrl: "/fr/organisations/",
+            expectedCurrentRoute: 'app_anonymous_organization_display_all_allow_to_change_locale_in_url',
+            expectedNewRoute: 'app_anonymous_organization_display_all_organization',
+        );
+    }
+
+    public function testOneOrganizationPageAllowToTruncateUrlToRedirectToAllOrganizationsPage(): void
+    {
+        $client = static::createClient();
+        $this->checkRedirectionToNewUrl(
+            client: $client,
+            currentUrl: "/en/organization/",
+            expectedNewUrl: "/en/organizations/",
+            expectedCurrentRoute: 'app_anonymous_organization_display_all_fix_url_typo',
+            expectedNewRoute: 'app_anonymous_organization_display_all_organization',
+        );
+        $this->checkRedirectionToNewUrl(
+            client: $client,
+            currentUrl: "/fr/organisation/",
+            expectedNewUrl: "/fr/organisations/",
+            expectedCurrentRoute: 'app_anonymous_organization_display_all_fix_url_typo',
+            expectedNewRoute: 'app_anonymous_organization_display_all_organization',
+        );
+    }
+
+
+    public function testOneOrganizationPageAllowToChangeLocaleInUrl(): void
+    {
+        $organizationId = AppOrganizationFixtures::ORGANIZATIONS[2]['id'];
+        $organizationSlug = AppOrganizationFixtures::ORGANIZATIONS[$organizationId]['slug'];
+        $client = static::createClient();
+        $this->checkRedirectionToNewUrl(
+            client: $client,
+            currentUrl: "/en/organisation/$organizationId/$organizationSlug/",
+            expectedNewUrl: "/en/organization/$organizationId/$organizationSlug/",
+            expectedCurrentRoute: 'app_anonymous_organization_display_one_allow_to_change_locale_in_url',
+            expectedNewRoute: 'app_anonymous_organization_display_one_organization',
+        );
+        $this->checkRedirectionToNewUrl(
+            client: $client,
+            currentUrl: "/fr/organization/$organizationId/$organizationSlug/",
+            expectedNewUrl: "/fr/organisation/$organizationId/$organizationSlug/",
+            expectedCurrentRoute: 'app_anonymous_organization_display_one_allow_to_change_locale_in_url',
+            expectedNewRoute: 'app_anonymous_organization_display_one_organization',
+        );
+    }
+}
diff --git a/webapp/tests/Functional/User/FunctionalTestCreateAccountTest.php b/webapp/tests/Functional/User/FunctionalTestCreateAccountTest.php
index 0487f13..eba9458 100644
--- a/webapp/tests/Functional/User/FunctionalTestCreateAccountTest.php
+++ b/webapp/tests/Functional/User/FunctionalTestCreateAccountTest.php
@@ -31,7 +31,7 @@
 use Symfony\Component\HttpFoundation\Response;
 
 /**
- * @group gogogo
+ * @group gogogo2
  */
 class FunctionalTestCreateAccountTest extends WebTestCase
 {
-- 
GitLab