2022-08-23 17:05:12 +05:45
<? php
declare ( strict_types = 1 );
2022-08-23 09:26:30 +05:45
2022-08-23 17:05:12 +05:45
/**
* ownCloud
*
* @author Viktor Scharf <v.scharf@owncloud.com>
* @copyright Copyright (c) 2022 Viktor Scharf v.scharf@owncloud.com
*/
use Behat\Behat\Context\Context ;
use Behat\Behat\Hook\Scope\BeforeScenarioScope ;
use GuzzleHttp\Exception\GuzzleException ;
use Behat\Gherkin\Node\TableNode ;
2024-04-05 11:31:46 +05:45
use PHPUnit\Framework\Assert ;
2024-05-30 17:07:09 +05:45
use TestHelpers\WebDavHelper ;
2022-08-23 17:05:12 +05:45
require_once 'bootstrap.php' ;
/**
* Context for the TUS-specific steps using the Graph API
*/
class SpacesTUSContext implements Context {
private FeatureContext $featureContext ;
private TUSContext $tusContext ;
private SpacesContext $spacesContext ;
/**
* This will run before EVERY scenario.
* It will set the properties for this object.
*
* @BeforeScenario
*
* @param BeforeScenarioScope $scope
*
* @return void
*/
public function before ( BeforeScenarioScope $scope ) : void {
// Get the environment
$environment = $scope -> getEnvironment ();
// Get all the contexts you need in this context from here
$this -> featureContext = $environment -> getContext ( 'FeatureContext' );
$this -> spacesContext = $environment -> getContext ( 'SpacesContext' );
$this -> tusContext = $environment -> getContext ( 'TUSContext' );
}
/**
* @Given /^user "([^"]*)" has uploaded a file from "([^"]*)" to "([^"]*)" via TUS inside of the space "([^"]*)" using the WebDAV API$/
*
* @param string $user
* @param string $source
* @param string $destination
* @param string $spaceName
*
* @return void
*
* @throws Exception
* @throws GuzzleException
*/
public function userHasUploadedFileViaTusInSpace ( string $user , string $source , string $destination , string $spaceName ) : void {
2023-10-12 16:30:51 +05:45
$this -> spacesContext -> setSpaceIDByName ( $user , $spaceName );
2024-04-05 11:31:46 +05:45
$this -> tusContext -> uploadFileUsingTus ( $user , $source , $destination );
$this -> featureContext -> setLastUploadDeleteTime ( \time ());
2022-08-23 17:05:12 +05:45
}
/**
* @When /^user "([^"]*)" uploads a file from "([^"]*)" to "([^"]*)" via TUS inside of the space "([^"]*)" using the WebDAV API$/
*
* @param string $user
* @param string $source
* @param string $destination
2022-09-06 12:32:56 +05:45
* @param string $spaceName
2022-08-23 17:05:12 +05:45
*
* @return void
* @throws Exception
* @throws GuzzleException
*/
public function userUploadsAFileViaTusInsideOfTheSpaceUsingTheWebdavApi (
string $user ,
string $source ,
string $destination ,
2023-05-02 17:25:56 +05:45
string $spaceName
2022-08-23 17:05:12 +05:45
) : void {
$this -> spacesContext -> setSpaceIDByName ( $user , $spaceName );
2024-04-05 11:31:46 +05:45
$this -> tusContext -> uploadFileUsingTus ( $user , $source , $destination );
$this -> featureContext -> setLastUploadDeleteTime ( \time ());
2022-08-23 17:05:12 +05:45
}
/**
2023-01-06 08:59:02 +01:00
* @Given user :user has created a new TUS resource for the space :spaceName with content :content using the WebDAV API with these headers:
2022-08-23 17:05:12 +05:45
*
* @param string $user
* @param string $spaceName
2023-01-06 08:59:02 +01:00
* @param string $content
2022-08-23 17:05:12 +05:45
* @param TableNode $headers
*
* @return void
*
* @throws Exception
* @throws GuzzleException
*/
public function userHasCreatedANewTusResourceForTheSpaceUsingTheWebdavApiWithTheseHeaders (
string $user ,
string $spaceName ,
2023-01-06 08:59:02 +01:00
string $content ,
2022-08-23 17:05:12 +05:45
TableNode $headers
) : void {
2023-10-12 16:30:51 +05:45
$this -> spacesContext -> setSpaceIDByName ( $user , $spaceName );
$response = $this -> tusContext -> createNewTUSResourceWithHeaders ( $user , $headers , $content );
$this -> featureContext -> theHTTPStatusCodeShouldBe ( 201 , "Expected response status code should be 201" , $response );
2022-08-23 17:05:12 +05:45
}
/**
2023-01-06 08:59:02 +01:00
* @When user :user creates a new TUS resource for the space :spaceName with content :content using the WebDAV API with these headers:
2022-08-23 17:05:12 +05:45
*
* @param string $user
* @param string $spaceName
2023-01-06 08:59:02 +01:00
* @param string $content
2022-08-23 17:05:12 +05:45
* @param TableNode $headers
*
* @return void
*
* @throws Exception
* @throws GuzzleException
*/
public function userCreatesANewTusResourceForTheSpaceUsingTheWebdavApiWithTheseHeaders (
string $user ,
string $spaceName ,
2023-01-06 08:59:02 +01:00
string $content ,
2022-08-23 17:05:12 +05:45
TableNode $headers
) : void {
$this -> spacesContext -> setSpaceIDByName ( $user , $spaceName );
2023-09-25 13:32:42 +05:45
$response = $this -> tusContext -> createNewTUSResourceWithHeaders ( $user , $headers , $content );
$this -> featureContext -> setResponse ( $response );
2022-08-23 17:05:12 +05:45
}
/**
2024-05-16 15:03:21 +05:45
* Uploads a file with content to the specified space using the TUS protocol via the WebDAV API.
2022-08-23 17:05:12 +05:45
*
* @param string $user
* @param string $content
* @param string $resource
* @param string $spaceName
*
* @return void
* @throws Exception|GuzzleException
*/
2024-05-16 15:03:21 +05:45
private function uploadFileViaTus ( string $user , string $content , string $resource , string $spaceName ) : void {
2022-08-23 17:05:12 +05:45
$this -> spacesContext -> setSpaceIDByName ( $user , $spaceName );
2024-04-05 11:31:46 +05:45
$tmpFile = $this -> tusContext -> writeDataToTempFile ( $content );
try {
$this -> tusContext -> uploadFileUsingTus (
$user ,
\basename ( $tmpFile ),
$resource
);
$this -> featureContext -> setLastUploadDeleteTime ( \time ());
} catch ( Exception $e ) {
Assert :: assertStringContainsString ( 'Unable to create resource' , ( string ) $e );
}
\unlink ( $tmpFile );
2022-08-23 17:05:12 +05:45
}
2022-08-23 09:26:30 +05:45
2024-05-16 15:03:21 +05:45
/**
* @When /^user "([^"]*)" uploads a file with content "([^"]*)" to "([^"]*)" via TUS inside of the space "([^"]*)" using the WebDAV API$/
*
* @param string $user
* @param string $content
* @param string $resource
* @param string $spaceName
*
* @return void
* @throws Exception|GuzzleException
*/
public function userUploadsAFileWithContentToViaTusInsideOfTheSpaceUsingTheWebdavApi (
string $user ,
string $content ,
string $resource ,
string $spaceName
) : void {
$this -> uploadFileViaTus ( $user , $content , $resource , $spaceName );
}
2023-05-24 17:12:19 +02:00
/**
2023-10-12 16:30:51 +05:45
* @Given /^user "([^"]*)" has uploaded a file with content "([^"]*)" to "([^"]*)" via TUS inside of the space "([^"]*)"$/
2023-05-24 17:12:19 +02:00
*
* @param string $user
* @param string $content
* @param string $resource
* @param string $spaceName
*
* @return void
* @throws Exception|GuzzleException
*/
public function userHasUploadedAFileWithContentToViaTusInsideOfTheSpace (
string $user ,
string $content ,
string $resource ,
string $spaceName
) : void {
2024-05-16 15:03:21 +05:45
$this -> uploadFileViaTus ( $user , $content , $resource , $spaceName );
2023-05-24 17:12:19 +02:00
}
2022-08-23 09:26:30 +05:45
/**
* @When /^user "([^"]*)" uploads a file "([^"]*)" to "([^"]*)" with mtime "([^"]*)" via TUS inside of the space "([^"]*)" 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
2022-08-23 09:26:30 +05:45
* @param string $spaceName
*
* @return void
*
* @throws Exception
* @throws GuzzleException
*/
public function userUploadsAFileToWithMtimeViaTusInsideOfTheSpaceUsingTheWebdavApi (
string $user ,
string $source ,
string $destination ,
string $mtime ,
string $spaceName
) : void {
2023-09-21 17:17:28 +02:00
switch ( $mtime ) {
case "today" :
$mtime = date ( 'Y-m-d' , strtotime ( 'today' ));
break ;
case "yesterday" :
$mtime = date ( 'Y-m-d' , strtotime ( 'yesterday' ));
break ;
case "lastWeek" :
$mtime = date ( 'Y-m-d' , strtotime ( '-7 days' ));
break ;
case "lastMonth" :
2023-10-31 17:34:32 +05:45
$mtime = date ( 'Y-m-d' , strtotime ( 'first day of previous month' ));
2023-09-21 17:17:28 +02:00
break ;
case "lastYear" :
$mtime = date ( 'Y-m' . '-01' , strtotime ( '-1 year' ));
break ;
default :
}
2022-08-23 09:26:30 +05:45
$this -> spacesContext -> setSpaceIDByName ( $user , $spaceName );
2024-04-05 11:31:46 +05:45
$mtime = new DateTime ( $mtime );
$mtime = $mtime -> format ( 'U' );
$user = $this -> featureContext -> getActualUsername ( $user );
$this -> tusContext -> uploadFileUsingTus (
$user ,
$source ,
$destination ,
[ 'mtime' => $mtime ]
);
$this -> featureContext -> setLastUploadDeleteTime ( \time ());
2022-08-23 09:26:30 +05:45
}
/**
* @Given /^user "([^"]*)" has uploaded file with checksum "([^"]*)" to the last created TUS Location with offset "([^"]*)" and content "([^"]*)" via TUS inside of the space "([^"]*)" using the WebDAV API$/
*
* @param string $user
* @param string $checksum
* @param string $offset
* @param string $content
* @param string $spaceName
*
* @return void
* @throws Exception|GuzzleException
*/
public function userHasUploadedFileWithChecksumToTheLastCreatedTusLocationWithOffsetAndContentViaTusInsideOfTheSpaceUsingTheWebdavApi (
string $user ,
string $checksum ,
string $offset ,
string $content ,
string $spaceName
) : void {
$this -> spacesContext -> setSpaceIDByName ( $user , $spaceName );
2024-04-05 11:31:46 +05:45
$response = $this -> tusContext -> sendsAChunkToTUSLocationWithOffsetAndData ( $user , $offset , $content , $checksum );
$this -> featureContext -> theHTTPStatusCodeShouldBe ( 204 , "" , $response );
2022-08-23 09:26:30 +05:45
}
/**
2023-10-12 16:30:51 +05:45
* @When /^user "([^"]*)" uploads file with checksum "([^"]*)" to the last created TUS Location with offset "([^"]*)" and content "([^"]*)" via TUS inside of the space "([^"]*)" using the WebDAV API$/
2022-08-23 09:26:30 +05:45
*
* @param string $user
* @param string $checksum
* @param string $offset
* @param string $content
* @param string $spaceName
*
* @return void
* @throws Exception|GuzzleException
*/
public function userUploadsFileWithChecksumToTheLastCreatedTusLocationWithOffsetAndContentViaTusInsideOfTheSpaceUsingTheWebdavApi (
string $user ,
string $checksum ,
string $offset ,
string $content ,
string $spaceName
) : void {
$this -> spacesContext -> setSpaceIDByName ( $user , $spaceName );
2023-10-12 16:30:51 +05:45
$response = $this -> tusContext -> sendsAChunkToTUSLocationWithOffsetAndData ( $user , $offset , $content , $checksum );
$this -> featureContext -> setResponse ( $response );
2022-08-23 09:26:30 +05:45
}
/**
* @When /^user "([^"]*)" sends a chunk to the last created TUS Location with offset "([^"]*)" and data "([^"]*)" with checksum "([^"]*)" via TUS inside of the space "([^"]*)" using the WebDAV API$/
*
* @param string $user
* @param string $offset
* @param string $data
* @param string $checksum
* @param string $spaceName
*
* @return void
* @throws Exception|GuzzleException
*/
public function userSendsAChunkToTheLastCreatedTusLocationWithOffsetAndDataWithChecksumViaTusInsideOfTheSpaceUsingTheWebdavApi (
string $user ,
string $offset ,
string $data ,
string $checksum ,
string $spaceName
) : void {
$this -> spacesContext -> setSpaceIDByName ( $user , $spaceName );
2024-04-05 11:31:46 +05:45
$response = $this -> tusContext -> sendsAChunkToTUSLocationWithOffsetAndData ( $user , $offset , $data , $checksum );
$this -> featureContext -> setResponse ( $response );
2022-08-23 09:26:30 +05:45
}
2024-02-23 11:47:52 +01:00
/**
* @When /^user "([^"]*)" sends a chunk to the last created TUS Location with data "([^"]*)" inside of the space "([^"]*)" with headers:$/
*
* @param string $user
* @param string $data
* @param string $spaceName
* @param TableNode $headers
*
* @return void
* @throws Exception|GuzzleException
*/
public function userSendsAChunkToTheLastCreatedTusLocationWithDataInsideOfTheSpaceWithHeaders (
string $user ,
string $data ,
string $spaceName ,
TableNode $headers
) : void {
$this -> spacesContext -> setSpaceIDByName ( $user , $spaceName );
$rows = $headers -> getRowsHash ();
$response = $this -> tusContext -> sendsAChunkToTUSLocationWithOffsetAndData ( $user , $rows [ 'Upload-Offset' ], $data , $rows [ 'Upload-Checksum' ], [ 'Origin' => $rows [ 'Origin' ]]);
$this -> featureContext -> setResponse ( $response );
}
2022-08-23 09:26:30 +05:45
/**
* @When /^user "([^"]*)" overwrites recently shared file with offset "([^"]*)" and data "([^"]*)" with checksum "([^"]*)" via TUS inside of the space "([^"]*)" using the WebDAV API with these headers:$/
*
* @param string $user
* @param string $offset
* @param string $data
* @param string $checksum
* @param string $spaceName
* @param TableNode $headers
*
* @return void
* @throws GuzzleException
*/
public function userOverwritesRecentlySharedFileWithOffsetAndDataWithChecksumViaTusInsideOfTheSpaceUsingTheWebdavApiWithTheseHeaders (
string $user ,
string $offset ,
string $data ,
string $checksum ,
string $spaceName ,
TableNode $headers
) : void {
$this -> spacesContext -> setSpaceIDByName ( $user , $spaceName );
2024-04-05 11:31:46 +05:45
$createResponse = $this -> tusContext -> createNewTUSResource ( $user , $headers );
$this -> featureContext -> theHTTPStatusCodeShouldBe ( 201 , "" , $createResponse );
$response = $this -> tusContext -> sendsAChunkToTUSLocationWithOffsetAndData ( $user , $offset , $data , $checksum );
$this -> featureContext -> setResponse ( $response );
2022-08-23 09:26:30 +05:45
}
/**
* @Then /^as "([^"]*)" the mtime of the file "([^"]*)" in space "([^"]*)" should be "([^"]*)"$/
*
* @param string $user
* @param string $resource
* @param string $spaceName
* @param string $mtime
*
* @return void
* @throws Exception|GuzzleException
*/
public function theMtimeOfTheFileInSpaceShouldBe (
string $user ,
string $resource ,
string $spaceName ,
string $mtime
) : void {
$this -> spacesContext -> setSpaceIDByName ( $user , $spaceName );
2024-05-30 17:07:09 +05:45
$mtime = new DateTime ( $mtime );
Assert :: assertEquals (
$mtime -> format ( 'U' ),
WebDavHelper :: getMtimeOfResource (
$this -> featureContext -> getActualUsername ( $user ),
$this -> featureContext -> getPasswordForUser ( $user ),
$this -> featureContext -> getBaseUrl (),
$resource ,
$this -> featureContext -> getStepLineRef (),
$this -> featureContext -> getDavPathVersion ()
)
);
2022-08-23 09:26:30 +05:45
}
2022-08-23 17:05:12 +05:45
}