From b9ccddf8a9d1cda72ce45aed0541ecb9b6543365 Mon Sep 17 00:00:00 2001
From: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Date: Wed, 19 Feb 2014 13:33:05 +0100
Subject: [PATCH] [TASK] getFilesInDirCanOrderByMtime-test should use vfs

As the other tests in GeneralUtilityTest also
getFilesInDirCanOrderByMtime() should use vfs.
Otherwise we might run into random problems if some
mtimes are equal.

Change-Id: Ic5ad10bd5beedb26c00d48602ebd5455525e4826
Resolves: #56121
Releases: 6.2
Reviewed-on: https://review.typo3.org/27717
Reviewed-by: Markus Klein
Tested-by: Markus Klein
Reviewed-by: Jigal van Hemert
Tested-by: Jigal van Hemert
---
 .../Tests/Unit/Utility/GeneralUtilityTest.php | 30 +++++++++++++++----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
index 1995c278ed23..f42d71e07cf6 100644
--- a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
+++ b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
@@ -3350,7 +3350,22 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 			'.secret.txt' => 'sammon',
 		);
 		\vfsStream::setup('test', NULL, $structure);
-		return \vfsStream::url('test');
+		$vfsUrl = \vfsStream::url('test');
+
+		if (version_compare(PHP_VERSION, '5.3.0', '>')) {
+			// set random values for mtime
+			foreach ($structure as $structureLevel1Key => $structureLevel1Content) {
+				$newMtime = rand();
+				if (is_array($structureLevel1Content)) {
+					foreach ($structureLevel1Content as $structureLevel2Key => $structureLevel2Content) {
+						touch($vfsUrl . '/' . $structureLevel1Key . '/' . $structureLevel2Key, $newMtime);
+					}
+				} else {
+					touch($vfsUrl . '/' . $structureLevel1Key, $newMtime);
+				}
+			}
+		}
+		return $vfsUrl;
 	}
 
 	/**
@@ -3416,10 +3431,10 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 	/**
 	 * @test
 	 */
-	public function getFilesInDirCanOrderAlphabetically() {
+	public function getFilesInDirDoesSortAlphabeticallyByDefault() {
 		$vfsStreamUrl = $this->getFilesInDirCreateTestDirectory();
 		$this->assertSame(
-			array_values(Utility\GeneralUtility::getFilesInDir($vfsStreamUrl, '', FALSE, '1')),
+			array_values(Utility\GeneralUtility::getFilesInDir($vfsStreamUrl, '', FALSE)),
 			array('.secret.txt', 'excludeMe.txt', 'test.css', 'test.js', 'testA.txt', 'testB.txt', 'testC.txt')
 		);
 	}
@@ -3428,8 +3443,13 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 	 * @test
 	 */
 	public function getFilesInDirCanOrderByMtime() {
+		if (version_compare(PHP_VERSION, '5.4.0', '<')) {
+			$this->markTestSkipped('touch() does not work with vfsStream in PHP 5.3 and below.');
+		}
+
+		$vfsStreamUrl = $this->getFilesInDirCreateTestDirectory();
 		$files = array();
-		$iterator = new \DirectoryIterator(__DIR__);
+		$iterator = new \DirectoryIterator($vfsStreamUrl);
 		foreach ($iterator as $fileinfo) {
 			if ($fileinfo->isFile()) {
 				$files[$fileinfo->getFilename()] = $fileinfo->getMTime();
@@ -3437,7 +3457,7 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 		}
 		asort($files);
 		$this->assertSame(
-			array_values(Utility\GeneralUtility::getFilesInDir(__DIR__, '', FALSE, 'mtime')),
+			array_values(Utility\GeneralUtility::getFilesInDir($vfsStreamUrl, '', FALSE, 'mtime')),
 			array_keys($files)
 		);
 	}
-- 
GitLab