From 691c76df67fe830fd5a6ad1d78d575ae1a41cacb Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@afi-sa.fr>
Date: Wed, 26 Oct 2016 17:01:11 +0200
Subject: [PATCH] dev #49273 send mail for unregistration

---
 .../opac/controllers/AbonneController.php     |   2 +
 library/Class/Formation/AbstractMail.php      | 117 ++++++++++++++++++
 library/Class/Formation/RegistrationMail.php  |  92 +++-----------
 .../Class/Formation/UnregistrationMail.php    |  52 ++++++++
 .../AbonneControllerFormationsTest.php        |  55 +++++++-
 5 files changed, 241 insertions(+), 77 deletions(-)
 create mode 100644 library/Class/Formation/AbstractMail.php
 create mode 100644 library/Class/Formation/UnregistrationMail.php

diff --git a/application/modules/opac/controllers/AbonneController.php b/application/modules/opac/controllers/AbonneController.php
index 131d0e441ca..e7714285214 100644
--- a/application/modules/opac/controllers/AbonneController.php
+++ b/application/modules/opac/controllers/AbonneController.php
@@ -97,6 +97,8 @@ class AbonneController extends ZendAfi_Controller_Action {
       $this->_helper->notify($this->_('Vous n\'êtes plus inscrit à la session du %s de la formation %s',
                                      $this->view->humanDate($session->getDateDebut(), 'd MMMM YYYY'),
                                      $session->getLibelleFormation()));
+
+      (new Class_Formation_UnregistrationMail($session, $this->_user))->send();
     };
 
     $this->_redirect('/formations');
diff --git a/library/Class/Formation/AbstractMail.php b/library/Class/Formation/AbstractMail.php
new file mode 100644
index 00000000000..cdd0a2c1d99
--- /dev/null
+++ b/library/Class/Formation/AbstractMail.php
@@ -0,0 +1,117 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+abstract class Class_Formation_AbstractMail {
+  use Trait_Translator;
+
+  protected
+    $_training_session,
+    $_subscriber;
+
+  public function __construct($session, $user) {
+    $this->_training_session = $session;
+    $this->_subscriber = $user;
+  }
+
+
+  public function send() {
+    return $this
+      ->_sendMailToSubscriber()
+      ->_sendMailToTeachers();
+  }
+
+
+  protected function _getSessionUrl() {
+    return Class_Url::absolute(['controller' => 'formations',
+                                'action' => 'detail-session',
+                                'id' => $this->_training_session->getId()],
+                               null,
+                               true);
+  }
+
+
+  protected function _getEditUserUrl() {
+    return Class_Url::absolute(['module' => 'admin',
+                                'controller' => 'users',
+                                'action' => 'edit',
+                                'id' => $this->_subscriber->getId()],
+                               null,
+                               true);
+  }
+
+
+  protected function _getSubscriberMailInfo() {
+    return ($this->_subscriber->hasMail()
+            ? $this->_subscriber->getMail()
+            : $this->_('non renseigné'));
+      }
+
+
+  protected function _sendMail($recipients, $subject, $body) {
+    if (!$recipients = array_filter($recipients))
+      return $this;
+    $mail = new ZendAfi_Mail('utf8');
+
+    foreach($recipients as  $recipient)
+      $mail->addTo($recipient);
+
+    $mail
+      ->setFrom('no-reply@' . Class_AdminVar::get('NOM_DOMAINE'))
+      ->setSubject($subject)
+      ->setBodyHtml($body)
+      ->send();
+
+    return $this;
+  }
+
+
+  protected function _sendMailToSubscriber() {
+    return $this->_sendMail([$this->_subscriber->getMail()],
+                            $this->_getSubscriberSubject(),
+                            $this->_getSubscriberBody());
+  }
+
+
+  protected function _sendMailToTeachers() {
+    $recipients = (new Storm_Model_Collection($this->_training_session->getIntervenants()))
+      ->collect('mail')
+      ->getArrayCopy();
+
+    return $this->_sendMail($recipients,
+                            $this->_getTeachersSubject(),
+                            $this->_getTeachersBody());
+  }
+
+
+  abstract protected function _getSubscriberSubject();
+
+
+  abstract protected function _getSubscriberBody();
+
+
+  abstract protected function _getTeachersSubject();
+
+
+  abstract protected function _getTeachersBody();
+
+}
+?>
\ No newline at end of file
diff --git a/library/Class/Formation/RegistrationMail.php b/library/Class/Formation/RegistrationMail.php
index 0dd00899de5..da3b55618ea 100644
--- a/library/Class/Formation/RegistrationMail.php
+++ b/library/Class/Formation/RegistrationMail.php
@@ -20,89 +20,33 @@
  */
 
 
-class Class_Formation_RegistrationMail {
-  use Trait_Translator;
-
-  protected
-    $_training_session,
-    $_subscriber;
-
-  public function __construct($session, $user) {
-    $this->_training_session = $session;
-    $this->_subscriber = $user;
-  }
-
-
-  public function send() {
-    return $this
-      ->_sendMailToSubscriber()
-      ->_sendMailToTeachers();
-  }
-
-
-  protected function _getSessionUrl() {
-    return Class_Url::absolute(['controller' => 'formations',
-                                'action' => 'detail-session',
-                                'id' => $this->_training_session->getId()],
-                               null,
-                               true);
+class Class_Formation_RegistrationMail extends Class_Formation_AbstractMail {
+  protected function _getSubscriberSubject() {
+    return $this->_('Confirmation d\'inscription à la formation "%s"',
+                    $this->_training_session->getLibelleFormation());
   }
 
 
-  protected function _newMail() {
-    return (new ZendAfi_Mail('utf8'))
-      ->setFrom('no-reply@' . Class_AdminVar::get('NOM_DOMAINE'));
+  protected function _getSubscriberBody() {
+    return $this->_('Bonjour,<br><br>nous vous confirmons votre inscription à la formation <a href="%s">"%s"</a>',
+                    $this->_getSessionUrl(),
+                    $this->_training_session->getLibelleFormation());
   }
 
 
-  protected function _sendMailToSubscriber() {
-    if (!$this->_subscriber->hasMail())
-      return $this;
-
-    $this->_newMail()
-     ->addTo($this->_subscriber->getMail())
-     ->setSubject($this->_('Confirmation d\'inscription à la formation "%s"',
-                           $this->_training_session->getLibelleFormation()))
-     ->setBodyHtml($this->_('Bonjour,<br><br>nous vous confirmons votre inscription à la formation <a href="%s">"%s"</a>',
-                            $this->_getSessionUrl(),
-                            $this->_training_session->getLibelleFormation()))
-     ->send();
-    return $this;
+  protected function _getTeachersSubject() {
+    return $this->_('Nouvelle inscription à la formation "%s"',
+                    $this->_training_session->getLibelleFormation());
   }
 
 
-  protected function _sendMailToTeachers() {
-    $mail = $this->_newMail();
-
-    $recipients = (new Storm_Model_Collection($this->_training_session->getIntervenants()))
-      ->select('hasMail')
-      ->collect('mail');
-
-    if ($recipients->isEmpty())
-      return $this;
-
-    foreach($recipients as  $recipient)
-      $mail->addTo($recipient);
-
-    $mail
-      ->setSubject($this->_('Nouvelle inscription à la formation "%s"',
-                            $this->_training_session->getLibelleFormation()))
-      ->setBodyHtml($this->_('L\'usager <a href="%s">%s (courriel: %s)</a> vient de s\'inscrire à la formation <a href="%s">%s</a>',
-                             Class_Url::absolute(['module' => 'admin',
-                                                  'controller' => 'users',
-                                                  'action' => 'edit',
-                                                  'id' => $this->_subscriber->getId()],
-                                                 null,
-                                                 true),
-                             $this->_subscriber->getNomComplet(),
-                             ($this->_subscriber->hasMail()
-                                ? $this->_subscriber->getMail()
-                                : $this->_('non renseigné')),
-                             $this->_getSessionUrl(),
-                             $this->_training_session->getLibelleFormation()))
-      ->send();
-
-    return $this;
+  protected function _getTeachersBody() {
+    return $this->_('L\'usager <a href="%s">%s (courriel: %s)</a> vient de s\'inscrire à la formation <a href="%s">%s</a>',
+                    $this->_getEditUserUrl(),
+                    $this->_subscriber->getNomComplet(),
+                    $this->_getSubscriberMailInfo(),
+                    $this->_getSessionUrl(),
+                    $this->_training_session->getLibelleFormation());
   }
 
 }
diff --git a/library/Class/Formation/UnregistrationMail.php b/library/Class/Formation/UnregistrationMail.php
new file mode 100644
index 00000000000..ea4bb5ed944
--- /dev/null
+++ b/library/Class/Formation/UnregistrationMail.php
@@ -0,0 +1,52 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+class Class_Formation_UnregistrationMail extends Class_Formation_AbstractMail {
+  protected function _getSubscriberSubject() {
+    return $this->_('Confirmation de désinscription à la formation "%s"',
+                    $this->_training_session->getLibelleFormation());
+  }
+
+
+  protected function _getSubscriberBody() {
+    return $this->_('Bonjour,<br><br>nous vous confirmons que vous n\'êtes plus inscrit à la formation <a href="%s">"%s"</a>',
+                    $this->_getSessionUrl(),
+                    $this->_training_session->getLibelleFormation());
+  }
+
+
+  protected function _getTeachersSubject() {
+    return $this->_('Désinscription de la formation "%s"',
+                    $this->_training_session->getLibelleFormation());
+  }
+
+
+  protected function _getTeachersBody() {
+    return $this->_('L\'usager <a href="%s">%s (courriel: %s)</a> s\'est désinscrit de la formation <a href="%s">%s</a>',
+                    $this->_getEditUserUrl(),
+                    $this->_subscriber->getNomComplet(),
+                    $this->_getSubscriberMailInfo(),
+                    $this->_getSessionUrl(),
+                    $this->_training_session->getLibelleFormation());
+  }
+}
+?>
\ No newline at end of file
diff --git a/tests/application/modules/opac/controllers/AbonneControllerFormationsTest.php b/tests/application/modules/opac/controllers/AbonneControllerFormationsTest.php
index a6a2cf68a7d..de283d41216 100644
--- a/tests/application/modules/opac/controllers/AbonneControllerFormationsTest.php
+++ b/tests/application/modules/opac/controllers/AbonneControllerFormationsTest.php
@@ -58,10 +58,14 @@ abstract class AbstractAbonneControllerFormationsTestCase extends AbstractContro
                                      'login' => 'Amadou',
                                      'password' => 'pwd',
                                      'mail' => 'amadou@afi-sa.fr',
-                                     'id_abon' => 435]);
+                                     'bib' => $this->fixture('Class_Bib',
+                                                             ['id' => 1,
+                                                              'libelle' => 'annecy']),
+                                     'idabon' => 435]);
     $this->_amadou
       ->beAbonneSIGB()
-      ->setUserGroups([$this->fixture('Class_UserGroup',['id' => 23])->addRightSuivreFormation()]);
+      ->setUserGroups([$this->fixture('Class_UserGroup',['id' => 23])->addRightSuivreFormation()])
+      ->assertSave();
 
     ZendAfi_Auth::getInstance()->logUser($this->_amadou);
 
@@ -789,9 +793,13 @@ class AbonneControllerFormationsInscritSessionWithoutRightSuivreFormationTest ex
 
 
 class AbonneControllerFormationsAmadouDesinscritSessionJuilletPythonTest extends AbstractAbonneControllerFormationsTestCase {
+  protected $_mails;
+
   public function setUp() {
     parent::setUp();
-    $this->dispatch('/opac/abonne/desinscrire-session/id/121');
+    $this->dispatch('/opac/abonne/desinscrire-session/id/121', true);
+
+    $this->_mails = $this->_mail_transport->getSentMails();
   }
 
   /** @test */
@@ -812,6 +820,47 @@ class AbonneControllerFormationsAmadouDesinscritSessionJuilletPythonTest extends
     $this->assertEquals(121, $inscription->getSessionFormationId());
     $this->assertEquals(435, $inscription->getStagiaireId());
   }
+
+
+  /** @test */
+  public function flashMessengerShouldContainsUnregisterNotification() {
+    $this->assertFlashMessengerContentContains('Vous n\'êtes plus inscrit');
+  }
+
+
+  /** @test */
+  public function firstMailRecipientShouldBeAmadouAtAfi() {
+    $this->assertEquals(['amadou@afi-sa.fr'], $this->_mails[0]->getRecipients());
+  }
+
+
+  /** @test */
+  public function firstMailSubjectShouldContainsUnregistrationConfirmation() {
+    $this->assertEquals('Confirmation de désinscription à la formation "Learn Python"',
+                        $this->_mails[0]->getSubject());
+  }
+
+
+  /** @test */
+  public function firstMailBodyShouldContainsUnregistrationConfirmation() {
+    $this->assertContains('vous n\'êtes plus inscrit à la formation',
+                          quoted_printable_decode($this->_mails[0]->getBodyHtml(true)));
+  }
+
+
+
+  /** @test */
+  public function secondMailSubjectShouldBeUnregistration() {
+    $this->assertEquals('Désinscription de la formation "Learn Python"',
+                        $this->_mails[1]->getSubject());
+  }
+
+
+  /** @test */
+  public function secondMailContentShouldBeUserHaveUnregistered() {
+    $this->assertEquals('L\'usager <a href="http://localhost' . BASE_URL .'/admin/users/edit/id/435">Ama Dou (courriel: amadou@afi-sa.fr)</a> s\'est désinscrit de la formation <a href="http://localhost' . BASE_URL . '/formations/detail-session/id/121">Learn Python</a>',
+                        quoted_printable_decode($this->_mails[1]->getBodyHtml(true)));
+  }
 }
 
 
-- 
GitLab