From 4d1686d25d7f986d1ba33ad942191c63a9dd15f3 Mon Sep 17 00:00:00 2001
From: efalcy <efalcy@afi-sa.fr>
Date: Tue, 26 Mar 2024 14:34:10 +0000
Subject: [PATCH] Cache not deleted on file system

---
 VERSIONS_HOTLINE/197682                       |  1 +
 library/Class/FileManager/FileSystem.php      | 15 ++++++------
 .../Class/FileManager/FileSystemTest.php      | 24 +++++++++++++++++++
 3 files changed, 33 insertions(+), 7 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/197682

diff --git a/VERSIONS_HOTLINE/197682 b/VERSIONS_HOTLINE/197682
new file mode 100644
index 00000000000..8af719ef0a5
--- /dev/null
+++ b/VERSIONS_HOTLINE/197682
@@ -0,0 +1 @@
+ - correctif #197682 : Administration : correction du sytème de mise en cache de l'explorateur de fichiers qui empéchait la création de dossier.
\ No newline at end of file
diff --git a/library/Class/FileManager/FileSystem.php b/library/Class/FileManager/FileSystem.php
index eedeb86eb71..51de1853c87 100644
--- a/library/Class/FileManager/FileSystem.php
+++ b/library/Class/FileManager/FileSystem.php
@@ -243,8 +243,9 @@ class Class_FileManager_FileSystem {
 
   public function clearCache() {
     $cache = new Storm_Cache;
-    foreach($this->_cached_paths as $path)
-      $cache->remove($path);
+
+    foreach($this->_cached_paths as $key =>$path)
+      $cache->remove($key);
 
     return true;
   }
@@ -347,7 +348,7 @@ class Class_FileManager_FileSystem {
 
 
   protected function _glob($path, $cache = true) {
-    $this->_cached_paths[] = $path;
+    $this->_cached_paths[json_encode($path)] = 1;
 
     $closure = function() use ($path)
       {
@@ -372,7 +373,6 @@ class Class_FileManager_FileSystem {
         sort($paths, SORT_NATURAL);
         return $paths;
       };
-
     if($cache)
       return (new Storm_Cache())
         ->memoize(json_encode($path), $closure);
@@ -384,8 +384,8 @@ class Class_FileManager_FileSystem {
   public function search($term, $path) {
     if(!$term)
       return [];
-
-    $this->_cached_paths[] = $term . $path;
+    $cache_key = json_encode([$term . $path]);
+    $this->_cached_paths[$cache_key] = 1;
 
     $closure = function() use ($term, $path) {
       $cmd = sprintf('find %s -iname "*%s*"',
@@ -407,8 +407,9 @@ class Class_FileManager_FileSystem {
       return array_filter(array_map([Class_FileManager::class, 'find'], $found));
     };
 
+
     return (new Storm_Cache())
-      ->memoize(json_encode([$term . $path]), $closure);
+      ->memoize( $cache_key, $closure);
   }
 
 
diff --git a/tests/library/Class/FileManager/FileSystemTest.php b/tests/library/Class/FileManager/FileSystemTest.php
index ed6c831bd56..234fa9fd2d1 100644
--- a/tests/library/Class/FileManager/FileSystemTest.php
+++ b/tests/library/Class/FileManager/FileSystemTest.php
@@ -35,3 +35,27 @@ class Class_FileManager_FileSystemTest extends ModelTestCase {
     parent::tearDown();
   }
 }
+
+
+
+
+class FileSystemCacheTest extends AbstractControllerTestCase {
+  public function setUp() {
+    parent::setUp();
+    Storm_Cache::beVolatile();
+    Class_FileManager::beOpenBar();
+  }
+
+
+  /** @test */
+  public function pageShouldContainsFichiersDirectory() {
+    $this->dispatch('/admin/file-manager/index');
+    $this->assertFalse( (new \Storm_Cache)->load(json_encode("userfiles")));
+  }
+
+
+  public function tearDown() {
+    Class_FileManager::reset();
+    parent::tearDown();
+  }
+}
-- 
GitLab