From 29b6ffcf206d617dc395d355e47090b57ae7e305 Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Fri, 31 Mar 2017 09:21:54 +0200
Subject: [PATCH] CIP #57952 tests maintenance

---
 .../admin/controllers/WidgetController.php    | 44 ++++++++++++-
 .../{widget/edit.phtml => render-form.phtml}  |  0
 .../ZendAfi/Form/Configuration/Widget/Add.php | 36 +++++++++++
 .../Form/Configuration/Widget/Base.php        |  3 +-
 .../ZendAfi/View/Helper/FonctionsAdmin.php    |  1 +
 .../controllers/AccueilControllerTest.php     | 39 ------------
 .../controllers/WidgetControllerTest.php      | 61 ++++++++++++++++++-
 7 files changed, 141 insertions(+), 43 deletions(-)
 rename application/modules/admin/views/scripts/{widget/edit.phtml => render-form.phtml} (100%)
 create mode 100644 library/ZendAfi/Form/Configuration/Widget/Add.php
 delete mode 100644 tests/application/modules/admin/controllers/AccueilControllerTest.php

diff --git a/application/modules/admin/controllers/WidgetController.php b/application/modules/admin/controllers/WidgetController.php
index c14151021c4..58c9d3ae93f 100644
--- a/application/modules/admin/controllers/WidgetController.php
+++ b/application/modules/admin/controllers/WidgetController.php
@@ -34,6 +34,38 @@ class Admin_WidgetController extends ZendAfi_Controller_Action {
   }
 
 
+  public function indexAction() {
+    return $this->_forward('index', 'index');
+  }
+
+
+  public function addAction() {
+    if((!$division = $this->_getParam('division'))
+       || (!$sibling = $this->_getParam('after'))
+       || (!$profil = Class_Profil::find($this->_getParam('id_profil'))))
+      return $this->_redirectClose($this->_getReferer());
+
+    if ($this->_request->isPost()) {
+      $new_id = $profil->createNewModuleAccueilId();
+      $profil->updateModuleConfigAccueil($new_id,
+                                         ['type_module' => $this->_getParam('module_type'),
+                                          'division' => $division]);
+
+      $profil->moveModuleOldDivPosNewDivPos($division,
+                                            $profil->getModulePositionInDiv($new_id, $division),
+                                            $division,
+                                            $profil->getModulePositionInDiv($sibling, $division) + 1);
+      $profil->save();
+      $this->_setParam('id', $new_id);
+      return $this->_edit(new Class_Systeme_Widget_Widget());
+    }
+
+    $this->view->form = ZendAfi_Form_Configuration_Widget_Add::newWith([]);
+    $this->view->form->setAction($this->view->url());
+    $this->renderScript('render-form.phtml');
+  }
+
+
   public function editMenuAction() {
     return $this->_edit(new Class_Systeme_Widget_Menu());
   }
@@ -61,10 +93,18 @@ class Admin_WidgetController extends ZendAfi_Controller_Action {
     $this->view->form = $form = call_user_func_array([$widget->getForm(), 'newWith'], [$widget->forForm()]);
     $form->setAction($this->view->url());
 
-    if ($this->_request->isPost() && $form->isValid($this->_request->getPost()))
+    if ($this->_request->isPost() && $form->isValid($this->_getParams($widget)))
       return $this->_update($widget->setNewDatas($this->_request->getPost()));
 
-    $this->renderScript('widget/edit.phtml');
+    $this->renderScript('render-form.phtml');
+  }
+
+
+  protected function _getParams($widget) {
+    if('add' == $this->_request->getActionName())
+      return $widget->forForm();
+
+    return $this->_request->getPost();
   }
 
 
diff --git a/application/modules/admin/views/scripts/widget/edit.phtml b/application/modules/admin/views/scripts/render-form.phtml
similarity index 100%
rename from application/modules/admin/views/scripts/widget/edit.phtml
rename to application/modules/admin/views/scripts/render-form.phtml
diff --git a/library/ZendAfi/Form/Configuration/Widget/Add.php b/library/ZendAfi/Form/Configuration/Widget/Add.php
new file mode 100644
index 00000000000..5410991ff28
--- /dev/null
+++ b/library/ZendAfi/Form/Configuration/Widget/Add.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * Copyright (c) 2012-2017, 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 ZendAfi_Form_Configuration_Widget_Add extends ZendAfi_Form_Configuration_Widget_Base {
+  public function init() {
+    parent::init();
+
+    $widgets = Class_Systeme_ModulesAccueil::getWidgets();
+
+    $this->addElement('select',
+                     'module_type',
+                     ['label' => $this->_('Type de boite à ajouter'),
+                      'registerInArrayValidator' => false,
+                      'multiOptions' => $widgets])
+         ->addToSelectionGroup(['module_type']);
+  }
+}
diff --git a/library/ZendAfi/Form/Configuration/Widget/Base.php b/library/ZendAfi/Form/Configuration/Widget/Base.php
index d93f1d4ada9..42ea2b41da2 100644
--- a/library/ZendAfi/Form/Configuration/Widget/Base.php
+++ b/library/ZendAfi/Form/Configuration/Widget/Base.php
@@ -28,7 +28,8 @@ class ZendAfi_Form_Configuration_Widget_Base extends ZendAfi_Form {
     $this
       ->addElement('text',
                    'titre',
-                   ['label' => $this->_('Titre de la boite')])
+                   ['label' => $this->_('Titre de la boite'),
+                    'placeholder' => $this->_('Titre de la boite')])
 
       ->addElement('select',
                    'boite',
diff --git a/library/ZendAfi/View/Helper/FonctionsAdmin.php b/library/ZendAfi/View/Helper/FonctionsAdmin.php
index 43225ca6b8f..1e4d5630013 100644
--- a/library/ZendAfi/View/Helper/FonctionsAdmin.php
+++ b/library/ZendAfi/View/Helper/FonctionsAdmin.php
@@ -102,6 +102,7 @@ class ZendAfi_View_Helper_FonctionsAdmin extends ZendAfi_View_Helper_BaseHelper
                              'controller' => 'widget',
                              'action' => 'add',
                              'after' => $this->id_module,
+                             'division' => $this->division,
                              'id_profil' => $this->_getIdProfil()],
                             null, true);
 
diff --git a/tests/application/modules/admin/controllers/AccueilControllerTest.php b/tests/application/modules/admin/controllers/AccueilControllerTest.php
deleted file mode 100644
index 848e158b760..00000000000
--- a/tests/application/modules/admin/controllers/AccueilControllerTest.php
+++ /dev/null
@@ -1,39 +0,0 @@
-<?php
-
-class AccueilControllerAddBoxInFrontTest extends AccueilControllerConfigBoiteLoginTestCase {
-
-  protected $profil;
-
-  public function setUp() {
-    parent::setUp();
-    $this->postDispatch('/admin/accueil/add-block', ['division' => 4,
-                                                     'module_type' => 'KIOSQUE',
-                                                     'id_module' => 32,
-                                                     'titre' => 'mon titre',
-                                                     'boite' => '',
-                                                     ]);
-    $id_profil = Class_Profil::getCurrentProfil()->getId();
-    Class_Profil::clearCache();
-    $this->profil = Class_Profil::find($id_profil);
-  }
-
-  /** @test */
-  public function currentProfilShouldHaveTitre() {
-    $this->assertEquals('mon titre', $this->profil->getCfgAccueilAsArray()['modules'][1]['preferences']['titre']);
-  }
-
-  /** @test */
-  public function currentProfilShouldHaveTypeKiosque() {
-    $this->assertEquals('KIOSQUE', $this->profil->getCfgAccueilAsArray()['modules'][1]['type_module']);
-  }
-
-  /** @test */
-  public function currentProfilShouldHaveSlideShow() {
-    $this->assertEquals('slide_show', $this->profil->getCfgAccueilAsArray()['modules'][1]['preferences']['style_liste']);
-  }
-
-  /** @test */
-  public function currentProfilShouldHaveNbNotices() {
-    $this->assertEquals(20, $this->profil->getCfgAccueilAsArray()['modules'][1]['preferences']['nb_notices']);
-  }
-}
diff --git a/tests/application/modules/admin/controllers/WidgetControllerTest.php b/tests/application/modules/admin/controllers/WidgetControllerTest.php
index f7c104a7f43..9e80324eb25 100644
--- a/tests/application/modules/admin/controllers/WidgetControllerTest.php
+++ b/tests/application/modules/admin/controllers/WidgetControllerTest.php
@@ -1890,6 +1890,65 @@ class WidgetControllerAddActionLinkTest extends WidgetControllerWidgetConfigurat
 
   /** @test */
   public function linkAddWidgetShouldBePresent() {
-    $this->assertXPath('//a[contains(@href, "admin/widget/add/after/6/id_profil/5")]');
+    $this->assertXPath('//a[contains(@href, "admin/widget/add/after/6/division/3/id_profil/5")]');
+  }
+}
+
+
+
+
+class WidgetControllerAddActionDispatchTest extends WidgetControllerWidgetConfigurationTestCase {
+  public function setUp() {
+    parent::setUp();
+    $this->dispatch('/admin/widget/add/after/6/division/3/id_profil/5', true);
+  }
+
+
+  /** @test */
+  public function submitButtonShouldBePresent() {
+    $this->assertXpath('//button[@type="submit"]');
+  }
+}
+
+
+
+class WidgetControllerAddActionPostDispatchTest extends WidgetControllerWidgetConfigurationTestCase {
+  public function setUp() {
+    parent::setUp();
+    $this->postDispatch('/admin/widget/add/after/6/division/3/id_profil/5',
+                        ['titre' => 'mon titre',
+                         'boite' => 'red_widget',
+                         'module_type' => 'KIOSQUE']);
+    Class_Profil::clearCache();
+  }
+
+
+  /** @test */
+  public function shouldRedirect() {
+    $this->assertRedirect();
+  }
+
+
+  /** @test */
+  public function currentProfilShouldHaveTitre() {
+    $this->assertEquals('mon titre', $this->_profil->getCfgAccueilAsArray()['modules'][1]['preferences']['titre']);
+  }
+
+
+  /** @test */
+  public function currentProfilShouldHaveTypeKiosque() {
+    $this->assertEquals('KIOSQUE', $this->_profil->getCfgAccueilAsArray()['modules'][1]['type_module']);
+  }
+
+
+  /** @test */
+  public function currentProfilShouldHaveSlideShow() {
+    $this->assertEquals('slide_show', $this->_profil->getCfgAccueilAsArray()['modules'][1]['preferences']['style_liste']);
+  }
+
+
+  /** @test */
+  public function currentProfilShouldHaveNbNotices() {
+    $this->assertEquals(20, $this->_profil->getCfgAccueilAsArray()['modules'][1]['preferences']['nb_notices']);
   }
 }
\ No newline at end of file
-- 
GitLab