From 96e03a966b64b8168c1b1d0221a394b019a8a50a Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Tue, 21 Oct 2014 17:24:49 +0200
Subject: [PATCH] 
 hotline_#17191_fix_sito_html_img_if_bluga_disabled_and_no_local_img

---
 ..._elment_img_when_no_bluga_and_no_local_img |  1 +
 library/Class/AdminVar.php                    |  5 ++
 library/ZendAfi/View/Helper/WebThumbnail.php  | 15 +++--
 .../ZendAfi/View/Helper/WebThumbnailTest.php  | 60 +++++++++++++------
 4 files changed, 59 insertions(+), 22 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/#17191_no_html_elment_img_when_no_bluga_and_no_local_img

diff --git a/VERSIONS_HOTLINE/#17191_no_html_elment_img_when_no_bluga_and_no_local_img b/VERSIONS_HOTLINE/#17191_no_html_elment_img_when_no_bluga_and_no_local_img
new file mode 100644
index 00000000000..378ed4aa395
--- /dev/null
+++ b/VERSIONS_HOTLINE/#17191_no_html_elment_img_when_no_bluga_and_no_local_img
@@ -0,0 +1 @@
+- ticket #17191 : supprime la balise image dans la boite sitothèque si bluga n'est pas activé ou si la vignette du site n'est pas sur le serveur
\ No newline at end of file
diff --git a/library/Class/AdminVar.php b/library/Class/AdminVar.php
index d3ace323f5b..875124191f1 100644
--- a/library/Class/AdminVar.php
+++ b/library/Class/AdminVar.php
@@ -428,6 +428,11 @@
 	}
 
 
+	public static function isBlugaEnabled() {
+		return '' != self::get('BLUGA_API_KEY');
+	}
+
+
 	public static function isAfficherDispoSurRechercheEnabled() {
 		return self::isModuleEnabled('AFFICHER_DISPONIBILITE_SUR_RECHERCHE');
 	}
diff --git a/library/ZendAfi/View/Helper/WebThumbnail.php b/library/ZendAfi/View/Helper/WebThumbnail.php
index 8bb050bd42b..d985599dea0 100644
--- a/library/ZendAfi/View/Helper/WebThumbnail.php
+++ b/library/ZendAfi/View/Helper/WebThumbnail.php
@@ -16,7 +16,7 @@
  *
  * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
  * along with AFI-OPAC 2.0; 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
  */
 //////////////////////////////////////////////////////////////////////////////////////////
 // OPAC3 :	Créer un thumbnail d'un site web et renvoi l'adresse de l'image
@@ -32,18 +32,25 @@ class ZendAfi_View_Helper_WebThumbnail extends ZendAfi_View_Helper_BaseHelper {
 
 		$filename = $this->fileNameFromUrl($url);
 		$filepath = $this->fullPath($filename);
-		
-		if (!$this->getFileWriter()->fileExists($filepath))
-			return BASE_URL."/sito/webthumbnail?url=$url";
+
+		if (!$this->getFileWriter()->fileExists($filepath)) {
+			return Class_AdminVar::isBlugaEnabled()
+				? $this->view->absoluteUrl(['controller' => 'sito',
+																		'action' => 'webthumbnail',
+																		'url' =>$url], null, true)
+				: '';
+		}
 
 		return $this->fullUrl($filename);
 	}
 
+
 	public function getThumbsDir() {
 		$path = USERFILESPATH.'/'.self::THUMBS_DIR.'/';
 		return str_replace('//', '/', $path);
 	}
 
+
 	public function checkThumbsDir() {
 		$dir = $this->getThumbsDir();
 		$file_writer = $this->getFileWriter();
diff --git a/tests/library/ZendAfi/View/Helper/WebThumbnailTest.php b/tests/library/ZendAfi/View/Helper/WebThumbnailTest.php
index a92227bed32..a79af211724 100644
--- a/tests/library/ZendAfi/View/Helper/WebThumbnailTest.php
+++ b/tests/library/ZendAfi/View/Helper/WebThumbnailTest.php
@@ -16,7 +16,7 @@
  *
  * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
  * along with AFI-OPAC 2.0; 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
  */
 require_once 'ViewHelperTestCase.php';
 require_once 'ZendAfi/View/Helper/WebThumbnail.php';
@@ -28,6 +28,7 @@ abstract class ViewHelperWebThumbnailTestCase extends ViewHelperTestCase {
 	public function setUp() {
 		parent::setUp();
 		$this->helper = new ZendAfi_View_Helper_WebThumbnail();
+		$this->helper->setView(new ZendAfi_Controller_Action_Helper_View());
 	}
 
 
@@ -45,7 +46,7 @@ class ViewHelperWebThumbnailTestReturnedUrl extends ViewHelperWebThumbnailTestCa
 		parent::setUp();
 		$this->thumbnailer = $this->mock();
 		$this->helper->setThumbnailer($this->thumbnailer);
-		
+
 		$this->_file_writer = Storm_Test_ObjectWrapper::mock()
 			->whenCalled('fileExists')->answers(false)
 			->whenCalled('dirExists')->answers(true);
@@ -64,13 +65,14 @@ $this->google_thumbnail_path = USERFILESPATH.'/web_thumbnails/www_google_com.jpg
 
 	/** @test */
 	public function googleDotComThumbnailUrl() {
+		Class_AdminVar::newInstanceWithId('BLUGA_API_KEY', ['valeur' => '12345']);
 		$this->thumbnailer
 			->whenCalled('fetchUrlToFile')
 			->with('http://www.google.com', $this->google_thumbnail_path)
 			->answers(true);
 
 		$url = $this->helper->webThumbnail('http://www.google.com');
-		$this->assertEquals(BASE_URL . '/sito/webthumbnail?url=http://www.google.com',
+		$this->assertContains('/sito/webthumbnail/url/'.urlencode('http://www.google.com'),
 												$url);
 	}
 
@@ -78,6 +80,8 @@ $this->google_thumbnail_path = USERFILESPATH.'/web_thumbnails/www_google_com.jpg
 	/** @test */
 	public function googleDotComThumbnailUrlWithExistingFile() {
 		$this->_file_writer
+			->whenCalled('dirExists')
+			->answers(true)
 			->whenCalled('fileExists')
 			->with($this->google_thumbnail_path)
       ->answers(true);
@@ -99,7 +103,7 @@ $this->google_thumbnail_path = USERFILESPATH.'/web_thumbnails/www_google_com.jpg
 
 		$this->thumbnailer
 			->whenCalled('fetchUrlToFile')
-			->with('http://www.google.fr/search?sourceid=chrome&ie=UTF-8&q=harry+potter', 
+			->with('http://www.google.fr/search?sourceid=chrome&ie=UTF-8&q=harry+potter',
 						 USERFILESPATH.'/web_thumbnails/www_google_fr_search_sourceid_chrome_ie_UTF-8_q_harry_potter.jpg')
 			->answers(true);
 
@@ -111,14 +115,14 @@ $this->google_thumbnail_path = USERFILESPATH.'/web_thumbnails/www_google_com.jpg
 
 	/** @test */
 	public function subpageUrlWithSpacesAndHTMLEntites() {
-		$this->_file_writer 
+		$this->_file_writer
 			->whenCalled('fileExists')
 			->with(USERFILESPATH.'/web_thumbnails/astrolabe_fr_my_search.jpg')
       ->answers(true);
 
 		$this->thumbnailer
 			->whenCalled('fetchUrlToFile')
-			->with('https://astrolabe.fr/my%20search', 
+			->with('https://astrolabe.fr/my%20search',
 						 USERFILESPATH.'/web_thumbnails/astrolabe_fr_my_search.jpg')
 			->answers(true);
 
@@ -129,7 +133,26 @@ $this->google_thumbnail_path = USERFILESPATH.'/web_thumbnails/www_google_com.jpg
 
 
 	/** @test */
-	public function thumbnailerCannotFetchImageReturnsEmptyUrl() {
+	public function thumbnailerCannotFetchImageAndBlugaDisabledReturnsEmptyUrl() {
+		$this->_file_writer
+			->whenCalled('fileExists')
+			->with($this->google_thumbnail_path)
+      ->answers(false);
+
+		$this->thumbnailer
+			->whenCalled('fetchUrlToFile')
+			->with('http://www.google.com',
+						 $this->google_thumbnail_path)
+			->answers(false);
+
+		$url = $this->helper->webThumbnail('http://www.google.com');
+		$this->assertEquals('',	$url);
+	}
+
+
+	/** @test */
+	public function thumbnailerCannotFetchImageAndBlugaEnabledReturnsWebthumbnailUrl() {
+		Class_AdminVar::newInstanceWithId('BLUGA_API_KEY', ['valeur' => '12345']);
 		$this->_file_writer
 			->whenCalled('fileExists')
 			->with($this->google_thumbnail_path)
@@ -137,13 +160,14 @@ $this->google_thumbnail_path = USERFILESPATH.'/web_thumbnails/www_google_com.jpg
 
 		$this->thumbnailer
 			->whenCalled('fetchUrlToFile')
-			->with('http://www.google.com', 
+			->with('http://www.google.com',
 						 $this->google_thumbnail_path)
 			->answers(false);
 
 		$url = $this->helper->webThumbnail('http://www.google.com');
-		$this->assertEquals(BASE_URL.'/sito/webthumbnail?url=http://www.google.com',	$url);
+		$this->assertContains(BASE_URL.'/sito/webthumbnail/url/'.urlencode('http://www.google.com'),	$url);
 	}
+
 }
 
 
@@ -177,7 +201,7 @@ class WebThumbnailerTestFetchFile extends ViewHelperTestCase {
 
 	/** @test */
 	public function fetchGoogleWithoutProxy() {
-		Zend_Registry::set('cfg', 
+		Zend_Registry::set('cfg',
 											 new Zend_Config(['proxy' => ['host' => null, 'port' => null]]));
 
 		$this->_fetchGoogle();
@@ -189,15 +213,15 @@ class WebThumbnailerTestFetchFile extends ViewHelperTestCase {
 
 	/** @test */
 	public function fetchGoogleThroughProxy() {
-		Zend_Registry::set('cfg', 
-											 new Zend_Config(['proxy' => 
+		Zend_Registry::set('cfg',
+											 new Zend_Config(['proxy' =>
 																				['host' => '192.168.2.1',
 																				 'port' => '8180',
 																				 'user' => 'afi',
 																				 'pass' => 'pafgjl']]));
 
 		$this->_fetchGoogle();
-		$this->assertEquals(new Bluga_HTTP_Request_Uri('tcp://192.168.2.1:8180'), 
+		$this->assertEquals(new Bluga_HTTP_Request_Uri('tcp://192.168.2.1:8180'),
 												$this->bluga->httpRequestAdapter->proxy);
 		$this->assertEquals(" Basic ".base64_encode('afi:pafgjl'),
 												$this->bluga->httpRequestAdapter->headers['Proxy-Authorization']);
@@ -206,15 +230,15 @@ class WebThumbnailerTestFetchFile extends ViewHelperTestCase {
 
 	/** @test */
 	public function fetchGoogleThroughProxyWithoutAuth() {
-		Zend_Registry::set('cfg', 
-											 new Zend_Config(['proxy' => 
+		Zend_Registry::set('cfg',
+											 new Zend_Config(['proxy' =>
 																				['host' => '192.168.2.3',
 																				 'port' => '3128',
 																				 'user' => null,
 																				 'pass' => null]]));
 
 		$this->_fetchGoogle();
-		$this->assertEquals(new Bluga_HTTP_Request_Uri('tcp://192.168.2.3:3128'), 
+		$this->assertEquals(new Bluga_HTTP_Request_Uri('tcp://192.168.2.3:3128'),
 												$this->bluga->httpRequestAdapter->proxy);
 		$this->assertFalse(array_key_exists('Proxy-Authorization',
 																				$this->bluga->httpRequestAdapter->headers));
@@ -239,13 +263,13 @@ class WebThumbnailerTestFetchFile extends ViewHelperTestCase {
 			->whenCalled('checkJobStatus')->answers(null)
 			->whenCalled('fetchToFile')->with($job, 'thumbnails/google_com.jpg')->answers(null);
 
-		$this->assertTrue($this->thumbnailer->fetchUrlToFile('http://google.com', 
+		$this->assertTrue($this->thumbnailer->fetchUrlToFile('http://google.com',
 																												 'thumbnails/google_com.jpg'));
 	}
 
 
 	public function testFetchGoogleWithoutKeyReturnsFalse() {
-		$this->assertFalse($this->thumbnailer->fetchUrlToFile('http://google.com', 
+		$this->assertFalse($this->thumbnailer->fetchUrlToFile('http://google.com',
 																													'thumbnails/google_com.jpg'));
 	}
 }
-- 
GitLab