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 TestHelpers\HttpRequestHelper ;
use TestHelpers\WebDavHelper ;
use TusPhp\Exception\ConnectionException ;
use TusPhp\Exception\TusException ;
use TusPhp\Tus\Client ;
use PHPUnit\Framework\Assert ;
2023-09-25 13:32:42 +05:45
use Psr\Http\Message\ResponseInterface ;
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
2023-05-02 17:25:56 +05:45
private ? string $resourceLocation = null ;
2022-12-21 17:22:22 +05:45
2024-03-25 15:37:35 +05:45
/**
* @return string
*/
public function getTusResourceLocation () : string {
return $this -> resourceLocation ?: "" ;
}
2022-12-21 17:22:22 +05:45
/**
* @param string $user
* @param TableNode $headers
* @param string $content
*
2023-09-25 13:32:42 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*
* @throws Exception
* @throws GuzzleException
*/
2023-09-25 13:32:42 +05:45
public function createNewTUSResourceWithHeaders ( string $user , TableNode $headers , string $content = '' ) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$this -> featureContext -> verifyTableNodeColumnsCount ( $headers , 2 );
$user = $this -> featureContext -> getActualUsername ( $user );
$password = $this -> featureContext -> getUserPassword ( $user );
$this -> resourceLocation = null ;
2023-09-25 13:32:42 +05:45
$response = $this -> featureContext -> makeDavRequest (
$user ,
"POST" ,
null ,
$headers -> getRowsHash (),
$content ,
"files" ,
null ,
false ,
$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 ) {
$this -> resourceLocation = $locationHeader [ 0 ];
}
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
*
* @return ResponseInterface
*/
public function createNewTUSResource ( string $user , TableNode $headers ) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$rows = $headers -> getRows ();
$rows [] = [ 'Tus-Resumable' , '1.0.0' ];
2024-04-05 11:31:46 +05:45
return $this -> createNewTUSResourceWithHeaders ( $user , new TableNode ( $rows ));
2022-12-21 17:22:22 +05:45
}
/**
* @param string $user
* @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-02-23 11:47:52 +01:00
public function sendsAChunkToTUSLocationWithOffsetAndData ( string $user , string $offset , string $data , string $checksum = '' , ? array $extraHeaders = null ) : 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 = [
'Content-Type' => 'application/offset+octet-stream' ,
'Tus-Resumable' => '1.0.0' ,
'Upload-Checksum' => $checksum ,
'Upload-Offset' => $offset
];
$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 (
$this -> resourceLocation ,
$this -> featureContext -> getStepLineRef (),
'PATCH' ,
$user ,
$password ,
2024-02-23 11:47:52 +01:00
$headers ,
2023-09-25 13:32:42 +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 {
$response = $this -> sendsAChunkToTUSLocationWithOffsetAndData ( $user , $offset , $data );
$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 ,
string $source ,
string $destination ,
array $uploadMetadata = [],
int $noOfChunks = 1 ,
int $bytes = null ,
string $checksum = ''
) : void {
2024-04-05 11:31:46 +05:45
$this -> uploadFileUsingTus ( $user , $source , $destination , $uploadMetadata , $noOfChunks , $bytes , $checksum );
$this -> featureContext -> setLastUploadDeleteTime ( \time ());
}
/**
* @param string $user
* @param string $source
* @param string $destination
* @param array $uploadMetadata
* @param integer $noOfChunks
* @param integer $bytes
* @param string $checksum
*
* @return void
*/
public function uploadFileUsingTus (
? string $user ,
string $source ,
string $destination ,
array $uploadMetadata = [],
int $noOfChunks = 1 ,
int $bytes = null ,
string $checksum = ''
) {
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$password = $this -> featureContext -> getUserPassword ( $user );
$headers = [
'Authorization' => 'Basic ' . \base64_encode ( $user . ':' . $password )
];
if ( $bytes !== null ) {
$creationWithUploadHeader = [
'Content-Type' => 'application/offset+octet-stream' ,
'Tus-Resumable' => '1.0.0'
];
$headers = \array_merge ( $headers , $creationWithUploadHeader );
}
if ( $checksum != '' ) {
$checksumHeader = [
'Upload-Checksum' => $checksum
];
$headers = \array_merge ( $headers , $checksumHeader );
}
$client = new Client (
$this -> featureContext -> getBaseUrl (),
2024-01-22 20:01:10 +05:45
[
'verify' => false ,
2022-12-21 17:22:22 +05:45
'headers' => $headers
]
);
$client -> setApiPath (
WebDavHelper :: getDavPath (
$user ,
$this -> featureContext -> getDavPathVersion (),
"files" ,
WebDavHelper :: $SPACE_ID_FROM_OCIS
2023-05-02 17:25:56 +05:45
?: $this -> featureContext -> getPersonalSpaceIdForUser ( $user )
2022-12-21 17:22:22 +05:45
)
);
WebDavHelper :: $SPACE_ID_FROM_OCIS = '' ;
$client -> setMetadata ( $uploadMetadata );
$sourceFile = $this -> featureContext -> acceptanceTestsDirLocation () . $source ;
$client -> setKey (( string ) rand ()) -> file ( $sourceFile , $destination );
$this -> featureContext -> pauseUploadDelete ();
if ( $bytes !== null ) {
$client -> file ( $sourceFile , $destination ) -> createWithUpload ( $client -> getKey (), $bytes );
2024-01-22 20:01:10 +05:45
} elseif ( \filesize ( $sourceFile ) === 0 ) {
$client -> file ( $sourceFile , $destination ) -> createWithUpload ( $client -> getKey (), 0 );
2022-12-21 17:22:22 +05:45
} elseif ( $noOfChunks === 1 ) {
$client -> file ( $sourceFile , $destination ) -> upload ();
} else {
$bytesPerChunk = ( int ) \ceil ( \filesize ( $sourceFile ) / $noOfChunks );
for ( $i = 0 ; $i < $noOfChunks ; $i ++ ) {
$client -> upload ( $bytesPerChunk );
}
}
}
/**
* @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 ,
string $destination
) : void {
2024-04-26 15:27:00 +05:45
$temporaryFileName = $this -> writeDataToTempFile ( $content );
2022-12-21 17:22:22 +05:45
try {
2024-04-05 11:31:46 +05:45
$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-04-05 11:31:46 +05:45
$this -> featureContext -> setLastUploadDeleteTime ( \time ());
2022-12-21 17:22:22 +05:45
} catch ( Exception $e ) {
Assert :: assertStringContainsString ( 'TusPhp\Exception\FileException: Unable to create resource' , ( string ) $e );
}
2024-04-26 15:27:00 +05:45
\unlink ( $temporaryFileName );
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 ,
string $content ,
? int $noOfChunks ,
string $destination
) : void {
2024-04-26 15:27:00 +05:45
$temporaryFileName = $this -> writeDataToTempFile ( $content );
2024-04-05 11:31:46 +05:45
$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 ,
[],
$noOfChunks
);
2024-04-05 11:31:46 +05:45
$this -> featureContext -> setLastUploadDeleteTime ( \time ());
2024-04-26 15:27:00 +05:45
\unlink ( $temporaryFileName );
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 ,
string $mtime
) : void {
$mtime = new DateTime ( $mtime );
$mtime = $mtime -> format ( 'U' );
$user = $this -> featureContext -> getActualUsername ( $user );
2024-04-05 11:31:46 +05:45
$this -> uploadFileUsingTus (
2022-12-21 17:22:22 +05:45
$user ,
$source ,
$destination ,
[ 'mtime' => $mtime ]
);
2024-04-05 11:31:46 +05:45
$this -> featureContext -> setLastUploadDeleteTime ( \time ());
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 (),
"tus-upload-test-"
);
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
*/
public function setUpScenario ( BeforeScenarioScope $scope ) : void {
// Get the environment
$environment = $scope -> getEnvironment ();
// Get all the contexts you need in this context
$this -> featureContext = $environment -> getContext ( 'FeatureContext' );
}
/**
* @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 (
string $user ,
string $content ,
TableNode $headers
) : 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 ,
string $content
) : void {
2024-04-26 15:27:00 +05:45
$temporaryFileName = $this -> writeDataToTempFile ( $content );
2024-04-05 11:31:46 +05:45
$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 ,
[],
1 ,
- 1
);
2024-04-05 11:31:46 +05:45
$this -> featureContext -> setLastUploadDeleteTime ( \time ());
2024-04-26 15:27:00 +05:45
\unlink ( $temporaryFileName );
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 ,
string $content
) : void {
2023-09-25 13:32:42 +05:45
$response = $this -> sendsAChunkToTUSLocationWithOffsetAndData ( $user , $offset , $content , $checksum );
$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 ,
string $content
) : void {
2023-09-25 13:32:42 +05:45
$response = $this -> sendsAChunkToTUSLocationWithOffsetAndData ( $user , $offset , $content , $checksum );
$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
*/
public function userUploadsChunkFileWithChecksum ( string $user , string $offset , string $data , string $checksum ) : void {
2023-09-25 13:32:42 +05:45
$response = $this -> sendsAChunkToTUSLocationWithOffsetAndData ( $user , $offset , $data , $checksum );
$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
*/
public function userHasUploadedChunkFileWithChecksum ( string $user , string $offset , string $data , string $checksum ) : void {
2023-09-25 13:32:42 +05:45
$response = $this -> sendsAChunkToTUSLocationWithOffsetAndData ( $user , $offset , $data , $checksum );
$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
*/
public function userOverwritesFileWithChecksum ( string $user , string $offset , string $data , string $checksum , TableNode $headers ) : void {
2024-04-05 11:31:46 +05:45
$createResponse = $this -> createNewTUSResource ( $user , $headers );
$this -> featureContext -> theHTTPStatusCodeShouldBe ( 201 , "" , $createResponse );
2023-09-25 13:32:42 +05:45
$response = $this -> sendsAChunkToTUSLocationWithOffsetAndData ( $user , $offset , $data , $checksum );
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
}