From 0b2a7a9c6a12d71563ced4adac0eaf98c0c2b1dc Mon Sep 17 00:00:00 2001
From: Patrick Barroca <pbarroca@afi-sa.fr>
Date: Fri, 26 Jun 2020 16:56:42 +0200
Subject: [PATCH] hotline #109494 : fix error in pnb dilicom export when album
 has been deleted

---
 VERSIONS_HOTLINE/109494                       |  1 +
 library/Class/Loan/Pnb.php                    |  5 +++-
 library/Trait/AlbumDelegator.php              | 27 ++++++++++++-------
 tests/scenarios/PnbDilicom/PnbDilicomTest.php | 25 ++++++++++++++++-
 4 files changed, 47 insertions(+), 11 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/109494

diff --git a/VERSIONS_HOTLINE/109494 b/VERSIONS_HOTLINE/109494
new file mode 100644
index 00000000000..f82b7af96d3
--- /dev/null
+++ b/VERSIONS_HOTLINE/109494
@@ -0,0 +1 @@
+ - ticket #109494 : PNB Dilicom : Correction d'une erreur lors de l'export des prèts quand l'album dilicom a été supprimé
\ No newline at end of file
diff --git a/library/Class/Loan/Pnb.php b/library/Class/Loan/Pnb.php
index ed31add32bd..cc0ad7ba593 100644
--- a/library/Class/Loan/Pnb.php
+++ b/library/Class/Loan/Pnb.php
@@ -152,7 +152,10 @@ class Class_Loan_Pnb extends Storm_Model_Abstract {
 
 
   public function getItem() {
-    foreach(Class_Album_Item::findAllBy(['album_id' => $this->getAlbum()->getId()])
+    if (!$album = $this->getAlbum())
+      return;
+
+    foreach(Class_Album_Item::findAllBy(['album_id' => $album->getId()])
             as $item)
       if ($item->getOrderLineId() == $this->getOrderLineId())
         return $item;
diff --git a/library/Trait/AlbumDelegator.php b/library/Trait/AlbumDelegator.php
index 76f4a3d8f28..bccec7d059a 100644
--- a/library/Trait/AlbumDelegator.php
+++ b/library/Trait/AlbumDelegator.php
@@ -25,7 +25,7 @@
  */
 trait Trait_AlbumDelegator {
   public function getNoticeOPACId() {
-    return $this->getAlbum()->getNoticeId();
+    return $this->_withAlbumGet('notice_id');
   }
 
 
@@ -40,42 +40,44 @@ trait Trait_AlbumDelegator {
 
 
   public function getTitle() {
-    return $this->getAlbum()->getTitre();
+    return $this->_withAlbumGet('titre', '');
   }
 
 
   public function getMainAuthor() {
-    return $this->getAlbum()->getMainAuthorName();
+    return $this->_withAlbumGet('main_author_name', '');
   }
 
 
   public function getFirstEditor() {
-    return $this->getAlbum()->getFirstEditor();
+    return $this->_withAlbumGet('first_editor', '');
   }
 
 
   public function getFirstCollection() {
-    return $this->getAlbum()->getFirstCollection();
+    return $this->_withAlbumGet('first_collection', '');
   }
 
 
   public function getYear() {
-    return $this->getAlbum()->getAnnee();
+    return $this->_withAlbumGet('annee', '');
   }
 
 
   public function getFirstKind() {
-    return $this->getFirstLabelFromAlbumCodif('Class_CodifGenre', $this->getAlbum()->getGenre());
+    return $this->getFirstLabelFromAlbumCodif('Class_CodifGenre',
+                                              $this->_withAlbumGet('genre', ''));
   }
 
 
   public function getFirstSection() {
-    return $this->getFirstLabelFromAlbumCodif('Class_CodifSection', $this->getAlbum()->getSections());
+    return $this->getFirstLabelFromAlbumCodif('Class_CodifSection',
+                                              $this->_withAlbumGet('sections'));
   }
 
 
   public function getCategory() {
-    return $this->getAlbum()->getCategoryLabel();
+    return $this->_withAlbumGet('category_label', '');
   }
 
 
@@ -85,4 +87,11 @@ trait Trait_AlbumDelegator {
       ? ''
       : $labels->first();
   }
+
+
+  protected function _withAlbumGet($attribute, $default=null) {
+    return ($album = $this->getAlbum())
+      ? $album->callGetterByAttributeName($attribute)
+      : $default;
+  }
 }
diff --git a/tests/scenarios/PnbDilicom/PnbDilicomTest.php b/tests/scenarios/PnbDilicom/PnbDilicomTest.php
index 11d9150e6ff..a93bb251042 100644
--- a/tests/scenarios/PnbDilicom/PnbDilicomTest.php
+++ b/tests/scenarios/PnbDilicom/PnbDilicomTest.php
@@ -3862,7 +3862,7 @@ class PnbDilicomAdminAlbumControllerExportLoansCsvTest extends PnbDilicomAdminAl
                     'loan_link' => 'https://pnb-dilicom.centprod.com/v2//XXXXXXXX.do',
                     'order_line_id' => '82377a045ce56ef0a072a8b']);
 
-    $this->dispatch('admin/album/dilicom-export-loans-csv', true);
+    $this->dispatch('admin/album/dilicom-export-loans-csv');
   }
 
 
@@ -3898,6 +3898,29 @@ class PnbDilicomAdminAlbumControllerExportLoansCsvTest extends PnbDilicomAdminAl
 
 
 
+class PnbDilicomAdminAlbumControllerExportLoansCsvWithMissingAlbumTest
+  extends PnbDilicomAdminAlbumControllerTestCase {
+
+  public function setUp() {
+    parent::setUp();
+
+    Class_Album::find(23)->delete();
+    $this->dispatch('admin/album/dilicom-export-loans-csv');
+  }
+
+
+  /** @test */
+  public function filenameShouldBeDilicomLoansCsv() {
+    $this->assertContains(['name' => 'Content-Type',
+                           'value' => 'text/csv; name="dilicom_loans_csv.csv"',
+                           'replace' => true],
+                          $this->_response->getHeaders());
+  }
+}
+
+
+
+
 class PnbDilicomAdminIndexControllerTest extends AbstractControllerTestCase {
 
   protected $_storm_default_to_volatile = true;
-- 
GitLab