From 7b0d379feba01568ae5ba3f1bf3f95b96ce4a02e Mon Sep 17 00:00:00 2001
From: Yurii K <yurii-github@users.noreply.github.com>
Date: Sun, 23 Jul 2017 17:03:49 +0300
Subject: [PATCH] cleaning

---
 _test/bootstrap.php                           |  9 ++-------
 _test/tests/inc/pageutils_wikiFN.test.php     |  8 ++++----
 .../tests/lib/exe/css_at_import_less.test.php | 20 +++++++++++++++----
 inc/init.php                                  | 14 +++++++++++--
 lib/exe/css.php                               |  6 ++----
 5 files changed, 36 insertions(+), 21 deletions(-)

diff --git a/_test/bootstrap.php b/_test/bootstrap.php
index a83895c68..4f0d5efaa 100644
--- a/_test/bootstrap.php
+++ b/_test/bootstrap.php
@@ -3,11 +3,6 @@
  * Test Suite bootstrapping for DokuWiki
  */
 
-// convert windows path to unix-like
-function w2u($filename) {
-    return str_replace('\\', '/', $filename);
-}
-
 if(!defined('DOKU_UNITTEST')) define('DOKU_UNITTEST',dirname(__FILE__).'/');
 require_once DOKU_UNITTEST.'core/phpQuery-onefile.php';
 require_once DOKU_UNITTEST.'core/DokuWikiTest.php';
@@ -26,8 +21,8 @@ set_time_limit(0);
 ini_set('memory_limit','2048M');
 
 // prepare temporary directories
-define('DOKU_INC', w2u(dirname(dirname(__FILE__))).'/');
-define('TMP_DIR', w2u(sys_get_temp_dir()).'/dwtests-'.microtime(true));
+define('DOKU_INC', dirname(dirname(__FILE__)).'/');
+define('TMP_DIR', sys_get_temp_dir().'/dwtests-'.microtime(true));
 define('DOKU_CONF', TMP_DIR.'/conf/');
 define('DOKU_TMP_DATA', TMP_DIR.'/data/');
 
diff --git a/_test/tests/inc/pageutils_wikiFN.test.php b/_test/tests/inc/pageutils_wikiFN.test.php
index 6d9b73e22..33d6633ca 100644
--- a/_test/tests/inc/pageutils_wikiFN.test.php
+++ b/_test/tests/inc/pageutils_wikiFN.test.php
@@ -4,13 +4,13 @@ class wikifn_test extends DokuWikiTest {
 
 
     function test_cache_cleaning_cleanToUnclean(){
-        $this->assertEquals(wikiFN('wiki:',null,false),DOKU_TMP_DATA.'pages/wiki/.txt');
-        $this->assertEquals(wikiFN('wiki:',null,true),DOKU_TMP_DATA.'pages/wiki.txt');
+        $this->assertEquals(wikiFN('wiki:',null,false), w2u(DOKU_TMP_DATA.'pages/wiki/.txt'));
+        $this->assertEquals(wikiFN('wiki:',null,true), w2u(DOKU_TMP_DATA.'pages/wiki.txt'));
     }
 
     function test_cache_cleaning_uncleanToClean(){
-        $this->assertEquals(wikiFN('wiki:',null,true),DOKU_TMP_DATA.'pages/wiki.txt');
-        $this->assertEquals(wikiFN('wiki:',null,false),DOKU_TMP_DATA.'pages/wiki/.txt');
+        $this->assertEquals(wikiFN('wiki:',null,true), w2u(DOKU_TMP_DATA.'pages/wiki.txt'));
+        $this->assertEquals(wikiFN('wiki:',null,false), w2u(DOKU_TMP_DATA.'pages/wiki/.txt'));
     }
 
 }
diff --git a/_test/tests/lib/exe/css_at_import_less.test.php b/_test/tests/lib/exe/css_at_import_less.test.php
index c735ea3d5..ff980e5e1 100644
--- a/_test/tests/lib/exe/css_at_import_less.test.php
+++ b/_test/tests/lib/exe/css_at_import_less.test.php
@@ -26,7 +26,7 @@ class css_at_import_less_test extends DokuWikiTest {
             throw new Exception('failed to rename file');
         };
 
-        $this->import = w2u($import.'.less');
+        $this->import = $import.'.less';
     }
 
     private function csstest($input, $expected_css, $expected_less) {
@@ -34,14 +34,20 @@ class css_at_import_less_test extends DokuWikiTest {
         io_saveFile($this->file, $input);
         $css = css_loadfile($this->file, $location);
         $less = css_parseless($css);
-        $this->assertEquals($expected_css, $css);
+        $this->assertEquals($expected_css, w2u($css)); // w2u() for test pass, less works with both slashes on Windows OS
         $this->assertEquals($expected_less, $less);
     }
 
     public function test_basic() {
         $this->setUpFiles();
 
-        $import = preg_replace('#(^.*[/])#','',$this->import);
+        // remove path
+        if (isWindows()) {
+            $import = preg_replace('#(^.*[\\\\])#','',$this->import);
+        } else {
+            $import = preg_replace('#(^.*[/])#','',$this->import);
+        }
+
         $in_css = '@import "'.$import.'";';
         $in_less = '@foo: "bar";
 content: @foo;';
@@ -56,7 +62,13 @@ content: @foo;';
     public function test_subdirectory() {
         $this->setUpFiles('/foo/bar');
 
-        $import = preg_replace('#(^.*[/])#','',$this->import);
+        // remove path
+        if (isWindows()) {
+            $import = preg_replace('#(^.*[\\\\])#','',$this->import);
+        } else {
+            $import = preg_replace('#(^.*[/])#','',$this->import);
+        }
+
         $in_css = '@import "'.$import.'";';
         $in_less = '@foo: "bar";
 content: @foo;';
diff --git a/inc/init.php b/inc/init.php
index 8095a6084..e3dad8b33 100644
--- a/inc/init.php
+++ b/inc/init.php
@@ -3,8 +3,18 @@
  * Initialize some defaults needed for DokuWiki
  */
 
-function isWindows() {
-    return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? true : false;
+if (!function_exists('isWindows')) {
+    // checks if it is windows OS
+    function isWindows() {
+        return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? true : false;
+    }
+}
+
+if (!function_exists('w2u')) {
+    // convert windows path to unix-like on windows OS
+    function w2u($filename) {
+        return isWindows() ? str_replace('\\', '/', $filename) : $filename;
+    }
 }
 
 /**
diff --git a/lib/exe/css.php b/lib/exe/css.php
index b5d92e4d9..81e6a39ba 100644
--- a/lib/exe/css.php
+++ b/lib/exe/css.php
@@ -467,17 +467,15 @@ class DokuCssFile {
 
         if (is_null($this->relative_path)) {
             $basedir = array(DOKU_INC);
-            $filedir = dirname($this->filepath);
 
             // during testing, files may be found relative to a second base dir, TMP_DIR
             if (defined('DOKU_UNITTEST')) {
-                $basedir[] = w2u(realpath(TMP_DIR));
-                $filedir = w2u($filedir);
+                $basedir[] = realpath(TMP_DIR);
             }
 
             $basedir = array_map('preg_quote_cb', $basedir);
             $regex = '/^('.join('|',$basedir).')/';
-            $this->relative_path = preg_replace($regex, '', $filedir);
+            $this->relative_path = preg_replace($regex, '', dirname($this->filepath));
         }
 
         return $this->relative_path;
-- 
GitLab