From 9e010cd2dc3a021ad901078b885048b14caf7e1d Mon Sep 17 00:00:00 2001
From: Henri-Damien LAURENT <hdlaurent@afi-sa.fr>
Date: Fri, 6 Dec 2019 07:08:03 +0100
Subject: [PATCH] hotline#101519 : do not delete empty Basket with linked
 domain

---
 VERSIONS_HOTLINE/101519                       |  1 +
 .../Cosmogramme/Integration/PhasePanier.php   |  9 +++-
 library/Class/PanierNotice.php                | 10 +++-
 library/Class/PanierNoticeCatalogue.php       |  6 +--
 .../Integration/PhasePanierTest.php           | 48 +++++++++++++++----
 5 files changed, 56 insertions(+), 18 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/101519

diff --git a/VERSIONS_HOTLINE/101519 b/VERSIONS_HOTLINE/101519
new file mode 100644
index 00000000000..d519b3fc1ef
--- /dev/null
+++ b/VERSIONS_HOTLINE/101519
@@ -0,0 +1 @@
+ - ticket #101519 : Intégration : Bokeh ne supprime pas automatiquement les paniers devenus vides lorsqu'ils sont reliés à un domaine
\ No newline at end of file
diff --git a/library/Class/Cosmogramme/Integration/PhasePanier.php b/library/Class/Cosmogramme/Integration/PhasePanier.php
index b74d9c841ca..79ced34fbc0 100644
--- a/library/Class/Cosmogramme/Integration/PhasePanier.php
+++ b/library/Class/Cosmogramme/Integration/PhasePanier.php
@@ -20,7 +20,9 @@
  */
 
 
-class Class_Cosmogramme_Integration_PhasePanier extends Class_Cosmogramme_Integration_PhaseOnDataSource {
+class Class_Cosmogramme_Integration_PhasePanier
+  extends Class_Cosmogramme_Integration_PhaseOnDataSource {
+
   const MY_ID = 15;
 
   protected $_label = 'Intégration des fichiers paniers';
@@ -175,6 +177,9 @@ class Class_Cosmogramme_Integration_PhasePanier extends Class_Cosmogramme_Integr
 
 
   protected function _afterFileProcessed($integration) {
-    $cart_todelete = Class_PanierNotice::deleteBy(['notices'=>"", 'id_int_bib'=>$integration->getIdBib()]);
+    Class_PanierNotice::deleteBy(['notices' => '',
+                                  'id_int_bib' => $integration->getIdBib(),
+                                  'id not' => Class_PanierNotice::findAllIdsWithCatalogue()
+                                  ]);
   }
 }
diff --git a/library/Class/PanierNotice.php b/library/Class/PanierNotice.php
index f2c143247ce..38cf26b1dc5 100644
--- a/library/Class/PanierNotice.php
+++ b/library/Class/PanierNotice.php
@@ -58,8 +58,13 @@ class PanierNoticeLoader extends Storm_Model_Loader {
 
 
   public function findAllIdsWithCatalogue() {
-    $panier_notice_catalogue = Class_PanierNoticeCatalogue::getLoader()->findAll();
-    $ids_with_catalogue = array_map(function($association) {return $association->getIdPanier();}, $panier_notice_catalogue);
+    $panier_notice_catalogue = Class_PanierNoticeCatalogue::findAll();
+    $ids_with_catalogue = array_map(function($association)
+                                    {
+                                      return $association->getIdPanier();
+                                    },
+                                    $panier_notice_catalogue);
+
     return array_filter(array_unique($ids_with_catalogue));
   }
 
@@ -114,6 +119,7 @@ class PanierNoticeLoader extends Storm_Model_Loader {
 
 
 
+
 class Class_PanierNotice extends Storm_Model_Abstract {
   use Trait_Translator, Trait_Indexable, Trait_TimeSource;
 
diff --git a/library/Class/PanierNoticeCatalogue.php b/library/Class/PanierNoticeCatalogue.php
index 798ee61800e..dca6cfafd4e 100644
--- a/library/Class/PanierNoticeCatalogue.php
+++ b/library/Class/PanierNoticeCatalogue.php
@@ -16,11 +16,11 @@
  *
  * 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 
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
 class Class_PanierNoticeCatalogue extends Storm_Model_Abstract {
-  protected 
+  protected
     $_table_name = 'notices_paniers_catalogues',
     $_belongs_to = ['catalogue' => ['model' => 'Class_Catalogue',
                                     'referenced_in' => 'id_catalogue'],
@@ -28,5 +28,3 @@ class Class_PanierNoticeCatalogue extends Storm_Model_Abstract {
                     'panier_notice' => ['model' => 'Class_PanierNotice',
                                         'referenced_in' => 'id_panier']];
 }
-
-?>
\ No newline at end of file
diff --git a/tests/library/Class/Cosmogramme/Integration/PhasePanierTest.php b/tests/library/Class/Cosmogramme/Integration/PhasePanierTest.php
index 2bca243f8ca..049de9b90d7 100644
--- a/tests/library/Class/Cosmogramme/Integration/PhasePanierTest.php
+++ b/tests/library/Class/Cosmogramme/Integration/PhasePanierTest.php
@@ -126,6 +126,21 @@ abstract class PhasePanierKohaTestCase extends PhasePanierTestCase {
                     'libelle' => 'will be killed',
                     'notices' => 'VOL714']);
 
+    $this->fixture('Class_PanierNotice',
+                   ['id' => 5,
+                    'id_user' => 3,
+                    'id_sigb' => 1,
+                    'id_int_bib' => 2,
+                    'integration_hash' => '577f79a2c26968b6f5e1360df0dc17af',
+                    'libelle' => 'will not be killed due to domain link',
+                    'notices' => 'VOL714']);
+
+    $this->fixture('Class_Catalogue',
+                   ['id' => 42,
+                    'libelle' => 'Catalogue']);
+
+    Class_PanierNotice::find(5)->setDomaineIds(42);
+
     $this->fixture('Class_Notice',
                    ['id' => 23,
                     'type_doc' => 1,
@@ -162,7 +177,6 @@ abstract class PhasePanierKohaTestCase extends PhasePanierTestCase {
                                                       'id_int_bib' => 2,
                                                       'id_origine' => 58265])]]);
   }
-
 }
 
 
@@ -181,13 +195,19 @@ class PhasePanierKohaSameIdOrigineTest extends PhasePanierKohaTestCase {
     Class_Exemplaire::deleteBy([]);
     Class_IntProfilDonnees::deleteBy([]);
     Class_Cosmogramme_Integration::deleteBy([]);
-    parent:: tearDown();
+    parent::tearDown();
+  }
+
+
+  /** @test */
+  public function emptyBasketWithoutLinkedDomainShouldBeDeleted() {
+    $this->assertNull(Class_PanierNotice::find(4));
   }
 
 
   /** @test */
-  public function fourthBasketRecordsShouldBeDeleted() {
-    $this->assertEmpty(Class_PanierNotice::find(4));
+  public function emptyBasketWithLinkedDomainShouldNotBeDeleted() {
+    $this->assertNotNull(Class_PanierNotice::find(5));
   }
 
 
@@ -200,7 +220,8 @@ class PhasePanierKohaSameIdOrigineTest extends PhasePanierKohaTestCase {
 
   /** @test */
   public function firstBasketRecordsShouldContainsCOKEENSTOCKCASTAFIORELUNE() {
-    $this->assertEquals('COKEENSTOCK;CASTAFIORE;LUNE', Class_PanierNotice::find(1)->getNotices());
+    $this->assertEquals('COKEENSTOCK;CASTAFIORE;LUNE',
+                        Class_PanierNotice::find(1)->getNotices());
   }
 }
 
@@ -228,7 +249,7 @@ class PhasePanierFullImportTest extends PhasePanierKohaTestCase {
 
   /** @test */
   public function numberOfBasketsShouldBeEight() {
-    $this->assertCount(8, Class_PanierNotice::findAll());
+    $this->assertCount(9, Class_PanierNotice::findAll());
   }
 
 
@@ -293,7 +314,7 @@ class PhasePanierFullImportTest extends PhasePanierKohaTestCase {
   /** @test */
   public function basketFiveRecordsShouldContainsTIBET() {
     $this->assertEquals(['TIBET'],
-                        Class_PanierNotice::find(5)->getClesNotices());
+                        Class_PanierNotice::find(7)->getClesNotices());
   }
 
 
@@ -318,7 +339,14 @@ class PhasePanierFullImportTest extends PhasePanierKohaTestCase {
   /* cf. https://forge.afi-sa.net/issues/97326 */
   /** @test */
   public function emptyCart7RelatedToIntBib2ShouldBeDeleted(){
-    $this->assertEquals(null,Class_PanierNotice::find(7));
+    $this->assertEquals(null,Class_PanierNotice::find(8));
+  }
+
+
+  /* cf. https://forge.afi-sa.net/issues/101519 */
+  /** @test */
+  public function emptyCartWithLinkedDomainShouldBePresent(){
+    $this->assertNotNull(Class_PanierNotice::find(5));
   }
 }
 
@@ -415,8 +443,8 @@ class PhasePanierFullImportWithWrongProfilTest extends PhasePanierKohaTestCase {
 
 
   /** @test */
-  public function numberOfBasketsShouldBeFour() {
-    $this->assertCount(4, Class_PanierNotice::findAll());
+  public function numberOfBasketsShouldBeFive() {
+    $this->assertCount(5, Class_PanierNotice::findAll());
   }
 }
 
-- 
GitLab