123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- use PHPUnit\Framework\Assert;
- use Psr\Http\Message\StreamInterface;
- require __DIR__ . '/../../vendor/autoload.php';
- trait Download {
-
- private $downloadedFile;
-
- public function cleanupDownloadedFile() {
- $this->downloadedFile = null;
- }
-
- public function userDownloadsZipFileForEntriesInFolder($user, $entries, $folder) {
- $folder = trim($folder, '/');
- $this->asAn($user);
- $this->sendingToDirectUrl('GET', "/remote.php/dav/files/$user/$folder?accept=zip&files=[" . $entries . ']');
- $this->theHTTPStatusCodeShouldBe('200');
- }
- private function getDownloadedFile() {
- $this->downloadedFile = '';
-
- $body = $this->response->getBody();
- while (!$body->eof()) {
- $this->downloadedFile .= $body->read(8192);
- }
- $body->close();
- }
-
- public function theDownloadedFileIsAZipFile() {
- $this->getDownloadedFile();
- Assert::assertTrue(
- strpos($this->downloadedFile, "\x50\x4B\x01\x02") !== false,
- 'File does not contain the central directory file header'
- );
- }
-
- public function theDownloadedZipFileIsAZip32File() {
- $this->theDownloadedFileIsAZipFile();
-
-
- Assert::assertTrue(
- strpos($this->downloadedFile, "\x50\x4B\x06\x06") === false,
- 'File contains the zip64 end of central dir signature'
- );
- }
-
- public function theDownloadedZipFileIsAZip64File() {
- $this->theDownloadedFileIsAZipFile();
-
-
- Assert::assertTrue(
- strpos($this->downloadedFile, "\x50\x4B\x06\x06") !== false,
- 'File does not contain the zip64 end of central dir signature'
- );
- }
-
- public function theDownloadedZipFileContainsAFileNamedWithTheContentsOfFromData($fileName, $sourceFileName, $user) {
- $fileHeaderRegExp = '/';
- $fileHeaderRegExp .= "\x50\x4B\x03\x04";
- $fileHeaderRegExp .= '.{22,22}';
- $fileHeaderRegExp .= preg_quote(pack('v', strlen($fileName)), '/');
- $fileHeaderRegExp .= '(.{2,2})';
- $fileHeaderRegExp .= preg_quote($fileName, '/');
- $fileHeaderRegExp .= '/s';
-
-
- Assert::assertEquals(
- 1, preg_match($fileHeaderRegExp, $this->downloadedFile, $matches),
- 'Local header for file did not appear once in zip file'
- );
- $extraFieldLength = unpack('vextraFieldLength', $matches[1])['extraFieldLength'];
- $expectedFileContents = file_get_contents($this->getDataDirectory() . "/$user/files" . $sourceFileName);
- $fileHeaderAndContentRegExp = '/';
- $fileHeaderAndContentRegExp .= "\x50\x4B\x03\x04";
- $fileHeaderAndContentRegExp .= '.{22,22}';
- $fileHeaderAndContentRegExp .= preg_quote(pack('v', strlen($fileName)), '/');
- $fileHeaderAndContentRegExp .= '.{2,2}';
- $fileHeaderAndContentRegExp .= preg_quote($fileName, '/');
- $fileHeaderAndContentRegExp .= '.{' . $extraFieldLength . ',' . $extraFieldLength . '}';
- $fileHeaderAndContentRegExp .= preg_quote($expectedFileContents, '/');
- $fileHeaderAndContentRegExp .= '/s';
-
-
- Assert::assertEquals(
- 1, preg_match($fileHeaderAndContentRegExp, $this->downloadedFile),
- 'Local header and contents for file did not appear once in zip file'
- );
- }
-
- public function theDownloadedZipFileContainsAFolderNamed($folderName) {
- $folderHeaderRegExp = '/';
- $folderHeaderRegExp .= "\x50\x4B\x03\x04";
- $folderHeaderRegExp .= '.{22,22}';
- $folderHeaderRegExp .= preg_quote(pack('v', strlen($folderName)), '/');
- $folderHeaderRegExp .= '.{2,2}';
- $folderHeaderRegExp .= preg_quote($folderName, '/');
- $folderHeaderRegExp .= '/s';
-
-
- Assert::assertEquals(
- 1, preg_match($folderHeaderRegExp, $this->downloadedFile),
- 'Local header for folder did not appear once in zip file'
- );
- }
-
- public function theDownloadedFileHasContentOfUserFile($sourceFilename, $user) {
- $this->getDownloadedFile();
- $expectedFileContents = file_get_contents($this->getDataDirectory() . "/$user/files" . $sourceFilename);
-
- Assert::assertEquals(
- 0, strcmp($expectedFileContents, $this->downloadedFile),
- 'Downloaded file content does not match local file content'
- );
- }
- }
|