From 7f74ede73c14ec914091e5908a91bafd4ec30d74 Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Fri, 13 Apr 2018 11:53:14 +0200
Subject: [PATCH] dev #73871 fix tests failures

---
 library/Class/FileManager.php                 |  1 +
 library/Class/Notice/Xsl.php                  | 40 +++++++++++++++++++
 library/ZendAfi/View/Helper/Notice/Xsl.php    | 12 +-----
 .../modules/AbstractControllerTestCase.php    |  1 +
 tests/scenarios/Xsl/XslTest.php               |  9 +++++
 5 files changed, 53 insertions(+), 10 deletions(-)

diff --git a/library/Class/FileManager.php b/library/Class/FileManager.php
index 5a4076dcda6..4dfe3ea6c72 100644
--- a/library/Class/FileManager.php
+++ b/library/Class/FileManager.php
@@ -244,6 +244,7 @@ class Class_FileManager extends Class_Entity {
 
   public static function reset() {
     static::$_open_bar = false;
+    static::setFileSystem(null);
   }
 
 
diff --git a/library/Class/Notice/Xsl.php b/library/Class/Notice/Xsl.php
index 8a27c72ec41..ef7c8871e70 100644
--- a/library/Class/Notice/Xsl.php
+++ b/library/Class/Notice/Xsl.php
@@ -25,12 +25,21 @@ class Class_Notice_Xsl {
 
   const KEY = 'xslt';
 
+  protected static $_xslt_processor;
+
   protected
     $_record,
     $_profile,
     $_xsl_file;
 
 
+  public static function reset() {
+    static::$_xslt_processor = null;
+    static::setPhpCommand(null);
+    static::setYaz(null);
+  }
+
+
   public function __construct($record, $profile) {
     $this->_record = $record;
     $this->_profile = $profile;
@@ -109,4 +118,35 @@ class Class_Notice_Xsl {
     Class_FileManager::beOpenBar();
     return Class_FileManager::find($this->getXslFileUrl());
   }
+
+
+  public function importStylesheet($path) {
+    $xsl = new DOMDocument();
+    $xsl->load($path);
+
+    $xslt = $this->getXSLTProcessor();
+    $xslt->importStylesheet($xsl);
+
+    return $this;
+  }
+
+
+  public function transformToXml($path) {
+    $xml = new DOMDocument();
+    $xml->load($path);
+
+    return $this->getXSLTProcessor()->transformToXml($xml);
+  }
+
+
+  public function getXSLTProcessor() {
+    return static::$_xslt_processor
+      ? static::$_xslt_processor
+      : new XSLTProcessor();
+  }
+
+
+  public static function setXSLTProcessor($xslt_processor) {
+    static::$_xslt_processor = $xslt_processor;
+  }
 }
\ 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 36c17f27458..9862b28309f 100644
--- a/library/ZendAfi/View/Helper/Notice/Xsl.php
+++ b/library/ZendAfi/View/Helper/Notice/Xsl.php
@@ -35,16 +35,8 @@ class ZendAfi_View_Helper_Notice_Xsl extends ZendAfi_View_Helper_BaseHelper {
                          $this->_('Le fichier marc-xml n\'a pas pu être généré'),
                          ['class' => 'error']);
 
-    $xsl_doc = new DOMDocument();
-    $xsl_doc->load($xsl_file->getRealpath());
-
-    $xml = new DOMDocument();
-    $xml->load($marc_xml);
-
-    $xslt = new XSLTProcessor();
-    $xslt->importStylesheet($xsl_doc);
-
-    $response = $xslt->transformToXml($xml);
+    $xslt = $xsl->importStylesheet($xsl_file->getRealpath());
+    $response = $xsl->transformToXml($marc_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é.',
diff --git a/tests/application/modules/AbstractControllerTestCase.php b/tests/application/modules/AbstractControllerTestCase.php
index cdf885fb3e4..f8e932cfea6 100644
--- a/tests/application/modules/AbstractControllerTestCase.php
+++ b/tests/application/modules/AbstractControllerTestCase.php
@@ -194,6 +194,7 @@ abstract class AbstractControllerTestCase extends Zend_Test_PHPUnit_ControllerTe
     Class_Log::resetInstance();
     Class_WebService_BibNumerique_Vignette::resetInstance();
     Class_FileManager::reset();
+    Class_Notice_Xsl::reset();
   }
 
 
diff --git a/tests/scenarios/Xsl/XslTest.php b/tests/scenarios/Xsl/XslTest.php
index c7ecfac81bf..0f2bf35d817 100644
--- a/tests/scenarios/Xsl/XslTest.php
+++ b/tests/scenarios/Xsl/XslTest.php
@@ -143,6 +143,15 @@ class XslNoticeajaxDetailDispatchTest extends AbstractControllerTestCase {
 
     Class_Notice_Xsl::setPhpCommand($php_command);
 
+    $xslt_processor = $this->mock()
+                           ->whenCalled('importStylesheet')
+                           ->answers(true)
+
+                           ->whenCalled('transformToXml')
+                           ->answers('<li><strong>Numéro de notice Koha : </strong>2774</li><br><li>');
+
+    Class_Notice_Xsl::setXSLTProcessor($xslt_processor);
+
     $profile = Class_Profil::getCurrentProfil();
     $profile->setCfgModulesPreferences(['xslt' => 'tests/scenarios/Xsl/record_description.xsl'],
                                        'recherche',
-- 
GitLab