diff --git a/VERSIONS_HOTLINE/109521 b/VERSIONS_HOTLINE/109521 new file mode 100644 index 0000000000000000000000000000000000000000..265905b135278b02de56aed0f881bdf24122c4dd --- /dev/null +++ b/VERSIONS_HOTLINE/109521 @@ -0,0 +1 @@ + - ticket #109521 : Bluga : Ajout de variables permettant de spécifier les dimensions souhaités pour les vignettes \ No newline at end of file diff --git a/library/Class/AdminVar.php b/library/Class/AdminVar.php index 6647b073e27acb6a1f30b87e626d786a04236fbd..25d4c7b320f0973b7ce6bcdff4dc072850a71442 100644 --- a/library/Class/AdminVar.php +++ b/library/Class/AdminVar.php @@ -58,8 +58,9 @@ class Class_AdminVarLoader extends Storm_Model_Loader { public function getValueOrDefault($name) { - return (($var = Class_AdminVar::find($name)) && ('' !== $var->getValeur())) ? - $var->getValeur() : Class_AdminVar::getStaticMeta($name)->getValue(); + return ($var = Class_AdminVar::find($name)) && ('' !== $var->getValeur()) + ? $var->getValeur() + : Class_AdminVar::getStaticMeta($name)->getValue(); } @@ -383,6 +384,10 @@ Pour vous désabonner de la lettre d\'information, merci de cliquer sur le lien 'ID_BIBLIOSURF' => Class_AdminVar_Meta::newDefault($this->_('Nom de la bibliothèque chez bibliosurf (en minuscules)'))->bePrivate(), 'ID_READ_SPEAKER' => Class_AdminVar_Meta::newDefault($this->_('Numéro de client Read Speaker <a target="_blank" href="http://webreader.readspeaker.com">http://webreader.readspeaker.com</a>')), 'BLUGA_API_KEY' => Class_AdminVar_Meta::newDefault($this->_('Clé API Bluga Webthumb <a target="_blank" href="http://webthumb.bluga.net/home">http://webthumb.bluga.net/home</a>')), + 'BLUGA_WIDTH' => Class_AdminVar_Meta::newDefault($this->_('Largeur des vignettes Bluga (en px)'), + ['value' => 300]), + 'BLUGA_HEIGHT' => Class_AdminVar_Meta::newDefault($this->_('Hauteur des vignettes Bluga (en px)'), + ['value' => 300]), 'AIDE_FICHE_ABONNE' => Class_AdminVar_Meta::newDefault($this->_('Texte d\'aide affiché dans la fiche abonné')), 'INTERDIRE_ENREG_UTIL' => Class_AdminVar_Meta::newOnOff($this->_('Supprime l\'affichage du lien d\'enregistrement dans les différents formulaires de connexion et interdit l\'enregistrement d\'utilisateurs')), 'LANGUES' => Class_AdminVar_Meta::newMultiInput($this->_('Liste des codes langue utilisées en plus du français séparées par des ;. Exemple: en;ro;es')), diff --git a/library/Class/WebService/Bluga.php b/library/Class/WebService/Bluga.php index 9303112ba357f08604fe0b0c82a6dabe396b6969..8649d738c08c93f667160982e98de67fca718982 100644 --- a/library/Class/WebService/Bluga.php +++ b/library/Class/WebService/Bluga.php @@ -20,9 +20,23 @@ */ class Class_WebService_Bluga { + const + DEFAULT_WIDTH = 300, + DEFAULT_HEIGHT = 300; + + protected + $_bluga, + $_try_timeout = 5, + $_throw_errors = false, + $_width, + $_height; + + + public function setThrowErrors($flag) { + $this->_throw_errors = (bool)$flag; + return $this; + } - protected $_bluga; - protected $_try_timeout = 5; public function getBlugaWebthumb() { if (!isset($this->_bluga)) @@ -30,14 +44,19 @@ class Class_WebService_Bluga { return $this->_bluga; } - public function setTryTimeout($try_timeout) { - $this->_try_timeout = $try_timeout; - } public function setBlugaWebthumb($instance) { $this->_bluga = $instance; + return $this; + } + + + public function setTryTimeout($try_timeout) { + $this->_try_timeout = $try_timeout; + return $this; } + public function fetchUrlToFile($url, $filename, $size = 'small') { $api_key = $this->getApiKey(); if (empty($api_key)) @@ -48,14 +67,15 @@ class Class_WebService_Bluga { try { $bluga->setApiKey($api_key); - $job = $bluga->addUrl($url, $size); + $job = $bluga->addUrl($url, $size, $this->_getWidth(), $this->_getHeight()); $bluga->submitRequests(); $nb_of_try = 3; while (!$bluga->readyToDownload()) { $nb_of_try -= 1; - if ($nb_of_try < 0) return false; + if ($nb_of_try < 0) + return false; sleep($this->_try_timeout); $bluga->httpRequestAdapter = $this->_buildAdapter(); @@ -66,10 +86,34 @@ class Class_WebService_Bluga { $bluga->fetchToFile($job, $filename); return true; } catch (Exception $e) { + if ($this->_throw_errors) + throw $e; + return false; } } + + protected function _getWidth() { + if (null !== $this->_width) + return $this->_width; + + return $this->_width = 0 < ($width = (int)Class_AdminVar::getValueOrDefault('BLUGA_WIDTH')) + ? $width + : static::DEFAULT_WIDTH; + } + + + protected function _getHeight() { + if (null !== $this->_height) + return $this->_height; + + return $this->_height = 0 < ($height = (int)Class_AdminVar::getValueOrDefault('BLUGA_HEIGHT')) + ? $height + : static::DEFAULT_HEIGHT; + } + + public function getApiKey() { return Class_AdminVar::get('BLUGA_API_KEY'); } @@ -91,5 +135,4 @@ class Class_WebService_Bluga { $adapter->headers['Proxy-Authorization'] = " Basic $authProxy"; return $adapter; } - } \ No newline at end of file diff --git a/tests/library/ZendAfi/View/Helper/WebThumbnailTest.php b/tests/library/ZendAfi/View/Helper/WebThumbnailTest.php index e6e8d139f92808f596b0f2e581f074e0e2fd71c8..c593d188f52a3bde9358ab474e5dfd89d4f6ca43 100644 --- a/tests/library/ZendAfi/View/Helper/WebThumbnailTest.php +++ b/tests/library/ZendAfi/View/Helper/WebThumbnailTest.php @@ -177,13 +177,18 @@ class ViewHelperWebThumbnailTestThumbnailer extends ViewHelperWebThumbnailTestCa } -class WebThumbnailerTestFetchFile extends ViewHelperTestCase { +class WebThumbnailerFetchGoogleTest extends ViewHelperTestCase { + protected $_storm_default_to_volatile = true; + public function setUp() { parent::setUp(); - $this->thumbnailer = new Class_WebService_Bluga(); + $this->bluga = $this->mock(); - $this->thumbnailer->setBlugaWebthumb($this->bluga); - $this->thumbnailer->setTryTimeout(0); + $this->thumbnailer = (new Class_WebService_Bluga) + ->setThrowErrors(true) + ->setBlugaWebthumb($this->bluga) + ->setTryTimeout(0); + $this->_old_cfg = Zend_Registry::get('cfg'); } @@ -195,7 +200,7 @@ class WebThumbnailerTestFetchFile extends ViewHelperTestCase { /** @test */ - public function fetchGoogleWithoutProxy() { + public function withoutProxyAdapterShouldNotHaveProxyHeader() { Zend_Registry::set('cfg', new Zend_Config(['proxy' => ['host' => null, 'port' => null]])); @@ -207,7 +212,7 @@ class WebThumbnailerTestFetchFile extends ViewHelperTestCase { /** @test */ - public function fetchGoogleThroughProxy() { + public function withProxyAdapterShouldHaveProxyHeader() { Zend_Registry::set('cfg', new Zend_Config(['proxy' => ['host' => '192.168.2.1', @@ -224,7 +229,7 @@ class WebThumbnailerTestFetchFile extends ViewHelperTestCase { /** @test */ - public function fetchGoogleThroughProxyWithoutAuth() { + public function withProxyWithoutAuthAdapterShouldHaveProxyHeaderWithoutAuthorization() { Zend_Registry::set('cfg', new Zend_Config(['proxy' => ['host' => '192.168.2.3', @@ -240,19 +245,47 @@ class WebThumbnailerTestFetchFile extends ViewHelperTestCase { } - protected function _fetchGoogle() { - $this->bugla_api_key = new Class_AdminVar(); - $this->bugla_api_key - ->setId('BLUGA_API_KEY') - ->setValeur('12345'); - Class_AdminVar::getLoader()->cacheInstance($this->bugla_api_key); + /** @test */ + public function withoutKeyShouldReturnFalse() { + $this->assertFalse($this->thumbnailer->fetchUrlToFile('http://google.com', + 'thumbnails/google_com.jpg')); + } - $job = $this->mock(); + /** @test */ + public function shouldAddUrlWithDefaultWidthAndHeight() { + $this->_fetchGoogle(); + $this->assertEquals(['http://google.com', 'small', 300, 300], + $this->bluga->getAttributesForLastCallOn('addUrl')); + } + + + /** @test */ + public function withInvalidWidthShouldAddUrlWithDefaultWidthAndHeight() { + Class_AdminVar::set('BLUGA_WIDTH', 'toto'); + $this->_fetchGoogle(); + $this->assertEquals(['http://google.com', 'small', 300, 300], + $this->bluga->getAttributesForLastCallOn('addUrl')); + } + + /** @test */ + public function withSizeVariablesShouldAddUrlWithThem() { + Class_AdminVar::set('BLUGA_WIDTH', '800'); + Class_AdminVar::set('BLUGA_HEIGHT', '600'); + $this->_fetchGoogle(); + $this->assertEquals(['http://google.com', 'small', 800, 600], + $this->bluga->getAttributesForLastCallOn('addUrl')); + } + + + protected function _fetchGoogle() { + Class_AdminVar::set('BLUGA_API_KEY', '12345'); + + $job = $this->mock(); $this->bluga - ->whenCalled('setApiKey')->with('12345')->answers(null) - ->whenCalled('addUrl')->with('http://google.com', 'small')->answers($job) + ->whenCalled('setApiKey')->answers(null) + ->whenCalled('addUrl')->answers($job) ->whenCalled('submitRequests')->answers(null) ->whenCalled('readyToDownload')->answers(true) ->whenCalled('checkJobStatus')->answers(null) @@ -261,13 +294,4 @@ class WebThumbnailerTestFetchFile extends ViewHelperTestCase { $this->assertTrue($this->thumbnailer->fetchUrlToFile('http://google.com', 'thumbnails/google_com.jpg')); } - - - public function testFetchGoogleWithoutKeyReturnsFalse() { - $this->assertFalse($this->thumbnailer->fetchUrlToFile('http://google.com', - 'thumbnails/google_com.jpg')); - } } - - -?> \ No newline at end of file