From 55acbb8f752499f39def3b521521700f18439f9b Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Mon, 28 Jan 2019 14:59:30 +0100
Subject: [PATCH] hotline #85026 add default option in link selection to domain
 form

---
 VERSIONS_HOTLINE/85026                        |  2 ++
 .../admin/controllers/CatalogueController.php | 33 +++++++++--------
 .../controllers/CatalogueControllerTest.php   | 35 ++++++++++++++++++-
 3 files changed, 54 insertions(+), 16 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/85026

diff --git a/VERSIONS_HOTLINE/85026 b/VERSIONS_HOTLINE/85026
new file mode 100644
index 00000000000..0cc5aa1dad7
--- /dev/null
+++ b/VERSIONS_HOTLINE/85026
@@ -0,0 +1,2 @@
+ - ticket #85026 : Administration : ajout d'une valeur par défaut dans le formulaire de rattachement d'un panier à un  domaine.
+ 
\ No newline at end of file
diff --git a/application/modules/admin/controllers/CatalogueController.php b/application/modules/admin/controllers/CatalogueController.php
index 59e586a3a21..536fcb5aba3 100644
--- a/application/modules/admin/controllers/CatalogueController.php
+++ b/application/modules/admin/controllers/CatalogueController.php
@@ -200,34 +200,37 @@ class Admin_CatalogueController extends ZendAfi_Controller_Action {
     if (!$catalogue = Class_Catalogue::find((int)$this->_getParam("id_catalogue")))
       return $this->_redirect("admin/catalogue/index");
 
-    if ($this->_request->isPost()) {
-      $panier = Class_PanierNotice::find($this->_request->getPost('id_panier'));
-      $catalogue->addPanierNotice($panier)->save();
-      $panier->addCatalogue($catalogue)->save();
-      $panier->index();
-      $this->_helper->notify($this->_('Panier "%s" ajouté', $panier->getLibelle()));
-      $this->_redirect('admin/catalogue/paniers/id_catalogue/' . $catalogue->getId());
-      return;
-    }
-
     if ($id_panier_to_remove = (int)$this->_getParam('remove')) {
       $panier = Class_PanierNotice::find($id_panier_to_remove);
       $catalogue->removePanierNotice($panier)->save();
       $panier->removeCatalogue($catalogue)->save();
 
       $this->_helper->notify($this->_('Panier "%s" retiré', $panier->getLibelle()));
-      $this->_redirect('admin/catalogue/paniers/id_catalogue/' . $catalogue->getId());
+      return $this->_redirect('admin/catalogue/paniers/id_catalogue/' . $catalogue->getId());
+    }
+
+    if (!$this->_request->isPost()) {
+      $this->view->form_paniers = $this->formAjoutPanier($catalogue);
+      $this->view->catalogue = $catalogue;
+      $this->view->titre = $this->_('Paniers du domaine: %s', $catalogue->getLibelle());
       return;
     }
 
-    $this->view->form_paniers = $this->formAjoutPanier($catalogue);
-    $this->view->catalogue = $catalogue;
-    $this->view->titre = $this->_('Paniers du domaine: %s', $catalogue->getLibelle());
+    if (!$panier = Class_PanierNotice::find((int)$this->_request->getPost('id_panier'))) {
+      $this->_helper->notify($this->_('Aucun panier sélectionné pour ajout'));
+      return $this->_redirect('admin/catalogue/paniers/id_catalogue/' . $catalogue->getId());
+    }
+
+    $catalogue->addPanierNotice($panier)->save();
+    $panier->addCatalogue($catalogue)->save();
+    $panier->index();
+    $this->_helper->notify($this->_('Panier "%s" ajouté', $panier->getLibelle()));
+    $this->_redirect('admin/catalogue/paniers/id_catalogue/' . $catalogue->getId());
   }
 
 
   protected function formAjoutPanier($catalogue) {
-    $options = [];
+    $options = ['' => $this->_('Veuiller sélectionner un panier')];
     $paniers = Class_PanierNotice::findAllBelongsToAdmin();
     foreach($paniers as $panier)
       $options[$panier->getId()] = $panier->getLibelleForAdmins();
diff --git a/tests/application/modules/admin/controllers/CatalogueControllerTest.php b/tests/application/modules/admin/controllers/CatalogueControllerTest.php
index 0d36513e06f..a8127f74741 100644
--- a/tests/application/modules/admin/controllers/CatalogueControllerTest.php
+++ b/tests/application/modules/admin/controllers/CatalogueControllerTest.php
@@ -1357,12 +1357,20 @@ class CatalogueControllerPaniersHistoireTest extends AdminCatalogueControllerTes
   public function pageShouldContainsLinkToPreview() {
     $this->assertXPath('//a[contains(@href, "/recherche/simple/id_catalogue/100")]');
   }
+
+
+  /** @test */
+  public function defaultOptionShouldPresent() {
+    $this->assertXpathContentContains('//option', 'Veuiller sélectionner un panier');
+  }
 }
 
 
 
 
-class CatalogueControllerPaniersHistoirePostPanierSelectionSenorTest extends AdminCatalogueControllerTestCase {
+class CatalogueControllerPaniersHistoirePostPanierSelectionSenorTest
+  extends AdminCatalogueControllerTestCase {
+
   public function setUp() {
     parent::setUp();
 
@@ -1399,6 +1407,31 @@ class CatalogueControllerPaniersHistoirePostPanierSelectionSenorTest extends Adm
 
 
 
+class CatalogueControllerPaniersHistoirePostPanierSelectionEmptyTest
+  extends AdminCatalogueControllerTestCase {
+
+  public function setUp() {
+    parent::setUp();
+
+    $this->postDispatch('/admin/catalogue/paniers/id_catalogue/100',
+                        ['id_panier' => '']);
+  }
+
+
+  /** @test */
+  public function answerShouldRedirectToCataloguePanier100() {
+    $this->assertRedirectTo('/admin/catalogue/paniers/id_catalogue/100');
+  }
+
+
+  /** @test */
+  public function messageShouldBeAucunPanierSelectionnePourAjout() {
+    $this->assertFlashMessengerContentContains('Aucun panier sélectionné pour ajout');
+  }
+}
+
+
+
 
 class CatalogueControllerPaniersHistoireRemovePanierJeunesseTest extends AdminCatalogueControllerTestCase {
   public function setUp() {
-- 
GitLab