From c4b39a7968b8c7438b059e5fb91dde48d88398fc Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Thu, 12 Apr 2018 15:06:05 +0200
Subject: [PATCH] dev #73871 handle failures

---
 library/Class/Notice/Xsl.php               | 11 +++++++++++
 library/Class/Testing/PhpCommand.php       |  4 +++-
 library/ZendAfi/View/Helper/Notice/Xsl.php | 23 ++++++++++++++++++++--
 tests/scenarios/Xsl/XslTest.php            |  8 +++++++-
 4 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/library/Class/Notice/Xsl.php b/library/Class/Notice/Xsl.php
index d2f93527c3e..3e8b9557091 100644
--- a/library/Class/Notice/Xsl.php
+++ b/library/Class/Notice/Xsl.php
@@ -34,6 +34,7 @@ class Class_Notice_Xsl {
   public function __construct($record, $profile) {
     $this->_record = $record;
     $this->_profile = $profile;
+    $this->_logErrors();
     $this->_xsl_file = $this->_getXslFileFromProfile();
   }
 
@@ -78,6 +79,16 @@ class Class_Notice_Xsl {
   }
 
 
+  protected function _logErrors() {
+    return static::getPhpCommand()->libxml_use_internal_errors(true);
+  }
+
+
+  public function getErrors() {
+    return static::getPhpCommand()->libxml_get_errors();
+  }
+
+
   protected function _getXslFileFromProfile() {
     $settings = $this->_profile->getCfgModulesPreferences('recherche', 'viewnotice', $this->_record->getTypeDoc());
     if(!$settings)
diff --git a/library/Class/Testing/PhpCommand.php b/library/Class/Testing/PhpCommand.php
index 455626d40aa..8c974a67afc 100644
--- a/library/Class/Testing/PhpCommand.php
+++ b/library/Class/Testing/PhpCommand.php
@@ -22,6 +22,8 @@
 class Class_Testing_PhpCommand extends Class_Testing_FileSystem {
   public function __construct() {
     $this->_known_functions = array_merge($this->_known_functions,
-                                          ['extension_loaded']);
+                                          ['extension_loaded',
+                                           'libxml_use_internal_errors',
+                                           'libxml_get_errors']);
   }
 }
\ No newline at end of file
diff --git a/library/ZendAfi/View/Helper/Notice/Xsl.php b/library/ZendAfi/View/Helper/Notice/Xsl.php
index ba8e02b2455..af777b87f93 100644
--- a/library/ZendAfi/View/Helper/Notice/Xsl.php
+++ b/library/ZendAfi/View/Helper/Notice/Xsl.php
@@ -37,7 +37,8 @@ class ZendAfi_View_Helper_Notice_Xsl extends ZendAfi_View_Helper_BaseHelper {
     $xslt = new XSLTProcessor();
     $xslt->importStylesheet($xsl_doc);
 
-    if(!$response = $xslt->transformToXml($xml))
+    $response = $xslt->transformToXml($xml);
+    if(($errors = $xsl->getErrors()) || (!$response))
       return $this->_tag('p', $this->_('La transformation du %s en HTML par le fichier %s n\'a pas fonctionné.',
                                        $this->view->tagAnchor(Class_Url::absolute($marc_xml),
                                                               $this->_('marc-xml'),
@@ -45,9 +46,27 @@ class ZendAfi_View_Helper_Notice_Xsl extends ZendAfi_View_Helper_BaseHelper {
                                        $this->view->tagAnchor(Class_Url::absolute($xsl_file),
                                                               $this->_('xsl'),
                                                               ['target' => 'blank'])),
-                         ['class' => 'error']);
+                         ['class' => 'error'])
+        . $this->_renderXsltErrors($errors);
 
     $xsl->unlink($marc_xml);
     return $response;
   }
+
+
+  protected function _renderXsltErrors($errors) {
+    $no_error_message = $this->_tag('p', $this->_('Aucune erreur levée par XSLTProcessor'));
+    if(!$errors)
+      return $no_error_message;
+
+    if(empty($errors))
+      return $no_error_message;
+
+    $html = [];
+    foreach ($errors as $error)
+      $html [] = $this->_tag('li', $this->_('Erreur levée par Libxml: %s', $error->message));
+
+    return $this->_tag('ul',
+                       implode($html));
+  }
 }
\ No newline at end of file
diff --git a/tests/scenarios/Xsl/XslTest.php b/tests/scenarios/Xsl/XslTest.php
index 91228869214..e5e126a97e1 100644
--- a/tests/scenarios/Xsl/XslTest.php
+++ b/tests/scenarios/Xsl/XslTest.php
@@ -121,7 +121,13 @@ class XslNoticeajaxDetailDispatchTest extends AbstractControllerTestCase {
                         ->answers(true)
 
                         ->whenCalled('unlink')
-                        ->answers(true);
+                        ->answers(true)
+
+                        ->whenCalled('libxml_use_internal_errors')
+                        ->answers(true)
+
+                        ->whenCalled('libxml_get_errors')
+                        ->answers([]);
 
     Class_Notice_Xsl::setPhpCommand($php_command);
 
-- 
GitLab