mirror of
https://github.com/Ne-Lexa/php-zip.git
synced 2025-08-11 01:34:36 +02:00
Added a new option for extracting unix symlinks.
Added new parameter to get the list of extracted files.
This commit is contained in:
88
tests/SymlinkTest.php
Normal file
88
tests/SymlinkTest.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace PhpZip\Tests;
|
||||
|
||||
use PhpZip\Constants\ZipOptions;
|
||||
use PhpZip\Util\FilesUtil;
|
||||
use PhpZip\ZipFile;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @small
|
||||
*/
|
||||
final class SymlinkTest extends ZipFileTest
|
||||
{
|
||||
/**
|
||||
* This method is called before the first test of this test class is run.
|
||||
*/
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
parent::setUpBeforeClass();
|
||||
|
||||
if (\DIRECTORY_SEPARATOR === '\\') {
|
||||
self::markTestSkipped('only linux test');
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideAllowSymlink
|
||||
*
|
||||
* @param bool $allowSymlink
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function testSymlink($allowSymlink)
|
||||
{
|
||||
if (!is_dir($this->outputDirname)) {
|
||||
self::assertTrue(mkdir($this->outputDirname, 0755, true));
|
||||
}
|
||||
|
||||
$contentsFile = random_bytes(100);
|
||||
$filePath = $this->outputDirname . '/file.bin';
|
||||
$symlinkPath = $this->outputDirname . '/symlink.bin';
|
||||
$symlinkTarget = basename($filePath);
|
||||
self::assertNotFalse(file_put_contents($filePath, $contentsFile));
|
||||
self::assertTrue(symlink($symlinkTarget, $symlinkPath));
|
||||
|
||||
$finder = (new Finder())->in($this->outputDirname);
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addFromFinder($finder);
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
FilesUtil::removeDir($this->outputDirname);
|
||||
self::assertFalse(is_dir($this->outputDirname));
|
||||
self::assertTrue(mkdir($this->outputDirname, 0755, true));
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
$zipFile->extractTo($this->outputDirname, null, [
|
||||
ZipOptions::EXTRACT_SYMLINKS => $allowSymlink,
|
||||
]);
|
||||
$zipFile->close();
|
||||
|
||||
$splFileInfo = new \SplFileInfo($symlinkPath);
|
||||
|
||||
if ($allowSymlink) {
|
||||
self::assertTrue($splFileInfo->isLink());
|
||||
self::assertSame($splFileInfo->getLinkTarget(), $symlinkTarget);
|
||||
} else {
|
||||
self::assertFalse($splFileInfo->isLink());
|
||||
self::assertStringEqualsFile($symlinkPath, $symlinkTarget);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Generator
|
||||
*/
|
||||
public function provideAllowSymlink()
|
||||
{
|
||||
yield 'allow' => [true];
|
||||
yield 'deny' => [false];
|
||||
}
|
||||
}
|
@@ -1009,16 +1009,16 @@ class ZipFileTest extends ZipTestCase
|
||||
'test1.txt' => random_bytes(255),
|
||||
'test2.txt' => random_bytes(255),
|
||||
'test/test 2/test3.txt' => random_bytes(255),
|
||||
'test empty/dir' => null,
|
||||
'test empty/dir/' => null,
|
||||
];
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
|
||||
foreach ($entries as $entryName => $value) {
|
||||
if ($value === null) {
|
||||
foreach ($entries as $entryName => $contents) {
|
||||
if ($contents === null) {
|
||||
$zipFile->addEmptyDir($entryName);
|
||||
} else {
|
||||
$zipFile->addFromString($entryName, $value);
|
||||
$zipFile->addFromString($entryName, $contents);
|
||||
}
|
||||
}
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
@@ -1027,19 +1027,28 @@ class ZipFileTest extends ZipTestCase
|
||||
static::assertTrue(mkdir($this->outputDirname, 0755, true));
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
$zipFile->extractTo($this->outputDirname);
|
||||
$zipFile->extractTo($this->outputDirname, null, [], $extractedEntries);
|
||||
|
||||
foreach ($entries as $entryName => $value) {
|
||||
foreach ($entries as $entryName => $contents) {
|
||||
$fullExtractedFilename = $this->outputDirname . \DIRECTORY_SEPARATOR . $entryName;
|
||||
|
||||
if ($value === null) {
|
||||
static::assertTrue(
|
||||
isset($extractedEntries[$fullExtractedFilename]),
|
||||
'No extract info for ' . $fullExtractedFilename
|
||||
);
|
||||
|
||||
if ($contents === null) {
|
||||
static::assertTrue(is_dir($fullExtractedFilename));
|
||||
static::assertTrue(FilesUtil::isEmptyDir($fullExtractedFilename));
|
||||
} else {
|
||||
static::assertTrue(is_file($fullExtractedFilename));
|
||||
$contents = file_get_contents($fullExtractedFilename);
|
||||
static::assertSame($contents, $value);
|
||||
static::assertSame($contents, $contents);
|
||||
}
|
||||
|
||||
/** @var ZipEntry $entry */
|
||||
$entry = $extractedEntries[$fullExtractedFilename];
|
||||
static::assertSame($entry->getName(), $entryName);
|
||||
}
|
||||
$zipFile->close();
|
||||
}
|
||||
@@ -2431,7 +2440,7 @@ class ZipFileTest extends ZipTestCase
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipAfterBeforeWrite = $zipFile->getEntry('file 1');
|
||||
|
||||
static::assertEquals($zipAfterBeforeWrite, $zipEntryBeforeWrite);
|
||||
static::assertSame($zipAfterBeforeWrite, $zipEntryBeforeWrite);
|
||||
|
||||
$zipFile->close();
|
||||
}
|
||||
|
Reference in New Issue
Block a user