From 69af1048d940ae242f9191e212025a92b92efeaa Mon Sep 17 00:00:00 2001
From: David Beniamine <david.beniamine@tetras-libre.fr>
Date: Tue, 14 Jun 2022 14:10:54 +0200
Subject: [PATCH] Sendmail on capsule invitation even for registered users

---
 src/Controller/CapsuleEditorController.php    | 44 ++++++++++---------
 .../capsule/editors/email_editor.html.twig    | 11 +++--
 .../CapsuleEditorControllerTest.php           |  2 +-
 translations/messages.en.yaml                 |  4 +-
 translations/messages.fr.yaml                 |  4 +-
 5 files changed, 35 insertions(+), 30 deletions(-)

diff --git a/src/Controller/CapsuleEditorController.php b/src/Controller/CapsuleEditorController.php
index f2685fd..96776eb 100755
--- a/src/Controller/CapsuleEditorController.php
+++ b/src/Controller/CapsuleEditorController.php
@@ -303,12 +303,29 @@ class CapsuleEditorController extends AbstractController
                     ]
                 )
             );
-            return;
-        }
+            $link = null;
+        } else {
+            $link = $this->urlGenerator->generate(
+                'edit_capsule',
+                [ 'path' => $capsule->getLinkPath() ],
+                UrlGeneratorInterface::ABSOLUTE_URL
+            );
 
-        $capsule->addEditor($user_associated_with_email_address);
-        $this->entity_manager->persist($capsule);
-        $this->entity_manager->flush();
+            $capsule->addEditor($user_associated_with_email_address);
+            $this->entity_manager->persist($capsule);
+            $this->entity_manager->flush();
+
+            $this->addFlash(
+                'success',
+                $this->translator->trans(
+                    'editors.add.user.success',
+                    [
+                        'capsule_name' => $capsule->getName(),
+                        'user_email' => $editor_email
+                    ]
+                )
+            );
+        }
 
         $email = (new TemplatedEmail())
             ->to($editor_email)
@@ -317,24 +334,9 @@ class CapsuleEditorController extends AbstractController
             ->context([
                 'user' => $current_user,
                 'capsule' => $capsule,
-                'capsule_edit_link' => $this->urlGenerator->generate(
-                    'edit_capsule',
-                    [ 'path' => $capsule->getLinkPath() ],
-                    UrlGeneratorInterface::ABSOLUTE_URL
-                )
+                'capsule_edit_link' => $link
             ]);
 
         $this->mailer->send($email);
-
-        $this->addFlash(
-            'success',
-            $this->translator->trans(
-                'editors.add.user.success',
-                [
-                    'capsule_name' => $capsule->getName(),
-                    'user_email' => $editor_email
-                ]
-            )
-        );
     }
 }
diff --git a/templates/capsule/editors/email_editor.html.twig b/templates/capsule/editors/email_editor.html.twig
index 02ed404..b14f0fe 100644
--- a/templates/capsule/editors/email_editor.html.twig
+++ b/templates/capsule/editors/email_editor.html.twig
@@ -13,13 +13,16 @@
                 <p class="alert">
                     {{ 'editors.add.user.email.text'|trans(
                         {'%user_name%': user.getFullName(), '%capsule_name%': capsule.getName()}) }}
-                    <a href="{{ capsule_edit_link }}">
-                        {{ 'editors.add.user.email.link'|trans }}
-                    </a>
+                    {% if capsule_edit_link %}
+                        {{ 'editors.add.user.email.text_link' | trans }}
+                        <a href="{{ capsule_edit_link }}">
+                            {{ 'editors.add.user.email.link'|trans }}
+                        </a>
+                    {% endif %}
                 </p>
 
                 <p>{{ 'general.greeting'|trans }}</p>
             </div>
         </div>
     </body>
-</html>
\ No newline at end of file
+</html>
diff --git a/tests/functional/CapsuleEditorControllerTest.php b/tests/functional/CapsuleEditorControllerTest.php
index d238202..9ecc287 100644
--- a/tests/functional/CapsuleEditorControllerTest.php
+++ b/tests/functional/CapsuleEditorControllerTest.php
@@ -183,7 +183,7 @@ class CapsuleEditorControllerTest extends WebTestCase
         $form['capsule_editors_form[email]'] = $this->editor_non_author->getEmail();
         $this->client->submit($form);
 
-        $this->assertEmailCount(0);
+        $this->assertEmailCount(1);
         $this->assertResponseRedirects($uri, 302);
 
         $this->client->followRedirect();
diff --git a/translations/messages.en.yaml b/translations/messages.en.yaml
index bc361dc..8f661b7 100644
--- a/translations/messages.en.yaml
+++ b/translations/messages.en.yaml
@@ -143,7 +143,7 @@ editors:
         email:
           title: Invitation to edit a MemoRekall capsule
           text: You have been added by %user_name% as editor of the capsule "%capsule_name%".
-            In order to access and edit it, you first need to register on MemoRekall. Please follow this link to
+          text_link: In order to access and edit it, you first need to register on MemoRekall. Please follow this link to
           link_name: register
       user:
         success: The user user_email is now an editor of the capsule capsule_name.
@@ -201,4 +201,4 @@ groups:
     success: Group group_name removed successfully
   filter:
     label: Filter by group
-    no_filter: Show all
\ No newline at end of file
+    no_filter: Show all
diff --git a/translations/messages.fr.yaml b/translations/messages.fr.yaml
index 1df2832..8e4a547 100644
--- a/translations/messages.fr.yaml
+++ b/translations/messages.fr.yaml
@@ -148,7 +148,7 @@ editors:
         email:
           title: Invitation pour éditer une capsule sur MemoRekall
           text: Vous avez été ajouté par %user_name% en tant qu'éditeur de la capsule %capsule_name%.
-            Avant de pouvoir y accéder et la modifier, vous devez d'abord créer un compte sur MemoRekall. Veuillez suivre ce lien pour
+          text_link: Avant de pouvoir y accéder et la modifier, vous devez d'abord créer un compte sur MemoRekall. Veuillez suivre ce lien pour
           link_name: créer votre compte
       user:
         success: L'utilisateur user_email est maintenant éditeur de la capsule capsule_name.
@@ -206,4 +206,4 @@ groups:
     success: Le groupe group_name a bien été supprimé
   filter:
     label: Filtrer par groupe
-    no_filter: Tout afficher
\ No newline at end of file
+    no_filter: Tout afficher
-- 
GitLab