From 17756aaef58fb3d20426034093fd5869caf2b9d6 Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@afi-sa.fr>
Date: Wed, 8 Mar 2017 18:23:59 +0100
Subject: [PATCH] dev #15795 : authentication handled by id_int_bib

---
 cosmogramme/php/classes/classe_abonne.php     |   7 +-
 library/Class/IntBib.php                      |   3 +-
 library/Class/User/DoubleFinder.php           |  99 ++++++++++
 library/Class/Users.php                       |  36 +---
 library/Class/WebService/SIGB/Emprunteur.php  |  23 ++-
 .../Class/WebService/SIGB/LibraryWrapper.php  |  43 +++++
 library/ZendAfi/Auth/Adapter/CommSigb.php     |  13 +-
 .../opac/controllers/AuthControllerTest.php   |  45 ++---
 .../ZendAfi/Auth/Adapter/AuthCommSigbTest.php | 177 ++++++++++--------
 9 files changed, 295 insertions(+), 151 deletions(-)
 create mode 100644 library/Class/User/DoubleFinder.php
 create mode 100644 library/Class/WebService/SIGB/LibraryWrapper.php

diff --git a/cosmogramme/php/classes/classe_abonne.php b/cosmogramme/php/classes/classe_abonne.php
index 4e5e4f6dbbf..45fe27f1ca2 100644
--- a/cosmogramme/php/classes/classe_abonne.php
+++ b/cosmogramme/php/classes/classe_abonne.php
@@ -149,9 +149,10 @@ class abonne {
 
   protected function saveOrUpdateInDB($data){
     $new_user = Class_Users::newInstance($data);
-
-    if(!$user = Class_Users::findMatchingPatron($new_user))
-      $user = $new_user;
+    $finder = new Class_User_DoubleFinder($new_user);
+    $user = $finder->find()
+      ? $finder->getDouble()
+      : $new_user;
 
     $user
       ->updateAttributes($data)
diff --git a/library/Class/IntBib.php b/library/Class/IntBib.php
index 2035dcd33be..d2ad441ab52 100644
--- a/library/Class/IntBib.php
+++ b/library/Class/IntBib.php
@@ -210,7 +210,8 @@ class Class_IntBib extends Storm_Model_Abstract {
     if (!$service_name = static::detectService($this->getCommSigb()))
       return null;
 
-    return call_user_func([$service_name, 'getService'], $this->getModeComm());
+    $service = call_user_func([$service_name, 'getService'], $this->getModeComm());
+    return new Class_WebService_SIGB_LibraryWrapper($this, $service);
   }
 
 
diff --git a/library/Class/User/DoubleFinder.php b/library/Class/User/DoubleFinder.php
new file mode 100644
index 00000000000..0cb72a202b0
--- /dev/null
+++ b/library/Class/User/DoubleFinder.php
@@ -0,0 +1,99 @@
+<?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 Class_User_DoubleFinder extends Class_Entity {
+  public function __construct($patron) {
+    $this->setPatron($patron);
+  }
+
+
+  public function __call($name, $args) {
+    // forward call to patron property
+    if ('getPatron' == substr($name, 0, 9)
+        && ($property = substr($name, 9))) {
+      return call_user_func([$this->getPatron(), 'get'.$property]);
+    }
+
+    return parent::__call($name, $args);
+  }
+
+
+  public function find() {
+    if (!$this->getPatron())
+      return false;
+
+    if (!$id_int_bib = $this->getPatronIdIntBib())
+      return false;
+
+    foreach(['_matchByLoginAndOrder',
+             '_matchByLoginAndIdSigb',
+             '_matchByLoginAndLibrary',
+             '_matchByIdSigb']
+            as $method) {
+      if (call_user_func([$this, $method]))
+        return true;
+    }
+
+    return false;
+  }
+
+
+  protected function _matchByLoginAndOrder() {
+    if (!$ordreabon = $this->getPatronOrdreabon())
+      return false;
+
+    return $this->_matchByParams(['login' => $this->getPatronLogin(),
+                                  'ordreabon' => $ordreabon]);
+  }
+
+
+  protected function _matchByLoginAndIdSigb() {
+    return $this->_matchByParams(['login' => $this->getPatronLogin(),
+                                  'id_sigb' => $this->getPatronIdSigb()]);
+  }
+
+
+  protected function _matchByLoginAndLibrary() {
+    return $this->_matchByParams(['login' => $this->getPatronLogin(),
+                                  'id_site' => $this->getPatronIdSite()]);
+  }
+
+
+  protected function _matchByIdSigb() {
+    if (!$id_sigb = $this->getPatronIdSigb())
+      return false;
+
+    return $this->_matchByParams(['id_sigb' => $id_sigb]);
+  }
+
+
+  protected function _matchByParams($params) {
+    $params = array_merge(['id_int_bib' => $this->getPatronIdIntBib()],
+                          $params);
+
+    if (!$user = Class_Users::findFirstBy($params))
+      return false;
+
+    $this->setDouble($user);
+    return true;
+  }
+}
diff --git a/library/Class/Users.php b/library/Class/Users.php
index 43d05eb8dc0..613970a3bb2 100644
--- a/library/Class/Users.php
+++ b/library/Class/Users.php
@@ -43,37 +43,6 @@ class UsersLoader extends Storm_Model_Loader {
   }
 
 
-  public function findMatchingPatron($patron) {
-    if (!$patron)
-      return null;
-
-    if (!$id_int_bib = $patron->getIdIntBib())
-      return null;
-
-    if (($ordreabon = $patron->getOrdreabon())
-         && ($user = Class_Users::findFirstBy(['login' => $patron->getLogin(),
-                                               'ordreabon' => $ordreabon,
-                                               'id_int_bib' => $id_int_bib])))
-      return $user;
-
-    if ($user = Class_Users::findFirstBy(['login' => $patron->getLogin(),
-                                          'id_sigb' => $patron->getIdSigb(),
-                                          'id_int_bib' => $id_int_bib]))
-      return $user;
-
-    if ($user = Class_Users::findFirstBy(['login' => $patron->getLogin(),
-                                          'id_int_bib' => $id_int_bib]))
-      return $user;
-
-    if (($id_sigb = $patron->getIdSigb())
-        && ($user = Class_Users::findFirstBy(['id_int_bib' => $id_int_bib,
-                                              'id_sigb' => $patron->getIdSigb()])))
-      return $user;
-
-    return null;
-  }
-
-
   public function findAllLike($search, $by_right = null, $limit = 500) {
     $sql_template = 'select bib_admin_users.* from bib_admin_users ';
 
@@ -251,7 +220,8 @@ class UsersLoader extends Storm_Model_Loader {
       ->setOrdreabon($source->getOrdreabon())
       ->setTelephone($source->getTelephone())
       ->setNaissance($source->getNaissance())
-      ->setIdSite($source->getIdSite());
+      ->setIdSite($source->getIdSite())
+      ->setIdIntBib($source->getIdIntBib());
   }
 
 
@@ -427,7 +397,7 @@ class Class_Users extends Storm_Model_Abstract {
                               'referenced_in' => 'id_site'],
 
                     'int_bib' => ['model' => 'Class_IntBib',
-                                  'referenced_in' => 'id_site'],
+                                  'referenced_in' => 'id_int_bib'],
 
                     'zone' => ['through' => 'bib'],
 
diff --git a/library/Class/WebService/SIGB/Emprunteur.php b/library/Class/WebService/SIGB/Emprunteur.php
index 29497a33ada..0ffc80f4588 100644
--- a/library/Class/WebService/SIGB/Emprunteur.php
+++ b/library/Class/WebService/SIGB/Emprunteur.php
@@ -45,7 +45,8 @@ class Class_WebService_SIGB_Emprunteur {
     $_date_naissance,
     $_is_contact_email= 0,
     $_is_contact_sms= 0,
-    $_library_code;
+    $_library_code,
+    $_id_int_bib;
 
 
   public function __sleep() {
@@ -70,7 +71,8 @@ class Class_WebService_SIGB_Emprunteur {
             '_code_postal',
             '_is_contact_email',
             '_is_contact_sms',
-            '_library_code'];
+            '_library_code',
+            '_id_int_bib'];
   }
 
 
@@ -621,6 +623,17 @@ class Class_WebService_SIGB_Emprunteur {
   }
 
 
+  public function setIdIntBib($id) {
+    $this->_id_int_bib = $id;
+    return $this;
+  }
+
+
+  public function getIdIntBib() {
+    return $this->_id_int_bib;
+  }
+
+
   public function updateFromUserAndSave($user) {
     $this
       ->setNom($user->getNom())
@@ -711,8 +724,12 @@ class Class_WebService_SIGB_Emprunteur {
     if ($this->_id)
       $user->setIdSigb($this->_id);
 
-    if (($annexe = $this->getLibrary()) && ($bib = $annexe->getBib()) && ($int_bib = $bib->getIntBib()) && $int_bib->getSIGBComm())
+    if (($annexe = $this->getLibrary())
+        && ($bib = $annexe->getBib()))
       $user->setBib($bib);
+
+    if ($id_int_bib = $this->getIdIntBib())
+      $user->setIntBib(Class_IntBib::find($id_int_bib));
   }
 
 
diff --git a/library/Class/WebService/SIGB/LibraryWrapper.php b/library/Class/WebService/SIGB/LibraryWrapper.php
new file mode 100644
index 00000000000..97d6dc1f03e
--- /dev/null
+++ b/library/Class/WebService/SIGB/LibraryWrapper.php
@@ -0,0 +1,43 @@
+<?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 Class_WebService_SIGB_LibraryWrapper {
+  protected $_library, $_service;
+
+  public function __construct($library, $service) {
+    $this->_library = $library;
+    $this->_service = $service;
+  }
+
+
+  public function __call($name, $args) {
+    return call_user_func_array([$this->_service, $name], $args);
+  }
+
+
+  public function getEmprunteur($user) {
+    if ($emprunteur = $this->_service->getEmprunteur($user))
+      $emprunteur->setIdIntBib($this->_library->getId());
+
+    return $emprunteur;
+  }
+}
diff --git a/library/ZendAfi/Auth/Adapter/CommSigb.php b/library/ZendAfi/Auth/Adapter/CommSigb.php
index 63dbd615a95..207497357ff 100644
--- a/library/ZendAfi/Auth/Adapter/CommSigb.php
+++ b/library/ZendAfi/Auth/Adapter/CommSigb.php
@@ -70,12 +70,10 @@ class ZendAfi_Auth_Adapter_CommSigb implements Zend_Auth_Adapter_Interface {
 
 
   protected function _getUserToSave($user_from_sigb) {
-    $new_user = Class_Users::newInstance()->setLogin($this->_identity);
-
-    if ($user = Class_Users::findMatchingPatron($user_from_sigb))
-      return $user;
-
-    return $new_user;
+    $finder = new Class_User_DoubleFinder($user_from_sigb);
+    return ($finder->find())
+      ? $finder->getDouble()
+      : Class_Users::newInstance()->setLogin($this->_identity);
   }
 
 
@@ -111,7 +109,8 @@ class ZendAfi_Auth_Adapter_CommSigb implements Zend_Auth_Adapter_Interface {
       return Class_IntBib::findAllWithWebServices();
 
     $user_in_db = $users_in_db[0];
-    if (!$user_in_db->isAbonne() || (!$bib = $user_in_db->getIntBib()) || (!$bib->getSIGBComm()))
+    if (!$user_in_db->isAbonne()
+        || (!$bib = $user_in_db->getIntBib()) || (!$bib->getSIGBComm()))
       return [];
 
     return [$bib];
diff --git a/tests/application/modules/opac/controllers/AuthControllerTest.php b/tests/application/modules/opac/controllers/AuthControllerTest.php
index 726dffb701a..656cbfca4b5 100644
--- a/tests/application/modules/opac/controllers/AuthControllerTest.php
+++ b/tests/application/modules/opac/controllers/AuthControllerTest.php
@@ -27,9 +27,9 @@ abstract class PortailWithOneLoginModuleTestCase extends AbstractControllerTestC
   public function setUp() {
     parent::setUp();
 
-    Storm_Test_ObjectWrapper::onLoaderOfModel('Class_IntBib')
-      ->whenCalled('findAllBy')
-      ->answers([]);
+    $this->onLoaderOfModel('Class_IntBib')
+         ->whenCalled('findAllBy')
+         ->answers([]);
 
     $cfg_accueil = ['modules' => [4 => ['division' => '4',
                                         'id_module' => 4,
@@ -2566,9 +2566,9 @@ class AuthControllerPostWithSameIdSigbTest extends AbstractControllerTestCase {
 
     ZendAfi_Auth::getInstance()->clearIdentity();
 
-    $emprunteur = Class_WebService_SIGB_Emprunteur::newInstance(789, 'koha');
-    $emprunteur->setPassword('bar');
-    $emprunteur->beValid();
+    $emprunteur = Class_WebService_SIGB_Emprunteur::newInstance(789, 'koha')
+      ->setPassword('bar')
+      ->beValid();
 
     $service = $this->mock()
                     ->whenCalled('getEmprunteur')
@@ -2605,6 +2605,7 @@ class AuthControllerPostWithSameIdSigbTest extends AbstractControllerTestCase {
                     'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
                     'idabon' => 'different',
                     'id_site' => 13,
+                    'id_int_bib' => 13,
                     'id_sigb' => 789]);
 
     $this->fixture('Class_Users',
@@ -2614,11 +2615,10 @@ class AuthControllerPostWithSameIdSigbTest extends AbstractControllerTestCase {
                     'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
                     'idabon' => 'foo',
                     'id_site' => 56,
+                    'id_int_bib' => 56,
                     'id_sigb' => 789]);
 
-    $this->postDispatch('/opac/auth/login',
-                        ['username' => 'foo',
-                         'password' => 'bar']);
+    $this->postDispatch('/opac/auth/login', ['username' => 'foo', 'password' => 'bar']);
   }
 
 
@@ -2638,7 +2638,8 @@ class AuthControllerPostWithSameIdSigbTest extends AbstractControllerTestCase {
 
 
 
-class AuthControllerPostLoginWithDifferentIdSiteTest extends AbstractControllerTestCase {
+class AuthControllerPostLoginWithDifferentIdIntBibTest
+  extends AbstractControllerTestCase {
   protected $_storm_default_to_volatile = true;
 
   public function setUp() {
@@ -2654,7 +2655,7 @@ class AuthControllerPostLoginWithDifferentIdSiteTest extends AbstractControllerT
                            ['id' => 15,
                             'libelle' => 'pasc',
                             'id_origine' => 'PASC',
-                            'id_bib' => 987]);
+                            'bib' => $pasc_library]);
 
     $emprunteur = Class_WebService_SIGB_Emprunteur::newInstance(789, 'koha');
     $emprunteur->setPassword('bar')
@@ -2682,15 +2683,14 @@ class AuthControllerPostLoginWithDifferentIdSiteTest extends AbstractControllerT
     $this->fixture('Class_IntBib',
                    ['id' => 56,
                     'id_bib' => 56,
-                    'comm_sigb' => 5,
+                    'comm_sigb' => Class_IntBib::COM_KOHA,
                     'comm_params' => serialize($params)]);
 
     $this->fixture('Class_IntBib',
                    ['id' => 987,
                     'id_bib' => 987,
-                    'comm_sigb' => 5,
-                    'comm_params' => serialize($params)
-                   ]);
+                    'comm_sigb' => Class_IntBib::COM_KOHA,
+                    'comm_params' => serialize($params)]);
 
     $this->fixture('Class_Users',
                    ['id' => 5,
@@ -2699,18 +2699,21 @@ class AuthControllerPostLoginWithDifferentIdSiteTest extends AbstractControllerT
                     'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
                     'idabon' => 'foo',
                     'id_site' => 56,
+                    'id_int_bib' => 56,
                     'id_sigb' => null]);
 
-    $this->postDispatch('/opac/auth/login',
-                        ['username' => 'foo',
-                         'password' => 'bar']);
+    $this->postDispatch('/opac/auth/login', ['username' => 'foo',
+                                             'password' => 'bar']);
   }
 
 
   /** @test */
   public function userFooShouldBeLoggedAndCreated() {
-    $this->assertNotNull(Class_Users::getIdentity());
-    $this->assertEquals('foo', Class_Users::getIdentity()->getLogin());
-    $this->assertEquals('6', Class_Users::getIdentity()->getId());
+    $user = Class_Users::getIdentity();
+    $this->assertNotNull($user);
+    $this->assertEquals('foo', $user->getLogin());
+    $this->assertNotEquals(5, $user->getId());
+    $this->assertEquals('Pasc Library', $user->getLibelleBib());
+    $this->assertEquals(56, $user->getIdIntBib());
   }
 }
\ No newline at end of file
diff --git a/tests/library/ZendAfi/Auth/Adapter/AuthCommSigbTest.php b/tests/library/ZendAfi/Auth/Adapter/AuthCommSigbTest.php
index 2ccc6fbabba..fa6a681fb76 100644
--- a/tests/library/ZendAfi/Auth/Adapter/AuthCommSigbTest.php
+++ b/tests/library/ZendAfi/Auth/Adapter/AuthCommSigbTest.php
@@ -19,13 +19,12 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
-abstract class AuthCommSigbTestCase extends Storm_Test_ModelTestCase {
+abstract class AuthCommSigbTestCase extends ModelTestCase {
+  protected $_storm_default_to_volatile = true;
+
   public function setUp() {
     parent::setUp();
 
-    Class_Users::beVolatile();
-    Class_IntBib::beVolatile();
-
     $this->_zork = $this->fixture('Class_Users',
                                   ['id' => 4,
                                    'date_fin' => '2010-10-23',
@@ -33,10 +32,10 @@ abstract class AuthCommSigbTestCase extends Storm_Test_ModelTestCase {
                                    'role_level' => 2,
                                    'idabon' => '98475',
                                    'id_site' => 2,
+                                   'id_int_bib' => 2,
                                    'password' => 'xzy']);
 
-    Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Users');
-    Storm_Test_ObjectWrapper::onLoaderOfModel('Class_IntBib');
+    $this->onLoaderOfModel('Class_Users');
   }
 }
 
@@ -44,20 +43,25 @@ abstract class AuthCommSigbTestCase extends Storm_Test_ModelTestCase {
 
 
 class AuthCommSigbAuthenticationWithoutWebServicesTest extends AuthCommSigbTestCase {
+  protected $_result;
+
   public function setUp() {
     parent::setUp();
 
-    $this->_adapter = (new ZendAfi_Auth_Adapter_CommSigb())
+    $adapter = (new ZendAfi_Auth_Adapter_CommSigb())
       ->setIdentity('zork_sigb')
       ->setCredential('secret');
+
+    $this->_result = $adapter->authenticate();
   }
 
 
   /** @test */
   public function withOutWebServiceAuthenticateResultShouldNotBeValid() {
-    $this->assertFalse($this->_adapter->authenticate()->isValid());
+    $this->assertFalse($this->_result->isValid());
   }
 
+
   /** @test */
   public function noUserShouldHaveBeenSaved() {
     $this->assertFalse(Class_Users::methodHasBeenCalled('save'));
@@ -72,11 +76,13 @@ abstract class AuthCommSigbWithWebServicesAndAbonneZorkTestCase extends AuthComm
 
     $this->fixture('Class_IntBib', ['id' => 1, 'comm_sigb' => Class_IntBib::COM_NANOOK]);
     $this->fixture('Class_IntBib', ['id' => 95, 'comm_sigb' => Class_IntBib::COM_ORPHEE]);
-    $this->fixture('Class_IntBib', ['id' => 74, 'comm_sigb' => Class_IntBib::COM_OPSYS]);
+    $this->fixture('Class_IntBib', ['id' => 74,
+                                    'comm_sigb' => Class_IntBib::COM_OPSYS,
+                                    'nom_court' => 'TestingOpsys']);
 
-    Class_WebService_SIGB_Nanook::setService($this->nanook = Storm_Test_ObjectWrapper::mock());
-    Class_WebService_SIGB_Orphee::setService($this->orphee = Storm_Test_ObjectWrapper::mock());
-    Class_WebService_SIGB_Opsys::setService($this->opsys = Storm_Test_ObjectWrapper::mock());
+    Class_WebService_SIGB_Nanook::setService($this->nanook = $this->mock());
+    Class_WebService_SIGB_Orphee::setService($this->orphee = $this->mock());
+    Class_WebService_SIGB_Opsys::setService($this->opsys = $this->mock());
 
     $this->nanook
       ->whenCalled('getEmprunteur')
@@ -86,6 +92,15 @@ abstract class AuthCommSigbWithWebServicesAndAbonneZorkTestCase extends AuthComm
       ->whenCalled('getEmprunteur')
       ->answers(Class_WebService_SIGB_Emprunteur::nullInstance());
 
+    $annecy_library = $this->fixture('Class_Bib',
+                                     ['id' => 43,
+                                      'libelle' => 'annecy']);
+
+    $annecy_annexe = $this->fixture('Class_CodifAnnexe',
+                                    ['id' => 42,
+                                     'id_origine' => 'ANN',
+                                     'bib' => $annecy_library]);
+
     $this->opsys
       ->whenCalled('getEmprunteur')
       ->answers(Class_WebService_SIGB_Emprunteur::newInstance('001234')
@@ -94,10 +109,12 @@ abstract class AuthCommSigbWithWebServicesAndAbonneZorkTestCase extends AuthComm
                 ->setEMail('zork@gmail.com')
                 ->setEndDate('2015-12-25')
                 ->setPassword('secret')
+                ->setLibraryCode('ANN')
                 ->beValid());
-    $this->_zork->setIdSite(74)->save();
 
-    return $this;
+    $this->_zork->setIdIntBib(74)
+                ->setIdSite(43)
+                ->save();
   }
 
 
@@ -105,6 +122,7 @@ abstract class AuthCommSigbWithWebServicesAndAbonneZorkTestCase extends AuthComm
     Class_WebService_SIGB_Nanook::setService(null);
     Class_WebService_SIGB_Orphee::setService(null);
     Class_WebService_SIGB_Opsys::setService(null);
+
     parent::tearDown();
   }
 }
@@ -112,29 +130,25 @@ abstract class AuthCommSigbWithWebServicesAndAbonneZorkTestCase extends AuthComm
 
 
 
-class AuthCommSigbSuccessfullAuthenticationTest extends AuthCommSigbWithWebServicesAndAbonneZorkTestCase {
+class AuthCommSigbSuccessfullAuthenticationWithExistingUserTest
+  extends AuthCommSigbWithWebServicesAndAbonneZorkTestCase {
+  protected $_user;
+
   public function setUp() {
     parent::setUp();
 
-    Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Users')
-      ->whenCalled('save')
-      ->willDo(
-        function($user) {
-          $user->setId(23);
-          return true;
-        });
-
     $this->_adapter = (new ZendAfi_Auth_Adapter_CommSigb())
       ->setIdentity('zork_sigb')
       ->setCredential('secret');
 
-    $this->_authenticate_result = $this->_adapter->authenticate();
+    $this->_result = $this->_adapter->authenticate();
+    $this->_user = Class_Users::findFirstBy(['login' => 'zork_sigb']);
   }
 
 
   /** @test */
-  public function authenticateZorkShouldReturnValidResult() {
-    $this->assertTrue($this->_authenticate_result->isValid());
+  public function authenticateShouldBeValid() {
+    $this->assertTrue($this->_result->isValid());
   }
 
 
@@ -146,55 +160,39 @@ class AuthCommSigbSuccessfullAuthenticationTest extends AuthCommSigbWithWebServi
 
   /** @test */
   public function userShouldBeAbonneSIGB() {
-    $user = Class_Users::getFirstAttributeForLastCallOn('save');
-    $this->assertTrue($user->isAbonne());
+    $this->assertTrue($this->_user->isAbonne());
   }
 
 
   /** @test */
-  public function resultObjectShouldBeSetUp() {
-    $result = $this->_adapter->getResultObject();
-    $this->assertEquals(23, $result->ID_USER);
-    $this->assertEquals('001234', $result->IDABON);
-    $this->assertEquals(74, $result->ID_SITE);
-    $this->assertEquals('Zork', $result->NOM);
-    $this->assertEquals('Zinn', $result->PRENOM);
-    $this->assertEquals('zork@gmail.com', $result->MAIL);
-    $this->assertEquals('2015-12-25', $result->DATE_FIN);
-  }
-}
-
-
-
-
-class AuthCommSigbSuccessfullAuthenticationWithExistingUserTest extends AuthCommSigbWithWebServicesAndAbonneZorkTestCase {
-  public function setUp() {
-    parent::setUp();
-
-    $this->_adapter = (new ZendAfi_Auth_Adapter_CommSigb())
-      ->setIdentity('zork_sigb')
-      ->setCredential('secret');
-
-    $this->_authenticate_result = $this->_adapter->authenticate();
+  public function userShouldHaveLibraryAnnecy() {
+    $this->assertEquals('annecy', $this->_user->getBib()->getLibelle());
   }
 
 
   /** @test */
-  public function authenticateZorkShouldReturnValidResult() {
-    $this->assertTrue($this->_authenticate_result->isValid());
+  public function userShouldHaveIntegrationLibraryOpsys() {
+    $this->assertEquals('TestingOpsys', $this->_user->getIntBib()->getNomCourt());
   }
 
 
   /** @test */
-  public function zorkShouldHaveBeenSaved() {
-    $this->assertEquals($this->_zork, Class_Users::getFirstAttributeForLastCallOn('save'));
+  public function resultObjectShouldBeSetUp() {
+    $result = $this->_adapter->getResultObject();
+    $this->assertEquals($this->_user->getId(), $result->ID_USER);
+    $this->assertEquals('001234', $result->IDABON);
+    $this->assertEquals(43, $result->ID_SITE);
+    $this->assertEquals('Zork', $result->NOM);
+    $this->assertEquals('Zinn', $result->PRENOM);
+    $this->assertEquals('zork@gmail.com', $result->MAIL);
+    $this->assertEquals('2015-12-25', $result->DATE_FIN);
   }
 
 
   /** @test */
-  public function userShouldBeAbonneSIGB() {
-    $user = Class_Users::getFirstAttributeForLastCallOn('save');
-    $this->assertTrue($user->isAbonne());
+  public function zorkShouldHaveBeenSaved() {
+    $this->assertEquals($this->_zork,
+                        Class_Users::getFirstAttributeForLastCallOn('save'));
   }
 
 
@@ -213,26 +211,23 @@ class AuthCommSigbSuccessfullAuthenticationWithExistingUserTest extends AuthComm
 
 
 
-class AuthCommSigbSuccessfullAuthenticationWithExistingUserButWrongPasswordTest extends AuthCommSigbWithWebServicesAndAbonneZorkTestCase {
+class AuthCommSigbSuccessfullAuthenticationWithExistingUserButWrongPasswordTest
+  extends AuthCommSigbWithWebServicesAndAbonneZorkTestCase {
+
   public function setUp() {
     parent::setUp();
 
-    Class_Users
-      ::whenCalled('findAllBy')
-      ->with(['login' => 'zork_sigb'])
-      ->answers([$this->_zork]);
-
     $this->_adapter = (new ZendAfi_Auth_Adapter_CommSigb())
       ->setIdentity('zork_sigb')
       ->setCredential('oups');
 
-    $this->_authenticate_result = $this->_adapter->authenticate();
+    $this->_result = $this->_adapter->authenticate();
   }
 
 
   /** @test */
   public function authenticateZorkShouldNotReturnValidResult() {
-    $this->assertFalse($this->_authenticate_result->isValid());
+    $this->assertFalse($this->_result->isValid());
   }
 }
 
@@ -268,13 +263,11 @@ class AuthCommSigbErrorsTest extends AuthCommSigbWithWebServicesAndAbonneZorkTes
 
 
 
-
-
-class AuthCommSigbSuccessfullAuthenticationWithExistingFamilleUserTest extends AuthCommSigbWithWebServicesAndAbonneZorkTestCase {
+class AuthCommSigbSuccessfullAuthenticationWithExistingFamilleUserTest
+  extends AuthCommSigbWithWebServicesAndAbonneZorkTestCase {
 
   protected $_zowife, $_zork_boy, $_zork_girl;
 
-
   public function setUp() {
     parent::setUp();
 
@@ -295,7 +288,8 @@ class AuthCommSigbSuccessfullAuthenticationWithExistingFamilleUserTest extends A
                                         'password' => 'zorkgirl',
                                         'login' => 'zork_sigb',
                                         'id_sigb' => '001234',
-                                        'id_site' => 74]);
+                                        'id_site' => 74,
+                                        'id_int_bib' => 74]);
 
     $this->opsys
       ->whenCalled('getEmprunteur')
@@ -311,7 +305,13 @@ class AuthCommSigbSuccessfullAuthenticationWithExistingFamilleUserTest extends A
       ->setIdentity('zork_sigb')
       ->setCredential('zorkgirl');
 
-    $this->_authenticate_result = $this->_adapter->authenticate();
+    $this->_result = $this->_adapter->authenticate();
+  }
+
+
+  /** @test */
+  public function shouldBeAuthenticated() {
+    $this->assertTrue($this->_result->isValid());
   }
 
 
@@ -325,12 +325,13 @@ class AuthCommSigbSuccessfullAuthenticationWithExistingFamilleUserTest extends A
   public function zorkGirlShouldBeAbonneSIGB() {
     $this->assertTrue($this->_zork_girl->isAbonne());
   }
-
 }
 
 
 
-class AuthCommSigbSuccessfullAuthenticationWithNewLoginTest extends AuthCommSigbWithWebServicesAndAbonneZorkTestCase {
+class AuthCommSigbSuccessfullAuthenticationWithNewLoginTest
+  extends AuthCommSigbWithWebServicesAndAbonneZorkTestCase {
+
   public function setUp() {
     parent::setUp();
 
@@ -350,13 +351,13 @@ class AuthCommSigbSuccessfullAuthenticationWithNewLoginTest extends AuthCommSigb
       ->setIdentity('new_login')
       ->setCredential('xzyz');
 
-    $this->_authenticate_result = $this->_adapter->authenticate();
+    $this->_result = $this->_adapter->authenticate();
   }
 
 
   /** @test */
   public function authenticateZorkShouldBeValid() {
-    $this->assertTrue($this->_authenticate_result->isValid());
+    $this->assertTrue($this->_result->isValid());
   }
 
 
@@ -375,7 +376,9 @@ class AuthCommSigbSuccessfullAuthenticationWithNewLoginTest extends AuthCommSigb
 
 
 
-class AuthCommSigbErrorAuthenticationWithNewLoginTest extends AuthCommSigbWithWebServicesAndAbonneZorkTestCase {
+class AuthCommSigbErrorAuthenticationWithNewLoginTest
+  extends AuthCommSigbWithWebServicesAndAbonneZorkTestCase {
+
   public function setUp() {
     parent::setUp();
 
@@ -396,13 +399,13 @@ class AuthCommSigbErrorAuthenticationWithNewLoginTest extends AuthCommSigbWithWe
       ->setIdentity('new_login')
       ->setCredential('xzyz');
 
-    $this->_authenticate_result = $this->_adapter->authenticate();
+    $this->_result = $this->_adapter->authenticate();
   }
 
 
   /** @test */
   public function authenticateZorkShouldBeValid() {
-    $this->assertTrue($this->_authenticate_result->isValid());
+    $this->assertTrue($this->_result->isValid());
   }
 
 
@@ -416,12 +419,20 @@ class AuthCommSigbErrorAuthenticationWithNewLoginTest extends AuthCommSigbWithWe
   public function zorkPasswordShouldNotBeUpdated() {
     $this->assertEquals('xzy', $this->_zork->getPassword());
   }
+
+
+  /** @test */
+  public function newLoginShouldBeCreated() {
+    $this->assertNotNull(Class_Users::findFirstBy(['login' => 'new_login']));
+  }
 }
 
 
 
 
-class AuthCommSigbAuthenticationSetupInvalidUserTest extends AuthCommSigbWithWebServicesAndAbonneZorkTestCase {
+class AuthCommSigbAuthenticationSetupInvalidUserTest
+  extends AuthCommSigbWithWebServicesAndAbonneZorkTestCase {
+
   public function setUp() {
     parent::setUp();
 
@@ -433,13 +444,13 @@ class AuthCommSigbAuthenticationSetupInvalidUserTest extends AuthCommSigbWithWeb
       ->setIdentity('zork_sigb')
       ->setCredential('secret');
 
-    $this->_authenticate_result = $this->_adapter->authenticate();
+    $this->_result = $this->_adapter->authenticate();
   }
 
 
   /** @test */
   public function authenticateZorkShouldNotBeValid() {
-    $this->assertFalse($this->_authenticate_result->isValid());
+    $this->assertFalse($this->_result->isValid());
   }
 
 
-- 
GitLab