2022-12-21 17:22:22 +05:45
<? php declare ( strict_types = 1 );
/**
* @author Sergio Bertolin <sbertolin@owncloud.com>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
use Behat\Gherkin\Node\PyStringNode ;
use Behat\Gherkin\Node\TableNode ;
use GuzzleHttp\Exception\BadResponseException ;
use GuzzleHttp\Exception\GuzzleException ;
use PHPUnit\Framework\Assert ;
use Psr\Http\Message\ResponseInterface ;
use GuzzleHttp\Stream\StreamInterface ;
2024-06-07 15:10:59 +05:45
use TestHelpers\OcisHelper ;
2022-12-21 17:22:22 +05:45
use TestHelpers\UploadHelper ;
use TestHelpers\WebDavHelper ;
use TestHelpers\HttpRequestHelper ;
use TestHelpers\Asserts\WebDav as WebDavAssert ;
2023-10-13 11:51:08 +05:45
use TestHelpers\GraphHelper ;
2022-12-21 17:22:22 +05:45
/**
* WebDav functions
*/
trait WebDav {
2024-09-17 17:11:20 +05:45
// defaults to the new DAV path
// maybe we want to make spaces the default in the future
private int $currentDAVPath = WebDavHelper :: DAV_VERSION_NEW ;
2022-12-21 17:22:22 +05:45
/**
* @var ResponseInterface[]
*/
2023-05-02 17:25:56 +05:45
private array $uploadResponses ;
2022-12-21 17:22:22 +05:45
/**
2023-05-02 17:25:56 +05:45
* @var int|string|null
2022-12-21 17:22:22 +05:45
*/
private $storedFileID = null ;
2023-05-02 17:25:56 +05:45
private ? int $lastUploadDeleteTime = null ;
2022-12-21 17:22:22 +05:45
/**
* response content parsed from XML to an array
*/
2023-05-02 17:25:56 +05:45
private array $responseXml = [];
2022-12-21 17:22:22 +05:45
/**
* add resource created by admin in an array
* This array is used while cleaning up the resource created by admin during test run
* As of now it tracks only for (files|folder) creation
2024-04-26 15:27:00 +05:45
* This can be expanded and modified to track other actions like (upload, deleted.)
2022-12-21 17:22:22 +05:45
*/
2023-05-02 17:25:56 +05:45
private array $adminResources = [];
2022-12-21 17:22:22 +05:45
/**
* response content parsed into a SimpleXMLElement
*/
2023-09-06 09:22:38 +05:45
private ? SimpleXMLElement $responseXmlObject = null ;
2022-12-21 17:22:22 +05:45
2023-05-02 17:25:56 +05:45
private int $httpRequestTimeout = 0 ;
2022-12-21 17:22:22 +05:45
2023-05-02 17:25:56 +05:45
private ? int $chunkingToUse = null ;
2022-12-21 17:22:22 +05:45
/**
* The ability to do requests with depth infinity is disabled by default.
* This remembers when the setting dav.propfind.depth_infinity has been
* enabled, so that test code can make use of it as appropriate.
*/
2023-05-02 17:25:56 +05:45
private bool $davPropfindDepthInfinityEnabled = false ;
2022-12-21 17:22:22 +05:45
/**
* @return void
*/
public function davPropfindDepthInfinityEnabled () : void {
$this -> davPropfindDepthInfinityEnabled = true ;
}
/**
* @return void
*/
public function davPropfindDepthInfinityDisabled () : void {
$this -> davPropfindDepthInfinityEnabled = false ;
}
/**
* @return bool
*/
public function davPropfindDepthInfinityIsEnabled () : bool {
return $this -> davPropfindDepthInfinityEnabled ;
}
/**
* @param int $lastUploadDeleteTime
*
* @return void
*/
public function setLastUploadDeleteTime ( int $lastUploadDeleteTime ) : void {
$this -> lastUploadDeleteTime = $lastUploadDeleteTime ;
}
/**
* @return number
*/
public function getLastUploadDeleteTime () : int {
return $this -> lastUploadDeleteTime ;
}
/**
* @param SimpleXMLElement $responseXmlObject
*
* @return void
*/
public function setResponseXmlObject ( SimpleXMLElement $responseXmlObject ) : void {
$this -> responseXmlObject = $responseXmlObject ;
}
/**
* @return void
*/
public function clearResponseXmlObject () : void {
$this -> responseXmlObject = null ;
}
2024-05-30 17:07:09 +05:45
/**
* @param string $fileID
*
* @return void
*/
public function setStoredFileID ( string $fileID ) : void {
$this -> storedFileID = $fileID ;
}
/**
* @return string
*/
public function getStoredFileID () : string {
return $this -> storedFileID ;
}
2022-12-21 17:22:22 +05:45
/**
2023-10-05 09:55:11 +05:45
* @param SimpleXMLElement|null $xmlObject
2022-12-21 17:22:22 +05:45
*
* @return string the etag or an empty string if the getetag property does not exist
*/
2023-10-05 09:55:11 +05:45
public function getEtagFromResponseXmlObject ( ? SimpleXMLElement $xmlObject = null ) : string {
$xmlObject = $xmlObject ?? $this -> getResponseXml ();
2022-12-21 17:22:22 +05:45
$xmlPart = $xmlObject -> xpath ( "//d:prop/d:getetag" );
if ( ! \is_array ( $xmlPart ) || ( \count ( $xmlPart ) === 0 )) {
return '' ;
}
return $xmlPart [ 0 ] -> __toString ();
}
/**
*
2023-10-05 09:55:11 +05:45
* @param string $eTag
2022-12-21 17:22:22 +05:45
*
* @return boolean
*/
2023-10-05 09:55:11 +05:45
public function isEtagValid ( string $eTag ) : bool {
2024-04-26 15:27:00 +05:45
if ( \preg_match ( "/^ \" [a-f0-9:.]{1,32} \" $/" , $eTag )
2022-12-21 17:22:22 +05:45
) {
return true ;
} else {
return false ;
}
}
/**
* @param array $responseXml
*
* @return void
*/
public function setResponseXml ( array $responseXml ) : void {
$this -> responseXml = $responseXml ;
}
/**
2024-09-17 17:11:20 +05:45
* @Given /^using (old|new|spaces) DAV path$/
2022-12-21 17:22:22 +05:45
*
* @param string $davChoice
*
* @return void
*/
2024-09-17 17:11:20 +05:45
public function usingOldOrNewOrSpacesDavPath ( string $davChoice ) : void {
switch ( $davChoice ) {
case 'old' :
$this -> currentDAVPath = WebDavHelper :: DAV_VERSION_OLD ;
break ;
case 'new' :
$this -> currentDAVPath = WebDavHelper :: DAV_VERSION_NEW ;
break ;
case 'spaces' :
$this -> currentDAVPath = WebDavHelper :: DAV_VERSION_SPACES ;
break ;
default :
throw new Exception ( "Invalid DAV path: $davChoice " );
break ;
2022-12-21 17:22:22 +05:45
}
}
/**
* gives the DAV path of a file including the subfolder of the webserver
* e.g. when the server runs in `http://localhost/owncloud/`
2024-09-17 17:11:20 +05:45
* this function will return `owncloud/webdav/prueba.txt`
2022-12-21 17:22:22 +05:45
*
* @param string $user
2024-09-18 11:26:20 +05:45
* @param string $spaceId
2022-12-21 17:22:22 +05:45
*
* @return string
* @throws GuzzleException
*/
2024-09-18 11:26:20 +05:45
public function getFullDavFilesPath ( string $user , ? string $spaceId = null ) : string {
if ( $spaceId === null && $this -> getDavPathVersion () === WebDavHelper :: DAV_VERSION_SPACES ) {
$spaceId = WebDavHelper :: getPersonalSpaceIdForUser (
2022-12-21 17:22:22 +05:45
$this -> getBaseUrl (),
$user ,
$this -> getPasswordForUser ( $user ),
$this -> getStepLineRef ()
);
}
2024-09-17 17:11:20 +05:45
$davPathVersion = $this -> getDavPathVersion ();
2024-10-21 12:24:50 +05:45
$suffixPath = $user ;
2024-09-17 17:11:20 +05:45
if ( $davPathVersion === WebDavHelper :: DAV_VERSION_SPACES ) {
2024-10-21 12:24:50 +05:45
$suffixPath = $spaceId ;
2024-09-17 17:11:20 +05:45
}
2024-10-21 12:24:50 +05:45
$davPath = WebDavHelper :: getDavPath ( $davPathVersion , $suffixPath );
$path = " { $this -> getBasePath () } / $davPath " ;
2022-12-21 17:22:22 +05:45
$path = WebDavHelper :: sanitizeUrl ( $path );
return \ltrim ( $path , "/" );
}
/**
* @param string $token
*
* @return string
*/
2024-09-17 17:11:20 +05:45
public function getPublicLinkDavPath ( string $token ) : string {
$davPath = WebDavHelper :: getDavPath ( $this -> getDavPathVersion (), $token , "public-files" );
2024-10-21 12:24:50 +05:45
$path = " { $this -> getBasePath () } / $davPath " ;
2022-12-21 17:22:22 +05:45
$path = WebDavHelper :: sanitizeUrl ( $path );
return \ltrim ( $path , "/" );
}
/**
* Select a suitable DAV path version number.
* Some endpoints have only existed since a certain point in time, so for
* those make sure to return a DAV path version that works for that endpoint.
* Otherwise return the currently selected DAV path version.
*
* @param string|null $for the category of endpoint that the DAV path will be used for
*
* @return int DAV path version (1, 2 or 3) selected, or appropriate for the endpoint
*/
public function getDavPathVersion ( ? string $for = null ) :? int {
2024-09-17 17:11:20 +05:45
if ( \in_array ( $for , [ 'systemtags' , 'file_versions' ])) {
// 'systemtags' only exists since new DAV
// 'file_versions' only exists since new DAV
2022-12-21 17:22:22 +05:45
return WebDavHelper :: DAV_VERSION_NEW ;
}
2024-09-17 17:11:20 +05:45
return $this -> currentDAVPath ;
2022-12-21 17:22:22 +05:45
}
/**
* @param string|null $user
* @param string|null $method
* @param string|null $path
* @param array|null $headers
* @param StreamInterface|null $body
2024-09-18 11:26:20 +05:45
* @param string|null $spaceId
2022-12-21 17:22:22 +05:45
* @param string|null $type
* @param string|null $davPathVersion
* @param bool $stream Set to true to stream a response rather
* than download it all up-front.
* @param string|null $password
* @param array|null $urlParameter
* @param string|null $doDavRequestAsUser
2023-07-13 12:44:51 +05:45
* @param bool|null $isGivenStep
2022-12-21 17:22:22 +05:45
*
* @return ResponseInterface
2023-07-13 12:44:51 +05:45
*
* @throws GuzzleException
* @throws JsonException
2022-12-21 17:22:22 +05:45
*/
public function makeDavRequest (
? string $user ,
? string $method ,
? string $path ,
? array $headers ,
$body = null ,
2024-09-18 11:26:20 +05:45
? string $spaceId = null ,
2022-12-21 17:22:22 +05:45
? string $type = "files" ,
2024-09-17 17:11:20 +05:45
? int $davPathVersion = null ,
2022-12-21 17:22:22 +05:45
bool $stream = false ,
? string $password = null ,
? array $urlParameter = [],
2023-07-13 12:44:51 +05:45
? string $doDavRequestAsUser = null ,
? bool $isGivenStep = false
2022-12-21 17:22:22 +05:45
) : ResponseInterface {
$user = $this -> getActualUsername ( $user );
if ( $davPathVersion === null ) {
$davPathVersion = $this -> getDavPathVersion ();
} else {
$davPathVersion = ( int ) $davPathVersion ;
}
if ( $password === null ) {
$password = $this -> getPasswordForUser ( $user );
}
return WebDavHelper :: makeDavRequest (
$this -> getBaseUrl (),
$user ,
$password ,
$method ,
$path ,
$headers ,
2024-09-18 11:26:20 +05:45
$spaceId ,
2022-12-21 17:22:22 +05:45
$this -> getStepLineRef (),
$body ,
$davPathVersion ,
$type ,
null ,
"basic" ,
$stream ,
$this -> httpRequestTimeout ,
null ,
$urlParameter ,
2023-07-13 12:44:51 +05:45
$doDavRequestAsUser ,
$isGivenStep
2022-12-21 17:22:22 +05:45
);
}
2023-10-03 18:31:24 +05:45
/**
*
* @param string $user
* @param string $folder
* @param bool|null $isGivenStep
2024-07-08 17:22:25 +05:45
* @param string|null $password
2024-09-18 11:26:20 +05:45
* @param string|null $spaceId
2023-10-03 18:31:24 +05:45
*
* @return ResponseInterface
* @throws JsonException | GuzzleException
* @throws GuzzleException | JsonException
*/
2024-09-18 11:26:20 +05:45
public function createFolder ( string $user , string $folder , ? bool $isGivenStep = false , ? string $password = null , ? string $spaceId = null ) : ResponseInterface {
2023-10-03 18:31:24 +05:45
return $this -> makeDavRequest (
$user ,
"MKCOL" ,
$folder ,
[],
null ,
2024-09-18 11:26:20 +05:45
$spaceId ,
2023-10-03 18:31:24 +05:45
"files" ,
null ,
false ,
2024-07-08 17:22:25 +05:45
$password ,
2023-10-03 18:31:24 +05:45
[],
null ,
$isGivenStep
);
}
2022-12-21 17:22:22 +05:45
/**
* @param string $user
* @param string|null $path
* @param string|null $doDavRequestAsUser
* @param string|null $width
* @param string|null $height
*
2023-09-11 17:29:28 +05:45
* @return ResponseInterface
2023-09-12 12:05:55 +05:45
* @throws GuzzleException
2022-12-21 17:22:22 +05:45
*/
2023-09-11 17:29:28 +05:45
public function downloadPreviews ( string $user , ? string $path , ? string $doDavRequestAsUser , ? string $width , ? string $height ) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$user = $this -> getActualUsername ( $user );
$doDavRequestAsUser = $this -> getActualUsername ( $doDavRequestAsUser );
2024-09-17 17:11:20 +05:45
$doDavRequestAsUser = $doDavRequestAsUser ?? $user ;
$password = $this -> getPasswordForUser ( $doDavRequestAsUser );
2022-12-21 17:22:22 +05:45
$urlParameter = [
'x' => $width ,
'y' => $height ,
'preview' => '1'
];
2023-09-11 17:29:28 +05:45
return $this -> makeDavRequest (
2022-12-21 17:22:22 +05:45
$user ,
"GET" ,
$path ,
[],
null ,
2024-09-18 11:26:20 +05:45
null ,
2022-12-21 17:22:22 +05:45
"files" ,
2023-09-12 12:05:55 +05:45
null ,
2022-12-21 17:22:22 +05:45
false ,
2024-09-17 17:11:20 +05:45
$password ,
2022-12-21 17:22:22 +05:45
$urlParameter ,
$doDavRequestAsUser
);
}
/**
* @Then the number of versions should be :arg1
*
* @param int $number
*
* @return void
* @throws Exception
*/
public function theNumberOfVersionsShouldBe ( int $number ) : void {
2024-09-17 17:11:20 +05:45
$resXml = $this -> getResponseXml ();
2022-12-21 17:22:22 +05:45
if ( $resXml === null ) {
$resXml = HttpRequestHelper :: getResponseXml (
$this -> getResponse (),
__METHOD__
);
$this -> setResponseXmlObject ( $resXml );
}
$xmlPart = $resXml -> xpath ( "//d:getlastmodified" );
$actualNumber = \count ( $xmlPart );
Assert :: assertEquals (
$number ,
$actualNumber ,
"Expected number of versions was ' $number ', but got ' $actualNumber '"
);
}
/**
* @Then the number of etag elements in the response should be :number
*
* @param int $number
*
* @return void
* @throws Exception
*/
public function theNumberOfEtagElementInTheResponseShouldBe ( int $number ) : void {
2024-09-17 17:11:20 +05:45
$resXml = $this -> getResponseXml ();
2022-12-21 17:22:22 +05:45
if ( $resXml === null ) {
$resXml = HttpRequestHelper :: getResponseXml (
$this -> getResponse (),
__METHOD__
);
}
$xmlPart = $resXml -> xpath ( "//d:getetag" );
$actualNumber = \count ( $xmlPart );
Assert :: assertEquals (
$number ,
$actualNumber ,
"Expected number of etag elements was ' $number ', but got ' $actualNumber '"
);
}
/**
* @param string $user
* @param string $fileDestination
*
* @return string
* @throws GuzzleException
*/
public function destinationHeaderValue ( string $user , string $fileDestination ) : string {
2024-07-23 15:16:00 +05:45
$fileDestination = \ltrim ( $fileDestination , "/" );
2022-12-21 17:22:22 +05:45
$spaceId = $this -> getPersonalSpaceIdForUser ( $user );
2024-07-23 15:16:00 +05:45
// If the destination is a share, we need to get the space ID for the Shares space
if ( \str_starts_with ( $fileDestination , "Shares/" )) {
$spaceId = $this -> spacesContext -> getSpaceIdByName ( $user , "Shares" );
if ( $this -> getDavPathVersion () === WebDavHelper :: DAV_VERSION_SPACES ) {
$fileDestination = \preg_replace ( "/^Shares\//" , "" , $fileDestination );
}
}
2024-09-17 17:11:20 +05:45
$davPathVersion = $this -> getDavPathVersion ();
2024-10-21 12:24:50 +05:45
$suffixPath = $user ;
2024-09-17 17:11:20 +05:45
if ( $davPathVersion === WebDavHelper :: DAV_VERSION_SPACES ) {
2024-10-21 12:24:50 +05:45
$suffixPath = $spaceId ;
2024-09-17 17:11:20 +05:45
}
2024-10-21 12:24:50 +05:45
$davPath = WebDavHelper :: getDavPath ( $davPathVersion , $suffixPath );
2024-09-17 17:11:20 +05:45
$fullUrl = $this -> getBaseUrl () . "/ $davPath " ;
2024-07-23 15:16:00 +05:45
return \rtrim ( $fullUrl , '/' ) . '/' . $fileDestination ;
2022-12-21 17:22:22 +05:45
}
/**
* @Given /^user "([^"]*)" has moved (?:file|folder|entry) "([^"]*)" to "([^"]*)"$/
*
* @param string|null $user
* @param string|null $fileSource
* @param string|null $fileDestination
*
* @return void
*/
public function userHasMovedFile (
? string $user ,
? string $fileSource ,
? string $fileDestination
) : void {
2024-07-23 15:16:00 +05:45
$response = $this -> moveResource ( $user , $fileSource , $fileDestination );
2023-10-11 16:15:31 +05:45
$actualStatusCode = $response -> getStatusCode ();
2024-05-30 17:07:09 +05:45
$this -> theHTTPStatusCodeShouldBe (
201 ,
" Failed moving resource ' $fileSource ' to ' $fileDestination '." .
" Expected status code was 201 but got ' $actualStatusCode ' " ,
$response
);
2022-12-21 17:22:22 +05:45
}
/**
* @param string $user
2024-05-02 12:21:35 +05:45
* @param string $source
* @param string $destination
2022-12-21 17:22:22 +05:45
*
2024-05-02 12:21:35 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*/
2024-05-02 12:21:35 +05:45
public function moveResource ( string $user , string $source , string $destination ) {
2022-12-21 17:22:22 +05:45
$user = $this -> getActualUsername ( $user );
2024-05-02 12:21:35 +05:45
$headers [ 'Destination' ] = $this -> destinationHeaderValue (
$user ,
$destination
);
return $this -> makeDavRequest (
$user ,
"MOVE" ,
$source ,
$headers
);
2022-12-21 17:22:22 +05:45
}
/**
2024-05-02 12:21:35 +05:45
* @When user :user moves file :source to :destination using the WebDAV API
* @When user :user moves folder :source to :destination using the WebDAV API
2022-12-21 17:22:22 +05:45
*
* @param string $user
2024-05-02 12:21:35 +05:45
* @param string $source
* @param string $destination
2022-12-21 17:22:22 +05:45
*
* @return void
* @throws JsonException
* @throws GuzzleException
*/
2024-05-02 12:21:35 +05:45
public function userMovesFileOrFolderUsingTheWebDavAPI (
2022-12-21 17:22:22 +05:45
string $user ,
2024-05-02 12:21:35 +05:45
string $source ,
string $destination
2022-12-21 17:22:22 +05:45
) : void {
2024-05-02 12:21:35 +05:45
$response = $this -> moveResource ( $user , $source , $destination );
$this -> setResponse ( $response );
$this -> pushToLastHttpStatusCodesArray ();
2022-12-21 17:22:22 +05:45
}
/**
* @When user :user moves the following file using the WebDAV API
*
* @param string $user
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function userMovesTheFollowingFileUsingTheWebdavApi ( string $user , TableNode $table ) : void {
$this -> verifyTableNodeColumns ( $table , [ "source" , "destination" ]);
$rows = $table -> getHash ();
foreach ( $rows as $row ) {
2024-05-02 12:21:35 +05:45
$response = $this -> moveResource ( $user , $row [ "source" ], $row [ "destination" ]);
$this -> setResponse ( $response );
$this -> pushToLastHttpStatusCodesArray (
( string ) $response -> getStatusCode ()
);
2022-12-21 17:22:22 +05:45
}
}
/**
* @When /^user "([^"]*)" moves the following (?:files|folders|entries)\s?(asynchronously|) using the WebDAV API$/
*
* @param string $user
* @param string $type "asynchronously" or empty
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function userMovesFollowingFileUsingTheAPI (
string $user ,
string $type ,
TableNode $table
) : void {
$this -> verifyTableNodeColumns ( $table , [ "from" , "to" ]);
$paths = $table -> getHash ();
foreach ( $paths as $file ) {
2024-05-02 12:21:35 +05:45
$response = $this -> moveResource ( $user , $file [ 'from' ], $file [ 'to' ]);
$this -> pushToLastHttpStatusCodesArray (
( string ) $response -> getStatusCode ()
);
2022-12-21 17:22:22 +05:45
}
}
/**
* @Then /^user "([^"]*)" should be able to rename (file|folder|entry) "([^"]*)" to "([^"]*)"$/
*
* @param string $user
* @param string $entry
* @param string $source
* @param string $destination
*
* @return void
* @throws Exception
*/
public function theUserShouldBeAbleToRenameEntryTo ( string $user , string $entry , string $source , string $destination ) : void {
$user = $this -> getActualUsername ( $user );
2024-05-30 17:07:09 +05:45
$this -> checkFileOrFolderExistsForUser ( $user , $entry , $source );
2024-05-02 12:21:35 +05:45
$response = $this -> moveResource ( $user , $source , $destination );
$this -> theHTTPStatusCodeShouldBeBetween ( 201 , 204 , $response );
2024-05-30 17:07:09 +05:45
$this -> checkFileOrFolderDoesNotExistsForUser ( $user , $entry , $source );
$this -> checkFileOrFolderExistsForUser ( $user , $entry , $destination );
2022-12-21 17:22:22 +05:45
}
/**
* @Then /^user "([^"]*)" should not be able to rename (file|folder|entry) "([^"]*)" to "([^"]*)"$/
*
* @param string $user
* @param string $entry
* @param string $source
* @param string $destination
*
* @return void
* @throws Exception
*/
public function theUserShouldNotBeAbleToRenameEntryTo ( string $user , string $entry , string $source , string $destination ) : void {
2024-05-30 17:07:09 +05:45
$this -> checkFileOrFolderExistsForUser ( $user , $entry , $source );
2024-05-02 12:21:35 +05:45
$response = $this -> moveResource ( $user , $source , $destination );
$this -> theHTTPStatusCodeShouldBeBetween ( 400 , 499 , $response );
2024-05-30 17:07:09 +05:45
$this -> checkFileOrFolderExistsForUser ( $user , $entry , $source );
$this -> checkFileOrFolderDoesNotExistsForUser ( $user , $entry , $destination );
2022-12-21 17:22:22 +05:45
}
/**
* @param string $user
* @param string $fileSource
* @param string $fileDestination
*
2023-10-11 16:15:31 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*/
2023-10-11 16:15:31 +05:45
public function copyFile (
2022-12-21 17:22:22 +05:45
string $user ,
string $fileSource ,
string $fileDestination
2023-10-11 16:15:31 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$user = $this -> getActualUsername ( $user );
$headers [ 'Destination' ] = $this -> destinationHeaderValue (
$user ,
$fileDestination
);
2023-10-11 16:15:31 +05:45
return $this -> makeDavRequest (
2022-12-21 17:22:22 +05:45
$user ,
"COPY" ,
$fileSource ,
$headers
);
2023-10-11 16:15:31 +05:45
}
/**
* @When /^user "([^"]*)" copies (?:file|folder) "([^"]*)" to "([^"]*)" using the WebDAV API$/
*
* @param string $user
* @param string $fileSource
* @param string $fileDestination
*
* @return void
*/
public function userCopiesFileUsingTheAPI (
string $user ,
string $fileSource ,
string $fileDestination
) : void {
$response = $this -> copyFile ( $user , $fileSource , $fileDestination );
$this -> setResponse ( $response );
$this -> pushToLastHttpStatusCodesArray ();
2022-12-21 17:22:22 +05:45
}
/**
* @Given /^user "([^"]*)" has copied file "([^"]*)" to "([^"]*)"$/
*
* @param string $user
* @param string $fileSource
* @param string $fileDestination
*
* @return void
*/
public function userHasCopiedFileUsingTheAPI (
string $user ,
string $fileSource ,
string $fileDestination
) : void {
2023-10-11 16:15:31 +05:45
$response = $this -> copyFile ( $user , $fileSource , $fileDestination );
2022-12-21 17:22:22 +05:45
$this -> theHTTPStatusCodeShouldBe (
[ "201" , "204" ],
2023-10-11 16:15:31 +05:45
"HTTP status code was not 201 or 204 while trying to copy file ' $fileSource ' to ' $fileDestination ' for user ' $user '" ,
$response
2022-12-21 17:22:22 +05:45
);
}
/**
* @param string $user
* @param string $fileSource
* @param string $range
*
2023-10-11 16:15:31 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*/
2023-10-11 16:15:31 +05:45
public function downloadFileWithRange ( string $user , string $fileSource , string $range ) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$user = $this -> getActualUsername ( $user );
$headers [ 'Range' ] = $range ;
2023-10-11 16:15:31 +05:45
return $this -> makeDavRequest (
2022-12-21 17:22:22 +05:45
$user ,
"GET" ,
$fileSource ,
$headers
);
}
2023-10-11 16:15:31 +05:45
/**
* @When /^user "([^"]*)" downloads file "([^"]*)" with range "([^"]*)" using the WebDAV API$/
*
* @param string $user
* @param string $fileSource
* @param string $range
*
* @return void
*/
public function userDownloadsFileWithRangeUsingWebDavApi ( string $user , string $fileSource , string $range ) : void {
$this -> setResponse ( $this -> downloadFileWithRange ( $user , $fileSource , $range ));
}
2022-12-21 17:22:22 +05:45
/**
* @Then /^user "([^"]*)" using password "([^"]*)" should not be able to download file "([^"]*)"$/
*
* @param string $user
* @param string $password
* @param string $fileName
*
* @return void
*/
public function userUsingPasswordShouldNotBeAbleToDownloadFile (
string $user ,
string $password ,
string $fileName
) : void {
$user = $this -> getActualUsername ( $user );
$password = $this -> getActualPassword ( $password );
2023-10-11 16:15:31 +05:45
$response = $this -> downloadFileAsUserUsingPassword ( $user , $fileName , $password );
2022-12-21 17:22:22 +05:45
Assert :: assertGreaterThanOrEqual (
400 ,
2023-10-11 16:15:31 +05:45
$response -> getStatusCode (),
2022-12-21 17:22:22 +05:45
__METHOD__
. ' download must fail'
);
Assert :: assertLessThanOrEqual (
499 ,
2023-10-11 16:15:31 +05:45
$response -> getStatusCode (),
2022-12-21 17:22:22 +05:45
__METHOD__
. ' 4xx error expected but got status code "'
2023-10-11 16:15:31 +05:45
. $response -> getStatusCode () . '"'
2022-12-21 17:22:22 +05:45
);
}
/**
* @Then /^user "([^"]*)" should not be able to download file "([^"]*)"$/
*
* @param string $user
* @param string $fileName
*
* @return void
* @throws JsonException
*/
public function userShouldNotBeAbleToDownloadFile (
string $user ,
string $fileName
) : void {
$user = $this -> getActualUsername ( $user );
$password = $this -> getPasswordForUser ( $user );
2023-10-11 16:15:31 +05:45
$response = $this -> downloadFileAsUserUsingPassword ( $user , $fileName , $password );
2022-12-21 17:22:22 +05:45
Assert :: assertGreaterThanOrEqual (
400 ,
2023-10-11 16:15:31 +05:45
$response -> getStatusCode (),
2022-12-21 17:22:22 +05:45
__METHOD__
. ' download must fail'
);
Assert :: assertLessThanOrEqual (
499 ,
2023-10-11 16:15:31 +05:45
$response -> getStatusCode (),
2022-12-21 17:22:22 +05:45
__METHOD__
. ' 4xx error expected but got status code "'
2023-10-11 16:15:31 +05:45
. $response -> getStatusCode () . '"'
2022-12-21 17:22:22 +05:45
);
}
/**
* @Then /^user "([^"]*)" should be able to access a skeleton file$/
*
* @param string $user
*
* @return void
*/
public function userShouldBeAbleToAccessASkeletonFile ( string $user ) : void {
2024-05-30 17:07:09 +05:45
$user = $this -> getActualUsername ( $user );
$response = $this -> downloadFileAsUserUsingPassword ( $user , "textfile0.txt" );
$actualStatus = $response -> getStatusCode ();
Assert :: assertEquals (
200 ,
$actualStatus ,
"Expected status code to be '200', but got ' $actualStatus '"
2022-12-21 17:22:22 +05:45
);
2024-05-30 17:07:09 +05:45
$this -> checkDownloadedContentMatches ( "ownCloud test text file 0 \n " , '' , $response );
2022-12-21 17:22:22 +05:45
}
/**
* @Then the size of the downloaded file should be :size bytes
*
* @param string $size
*
* @return void
*/
public function sizeOfDownloadedFileShouldBe ( string $size ) : void {
$actualSize = \strlen (( string ) $this -> response -> getBody ());
Assert :: assertEquals (
$size ,
$actualSize ,
"Expected size of the downloaded file was ' $size ' but got ' $actualSize '"
);
}
/**
* @Then /^the downloaded content should end with "([^"]*)"$/
*
* @param string $content
*
* @return void
*/
public function downloadedContentShouldEndWith ( string $content ) : void {
$actualContent = \substr (( string ) $this -> response -> getBody (), - \strlen ( $content ));
Assert :: assertEquals (
$content ,
$actualContent ,
"The downloaded content was expected to end with ' $content ', but actually ended with ' $actualContent '."
);
}
/**
* @Then /^the downloaded content should be "([^"]*)"$/
*
* @param string $content
*
* @return void
*/
public function downloadedContentShouldBe ( string $content ) : void {
2024-07-29 14:57:52 +05:45
$this -> checkDownloadedContentMatches ( $content , '' , $this -> getResponse ());
2022-12-21 17:22:22 +05:45
}
/**
* @param string $expectedContent
* @param string $extraErrorText
2023-09-11 17:29:28 +05:45
* @param ResponseInterface|null $response
2022-12-21 17:22:22 +05:45
*
* @return void
*/
public function checkDownloadedContentMatches (
string $expectedContent ,
2023-09-11 17:29:28 +05:45
string $extraErrorText = "" ,
? ResponseInterface $response = null
2022-12-21 17:22:22 +05:45
) : void {
2023-09-11 17:29:28 +05:45
$response = $response ?? $this -> response ;
$actualContent = ( string ) $response -> getBody ();
2022-12-21 17:22:22 +05:45
// For this test we really care about the content.
// A separate "Then" step can specifically check the HTTP status.
// But if the content is wrong (e.g. empty) then it is useful to
// report the HTTP status to give some clue what might be the problem.
2023-10-11 16:15:31 +05:45
$actualStatus = $response -> getStatusCode ();
2022-12-21 17:22:22 +05:45
if ( $extraErrorText !== "" ) {
$extraErrorText .= " \n " ;
}
Assert :: assertEquals (
$expectedContent ,
$actualContent ,
$extraErrorText . "The content was expected to be ' $expectedContent ', but actually is ' $actualContent '. HTTP status was $actualStatus "
);
}
/**
* @Then the content in the response should match the following content:
*
* @param PyStringNode $content
*
* @return void
*/
public function theContentInTheResponseShouldMatchTheFollowingContent ( PyStringNode $content ) : void {
2024-07-29 14:57:52 +05:45
$this -> checkDownloadedContentMatches ( $content -> getRaw (), '' , $this -> getResponse ());
}
/**
* @Then the content in the response should include the following content:
*
* @param PyStringNode $content
*
* @return void
*/
public function theContentInTheResponseShouldIncludeTheFollowingContent ( PyStringNode $content ) : void {
Assert :: assertStringContainsString (
$content -> getRaw (),
( string ) $this -> response -> getBody ()
);
2022-12-21 17:22:22 +05:45
}
/**
* @Then /^if the HTTP status code was "([^"]*)" then the downloaded content for multipart byterange should be:$/
*
* @param int $statusCode
* @param PyStringNode $content
*
* @return void
*
*/
2023-05-02 17:25:56 +05:45
public function theDownloadedContentForMultipartByteRangeShouldBe ( int $statusCode , PyStringNode $content ) : void {
2022-12-21 17:22:22 +05:45
$actualStatusCode = $this -> response -> getStatusCode ();
if ( $actualStatusCode === $statusCode ) {
$actualContent = ( string ) $this -> response -> getBody ();
$pattern = [ "/--\w*/" , "/\s*/m" ];
$actualContent = \preg_replace ( $pattern , "" , $actualContent );
$content = \preg_replace ( "/\s*/m" , '' , $content -> getRaw ());
Assert :: assertEquals (
$content ,
$actualContent ,
"The downloaded content was expected to be ' $content ', but actually is ' $actualContent '. HTTP status was $actualStatusCode "
);
}
}
/**
* @Then /^if the HTTP status code was "([^"]*)" then the downloaded content should be "([^"]*)"$/
*
* @param int $statusCode
* @param string $content
*
* @return void
*/
public function checkStatusCodeForDownloadedContentShouldBe ( int $statusCode , string $content ) : void {
$actualStatusCode = $this -> response -> getStatusCode ();
if ( $actualStatusCode === $statusCode ) {
2024-07-29 14:57:52 +05:45
$this -> checkDownloadedContentMatches ( $content , '' , $this -> getResponse ());
2022-12-21 17:22:22 +05:45
}
}
/**
2024-05-17 14:19:53 +05:45
* @Then the content of file :fileName for user :user should be :content
2022-12-21 17:22:22 +05:45
*
* @param string $fileName
* @param string $user
* @param string $content
*
* @return void
*/
public function contentOfFileForUserShouldBe ( string $fileName , string $user , string $content ) : void {
$user = $this -> getActualUsername ( $user );
2023-10-11 16:15:31 +05:45
$response = $this -> downloadFileAsUserUsingPassword ( $user , $fileName );
$actualStatus = $response -> getStatusCode ();
2023-06-28 10:12:04 +05:45
if ( $actualStatus !== 200 ) {
throw new Exception (
"Expected status code to be '200', but got ' $actualStatus '"
);
}
2023-10-11 16:15:31 +05:45
$this -> checkDownloadedContentMatches ( $content , '' , $response );
2022-12-21 17:22:22 +05:45
}
/**
* @Then /^the content of the following files for user "([^"]*)" should be "([^"]*)"$/
*
* @param string $user
* @param string $content
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function contentOfFollowingFilesShouldBe ( string $user , string $content , TableNode $table ) : void {
$this -> verifyTableNodeColumns ( $table , [ "path" ]);
$paths = $table -> getHash ();
2024-05-30 17:07:09 +05:45
$user = $this -> getActualUsername ( $user );
2022-12-21 17:22:22 +05:45
foreach ( $paths as $file ) {
2024-05-30 17:07:09 +05:45
$response = $this -> downloadFileAsUserUsingPassword ( $user , $file [ "path" ]);
$actualStatus = $response -> getStatusCode ();
Assert :: assertEquals (
200 ,
$actualStatus ,
"Expected status code to be '200', but got ' $actualStatus '"
);
$this -> checkDownloadedContentMatches ( $content , '' , $response );
2022-12-21 17:22:22 +05:45
}
}
/**
* @Then /^the content of file "([^"]*)" for user "([^"]*)" using password "([^"]*)" should be "([^"]*)"$/
*
* @param string $fileName
* @param string $user
* @param string|null $password
* @param string $content
*
* @return void
*/
public function contentOfFileForUserUsingPasswordShouldBe (
string $fileName ,
string $user ,
? string $password ,
string $content
) : void {
$user = $this -> getActualUsername ( $user );
$password = $this -> getActualPassword ( $password );
2023-10-11 16:15:31 +05:45
$response = $this -> downloadFileAsUserUsingPassword ( $user , $fileName , $password );
$this -> checkDownloadedContentMatches ( $content , '' , $response );
2022-12-21 17:22:22 +05:45
}
/**
* @Then /^the content of file "([^"]*)" for user "([^"]*)" should be:$/
*
* @param string $fileName
* @param string $user
* @param PyStringNode $content
*
* @return void
*/
public function contentOfFileForUserShouldBePyString (
string $fileName ,
string $user ,
PyStringNode $content
) : void {
2024-05-30 17:07:09 +05:45
$user = $this -> getActualUsername ( $user );
$response = $this -> downloadFileAsUserUsingPassword ( $user , $fileName );
$actualStatus = $response -> getStatusCode ();
Assert :: assertEquals (
200 ,
$actualStatus ,
"Expected status code to be '200', but got ' $actualStatus '"
);
$this -> checkDownloadedContentMatches ( $content -> getRaw (), '' , $response );
2022-12-21 17:22:22 +05:45
}
/**
* @When user :user downloads file :fileName using the WebDAV API
2024-05-02 12:21:35 +05:45
* @When user :user tries to download file :fileName using the WebDAV API
2022-12-21 17:22:22 +05:45
*
* @param string $user
* @param string $fileName
*
* @return void
*/
public function userDownloadsFileUsingTheAPI (
string $user ,
string $fileName
) : void {
2023-10-11 16:15:31 +05:45
$this -> setResponse ( $this -> downloadFileAsUserUsingPassword ( $user , $fileName ));
2022-12-21 17:22:22 +05:45
}
/**
* @param string $user
* @param string $fileName
* @param string|null $password
* @param array|null $headers
2024-09-18 11:26:20 +05:45
* @param string|null $spaceId
2022-12-21 17:22:22 +05:45
*
2023-10-11 16:15:31 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*/
public function downloadFileAsUserUsingPassword (
string $user ,
string $fileName ,
? string $password = null ,
2024-09-18 11:26:20 +05:45
? array $headers = [],
? string $spaceId = null ,
2023-10-11 16:15:31 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$user = $this -> getActualUsername ( $user );
$password = $this -> getActualPassword ( $password );
2023-10-11 16:15:31 +05:45
return $this -> makeDavRequest (
2022-12-21 17:22:22 +05:45
$user ,
'GET' ,
$fileName ,
$headers ,
null ,
2024-09-18 11:26:20 +05:45
$spaceId ,
2022-12-21 17:22:22 +05:45
"files" ,
null ,
false ,
$password
);
}
/**
* @When the public gets the size of the last shared public link using the WebDAV API
*
* @return void
* @throws Exception
*/
public function publicGetsSizeOfLastSharedPublicLinkUsingTheWebdavApi () : void {
2024-05-15 14:14:21 +05:45
$token = ( $this -> isUsingSharingNG ()) ? $this -> shareNgGetLastCreatedLinkShareToken () : $this -> getLastCreatedPublicShareToken ();
2024-09-17 17:11:20 +05:45
$davPath = WebDavHelper :: getDavPath ( $this -> getDavPathVersion (), $token , "public-files" );
2024-10-21 12:24:50 +05:45
$url = " { $this -> getBaseUrl () } / $davPath " ;
2022-12-21 17:22:22 +05:45
$this -> response = HttpRequestHelper :: sendRequest (
$url ,
$this -> getStepLineRef (),
2023-05-02 17:25:56 +05:45
"PROPFIND"
2022-12-21 17:22:22 +05:45
);
}
/**
* @When user :user gets the size of file :resource using the WebDAV API
*
* @param string $user
* @param string $resource
*
* @return void
* @throws Exception
*/
public function userGetsSizeOfFileUsingTheWebdavApi ( string $user , string $resource ) : void {
$user = $this -> getActualUsername ( $user );
$password = $this -> getPasswordForUser ( $user );
$this -> response = WebDavHelper :: propfind (
$this -> getBaseUrl (),
$user ,
$password ,
$resource ,
[],
$this -> getStepLineRef (),
"0" ,
2024-09-18 11:26:20 +05:45
null ,
2022-12-21 17:22:22 +05:45
"files" ,
$this -> getDavPathVersion ()
);
}
/**
* @Then the size of the file should be :size
*
* @param string $size
*
* @return void
* @throws Exception
*/
public function theSizeOfTheFileShouldBe ( string $size ) : void {
$responseXml = HttpRequestHelper :: getResponseXml (
$this -> response ,
__METHOD__
);
$xmlPart = $responseXml -> xpath ( "//d:prop/d:getcontentlength" );
$actualSize = ( string ) $xmlPart [ 0 ];
Assert :: assertEquals (
$size ,
$actualSize ,
__METHOD__
. " Expected size of the file was ' $size ', but got ' $actualSize ' instead."
);
}
2024-07-09 14:11:46 +05:45
/**
* @Then /^the content of file "([^"]*)" for user "([^"]*)" should be "([^"]*)" plus end-of-line$/
*
* @param string $fileName
* @param string $user
* @param string $content
*
* @return void
*/
public function contentOfFileForUserShouldBePlusEndOfLine ( string $fileName , string $user , string $content ) : void {
$user = $this -> getActualUsername ( $user );
$response = $this -> downloadFileAsUserUsingPassword ( $user , $fileName );
$actualStatus = $response -> getStatusCode ();
Assert :: assertEquals (
200 ,
$actualStatus ,
"Expected status code to be '200', but got ' $actualStatus '"
);
$this -> checkDownloadedContentMatches ( " $content \n " , '' , $response );
}
2022-12-21 17:22:22 +05:45
/**
* @Then the following headers should be set
*
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function theFollowingHeadersShouldBeSet ( TableNode $table ) : void {
$this -> verifyTableNodeColumns (
$table ,
[ 'header' , 'value' ]
);
foreach ( $table -> getColumnsHash () as $header ) {
$headerName = $header [ 'header' ];
$expectedHeaderValue = $header [ 'value' ];
$returnedHeader = $this -> response -> getHeader ( $headerName );
$expectedHeaderValue = $this -> substituteInLineCodes ( $expectedHeaderValue );
2023-05-02 17:25:56 +05:45
if ( empty ( $returnedHeader )) {
throw new Exception (
\sprintf (
"Missing expected header '%s'" ,
$headerName
)
);
2022-12-21 17:22:22 +05:45
}
2023-05-02 17:25:56 +05:45
$headerValue = $returnedHeader [ 0 ];
2022-12-21 17:22:22 +05:45
Assert :: assertEquals (
$expectedHeaderValue ,
$headerValue ,
__METHOD__
. " Expected value for header ' $headerName ' was ' $expectedHeaderValue ', but got ' $headerValue ' instead."
);
}
}
/**
2024-05-02 12:21:35 +05:45
* @Then as :user :entry :path should not exist
2022-12-21 17:22:22 +05:45
*
* @param string $user
* @param string $entry
2024-05-30 17:07:09 +05:45
* @param string $path
2022-12-21 17:22:22 +05:45
*
* @return ResponseInterface
* @throws Exception
*/
public function asFileOrFolderShouldNotExist (
2024-05-30 17:07:09 +05:45
string $user ,
string $entry ,
string $path
) : void {
$this -> checkFileOrFolderDoesNotExistsForUser ( $user , $entry , $path );
}
/**
* @param string $user
* @param string $entry
* @param string|null $path
* @param string $type
*
* @return void
*/
public function checkFileOrFolderDoesNotExistsForUser (
2022-12-21 17:22:22 +05:45
string $user ,
string $entry = "file" ,
? string $path = null ,
string $type = "files"
2024-01-25 14:06:45 +05:45
) : void {
2022-12-21 17:22:22 +05:45
$user = $this -> getActualUsername ( $user );
$path = $this -> substituteInLineCodes ( $path );
$response = $this -> listFolder (
$user ,
$path ,
'0' ,
null ,
2024-09-18 11:26:20 +05:45
null ,
2022-12-21 17:22:22 +05:45
$type
);
$statusCode = $response -> getStatusCode ();
2024-07-09 14:10:50 +05:45
if ( $statusCode < 400 || $statusCode > 499 ) {
2022-12-21 17:22:22 +05:45
try {
2024-01-25 14:06:45 +05:45
$responseXml = HttpRequestHelper :: getResponseXml (
2022-12-21 17:22:22 +05:45
$response ,
__METHOD__
);
} catch ( Exception $e ) {
Assert :: fail (
" $entry ' $path ' should not exist. But API returned $statusCode without XML in the body"
);
}
Assert :: assertTrue (
2023-10-05 09:55:11 +05:45
$this -> isEtagValid ( $this -> getEtagFromResponseXmlObject ( $responseXml )),
2022-12-21 17:22:22 +05:45
" $entry ' $path ' should not exist. But API returned $statusCode without an etag in the body"
);
2023-10-05 09:55:11 +05:45
$isCollection = $responseXml -> xpath ( "//d:prop/d:resourcetype/d:collection" );
2022-12-21 17:22:22 +05:45
if ( \count ( $isCollection ) === 0 ) {
$actualResourceType = "file" ;
} else {
$actualResourceType = "folder" ;
}
2024-02-15 21:26:17 +05:45
if ( $entry === $actualResourceType ) {
Assert :: fail (
" $entry ' $path ' should not exist. But it does."
);
}
2022-12-21 17:22:22 +05:45
}
}
/**
* @Then /^as "([^"]*)" the following (files|folders) should not exist$/
*
* @param string $user
* @param string $entry
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function followingFilesShouldNotExist (
string $user ,
string $entry ,
TableNode $table
) : void {
$this -> verifyTableNodeColumns ( $table , [ "path" ]);
$paths = $table -> getHash ();
$entry = \rtrim ( $entry , "s" );
foreach ( $paths as $file ) {
2024-05-30 17:07:09 +05:45
$this -> checkFileOrFolderDoesNotExistsForUser ( $user , $entry , $file [ "path" ]);
2022-12-21 17:22:22 +05:45
}
}
/**
2024-05-02 12:21:35 +05:45
* @Then as :user :entry :path should exist
2022-12-21 17:22:22 +05:45
*
* @param string $user
* @param string $entry
* @param string $path
*
* @return void
* @throws Exception
*/
public function asFileOrFolderShouldExist (
2024-05-30 17:07:09 +05:45
string $user ,
string $entry ,
string $path
) : void {
$this -> checkFileOrFolderExistsForUser ( $user , $entry , $path );
}
/**
* @param string $user
* @param string $entry
* @param string $path
* @param string|null $type
*
* @return void
*/
public function checkFileOrFolderExistsForUser (
2022-12-21 17:22:22 +05:45
string $user ,
string $entry ,
string $path ,
2024-05-30 17:07:09 +05:45
? string $type = "files"
2022-12-21 17:22:22 +05:45
) : void {
$user = $this -> getActualUsername ( $user );
$path = $this -> substituteInLineCodes ( $path );
2023-10-05 09:55:11 +05:45
$responseXml = $this -> listFolderAndReturnResponseXml (
2022-12-21 17:22:22 +05:45
$user ,
$path ,
'0' ,
null ,
$type
);
Assert :: assertTrue (
2023-10-05 09:55:11 +05:45
$this -> isEtagValid ( $this -> getEtagFromResponseXmlObject ( $responseXml )),
2022-12-21 17:22:22 +05:45
" $entry ' $path ' expected to exist for user $user but not found"
);
2023-10-05 09:55:11 +05:45
$isCollection = $responseXml -> xpath ( "//d:prop/d:resourcetype/d:collection" );
2022-12-21 17:22:22 +05:45
if ( $entry === "folder" ) {
Assert :: assertEquals ( \count ( $isCollection ), 1 , "Unexpectedly, ` $path ` is not a folder" );
} elseif ( $entry === "file" ) {
Assert :: assertEquals ( \count ( $isCollection ), 0 , "Unexpectedly, ` $path ` is not a file" );
}
}
/**
* @Then /^as "([^"]*)" the following (files|folders) should exist$/
*
* @param string $user
* @param string $entry
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function followingFilesOrFoldersShouldExist (
string $user ,
string $entry ,
TableNode $table
) : void {
$this -> verifyTableNodeColumns ( $table , [ "path" ]);
$paths = $table -> getHash ();
$entry = \rtrim ( $entry , "s" );
foreach ( $paths as $file ) {
2024-05-30 17:07:09 +05:45
$this -> checkFileOrFolderExistsForUser ( $user , $entry , $file [ "path" ]);
2022-12-21 17:22:22 +05:45
}
}
/**
*
* @param string $user
* @param string $entry
* @param string $path
* @param string $type
*
* @return bool
*/
public function fileOrFolderExists (
string $user ,
string $entry ,
string $path ,
string $type = "files"
) : bool {
try {
2024-05-30 17:07:09 +05:45
$this -> checkFileOrFolderExistsForUser ( $user , $entry , $path , $type );
2022-12-21 17:22:22 +05:45
return true ;
} catch ( Exception $e ) {
return false ;
}
}
/**
*
* @param string $user
* @param string $path
* @param string $folderDepth requires 1 to see elements without children
* @param array|null $properties
2024-09-18 11:26:20 +05:45
* @param string|null $spaceId
2022-12-21 17:22:22 +05:45
* @param string $type
*
* @return ResponseInterface
* @throws Exception
*/
public function listFolder (
string $user ,
string $path ,
string $folderDepth ,
? array $properties = null ,
2024-09-18 11:26:20 +05:45
? string $spaceId = null ,
2022-12-21 17:22:22 +05:45
string $type = "files"
) : ResponseInterface {
return WebDavHelper :: listFolder (
$this -> getBaseUrl (),
$this -> getActualUsername ( $user ),
$this -> getPasswordForUser ( $user ),
$path ,
$folderDepth ,
2024-09-18 11:26:20 +05:45
$spaceId ,
2022-12-21 17:22:22 +05:45
$this -> getStepLineRef (),
$properties ,
$type ,
$this -> getDavPathVersion ()
);
}
/**
*
* @param string $user
* @param string $path
* @param string $folderDepth requires 1 to see elements without children
* @param array|null $properties
* @param string $type
*
* @return SimpleXMLElement
* @throws Exception
*/
public function listFolderAndReturnResponseXml (
string $user ,
string $path ,
string $folderDepth ,
? array $properties = null ,
string $type = "files"
) : SimpleXMLElement {
return HttpRequestHelper :: getResponseXml (
$this -> listFolder (
$user ,
$path ,
$folderDepth ,
$properties ,
2024-09-18 11:26:20 +05:45
null ,
2022-12-21 17:22:22 +05:45
$type
),
__METHOD__
);
}
/**
* @Then /^user "([^"]*)" should (not|)\s?see the following elements$/
*
* @param string $user
* @param string $shouldOrNot
* @param TableNode $elements
*
* @return void
* @throws InvalidArgumentException|Exception
*
*/
public function userShouldSeeTheElements ( string $user , string $shouldOrNot , TableNode $elements ) : void {
$should = ( $shouldOrNot !== "not" );
$this -> checkElementList ( $user , $elements , $should );
}
/**
* asserts that the user can or cannot see a list of files/folders by propfind
*
* @param string $user
* @param TableNode $elements
* @param boolean $expectedToBeListed
*
* @return void
* @throws InvalidArgumentException
* @throws Exception
*
*/
public function checkElementList (
string $user ,
TableNode $elements ,
bool $expectedToBeListed = true
) : void {
$user = $this -> getActualUsername ( $user );
$this -> verifyTableNodeColumnsCount ( $elements , 1 );
$elementRows = $elements -> getRows ();
$elementsSimplified = $this -> simplifyArray ( $elementRows );
if ( $this -> davPropfindDepthInfinityIsEnabled ()) {
// get a full "infinite" list of the user's root folder in one request
// and process that to check the elements (resources)
$responseXmlObject = $this -> listFolderAndReturnResponseXml (
$user ,
"/" ,
2024-06-19 12:44:50 +05:45
"infinity"
2022-12-21 17:22:22 +05:45
);
foreach ( $elementsSimplified as $expectedElement ) {
// Allow the table of expected elements to have entries that do
// not have to specify the "implied" leading slash, or have multiple
// leading slashes, to make scenario outlines more flexible
$expectedElement = $this -> encodePath ( $expectedElement );
$expectedElement = "/" . \ltrim ( $expectedElement , "/" );
$webdavPath = "/" . $this -> getFullDavFilesPath ( $user ) . $expectedElement ;
$element = $responseXmlObject -> xpath (
"//d:response/d:href[text() = \" $webdavPath \" ]"
);
if ( $expectedToBeListed
&& ( ! isset ( $element [ 0 ]) || urldecode ( $element [ 0 ] -> __toString ()) !== urldecode ( $webdavPath ))
) {
Assert :: fail (
" $webdavPath is not in propfind answer but should be"
);
} elseif ( ! $expectedToBeListed && isset ( $element [ 0 ])
) {
Assert :: fail (
" $webdavPath is in propfind answer but should not be"
);
}
}
} else {
// do a PROPFIND for each element
foreach ( $elementsSimplified as $elementToRequest ) {
// Allow the table of expected elements to have entries that do
// not have to specify the "implied" leading slash, or have multiple
// leading slashes, to make scenario outlines more flexible
$elementToRequest = "/" . \ltrim ( $elementToRequest , "/" );
// Note: in the request we ask to do a PROPFIND on a resource like:
// /some-folder with spaces/sub-folder
// but the response has encoded values for the special characters like:
// /some-folder%20with%20spaces/sub-folder
// So we need both $elementToRequest and $expectedElement
$expectedElement = $this -> encodePath ( $elementToRequest );
$responseXmlObject = $this -> listFolderAndReturnResponseXml (
$user ,
$elementToRequest ,
"1"
);
2024-08-12 18:14:37 +05:45
2022-12-21 17:22:22 +05:45
$webdavPath = "/" . $this -> getFullDavFilesPath ( $user ) . $expectedElement ;
2024-10-29 15:43:10 +05:45
// return xmlobject that matches the x-path pattern.
2022-12-21 17:22:22 +05:45
$element = $responseXmlObject -> xpath (
2024-10-29 15:43:10 +05:45
"//d:response/d:href"
2022-12-21 17:22:22 +05:45
);
2024-11-08 10:06:15 +05:45
// check the first element because the requested resource will always be the first one
2024-10-29 15:43:10 +05:45
if ( ! $expectedToBeListed && isset ( $element [ 0 ])
2022-12-21 17:22:22 +05:45
) {
Assert :: fail (
" $webdavPath is in propfind answer but should not be"
);
2024-10-29 15:43:10 +05:45
};
if ( $expectedToBeListed ) {
$elementSanitized = rtrim ( $element [ 0 ] -> __toString (), '/' );
if ( ! isset ( $element [ 0 ]) || urldecode ( $elementSanitized ) !== urldecode ( $webdavPath )) {
Assert :: fail (
" $webdavPath is not in propfind answer but should be"
);
}
2022-12-21 17:22:22 +05:45
}
}
}
}
/**
* @param string $user
* @param string $source
* @param string $destination
2024-09-18 11:26:20 +05:45
* @param string $spaceId
2023-07-18 14:15:46 +05:45
* @param bool|null $isGivenStep
2022-12-21 17:22:22 +05:45
*
2023-10-11 16:15:31 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*/
2023-10-11 16:15:31 +05:45
public function uploadFile (
2023-07-18 14:15:46 +05:45
string $user ,
string $source ,
string $destination ,
2024-09-18 11:26:20 +05:45
string $spaceId = null ,
2023-07-18 14:15:46 +05:45
? bool $isGivenStep = false
2023-10-11 16:15:31 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$user = $this -> getActualUsername ( $user );
$file = \fopen ( $this -> acceptanceTestsDirLocation () . $source , 'r' );
$this -> pauseUploadDelete ();
2023-10-11 16:15:31 +05:45
$response = $this -> makeDavRequest (
2022-12-21 17:22:22 +05:45
$user ,
"PUT" ,
$destination ,
[],
2023-07-18 14:15:46 +05:45
$file ,
2024-09-18 11:26:20 +05:45
$spaceId ,
2023-07-18 14:15:46 +05:45
"files" ,
null ,
false ,
null ,
[],
null ,
$isGivenStep
2022-12-21 17:22:22 +05:45
);
$this -> lastUploadDeleteTime = \time ();
2023-10-11 16:15:31 +05:45
return $response ;
}
/**
* @When user :user uploads file :source to :destination using the WebDAV API
*
* @param string $user
* @param string $source
* @param string $destination
*
* @return void
*/
public function userUploadsAFileToUsingWebDavApi (
string $user ,
string $source ,
string $destination
) : void {
$response = $this -> uploadFile ( $user , $source , $destination );
$this -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
$this -> setResponseXml (
2023-10-11 16:15:31 +05:45
HttpRequestHelper :: parseResponseAsXml ( $response )
2022-12-21 17:22:22 +05:45
);
$this -> pushToLastHttpStatusCodesArray (
( string ) $this -> getResponse () -> getStatusCode ()
);
}
/**
* @Given user :user has uploaded file :source to :destination
*
* @param string $user
* @param string $source
* @param string $destination
*
2024-08-26 10:00:58 +05:45
* @return array
2022-12-21 17:22:22 +05:45
*/
2024-08-23 11:36:51 +05:45
public function userHasUploadedAFileTo ( string $user , string $source , string $destination ) : array {
2024-09-18 11:26:20 +05:45
$response = $this -> uploadFile ( $user , $source , $destination , null , true );
2022-12-21 17:22:22 +05:45
$this -> theHTTPStatusCodeShouldBe (
[ "201" , "204" ],
2023-10-11 16:15:31 +05:45
"HTTP status code was not 201 or 204 while trying to upload file ' $source ' to ' $destination ' for user ' $user '" ,
$response
2022-12-21 17:22:22 +05:45
);
2024-08-23 11:36:51 +05:45
return $response -> getHeader ( 'oc-fileid' );
2022-12-21 17:22:22 +05:45
}
/**
* Upload file as a user with different headers
*
* @param string $user
* @param string $source
* @param string $destination
* @param array|null $headers
* @param int|null $noOfChunks Only use for chunked upload when $this->chunkingToUse is not null
*
* @return void
* @throws Exception
*/
public function uploadFileWithHeaders (
string $user ,
string $source ,
string $destination ,
? array $headers = [],
? int $noOfChunks = 0
) : void {
$chunkingVersion = $this -> chunkingToUse ;
if ( $noOfChunks <= 0 ) {
$chunkingVersion = null ;
}
try {
$this -> responseXml = [];
$this -> pauseUploadDelete ();
$this -> response = UploadHelper :: upload (
$this -> getBaseUrl (),
$this -> getActualUsername ( $user ),
$this -> getUserPassword ( $user ),
$source ,
$destination ,
$this -> getStepLineRef (),
$headers ,
$this -> getDavPathVersion (),
$chunkingVersion ,
$noOfChunks
);
$this -> lastUploadDeleteTime = \time ();
} catch ( BadResponseException $e ) {
// 4xx and 5xx responses cause an exception
$this -> response = $e -> getResponse ();
}
}
2024-05-30 17:07:09 +05:45
/**
* @param string $user
* @param string $source
* @param string $destination
* @param integer $noOfChunks
* @param boolean $async
* @param array|null $headers
*
* @return void
*/
public function userUploadsAFileInChunk (
2022-12-21 17:22:22 +05:45
string $user ,
string $source ,
string $destination ,
int $noOfChunks = 2 ,
bool $async = false ,
? array $headers = []
) : void {
$user = $this -> getActualUsername ( $user );
Assert :: assertGreaterThan (
0 ,
$noOfChunks ,
"What does it mean to have $noOfChunks chunks?"
);
2024-11-07 14:12:16 +05:45
// use chunking version 1 as default, since version 2 uses "remote.php/dav/uploads" endpoint and it doesn't exist in oCIS
$this -> chunkingToUse = 1 ;
2022-12-21 17:22:22 +05:45
if ( $async === true ) {
$headers [ 'OC-LazyOps' ] = 'true' ;
}
$this -> uploadFileWithHeaders (
$user ,
$this -> acceptanceTestsDirLocation () . $source ,
$destination ,
$headers ,
$noOfChunks
);
$this -> pushToLastStatusCodesArrays ();
}
/**
* Uploading with old/new DAV and chunked/non-chunked.
* Except do not do the new-DAV-new-chunking combination. That is not being
* supported on all implementations.
*
* @When user :user uploads file :source to filenames based on :destination with all mechanisms except new chunking using the WebDAV API
*
* @param string $user
* @param string $source
* @param string $destination
*
* @return void
* @throws Exception
*/
public function userUploadsAFileToWithAllMechanismsExceptNewChunking (
string $user ,
string $source ,
string $destination
) : void {
$user = $this -> getActualUsername ( $user );
$this -> uploadResponses = UploadHelper :: uploadWithAllMechanisms (
$this -> getBaseUrl (),
$this -> getActualUsername ( $user ),
$this -> getUserPassword ( $user ),
$this -> acceptanceTestsDirLocation () . $source ,
$destination ,
$this -> getStepLineRef (),
false ,
'new'
);
}
/**
2024-11-07 14:12:16 +05:45
* @When /^user "([^"]*)" uploads file "([^"]*)" to "([^"]*)" in (\d+) chunks using the WebDAV API$/
2022-12-21 17:22:22 +05:45
*
* @param string $user
* @param string $source
* @param string $destination
2024-07-08 16:51:10 +05:45
* @param int $noOfChunks
2022-12-21 17:22:22 +05:45
*
* @return void
* @throws Exception
*/
2024-07-08 16:51:10 +05:45
public function userUploadsAFileToWithChunks (
2022-12-21 17:22:22 +05:45
string $user ,
string $source ,
2024-07-08 16:51:10 +05:45
string $destination ,
2024-11-07 14:12:16 +05:45
int $noOfChunks = 2
2022-12-21 17:22:22 +05:45
) : void {
2024-11-07 14:12:16 +05:45
$this -> userUploadsAFileInChunk ( $user , $source , $destination , $noOfChunks );
2022-12-21 17:22:22 +05:45
}
/**
* @Then /^the HTTP status code of all upload responses should be "([^"]*)"$/
*
* @param int $statusCode
*
* @return void
*/
public function theHTTPStatusCodeOfAllUploadResponsesShouldBe ( int $statusCode ) : void {
foreach ( $this -> uploadResponses as $response ) {
Assert :: assertEquals (
$statusCode ,
$response -> getStatusCode (),
'Response did not return expected status code'
);
}
}
/**
* @Then the HTTP status code of responses on all endpoints should be :statusCode
*
* @param int $statusCode
*
* @return void
* @throws Exception
*/
public function theHTTPStatusCodeOfResponsesOnAllEndpointsShouldBe ( int $statusCode ) : void {
$duplicateRemovedStatusCodes = \array_unique ( $this -> lastHttpStatusCodesArray );
if ( \count ( $duplicateRemovedStatusCodes ) === 1 ) {
Assert :: assertSame (
2023-05-02 17:25:56 +05:45
$statusCode ,
2022-12-21 17:22:22 +05:45
\intval ( $duplicateRemovedStatusCodes [ 0 ]),
'Responses did not return expected http status code'
);
$this -> emptyLastHTTPStatusCodesArray ();
} else {
throw new Exception (
'Expected same but found different http status codes of last requested responses.' .
'Found status codes: ' . \implode ( ',' , $this -> lastHttpStatusCodesArray )
);
}
}
/**
2024-06-04 12:45:00 +05:45
* @param string $statusCodes a comma-separated string of expected HTTP status codes
2022-12-21 17:22:22 +05:45
*
* @return void
* @throws Exception
*/
2024-06-07 15:10:59 +05:45
public function checkTheHTTPStatusCodeOfResponsesOnEachEndpoint ( string $statusCodes ) : void {
2024-06-04 12:45:00 +05:45
$expectedStatusCodes = \explode ( ',' , $statusCodes );
$actualStatusCodes = $this -> lastHttpStatusCodesArray ;
$count = \count ( $expectedStatusCodes );
$statusCodesAreAllOk = true ;
if ( $count === \count ( $actualStatusCodes )) {
2022-12-21 17:22:22 +05:45
for ( $i = 0 ; $i < $count ; $i ++ ) {
2024-06-04 12:45:00 +05:45
$expectedCode = ( int ) \trim ( $expectedStatusCodes [ $i ]);
$actualCode = ( int ) $actualStatusCodes [ $i ];
if ( $expectedCode !== $actualCode ) {
$statusCodesAreAllOk = false ;
}
2022-12-21 17:22:22 +05:45
}
} else {
2024-06-04 12:45:00 +05:45
$statusCodesAreAllOk = false ;
2022-12-21 17:22:22 +05:45
}
2024-06-04 12:45:00 +05:45
$this -> emptyLastHTTPStatusCodesArray ();
Assert :: assertTrue (
$statusCodesAreAllOk ,
'Expected HTTP status codes: "' . $statusCodes .
'". Found HTTP status codes: "' . \implode ( ',' , $actualStatusCodes ) . '"'
);
2022-12-21 17:22:22 +05:45
}
2024-06-07 15:10:59 +05:45
/**
* @Then the HTTP status code of responses on each endpoint should be :statusCodes respectively
*
* @param string $statusCodes a comma-separated string of expected HTTP status codes
*
* @return void
* @throws Exception
*/
public function theHTTPStatusCodeOfResponsesOnEachEndpointShouldBe ( string $statusCodes ) : void {
$this -> checkTheHTTPStatusCodeOfResponsesOnEachEndpoint ( $statusCodes );
}
/**
* @Then the HTTP status code of responses on each endpoint should be :ocisStatusCodes on oCIS or :revaStatusCodes on reva
*
* @param string $ocisStatusCodes a comma-separated string of expected HTTP status codes when running on oCIS
* @param string $revaStatusCodes a comma-separated string of expected HTTP status codes when running on reva
*
* @return void
* @throws Exception
*/
public function theHTTPStatusCodeOfResponsesOnEachEndpointShouldBeOcisReva ( string $ocisStatusCodes , string $revaStatusCodes ) : void {
if ( OcisHelper :: isTestingOnReva ()) {
$expectedStatusCodes = $revaStatusCodes ;
} else {
$expectedStatusCodes = $ocisStatusCodes ;
}
$this -> checkTheHTTPStatusCodeOfResponsesOnEachEndpoint ( $expectedStatusCodes );
}
2022-12-21 17:22:22 +05:45
/**
* @Then the OCS status code of responses on each endpoint should be :statusCode respectively
*
* @param string $statusCodes
*
* @return void
* @throws Exception
*/
public function theOCStatusCodeOfResponsesOnEachEndpointShouldBe ( string $statusCodes ) : void {
$statusCodes = \explode ( ',' , $statusCodes );
$count = \count ( $statusCodes );
if ( $count === \count ( $this -> lastOCSStatusCodesArray )) {
for ( $i = 0 ; $i < $count ; $i ++ ) {
Assert :: assertSame (
( int ) \trim ( $statusCodes [ $i ]),
( int ) $this -> lastOCSStatusCodesArray [ $i ],
'Responses did not return expected OCS status code'
);
}
} else {
throw new Exception (
'Expected OCS status codes: "' . \implode ( ',' , $statusCodes ) .
'". Found OCS status codes: "' . \implode ( ',' , $this -> lastOCSStatusCodesArray ) . '"'
);
}
}
/**
* @Then the OCS status code of responses on all endpoints should be :statusCode
*
* @param string $statusCode
*
* @return void
* @throws Exception
*/
public function theOCSStatusCodeOfResponsesOnAllEndpointsShouldBe ( string $statusCode ) : void {
$duplicateRemovedStatusCodes = \array_unique ( $this -> lastOCSStatusCodesArray );
if ( \count ( $duplicateRemovedStatusCodes ) === 1 ) {
Assert :: assertSame (
\intval ( $statusCode ),
\intval ( $duplicateRemovedStatusCodes [ 0 ]),
'Responses did not return expected ocs status code'
);
$this -> emptyLastOCSStatusCodesArray ();
} else {
throw new Exception (
'Expected same but found different ocs status codes of last requested responses.' .
'Found status codes: ' . \implode ( ',' , $this -> lastOCSStatusCodesArray )
);
}
}
/**
* @Then user :user should be able to upload file :source to :destination
*
* @param string $user
* @param string $source
* @param string $destination
*
* @return void
* @throws Exception
*/
public function userShouldBeAbleToUploadFileTo ( string $user , string $source , string $destination ) : void {
$user = $this -> getActualUsername ( $user );
2023-10-11 16:15:31 +05:45
$response = $this -> uploadFile ( $user , $source , $destination );
2022-12-21 17:22:22 +05:45
$this -> theHTTPStatusCodeShouldBe (
[ "201" , "204" ],
2023-10-11 16:15:31 +05:45
"HTTP status code was not 201 or 204 while trying to upload file ' $destination '" ,
$response
2022-12-21 17:22:22 +05:45
);
2024-05-30 17:07:09 +05:45
$this -> checkFileOrFolderExistsForUser ( $user , "file" , $destination );
2022-12-21 17:22:22 +05:45
}
/**
* @Then user :user should not be able to upload file :source to :destination
*
* @param string $user
* @param string $source
* @param string $destination
*
* @return void
* @throws Exception
*/
public function theUserShouldNotBeAbleToUploadFileTo ( string $user , string $source , string $destination ) : void {
$fileAlreadyExists = $this -> fileOrFolderExists ( $user , "file" , $destination );
if ( $fileAlreadyExists ) {
2023-10-11 16:15:31 +05:45
$response = $this -> downloadFileAsUserUsingPassword ( $user , $destination );
$initialContent = ( string ) $response -> getBody ();
2022-12-21 17:22:22 +05:45
}
2023-10-11 16:15:31 +05:45
$response = $this -> uploadFile ( $user , $source , $destination );
$this -> theHTTPStatusCodeShouldBe ([ "403" , "423" ], "" , $response );
2022-12-21 17:22:22 +05:45
if ( $fileAlreadyExists ) {
2023-10-11 16:15:31 +05:45
$response = $this -> downloadFileAsUserUsingPassword ( $user , $destination );
$currentContent = ( string ) $response -> getBody ();
2022-12-21 17:22:22 +05:45
Assert :: assertSame (
$initialContent ,
$currentContent ,
__METHOD__ . " user $user was unexpectedly able to upload $source to $destination - the content has changed:"
);
} else {
2024-05-30 17:07:09 +05:45
$this -> checkFileOrFolderDoesNotExistsForUser ( $user , "file" , $destination );
2022-12-21 17:22:22 +05:45
}
}
/**
* @Then /^the HTTP status code of all upload responses should be between "(\d+)" and "(\d+)"$/
*
* @param int $minStatusCode
* @param int $maxStatusCode
*
* @return void
*/
public function theHTTPStatusCodeOfAllUploadResponsesShouldBeBetween (
int $minStatusCode ,
int $maxStatusCode
) : void {
foreach ( $this -> uploadResponses as $response ) {
Assert :: assertGreaterThanOrEqual (
$minStatusCode ,
$response -> getStatusCode (),
'Response did not return expected status code'
);
Assert :: assertLessThanOrEqual (
$maxStatusCode ,
$response -> getStatusCode (),
'Response did not return expected status code'
);
}
}
2024-05-30 17:07:09 +05:45
/**
* @param string $user
* @param string $destination
* @param string $shouldOrNot
* @param string|null $exceptChunkingType
*
* @return void
*/
public function checkIfFilesExist (
string $user ,
string $destination ,
string $shouldOrNot ,
? string $exceptChunkingType = ''
2022-12-21 17:22:22 +05:45
) : void {
switch ( $exceptChunkingType ) {
case 'old' :
$exceptChunkingSuffix = 'olddav-oldchunking' ;
break ;
case 'new' :
$exceptChunkingSuffix = 'newdav-newchunking' ;
break ;
default :
$exceptChunkingSuffix = '' ;
break ;
}
if ( $shouldOrNot !== "not" ) {
foreach ([ 'old' , 'new' ] as $davVersion ) {
foreach ([ " { $davVersion } dav-regular" , " { $davVersion } dav- { $davVersion } chunking" ] as $suffix ) {
if ( $suffix !== $exceptChunkingSuffix ) {
2024-05-30 17:07:09 +05:45
$this -> checkFileOrFolderExistsForUser (
2022-12-21 17:22:22 +05:45
$user ,
'file' ,
" $destination - $suffix "
);
}
}
}
} else {
foreach ([ 'old' , 'new' ] as $davVersion ) {
foreach ([ " { $davVersion } dav-regular" , " { $davVersion } dav- { $davVersion } chunking" ] as $suffix ) {
if ( $suffix !== $exceptChunkingSuffix ) {
2024-05-30 17:07:09 +05:45
$this -> checkFileOrFolderDoesNotExistsForUser (
2022-12-21 17:22:22 +05:45
$user ,
'file' ,
" $destination - $suffix "
);
}
}
}
}
}
/**
* @Given user :user has uploaded file :destination of size :bytes bytes
*
* @param string $user
* @param string $destination
* @param string $bytes
*
* @return void
* @throws Exception
*/
public function userHasUploadedFileToOfSizeBytes ( string $user , string $destination , string $bytes ) : void {
$user = $this -> getActualUsername ( $user );
2024-05-30 17:07:09 +05:45
$filename = "filespecificSize.txt" ;
$this -> createLocalFileOfSpecificSize ( $filename , $bytes , 'a' );
Assert :: assertFileExists ( $this -> workStorageDirLocation () . $filename );
$response = $this -> uploadFile ( $user , $this -> temporaryStorageSubfolderName () . "/ $filename " , $destination );
2022-12-21 17:22:22 +05:45
$expectedElements = new TableNode ([[ " $destination " ]]);
$this -> checkElementList ( $user , $expectedElements );
2024-05-30 17:07:09 +05:45
$this -> theHTTPStatusCodeShouldBe ([ 201 , 204 ], '' , $response );
2022-12-21 17:22:22 +05:45
}
/**
* @Given user :user has uploaded file :destination ending with :text of size :bytes bytes
*
* @param string $user
* @param string $destination
* @param string $text
* @param string $bytes
*
* @return void
* @throws Exception
*/
public function userHasUploadedFileToEndingWithOfSizeBytes ( string $user , string $destination , string $text , string $bytes ) : void {
2024-05-30 17:07:09 +05:45
$filename = "filespecificSize.txt" ;
$this -> createLocalFileOfSpecificSize ( $filename , $bytes , $text );
Assert :: assertFileExists ( $this -> workStorageDirLocation () . $filename );
$response = $this -> uploadFile ( $user , $this -> temporaryStorageSubfolderName () . "/ $filename " , $destination );
$this -> theHTTPStatusCodeShouldBeBetween ( 200 , 299 , $response );
$this -> removeFile ( $this -> workStorageDirLocation (), $filename );
2022-12-21 17:22:22 +05:45
$expectedElements = new TableNode ([[ " $destination " ]]);
$this -> checkElementList ( $user , $expectedElements );
}
/**
* @param string $user
* @param string|null $content
* @param string $destination
2024-09-18 11:26:20 +05:45
* @param string $spaceId
2023-07-18 14:15:46 +05:45
* @param bool|null $isGivenStep
2022-12-21 17:22:22 +05:45
*
2023-10-11 16:15:31 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
* @throws JsonException
* @throws GuzzleException
*/
public function uploadFileWithContent (
string $user ,
? string $content ,
2023-07-18 14:15:46 +05:45
string $destination ,
2024-09-18 11:26:20 +05:45
? string $spaceId = null ,
? bool $isGivenStep = false ,
2023-10-11 16:15:31 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$user = $this -> getActualUsername ( $user );
$this -> pauseUploadDelete ();
2024-09-17 17:11:20 +05:45
if ( \str_starts_with ( $destination , "Shares/" ) && $this -> getDavPathVersion () === WebDavHelper :: DAV_VERSION_SPACES ) {
$spaceId = $this -> spacesContext -> getSpaceIdByName ( $user , "Shares" );
$destination = \str_replace ( "Shares/" , "" , $destination );
}
2023-10-11 16:15:31 +05:45
$response = $this -> makeDavRequest (
2022-12-21 17:22:22 +05:45
$user ,
"PUT" ,
$destination ,
[],
2023-07-18 14:15:46 +05:45
$content ,
2024-09-18 11:26:20 +05:45
$spaceId ,
2023-07-18 14:15:46 +05:45
"files" ,
null ,
false ,
null ,
[],
null ,
2024-09-18 11:26:20 +05:45
$isGivenStep ,
2022-12-21 17:22:22 +05:45
);
$this -> lastUploadDeleteTime = \time ();
2023-10-11 16:15:31 +05:45
return $response ;
2022-12-21 17:22:22 +05:45
}
/**
* @When user :user uploads file with content :content to :destination using the WebDAV API
*
* @param string $user
* @param string|null $content
* @param string $destination
*
* @return void
* @throws GuzzleException
* @throws JsonException
*/
public function userUploadsAFileWithContentTo (
string $user ,
? string $content ,
string $destination
) : void {
2023-10-11 16:15:31 +05:45
$response = $this -> uploadFileWithContent ( $user , $content , $destination );
$this -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
$this -> pushToLastHttpStatusCodesArray ();
}
/**
* @When /^user "([^"]*)" uploads the following files with content "([^"]*)"$/
*
* @param string $user
* @param string|null $content
* @param TableNode $table
*
* @return void
* @throws Exception|GuzzleException
*/
public function userUploadsFollowingFilesWithContentTo (
string $user ,
? string $content ,
TableNode $table
) : void {
$this -> verifyTableNodeColumns ( $table , [ "path" ]);
$paths = $table -> getHash ();
foreach ( $paths as $destination ) {
2023-10-11 16:15:31 +05:45
$response = $this -> uploadFileWithContent ( $user , $content , $destination [ "path" ]);
$this -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
$this -> pushToLastStatusCodesArrays ();
}
}
/**
* @When user :user uploads file :source to :destination with mtime :mtime using the WebDAV API
*
* @param string $user
* @param string $source
* @param string $destination
2023-04-21 14:59:50 +05:45
* @param string $mtime Time in human-readable format is taken as input which is converted into milliseconds that is used by API
2023-07-18 14:15:46 +05:45
* @param bool|null $isGivenStep
2022-12-21 17:22:22 +05:45
*
* @return void
* @throws Exception
*/
public function userUploadsFileToWithMtimeUsingTheWebdavApi (
string $user ,
string $source ,
string $destination ,
2023-07-18 14:15:46 +05:45
string $mtime ,
? bool $isGivenStep = false
2022-12-21 17:22:22 +05:45
) : void {
$mtime = new DateTime ( $mtime );
$mtime = $mtime -> format ( 'U' );
$user = $this -> getActualUsername ( $user );
$this -> response = UploadHelper :: upload (
$this -> getBaseUrl (),
$user ,
$this -> getPasswordForUser ( $user ),
$this -> acceptanceTestsDirLocation () . $source ,
$destination ,
$this -> getStepLineRef (),
[ "X-OC-Mtime" => $mtime ],
2023-07-18 14:15:46 +05:45
$this -> getDavPathVersion (),
null ,
1 ,
$isGivenStep
);
}
/**
2024-11-19 12:19:07 +05:45
* @Given user :user has uploaded file :source to :destination with mtime :mtime
2023-07-18 14:15:46 +05:45
*
* @param string $user
* @param string $source
* @param string $destination
* @param string $mtime Time in human-readable format is taken as input which is converted into milliseconds that is used by API
*
* @return void
* @throws Exception
*/
public function userHasUploadedFileToWithMtimeUsingTheWebdavApi (
string $user ,
string $source ,
string $destination ,
string $mtime
) : void {
2024-05-30 17:07:09 +05:45
$mtime = new DateTime ( $mtime );
$mtime = $mtime -> format ( 'U' );
$user = $this -> getActualUsername ( $user );
$response = UploadHelper :: upload (
$this -> getBaseUrl (),
2023-07-18 14:15:46 +05:45
$user ,
2024-05-30 17:07:09 +05:45
$this -> getPasswordForUser ( $user ),
$this -> acceptanceTestsDirLocation () . $source ,
2023-07-18 14:15:46 +05:45
$destination ,
2024-05-30 17:07:09 +05:45
$this -> getStepLineRef (),
[ "X-OC-Mtime" => $mtime ],
$this -> getDavPathVersion (),
null ,
1 ,
2023-07-18 14:15:46 +05:45
true
2022-12-21 17:22:22 +05:45
);
2024-05-30 17:07:09 +05:45
$this -> theHTTPStatusCodeShouldBe (
[ "201" , "204" ],
"" ,
$response
);
2022-12-21 17:22:22 +05:45
}
/**
* @Then as :user the mtime of the file :resource should be :mtime
*
* @param string $user
* @param string $resource
* @param string $mtime
*
* @return void
* @throws Exception
*/
public function theMtimeOfTheFileShouldBe (
string $user ,
string $resource ,
string $mtime
) : void {
$user = $this -> getActualUsername ( $user );
$password = $this -> getPasswordForUser ( $user );
$baseUrl = $this -> getBaseUrl ();
$mtime = new DateTime ( $mtime );
$mtime = $mtime -> format ( 'U' );
Assert :: assertEquals (
$mtime ,
WebDavHelper :: getMtimeOfResource (
$user ,
$password ,
$baseUrl ,
$resource ,
$this -> getStepLineRef (),
$this -> getDavPathVersion ()
)
);
}
/**
* @Given user :user has uploaded file with content :content to :destination
*
* @param string $user
* @param string|null $content
* @param string $destination
*
2024-05-30 17:07:09 +05:45
* @return void
2023-10-11 16:15:31 +05:45
* @throws GuzzleException
* @throws JsonException
2022-12-21 17:22:22 +05:45
*/
public function userHasUploadedAFileWithContentTo (
string $user ,
? string $content ,
string $destination
) : array {
$user = $this -> getActualUsername ( $user );
2024-09-18 11:26:20 +05:45
$response = $this -> uploadFileWithContent ( $user , $content , $destination , null , true );
2022-12-21 17:22:22 +05:45
$this -> theHTTPStatusCodeShouldBe (
[ "201" , "204" ],
2023-10-11 16:15:31 +05:45
"HTTP status code was not 201 or 204 while trying to upload file ' $destination ' for user ' $user '" ,
$response
2022-12-21 17:22:22 +05:45
);
2023-10-11 16:15:31 +05:45
return $response -> getHeader ( 'oc-fileid' );
2022-12-21 17:22:22 +05:45
}
/**
* @Given /^user "([^"]*)" has uploaded the following files with content "([^"]*)"$/
*
* @param string $user
* @param string|null $content
* @param TableNode $table
*
2023-10-11 16:15:31 +05:45
* @return void
* @throws Exception|GuzzleException
2022-12-21 17:22:22 +05:45
*/
public function userHasUploadedFollowingFilesWithContent (
string $user ,
? string $content ,
TableNode $table
2023-10-11 16:15:31 +05:45
) : void {
2022-12-21 17:22:22 +05:45
$this -> verifyTableNodeColumns ( $table , [ "path" ]);
$files = $table -> getHash ();
foreach ( $files as $destination ) {
2024-05-30 17:07:09 +05:45
$destination = $destination [ 'path' ];
$user = $this -> getActualUsername ( $user );
2024-09-18 11:26:20 +05:45
$response = $this -> uploadFileWithContent ( $user , $content , $destination , null , true );
2024-05-30 17:07:09 +05:45
$this -> theHTTPStatusCodeShouldBe (
[ "201" , "204" ],
"HTTP status code was not 201 or 204 while trying to upload file ' $destination ' for user ' $user '" ,
$response
);
2022-12-21 17:22:22 +05:45
}
}
/**
* @When /^user "([^"]*)" downloads the following files using the WebDAV API$/
*
* @param string $user
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function userDownloadsFollowingFiles (
string $user ,
TableNode $table
) : void {
$this -> verifyTableNodeColumns ( $table , [ "path" ]);
$files = $table -> getHash ();
$this -> emptyLastHTTPStatusCodesArray ();
foreach ( $files as $fileName ) {
2023-10-11 16:15:31 +05:45
$response = $this -> downloadFileAsUserUsingPassword ( $user , $fileName [ "path" ]);
$this -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
$this -> pushToLastStatusCodesArrays ();
}
}
/**
* @When user :user uploads a file with content :content and mtime :mtime to :destination using the WebDAV API
*
* @param string $user
* @param string|null $content
* @param string $mtime
* @param string $destination
*
* @return void
* @throws Exception
*/
public function userUploadsAFileWithContentAndMtimeTo (
string $user ,
? string $content ,
string $mtime ,
string $destination
) : void {
$user = $this -> getActualUsername ( $user );
$mtime = new DateTime ( $mtime );
$mtime = $mtime -> format ( 'U' );
2023-10-11 16:15:31 +05:45
$response = $this -> makeDavRequest (
2022-12-21 17:22:22 +05:45
$user ,
"PUT" ,
$destination ,
[ "X-OC-Mtime" => $mtime ],
$content
);
2023-10-11 16:15:31 +05:45
$this -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @param string $user
* @param string $checksum
* @param string|null $content
* @param string $destination
2023-07-18 14:15:46 +05:45
* @param bool|null $isGivenStep
2024-09-18 11:26:20 +05:45
* @param string|null $spaceId
2022-12-21 17:22:22 +05:45
*
2023-10-11 16:15:31 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*/
2023-10-11 16:15:31 +05:45
public function uploadFileWithChecksumAndContent (
2022-12-21 17:22:22 +05:45
string $user ,
string $checksum ,
? string $content ,
2023-07-18 14:15:46 +05:45
string $destination ,
2024-09-18 11:26:20 +05:45
? bool $isGivenStep = false ,
? string $spaceId = null
2023-10-11 16:15:31 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$this -> pauseUploadDelete ();
2023-10-11 16:15:31 +05:45
$response = $this -> makeDavRequest (
2022-12-21 17:22:22 +05:45
$user ,
"PUT" ,
$destination ,
[ 'OC-Checksum' => $checksum ],
2023-07-18 14:15:46 +05:45
$content ,
2024-09-18 11:26:20 +05:45
$spaceId ,
2023-07-18 14:15:46 +05:45
"files" ,
null ,
false ,
null ,
[],
null ,
$isGivenStep
2022-12-21 17:22:22 +05:45
);
$this -> lastUploadDeleteTime = \time ();
2023-10-11 16:15:31 +05:45
return $response ;
}
/**
* @When user :user uploads file with checksum :checksum and content :content to :destination using the WebDAV API
*
* @param string $user
* @param string $checksum
* @param string $content
* @param string $destination
*
* @return void
*/
public function userUploadsAFileWithChecksumAndContentTo (
string $user ,
string $checksum ,
string $content ,
string $destination
) : void {
$response = $this -> uploadFileWithChecksumAndContent ( $user , $checksum , $content , $destination );
$this -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @Given user :user has uploaded file with checksum :checksum and content :content to :destination
*
* @param string $user
* @param string $checksum
* @param string|null $content
* @param string $destination
*
* @return void
*/
public function userHasUploadedAFileWithChecksumAndContentTo (
string $user ,
string $checksum ,
? string $content ,
string $destination
) : void {
2023-10-11 16:15:31 +05:45
$response = $this -> uploadFileWithChecksumAndContent (
2022-12-21 17:22:22 +05:45
$user ,
$checksum ,
$content ,
2023-07-18 14:15:46 +05:45
$destination ,
true
2022-12-21 17:22:22 +05:45
);
$this -> theHTTPStatusCodeShouldBe (
[ "201" , "204" ],
2023-10-11 16:15:31 +05:45
"HTTP status code was not 201 or 204 while trying to upload file with checksum ' $checksum ' to ' $destination ' for user ' $user '" ,
$response
2022-12-21 17:22:22 +05:45
);
}
/**
* @Then /^user "([^"]*)" should be able to delete (file|folder|entry) "([^"]*)"$/
*
* @param string $user
* @param string $entry
* @param string $source
*
* @return void
* @throws Exception
*/
public function userShouldBeAbleToDeleteEntry ( string $user , string $entry , string $source ) : void {
$user = $this -> getActualUsername ( $user );
2024-05-30 17:07:09 +05:45
$this -> checkFileOrFolderExistsForUser ( $user , $entry , $source );
2023-10-11 16:15:31 +05:45
$this -> deleteFile ( $user , $source );
2024-05-30 17:07:09 +05:45
$this -> checkFileOrFolderDoesNotExistsForUser ( $user , $entry , $source );
2022-12-21 17:22:22 +05:45
}
/**
* @Then /^user "([^"]*)" should not be able to delete (file|folder|entry) "([^"]*)"$/
*
* @param string $user
* @param string $entry
* @param string $source
*
* @return void
* @throws Exception
*/
public function theUserShouldNotBeAbleToDeleteEntry ( string $user , string $entry , string $source ) : void {
2024-05-30 17:07:09 +05:45
$this -> checkFileOrFolderExistsForUser ( $user , $entry , $source );
2023-10-11 16:15:31 +05:45
$this -> deleteFile ( $user , $source );
2024-05-30 17:07:09 +05:45
$this -> checkFileOrFolderExistsForUser ( $user , $entry , $source );
2022-12-21 17:22:22 +05:45
}
/**
* @param string $user
2023-06-30 12:22:42 +05:45
* @param string $resource
2022-12-21 17:22:22 +05:45
*
* @return void
*/
2023-10-11 16:15:31 +05:45
public function deleteFile ( string $user , string $resource ) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$user = $this -> getActualUsername ( $user );
$this -> pauseUploadDelete ();
2023-10-11 16:15:31 +05:45
$response = $this -> makeDavRequest ( $user , 'DELETE' , $resource , []);
2022-12-21 17:22:22 +05:45
$this -> lastUploadDeleteTime = \time ();
2023-10-11 16:15:31 +05:45
return $response ;
}
/**
* @When user :user deletes file/folder :resource using the WebDAV API
*
* @param string $user
* @param string $resource
*
* @return void
*/
public function userDeletesFile ( string $user , string $resource ) : void {
$response = $this -> deleteFile ( $user , $resource );
$this -> setResponse ( $response );
$this -> pushToLastStatusCodesArrays ();
2022-12-21 17:22:22 +05:45
}
2024-08-07 14:08:31 +05:45
/**
2024-09-17 17:11:20 +05:45
* @When user :user deletes file :filename from space :space using file-id :fileId
2024-08-07 14:08:31 +05:45
*
* @param string $user
* @param string $filename
* @param string $space
2024-09-17 17:11:20 +05:45
* @param string $fileId
2024-08-07 14:08:31 +05:45
*
* @return void
*/
2024-09-17 17:11:20 +05:45
public function userDeletesFileFromSpaceUsingFileIdPath ( string $user , string $filename , string $space , string $fileId ) : void {
$baseUrl = $this -> getBaseUrl ();
$davPath = WebDavHelper :: getDavPath ( $this -> getDavPathVersion ());
2024-08-07 14:08:31 +05:45
$user = $this -> getActualUsername ( $user );
$password = $this -> getPasswordForUser ( $user );
2024-09-17 17:11:20 +05:45
$fullUrl = " $baseUrl / $davPath / $fileId " ;
2024-08-07 14:08:31 +05:45
$response = HttpRequestHelper :: sendRequest (
2024-09-17 17:11:20 +05:45
$fullUrl ,
2024-08-07 14:08:31 +05:45
null ,
'DELETE' ,
$user ,
$password
);
$this -> setResponse ( $response );
$this -> pushToLastStatusCodesArrays ();
}
2024-10-16 12:02:26 +05:45
/**
* @Given user :user has updated a file with content :content using file-id :fileId
*
* @param string $user
* @param string $content
* @param string $fileId
*
* @return void
*/
public function userHasUpdatedAFileWithContentUsingFileId ( string $user , string $content , string $fileId ) : void {
$baseUrl = $this -> getBaseUrl ();
$sourceDavPath = WebdavHelper :: getDavPath ( $this -> getDavPathVersion ());
$fullUrl = " $baseUrl / $sourceDavPath / $fileId " ;
$response = HttpRequestHelper :: sendRequest (
$fullUrl ,
$this -> getStepLineRef (),
'PUT' ,
$user ,
$this -> getPasswordForUser ( $user ),
null ,
$content
);
$this -> theHTTPStatusCodeShouldBe ( '204' , '' , $response );
}
2022-12-21 17:22:22 +05:45
/**
2023-06-30 12:22:42 +05:45
* @Given /^user "([^"]*)" has deleted (?:file|folder|entity) "([^"]*)"$/
2022-12-21 17:22:22 +05:45
*
* @param string $user
2023-06-30 12:22:42 +05:45
* @param string $resource
2022-12-21 17:22:22 +05:45
*
* @return void
2023-05-31 16:00:10 +05:45
* @throws Exception
2022-12-21 17:22:22 +05:45
*/
2023-06-30 12:22:42 +05:45
public function userHasDeletedResource ( string $user , string $resource ) : void {
2022-12-21 17:22:22 +05:45
$user = $this -> getActualUsername ( $user );
2023-10-11 16:15:31 +05:45
$response = $this -> deleteFile ( $user , $resource );
2022-12-21 17:22:22 +05:45
// If the file or folder was there and got deleted then we get a 204
// That is good and the expected status
2023-05-02 17:25:56 +05:45
// If the file or folder was already not there then we get a 404
2022-12-21 17:22:22 +05:45
// That is not expected. Scenarios that use "Given user has deleted..."
// should only be using such steps when it is a file that exists and needs
// to be deleted.
$this -> theHTTPStatusCodeShouldBe (
[ "204" ],
2023-10-11 16:15:31 +05:45
"HTTP status code was not 204 while trying to delete resource ' $resource ' for user ' $user '" ,
$response
2022-12-21 17:22:22 +05:45
);
}
/**
2023-06-30 12:22:42 +05:45
* @Given /^user "([^"]*)" has deleted the following (?:files|folders|resources)$/
2022-12-21 17:22:22 +05:45
*
* @param string $user
* @param TableNode $table
*
* @return void
* @throws Exception
*/
2023-06-30 12:22:42 +05:45
public function userHasDeletedFollowingFiles ( string $user , TableNode $table ) : void {
2022-12-21 17:22:22 +05:45
$this -> verifyTableNodeColumns ( $table , [ "path" ]);
$paths = $table -> getHash ();
foreach ( $paths as $file ) {
2024-05-30 17:07:09 +05:45
$file = $file [ "path" ];
$user = $this -> getActualUsername ( $user );
$response = $this -> deleteFile ( $user , $file );
$this -> theHTTPStatusCodeShouldBe (
[ "204" ],
"HTTP status code was not 204 while trying to delete resource ' $file ' for user ' $user '" ,
$response
);
2022-12-21 17:22:22 +05:45
}
}
/**
2023-06-30 12:22:42 +05:45
* @When /^user "([^"]*)" deletes the following (?:files|folders)$/
2022-12-21 17:22:22 +05:45
*
* @param string $user
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function userDeletesFollowingFiles ( string $user , TableNode $table ) : void {
$user = $this -> getActualUsername ( $user );
$this -> verifyTableNodeColumns ( $table , [ "path" ]);
$paths = $table -> getHash ();
foreach ( $paths as $file ) {
2024-05-30 17:07:09 +05:45
$response = $this -> deleteFile ( $user , $file [ "path" ]);
$this -> setResponse ( $response );
$this -> pushToLastStatusCodesArrays ();
2022-12-21 17:22:22 +05:45
$this -> pushToLastStatusCodesArrays ();
}
}
/**
2023-06-30 12:22:42 +05:45
* @When /^user "([^"]*)" deletes these (?:files|folders|entries) without delays using the WebDAV API$/
2022-12-21 17:22:22 +05:45
*
* @param string $user
* @param TableNode $table of files or folders to delete
*
* @return void
* @throws Exception
*/
public function userDeletesFilesFoldersWithoutDelays ( string $user , TableNode $table ) : void {
$user = $this -> getActualUsername ( $user );
$this -> verifyTableNodeColumnsCount ( $table , 1 );
foreach ( $table -> getTable () as $entry ) {
$entryName = $entry [ 0 ];
$this -> response = $this -> makeDavRequest ( $user , 'DELETE' , $entryName , []);
$this -> pushToLastStatusCodesArrays ();
}
$this -> lastUploadDeleteTime = \time ();
}
/**
* @When user :user creates folder :destination using the WebDAV API
*
* @param string $user
* @param string $destination
*
* @return void
2023-10-11 16:15:31 +05:45
* @throws JsonException
* @throws GuzzleException
2022-12-21 17:22:22 +05:45
*/
2023-10-03 18:31:24 +05:45
public function userCreatesFolder ( string $user , string $destination ) : void {
$response = $this -> createFolder ( $user , $destination );
$this -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @Given user :user has created folder :destination
*
* @param string $user
* @param string $destination
*
* @return void
* @throws JsonException
* @throws GuzzleException
*/
public function userHasCreatedFolder ( string $user , string $destination ) : void {
2023-10-11 16:15:31 +05:45
$user = $this -> getActualUsername ( $user );
2023-10-03 18:31:24 +05:45
$response = $this -> createFolder ( $user , $destination , true );
2022-12-21 17:22:22 +05:45
$this -> theHTTPStatusCodeShouldBe (
[ "201" , "204" ],
2023-10-03 18:31:24 +05:45
"HTTP status code was not 201 or 204 while trying to create folder ' $destination ' for user ' $user '" ,
$response
2022-12-21 17:22:22 +05:45
);
}
/**
* @Given admin has created folder :destination
*
* @param string $destination
*
* @return void
* @throws JsonException
* @throws GuzzleException
*/
public function adminHasCreatedFolder ( string $destination ) : void {
$admin = $this -> getAdminUsername ();
Assert :: assertEquals (
"admin" ,
$admin ,
__METHOD__ . "The provided user is not admin but '" . $admin . "'"
);
2023-10-05 11:42:59 +05:45
$response = $this -> createFolder ( $admin , $destination , true );
2022-12-21 17:22:22 +05:45
$this -> theHTTPStatusCodeShouldBe (
[ "201" , "204" ],
2023-10-03 18:31:24 +05:45
"HTTP status code was not 201 or 204 while trying to create folder ' $destination ' for admin ' $admin '" ,
$response
2022-12-21 17:22:22 +05:45
);
$this -> adminResources [] = $destination ;
}
/**
* @Given /^user "([^"]*)" has created the following folders$/
*
* @param string $user
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function userHasCreatedFollowingFolders ( string $user , TableNode $table ) : void {
$this -> verifyTableNodeColumns ( $table , [ "path" ]);
$paths = $table -> getHash ();
foreach ( $paths as $path ) {
2024-05-30 17:07:09 +05:45
$destination = $path [ "path" ];
$user = $this -> getActualUsername ( $user );
$response = $this -> createFolder ( $user , $destination , true );
$this -> theHTTPStatusCodeShouldBe (
[ "201" , "204" ],
"HTTP status code was not 201 or 204 while trying to create folder ' $destination ' for user ' $user '" ,
$response
);
2022-12-21 17:22:22 +05:45
}
}
/**
* @Then user :user should be able to create folder :destination
*
* @param string $user
* @param string $destination
*
* @return void
* @throws Exception
*/
public function userShouldBeAbleToCreateFolder ( string $user , string $destination ) : void {
2024-05-30 17:07:09 +05:45
$user = $this -> getActualUsername ( $user );
$response = $this -> createFolder ( $user , $destination , true );
$this -> theHTTPStatusCodeShouldBe (
[ "201" , "204" ],
"HTTP status code was not 201 or 204 while trying to create folder ' $destination ' for user ' $user '" ,
$response
);
$this -> checkFileOrFolderExistsForUser (
2022-12-21 17:22:22 +05:45
$user ,
"folder" ,
$destination
);
}
2024-07-08 17:22:25 +05:45
/**
* @Then user :user should be able to create folder :destination using password :password
*
* @param string $user
* @param string $destination
* @param string $password
*
* @return void
* @throws Exception
*/
public function userShouldBeAbleToCreateFolderUsingPassword ( string $user , string $destination , string $password ) : void {
$user = $this -> getActualUsername ( $user );
$response = $this -> createFolder ( $user , $destination , true , $password );
$this -> theHTTPStatusCodeShouldBe (
[ "201" , "204" ],
"HTTP status code was not 201 or 204 while trying to create folder ' $destination ' for user ' $user '" ,
$response
);
$this -> checkFileOrFolderExistsForUser (
$user ,
"folder" ,
$destination
);
}
2022-12-21 17:22:22 +05:45
/**
* @Then user :user should not be able to create folder :destination
*
* @param string $user
* @param string $destination
*
* @return void
* @throws Exception
*/
public function userShouldNotBeAbleToCreateFolder ( string $user , string $destination ) : void {
$user = $this -> getActualUsername ( $user );
2023-10-03 18:31:24 +05:45
$response = $this -> createFolder ( $user , $destination );
2023-10-11 16:15:31 +05:45
$this -> theHTTPStatusCodeShouldBeBetween ( 400 , 499 , $response );
2024-05-30 17:07:09 +05:45
$this -> checkFileOrFolderDoesNotExistsForUser (
2023-10-11 16:15:31 +05:45
$user ,
"folder" ,
$destination
2022-12-21 17:22:22 +05:45
);
}
2024-07-08 17:22:25 +05:45
/**
* @Then user :user should not be able to create folder :destination using password :password
*
* @param string $user
* @param string $destination
* @param string $password
*
* @return void
* @throws Exception
*/
public function userShouldNotBeAbleToCreateFolderUsingPassword ( string $user , string $destination , string $password ) : void {
$user = $this -> getActualUsername ( $user );
$response = $this -> createFolder ( $user , $destination , false , $password );
$this -> theHTTPStatusCodeShouldBeBetween ( 400 , 499 , $response );
$this -> checkFileOrFolderDoesNotExistsForUser (
$user ,
"folder" ,
$destination
);
}
2022-12-21 17:22:22 +05:45
/**
* Old style chunking upload
*
* @When user :user uploads the following :total chunks to :file with old chunking and using the WebDAV API
*
* @param string $user
* @param string $total
* @param string $file
* @param TableNode $chunkDetails table of 2 columns, chunk number and chunk
* content with column headings, e.g.
* | number | content |
* | 1 | first data |
* | 2 | followed by second data |
* Chunks may be numbered out-of-order if desired.
*
* @return void
* @throws Exception
*/
public function userUploadsTheFollowingTotalChunksUsingOldChunking (
string $user ,
string $total ,
string $file ,
TableNode $chunkDetails
) : void {
$this -> verifyTableNodeColumns ( $chunkDetails , [ 'number' , 'content' ]);
foreach ( $chunkDetails -> getHash () as $chunkDetail ) {
$chunkNumber = ( int ) $chunkDetail [ 'number' ];
$chunkContent = $chunkDetail [ 'content' ];
2024-05-30 17:07:09 +05:45
$this -> setResponse ( $this -> userUploadChunkedFile ( $user , $chunkNumber , ( int ) $total , $chunkContent , $file ));
2022-12-21 17:22:22 +05:45
}
}
/**
* Old style chunking upload
*
* @When user :user uploads the following chunks to :file with old chunking and using the WebDAV API
*
* @param string $user
* @param string $file
* @param TableNode $chunkDetails table of 2 columns, chunk number and chunk
* content with column headings, e.g.
* | number | content |
* | 1 | first data |
* | 2 | followed by second data |
* Chunks may be numbered out-of-order if desired.
*
* @return void
* @throws Exception
*/
public function userUploadsTheFollowingChunksUsingOldChunking (
string $user ,
string $file ,
TableNode $chunkDetails
) : void {
2024-05-30 17:07:09 +05:45
$total = ( string ) \count ( $chunkDetails -> getHash ());
$this -> verifyTableNodeColumns ( $chunkDetails , [ 'number' , 'content' ]);
foreach ( $chunkDetails -> getHash () as $chunkDetail ) {
$chunkNumber = ( int ) $chunkDetail [ 'number' ];
$chunkContent = $chunkDetail [ 'content' ];
$this -> setResponse ( $this -> userUploadChunkedFile ( $user , $chunkNumber , ( int ) $total , $chunkContent , $file ));
}
2022-12-21 17:22:22 +05:45
}
/**
* Old style chunking upload
*
* @param string $user
* @param int $num
* @param int $total
* @param string|null $data
* @param string $destination
2023-07-18 14:15:46 +05:45
* @param bool|null $isGivenStep
2022-12-21 17:22:22 +05:45
*
2023-10-11 16:15:31 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*/
2023-10-11 16:15:31 +05:45
public function userUploadChunkedFile (
2022-12-21 17:22:22 +05:45
string $user ,
int $num ,
int $total ,
? string $data ,
2023-07-18 14:15:46 +05:45
string $destination ,
? bool $isGivenStep = false
2023-10-11 16:15:31 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$user = $this -> getActualUsername ( $user );
$num -= 1 ;
$file = " $destination -chunking-42- $total - $num " ;
$this -> pauseUploadDelete ();
2023-10-11 16:15:31 +05:45
$response = $this -> makeDavRequest (
2022-12-21 17:22:22 +05:45
$user ,
'PUT' ,
$file ,
[ 'OC-Chunked' => '1' ],
$data ,
2024-09-18 11:26:20 +05:45
null ,
2024-11-07 14:12:16 +05:45
"files" ,
2023-07-18 14:15:46 +05:45
null ,
false ,
null ,
[],
null ,
$isGivenStep
2022-12-21 17:22:22 +05:45
);
$this -> lastUploadDeleteTime = \time ();
2023-10-11 16:15:31 +05:45
return $response ;
}
2022-12-21 17:22:22 +05:45
/**
* New style chunking upload
*
* @param string $user
* @param string $type "asynchronously" or empty
* @param string $file
* @param TableNode $chunkDetails table of 2 columns, chunk number and chunk
* content with column headings, e.g.
* | number | content |
* | 1 | first data |
* | 2 | second data |
* Chunks may be numbered out-of-order if desired.
2023-07-18 14:15:46 +05:45
* @param bool|null $isGivenStep
2022-12-21 17:22:22 +05:45
*
* @return void
* @throws Exception
*/
public function uploadTheFollowingChunksUsingNewChunking (
string $user ,
string $type ,
string $file ,
TableNode $chunkDetails ,
2023-07-18 14:15:46 +05:45
? bool $isGivenStep = false
2022-12-21 17:22:22 +05:45
) : void {
$user = $this -> getActualUsername ( $user );
$async = false ;
if ( $type === "asynchronously" ) {
$async = true ;
}
$this -> verifyTableNodeColumns ( $chunkDetails , [ "number" , "content" ]);
$this -> userUploadsChunksUsingNewChunking (
$user ,
$file ,
'chunking-42' ,
$chunkDetails -> getHash (),
$async ,
2023-07-18 14:15:46 +05:45
$isGivenStep
2022-12-21 17:22:22 +05:45
);
}
/**
* New style chunking upload
*
* @param string $user
* @param string $file
* @param string $chunkingId
* @param array $chunkDetails of chunks of the file. Each array entry is
* itself an array of 2 items:
* [number] the chunk number
* [content] data content of the chunk
* Chunks may be numbered out-of-order if desired.
* @param bool $async use asynchronous MOVE at the end or not
2023-07-18 14:15:46 +05:45
* @param bool $isGivenStep
2022-12-21 17:22:22 +05:45
*
* @return void
*/
public function userUploadsChunksUsingNewChunking (
string $user ,
string $file ,
string $chunkingId ,
array $chunkDetails ,
bool $async = false ,
2023-07-18 14:15:46 +05:45
bool $isGivenStep = false
2022-12-21 17:22:22 +05:45
) : void {
$this -> pauseUploadDelete ();
2023-07-18 14:15:46 +05:45
if ( $isGivenStep ) {
2024-05-30 17:07:09 +05:45
$response = $this -> userCreateANewChunkingUploadWithId ( $user , $chunkingId , true );
$this -> theHTTPStatusCodeShouldBeBetween ( 200 , 299 , $response );
2022-12-21 17:22:22 +05:45
} else {
2024-05-30 17:07:09 +05:45
$this -> setResponse ( $this -> userCreateANewChunkingUploadWithId ( $user , $chunkingId ));
2022-12-21 17:22:22 +05:45
}
foreach ( $chunkDetails as $chunkDetail ) {
$chunkNumber = ( int ) $chunkDetail [ 'number' ];
$chunkContent = $chunkDetail [ 'content' ];
2023-07-18 14:15:46 +05:45
if ( $isGivenStep ) {
2024-05-30 17:07:09 +05:45
$response = $this -> userUploadNewChunkFileOfWithToId ( $user , $chunkNumber , $chunkContent , $chunkingId , true );
$this -> theHTTPStatusCodeShouldBeBetween ( 200 , 299 , $response );
2022-12-21 17:22:22 +05:45
} else {
2024-05-30 17:07:09 +05:45
$response = $this -> userUploadNewChunkFileOfWithToId ( $user , $chunkNumber , $chunkContent , $chunkingId );
$this -> setResponse ( $response );
$this -> pushToLastStatusCodesArrays ();
2022-12-21 17:22:22 +05:45
}
}
$headers = [];
if ( $async === true ) {
$headers = [ 'OC-LazyOps' => 'true' ];
}
2023-10-11 16:15:31 +05:45
$response = $this -> moveNewDavChunkToFinalFile ( $user , $chunkingId , $file , $headers , $isGivenStep );
2023-07-18 14:15:46 +05:45
if ( $isGivenStep ) {
2023-10-11 16:15:31 +05:45
$this -> theHTTPStatusCodeShouldBeBetween ( 200 , 299 , $response );
} else {
$this -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
$this -> lastUploadDeleteTime = \time ();
}
/**
*
* @param string $user
* @param string $id
2023-07-18 14:15:46 +05:45
* @param bool|null $isGivenStep
2022-12-21 17:22:22 +05:45
*
2023-10-11 16:15:31 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*/
2023-10-11 16:15:31 +05:45
public function userCreateANewChunkingUploadWithId (
2023-07-18 14:15:46 +05:45
string $user ,
string $id ,
? bool $isGivenStep = false
2023-10-11 16:15:31 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$user = $this -> getActualUsername ( $user );
$destination = "/uploads/ $user / $id " ;
2023-10-11 16:15:31 +05:45
return $this -> makeDavRequest (
2022-12-21 17:22:22 +05:45
$user ,
'MKCOL' ,
$destination ,
[],
null ,
2024-10-08 14:46:33 +05:45
null ,
2023-07-18 14:15:46 +05:45
"uploads" ,
null ,
false ,
null ,
[],
null ,
$isGivenStep
2022-12-21 17:22:22 +05:45
);
}
/**
* @param string $user
* @param int $num
* @param string|null $data
* @param string $id
2023-07-18 14:15:46 +05:45
* @param bool|null $isGivenStep
2022-12-21 17:22:22 +05:45
*
2023-10-11 16:15:31 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*/
2023-10-11 16:15:31 +05:45
public function userUploadNewChunkFileOfWithToId (
2023-07-18 14:15:46 +05:45
string $user ,
int $num ,
? string $data ,
string $id ,
? bool $isGivenStep = false
2023-10-11 16:15:31 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$user = $this -> getActualUsername ( $user );
$destination = "/uploads/ $user / $id / $num " ;
2023-10-11 16:15:31 +05:45
return $this -> makeDavRequest (
2022-12-21 17:22:22 +05:45
$user ,
'PUT' ,
$destination ,
[],
$data ,
2024-10-08 14:46:33 +05:45
null ,
2023-07-18 14:15:46 +05:45
"uploads" ,
null ,
false ,
null ,
[],
null ,
$isGivenStep
2022-12-21 17:22:22 +05:45
);
2023-10-11 16:15:31 +05:45
}
/**
* @param string $user
* @param string $id
* @param string $type "asynchronously" or empty
* @param string $dest
*
* @return ResponseInterface
*/
public function userMoveNewChunkFileWithIdToMychunkedfile (
string $user ,
string $id ,
string $type ,
string $dest
) : ResponseInterface {
$headers = [];
if ( $type === "asynchronously" ) {
$headers = [ 'OC-LazyOps' => 'true' ];
}
return $this -> moveNewDavChunkToFinalFile ( $user , $id , $dest , $headers );
2022-12-21 17:22:22 +05:45
}
/**
* @When /^user "([^"]*)" moves new chunk file with id "([^"]*)"\s?(asynchronously|) to "([^"]*)" using the WebDAV API$/
*
* @param string $user
* @param string $id
* @param string $type "asynchronously" or empty
* @param string $dest
*
* @return void
*/
public function userMovesNewChunkFileWithIdToMychunkedfile (
string $user ,
string $id ,
string $type ,
string $dest
) : void {
2023-10-11 16:15:31 +05:45
$this -> setResponse ( $this -> userMoveNewChunkFileWithIdToMychunkedfile ( $user , $id , $type , $dest ));
2022-12-21 17:22:22 +05:45
}
/**
* @param string $user
* @param string $id
* @param string $type "asynchronously" or empty
* @param string $dest
* @param int $size
*
2023-10-11 16:15:31 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*/
2023-10-11 16:15:31 +05:45
public function userMoveNewChunkFileWithIdToMychunkedfileWithSize (
2022-12-21 17:22:22 +05:45
string $user ,
string $id ,
string $type ,
string $dest ,
int $size
2023-10-11 16:15:31 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$headers = [ 'OC-Total-Length' => $size ];
if ( $type === "asynchronously" ) {
$headers [ 'OC-LazyOps' ] = 'true' ;
}
2023-10-11 16:15:31 +05:45
return $this -> moveNewDavChunkToFinalFile (
2022-12-21 17:22:22 +05:45
$user ,
$id ,
$dest ,
$headers
);
2023-10-11 16:15:31 +05:45
}
2022-12-21 17:22:22 +05:45
/**
* @param string $user
* @param string $id
* @param string $type "asynchronously" or empty
* @param string $dest
* @param string $checksum
*
2023-10-11 16:15:31 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*/
2023-10-11 16:15:31 +05:45
public function userMoveNewChunkFileWithIdToMychunkedfileWithChecksum (
2022-12-21 17:22:22 +05:45
string $user ,
string $id ,
string $type ,
string $dest ,
string $checksum
2023-10-11 16:15:31 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$headers = [ 'OC-Checksum' => $checksum ];
if ( $type === "asynchronously" ) {
$headers [ 'OC-LazyOps' ] = 'true' ;
}
2023-10-11 16:15:31 +05:45
return $this -> moveNewDavChunkToFinalFile (
2022-12-21 17:22:22 +05:45
$user ,
$id ,
$dest ,
$headers
);
2023-10-11 16:15:31 +05:45
}
2022-12-21 17:22:22 +05:45
/**
* @Given /^user "([^"]*)" has moved new chunk file with id "([^"]*)"\s?(asynchronously|) to "([^"]*)" with checksum "([^"]*)"
*
* @param string $user
* @param string $id
* @param string $type "asynchronously" or empty
* @param string $dest
* @param string $checksum
*
* @return void
*/
public function userHasMovedNewChunkFileWithIdToMychunkedfileWithChecksum (
string $user ,
string $id ,
string $type ,
string $dest ,
string $checksum
) : void {
2023-10-11 16:15:31 +05:45
$response = $this -> userMoveNewChunkFileWithIdToMychunkedfileWithChecksum (
2022-12-21 17:22:22 +05:45
$user ,
$id ,
$type ,
$dest ,
$checksum
);
2023-10-11 16:15:31 +05:45
$this -> theHTTPStatusCodeShouldBe ( "201" , "" , $response );
2022-12-21 17:22:22 +05:45
}
/**
* Move chunked new DAV file to final file
*
* @param string $user user
* @param string $id upload id
* @param string $destination destination path
* @param array $headers extra headers
2023-07-18 14:15:46 +05:45
* @param bool|null $isGivenStep
2022-12-21 17:22:22 +05:45
*
2023-10-11 16:15:31 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*/
2023-07-18 14:15:46 +05:45
private function moveNewDavChunkToFinalFile (
string $user ,
string $id ,
string $destination ,
array $headers ,
? bool $isGivenStep = false
2023-10-11 16:15:31 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$user = $this -> getActualUsername ( $user );
$source = "/uploads/ $user / $id /.file" ;
$headers [ 'Destination' ] = $this -> destinationHeaderValue (
$user ,
$destination
);
2023-10-11 16:15:31 +05:45
return $this -> makeDavRequest (
2022-12-21 17:22:22 +05:45
$user ,
'MOVE' ,
$source ,
$headers ,
null ,
2024-10-08 14:46:33 +05:45
null ,
2023-07-18 14:15:46 +05:45
"uploads" ,
null ,
false ,
null ,
[],
null ,
$isGivenStep
2022-12-21 17:22:22 +05:45
);
}
/**
* Delete chunked-upload directory
*
* @param string $user user
* @param string $id upload id
* @param array $headers extra headers
*
2023-10-11 16:15:31 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*/
2023-10-11 16:15:31 +05:45
private function deleteUpload ( string $user , string $id , array $headers ) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$source = "/uploads/ $user / $id " ;
2023-10-11 16:15:31 +05:45
return $this -> makeDavRequest (
2022-12-21 17:22:22 +05:45
$user ,
'DELETE' ,
$source ,
$headers ,
null ,
2024-10-08 14:46:33 +05:45
null ,
2022-12-21 17:22:22 +05:45
"uploads"
);
}
/**
* URL encodes the given path but keeps the slashes
*
* @param string $path to encode
*
* @return string encoded path
*/
public function encodePath ( string $path ) : string {
// slashes need to stay
// in ocis even brackets are encoded
2023-05-02 17:25:56 +05:45
return \str_replace ( '%2F' , '/' , \rawurlencode ( $path ));
2022-12-21 17:22:22 +05:45
}
/**
2023-10-11 16:15:31 +05:45
* an unauthenticated client connects to the DAV endpoint using the WebDAV API
2022-12-21 17:22:22 +05:45
*
2023-10-11 16:15:31 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*/
2023-10-11 16:15:31 +05:45
public function connectToDavEndpoint () : ResponseInterface {
return $this -> makeDavRequest (
2022-12-21 17:22:22 +05:45
null ,
'PROPFIND' ,
'' ,
[]
);
}
2023-10-11 16:15:31 +05:45
/**
* @When an unauthenticated client connects to the DAV endpoint using the WebDAV API
*
* @return void
*/
public function connectingToDavEndpoint () : void {
$this -> setResponse ( $this -> connectToDavEndpoint ());
}
2022-12-21 17:22:22 +05:45
/**
* @Then there should be no duplicate headers
*
* @return void
* @throws Exception
*/
public function thereAreNoDuplicateHeaders () : void {
$headers = $this -> response -> getHeaders ();
foreach ( $headers as $headerName => $headerValues ) {
// if a header has multiple values, they must be different
if ( \count ( $headerValues ) > 1
&& \count ( \array_unique ( $headerValues )) < \count ( $headerValues )
) {
throw new Exception ( "Duplicate header found: $headerName " );
}
}
}
/**
* @Then the following headers should not be set
*
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function theFollowingHeadersShouldNotBeSet ( TableNode $table ) : void {
$this -> verifyTableNodeColumns (
$table ,
[ 'header' ]
);
foreach ( $table -> getColumnsHash () as $header ) {
$headerName = $header [ 'header' ];
$headerValue = $this -> response -> getHeader ( $headerName );
//Note: getHeader returns an empty array if the named header does not exist
2023-05-02 17:25:56 +05:45
$headerValue0 = $headerValue [ 0 ] ?? '' ;
2022-12-21 17:22:22 +05:45
Assert :: assertEmpty (
$headerValue ,
"header $headerName should not exist " .
"but does and is set to $headerValue0 "
);
}
}
/**
* @Then the following headers should match these regular expressions
*
* @param TableNode $table
*
* @return void
* @throws Exception
*/
2024-05-30 17:07:09 +05:45
public function theFollowingHeadersShouldMatchTheseRegularExpressions ( TableNode $table ) : void {
$this -> headersShouldMatchRegularExpressions ( $table );
}
/**
* @param TableNode $table
*
* @return void
*/
2022-12-21 17:22:22 +05:45
public function headersShouldMatchRegularExpressions ( TableNode $table ) : void {
$this -> verifyTableNodeColumnsCount ( $table , 2 );
foreach ( $table -> getTable () as $header ) {
$headerName = $header [ 0 ];
$expectedHeaderValue = $header [ 1 ];
$expectedHeaderValue = $this -> substituteInLineCodes (
$expectedHeaderValue ,
null ,
[ 'preg_quote' => [ '/' ]]
);
$returnedHeaders = $this -> response -> getHeader ( $headerName );
$returnedHeader = $returnedHeaders [ 0 ];
Assert :: assertNotFalse (
( bool ) \preg_match ( $expectedHeaderValue , $returnedHeader ),
"' $expectedHeaderValue ' does not match ' $returnedHeader '"
);
}
}
/**
* @Then /^if the HTTP status code was "([^"]*)" then the following headers should match these regular expressions$/
*
* @param int $statusCode
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function statusCodeShouldMatchTheseRegularExpressions ( int $statusCode , TableNode $table ) : void {
$actualStatusCode = $this -> response -> getStatusCode ();
if ( $actualStatusCode === $statusCode ) {
$this -> headersShouldMatchRegularExpressions ( $table );
}
}
/**
* @Then the following headers should match these regular expressions for user :user
*
* @param string $user
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function headersShouldMatchRegularExpressionsForUser ( string $user , TableNode $table ) : void {
$this -> verifyTableNodeColumnsCount ( $table , 2 );
$user = $this -> getActualUsername ( $user );
foreach ( $table -> getTable () as $header ) {
$headerName = $header [ 0 ];
$expectedHeaderValue = $header [ 1 ];
$expectedHeaderValue = $this -> substituteInLineCodes (
$expectedHeaderValue ,
$user ,
[ 'preg_quote' => [ '/' ]]
);
$returnedHeaders = $this -> response -> getHeader ( $headerName );
$returnedHeader = $returnedHeaders [ 0 ];
Assert :: assertNotFalse (
( bool ) \preg_match ( $expectedHeaderValue , $returnedHeader ),
"' $expectedHeaderValue ' does not match ' $returnedHeader '"
);
}
}
/**
* @When /^user "([^"]*)" deletes everything from folder "([^"]*)" using the WebDAV API$/
*
* @param string $user
* @param string $folder
*
* @return void
* @throws Exception
*/
public function userDeletesEverythingInFolder (
2024-05-30 17:07:09 +05:45
string $user ,
string $folder
) : void {
$this -> deleteEverythingInFolder ( $user , $folder , false );
}
/**
* @param string $user
* @param string $folder
* @param boolean $checkEachDelete
*
* @return void
*/
public function deleteEverythingInFolder (
2022-12-21 17:22:22 +05:45
string $user ,
string $folder ,
bool $checkEachDelete = false
) : void {
$user = $this -> getActualUsername ( $user );
$responseXmlObject = $this -> listFolderAndReturnResponseXml (
$user ,
$folder ,
'1'
);
$elementList = $responseXmlObject -> xpath ( "//d:response/d:href" );
if ( \is_array ( $elementList ) && \count ( $elementList )) {
\array_shift ( $elementList ); //don't delete the folder itself
$davPrefix = "/" . $this -> getFullDavFilesPath ( $user );
foreach ( $elementList as $element ) {
$element = \substr (( string ) $element , \strlen ( $davPrefix ));
if ( $checkEachDelete ) {
2024-05-30 17:07:09 +05:45
$user = $this -> getActualUsername ( $user );
$response = $this -> deleteFile ( $user , $element );
$this -> theHTTPStatusCodeShouldBe (
[ "204" ],
"HTTP status code was not 204 while trying to delete resource ' $element ' for user ' $user '" ,
$response
);
2022-12-21 17:22:22 +05:45
} else {
2024-05-30 17:07:09 +05:45
$this -> setResponse ( $this -> deleteFile ( $user , $element ));
$this -> pushToLastStatusCodesArrays ();
2022-12-21 17:22:22 +05:45
}
}
}
}
/**
* @When user :user downloads the preview of :path with width :width and height :height using the WebDAV API
2023-09-12 12:05:55 +05:45
* @When user :user tries to download the preview of nonexistent file :path with width :width and height :height using the WebDAV API
2022-12-21 17:22:22 +05:45
*
* @param string $user
* @param string $path
* @param string $width
* @param string $height
*
* @return void
*/
public function downloadPreviewOfFiles ( string $user , string $path , string $width , string $height ) : void {
2023-09-11 17:29:28 +05:45
$response = $this -> downloadPreviews (
2022-12-21 17:22:22 +05:45
$user ,
$path ,
null ,
$width ,
$height
);
2023-09-11 17:29:28 +05:45
$this -> setResponse ( $response );
2024-09-18 14:23:43 +05:45
}
/**
* @When user :user tries to download the preview of :path with width :width and height :height and preview set to 0 using the WebDAV API
*
* @param string $user
* @param string $path
* @param string $width
* @param string $height
*
* @return void
*/
public function userDownloadsThePreviewOfWithPreviewZero ( string $user , string $path , string $width , string $height ) {
$user = $this -> getActualUsername ( $user );
$urlParameter = [
'x' => $width ,
'y' => $height ,
'preview' => '0'
];
$response = $this -> makeDavRequest (
$user ,
"GET" ,
$path ,
[],
null ,
2024-09-18 11:26:20 +05:45
null ,
2024-09-18 14:23:43 +05:45
"files" ,
null ,
false ,
null ,
$urlParameter ,
);
$this -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
2023-10-13 11:51:08 +05:45
/**
* @When user :user downloads the preview of shared resource :path with width :width and height :height using the WebDAV API
*
* @param string $user
* @param string $path
* @param string $width
* @param string $height
*
* @return void
*/
public function userDownloadsThePreviewOfSharedResourceWithWidthAndHeightUsingTheWebdavApi ( string $user , string $path , string $width , string $height ) : void {
2024-09-17 17:11:20 +05:45
if ( $this -> getDavPathVersion () === WebDavHelper :: DAV_VERSION_SPACES ) {
2023-10-13 11:51:08 +05:45
$this -> setResponse ( $this -> downloadSharedFilePreview ( $user , $path , $width , $height ));
} else {
$this -> setResponse ( $this -> downloadPreviews ( $user , $path , null , $width , $height ));
}
}
2024-09-18 16:39:46 +05:45
/**
* @When user :user downloads the preview of :path with width :width and height :height and processor :processor using the WebDAV API
*
* @param string $user
* @param string $path
* @param string $width
* @param string $height
* @param string $processor
*
* @return void
*/
public function userDownloadsThePreviewOfWithWidthHeightProcessorUsingWebDAVAPI ( string $user , string $path , string $width , string $height , string $processor ) : void {
$user = $this -> getActualUsername ( $user );
$urlParameter = [
'x' => $width ,
'y' => $height ,
'preview' => '1' ,
'processor' => $processor
];
$response = $this -> makeDavRequest (
$user ,
"GET" ,
$path ,
[],
null ,
2024-09-18 11:26:20 +05:45
null ,
2024-09-18 16:39:46 +05:45
"files" ,
null ,
false ,
null ,
$urlParameter ,
);
$this -> setResponse ( $response );
}
2023-10-13 11:51:08 +05:45
/**
* @Given user :user has downloaded the preview of shared resource :path with width :width and height :height
*
* @param string $user
* @param string $path
* @param string $width
* @param string $height
*
* @return void
*/
public function userHasDownloadedThePreviewOfSharedResourceWithWidthAndHeight ( string $user , string $path , string $width , string $height ) : void {
2024-09-17 17:11:20 +05:45
if ( $this -> getDavPathVersion () === WebDavHelper :: DAV_VERSION_SPACES ) {
2023-10-13 11:51:08 +05:45
$response = $this -> downloadSharedFilePreview ( $user , $path , $width , $height );
} else {
$response = $this -> downloadPreviews ( $user , $path , null , $width , $height );
}
$this -> setResponse ( $response );
$this -> theHTTPStatusCodeShouldBe ( 200 , '' , $response );
2024-05-30 17:07:09 +05:45
$this -> checkImageDimensions ( $width , $height );
2024-09-18 16:39:46 +05:45
$response -> getBody () -> rewind ();
2023-10-13 11:51:08 +05:45
// save response to user response dictionary for further comparisons
2024-09-18 16:39:46 +05:45
$this -> userResponseBodyContents [ $user ] = $response -> getBody () -> getContents ();
2023-10-13 11:51:08 +05:45
}
/**
* @Then as user :user the preview of shared resource :path with width :width and height :height should have been changed
*
* @param string $user
* @param string $path
* @param string $width
* @param string $height
*
* @return void
*/
public function asUserThePreviewOfSharedResourceWithWidthAndHeightShouldHaveBeenChanged ( string $user , string $path , string $width , string $height ) : void {
2024-09-17 17:11:20 +05:45
if ( $this -> getDavPathVersion () === WebDavHelper :: DAV_VERSION_SPACES ) {
2023-10-13 11:51:08 +05:45
$response = $this -> downloadSharedFilePreview ( $user , $path , $width , $height );
} else {
$response = $this -> downloadPreviews ( $user , $path , null , $width , $height );
}
$this -> setResponse ( $response );
$this -> theHTTPStatusCodeShouldBe ( 200 , '' , $response );
$newResponseBodyContents = $this -> response -> getBody () -> getContents ();
Assert :: assertNotEquals (
$newResponseBodyContents ,
// different users can download files before and after an update is made to a file
2024-04-26 15:27:00 +05:45
// previous response content is fetched from the user response body content array entry for that user
2023-10-13 11:51:08 +05:45
$this -> userResponseBodyContents [ $user ],
__METHOD__ . " previous and current previews content is same but expected to be different" ,
);
// update the saved content for the next comparison
$this -> userResponseBodyContents [ $user ] = $newResponseBodyContents ;
}
/**
* @When user :user uploads file with content :content to shared resource :destination using the WebDAV API
*
* @param string $user
* @param string $content
* @param string $destination
*
* @return void
*/
public function userUploadsFileWithContentSharedResourceToUsingTheWebdavApi ( string $user , string $content , string $destination ) : void {
2024-09-17 17:11:20 +05:45
$this -> setResponse ( $this -> uploadFileWithContent ( $user , $content , $destination ));
2023-10-13 11:51:08 +05:45
}
/**
* @param string $user
* @param string $path
*
* @return string
* @throws GuzzleException
*/
2024-01-04 18:00:04 +05:45
public function getSharesMountPath (
2023-10-13 11:51:08 +05:45
string $user ,
string $path
) : string {
$user = $this -> getActualUsername ( $user );
$path = trim ( $path , "/" );
$pathArray = explode ( "/" , $path );
2024-01-04 18:00:04 +05:45
$sharedFolder = $pathArray [ 0 ] === "Shares" ? $pathArray [ 1 ] : $pathArray [ 0 ];
2023-10-13 11:51:08 +05:45
$shareMountId = GraphHelper :: getShareMountId (
$this -> getBaseUrl (),
$this -> getStepLineRef (),
$user ,
$this -> getPasswordForUser ( $user ),
2024-01-04 18:00:04 +05:45
$sharedFolder
2023-10-13 11:51:08 +05:45
);
2024-01-04 18:00:04 +05:45
$path = \array_slice ( $pathArray , array_search ( $sharedFolder , $pathArray ) + 1 );
$path = \implode ( "/" , $path );
return " $shareMountId / $path " ;
2023-10-13 11:51:08 +05:45
}
/**
* @param string $user
* @param string $path
* @param string|null $width
* @param string|null $height
*
* @return ResponseInterface
* @throws GuzzleException
*/
public function downloadSharedFilePreview (
string $user ,
string $path ,
? string $width = null ,
? string $height = null
) : ResponseInterface {
if ( $width !== null && $height !== null ) {
$urlParameter = [
'x' => $width ,
'y' => $height ,
'forceIcon' => '0' ,
'preview' => '1'
];
$urlParameter = \http_build_query ( $urlParameter , '' , '&' );
} else {
$urlParameter = null ;
}
2024-09-17 17:11:20 +05:45
$davPathVersion = $this -> getDavPathVersion ();
2024-10-21 12:24:50 +05:45
$suffixPath = $user ;
2024-09-17 17:11:20 +05:45
if ( $davPathVersion === WebDavHelper :: DAV_VERSION_SPACES ) {
2024-10-21 12:24:50 +05:45
$suffixPath = $this -> getSharesMountPath ( $user , $path );
2024-09-17 17:11:20 +05:45
$path = "" ;
}
$path = \ltrim ( $path , "/" );
2024-10-21 12:24:50 +05:45
$davPath = WebDavHelper :: getDavPath ( $this -> getDavPathVersion (), $suffixPath );
2024-09-17 17:11:20 +05:45
$davPath = \rtrim ( $davPath , "/" );
$fullUrl = $this -> getBaseUrl () . "/ $davPath / $path ? $urlParameter " ;
2023-10-13 11:51:08 +05:45
return HttpRequestHelper :: sendRequest (
$fullUrl ,
$this -> getStepLineRef (),
'GET' ,
$user ,
$this -> getPasswordForUser ( $user )
);
}
/**
2024-09-17 17:11:20 +05:45
* @When user :user downloads the preview of :path of :ofUser with width :width and height :height using the WebDAV API
2022-12-21 17:22:22 +05:45
*
2024-09-17 17:11:20 +05:45
* @param string $user
2022-12-21 17:22:22 +05:45
* @param string $path
2024-09-17 17:11:20 +05:45
* @param string $ofUser
2022-12-21 17:22:22 +05:45
* @param string $width
* @param string $height
*
* @return void
*/
2024-09-17 17:11:20 +05:45
public function downloadPreviewOfOtherUser ( string $user , string $path , string $ofUser , string $width , string $height ) : void {
2023-09-11 17:29:28 +05:45
$response = $this -> downloadPreviews (
2024-09-17 17:11:20 +05:45
$ofUser ,
2022-12-21 17:22:22 +05:45
$path ,
2024-09-17 17:11:20 +05:45
$user ,
2022-12-21 17:22:22 +05:45
$width ,
$height
);
2023-09-11 17:29:28 +05:45
$this -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @Then the downloaded image should be :width pixels wide and :height pixels high
*
2024-05-30 17:07:09 +05:45
* @param string $width
* @param string $height
*
* @return void
*/
public function imageDimensionsShouldBe ( string $width , string $height ) : void {
$this -> checkImageDimensions ( $width , $height );
}
/**
2022-12-21 17:22:22 +05:45
* @param string $width
* @param string $height
2023-10-11 16:15:31 +05:45
* @param ResponseInterface|null $response
2022-12-21 17:22:22 +05:45
*
* @return void
*/
2024-05-30 17:07:09 +05:45
public function checkImageDimensions ( string $width , string $height , ? ResponseInterface $response = null ) : void {
2023-10-11 16:15:31 +05:45
$response = $response ?? $this -> getResponse ();
2024-09-18 16:39:46 +05:45
$response -> getBody () -> rewind ();
$responseBodyContent = $response -> getBody () -> getContents ();
$size = \getimagesizefromstring ( $responseBodyContent );
2022-12-21 17:22:22 +05:45
Assert :: assertNotFalse ( $size , "could not get size of image" );
Assert :: assertEquals ( $width , $size [ 0 ], "width not as expected" );
Assert :: assertEquals ( $height , $size [ 1 ], "height not as expected" );
}
/**
* @Then the downloaded preview content should match with :preview fixtures preview content
*
* @param string $filename relative path from fixtures directory
*
* @return void
* @throws Exception
*/
public function theDownloadedPreviewContentShouldMatchWithFixturesPreviewContentFor ( string $filename ) : void {
2024-08-27 14:23:28 +05:45
$expectedPreview = \file_get_contents ( __DIR__ . "/../fixtures/" . $filename );
2024-09-18 16:39:46 +05:45
$response = $response ?? $this -> getResponse ();
$response -> getBody () -> rewind ();
$responseBodyContent = $response -> getBody () -> getContents ();
Assert :: assertEquals ( $expectedPreview , $responseBodyContent );
2022-12-21 17:22:22 +05:45
}
/**
* @Given user :user has downloaded the preview of :path with width :width and height :height
*
* @param string $user
* @param string $path
* @param string $width
* @param string $height
*
* @return void
*/
public function userDownloadsThePreviewOfWithWidthAndHeight ( string $user , string $path , string $width , string $height ) : void {
2023-10-11 16:15:31 +05:45
$response = $this -> downloadPreviews (
$user ,
$path ,
null ,
$width ,
$height
);
$this -> theHTTPStatusCodeShouldBe ( 200 , "" , $response );
2024-05-30 17:07:09 +05:45
$this -> checkImageDimensions ( $width , $height , $response );
2024-09-18 16:39:46 +05:45
$response -> getBody () -> rewind ();
2022-12-21 17:22:22 +05:45
// save response to user response dictionary for further comparisons
2024-09-18 16:39:46 +05:45
$this -> userResponseBodyContents [ $user ] = $response -> getBody () -> getContents ();
2022-12-21 17:22:22 +05:45
}
/**
* @Then as user :user the preview of :path with width :width and height :height should have been changed
*
* @param string $user
* @param string $path
* @param string $width
* @param string $height
*
* @return void
*/
public function asUserThePreviewOfPathWithHeightAndWidthShouldHaveBeenChanged ( string $user , string $path , string $width , string $height ) : void {
2023-10-11 16:15:31 +05:45
$response = $this -> downloadPreviews (
$user ,
$path ,
null ,
$width ,
$height
);
$this -> theHTTPStatusCodeShouldBe ( 200 , "" , $response );
$newResponseBodyContents = $response -> getBody () -> getContents ();
2022-12-21 17:22:22 +05:45
Assert :: assertNotEquals (
$newResponseBodyContents ,
// different users can download files before and after an update is made to a file
2024-04-26 15:27:00 +05:45
// previous response content is fetched from the user response body content array entry for that user
2022-12-21 17:22:22 +05:45
$this -> userResponseBodyContents [ $user ],
__METHOD__ . " previous and current previews content is same but expected to be different" ,
);
// update the saved content for the next comparison
$this -> userResponseBodyContents [ $user ] = $newResponseBodyContents ;
}
/**
* @param string $user
* @param string $path
2024-09-18 11:26:20 +05:45
* @param string $spaceId
2022-12-21 17:22:22 +05:45
*
* @return string|null
*/
2024-09-18 11:26:20 +05:45
public function getFileIdForPath ( string $user , string $path , string $spaceId = null ) : ? string {
2022-12-21 17:22:22 +05:45
$user = $this -> getActualUsername ( $user );
try {
return WebDavHelper :: getFileIdForPath (
$this -> getBaseUrl (),
$user ,
$this -> getPasswordForUser ( $user ),
$path ,
2024-09-18 11:26:20 +05:45
$spaceId ,
2022-12-21 17:22:22 +05:45
$this -> getStepLineRef (),
$this -> getDavPathVersion ()
);
} catch ( Exception $e ) {
return null ;
}
}
/**
2024-04-26 15:27:00 +05:45
* @Given /^user "([^"]*)" has stored id of (?:file|folder) "([^"]*)"$/
2022-12-21 17:22:22 +05:45
*
* @param string $user
* @param string $path
*
* @return void
*/
2024-04-26 15:27:00 +05:45
public function userStoresFileIdForPath ( string $user , string $path ) : void {
2022-12-21 17:22:22 +05:45
$this -> storedFileID = $this -> getFileIdForPath ( $user , $path );
}
/**
* @Then /^user "([^"]*)" (file|folder) "([^"]*)" should have the previously stored id$/
*
2024-09-18 11:26:20 +05:45
* @param string370 $user
2022-12-21 17:22:22 +05:45
* @param string $fileOrFolder
* @param string $path
*
* @return void
*/
public function userFileShouldHaveStoredId ( string $user , string $fileOrFolder , string $path ) : void {
$user = $this -> getActualUsername ( $user );
$currentFileID = $this -> getFileIdForPath ( $user , $path );
Assert :: assertEquals (
$currentFileID ,
$this -> storedFileID ,
__METHOD__
2023-05-02 17:25:56 +05:45
. " User ' $user ' $fileOrFolder ' $path ' does not have the previously stored id ' $this->storedFileID ', but has ' $currentFileID '."
2022-12-21 17:22:22 +05:45
);
}
/**
* @Then /^the (?:Cal|Card)?DAV (exception|message|reason) should be "([^"]*)"$/
*
* @param string $element exception|message|reason
* @param string $message
*
* @return void
* @throws Exception
*/
public function theDavElementShouldBe ( string $element , string $message ) : void {
2023-10-05 11:42:59 +05:45
$resXmlArray = HttpRequestHelper :: parseResponseAsXml ( $this -> getResponse ());
2022-12-21 17:22:22 +05:45
WebDavAssert :: assertDavResponseElementIs (
$element ,
$message ,
2023-10-05 11:42:59 +05:45
$resXmlArray ,
2022-12-21 17:22:22 +05:45
__METHOD__
);
}
/**
* @param string $shouldOrNot (not|)
* @param TableNode $expectedFiles
* @param string|null $user
* @param string|null $method
* @param string|null $folderpath
2024-09-18 11:26:20 +05:45
* @param string|null $spaceId
2022-12-21 17:22:22 +05:45
*
* @return void
* @throws GuzzleException
*/
public function propfindResultShouldContainEntries (
string $shouldOrNot ,
TableNode $expectedFiles ,
? string $user = null ,
? string $method = 'REPORT' ,
2024-09-18 11:26:20 +05:45
? string $folderpath = '' ,
? string $spaceId = null
2022-12-21 17:22:22 +05:45
) : void {
2024-09-17 17:11:20 +05:45
$folderpath = \trim ( $folderpath , "/" );
2022-12-21 17:22:22 +05:45
$this -> verifyTableNodeColumnsCount ( $expectedFiles , 1 );
$elementRows = $expectedFiles -> getRows ();
$should = ( $shouldOrNot !== "not" );
foreach ( $elementRows as $expectedFile ) {
2023-09-13 18:16:28 +05:45
$resource = $expectedFile [ 0 ];
2024-02-15 14:02:59 +05:45
$resource = $this -> substituteInLineCodes ( $resource , $user );
2023-09-13 18:16:28 +05:45
if ( $resource === '' ) {
continue ;
}
if ( $method === "REPORT" ) {
$fileFound = $this -> findEntryFromSearchResponse (
2024-09-18 11:26:20 +05:45
$resource ,
false ,
$spaceId ,
2023-09-13 18:16:28 +05:45
);
2023-10-10 12:57:41 +05:45
if ( \is_object ( $fileFound )) {
$fileFound = $fileFound -> xpath ( "d:propstat//oc:name" );
}
2023-09-13 18:16:28 +05:45
} else {
$fileFound = $this -> findEntryFromPropfindResponse (
$resource ,
$user ,
"files" ,
2024-09-18 11:26:20 +05:45
$folderpath ,
$spaceId
2023-09-13 18:16:28 +05:45
);
}
2022-12-21 17:22:22 +05:45
if ( $should ) {
Assert :: assertNotEmpty (
$fileFound ,
2023-09-13 18:16:28 +05:45
"response does not contain the entry ' $resource '"
2022-12-21 17:22:22 +05:45
);
} else {
Assert :: assertFalse (
$fileFound ,
2023-09-13 18:16:28 +05:45
"response does contain the entry ' $resource ' but should not"
2022-12-21 17:22:22 +05:45
);
}
}
}
/**
* @Then /^the (?:propfind|search) result of user "([^"]*)" should (not|)\s?contain these (?:files|entries):$/
*
* @param string $user
* @param string $shouldOrNot (not|)
* @param TableNode $expectedFiles
*
* @return void
* @throws Exception
*/
public function thePropfindResultShouldContainEntries (
string $user ,
string $shouldOrNot ,
TableNode $expectedFiles
) : void {
$user = $this -> getActualUsername ( $user );
$this -> propfindResultShouldContainEntries (
$shouldOrNot ,
$expectedFiles ,
$user
);
}
/**
* @Then /^the (?:propfind|search) result of user "([^"]*)" should contain only these (?:files|entries):$/
*
* @param string $user
* @param TableNode $expectedFiles
*
* @return void
* @throws Exception
*/
public function thePropfindResultShouldContainOnlyEntries (
string $user ,
TableNode $expectedFiles
) : void {
$user = $this -> getActualUsername ( $user );
Assert :: assertEquals (
\count ( $expectedFiles -> getTable ()),
$this -> getNumberOfEntriesInPropfindResponse (
$user
),
"The number of elements in the response doesn't matches with expected number of elements"
);
$this -> propfindResultShouldContainEntries (
'' ,
$expectedFiles ,
$user
);
}
/**
* @Then the propfind/search result should contain :numFiles files/entries
*
* @param int $numFiles
*
* @return void
*/
public function propfindResultShouldContainNumEntries ( int $numFiles ) : void {
2024-05-30 17:07:09 +05:45
$this -> checkIFResponseContainsNumberEntries ( $numFiles );
}
/**
* @param integer $numFiles
*
* @return void
*/
public function checkIFResponseContainsNumberEntries ( int $numFiles ) : void {
2022-12-21 17:22:22 +05:45
//if we are using that step the second time in a scenario e.g. 'But ... should not'
//then don't parse the result again, because the result in a ResponseInterface
if ( empty ( $this -> responseXml )) {
$this -> setResponseXml (
HttpRequestHelper :: parseResponseAsXml ( $this -> response )
);
}
Assert :: assertIsArray (
$this -> responseXml ,
__METHOD__ . " responseXml is not an array"
);
Assert :: assertArrayHasKey (
"value" ,
$this -> responseXml ,
__METHOD__ . " responseXml does not have key 'value'"
);
$multistatusResults = $this -> responseXml [ "value" ];
if ( $multistatusResults === null ) {
$multistatusResults = [];
}
Assert :: assertEquals (
2023-05-02 17:25:56 +05:45
$numFiles ,
2022-12-21 17:22:22 +05:45
\count ( $multistatusResults ),
__METHOD__
. " Expected result to contain '"
2023-05-02 17:25:56 +05:45
. $numFiles
2022-12-21 17:22:22 +05:45
. "' files/entries, but got '"
. \count ( $multistatusResults )
. "' files/entries."
);
}
/**
* @Then the propfind/search result should contain any :expectedNumber of these files/entries:
*
* @param integer $expectedNumber
* @param TableNode $expectedFiles
*
* @return void
* @throws Exception
*/
public function theSearchResultShouldContainAnyOfTheseEntries (
int $expectedNumber ,
TableNode $expectedFiles
) : void {
2024-05-30 17:07:09 +05:45
$this -> checkIfSearchResultContainsFiles (
2022-12-21 17:22:22 +05:45
$this -> getCurrentUser (),
$expectedNumber ,
$expectedFiles
);
}
/**
* @Then the propfind/search result of user :user should contain any :expectedNumber of these files/entries:
*
* @param string $user
* @param integer $expectedNumber
* @param TableNode $expectedFiles
*
* @return void
* @throws Exception
*/
public function theSearchResultOfUserShouldContainAnyOfTheseEntries (
string $user ,
int $expectedNumber ,
TableNode $expectedFiles
2024-05-30 17:07:09 +05:45
) : void {
$this -> checkIfSearchResultContainsFiles (
$user ,
$expectedNumber ,
$expectedFiles
);
}
/**
* @param string $user
* @param integer $expectedNumber
* @param TableNode $expectedFiles
*
* @return void
*/
public function checkIfSearchResultContainsFiles (
string $user ,
int $expectedNumber ,
TableNode $expectedFiles
2022-12-21 17:22:22 +05:45
) : void {
$user = $this -> getActualUsername ( $user );
$this -> verifyTableNodeColumnsCount ( $expectedFiles , 1 );
2024-05-30 17:07:09 +05:45
$this -> checkIFResponseContainsNumberEntries ( $expectedNumber );
2022-12-21 17:22:22 +05:45
$elementRows = $expectedFiles -> getColumn ( 0 );
// Remove any "/" from the front (or back) of the expected values passed
// into the step. findEntryFromPropfindResponse returns entries without
// any leading (or trailing) slash
$expectedEntries = \array_map (
function ( $value ) {
return \trim ( $value , "/" );
},
$elementRows
);
2023-10-10 12:57:41 +05:45
$resultEntries = $this -> findEntryFromSearchResponse ();
2022-12-21 17:22:22 +05:45
foreach ( $resultEntries as $resultEntry ) {
Assert :: assertContains ( $resultEntry , $expectedEntries );
}
}
/**
* @When user :arg1 lists the resources in :path with depth :depth using the WebDAV API
*
* @param string $user
* @param string $path
* @param string $depth
*
* @return void
* @throws Exception
*/
public function userListsTheResourcesInPathWithDepthUsingTheWebdavApi ( string $user , string $path , string $depth ) : void {
$response = $this -> listFolder (
$user ,
$path ,
$depth
);
$this -> setResponse ( $response );
$this -> setResponseXml ( HttpRequestHelper :: parseResponseAsXml ( $this -> response ));
}
/**
* @Then the last DAV response for user :user should contain these nodes/elements
*
* @param string $user
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function theLastDavResponseShouldContainTheseNodes ( string $user , TableNode $table ) : void {
$this -> verifyTableNodeColumns ( $table , [ "name" ]);
foreach ( $table -> getHash () as $row ) {
$path = $this -> substituteInLineCodes ( $row [ 'name' ]);
$res = $this -> findEntryFromPropfindResponse ( $path , $user );
Assert :: assertNotFalse ( $res , "expected $path to be in DAV response but was not found" );
}
}
/**
* @Then the last DAV response for user :user should not contain these nodes/elements
*
* @param string $user
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function theLastDavResponseShouldNotContainTheseNodes ( string $user , TableNode $table ) : void {
$this -> verifyTableNodeColumns ( $table , [ "name" ]);
foreach ( $table -> getHash () as $row ) {
$path = $this -> substituteInLineCodes ( $row [ 'name' ]);
$res = $this -> findEntryFromPropfindResponse ( $path , $user );
Assert :: assertFalse ( $res , "expected $path to not be in DAV response but was found" );
}
}
/**
* @Then the last public link DAV response should contain these nodes/elements
*
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function theLastPublicDavResponseShouldContainTheseNodes ( TableNode $table ) : void {
2024-05-17 15:17:11 +05:45
$token = ( $this -> isUsingSharingNG ()) ? $this -> shareNgGetLastCreatedLinkShareToken () : $this -> getLastCreatedPublicShareToken ();
2022-12-21 17:22:22 +05:45
$this -> verifyTableNodeColumns ( $table , [ "name" ]);
2024-09-17 17:11:20 +05:45
2022-12-21 17:22:22 +05:45
foreach ( $table -> getHash () as $row ) {
$path = $this -> substituteInLineCodes ( $row [ 'name' ]);
2024-09-17 17:11:20 +05:45
$res = $this -> findEntryFromPropfindResponse ( $path , $token , "public-files" );
2022-12-21 17:22:22 +05:45
Assert :: assertNotFalse ( $res , "expected $path to be in DAV response but was not found" );
}
}
/**
* @Then the last public link DAV response should not contain these nodes/elements
*
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function theLastPublicDavResponseShouldNotContainTheseNodes ( TableNode $table ) : void {
2024-05-17 15:17:11 +05:45
$token = ( $this -> isUsingSharingNG ()) ? $this -> shareNgGetLastCreatedLinkShareToken () : $this -> getLastCreatedPublicShareToken ();
2022-12-21 17:22:22 +05:45
$this -> verifyTableNodeColumns ( $table , [ "name" ]);
2024-09-17 17:11:20 +05:45
2022-12-21 17:22:22 +05:45
foreach ( $table -> getHash () as $row ) {
$path = $this -> substituteInLineCodes ( $row [ 'name' ]);
2024-09-17 17:11:20 +05:45
$res = $this -> findEntryFromPropfindResponse ( $path , $token , "public-files" );
2022-12-21 17:22:22 +05:45
Assert :: assertFalse ( $res , "expected $path to not be in DAV response but was found" );
}
}
/**
* @When the public lists the resources in the last created public link with depth :depth using the WebDAV API
*
* @param string $depth
*
* @return void
* @throws Exception
*/
public function thePublicListsTheResourcesInTheLastCreatedPublicLinkWithDepthUsingTheWebdavApi ( string $depth ) : void {
2024-05-15 17:37:54 +05:45
$token = ( $this -> isUsingSharingNG ()) ? $this -> shareNgGetLastCreatedLinkShareToken () : $this -> getLastCreatedPublicShareToken ();
2022-12-21 17:22:22 +05:45
$response = $this -> listFolder (
2024-05-15 17:37:54 +05:45
$token ,
2022-12-21 17:22:22 +05:45
'/' ,
$depth ,
null ,
2024-09-18 11:26:20 +05:45
null ,
2024-09-17 17:11:20 +05:45
"public-files"
2022-12-21 17:22:22 +05:45
);
$this -> setResponse ( $response );
$this -> setResponseXml ( HttpRequestHelper :: parseResponseAsXml ( $this -> response ));
}
/**
* @param string|null $user
*
* @return array
*/
public function findEntryFromReportResponse ( ? string $user ) : array {
2024-09-17 17:11:20 +05:45
$responseXmlObj = $this -> getResponseXml ();
2022-12-21 17:22:22 +05:45
$responseResources = [];
$hrefs = $responseXmlObj -> xpath ( '//d:href' );
foreach ( $hrefs as $href ) {
$hrefParts = \explode ( "/" , ( string ) $href [ 0 ]);
if ( \in_array ( $user , $hrefParts )) {
$entry = \urldecode ( \end ( $hrefParts ));
2023-05-02 17:25:56 +05:45
$responseResources [] = $entry ;
2022-12-21 17:22:22 +05:45
} else {
throw new Error ( "Expected user: $hrefParts[5] but found: $user " );
}
}
return $responseResources ;
}
/**
* parses a PROPFIND response from $this->response into xml
* and returns found search results if found else returns false
*
* @param string|null $user
*
* @return int
*/
public function getNumberOfEntriesInPropfindResponse (
? string $user = null
) : int {
2023-05-02 17:25:56 +05:45
$multistatusResults = $this -> getMultiStatusResultFromPropfindResult ( $user );
2022-12-21 17:22:22 +05:45
return \count ( $multistatusResults );
}
/**
* parses a PROPFIND response from $this->response
* and returns multistatus data from the response
*
* @param string|null $user
*
* @return array
*/
2023-05-02 17:25:56 +05:45
public function getMultiStatusResultFromPropfindResult (
2022-12-21 17:22:22 +05:45
? string $user = null
) : array {
//if we are using that step the second time in a scenario e.g. 'But ... should not'
//then don't parse the result again, because the result in a ResponseInterface
if ( empty ( $this -> responseXml )) {
$this -> setResponseXml (
HttpRequestHelper :: parseResponseAsXml ( $this -> response )
);
}
Assert :: assertNotEmpty ( $this -> responseXml , __METHOD__ . ' Response is empty' );
if ( $user === null ) {
$user = $this -> getCurrentUser ();
}
Assert :: assertIsArray (
$this -> responseXml ,
__METHOD__ . " responseXml for user $user is not an array"
);
Assert :: assertArrayHasKey (
"value" ,
$this -> responseXml ,
__METHOD__ . " responseXml for user $user does not have key 'value'"
);
$multistatus = $this -> responseXml [ "value" ];
if ( $multistatus == null ) {
$multistatus = [];
}
return $multistatus ;
}
/**
* Escapes the given string for
* 1. Space --> %20
* 2. Opening Small Bracket --> %28
* 3. Closing Small Bracket --> %29
*
* @param string $path - File path to parse
*
* @return string
*/
public function escapePath ( string $path ) : string {
return \str_replace ([ " " , "(" , ")" ], [ "%20" , "%28" , "%29" ], $path );
}
/**
* parses a PROPFIND response from $this->response into xml
* and returns found search results if found else returns false
*
* @param string|null $entryNameToSearch
* @param string|null $user
* @param string $type
* @param string $folderPath
2024-09-18 11:26:20 +05:45
* @param string|null $spaceId
2022-12-21 17:22:22 +05:45
*
* @return string|array|boolean
*
* string if $entryNameToSearch is given and is found
* array if $entryNameToSearch is not given
* boolean false if $entryNameToSearch is given and is not found
*
* @throws GuzzleException
*/
public function findEntryFromPropfindResponse (
? string $entryNameToSearch = null ,
? string $user = null ,
string $type = "files" ,
2024-09-18 11:26:20 +05:45
string $folderPath = '' ,
? string $spaceId = null
2022-12-21 17:22:22 +05:45
) {
$trimmedEntryNameToSearch = '' ;
// trim any leading "/" passed by the caller, we can just match the "raw" name
if ( $entryNameToSearch != null ) {
$trimmedEntryNameToSearch = \trim ( $entryNameToSearch , "/" );
}
// url encode for spaces and brackets that may appear in the filePath
$folderPath = $this -> escapePath ( $folderPath );
2024-09-17 17:11:20 +05:45
// topWebDavPath should be something like '/webdav/' or '/dav/files/{user}/'
2024-09-18 11:26:20 +05:45
$topWebDavPath = "/" . $this -> getFullDavFilesPath ( $user , $spaceId ) . "/" . $folderPath ;
2022-12-21 17:22:22 +05:45
switch ( $type ) {
case "files" :
break ;
case "public-files" :
2024-09-17 17:11:20 +05:45
$topWebDavPath = "/" . $this -> getPublicLinkDavPath ( $user ) . "/" ;
2022-12-21 17:22:22 +05:45
break ;
default :
throw new Exception ( "error" );
}
2023-05-02 17:25:56 +05:45
$multistatusResults = $this -> getMultiStatusResultFromPropfindResult ( $user );
2022-12-21 17:22:22 +05:45
$results = [];
2023-05-02 17:25:56 +05:45
foreach ( $multistatusResults as $multistatusResult ) {
$entryPath = $multistatusResult [ 'value' ][ 0 ][ 'value' ];
$entryName = \str_replace ( $topWebDavPath , "" , $entryPath );
$entryName = \rawurldecode ( $entryName );
$entryName = \trim ( $entryName , "/" );
if ( $trimmedEntryNameToSearch === $entryName ) {
return $multistatusResult ;
}
$results [] = $entryName ;
2022-12-21 17:22:22 +05:45
}
if ( $entryNameToSearch === null ) {
return $results ;
}
return false ;
}
2023-09-13 18:16:28 +05:45
/**
* parses a REPORT response from $this->response into xml
* and returns found search results if found else returns false
*
* @param string|null $entryNameToSearch
2023-09-15 15:31:46 +05:45
* @param bool|null $searchForHighlightString
2024-09-18 11:26:20 +05:45
* @param string|null $spaceId
2023-09-13 18:16:28 +05:45
*
* @return string|array|boolean
*
* string if $entryNameToSearch is given and is found
* array if $entryNameToSearch is not given
* boolean false if $entryNameToSearch is given and is not found
*
2023-09-15 15:31:46 +05:45
* @throws Exception
2023-09-13 18:16:28 +05:45
*/
public function findEntryFromSearchResponse (
2023-09-15 15:31:46 +05:45
? string $entryNameToSearch = null ,
2024-09-18 11:26:20 +05:45
? bool $searchForHighlightString = false ,
? string $spaceId = null
2023-09-13 18:16:28 +05:45
) {
// trim any leading "/" passed by the caller, we can just match the "raw" name
if ( $entryNameToSearch !== null ) {
$entryNameToSearch = \trim ( $entryNameToSearch , "/" );
}
2024-09-17 17:11:20 +05:45
$spacesBaseUrl = "/" . WebDavHelper :: getDavPath ( $this -> getDavPathVersion (), $spaceId );
$spacesBaseUrl = \rtrim ( $spacesBaseUrl , "/" ) . "/" ;
$hrefRegex = \preg_quote ( $spacesBaseUrl , "/" );
if ( \in_array ( $this -> getDavPathVersion (), [ WebDavHelper :: DAV_VERSION_SPACES , WebDavHelper :: DAV_VERSION_NEW ])
&& ! GraphHelper :: isSpaceId ( $entryNameToSearch ?? '' )
) {
$hrefRegex .= "[a-zA-Z0-9-_$!:%]+" ;
}
$hrefRegex = "/^" . $hrefRegex . "/" ;
2023-09-13 18:16:28 +05:45
$searchResults = $this -> getResponseXml () -> xpath ( "//d:multistatus/d:response" );
$results = [];
foreach ( $searchResults as $item ) {
$href = ( string ) $item -> xpath ( "d:href" )[ 0 ];
$shareRootXml = $item -> xpath ( "d:propstat//oc:shareroot" );
2024-09-17 17:11:20 +05:45
$resourcePath = \preg_replace ( $hrefRegex , "" , $href );
2024-04-26 15:27:00 +05:45
// do not try to parse the resource path
2024-02-15 14:02:59 +05:45
// if the item to search is space itself
if ( ! GraphHelper :: isSpaceId ( $entryNameToSearch ?? '' )) {
2024-09-17 17:11:20 +05:45
$resourcePath = \substr ( $resourcePath , \strpos ( $resourcePath , '/' ) + 1 );
2024-02-15 14:02:59 +05:45
}
2023-09-13 18:16:28 +05:45
if ( \count ( $shareRootXml )) {
$shareroot = \trim (( string ) $shareRootXml [ 0 ], "/" );
$resourcePath = $shareroot . "/" . $resourcePath ;
}
$resourcePath = \rawurldecode ( $resourcePath );
if ( $entryNameToSearch === $resourcePath ) {
2024-04-26 15:27:00 +05:45
// If searching for a single entry,
// we return a SimpleXmlElement of found item
2023-10-10 12:57:41 +05:45
return $item ;
2023-09-13 18:16:28 +05:45
}
2023-09-15 15:31:46 +05:45
if ( $searchForHighlightString ) {
2023-10-10 12:57:41 +05:45
// If searching for highlighted string,
// we return an array of entries with highlighted content as value
// Example:
// [
// "<entryName1>" => "<highlighted-content>"
// "<entryName2>" => "<highlighted-content>"
// ]
2023-09-15 15:31:46 +05:45
$actualHighlightString = $item -> xpath ( "d:propstat//oc:highlights" );
$results [ $resourcePath ] = ( string ) $actualHighlightString [ 0 ];
} else {
2023-10-10 12:57:41 +05:45
// If list all the entries i.e. $entryNameToSearch=null,
// we return an array of entries in the response
// Example:
// ["<entry1>", "<entry2>"]
2023-09-15 15:31:46 +05:45
$results [] = $resourcePath ;
}
2023-09-13 18:16:28 +05:45
}
if ( $entryNameToSearch === null ) {
return $results ;
}
return false ;
}
2022-12-21 17:22:22 +05:45
/**
* Prevent creating two uploads and/or deletes with the same "stime"
* That is based on seconds in some implementations.
* This prevents duplication of etags or other problems with
* trashbin/versions save/restore.
*
* Set env var UPLOAD_DELETE_WAIT_TIME to 1 to activate a 1-second pause.
* By default, there is no pause. That allows testing of implementations
* which should be able to cope with multiple upload/delete actions in the
* same second.
*
* @return void
*/
public function pauseUploadDelete () : void {
$time = \time ();
$uploadWaitTime = \getenv ( "UPLOAD_DELETE_WAIT_TIME" );
$uploadWaitTime = $uploadWaitTime ? ( int ) $uploadWaitTime : 0 ;
if (( $this -> lastUploadDeleteTime !== null )
&& ( $uploadWaitTime > 0 )
&& (( $time - $this -> lastUploadDeleteTime ) < $uploadWaitTime )
) {
\sleep ( $uploadWaitTime );
}
}
2024-05-30 17:07:09 +05:45
/**
* @param string $index
* @param string $expectedUsername
*
* @return void
*/
public function checkAuthorOfAVersionOfFile ( string $index , string $expectedUsername ) : void {
2022-12-21 17:22:22 +05:45
$expectedUserDisplayName = $this -> getUserDisplayName ( $expectedUsername );
2024-09-17 17:11:20 +05:45
$resXml = $this -> getResponseXml ();
2022-12-21 17:22:22 +05:45
if ( $resXml === null ) {
$resXml = HttpRequestHelper :: getResponseXml (
$this -> getResponse (),
__METHOD__
);
$this -> setResponseXmlObject ( $resXml );
}
// the username should be in oc:meta-version-edited-by
2023-01-21 11:14:35 +05:45
$xmlPart = $resXml -> xpath ( "//oc:meta-version-edited-by" );
$authors = [];
foreach ( $xmlPart as $idx => $author ) {
// The first element is the root path element which is not a version
// So skipping it
if ( $idx !== 0 ) {
$authors [] = $author -> __toString ();
}
}
if ( ! isset ( $authors [ $index - 1 ])) {
2022-12-21 17:22:22 +05:45
Assert :: fail (
'could not find version with index "' . $index . '" for oc:meta-version-edited-by property in response to user "' . $this -> responseUser . '"'
);
}
2023-01-21 11:14:35 +05:45
$actualUser = $authors [ $index - 1 ];
2022-12-21 17:22:22 +05:45
Assert :: assertEquals (
$expectedUsername ,
$actualUser ,
"Expected user of version with index $index in response to user ' $this->responseUser ' was ' $expectedUsername ', but got ' $actualUser '"
);
// the user's display name should be in oc:meta-version-edited-by-name
2023-01-21 11:14:35 +05:45
$xmlPart = $resXml -> xpath ( "//oc:meta-version-edited-by-name" );
$displaynames = [];
foreach ( $xmlPart as $idx => $displayname ) {
// The first element is the root path element which is not a version
// So skipping it
if ( $idx !== 0 ) {
$displaynames [] = $displayname -> __toString ();
}
}
if ( ! isset ( $displaynames [ $index - 1 ])) {
2022-12-21 17:22:22 +05:45
Assert :: fail (
'could not find version with index "' . $index . '" for oc:meta-version-edited-by-name property in response to user "' . $this -> responseUser . '"'
);
}
2023-01-21 11:14:35 +05:45
$actualUserDisplayName = $displaynames [ $index - 1 ];
2022-12-21 17:22:22 +05:45
Assert :: assertEquals (
$expectedUserDisplayName ,
$actualUserDisplayName ,
2023-01-21 11:14:35 +05:45
"Expected display name of version with index $index in response to user ' $this->responseUser ' was ' $expectedUserDisplayName ', but got ' $actualUserDisplayName '"
2022-12-21 17:22:22 +05:45
);
}
2023-04-27 16:04:17 +05:45
/**
* @When user :user downloads the content of GDPR report :pathToFile
*
* @param string $user
* @param string $pathToFile
*
* @return void
* @throws Exception
*/
public function userGetsTheContentOfGeneratedJsonReport ( string $user , string $pathToFile ) : void {
$password = $this -> getPasswordForUser ( $user );
2023-10-11 16:15:31 +05:45
$response = $this -> downloadFileAsUserUsingPassword ( $user , $pathToFile , $password );
$this -> setResponse ( $response );
2023-04-27 16:04:17 +05:45
$this -> pushToLastStatusCodesArrays ();
}
2022-12-21 17:22:22 +05:45
}