2022-12-21 17:22:22 +05:45
<? php declare ( strict_types = 1 );
/**
* @author Artur Neumann <artur@jankaritech.com>
*
* @copyright Copyright (c) 2020, 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\Behat\Context\Context ;
use Behat\Behat\Hook\Scope\BeforeScenarioScope ;
use Behat\Gherkin\Node\TableNode ;
use GuzzleHttp\Exception\GuzzleException ;
use TusPhp\Exception\ConnectionException ;
use TusPhp\Exception\TusException ;
2023-09-25 13:32:42 +05:45
use Psr\Http\Message\ResponseInterface ;
2024-10-08 12:26:40 +05:45
use TestHelpers\HttpRequestHelper ;
use TestHelpers\WebDavHelper ;
use TestHelpers\BehatHelper ;
2024-12-24 15:30:14 +05:45
use TestHelpers\TusClient ;
2022-12-21 17:22:22 +05:45
require_once 'bootstrap.php' ;
/**
* TUS related test steps
*/
class TUSContext implements Context {
2023-05-02 17:25:56 +05:45
private FeatureContext $featureContext ;
2022-12-21 17:22:22 +05:45
2024-10-03 11:39:38 +05:45
private array $tusResourceLocations = [];
2022-12-21 17:22:22 +05:45
2024-03-25 15:37:35 +05:45
/**
2024-10-03 11:39:38 +05:45
* @param string $filenameHash
* @param string $location
*
* @return void
*/
public function saveTusResourceLocation ( string $filenameHash , string $location ) : void {
$this -> tusResourceLocations [ $filenameHash ][] = $location ;
}
/**
* @param string $filenameHash
* @param int|null $index
*
* @return string
*/
public function getTusResourceLocation ( string $filenameHash , ? int $index = null ) : string {
if ( $index === null ) {
// get the last one
$index = \count ( $this -> tusResourceLocations [ $filenameHash ]) - 1 ;
}
return $this -> tusResourceLocations [ $filenameHash ][ $index ];
}
/**
* @return string
*/
public function getLastTusResourceLocation () : string {
$lastKey = \array_key_last ( $this -> tusResourceLocations );
$index = \count ( $this -> tusResourceLocations [ $lastKey ]) - 1 ;
return $this -> tusResourceLocations [ $lastKey ][ $index ];
}
/**
* @param string $uploadMetadata
*
2024-03-25 15:37:35 +05:45
* @return string
*/
2024-10-03 11:39:38 +05:45
public function parseFilenameHash ( string $uploadMetadata ) : string {
$filenameHash = \explode ( "filename " , $uploadMetadata )[ 1 ] ?? '' ;
return \explode ( " " , $filenameHash , 2 )[ 0 ];
2024-03-25 15:37:35 +05:45
}
2022-12-21 17:22:22 +05:45
/**
* @param string $user
2024-10-03 11:39:38 +05:45
* @param TableNode $headersTable
2022-12-21 17:22:22 +05:45
* @param string $content
2024-09-18 11:26:20 +05:45
* @param string|null $spaceId
2022-12-21 17:22:22 +05:45
*
2023-09-25 13:32:42 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*
* @throws Exception
* @throws GuzzleException
*/
2024-12-24 09:30:42 +05:45
public function createNewTUSResourceWithHeaders (
string $user ,
TableNode $headersTable ,
string $content = '' ,
2025-06-19 15:34:02 +05:45
? string $spaceId = null ,
2024-12-24 09:30:42 +05:45
) : ResponseInterface {
2024-10-03 11:39:38 +05:45
$this -> featureContext -> verifyTableNodeColumnsCount ( $headersTable , 2 );
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$password = $this -> featureContext -> getUserPassword ( $user );
2024-10-03 11:39:38 +05:45
$headers = $headersTable -> getRowsHash ();
2023-09-25 13:32:42 +05:45
$response = $this -> featureContext -> makeDavRequest (
$user ,
"POST" ,
null ,
2024-10-03 11:39:38 +05:45
$headers ,
2023-09-25 13:32:42 +05:45
$content ,
2024-09-18 11:26:20 +05:45
$spaceId ,
2023-09-25 13:32:42 +05:45
"files" ,
null ,
false ,
2025-06-19 15:34:02 +05:45
$password ,
2022-12-21 17:22:22 +05:45
);
2023-09-25 13:32:42 +05:45
$locationHeader = $response -> getHeader ( 'Location' );
2022-12-21 17:22:22 +05:45
if ( \sizeof ( $locationHeader ) > 0 ) {
2024-10-03 11:39:38 +05:45
$filenameHash = $this -> parseFilenameHash ( $headers [ 'Upload-Metadata' ]);
$this -> saveTusResourceLocation ( $filenameHash , $locationHeader [ 0 ]);
2022-12-21 17:22:22 +05:45
}
2023-09-25 13:32:42 +05:45
return $response ;
}
/**
* @When user :user creates a new TUS resource on the WebDAV API with these headers:
*
* @param string $user
* @param TableNode $headers
* @param string $content
*
* @return void
*
* @throws Exception
* @throws GuzzleException
*/
public function userCreateNewTUSResourceWithHeaders ( string $user , TableNode $headers , string $content = '' ) : void {
$response = $this -> createNewTUSResourceWithHeaders ( $user , $headers , $content );
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @Given user :user has created a new TUS resource on the WebDAV API with these headers:
*
* @param string $user
* @param TableNode $headers Tus-Resumable: 1.0.0 header is added automatically
*
* @return void
*
* @throws Exception
* @throws GuzzleException
*/
2023-09-25 13:32:42 +05:45
public function userHasCreatedNewTUSResourceWithHeaders ( string $user , TableNode $headers ) : void {
2024-04-05 11:31:46 +05:45
$response = $this -> createNewTUSResource ( $user , $headers );
$this -> featureContext -> theHTTPStatusCodeShouldBe ( 201 , "" , $response );
}
/**
* @param string $user
* @param TableNode $headers
2024-09-18 11:26:20 +05:45
* @param string|null $spaceId
2024-04-05 11:31:46 +05:45
*
* @return ResponseInterface
*/
2024-12-24 09:30:42 +05:45
public function createNewTUSResource ( string $user , TableNode $headers , ? string $spaceId = null ) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$rows = $headers -> getRows ();
$rows [] = [ 'Tus-Resumable' , '1.0.0' ];
2024-09-18 11:26:20 +05:45
return $this -> createNewTUSResourceWithHeaders ( $user , new TableNode ( $rows ), '' , $spaceId );
2022-12-21 17:22:22 +05:45
}
/**
* @param string $user
2024-10-03 11:39:38 +05:45
* @param string $resourceLocation
2022-12-21 17:22:22 +05:45
* @param string $offset
* @param string $data
* @param string $checksum
2024-02-23 11:47:52 +01:00
* @param array|null $extraHeaders
2022-12-21 17:22:22 +05:45
*
2023-09-25 13:32:42 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*
* @throws GuzzleException
* @throws JsonException
*/
2024-12-24 09:30:42 +05:45
public function uploadChunkToTUSLocation (
string $user ,
string $resourceLocation ,
string $offset ,
string $data ,
string $checksum = '' ,
2025-06-19 15:34:02 +05:45
? array $extraHeaders = null ,
2024-12-24 09:30:42 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$password = $this -> featureContext -> getUserPassword ( $user );
2024-02-23 11:47:52 +01:00
$headers = [
2025-06-19 10:34:17 +05:45
'Content-Type' => 'application/offset+octet-stream' ,
'Tus-Resumable' => '1.0.0' ,
'Upload-Checksum' => $checksum ,
2025-06-19 10:46:56 +05:45
'Upload-Offset' => $offset ,
2024-02-23 11:47:52 +01:00
];
$headers = empty ( $extraHeaders ) ? $headers : array_merge ( $headers , $extraHeaders );
2024-04-26 15:27:00 +05:45
2023-09-25 13:32:42 +05:45
return HttpRequestHelper :: sendRequest (
2024-10-03 11:39:38 +05:45
$resourceLocation ,
2023-09-25 13:32:42 +05:45
'PATCH' ,
$user ,
$password ,
2024-02-23 11:47:52 +01:00
$headers ,
2025-06-19 15:34:02 +05:45
$data ,
2022-12-21 17:22:22 +05:45
);
2023-09-25 13:32:42 +05:45
}
/**
* @When user :user sends a chunk to the last created TUS Location with offset :offset and data :data using the WebDAV API
*
* @param string $user
* @param string $offset
* @param string $data
*
* @return void
*
* @throws GuzzleException
* @throws JsonException
*/
public function userSendsAChunkToTUSLocationWithOffsetAndData ( string $user , string $offset , string $data ) : void {
2024-10-03 11:39:38 +05:45
$resourceLocation = $this -> getLastTusResourceLocation ();
$response = $this -> uploadChunkToTUSLocation ( $user , $resourceLocation , $offset , $data );
2023-09-25 13:32:42 +05:45
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @When user :user uploads file :source to :destination using the TUS protocol on the WebDAV API
*
* @param string|null $user
* @param string $source
* @param string $destination
* @param array $uploadMetadata array of metadata to be placed in the
* `Upload-Metadata` header.
* see https://tus.io/protocols/resumable-upload.html#upload-metadata
* Don't Base64 encode the value.
* @param int $noOfChunks
* @param int|null $bytes
* @param string $checksum
*
* @return void
* @throws ConnectionException
* @throws GuzzleException
* @throws JsonException
* @throws ReflectionException
* @throws TusException
*/
public function userUploadsUsingTusAFileTo (
? string $user ,
2025-06-19 12:12:17 +05:45
string $source ,
string $destination ,
array $uploadMetadata = [],
int $noOfChunks = 1 ,
2025-06-24 11:39:50 +02:00
? int $bytes = null ,
2025-06-19 15:34:02 +05:45
string $checksum = '' ,
2022-12-21 17:22:22 +05:45
) : void {
2024-12-24 15:30:14 +05:45
$response = $this -> uploadFileUsingTus (
$user ,
$source ,
$destination ,
null ,
$uploadMetadata ,
$noOfChunks ,
$bytes ,
2025-06-19 15:34:02 +05:45
$checksum ,
2024-12-24 15:30:14 +05:45
);
2024-04-05 11:31:46 +05:45
$this -> featureContext -> setLastUploadDeleteTime ( \time ());
2024-12-24 15:30:14 +05:45
$this -> featureContext -> setResponse ( $response );
2024-04-05 11:31:46 +05:45
}
/**
* @param string $user
* @param string $source
* @param string $destination
2024-09-18 11:26:20 +05:45
* @param string|null $spaceId
2024-04-05 11:31:46 +05:45
* @param array $uploadMetadata
* @param integer $noOfChunks
2025-06-24 11:39:50 +02:00
* @param integer|null $bytes
2024-04-05 11:31:46 +05:45
* @param string $checksum
*
2024-12-24 15:30:14 +05:45
* @return ResponseInterface
2024-04-05 11:31:46 +05:45
*/
public function uploadFileUsingTus (
? string $user ,
2025-06-19 12:12:17 +05:45
string $source ,
string $destination ,
? string $spaceId = null ,
array $uploadMetadata = [],
int $noOfChunks = 1 ,
2025-06-24 11:39:50 +02:00
? int $bytes = null ,
2025-06-19 12:12:17 +05:45
string $checksum = '' ,
2024-12-24 15:30:14 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$password = $this -> featureContext -> getUserPassword ( $user );
$headers = [
2025-06-19 10:46:56 +05:45
'Authorization' => 'Basic ' . \base64_encode ( $user . ':' . $password ),
2022-12-21 17:22:22 +05:45
];
if ( $bytes !== null ) {
$creationWithUploadHeader = [
'Content-Type' => 'application/offset+octet-stream' ,
2025-06-19 10:46:56 +05:45
'Tus-Resumable' => '1.0.0' ,
2022-12-21 17:22:22 +05:45
];
$headers = \array_merge ( $headers , $creationWithUploadHeader );
}
if ( $checksum != '' ) {
$checksumHeader = [
2025-06-19 10:46:56 +05:45
'Upload-Checksum' => $checksum ,
2022-12-21 17:22:22 +05:45
];
$headers = \array_merge ( $headers , $checksumHeader );
}
2024-09-17 17:11:20 +05:45
$davPathVersion = $this -> featureContext -> 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 ?: $this -> featureContext -> getPersonalSpaceIdForUser ( $user );
2024-09-17 17:11:20 +05:45
}
2022-12-21 17:22:22 +05:45
$sourceFile = $this -> featureContext -> acceptanceTestsDirLocation () . $source ;
2024-12-24 15:30:14 +05:45
$client = $this -> createTusClient (
$this -> featureContext -> getBaseUrl (),
$headers ,
$sourceFile ,
$destination ,
WebDavHelper :: getDavPath ( $davPathVersion , $suffixPath ),
2025-06-19 15:34:02 +05:45
$uploadMetadata ,
2024-12-24 15:30:14 +05:45
);
2022-12-21 17:22:22 +05:45
$this -> featureContext -> pauseUploadDelete ();
2024-12-24 15:30:14 +05:45
$response = null ;
2022-12-21 17:22:22 +05:45
if ( $bytes !== null ) {
2024-12-24 15:30:14 +05:45
return $client -> file ( $sourceFile , $destination )
-> createWithUploadRR ( $client -> getKey (), $bytes );
2024-01-22 20:01:10 +05:45
} elseif ( \filesize ( $sourceFile ) === 0 ) {
2024-12-24 15:30:14 +05:45
return $client -> file ( $sourceFile , $destination ) -> createWithUploadRR ( $client -> getKey (), 0 );
2022-12-21 17:22:22 +05:45
} elseif ( $noOfChunks === 1 ) {
2024-12-24 15:30:14 +05:45
return $client -> file ( $sourceFile , $destination ) -> uploadRR ();
}
2025-06-19 10:58:37 +05:45
$bytesPerChunk = ( int ) \ceil ( \filesize ( $sourceFile ) / $noOfChunks );
for ( $i = 0 ; $i < $noOfChunks ; $i ++ ) {
$response = $client -> uploadRR ( $bytesPerChunk );
}
return $response ;
2024-12-24 15:30:14 +05:45
}
/**
* @param string $source
* @param string $destination
* @param string $password
*
* @return ResponseInterface
* @throws GuzzleException
* @throws ReflectionException
*/
public function publicUploadFileUsingTus (
string $source ,
string $destination ,
string $password ,
) : ResponseInterface {
$password = $this -> featureContext -> getActualPassword ( $password );
if ( $this -> featureContext -> isUsingSharingNG ()) {
$token = $this -> featureContext -> shareNgGetLastCreatedLinkShareToken ();
} else {
$token = $this -> featureContext -> getLastCreatedPublicShareToken ();
2022-12-21 17:22:22 +05:45
}
2024-12-24 15:30:14 +05:45
$headers = [
'Authorization' => 'Basic ' . \base64_encode ( "public: $password " ),
];
$sourceFile = $this -> featureContext -> acceptanceTestsDirLocation () . $source ;
$url = WebdavHelper :: getDavPath ( WebDavHelper :: DAV_VERSION_SPACES , $token , "public-files" );
$client = $this -> createTusClient (
$this -> featureContext -> getBaseUrl (),
$headers ,
$sourceFile ,
$destination ,
2025-06-19 15:34:02 +05:45
$url ,
2024-12-24 15:30:14 +05:45
);
$response = $client -> createWithUploadRR ( "" , 0 );
return $response ;
}
/**
* Creates the TusClient with API path, headers, metadata, upload key and checksum
*
* @param string $baseUrl
* @param array $headers
* @param string $sourceFile
* @param string $destination
* @param string $path
* @param array $metadata
*
* @return TusClient
* @throws ReflectionException
*/
private function createTusClient (
string $baseUrl ,
2025-06-19 12:12:17 +05:45
array $headers ,
2024-12-24 15:30:14 +05:45
string $sourceFile ,
string $destination ,
string $path ,
2025-06-19 15:34:02 +05:45
array $metadata = [],
2024-12-24 15:30:14 +05:45
) : TusClient {
$client = new TusClient (
$baseUrl ,
[
'verify' => false ,
'headers' => $headers ,
2025-06-19 15:34:02 +05:45
],
2024-12-24 15:30:14 +05:45
);
$client -> setApiPath ( $path );
$client -> setMetadata ( $metadata );
$client -> setChecksumAlgorithm ( 'sha1' );
$client -> setKey (( string ) rand ()) -> file ( $sourceFile , $destination );
return $client ;
2022-12-21 17:22:22 +05:45
}
/**
* @When user :user uploads file with content :content to :destination using the TUS protocol on the WebDAV API
*
* @param string $user
* @param string $content
* @param string $destination
*
* @return void
* @throws GuzzleException
* @throws Exception
*/
public function userUploadsAFileWithContentToUsingTus (
string $user ,
string $content ,
2025-06-19 15:34:02 +05:45
string $destination ,
2022-12-21 17:22:22 +05:45
) : void {
2024-04-26 15:27:00 +05:45
$temporaryFileName = $this -> writeDataToTempFile ( $content );
2025-02-11 17:01:57 +05:45
$response = $this -> uploadFileUsingTus (
$user ,
\basename ( $temporaryFileName ),
2025-06-19 15:34:02 +05:45
$destination ,
2025-02-11 17:01:57 +05:45
);
2024-04-26 15:27:00 +05:45
\unlink ( $temporaryFileName );
2025-02-11 17:01:57 +05:45
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @When user :user uploads file with content :content in :noOfChunks chunks to :destination using the TUS protocol on the WebDAV API
*
* @param string|null $user
* @param string $content
* @param int|null $noOfChunks
* @param string $destination
*
* @return void
* @throws ConnectionException
* @throws GuzzleException
* @throws JsonException
* @throws ReflectionException
* @throws TusException
* @throws Exception
* @throws GuzzleException
*/
public function userUploadsAFileWithContentInChunksUsingTus (
? string $user ,
2025-06-19 12:12:17 +05:45
string $content ,
? int $noOfChunks ,
2025-06-19 15:34:02 +05:45
string $destination ,
2022-12-21 17:22:22 +05:45
) : void {
2024-04-26 15:27:00 +05:45
$temporaryFileName = $this -> writeDataToTempFile ( $content );
2025-02-11 17:01:57 +05:45
$response = $this -> uploadFileUsingTus (
2022-12-21 17:22:22 +05:45
$user ,
2024-04-26 15:27:00 +05:45
\basename ( $temporaryFileName ),
2022-12-21 17:22:22 +05:45
$destination ,
2024-09-18 11:26:20 +05:45
null ,
2022-12-21 17:22:22 +05:45
[],
2025-06-19 15:34:02 +05:45
$noOfChunks ,
2022-12-21 17:22:22 +05:45
);
2024-04-05 11:31:46 +05:45
$this -> featureContext -> setLastUploadDeleteTime ( \time ());
2024-04-26 15:27:00 +05:45
\unlink ( $temporaryFileName );
2025-02-11 17:01:57 +05:45
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
2024-11-19 12:19:07 +05:45
/**
* @Given user :user has uploaded file :source to :destination with mtime :mtime using the TUS protocol
*
* @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
* @throws GuzzleException
*/
public function userHasUploadedFileWithMtimeUsingTUS (
string $user ,
string $source ,
string $destination ,
2025-06-19 15:34:02 +05:45
string $mtime ,
2024-11-19 12:19:07 +05:45
) : void {
$mtime = new DateTime ( $mtime );
$mtime = $mtime -> format ( 'U' );
$user = $this -> featureContext -> getActualUsername ( $user );
2025-02-11 17:01:57 +05:45
$response = $this -> uploadFileUsingTus (
2024-11-19 12:19:07 +05:45
$user ,
$source ,
$destination ,
null ,
2025-06-19 15:34:02 +05:45
[ 'mtime' => $mtime ],
2024-11-19 12:19:07 +05:45
);
$this -> featureContext -> setLastUploadDeleteTime ( \time ());
2025-02-11 17:01:57 +05:45
$this -> featureContext -> theHTTPStatusCodeShouldBe (
[ "201" , "204" ],
2025-05-12 09:53:16 +05:45
"Failed to upload file ' $source ' for user ' $user '" ,
2025-06-19 15:34:02 +05:45
$response ,
2025-02-11 17:01:57 +05:45
);
2024-11-19 12:19:07 +05:45
}
2022-12-21 17:22:22 +05:45
/**
* @When user :user uploads file :source to :destination with mtime :mtime using the TUS protocol on 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
2022-12-21 17:22:22 +05:45
*
* @return void
* @throws Exception
* @throws GuzzleException
*/
public function userUploadsFileWithContentToWithMtimeUsingTUS (
string $user ,
string $source ,
string $destination ,
2025-06-19 15:34:02 +05:45
string $mtime ,
2022-12-21 17:22:22 +05:45
) : void {
$mtime = new DateTime ( $mtime );
$mtime = $mtime -> format ( 'U' );
$user = $this -> featureContext -> getActualUsername ( $user );
2025-02-11 17:01:57 +05:45
$response = $this -> uploadFileUsingTus (
2022-12-21 17:22:22 +05:45
$user ,
$source ,
$destination ,
2024-09-18 11:26:20 +05:45
null ,
2025-06-19 15:34:02 +05:45
[ 'mtime' => $mtime ],
2022-12-21 17:22:22 +05:45
);
2024-04-05 11:31:46 +05:45
$this -> featureContext -> setLastUploadDeleteTime ( \time ());
2025-02-11 17:01:57 +05:45
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @param string $content
*
* @return string the file name
* @throws Exception
*/
2024-04-05 11:31:46 +05:45
public function writeDataToTempFile ( string $content ) : string {
2024-04-26 15:27:00 +05:45
$temporaryFileName = \tempnam (
2022-12-21 17:22:22 +05:45
$this -> featureContext -> acceptanceTestsDirLocation (),
2025-06-19 15:34:02 +05:45
"tus-upload-test-" ,
2022-12-21 17:22:22 +05:45
);
2024-04-26 15:27:00 +05:45
if ( $temporaryFileName === false ) {
2022-12-21 17:22:22 +05:45
throw new \Exception ( "could not create a temporary filename" );
}
2024-04-26 15:27:00 +05:45
$temporaryFile = \fopen ( $temporaryFileName , "w" );
if ( $temporaryFile === false ) {
throw new \Exception ( "could not open " . $temporaryFileName . " for write" );
2022-12-21 17:22:22 +05:45
}
2024-04-26 15:27:00 +05:45
\fwrite ( $temporaryFile , $content );
\fclose ( $temporaryFile );
return $temporaryFileName ;
2022-12-21 17:22:22 +05:45
}
/**
* @BeforeScenario
*
* @param BeforeScenarioScope $scope
*
* @return void
*/
2024-10-08 12:26:40 +05:45
public function before ( BeforeScenarioScope $scope ) : void {
2022-12-21 17:22:22 +05:45
// Get the environment
$environment = $scope -> getEnvironment ();
// Get all the contexts you need in this context
2024-10-08 12:26:40 +05:45
$this -> featureContext = BehatHelper :: getContext ( $scope , $environment , 'FeatureContext' );
2024-10-03 11:39:38 +05:45
// clear TUS locations cache
$this -> tusResourceLocations = [];
2022-12-21 17:22:22 +05:45
}
/**
* @When user :user creates a new TUS resource with content :content on the WebDAV API with these headers:
*
* @param string $user
* @param string $content
* @param TableNode $headers
*
* @return void
* @throws Exception
* @throws GuzzleException
*/
public function userCreatesWithUpload (
2025-06-19 12:12:17 +05:45
string $user ,
string $content ,
2025-06-19 15:34:02 +05:45
TableNode $headers ,
2022-12-21 17:22:22 +05:45
) : void {
2023-09-25 13:32:42 +05:45
$response = $this -> createNewTUSResourceWithHeaders ( $user , $headers , $content );
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @When user :user creates file :source and uploads content :content in the same request using the TUS protocol on the WebDAV API
*
* @param string $user
* @param string $source
* @param string $content
*
* @return void
* @throws Exception
*/
public function userUploadsWithCreatesWithUpload (
string $user ,
string $source ,
2025-06-19 15:34:02 +05:45
string $content ,
2022-12-21 17:22:22 +05:45
) : void {
2024-04-26 15:27:00 +05:45
$temporaryFileName = $this -> writeDataToTempFile ( $content );
2025-02-11 17:01:57 +05:45
$response = $this -> uploadFileUsingTus (
2022-12-21 17:22:22 +05:45
$user ,
2024-04-26 15:27:00 +05:45
\basename ( $temporaryFileName ),
2022-12-21 17:22:22 +05:45
$source ,
2024-09-18 11:26:20 +05:45
null ,
2022-12-21 17:22:22 +05:45
[],
1 ,
2025-06-19 15:34:02 +05:45
- 1 ,
2022-12-21 17:22:22 +05:45
);
2024-04-05 11:31:46 +05:45
$this -> featureContext -> setLastUploadDeleteTime ( \time ());
2024-04-26 15:27:00 +05:45
\unlink ( $temporaryFileName );
2025-02-11 17:01:57 +05:45
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @When user :user uploads file with checksum :checksum to the last created TUS Location with offset :offset and content :content using the TUS protocol on the WebDAV API
*
* @param string $user
* @param string $checksum
* @param string $offset
* @param string $content
*
* @return void
* @throws Exception
*/
public function userUploadsFileWithChecksum (
string $user ,
string $checksum ,
string $offset ,
2025-06-19 15:34:02 +05:45
string $content ,
2022-12-21 17:22:22 +05:45
) : void {
2024-10-03 11:39:38 +05:45
$resourceLocation = $this -> getLastTusResourceLocation ();
$response = $this -> uploadChunkToTUSLocation ( $user , $resourceLocation , $offset , $content , $checksum );
$this -> featureContext -> setResponse ( $response );
}
/**
* @When user :user uploads content :content with checksum :checksum and offset :offset to the index :locationIndex location of file :filename using the TUS protocol
* @When user :user tries to upload content :content with checksum :checksum and offset :offset to the index :locationIndex location of file :filename using the TUS protocol
*
* @param string $user
* @param string $content
* @param string $checksum
* @param string $offset
* @param string $locationIndex
* @param string $filename
*
* @return void
* @throws Exception
*/
public function userUploadsContentWithChecksumAndOffsetToIndexLocationUsingTUSProtocol (
string $user ,
string $content ,
string $checksum ,
string $offset ,
string $locationIndex ,
2025-06-19 15:34:02 +05:45
string $filename ,
2024-10-03 11:39:38 +05:45
) : void {
$filenameHash = \base64_encode ( $filename );
$resourceLocation = $this -> getTusResourceLocation ( $filenameHash , ( int ) $locationIndex );
$response = $this -> uploadChunkToTUSLocation ( $user , $resourceLocation , $offset , $content , $checksum );
2023-09-25 13:32:42 +05:45
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @Given user :user has uploaded file with checksum :checksum to the last created TUS Location with offset :offset and content :content using the TUS protocol on the WebDAV API
*
* @param string $user
* @param string $checksum
* @param string $offset
* @param string $content
*
* @return void
* @throws Exception
*/
public function userHasUploadedFileWithChecksum (
string $user ,
string $checksum ,
string $offset ,
2025-06-19 15:34:02 +05:45
string $content ,
2022-12-21 17:22:22 +05:45
) : void {
2024-10-03 11:39:38 +05:45
$resourceLocation = $this -> getLastTusResourceLocation ();
$response = $this -> uploadChunkToTUSLocation ( $user , $resourceLocation , $offset , $content , $checksum );
2023-09-25 13:32:42 +05:45
$this -> featureContext -> theHTTPStatusCodeShouldBe ( 204 , "" , $response );
2022-12-21 17:22:22 +05:45
}
/**
* @When user :user sends a chunk to the last created TUS Location with offset :offset and data :data with checksum :checksum using the TUS protocol on the WebDAV API
*
* @param string $user
* @param string $offset
* @param string $data
* @param string $checksum
*
* @return void
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function userUploadsChunkFileWithChecksum (
string $user ,
string $offset ,
string $data ,
2025-06-19 15:34:02 +05:45
string $checksum ,
2024-12-24 09:30:42 +05:45
) : void {
2024-10-03 11:39:38 +05:45
$resourceLocation = $this -> getLastTusResourceLocation ();
$response = $this -> uploadChunkToTUSLocation ( $user , $resourceLocation , $offset , $data , $checksum );
2023-09-25 13:32:42 +05:45
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @Given user :user has uploaded a chunk to the last created TUS Location with offset :offset and data :data with checksum :checksum using the TUS protocol on the WebDAV API
*
* @param string $user
* @param string $offset
* @param string $data
* @param string $checksum
*
* @return void
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function userHasUploadedChunkFileWithChecksum (
string $user ,
string $offset ,
string $data ,
2025-06-19 15:34:02 +05:45
string $checksum ,
2024-12-24 09:30:42 +05:45
) : void {
2024-10-03 11:39:38 +05:45
$resourceLocation = $this -> getLastTusResourceLocation ();
$response = $this -> uploadChunkToTUSLocation ( $user , $resourceLocation , $offset , $data , $checksum );
2023-09-25 13:32:42 +05:45
$this -> featureContext -> theHTTPStatusCodeShouldBe ( 204 , "" , $response );
2022-12-21 17:22:22 +05:45
}
/**
* @When user :user overwrites recently shared file with offset :offset and data :data with checksum :checksum using the TUS protocol on the WebDAV API with these headers:
* @When user :user overwrites existing file with offset :offset and data :data with checksum :checksum using the TUS protocol on the WebDAV API with these headers:
*
* @param string $user
* @param string $offset
* @param string $data
* @param string $checksum
* @param TableNode $headers Tus-Resumable: 1.0.0 header is added automatically
*
* @return void
*
* @throws GuzzleException
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function userOverwritesFileWithChecksum (
string $user ,
string $offset ,
string $data ,
string $checksum ,
2025-06-19 15:34:02 +05:45
TableNode $headers ,
2024-12-24 09:30:42 +05:45
) : void {
2024-04-05 11:31:46 +05:45
$createResponse = $this -> createNewTUSResource ( $user , $headers );
$this -> featureContext -> theHTTPStatusCodeShouldBe ( 201 , "" , $createResponse );
2024-10-03 11:39:38 +05:45
$resourceLocation = $this -> getLastTusResourceLocation ();
$response = $this -> uploadChunkToTUSLocation ( $user , $resourceLocation , $offset , $data , $checksum );
2023-09-25 13:32:42 +05:45
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
}