12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097 |
- <?php
- use GuzzleHttp\Client as GClient;
- use GuzzleHttp\Message\ResponseInterface;
- use PHPUnit\Framework\Assert;
- use Sabre\DAV\Client as SClient;
- use Sabre\DAV\Xml\Property\ResourceType;
- require __DIR__ . '/../../vendor/autoload.php';
- trait WebDav {
- use Sharing;
-
- private $davPath = "remote.php/webdav";
-
- private $usingOldDavPath = true;
-
- private $response;
-
- private $storedETAG = null;
-
- private $storedFileID = null;
- private string $s3MultipartDestination;
- private string $uploadId;
-
- public function usingDavPath($davPath) {
- $this->davPath = $davPath;
- }
-
- public function usingOldDavPath() {
- $this->davPath = "remote.php/webdav";
- $this->usingOldDavPath = true;
- }
-
- public function usingNewDavPath() {
- $this->davPath = "remote.php/dav";
- $this->usingOldDavPath = false;
- }
- public function getDavFilesPath($user) {
- if ($this->usingOldDavPath === true) {
- return $this->davPath;
- } else {
- return $this->davPath . '/files/' . $user;
- }
- }
- public function makeDavRequest($user, $method, $path, $headers, $body = null, $type = "files") {
- if ($type === "files") {
- $fullUrl = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user) . "$path";
- } elseif ($type === "uploads") {
- $fullUrl = substr($this->baseUrl, 0, -4) . $this->davPath . "$path";
- } else {
- $fullUrl = substr($this->baseUrl, 0, -4) . $this->davPath . '/' . $type . "$path";
- }
- $client = new GClient();
- $options = [
- 'headers' => $headers,
- 'body' => $body
- ];
- if ($user === 'admin') {
- $options['auth'] = $this->adminUser;
- } else {
- $options['auth'] = [$user, $this->regularUser];
- }
- return $client->request($method, $fullUrl, $options);
- }
-
- public function userMovedFile($user, $entry, $fileSource, $fileDestination) {
- $fullUrl = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user);
- $headers['Destination'] = $fullUrl . $fileDestination;
- $this->response = $this->makeDavRequest($user, "MOVE", $fileSource, $headers);
- Assert::assertEquals(201, $this->response->getStatusCode());
- }
-
- public function userMovesFile($user, $entry, $fileSource, $fileDestination) {
- $fullUrl = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user);
- $headers['Destination'] = $fullUrl . $fileDestination;
- try {
- $this->response = $this->makeDavRequest($user, "MOVE", $fileSource, $headers);
- } catch (\GuzzleHttp\Exception\ClientException $e) {
- $this->response = $e->getResponse();
- }
- }
-
- public function userCopiesFileTo($user, $fileSource, $fileDestination) {
- $fullUrl = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user);
- $headers['Destination'] = $fullUrl . $fileDestination;
- try {
- $this->response = $this->makeDavRequest($user, 'COPY', $fileSource, $headers);
- } catch (\GuzzleHttp\Exception\ClientException $e) {
-
- $this->response = $e->getResponse();
- }
- }
-
- public function downloadFileWithRange($fileSource, $range) {
- $headers['Range'] = $range;
- $this->response = $this->makeDavRequest($this->currentUser, "GET", $fileSource, $headers);
- }
-
- public function downloadPublicFileWithRange($range) {
- $token = $this->lastShareData->data->token;
- $fullUrl = substr($this->baseUrl, 0, -4) . "public.php/webdav";
- $client = new GClient();
- $options = [];
- $options['auth'] = [$token, ""];
- $options['headers'] = [
- 'Range' => $range
- ];
- $this->response = $client->request("GET", $fullUrl, $options);
- }
-
- public function downloadPublicFileInsideAFolderWithRange($path, $range) {
- $token = $this->lastShareData->data->token;
- $fullUrl = substr($this->baseUrl, 0, -4) . "public.php/webdav" . "$path";
- $client = new GClient();
- $options = [
- 'headers' => [
- 'Range' => $range
- ]
- ];
- $options['auth'] = [$token, ""];
- $this->response = $client->request("GET", $fullUrl, $options);
- }
-
- public function downloadedContentShouldBe($content) {
- Assert::assertEquals($content, (string)$this->response->getBody());
- }
-
- public function checkPropForFile($file, $prefix, $prop, $value) {
- $elementList = $this->propfindFile($this->currentUser, $file, "<$prefix:$prop/>");
- $property = $elementList['/'.$this->getDavFilesPath($this->currentUser).$file][200]["{DAV:}$prop"];
- Assert::assertEquals($property, $value);
- }
-
- public function search(): void {
- $this->searchFile($this->currentUser);
- Assert::assertEquals(207, $this->response->getStatusCode());
- }
-
- public function downloadedContentWhenDownloadindShouldBe($fileSource, $range, $content) {
- $this->downloadFileWithRange($fileSource, $range);
- $this->downloadedContentShouldBe($content);
- }
-
- public function downloadingFile($fileName) {
- try {
- $this->response = $this->makeDavRequest($this->currentUser, 'GET', $fileName, []);
- } catch (\GuzzleHttp\Exception\ClientException $e) {
- $this->response = $e->getResponse();
- }
- }
-
- public function downloadedContentShouldStartWith($start) {
- if (strpos($this->response->getBody()->getContents(), $start) !== 0) {
- throw new \Exception(
- sprintf(
- "Expected '%s', got '%s'",
- $start,
- $this->response->getBody()->getContents()
- )
- );
- }
- }
-
- public function asGetsPropertiesOfFolderWith($user, $elementType, $path, $propertiesTable) {
- $properties = null;
- if ($propertiesTable instanceof \Behat\Gherkin\Node\TableNode) {
- foreach ($propertiesTable->getRows() as $row) {
- $properties[] = $row[0];
- }
- }
- $this->response = $this->listFolder($user, $path, 0, $properties);
- }
-
- public function asTheFileOrFolderDoesNotExist($user, $entry, $path) {
- $client = $this->getSabreClient($user);
- $response = $client->request('HEAD', $this->makeSabrePath($user, $path));
- if ($response['statusCode'] !== 404) {
- throw new \Exception($entry . ' "' . $path . '" expected to not exist (status code ' . $response['statusCode'] . ', expected 404)');
- }
- return $response;
- }
-
- public function asTheFileOrFolderExists($user, $entry, $path) {
- $this->response = $this->listFolder($user, $path, 0);
- }
-
- public function theSingleResponseShouldContainAPropertyWithValue($key, $expectedValue) {
- $keys = $this->response;
- if (!array_key_exists($key, $keys)) {
- throw new \Exception("Cannot find property \"$key\" with \"$expectedValue\"");
- }
- $value = $keys[$key];
- if ($value instanceof ResourceType) {
- $value = $value->getValue();
- if (empty($value)) {
- $value = '';
- } else {
- $value = $value[0];
- }
- }
- if ($value != $expectedValue) {
- throw new \Exception("Property \"$key\" found with value \"$value\", expected \"$expectedValue\"");
- }
- }
-
- public function theResponseShouldContainAShareTypesPropertyWith($table) {
- $keys = $this->response;
- if (!array_key_exists('{http://owncloud.org/ns}share-types', $keys)) {
- throw new \Exception("Cannot find property \"{http://owncloud.org/ns}share-types\"");
- }
- $foundTypes = [];
- $data = $keys['{http://owncloud.org/ns}share-types'];
- foreach ($data as $item) {
- if ($item['name'] !== '{http://owncloud.org/ns}share-type') {
- throw new \Exception('Invalid property found: "' . $item['name'] . '"');
- }
- $foundTypes[] = $item['value'];
- }
- foreach ($table->getRows() as $row) {
- $key = array_search($row[0], $foundTypes);
- if ($key === false) {
- throw new \Exception('Expected type ' . $row[0] . ' not found');
- }
- unset($foundTypes[$key]);
- }
- if ($foundTypes !== []) {
- throw new \Exception('Found more share types then specified: ' . $foundTypes);
- }
- }
-
- public function theResponseShouldContainAnEmptyProperty($property) {
- $properties = $this->response;
- if (!array_key_exists($property, $properties)) {
- throw new \Exception("Cannot find property \"$property\"");
- }
- if ($properties[$property] !== null) {
- throw new \Exception("Property \"$property\" is not empty");
- }
- }
-
- public function listFolder($user, $path, $folderDepth, $properties = null) {
- $client = $this->getSabreClient($user);
- if (!$properties) {
- $properties = [
- '{DAV:}getetag'
- ];
- }
- $response = $client->propfind($this->makeSabrePath($user, $path), $properties, $folderDepth);
- return $response;
- }
-
- public function propfindFile(string $user, string $path, string $properties = '') {
- $client = $this->getSabreClient($user);
- $body = '<?xml version="1.0" encoding="utf-8" ?>
- <d:propfind xmlns:d="DAV:"
- xmlns:oc="http://owncloud.org/ns"
- xmlns:nc="http://nextcloud.org/ns"
- xmlns:ocs="http://open-collaboration-services.org/ns">
- <d:prop>
- ' . $properties . '
- </d:prop>
- </d:propfind>';
- $response = $client->request('PROPFIND', $this->makeSabrePath($user, $path), $body);
- $parsedResponse = $client->parseMultistatus($response['body']);
- return $parsedResponse;
- }
-
- public function searchFile(string $user, ?string $properties = null, ?string $scope = null, ?string $condition = null) {
- $client = $this->getSabreClient($user);
- if ($properties === null) {
- $properties = '<oc:fileid /> <d:getlastmodified /> <d:getetag /> <d:getcontenttype /> <d:getcontentlength /> <nc:has-preview /> <oc:favorite /> <d:resourcetype />';
- }
- if ($condition === null) {
- $condition = '<d:and>
- <d:or>
- <d:eq>
- <d:prop>
- <d:getcontenttype/>
- </d:prop>
- <d:literal>image/png</d:literal>
- </d:eq>
-
- <d:eq>
- <d:prop>
- <d:getcontenttype/>
- </d:prop>
- <d:literal>image/jpeg</d:literal>
- </d:eq>
-
- <d:eq>
- <d:prop>
- <d:getcontenttype/>
- </d:prop>
- <d:literal>image/heic</d:literal>
- </d:eq>
-
- <d:eq>
- <d:prop>
- <d:getcontenttype/>
- </d:prop>
- <d:literal>video/mp4</d:literal>
- </d:eq>
-
- <d:eq>
- <d:prop>
- <d:getcontenttype/>
- </d:prop>
- <d:literal>video/quicktime</d:literal>
- </d:eq>
- </d:or>
- <d:eq>
- <d:prop>
- <oc:owner-id/>
- </d:prop>
- <d:literal>' . $user . '</d:literal>
- </d:eq>
- </d:and>';
- }
- if ($scope === null) {
- $scope = '<d:href>/files/' . $user . '</d:href><d:depth>infinity</d:depth>';
- }
- $body = '<?xml version="1.0" encoding="UTF-8"?>
- <d:searchrequest xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns" xmlns:ns="https://github.com/icewind1991/SearchDAV/ns" xmlns:ocs="http://open-collaboration-services.org/ns">
- <d:basicsearch>
- <d:select>
- <d:prop>' . $properties . '</d:prop>
- </d:select>
- <d:from><d:scope>' . $scope . '</d:scope></d:from>
- <d:where>' . $condition . '</d:where>
- <d:orderby>
- <d:order>
- <d:prop><d:getlastmodified/></d:prop>
- <d:descending/>
- </d:order>
- </d:orderby>
- <d:limit>
- <d:nresults>35</d:nresults>
- <ns:firstresult>0</ns:firstresult>
- </d:limit>
- </d:basicsearch>
- </d:searchrequest>';
- try {
- $this->response = $this->makeDavRequest($user, "SEARCH", '', [
- 'Content-Type' => 'text/xml'
- ], $body, '');
- var_dump((string)$this->response->getBody());
- } catch (\GuzzleHttp\Exception\ServerException $e) {
-
- $this->response = $e->getResponse();
- } catch (\GuzzleHttp\Exception\ClientException $e) {
-
- $this->response = $e->getResponse();
- }
- }
-
- public function reportFolder($user, $path, $properties, $filterRules) {
- $client = $this->getSabreClient($user);
- $body = '<?xml version="1.0" encoding="utf-8" ?>
- <oc:filter-files xmlns:a="DAV:" xmlns:oc="http://owncloud.org/ns" >
- <a:prop>
- ' . $properties . '
- </a:prop>
- <oc:filter-rules>
- ' . $filterRules . '
- </oc:filter-rules>
- </oc:filter-files>';
- $response = $client->request('REPORT', $this->makeSabrePath($user, $path), $body);
- $parsedResponse = $client->parseMultistatus($response['body']);
- return $parsedResponse;
- }
- public function makeSabrePath($user, $path, $type = 'files') {
- if ($type === 'files') {
- return $this->encodePath($this->getDavFilesPath($user) . $path);
- } else {
- return $this->encodePath($this->davPath . '/' . $type . '/' . $user . '/' . $path);
- }
- }
- public function getSabreClient($user) {
- $fullUrl = substr($this->baseUrl, 0, -4);
- $settings = [
- 'baseUri' => $fullUrl,
- 'userName' => $user,
- ];
- if ($user === 'admin') {
- $settings['password'] = $this->adminUser[1];
- } else {
- $settings['password'] = $this->regularUser;
- }
- $settings['authType'] = SClient::AUTH_BASIC;
- return new SClient($settings);
- }
-
- public function checkElementList($user, $expectedElements) {
- $elementList = $this->listFolder($user, '/', 3);
- if ($expectedElements instanceof \Behat\Gherkin\Node\TableNode) {
- $elementRows = $expectedElements->getRows();
- $elementsSimplified = $this->simplifyArray($elementRows);
- foreach ($elementsSimplified as $expectedElement) {
- $webdavPath = "/" . $this->getDavFilesPath($user) . $expectedElement;
- if (!array_key_exists($webdavPath, $elementList)) {
- Assert::fail("$webdavPath" . " is not in propfind answer");
- }
- }
- }
- }
-
- public function userUploadsAFileTo($user, $source, $destination) {
- $file = \GuzzleHttp\Psr7\Utils::streamFor(fopen($source, 'r'));
- try {
- $this->response = $this->makeDavRequest($user, "PUT", $destination, [], $file);
- } catch (\GuzzleHttp\Exception\ServerException $e) {
-
- $this->response = $e->getResponse();
- } catch (\GuzzleHttp\Exception\ClientException $e) {
-
- $this->response = $e->getResponse();
- }
- }
-
- public function userAddsAFileTo($user, $bytes, $destination) {
- $filename = "filespecificSize.txt";
- $this->createFileSpecificSize($filename, $bytes);
- Assert::assertEquals(1, file_exists("work/$filename"));
- $this->userUploadsAFileTo($user, "work/$filename", $destination);
- $this->removeFile("work/", $filename);
- $expectedElements = new \Behat\Gherkin\Node\TableNode([["$destination"]]);
- $this->checkElementList($user, $expectedElements);
- }
-
- public function userUploadsAFileWithContentTo($user, $content, $destination) {
- $file = \GuzzleHttp\Psr7\Utils::streamFor($content);
- try {
- $this->response = $this->makeDavRequest($user, "PUT", $destination, [], $file);
- } catch (\GuzzleHttp\Exception\ServerException $e) {
-
- $this->response = $e->getResponse();
- } catch (\GuzzleHttp\Exception\ClientException $e) {
-
- $this->response = $e->getResponse();
- }
- }
-
- public function userDeletesFile($user, $type, $file) {
- try {
- $this->response = $this->makeDavRequest($user, 'DELETE', $file, []);
- } catch (\GuzzleHttp\Exception\ServerException $e) {
-
- $this->response = $e->getResponse();
- } catch (\GuzzleHttp\Exception\ClientException $e) {
-
- $this->response = $e->getResponse();
- }
- }
-
- public function userCreatedAFolder($user, $destination) {
- try {
- $destination = '/' . ltrim($destination, '/');
- $this->response = $this->makeDavRequest($user, "MKCOL", $destination, []);
- } catch (\GuzzleHttp\Exception\ServerException $e) {
-
- $this->response = $e->getResponse();
- } catch (\GuzzleHttp\Exception\ClientException $e) {
-
- $this->response = $e->getResponse();
- }
- }
-
- public function userUploadsChunkFileOfWithToWithChecksum($user, $num, $total, $data, $destination) {
- $num -= 1;
- $data = \GuzzleHttp\Psr7\Utils::streamFor($data);
- $file = $destination . '-chunking-42-' . $total . '-' . $num;
- $this->makeDavRequest($user, 'PUT', $file, ['OC-Chunked' => '1'], $data, "uploads");
- }
-
- public function userUploadsBulkedFiles($user, $name1, $content1, $name2, $content2, $name3, $content3) {
- $boundary = "boundary_azertyuiop";
- $body = "";
- $body .= '--'.$boundary."\r\n";
- $body .= "X-File-Path: ".$name1."\r\n";
- $body .= "X-File-MD5: f6a6263167c92de8644ac998b3c4e4d1\r\n";
- $body .= "X-OC-Mtime: 1111111111\r\n";
- $body .= "Content-Length: ".strlen($content1)."\r\n";
- $body .= "\r\n";
- $body .= $content1."\r\n";
- $body .= '--'.$boundary."\r\n";
- $body .= "X-File-Path: ".$name2."\r\n";
- $body .= "X-File-MD5: 87c7d4068be07d390a1fffd21bf1e944\r\n";
- $body .= "X-OC-Mtime: 2222222222\r\n";
- $body .= "Content-Length: ".strlen($content2)."\r\n";
- $body .= "\r\n";
- $body .= $content2."\r\n";
- $body .= '--'.$boundary."\r\n";
- $body .= "X-File-Path: ".$name3."\r\n";
- $body .= "X-File-MD5: e86a1cf0678099986a901c79086f5617\r\n";
- $body .= "X-File-Mtime: 3333333333\r\n";
- $body .= "Content-Length: ".strlen($content3)."\r\n";
- $body .= "\r\n";
- $body .= $content3."\r\n";
- $body .= '--'.$boundary."--\r\n";
- $stream = fopen('php://temp', 'r+');
- fwrite($stream, $body);
- rewind($stream);
- $client = new GClient();
- $options = [
- 'auth' => [$user, $this->regularUser],
- 'headers' => [
- 'Content-Type' => 'multipart/related; boundary='.$boundary,
- 'Content-Length' => (string)strlen($body),
- ],
- 'body' => $body
- ];
- return $client->request("POST", substr($this->baseUrl, 0, -4) . "remote.php/dav/bulk", $options);
- }
-
- public function userCreatesANewChunkingUploadWithId($user, $id) {
- $this->parts = [];
- $destination = '/uploads/' . $user . '/' . $id;
- $this->makeDavRequest($user, 'MKCOL', $destination, [], null, "uploads");
- }
-
- public function userUploadsNewChunkFileOfWithToId($user, $num, $data, $id) {
- $data = \GuzzleHttp\Psr7\Utils::streamFor($data);
- $destination = '/uploads/' . $user . '/' . $id . '/' . $num;
- $this->makeDavRequest($user, 'PUT', $destination, [], $data, "uploads");
- }
-
- public function userMovesNewChunkFileWithIdToMychunkedfile($user, $id, $dest) {
- $source = '/uploads/' . $user . '/' . $id . '/.file';
- $destination = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user) . $dest;
- $this->makeDavRequest($user, 'MOVE', $source, [
- 'Destination' => $destination
- ], null, "uploads");
- }
-
- public function userMovesNewChunkFileWithIdToMychunkedfileWithSize($user, $id, $dest, $size) {
- $source = '/uploads/' . $user . '/' . $id . '/.file';
- $destination = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user) . $dest;
- try {
- $this->response = $this->makeDavRequest($user, 'MOVE', $source, [
- 'Destination' => $destination,
- 'OC-Total-Length' => $size
- ], null, "uploads");
- } catch (\GuzzleHttp\Exception\BadResponseException $ex) {
- $this->response = $ex->getResponse();
- }
- }
-
- public function userCreatesANewChunkingv2UploadWithIdAndDestination($user, $id, $targetDestination) {
- $this->s3MultipartDestination = $this->getTargetDestination($user, $targetDestination);
- $this->newUploadId();
- $destination = '/uploads/' . $user . '/' . $this->getUploadId($id);
- $this->response = $this->makeDavRequest($user, 'MKCOL', $destination, [
- 'Destination' => $this->s3MultipartDestination,
- ], null, "uploads");
- }
-
- public function userUploadsNewChunkv2FileToIdAndDestination($user, $num, $id) {
- $data = \GuzzleHttp\Psr7\Utils::streamFor(fopen('/tmp/part-upload-' . $num, 'r'));
- $destination = '/uploads/' . $user . '/' . $this->getUploadId($id) . '/' . $num;
- $this->response = $this->makeDavRequest($user, 'PUT', $destination, [
- 'Destination' => $this->s3MultipartDestination
- ], $data, "uploads");
- }
-
- public function userMovesNewChunkv2FileWithIdToMychunkedfileAndDestination($user, $id) {
- $source = '/uploads/' . $user . '/' . $this->getUploadId($id) . '/.file';
- try {
- $this->response = $this->makeDavRequest($user, 'MOVE', $source, [
- 'Destination' => $this->s3MultipartDestination,
- ], null, "uploads");
- } catch (\GuzzleHttp\Exception\ServerException $e) {
-
- $this->response = $e->getResponse();
- } catch (\GuzzleHttp\Exception\ClientException $e) {
-
- $this->response = $e->getResponse();
- }
- }
- private function getTargetDestination(string $user, string $destination): string {
- return substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user) . $destination;
- }
- private function getUploadId(string $id): string {
- return $id . '-' . $this->uploadId;
- }
- private function newUploadId() {
- $this->uploadId = (string)time();
- }
-
- public function downloadingFileAs($fileName, $user) {
- try {
- $this->response = $this->makeDavRequest($user, 'GET', $fileName, []);
- } catch (\GuzzleHttp\Exception\ServerException $e) {
-
- $this->response = $e->getResponse();
- } catch (\GuzzleHttp\Exception\ClientException $e) {
-
- $this->response = $e->getResponse();
- }
- }
-
- private function encodePath($path) {
-
- return str_replace('%2F', '/', rawurlencode($path));
- }
-
- public function userFavoritesElement($user, $path) {
- $this->response = $this->changeFavStateOfAnElement($user, $path, 1, 0, null);
- }
-
- public function userUnfavoritesElement($user, $path) {
- $this->response = $this->changeFavStateOfAnElement($user, $path, 0, 0, null);
- }
-
- public function changeFavStateOfAnElement($user, $path, $favOrUnfav, $folderDepth, $properties = null) {
- $fullUrl = substr($this->baseUrl, 0, -4);
- $settings = [
- 'baseUri' => $fullUrl,
- 'userName' => $user,
- ];
- if ($user === 'admin') {
- $settings['password'] = $this->adminUser[1];
- } else {
- $settings['password'] = $this->regularUser;
- }
- $settings['authType'] = SClient::AUTH_BASIC;
- $client = new SClient($settings);
- if (!$properties) {
- $properties = [
- '{http://owncloud.org/ns}favorite' => $favOrUnfav
- ];
- }
- $response = $client->proppatch($this->getDavFilesPath($user) . $path, $properties, $folderDepth);
- return $response;
- }
-
- public function userStoresEtagOfElement($user, $path) {
- $propertiesTable = new \Behat\Gherkin\Node\TableNode([['{DAV:}getetag']]);
- $this->asGetsPropertiesOfFolderWith($user, 'entry', $path, $propertiesTable);
- $pathETAG[$path] = $this->response['{DAV:}getetag'];
- $this->storedETAG[$user] = $pathETAG;
- }
-
- public function checkIfETAGHasNotChanged($path, $user) {
- $propertiesTable = new \Behat\Gherkin\Node\TableNode([['{DAV:}getetag']]);
- $this->asGetsPropertiesOfFolderWith($user, 'entry', $path, $propertiesTable);
- Assert::assertEquals($this->response['{DAV:}getetag'], $this->storedETAG[$user][$path]);
- }
-
- public function checkIfETAGHasChanged($path, $user) {
- $propertiesTable = new \Behat\Gherkin\Node\TableNode([['{DAV:}getetag']]);
- $this->asGetsPropertiesOfFolderWith($user, 'entry', $path, $propertiesTable);
- Assert::assertNotEquals($this->response['{DAV:}getetag'], $this->storedETAG[$user][$path]);
- }
-
- public function connectingToDavEndpoint() {
- try {
- $this->response = $this->makeDavRequest(null, 'PROPFIND', '', []);
- } catch (\GuzzleHttp\Exception\ClientException $e) {
- $this->response = $e->getResponse();
- }
- }
-
- public function thereAreNoDuplicateHeaders() {
- $headers = $this->response->getHeaders();
- foreach ($headers as $headerName => $headerValues) {
-
- if (count($headerValues) > 1 && count(array_unique($headerValues)) < count($headerValues)) {
- throw new \Exception('Duplicate header found: ' . $headerName);
- }
- }
- }
-
- public function checkFavoritedElements($user, $folder, $expectedElements) {
- $elementList = $this->reportFolder($user,
- $folder,
- '<oc:favorite/>',
- '<oc:favorite>1</oc:favorite>');
- if ($expectedElements instanceof \Behat\Gherkin\Node\TableNode) {
- $elementRows = $expectedElements->getRows();
- $elementsSimplified = $this->simplifyArray($elementRows);
- foreach ($elementsSimplified as $expectedElement) {
- $webdavPath = "/" . $this->getDavFilesPath($user) . $expectedElement;
- if (!array_key_exists($webdavPath, $elementList)) {
- Assert::fail("$webdavPath" . " is not in report answer");
- }
- }
- }
- }
-
- public function userDeletesEverythingInFolder($user, $folder) {
- $elementList = $this->listFolder($user, $folder, 1);
- $elementListKeys = array_keys($elementList);
- array_shift($elementListKeys);
- $davPrefix = "/" . $this->getDavFilesPath($user);
- foreach ($elementListKeys as $element) {
- if (substr($element, 0, strlen($davPrefix)) == $davPrefix) {
- $element = substr($element, strlen($davPrefix));
- }
- $this->userDeletesFile($user, "element", $element);
- }
- }
-
- private function getFileIdForPath($user, $path) {
- $propertiesTable = new \Behat\Gherkin\Node\TableNode([["{http://owncloud.org/ns}fileid"]]);
- $this->asGetsPropertiesOfFolderWith($user, 'file', $path, $propertiesTable);
- return (int)$this->response['{http://owncloud.org/ns}fileid'];
- }
-
- public function userStoresFileIdForPath($user, $path) {
- $this->storedFileID = $this->getFileIdForPath($user, $path);
- }
-
- public function userChecksFileIdForPath($user, $path) {
- $currentFileID = $this->getFileIdForPath($user, $path);
- Assert::assertEquals($currentFileID, $this->storedFileID);
- }
-
- public function userCreatesAFileLocallyWithChunks($arg1, $chunks) {
- $this->parts = [];
- for ($i = 1;$i <= (int)$chunks;$i++) {
- $randomletter = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 1);
- file_put_contents('/tmp/part-upload-' . $i, str_repeat($randomletter, 5 * 1024 * 1024));
- $this->parts[] = '/tmp/part-upload-' . $i;
- }
- }
-
- public function userCreatesAChunk($user, $id, $size) {
- $randomletter = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 1);
- file_put_contents('/tmp/part-upload-' . $id, str_repeat($randomletter, (int)$size * 1024 * 1024));
- $this->parts[] = '/tmp/part-upload-' . $id;
- }
-
- public function downloadedContentShouldBeTheCreatedFile() {
- $content = '';
- sort($this->parts);
- foreach ($this->parts as $part) {
- $content .= file_get_contents($part);
- }
- Assert::assertEquals($content, (string)$this->response->getBody());
- }
-
- public function theSmultipartUploadWasSuccessful($status) {
- Assert::assertEquals((int)$status, $this->response->getStatusCode());
- }
-
- public function theUploadShouldFailOnObjectStorage() {
- $descriptor = [
- 0 => ['pipe', 'r'],
- 1 => ['pipe', 'w'],
- 2 => ['pipe', 'w'],
- ];
- $process = proc_open('php occ config:system:get objectstore --no-ansi', $descriptor, $pipes, '../../');
- $lastCode = proc_close($process);
- if ($lastCode === 0) {
- $this->theHTTPStatusCodeShouldBe(500);
- }
- }
- }
|