From 6d93b3b124b6ce2255fc7e49872c7cfa87db8de5 Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Thu, 18 Feb 2021 11:45:12 +0100
Subject: [PATCH] hotline MT #128703 fix pnb loan link in record view

---
 VERSIONS_HOTLINE/128703                       |   1 +
 .../opac/controllers/NoticeajaxController.php |  29 ++--
 .../ZendAfi/Controller/Action/Helper/Ajax.php |   4 +-
 .../Action/Helper/HTMLAjaxResponse.php        |   2 +-
 library/templates/Intonation/View/Ajax.php    |   3 +-
 .../templates/Intonation/View/RenderEmbed.php |   3 +
 .../Intonation/View/RenderRecord/Items.php    |   1 -
 .../TemplateDigitalResourcesTest.php          | 134 ++++++++++++++++++
 tests/scenarios/Templates/TemplatesTest.php   |  69 ---------
 9 files changed, 158 insertions(+), 88 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/128703
 create mode 100644 tests/scenarios/Templates/TemplateDigitalResourcesTest.php

diff --git a/VERSIONS_HOTLINE/128703 b/VERSIONS_HOTLINE/128703
new file mode 100644
index 00000000000..44b824bc195
--- /dev/null
+++ b/VERSIONS_HOTLINE/128703
@@ -0,0 +1 @@
+ - ticket #128703 : Magasin de thèmes : correction des liens d'emprunt de ressources PNB.
\ No newline at end of file
diff --git a/application/modules/opac/controllers/NoticeajaxController.php b/application/modules/opac/controllers/NoticeajaxController.php
index 54342932a71..0d81295f708 100644
--- a/application/modules/opac/controllers/NoticeajaxController.php
+++ b/application/modules/opac/controllers/NoticeajaxController.php
@@ -623,22 +623,22 @@ class NoticeAjaxController extends ZendAfi_Controller_Action {
              'clef_oeuvre' => $this->notice->getClefOeuvre(),
              'type_doc' => $this->notice->getFamilleId()];
 
-    $helper = $this->_helper->getHelper('HTMLAjaxResponse');
-
     if ( !$data = Class_WebService_AllServices::runServiceAfi(6, $args))
-      return $helper->direct('');
+      return $this->_helper->ajax();
 
     if ( !isset($data['player']) || (!$html = $data['player']))
-      return $helper->direct('');
+      return $this->_helper->ajax();
 
-    $source = isset($data['source']) ? $data['source'] : $this->_('source inconnue');
+    return $this->_helper->ajax(function() use ($html, $data)
+                                {
+                                  $source = isset($data['source'])
+                                    ? $data['source']
+                                    : $this->_('source inconnue');
 
-    preg_match('/src="([^"]+)"/', $html, $match);
+                                  preg_match('/src="([^"]+)"/', $html, $match);
 
-    if ( !$url = $match[1])
-      return $helper->direct('');
-
-    $helper->htmlAjaxResponseWithScript($this->view->renderEmbed($data['source'], $url));
+                                  return $this->view->renderEmbed($source, $match[1]);
+                                });
   }
 
 
@@ -707,10 +707,11 @@ class NoticeAjaxController extends ZendAfi_Controller_Action {
     if (empty($html))
       $html = $this->view->renderEmptyMedia();
 
-    $helper = $this->_helper->getHelper('HTMLAjaxResponse');
-    return empty($html)
-      ? $helper->direct('')
-      : $helper->htmlAjaxResponseWithScript($this->view->grid($html));
+    return $this->_helper->ajax(function() use ($html)
+                                {
+                                  if ($html)
+                                    return $this->view->grid($html);
+                                });
   }
 
 
diff --git a/library/ZendAfi/Controller/Action/Helper/Ajax.php b/library/ZendAfi/Controller/Action/Helper/Ajax.php
index 1d6063891e8..96f761706b7 100644
--- a/library/ZendAfi/Controller/Action/Helper/Ajax.php
+++ b/library/ZendAfi/Controller/Action/Helper/Ajax.php
@@ -21,7 +21,9 @@
 
 
 class ZendAfi_Controller_Action_Helper_Ajax extends Zend_Controller_Action_Helper_Abstract {
-  public function direct($callback) {
+  public function direct($callback = null) {
+    if (!$callback)
+      $callback = function() {};
     session_write_close();
     $view = $this->getActionController()->view;
     $viewRenderer = $this->getActionController()->getHelper('ViewRenderer');
diff --git a/library/ZendAfi/Controller/Action/Helper/HTMLAjaxResponse.php b/library/ZendAfi/Controller/Action/Helper/HTMLAjaxResponse.php
index 4b7c1d5ad10..ba111ec3c51 100644
--- a/library/ZendAfi/Controller/Action/Helper/HTMLAjaxResponse.php
+++ b/library/ZendAfi/Controller/Action/Helper/HTMLAjaxResponse.php
@@ -35,7 +35,7 @@ class ZendAfi_Controller_Action_Helper_HTMLAjaxResponse
   public function htmlAjaxResponseWithScript($content) {
     Class_ScriptLoader::getInstance()
       ->addAdminScript('onload_utils')
-      ->addJQueryReady('setupAnchorsTarget();');
+      ->addJQueryReady('initializePopups();setupAnchorsTarget();');
 
     $this->direct(Class_ScriptLoader::getInstance()->html() . $content);
   }
diff --git a/library/templates/Intonation/View/Ajax.php b/library/templates/Intonation/View/Ajax.php
index 46e2e86f752..08fa740deb9 100644
--- a/library/templates/Intonation/View/Ajax.php
+++ b/library/templates/Intonation/View/Ajax.php
@@ -33,8 +33,7 @@ class Intonation_View_Ajax extends Intonation_View_Opac {
 
   public function render() {
     Class_ScriptLoader::getInstance()
-      ->addJQueryReady('initializePopups();')
-      ->addJQueryReady('setupAnchorsTarget();');
+      ->addJQueryReady('initializePopups();setupAnchorsTarget();');
 
     return $this->_body . Class_ScriptLoader::getInstance()->html();
   }
diff --git a/library/templates/Intonation/View/RenderEmbed.php b/library/templates/Intonation/View/RenderEmbed.php
index 8a54a89f99e..5f22ea4361c 100644
--- a/library/templates/Intonation/View/RenderEmbed.php
+++ b/library/templates/Intonation/View/RenderEmbed.php
@@ -22,6 +22,9 @@
 
 class Intonation_View_RenderEmbed extends ZendAfi_View_Helper_BaseHelper {
   public function renderEmbed($source, $url) {
+    if (!$url)
+      return '';
+
     $html = $this->_div(['class' => 'embed-responsive embed-responsive-16by9'],
                         $this->_tag('iframe',
                                     '',
diff --git a/library/templates/Intonation/View/RenderRecord/Items.php b/library/templates/Intonation/View/RenderRecord/Items.php
index 15214d1ec2c..922ac20aa35 100644
--- a/library/templates/Intonation/View/RenderRecord/Items.php
+++ b/library/templates/Intonation/View/RenderRecord/Items.php
@@ -21,7 +21,6 @@
 
 
 class Intonation_View_RenderRecord_Items extends Intonation_View_RenderRecord_Abstract {
-
   public function RenderRecord_Items($record) {
     return $this->_renderCurrentSection($record, 'document_items');
   }
diff --git a/tests/scenarios/Templates/TemplateDigitalResourcesTest.php b/tests/scenarios/Templates/TemplateDigitalResourcesTest.php
new file mode 100644
index 00000000000..5d4c573396f
--- /dev/null
+++ b/tests/scenarios/Templates/TemplateDigitalResourcesTest.php
@@ -0,0 +1,134 @@
+<?php
+/**
+ * Copyright (c) 2012-2021, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * 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
+ */
+
+
+class TemplateDigitalResourcesDispatchTest extends AbstractControllerTestCase {
+  protected $_storm_default_to_volatile = true;
+
+
+  public function setUp() {
+    parent::setUp();
+
+    $this->_buildTemplateProfil(['id' => 9,
+                                 'template' => 'MUSCLE']);
+
+    $this->fixture('Class_Notice',
+                   ['id' => 123,
+                    'type_doc' => Class_TypeDoc::LIVRE_NUM]);
+
+    $this->fixture('Class_Exemplaire',
+                   ['id' => 34,
+                    'id_origine' => 8,
+                    'id_notice' => 123]);
+
+    $this->fixture('Class_Album',
+                   ['id' => 8,
+                    'titre' => 'old book',
+                    'type_doc_id' => Class_TypeDoc::LIVRE_NUM]);
+
+    $this->dispatch('noticeajax/digital-resources/id_notice/123');
+  }
+
+
+  /** @test */
+  public function bookletShouldBeLoadedWithAlbumTypeLivreNumerique() {
+    $this->assertXPathContentContains('//script', '_load_in_scriptsRoot');
+  }
+
+
+  /** @test */
+  public function setupAnchorsTargetShouldBeInScript() {
+    $this->assertXPathContentContains('//script', 'setupAnchorsTarget();');
+  }
+
+
+  /** @test */
+  public function initializePopupsShouldBeInScript() {
+    $this->assertXPathContentContains('//script', 'initializePopups();');
+  }
+}
+
+
+
+
+class TemplateDigitalResourcesMediaAndTrailerDispatchTest extends AbstractControllerTestCase {
+
+  protected $_storm_default_to_volatile = true;
+
+
+  public function setUp() {
+    parent::setUp();
+
+
+    $this->_buildTemplateProfil(['id' => 5,
+                                 'template' => 'CHILI']);
+
+    $this->fixture('Class_Notice',
+                   ['id' => 34,
+                    'titre_principal' => 'Psycho',
+                   ]);
+
+    Class_CosmoVar::set('url_services', 'https://cache-server.org');
+
+    $mock = $this
+      ->mock()
+      ->whenCalled('open_url')
+      ->answers(json_encode(['source' => 'testing',
+                             'player' => '<iframe src="https://www.super-trailers.org/?media=18397590" style="width:500px; height:400px" frameborder="0"></iframe>']));
+
+    Class_WebService_AllServices::setHttpClient($mock);
+  }
+
+
+  /** @test */
+  public function noticeajaxTrailershouldDisplayTrailer() {
+    $this->dispatch('/noticeajax/trailer/id/34');
+    $this->assertXPath('//div[contains(@class, "embed-responsive")]//iframe[@src= "https://www.super-trailers.org/?media=18397590"][@class="embed-responsive-item"]', $this->_response->getBody());
+  }
+
+
+  /** @test */
+  public function noticeajaxTrailershouldContainsInitializePopup() {
+    $this->dispatch('/noticeajax/trailer/id/34');
+    $this->assertXPathContentContains('//script', 'initializePopups();');
+  }
+
+
+  /** @test */
+  public function noticeajaxMediaDisplayTrailer() {
+    $this->dispatch('/noticeajax/media/id/34');
+    $this->assertXPath('//div[contains(@class, "embed-responsive")]//iframe[@src= "https://www.super-trailers.org/?media=18397590"][@class="embed-responsive-item"]');
+  }
+
+
+  /** @test */
+  public function shouldRenderEditTrailer() {
+    $this->dispatch('/noticeajax/media/id/34');
+    $this->assertXPath('//button[contains(@onclick, "/admin/records/trailer/id/34")]');
+  }
+
+
+  /** @test */
+  public function noticeajaxMediaShouldContainsInitializePopup() {
+    $this->dispatch('/noticeajax/media/id/34');
+    $this->assertXPathContentContains('//script', 'initializePopups();');
+  }
+}
diff --git a/tests/scenarios/Templates/TemplatesTest.php b/tests/scenarios/Templates/TemplatesTest.php
index a8fa68f82e9..0d9d4a08f89 100644
--- a/tests/scenarios/Templates/TemplatesTest.php
+++ b/tests/scenarios/Templates/TemplatesTest.php
@@ -1327,49 +1327,6 @@ class TemplatesRecordMediaDispatchTest extends TemplatesIntonationTestCase {
 
 
 
-class TemplatesDispatchNoticeAjaxTrailerTest extends TemplatesIntonationTestCase {
-
-  public function setUp() {
-    parent::setUp();
-    $this->fixture('Class_Notice',
-                   ['id' => 34,
-                    'titre_principal' => 'Psycho',
-                   ]);
-
-    Class_CosmoVar::set('url_services', 'https://cache-server.org');
-
-    $mock = $this
-      ->mock()
-      ->whenCalled('open_url')
-      ->answers(json_encode(['source' => 'testing',
-                             'player' => '<iframe src="https://www.super-trailers.org/?media=18397590" style="width:500px; height:400px" frameborder="0"></iframe>']));
-
-    Class_WebService_AllServices::setHttpClient($mock);
-  }
-
-
-  /** @test */
-  public function noticeajaxTrailershouldDisplayTrailer() {
-    $this->dispatch('/noticeajax/trailer/id/34/id_profil/72');
-    $this->assertXPath('//div[contains(@class, "embed-responsive")]//iframe[@src= "https://www.super-trailers.org/?media=18397590"][@class="embed-responsive-item"]');
-  }
-
-
-  /** @test */
-  public function noticeajaxMediaDisplayTrailer() {
-    $this->dispatch('/noticeajax/media/id/34/id_profil/72');
-    $this->assertXPath('//div[contains(@class, "embed-responsive")]//iframe[@src= "https://www.super-trailers.org/?media=18397590"][@class="embed-responsive-item"]');
-  }
-
-
-  /** @test */
-  public function shouldRenderEditTrailer() {
-    $this->dispatch('/noticeajax/media/id/34/id_profil/72');
-    $this->assertXPath('//button[contains(@onclick, "/admin/records/trailer/id/34")]');
-  }
-}
-
-
 
 class TemplatesDispatchNoticeAjaxPicturesTest extends TemplatesIntonationTestCase {
 
@@ -2253,32 +2210,6 @@ class TemplateIntonationWithSkinTest extends TemplatesEnabledTestCase {
 
 
 
-class TemplatesNoticeAjaxDigitalResourceTest extends TemplatesIntonationTestCase {
-
-  public function setup() {
-    parent::setup();
-    $this->fixture('Class_Notice', ['id' => 123,
-                                    'type_doc' => Class_TypeDoc::LIVRE_NUM]);
-    $this->fixture('Class_Exemplaire', ['id' => 34,
-                                        'id_origine' => 8,
-                                        'id_notice' => 123]);
-
-    $album = $this->fixture('Class_Album', ['id' => 8,
-                                            'titre' => 'old book',
-                                            'type_doc_id' => Class_TypeDoc::LIVRE_NUM]);
-
-
-    $this->dispatch('noticeajax/digital-resources/id_notice/123/id_profil/72');
-  }
-
-
-  /** @test */
-  public function bookletShouldBeLoadedWithAlbumTypeLivreNumerique() {
-    $this->assertXPathContentContains('//script', '_load_in_scriptsRoot');
-  }
-}
-
-
 
 class TemplatesDispatchIntonationAuthLoginTest extends TemplatesIntonationTestCase {
 
-- 
GitLab