From 30085ef3769da1a92634254411d688dbb753e762 Mon Sep 17 00:00:00 2001
From: Yurii K <yurii-github@users.noreply.github.com>
Date: Sun, 23 Jul 2017 16:13:12 +0300
Subject: [PATCH] fix tests on windows

---
 _test/bootstrap.php                           |  9 ++++--
 _test/tests/inc/mailer.test.php               | 32 ++++++++++++++++---
 .../tests/lib/exe/css_at_import_less.test.php | 14 ++++----
 inc/Mailer.class.php                          |  5 +--
 inc/init.php                                  |  4 +++
 lib/exe/css.php                               |  6 ++--
 6 files changed, 50 insertions(+), 20 deletions(-)

diff --git a/_test/bootstrap.php b/_test/bootstrap.php
index 4f0d5efaa..a83895c68 100644
--- a/_test/bootstrap.php
+++ b/_test/bootstrap.php
@@ -3,6 +3,11 @@
  * 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';
@@ -21,8 +26,8 @@ set_time_limit(0);
 ini_set('memory_limit','2048M');
 
 // prepare temporary directories
-define('DOKU_INC', dirname(dirname(__FILE__)).'/');
-define('TMP_DIR', sys_get_temp_dir().'/dwtests-'.microtime(true));
+define('DOKU_INC', w2u(dirname(dirname(__FILE__))).'/');
+define('TMP_DIR', w2u(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/mailer.test.php b/_test/tests/inc/mailer.test.php
index 6100ef1bf..683f821dc 100644
--- a/_test/tests/inc/mailer.test.php
+++ b/_test/tests/inc/mailer.test.php
@@ -88,27 +88,49 @@ class mailer_test extends DokuWikiTest {
         $mail->to('Andreas Gohr <andi@splitbrain.org>');
         $mail->cleanHeaders();
         $headers = $mail->prop('headers');
-        $this->assertEquals('Andreas Gohr <andi@splitbrain.org>', $headers['To']);
+
+        if (isWindows()) { // see FS#652
+            $this->assertEquals('andi@splitbrain.org', $headers['To']);
+        } else {
+            $this->assertEquals('Andreas Gohr <andi@splitbrain.org>', $headers['To']);
+        }
 
         $mail->to('Andreas Gohr <andi@splitbrain.org> , foo <foo@example.com>');
         $mail->cleanHeaders();
         $headers = $mail->prop('headers');
-        $this->assertEquals('Andreas Gohr <andi@splitbrain.org>, foo <foo@example.com>', $headers['To']);
+        if (isWindows()) { // see FS#652
+            $this->assertEquals('andi@splitbrain.org,  foo@example.com', $headers['To']);
+        } else {
+            $this->assertEquals('Andreas Gohr <andi@splitbrain.org>, foo <foo@example.com>', $headers['To']);
+        }
 
         $mail->to('Möp <moep@example.com> , foo <foo@example.com>');
         $mail->cleanHeaders();
         $headers = $mail->prop('headers');
-        $this->assertEquals('=?UTF-8?B?TcO2cA==?= <moep@example.com>, foo <foo@example.com>', $headers['To']);
+
+        if (isWindows()) { // see FS#652
+            $this->assertEquals('moep@example.com,  foo@example.com', $headers['To']);
+        } else {
+            $this->assertEquals('=?UTF-8?B?TcO2cA==?= <moep@example.com>, foo <foo@example.com>', $headers['To']);
+        }
 
         $mail->to(array('Möp <moep@example.com> ',' foo <foo@example.com>'));
         $mail->cleanHeaders();
         $headers = $mail->prop('headers');
-        $this->assertEquals('=?UTF-8?B?TcO2cA==?= <moep@example.com>, foo <foo@example.com>', $headers['To']);
+        if (isWindows()) { // see FS#652
+            $this->assertEquals('moep@example.com,  foo@example.com', $headers['To']);
+        } else {
+            $this->assertEquals('=?UTF-8?B?TcO2cA==?= <moep@example.com>, foo <foo@example.com>', $headers['To']);
+        }
 
         $mail->to(array('Beet, L van <lvb@example.com>',' foo <foo@example.com>'));
         $mail->cleanHeaders();
         $headers = $mail->prop('headers');
-        $this->assertEquals('=?UTF-8?B?QmVldCwgTCB2YW4=?= <lvb@example.com>, foo <foo@example.com>', $headers['To']);
+        if (isWindows()) { // see FS#652
+            $this->assertEquals('lvb@example.com,  foo@example.com', $headers['To']);
+        } else {
+            $this->assertEquals('=?UTF-8?B?QmVldCwgTCB2YW4=?= <lvb@example.com>, foo <foo@example.com>', $headers['To']);
+        }
 
 
     }
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 4a6efcf44..c735ea3d5 100644
--- a/_test/tests/lib/exe/css_at_import_less.test.php
+++ b/_test/tests/lib/exe/css_at_import_less.test.php
@@ -14,19 +14,19 @@ class css_at_import_less_test extends DokuWikiTest {
             mkdir($dir, 0777, true);
         }
         if (!is_dir($dir)) {
-            $this->markTestSkipped('Could not create directory.');
+            throw new Exception('Could not create directory.');
         }
 
         $this->file = tempnam($dir, 'css');
 
         $import = '';
-        do {
-            if ($import) unlink($import);
-            $import = tempnam($dir, 'less');
-            $ok = rename($import, $import.'.less');
-        } while (!$ok);
+        if ($import) unlink($import);
+        $import = tempnam($dir, 'less');
+        if (rename($import, $import.'.less') === false) {
+            throw new Exception('failed to rename file');
+        };
 
-        $this->import = $import.'.less';
+        $this->import = w2u($import.'.less');
     }
 
     private function csstest($input, $expected_css, $expected_less) {
diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php
index b82505f8f..041a7ac35 100644
--- a/inc/Mailer.class.php
+++ b/inc/Mailer.class.php
@@ -331,9 +331,6 @@ class Mailer {
      * @return false|string  the prepared header (can contain multiple lines)
      */
     public function cleanAddress($addresses) {
-        // No named recipients for To: in Windows (see FS#652)
-        $names = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true;
-
         $headers = '';
         if(!is_array($addresses)){
             $addresses = explode(',', $addresses);
@@ -367,7 +364,7 @@ class Mailer {
             }
 
             // text was given
-            if(!empty($text) && $names) {
+            if(!empty($text) && !isWindows()) { // No named recipients for To: in Windows (see FS#652)
                 // add address quotes
                 $addr = "<$addr>";
 
diff --git a/inc/init.php b/inc/init.php
index 4e4cd6450..8095a6084 100644
--- a/inc/init.php
+++ b/inc/init.php
@@ -3,6 +3,10 @@
  * Initialize some defaults needed for DokuWiki
  */
 
+function isWindows() {
+    return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? true : false;
+}
+
 /**
  * timing Dokuwiki execution
  *
diff --git a/lib/exe/css.php b/lib/exe/css.php
index 81e6a39ba..b5d92e4d9 100644
--- a/lib/exe/css.php
+++ b/lib/exe/css.php
@@ -467,15 +467,17 @@ 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[] = realpath(TMP_DIR);
+                $basedir[] = w2u(realpath(TMP_DIR));
+                $filedir = w2u($filedir);
             }
 
             $basedir = array_map('preg_quote_cb', $basedir);
             $regex = '/^('.join('|',$basedir).')/';
-            $this->relative_path = preg_replace($regex, '', dirname($this->filepath));
+            $this->relative_path = preg_replace($regex, '', $filedir);
         }
 
         return $this->relative_path;
-- 
GitLab