From 989d607e2ca5cdf3d771640065645dd5f8e16fb8 Mon Sep 17 00:00:00 2001
From: efalcy <efalcy@afi-sa.fr>
Date: Fri, 23 Oct 2015 17:51:56 +0200
Subject: [PATCH] hotline #30552 : role is lost when editing user via admin
 panel

---
 VERSIONS_HOTLINE/30552                        |  1 +
 .../admin/controllers/UsersController.php     |  3 +-
 .../admin/views/scripts/users/_form.phtml     | 27 +++---
 .../admin/controllers/UsersControllerTest.php | 88 +++++++++++++++++--
 4 files changed, 102 insertions(+), 17 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/30552

diff --git a/VERSIONS_HOTLINE/30552 b/VERSIONS_HOTLINE/30552
new file mode 100644
index 00000000000..6cd3c16776d
--- /dev/null
+++ b/VERSIONS_HOTLINE/30552
@@ -0,0 +1 @@
+ - ticket #30552 : Le pseudo d'un utilisateur (abonné SIGB) ne résiste pas à la mise à jour 
\ No newline at end of file
diff --git a/application/modules/admin/controllers/UsersController.php b/application/modules/admin/controllers/UsersController.php
index a47b5f01345..9df3a92f021 100644
--- a/application/modules/admin/controllers/UsersController.php
+++ b/application/modules/admin/controllers/UsersController.php
@@ -172,8 +172,7 @@ class Admin_UsersController extends ZendAfi_Controller_Action {
   //------------------------------------------------------------------------------------------------------
   function editAction() {
     $id_user = $this->_request->getParam('id',0);
-    $user = Class_Users::getLoader()->find($id_user);
-
+    $user = Class_Users::find($id_user);
     $this->view->titre = "Modifier l'utilisateur: ".$user->getLogin();
 
     if ($this->_request->isPost())
diff --git a/application/modules/admin/views/scripts/users/_form.phtml b/application/modules/admin/views/scripts/users/_form.phtml
index 90416b49b38..06a9345cd27 100644
--- a/application/modules/admin/views/scripts/users/_form.phtml
+++ b/application/modules/admin/views/scripts/users/_form.phtml
@@ -33,7 +33,7 @@
         <tr>
           <td class="droite">Civilité&nbsp;</td>
           <td class="gauche">
-            <?php 
+            <?php
             echo $this->formSelect('civilite',
                                    $this->user->getCivilite(),
                                    null,
@@ -52,10 +52,18 @@
           <td class="droite"><?php echo $this->traduire('Rôle'); ?>&nbsp;</td>
           <td class="gauche">
             <?php $comboListeRole = new ZendAfi_Acl_AdminControllerRoles();
-            if ($this->user->getId() == '1') 
-            echo '<select disabled="disabled"><option selected="selected" value="7">super_admin</option></select><input type="hidden" value="7" name="role" id="role"/>';
-            else 
-            print $this->user->getRoleLevel() != 2 ? $comboListeRole->rendCombo($this->user->getRole(),$this->authUser->ROLE_LEVEL) : ZendAfi_Acl_AdminControllerRoles::getLibelleRole(2); ; 
+                  if ($this->user->getId() == '1')
+                    echo '<select disabled="disabled"><option selected="selected" value="7">super_admin</option></select><input type="hidden" value="7" name="role" id="role"/>';
+                  else
+                    print $this->user->getRoleLevel() != 2 ? $comboListeRole->rendCombo($this->user->getRole(),$this->authUser->ROLE_LEVEL) : ZendAfi_Acl_AdminControllerRoles::getLibelleRole(2);
+                  if ($this->user->getRoleLevel() == 2)
+                    echo $this->tag('input','',['type' =>'hidden',
+                                                'value' => 2,
+                                                'name' => 'role',
+                                                'id' => 'role']);
+
+
+
             ?>
 
           </td>
@@ -64,7 +72,7 @@
         <tr>
           <td class="droite"><?php echo $this->traduire('Groupes'); ?>&nbsp;</td>
           <td class="gauche">
-            <?php 
+            <?php
             echo $this->getHelper('TreeSelect')
                       ->categoriesNotSelectable()
                       ->treeSelect(implode('-', $this->user->getUserGroupsIds()),
@@ -73,7 +81,7 @@
                                    $this->url(['module' => 'admin',
                                                'controller' => 'usergroup',
                                                'action' => 'list.json']),
-                                   "form", 
+                                   "form",
                                    "usergroup_categories_ids");
             ?>
           </td>
@@ -89,12 +97,12 @@
         <input type="hidden" value="'.$this->id_bib.'" name="bib" id="bib"/>';
             } else {
               $bib = new Class_Bib();
-              echo $bib->getComboBib($this->user->getIdSite());  
+              echo $bib->getComboBib($this->user->getIdSite());
             }
             ?>
             <script type="text/javascript">
              formSelectToggleVisibilityForElement("select[name='role']",
-                                                  "#option_bib", 
+                                                  "#option_bib",
                                                   ["2", "3", "4"]);
              formSelectToggleVisibilityForElement("select[name='role']",
                                                   "#abonne_sigb", "2");
@@ -160,4 +168,3 @@
     </table>
   </form>
 </div>
-
diff --git a/tests/application/modules/admin/controllers/UsersControllerTest.php b/tests/application/modules/admin/controllers/UsersControllerTest.php
index 0c892f060de..b02bd9dc173 100644
--- a/tests/application/modules/admin/controllers/UsersControllerTest.php
+++ b/tests/application/modules/admin/controllers/UsersControllerTest.php
@@ -115,7 +115,6 @@ class UsersControllerEditMarcusTest extends UsersControllerWithMarcusTestCase {
     $this->dispatch('/admin/users/edit/id/10', true);
   }
 
-
   /** @test **/
   public function roleLevelShouldBeSIGBSubscriber() {
     $this->assertXpathContentContains('//tr/td','abonné identifié SIGB');
@@ -164,6 +163,13 @@ class UsersControllerEditMarcusTest extends UsersControllerWithMarcusTestCase {
   }
 
 
+
+  /** @test **/
+  public function testHiddenInputForRole() {
+    $this->assertXPath("//input[@name='role'][@value=2][@type='hidden']",$this->_response->getBody());
+  }
+
+
   /** @test **/
   public function testSelectedBibIsIdOne() {
     $this->assertXPath("//input[@name='bib'][@value='1']", $this->_response->getBody());
@@ -239,7 +245,21 @@ class UsersControllerEditMarcusTest extends UsersControllerWithMarcusTestCase {
 }
 
 
-class rsControllerEditMarcusAsAdminPortailTest extends UsersControllerWithMarcusTestCase {
+class UsersControllerEditMarcusAsAbonPortailTest extends UsersControllerWithMarcusTestCase {
+  public function setUp() {
+    parent::setUp();
+
+  }
+
+
+  /** @test */
+  function comboBibShouldBeVisible() {
+    $this->dispatch('/admin/users/edit/id/10');
+    $this->assertXPath('//select[@name="bib"]');
+  }
+
+}
+class UserrsControllerEditMarcusAsAdminPortailTest extends UsersControllerWithMarcusTestCase {
   public function setUp() {
     parent::setUp();
     $this->marcus->setRoleLevel(ZendAfi_Acl_AdminControllerRoles::ADMIN_PORTAIL);
@@ -262,8 +282,6 @@ class rsControllerEditMarcusAsAdminPortailTest extends UsersControllerWithMarcus
 }
 
 
-
-
 class UsersControllerDeleteMarcusTest extends UsersControllerWithMarcusTestCase {
   public function setUp() {
     parent::setUp();
@@ -296,7 +314,7 @@ class UsersControllerPostMarcusDataTest extends UsersControllerWithMarcusTestCas
                                'password' => 'tutu',
                                'nom' => 'Davis',
                                'prenom' => 'Miles',
-                               'pseudo' => 'Dave',
+                               'pseudo' => 'Dave miles murks??',
                                'mail' => 'mdavis@free.fr',
                                'role' => '4',
                                'bib' => '1',
@@ -452,6 +470,60 @@ class UsersControllerPostMarcusInvalidDataTest extends UsersControllerWithMarcus
   }
 }
 
+class UsersControllerPostValidDataTest extends UsersControllerWithMarcusTestCase {
+
+  public function setUp() {
+    Class_Users::beVolatile();
+    parent::setUp();
+    Class_Users::clearCache();
+    $this->opsys_service = $this->getMock('MockOpsysService', array('saveEmprunteur'));
+    $this->emprunteur = new Class_WebService_SIGB_Emprunteur('2341', 'Marcus');
+    $this->emprunteur->setService($this->opsys_service);
+
+    $this->marcus->setFicheSIGB(array('type_comm' => Class_IntBib::COM_OPSYS,
+                                      'fiche' => $this->emprunteur,
+                                      'nom_aff' => 'Marcus'));
+
+    $this->user_loader
+      ->whenCalled('save')
+      ->with($this->marcus)
+      ->answers(true);
+
+    $this->postDispatch('/admin/users/edit/10',['username' => 'mmiller',
+                                                'password' => 'tutu',
+                                                'nom' => 'Miller',
+                                                'prenom' => 'Marcus',
+                                                'pseudo' => 'Macus miller ? ',
+                                                'mail' => 'marcus@gmail.com',
+                                                'role' => 'abonne_sigb',
+                                                'role_level' =>ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
+                                                'bib' => '1',
+                                                'id_abon' => '2341',
+                                                'id_site' => '1',
+                                                'ordre' => '2',
+                                                'telephone' => '04 12 34 56 78',
+                                                'adresse' => '',
+                                                'code_postal' => '',
+                                                'ville' => '',
+                                                'naissance' => '',
+                                                'mobile' => '',
+                                                'civilite' => 1]);
+
+  }
+
+  /** @test */
+  public function idAbonShouldNotBeModified() {
+    $this->assertEquals('00123',Class_Users::find(10)->getIdabon());
+  }
+
+  /** @test */
+  public function roleShouldNotBeModified() {
+    $this->assertEquals(ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,Class_Users::find(10)->getRoleLevel());
+  }
+
+}
+
+
 
 class UsersControllerPostValidDataWithCommOpsysTest extends UsersControllerWithMarcusTestCase {
   public function setUp() {
@@ -471,6 +543,12 @@ class UsersControllerPostValidDataWithCommOpsysTest extends UsersControllerWithM
       ->answers(true);
   }
 
+
+  /** @test */
+  public function idAbonShouldNotBeModified() {
+    $this->assertEquals(2341,Class_Users::find(10)->getIdabon());
+  }
+
   protected function _postData() {
     $this->_postEditData(array('username' => 'mdavis',
                                'password' => 'tutu',
-- 
GitLab