[BUGFIX] Fix .zip-export on windows and add unit test 18/19118/3
authorJost Baron <jost.baron@gmx.de>
Fri, 15 Mar 2013 20:06:21 +0000 (21:06 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Wed, 20 Mar 2013 20:34:17 +0000 (21:34 +0100)
On Windows servers (xampp), the .zip-export of extensions did not
work because \ZipArchive::addFile can't add directories on Windows.

Added a unit test for the export function.

Fixes: #45140
Releases: 6.0, 6.1
Change-Id: If248bf031d3d7b621971de609b86eedc0ee05a64
Reviewed-on: https://review.typo3.org/19118
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/extensionmanager/Classes/Utility/FileHandlingUtility.php
typo3/sysext/extensionmanager/Tests/Unit/Utility/FileHandlingUtilityTest.php

index 8b55ac4..7e1c1a6 100644 (file)
@@ -380,7 +380,14 @@ class FileHandlingUtility implements \TYPO3\CMS\Core\SingletonInterface {
                $files = array_filter($files);
 
                foreach ($files as $file) {
-                       $zip->addFile($extensionPath . $file, $file);
+                       $fullPath = $extensionPath . $file;
+                       // Distinguish between files and directories, as creation of the archive
+                       // fails on Windows when trying to add a directory with "addFile".
+                       if (is_dir($fullPath)) {
+                               $zip->addEmptyDir($file);
+                       } else {
+                               $zip->addFile($fullPath, $file);
+                       }
                }
 
                $zip->close();
index 88639f7..b7f3ad4 100644 (file)
@@ -444,6 +444,60 @@ class FileHandlingUtilityTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase
                        )
                );
        }
+
+       /**
+        * Warning: This test asserts multiple things at once to keep the setup short.
+        *
+        * @test
+        */
+       public function createZipFileFromExtensionGeneratesCorrectArchive() {
+               // Create extension for testing:
+               $extKey = $this->createFakeExtension();
+               $extensionRoot = $this->fakedExtensions[$extKey]['siteAbsPath'];
+
+               // Build mocked fileHandlingUtility:
+               $fileHandlerMock = $this->getAccessibleMock(
+                       'TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility',
+                       array('getAbsoluteExtensionPath', 'getExtensionVersion')
+               );
+               $fileHandlerMock->expects($this->any())
+                       ->method('getAbsoluteExtensionPath')
+                       ->will($this->returnValue($extensionRoot));
+               $fileHandlerMock->expects($this->any())
+                       ->method('getExtensionVersion')
+                       ->will($this->returnValue('0.0.0'));
+
+               // Add files and directories to extension:
+               touch($extensionRoot . 'emptyFile.txt');
+               file_put_contents($extensionRoot . 'notEmptyFile.txt', 'content');
+               touch($extensionRoot . '.hiddenFile');
+               mkdir($extensionRoot . 'emptyDir');
+               mkdir($extensionRoot . 'notEmptyDir');
+               touch($extensionRoot . 'notEmptyDir/file.txt');
+
+               // Create zip-file from extension
+               $filename = $fileHandlerMock->_call('createZipFileFromExtension', $extKey);
+
+               $expectedFilename = PATH_site . 'typo3temp/' . $extKey . '_0.0.0.zip';
+               $this->assertEquals($expectedFilename, $filename, 'Archive file name differs from expectation');
+
+               // File was created
+               $this->assertTrue(file_exists($filename), 'Zip file not created');
+               $this->resourcesToRemove[] = $filename;
+
+               // Read archive and check its contents
+               $archive = new \ZipArchive();
+               $this->assertTrue($archive->open($filename), 'Unable to open archive');
+               $this->assertEquals($archive->statName('emptyFile.txt')->size, 0, 'Empty file not in archive');
+               $this->assertEquals($archive->getFromName('notEmptyFile.txt'), 'content', 'Expected content not found');
+               $this->assertFalse($archive->statName('.hiddenFile'), 'Hidden file not in archive');
+               $this->assertTrue(is_array($archive->statName('emptyDir/')), 'Empty directory not in archive');
+               $this->assertTrue(is_array($archive->statName('notEmptyDir/')), 'Not empty directory not in archive');
+               $this->assertTrue(is_array($archive->statName('notEmptyDir/file.txt')), 'File within directory not in archive');
+
+               // Check that the archive has no additional content
+               $this->assertEquals($archive->numFiles, 5, 'Too many or too less files in archive');
+       }
 }
 
-?>
\ No newline at end of file
+?>