function ZipTest::testOverwriteArchive

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Archiver/ZipTest.php \Drupal\KernelTests\Core\Archiver\ZipTest::testOverwriteArchive()

Tests that the Zip archiver is created and overwritten.

File

core/tests/Drupal/KernelTests/Core/Archiver/ZipTest.php, line 38

Class

ZipTest
@coversDefaultClass \Drupal\Core\Archiver\Zip[[api-linebreak]] @group zip

Namespace

Drupal\KernelTests\Core\Archiver

Code

public function testOverwriteArchive() : void {
  // Create an archive similarly to how it's done in ::testCreateArchive.
  $files = $this->getTestFiles('text');
  $textFile = current($files);
  $archiveFilename = $this->fileSystem
    ->realpath('public://' . $this->randomMachineName() . '.zip');
  $zip = new Zip($archiveFilename, [
    'flags' => \ZipArchive::CREATE,
  ]);
  $zip->add($this->fileSystem
    ->realPath($textFile->uri));
  $zip->getArchive()
    ->close();
  $this->assertArchiveContainsFile($archiveFilename, $this->fileSystem
    ->realPath($textFile->uri));
  // Overwrite the zip with just a new text file.
  $secondTextFile = next($files);
  $zip = new Zip($archiveFilename, [
    'flags' => \ZipArchive::OVERWRITE,
  ]);
  $zip->add($this->fileSystem
    ->realpath($secondTextFile->uri));
  $zip->getArchive()
    ->close();
  $this->assertArchiveNotContainsFile($archiveFilename, $this->fileSystem
    ->realPath($textFile->uri));
  $this->assertArchiveContainsFile($archiveFilename, $this->fileSystem
    ->realPath($secondTextFile->uri));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.