From 799af6e440aad8a8ccab587a58d12eaeb926ab29 Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Tue, 10 Jul 2018 16:55:55 +0200
Subject: [PATCH] hotline #76959 fix album thumbnailing

---
 VERSIONS_HOTLINE/76959                        |  4 +-
 .../admin/controllers/RecordsController.php   | 10 ++++
 library/Class/Album.php                       |  7 ++-
 library/Class/Indexation/PseudoNotice.php     |  1 +
 .../controllers/RecordsControllerTest.php     | 52 +++++++++++++++++++
 5 files changed, 71 insertions(+), 3 deletions(-)

diff --git a/VERSIONS_HOTLINE/76959 b/VERSIONS_HOTLINE/76959
index 9e18fb2fd6f..a90c8b5e27a 100644
--- a/VERSIONS_HOTLINE/76959
+++ b/VERSIONS_HOTLINE/76959
@@ -1,2 +1,4 @@
- - ticket #76959 : Explorateur de fichiers : correction du vignettage des images.
+ - ticket #76959 : Ressources numériques : correction du vignettage des images.
+ 
+ 
  
\ No newline at end of file
diff --git a/application/modules/admin/controllers/RecordsController.php b/application/modules/admin/controllers/RecordsController.php
index e96e98d516c..c3d0296bb7b 100644
--- a/application/modules/admin/controllers/RecordsController.php
+++ b/application/modules/admin/controllers/RecordsController.php
@@ -27,6 +27,16 @@ class Admin_RecordsController extends ZendAfi_Controller_Action {
       return $this->_redirectToReferer();
     }
 
+    if ($album = $record->getAlbum()){
+      $album->createThumbnail();
+      $url_vignette = $album->getThumbnailUrl();
+      $record->setUrlVignette($url_vignette);
+      $record->setUrlImage($url_vignette);
+      $record->save();
+
+      $this->_helper->notify($this->_('Vignette réinitialisée pour "%s"', $record->getTitrePrincipal()));
+      return $this->_redirectToReferer();
+    }
 
     $record
       ->setUrlImage('')
diff --git a/library/Class/Album.php b/library/Class/Album.php
index a327e18ca14..7e8f2dacb0c 100644
--- a/library/Class/Album.php
+++ b/library/Class/Album.php
@@ -685,8 +685,8 @@ class Class_Album extends Storm_Model_Abstract {
     $on_update = function($old_file, $new_file) use ($to_delete)
       {
         if ($old_file && $old_file != $new_file)
-        foreach($to_delete as $item)
-          $this->getFileSystem()->unlink($item);
+          foreach($to_delete as $item)
+            $this->getFileSystem()->unlink($item);
 
         $this->createThumbnail();
 
@@ -723,6 +723,9 @@ class Class_Album extends Storm_Model_Abstract {
     if (!$this->hasFichier())
       return true;
 
+    if ($this->getFileSystem()->file_exists($this->getThumbnailPath()))
+      return true;
+
     try {
       $image = new Imagick($this->getVignettePath());
       $image->thumbnailImage(160, 0);
diff --git a/library/Class/Indexation/PseudoNotice.php b/library/Class/Indexation/PseudoNotice.php
index 6ecd6d4d581..16f58ab1afb 100644
--- a/library/Class/Indexation/PseudoNotice.php
+++ b/library/Class/Indexation/PseudoNotice.php
@@ -441,6 +441,7 @@ class Class_Indexation_PseudoNotice_Album extends Class_Indexation_PseudoNotice{
 
 
   protected function _index() {
+    $this->_model->createThumbnail();
     $url_vignette = $this->_model->getThumbnailUrl();
     $this->_notice->setUrlVignette($url_vignette);
     $this->_notice->setUrlImage($url_vignette);
diff --git a/tests/application/modules/admin/controllers/RecordsControllerTest.php b/tests/application/modules/admin/controllers/RecordsControllerTest.php
index 10637e8e9a7..526b154471e 100644
--- a/tests/application/modules/admin/controllers/RecordsControllerTest.php
+++ b/tests/application/modules/admin/controllers/RecordsControllerTest.php
@@ -96,6 +96,58 @@ class RecordsControllerResetThumbnailTest extends RecordsControllerTestCase {
 
 
 
+class RecordsControllerResetAlbumThumbnailTest extends RecordsControllerTestCase {
+  public function setUp() {
+    parent::setUp();
+
+    $album = $this->fixture('Class_Album',
+                            ['id' => 5,
+                             'visible' => 1,
+                             'status' => 3,
+                             'titre' => 'Potter',
+                             'fichier' => 'potter.png']);
+
+    $album->setFileSystem($this->mock()
+                          ->whenCalled('file_exists')
+                          ->with('./userfiles/album/5/thumb_potter.png')
+                          ->answers(true));
+
+    $album->index();
+    $id = $album->getNoticeId();
+
+    $this->dispatch('/admin/records/reset-thumbnail/id/12345', true);
+
+    Class_Notice::clearCache();
+    $this->_notice = Class_Notice::find($id);
+  }
+
+
+  /** @test */
+  public function urlImageShouldPotter() {
+    $this->assertContains('/userfiles/album/5/thumb_potter.png', $this->_notice->getUrlImage());
+  }
+
+
+  /** @test */
+  public function urlThumbnailShouldBePotter() {
+    $this->assertContains('/userfiles/album/5/thumb_potter.png', $this->_notice->getUrlVignette());
+  }
+
+
+  /** @test */
+  public function actionShouldRedirect() {
+    $this->assertRedirect();
+  }
+
+
+  /** @test */
+  public function responseShouldNotifyRecordUpdated() {
+    $this->assertFlashMessengerContentContains('Vignette réinitialisée pour "Harry Potter"');
+  }
+}
+
+
+
 class RecordsControllerResetThumbnailUnknowRecordTest extends RecordsControllerTestCase {
   public function setUp() {
     parent::setUp();
-- 
GitLab