2022-12-21 17:22:22 +05:45
<? php declare ( strict_types = 1 );
/**
* @author Vincent Petry <pvince81@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\Behat\Context\Context ;
use Behat\Behat\Hook\Scope\BeforeScenarioScope ;
use Behat\Gherkin\Node\TableNode ;
2023-05-02 17:25:56 +05:45
use GuzzleHttp\Exception\GuzzleException ;
2022-12-21 17:22:22 +05:45
use PHPUnit\Framework\Assert ;
use Psr\Http\Message\ResponseInterface ;
use TestHelpers\HttpRequestHelper ;
use TestHelpers\WebDavHelper ;
2024-10-08 12:26:40 +05:45
use TestHelpers\BehatHelper ;
2022-12-21 17:22:22 +05:45
require_once 'bootstrap.php' ;
/**
* Trashbin context
*/
class TrashbinContext implements Context {
2023-05-02 17:25:56 +05:45
private FeatureContext $featureContext ;
2022-12-21 17:22:22 +05:45
/**
* @param string|null $user user
*
* @return ResponseInterface
*/
2024-12-24 09:30:42 +05:45
public function emptyTrashbin ( ? string $user ) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$davPathVersion = $this -> featureContext -> getDavPathVersion ();
2023-09-15 15:31:25 +05:45
return WebDavHelper :: makeDavRequest (
2022-12-21 17:22:22 +05:45
$this -> featureContext -> getBaseUrl (),
$user ,
$this -> featureContext -> getPasswordForUser ( $user ),
'DELETE' ,
null ,
[],
2024-09-18 11:26:20 +05:45
null ,
2022-12-21 17:22:22 +05:45
null ,
$davPathVersion ,
2025-06-19 15:34:02 +05:45
'trash-bin' ,
2022-12-21 17:22:22 +05:45
);
}
2023-09-15 15:31:25 +05:45
/**
* @When user :user empties the trashbin using the trashbin API
*
* @param string $user user
*
* @return void
*/
public function userEmptiesTrashbin ( string $user ) : void {
$this -> featureContext -> setResponse ( $this -> emptyTrashbin ( $user ));
}
2022-12-21 17:22:22 +05:45
/**
* @Given user :user has emptied the trashbin
*
* @param string $user user
*
* @return void
*/
2024-12-24 09:30:42 +05:45
public function userHasEmptiedTrashbin ( string $user ) : void {
2022-12-21 17:22:22 +05:45
$response = $this -> emptyTrashbin ( $user );
2023-09-15 15:31:25 +05:45
$this -> featureContext -> theHTTPStatusCodeShouldBe ( 204 , '' , $response );
2022-12-21 17:22:22 +05:45
}
/**
* Get files list from the response from trashbin api
*
2024-11-29 13:19:54 +05:45
* @param SimpleXMLElement|null $responseXmlObject
2022-12-21 17:22:22 +05:45
*
* @return array
*/
2024-11-29 13:19:54 +05:45
public function getTrashbinContentFromResponseXml ( ? SimpleXMLElement $responseXmlObject ) : array {
$xmlElements = $responseXmlObject -> xpath ( '//d:response' );
2022-12-21 17:22:22 +05:45
$files = \array_map (
static function ( SimpleXMLElement $element ) {
$href = $element -> xpath ( './d:href' )[ 0 ];
$propStats = $element -> xpath ( './d:propstat' );
$successPropStat = \array_filter (
$propStats ,
static function ( SimpleXMLElement $propStat ) {
$status = $propStat -> xpath ( './d:status' );
return ( string ) $status [ 0 ] === 'HTTP/1.1 200 OK' ;
2025-06-19 15:34:02 +05:45
},
2022-12-21 17:22:22 +05:45
);
if ( isset ( $successPropStat [ 0 ])) {
$successPropStat = $successPropStat [ 0 ];
$name = $successPropStat -> xpath ( './d:prop/oc:trashbin-original-filename' );
$mtime = $successPropStat -> xpath ( './d:prop/oc:trashbin-delete-timestamp' );
$resourcetype = $successPropStat -> xpath ( './d:prop/d:resourcetype' );
2024-12-24 09:30:42 +05:45
if ( \array_key_exists ( 0 , $resourcetype )
&& ( $resourcetype [ 0 ] -> asXML () === "<d:resourcetype><d:collection/></d:resourcetype>" )
) {
2022-12-21 17:22:22 +05:45
$collection [ 0 ] = true ;
} else {
$collection [ 0 ] = false ;
}
$originalLocation = $successPropStat -> xpath ( './d:prop/oc:trashbin-original-location' );
} else {
$name = [];
$mtime = [];
$collection = [];
$originalLocation = [];
}
return [
'href' => ( string ) $href ,
'name' => isset ( $name [ 0 ]) ? ( string ) $name [ 0 ] : null ,
'mtime' => isset ( $mtime [ 0 ]) ? ( string ) $mtime [ 0 ] : null ,
2024-04-26 15:27:00 +05:45
'collection' => $collection [ 0 ] ?? false ,
2025-06-19 10:46:56 +05:45
'original-location' => isset ( $originalLocation [ 0 ]) ? ( string ) $originalLocation [ 0 ] : null ,
2022-12-21 17:22:22 +05:45
];
},
2025-06-19 15:34:02 +05:45
$xmlElements ,
2022-12-21 17:22:22 +05:45
);
return $files ;
}
/**
* List the top of the trashbin folder for a user
*
* @param string|null $user user
* @param string $depth
*
* @return array response
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function listTopOfTrashbinFolder ( ? string $user , string $depth = "1" ) : array {
2022-12-21 17:22:22 +05:45
$password = $this -> featureContext -> getPasswordForUser ( $user );
$davPathVersion = $this -> featureContext -> getDavPathVersion ();
2024-09-17 17:11:20 +05:45
2024-10-21 12:24:50 +05:45
$suffixPath = $user ;
2024-09-17 17:11:20 +05:45
$spaceId = null ;
if ( $davPathVersion === WebDavHelper :: DAV_VERSION_SPACES ) {
$spaceId = WebDavHelper :: getPersonalSpaceIdForUser (
$this -> featureContext -> getBaseUrl (),
$user ,
$password ,
);
2024-10-21 12:24:50 +05:45
$suffixPath = $spaceId ;
2024-09-17 17:11:20 +05:45
}
2022-12-21 17:22:22 +05:45
$response = WebDavHelper :: listFolder (
$this -> featureContext -> getBaseUrl (),
$user ,
$password ,
"" ,
$depth ,
2024-09-17 17:11:20 +05:45
$spaceId ,
2022-12-21 17:22:22 +05:45
[
'oc:trashbin-original-filename' ,
'oc:trashbin-original-location' ,
'oc:trashbin-delete-timestamp' ,
2025-06-19 10:46:56 +05:45
'd:getlastmodified' ,
2022-12-21 17:22:22 +05:45
],
'trash-bin' ,
2025-06-19 15:34:02 +05:45
$davPathVersion ,
2022-12-21 17:22:22 +05:45
);
$this -> featureContext -> setResponse ( $response );
2024-11-29 13:19:54 +05:45
$files = $this -> getTrashbinContentFromResponseXml (
HttpRequestHelper :: getResponseXml (
$response ,
2025-06-19 15:34:02 +05:45
__METHOD__ ,
),
2024-11-29 13:19:54 +05:45
);
2022-12-21 17:22:22 +05:45
// filter root element
$files = \array_filter (
$files ,
2024-10-21 12:24:50 +05:45
static function ( $element ) use ( $davPathVersion , $suffixPath ) {
$davPath = WebDavHelper :: getDavPath ( $davPathVersion , $suffixPath , "trash-bin" );
2024-09-17 17:11:20 +05:45
return ( $element [ 'href' ] !== "/" . $davPath . "/" );
2025-06-19 15:34:02 +05:45
},
2022-12-21 17:22:22 +05:45
);
return $files ;
}
/**
* List trashbin folder
*
* @param string|null $user user
* @param string $depth
*
* @return array of all the items in the trashbin of the user
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function listTrashbinFolder ( ? string $user , string $depth = "1" ) : array {
2022-12-21 17:22:22 +05:45
return $this -> listTrashbinFolderCollection (
$user ,
"" ,
2025-06-19 15:34:02 +05:45
$depth ,
2022-12-21 17:22:22 +05:45
);
}
/**
* List a collection in the trashbin
*
* @param string|null $user user
* @param string|null $collectionPath the string of ids of the folder and sub-folders
* @param string $depth
* @param int $level
*
* @return array response
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function listTrashbinFolderCollection (
? string $user ,
? string $collectionPath = "" ,
string $depth = "1" ,
2025-06-19 15:34:02 +05:45
int $level = 1 ,
2024-12-24 09:30:42 +05:45
) : array {
2022-12-21 17:22:22 +05:45
// $collectionPath should be some list of file-ids like 2147497661/2147497662
// or the empty string, which will list the whole trashbin from the top.
$collectionPath = \trim ( $collectionPath , "/" );
$password = $this -> featureContext -> getPasswordForUser ( $user );
$davPathVersion = $this -> featureContext -> getDavPathVersion ();
$response = WebDavHelper :: listFolder (
$this -> featureContext -> getBaseUrl (),
$user ,
$password ,
$collectionPath ,
$depth ,
2024-09-18 11:26:20 +05:45
null ,
2022-12-21 17:22:22 +05:45
[
'oc:trashbin-original-filename' ,
'oc:trashbin-original-location' ,
'oc:trashbin-delete-timestamp' ,
'd:resourcetype' ,
2025-06-19 10:46:56 +05:45
'd:getlastmodified' ,
2022-12-21 17:22:22 +05:45
],
'trash-bin' ,
2025-06-19 15:34:02 +05:45
$davPathVersion ,
2022-12-21 17:22:22 +05:45
);
2023-07-12 12:26:51 +05:45
$response -> getBody () -> rewind ();
$statusCode = $response -> getStatusCode ();
$respBody = $response -> getBody () -> getContents ();
2024-12-24 09:30:42 +05:45
Assert :: assertEquals (
"207" ,
$statusCode ,
2025-06-19 15:34:02 +05:45
"Expected status code to be '207' but got $statusCode \n Response \n $respBody " ,
2024-12-24 09:30:42 +05:45
);
2023-07-12 12:26:51 +05:45
2024-11-29 13:19:54 +05:45
$files = $this -> getTrashbinContentFromResponseXml (
HttpRequestHelper :: getResponseXml (
$response ,
2025-06-19 15:34:02 +05:45
__METHOD__ . " $collectionPath " ,
),
2022-12-21 17:22:22 +05:45
);
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 = WebDavHelper :: getPersonalSpaceIdForUser (
2023-07-12 12:26:51 +05:45
$this -> featureContext -> getBaseUrl (),
$user ,
$this -> featureContext -> getPasswordForUser ( $user ),
);
}
2024-10-21 12:24:50 +05:45
$endpoint = WebDavHelper :: getDavPath ( $davPathVersion , $suffixPath , "trash-bin" );
2023-07-12 12:26:51 +05:45
2022-12-21 17:22:22 +05:45
// filter out the collection itself, we only want to return the members
$files = \array_filter (
$files ,
2023-07-12 12:26:51 +05:45
static function ( $element ) use ( $endpoint , $collectionPath ) {
2022-12-21 17:22:22 +05:45
$path = $collectionPath ;
if ( $path !== "" ) {
$path = $path . "/" ;
}
2024-09-17 17:11:20 +05:45
return ( $element [ 'href' ] !== "/ $endpoint / $path " );
2025-06-19 15:34:02 +05:45
},
2022-12-21 17:22:22 +05:45
);
foreach ( $files as $file ) {
// check for unexpected/invalid href values and fail early in order to
// avoid "common" situations that could cause infinite recursion.
$trashbinRef = $file [ "href" ];
$trimmedTrashbinRef = \trim ( $trashbinRef , "/" );
2024-09-17 17:11:20 +05:45
$expectedStartLength = \strlen ( $endpoint );
if (( \substr ( $trimmedTrashbinRef , 0 , $expectedStartLength ) !== $endpoint )
2022-12-21 17:22:22 +05:45
|| ( \strlen ( $trimmedTrashbinRef ) === $expectedStartLength )
) {
// A top href (maybe without even the username) has been returned
// in the response. That should never happen, or have been filtered out
2024-04-26 15:27:00 +05:45
// by the code above.
2022-12-21 17:22:22 +05:45
throw new Exception (
2025-06-19 15:34:02 +05:45
__METHOD__ . " Error: unexpected href in trashbin propfind at level $level : ' $trashbinRef '" ,
2022-12-21 17:22:22 +05:45
);
}
if ( $file [ "collection" ]) {
$trimmedHref = \trim ( $trashbinRef , "/" );
$explodedHref = \explode ( "/" , $trimmedHref );
$trashbinId = $collectionPath . "/" . end ( $explodedHref );
$nextFiles = $this -> listTrashbinFolderCollection (
$user ,
$trashbinId ,
$depth ,
2025-06-19 15:34:02 +05:45
$level + 1 ,
2022-12-21 17:22:22 +05:45
);
2024-04-26 15:27:00 +05:45
// Filter the collection element. We only want the members.
2022-12-21 17:22:22 +05:45
$nextFiles = \array_filter (
$nextFiles ,
static function ( $element ) use ( $user , $trashbinRef ) {
return ( $element [ 'href' ] !== $trashbinRef );
2025-06-19 15:34:02 +05:45
},
2022-12-21 17:22:22 +05:45
);
\array_push ( $files , ... $nextFiles );
}
}
return $files ;
}
/**
* @When user :user lists the resources in the trashbin with depth :depth using the WebDAV API
*
* @param string $user
* @param string $depth
*
* @return void
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function userGetsFilesInTheTrashbinWithDepthUsingTheWebdavApi ( string $user , string $depth ) : void {
2022-12-21 17:22:22 +05:45
$this -> listTopOfTrashbinFolder ( $user , $depth );
}
/**
* @Then the trashbin DAV response should not contain these nodes
*
* @param TableNode $table
*
* @return void
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function theTrashbinDavResponseShouldNotContainTheseNodes ( TableNode $table ) : void {
2022-12-21 17:22:22 +05:45
$this -> featureContext -> verifyTableNodeColumns ( $table , [ 'name' ]);
2024-12-24 09:30:42 +05:45
$files = $this -> getTrashbinContentFromResponseXml (
2025-06-19 15:34:02 +05:45
HttpRequestHelper :: getResponseXml ( $this -> featureContext -> getResponse (), __METHOD__ ),
2024-12-24 09:30:42 +05:45
);
2022-12-21 17:22:22 +05:45
foreach ( $table -> getHash () as $row ) {
$path = trim (( string ) $row [ 'name' ], "/" );
foreach ( $files as $file ) {
if ( trim (( string ) $file [ 'original-location' ], "/" ) === $path ) {
throw new Exception ( "file $path was not expected in trashbin response but was found" );
}
}
}
}
/**
* @Then the trashbin DAV response should contain these nodes
*
* @param TableNode $table
*
* @return void
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function theTrashbinDavResponseShouldContainTheseNodes ( TableNode $table ) : void {
2022-12-21 17:22:22 +05:45
$this -> featureContext -> verifyTableNodeColumns ( $table , [ 'name' ]);
2024-12-24 09:30:42 +05:45
$files = $this -> getTrashbinContentFromResponseXml (
2025-06-19 15:34:02 +05:45
HttpRequestHelper :: getResponseXml ( $this -> featureContext -> getResponse (), __METHOD__ ),
2024-12-24 09:30:42 +05:45
);
2022-12-21 17:22:22 +05:45
foreach ( $table -> getHash () as $row ) {
$path = trim ( $row [ 'name' ], "/" );
$found = false ;
foreach ( $files as $file ) {
if ( trim (( string ) $file [ 'original-location' ], "/" ) === $path ) {
$found = true ;
break ;
}
}
if ( ! $found ) {
throw new Exception ( "file $path was expected in trashbin response but was not found" );
}
}
}
/**
* Send a webdav request to list the trashbin content
*
* @param string $user user
* @param string|null $asUser - To send request as another user
* @param string|null $password
*
2024-09-17 17:11:20 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function sendTrashbinListRequest (
string $user ,
? string $asUser = null ,
2025-06-19 15:34:02 +05:45
? string $password = null ,
2024-12-24 09:30:42 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$asUser = $asUser ?? $user ;
$password = $password ?? $this -> featureContext -> getPasswordForUser ( $asUser );
2024-09-17 17:11:20 +05:45
return WebDavHelper :: propfind (
2022-12-21 17:22:22 +05:45
$this -> featureContext -> getBaseUrl (),
2024-09-17 17:11:20 +05:45
$user ,
2022-12-21 17:22:22 +05:45
$password ,
null ,
[
'oc:trashbin-original-filename' ,
'oc:trashbin-original-location' ,
'oc:trashbin-delete-timestamp' ,
2025-06-19 10:46:56 +05:45
'd:getlastmodified' ,
2022-12-21 17:22:22 +05:45
],
'1' ,
2024-09-18 11:26:20 +05:45
null ,
2022-12-21 17:22:22 +05:45
'trash-bin' ,
2024-09-17 17:11:20 +05:45
$this -> featureContext -> getDavPathVersion (),
2025-06-19 15:34:02 +05:45
$asUser ,
2022-12-21 17:22:22 +05:45
);
}
/**
* @When user :asUser tries to list the trashbin content for user :user
*
* @param string $asUser
* @param string $user
*
* @return void
* @throws Exception
*/
2025-06-19 12:15:41 +05:45
public function userTriesToListTheTrashbinContentForUser ( string $asUser , string $user ) : void {
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$asUser = $this -> featureContext -> getActualUsername ( $asUser );
2024-09-17 17:11:20 +05:45
$response = $this -> sendTrashbinListRequest ( $user , $asUser );
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @When user :asUser tries to list the trashbin content for user :user using password :password
*
* @param string $asUser
* @param string $user
* @param string $password
*
* @return void
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function userTriesToListTheTrashbinContentForUserUsingPassword (
string $asUser ,
string $user ,
2025-06-19 15:34:02 +05:45
string $password ,
2024-12-24 09:30:42 +05:45
) : void {
2024-09-17 17:11:20 +05:45
$response = $this -> sendTrashbinListRequest ( $user , $asUser , $password );
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @Then the last webdav response should contain the following elements
*
* @param TableNode $elements
*
* @return void
*/
2024-12-24 09:30:42 +05:45
public function theLastWebdavResponseShouldContainFollowingElements ( TableNode $elements ) : void {
2024-11-28 14:57:07 +05:45
$files = $this -> getTrashbinContentFromResponseXml ( HttpRequestHelper :: getResponseXml ( $response , __METHOD__ ));
2022-12-21 17:22:22 +05:45
$elementRows = $elements -> getHash ();
foreach ( $elementRows as $expectedElement ) {
$found = false ;
$expectedPath = $expectedElement [ 'path' ];
foreach ( $files as $file ) {
if ( \ltrim ( $expectedPath , "/" ) === \ltrim ( $file [ 'original-location' ], "/" )) {
$found = true ;
break ;
}
}
Assert :: assertTrue ( $found , " $expectedPath expected to be listed in response but not found" );
}
}
/**
* @Then the last webdav response should not contain the following elements
*
* @param TableNode $elements
*
* @return void
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function theLastWebdavResponseShouldNotContainFollowingElements ( TableNode $elements ) : void {
$files = $this -> getTrashbinContentFromResponseXml (
2025-06-19 15:34:02 +05:45
HttpRequestHelper :: getResponseXml ( $this -> featureContext -> getResponse ()),
2024-12-24 09:30:42 +05:45
);
2022-12-21 17:22:22 +05:45
// 'user' is also allowed in the table even though it is not used anywhere
// This for better readability in feature files
$this -> featureContext -> verifyTableNodeColumns ( $elements , [ 'path' ], [ 'path' , 'user' ]);
$elementRows = $elements -> getHash ();
foreach ( $elementRows as $expectedElement ) {
$notFound = true ;
$expectedPath = "/" . \ltrim ( $expectedElement [ 'path' ], "/" );
foreach ( $files as $file ) {
// 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
if ( $expectedPath === $file [ 'original-location' ]) {
$notFound = false ;
}
}
Assert :: assertTrue ( $notFound , " $expectedPath expected not to be listed in response but found" );
}
}
/**
2024-08-13 12:03:54 +05:45
* @When user :user tries to delete the file with original path :path from the trashbin of user :ofUser using the trashbin API
2022-12-21 17:22:22 +05:45
*
* @param string $user
2024-08-13 12:03:54 +05:45
* @param string $path
* @param string $ofUser
2022-12-21 17:22:22 +05:45
*
* @return void
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function userTriesToDeleteFromTrashbinOfUser ( string $user , string $path , string $ofUser ) : void {
2024-08-13 12:03:54 +05:45
$response = $this -> deleteItemFromTrashbin ( $user , $path , $ofUser );
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
2024-08-13 12:03:54 +05:45
* @When user :user tries to delete the file with original path :path from the trashbin of user :ofUser using the password :password and the trashbin API
2022-12-21 17:22:22 +05:45
*
2024-08-13 12:05:22 +05:45
* @param string $user
2024-08-13 12:03:54 +05:45
* @param string $path
* @param string $ofUser
* @param string $password
2022-12-21 17:22:22 +05:45
*
* @return void
* @throws JsonException
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function userTriesToDeleteFromTrashbinOfUserUsingPassword (
string $user ,
string $path ,
string $ofUser ,
2025-06-19 15:34:02 +05:45
string $password ,
2024-12-24 09:30:42 +05:45
) : void {
2024-08-13 12:03:54 +05:45
$response = $this -> deleteItemFromTrashbin ( $user , $path , $ofUser , $password );
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @When user :asUser tries to restore the file with original path :path from the trashbin of user :user using the trashbin API
*
* @param string|null $asUser
* @param string|null $path
* @param string|null $user
*
* @return void
* @throws JsonException
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function userTriesToRestoreFromTrashbinOfUser ( ? string $asUser , ? string $path , ? string $user ) : void {
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$asUser = $this -> featureContext -> getActualUsername ( $asUser );
2024-06-26 12:16:22 +05:45
$response = $this -> restoreElement ( $user , $path , null , $asUser );
2023-09-15 15:31:25 +05:45
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @When user :asUser tries to restore the file with original path :path from the trashbin of user :user using the password :password and the trashbin API
*
* @param string|null $asUser
* @param string|null $path
* @param string|null $user
* @param string|null $password
*
* @return void
* @throws JsonException
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function userTriesToRestoreFromTrashbinOfUserUsingPassword (
? string $asUser ,
? string $path ,
? string $user ,
2025-06-19 15:34:02 +05:45
? string $password ,
2024-12-24 09:30:42 +05:45
) : void {
2022-12-21 17:22:22 +05:45
$asUser = $this -> featureContext -> getActualUsername ( $asUser );
$user = $this -> featureContext -> getActualUsername ( $user );
2024-06-26 12:16:22 +05:45
$response = $this -> restoreElement ( $user , $path , null , $asUser , $password );
2023-09-15 15:31:25 +05:45
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
2024-09-17 17:11:20 +05:45
* converts the trashItemHRef from /<base>/dav/trash-bin/<user>/<item_id>/ to /trash-bin/<user>/<item_id>
2022-12-21 17:22:22 +05:45
*
* @param string $href
*
* @return string
*/
2024-12-24 09:30:42 +05:45
private function convertTrashbinHref ( string $href ) : string {
2022-12-21 17:22:22 +05:45
$trashItemHRef = \trim ( $href , '/' );
$trashItemHRef = \strstr ( $trashItemHRef , '/trash-bin' );
$trashItemHRef = \trim ( $trashItemHRef , '/' );
$parts = \explode ( '/' , $trashItemHRef );
$decodedParts = \array_slice ( $parts , 2 );
return '/' . \join ( '/' , $decodedParts );
}
/**
* @When /^user "([^"]*)" tries to delete the (?:file|folder|entry) with original path "([^"]*)" from the trashbin using the trashbin API$/
*
2024-04-11 09:56:27 +05:45
* @param string $user
* @param string $originalPath
*
2024-04-26 15:27:00 +05:45
* @return void
2024-04-11 09:56:27 +05:45
*/
2024-12-24 09:30:42 +05:45
public function userTriesToDeleteFileWithOriginalPathFromTrashbinUsingTrashbinAPI (
string $user ,
2025-06-19 15:34:02 +05:45
string $originalPath ,
2024-12-24 09:30:42 +05:45
) : void {
2024-08-13 12:03:54 +05:45
$response = $this -> deleteItemFromTrashbin ( $user , $originalPath );
$this -> featureContext -> setResponse ( $response );
2024-04-11 09:56:27 +05:45
}
/**
2024-08-13 12:03:54 +05:45
* @param string $user
* @param string $originalPath
* @param string|null $ofUser
2022-12-21 17:22:22 +05:45
* @param string|null $password
*
2024-08-13 12:03:54 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
*/
2024-12-24 09:30:42 +05:45
public function deleteItemFromTrashbin (
string $user ,
string $originalPath ,
? string $ofUser = null ,
2025-06-19 15:34:02 +05:45
? string $password = null ,
2024-12-24 09:30:42 +05:45
) : ResponseInterface {
2024-08-13 12:03:54 +05:45
$ofUser = $ofUser ?? $user ;
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
2024-08-13 12:03:54 +05:45
$ofUser = $this -> featureContext -> getActualUsername ( $ofUser );
$listing = $this -> listTrashbinFolder ( $ofUser );
2022-12-21 17:22:22 +05:45
2024-08-13 12:03:54 +05:45
$path = "" ;
$originalPath = \trim ( $originalPath , '/' );
2022-12-21 17:22:22 +05:45
foreach ( $listing as $entry ) {
2023-05-30 15:03:54 +05:45
// The entry for the trashbin root can have original-location null.
// That is reasonable, because the trashbin root is not something that can be restored.
$originalLocation = $entry [ 'original-location' ] ?? '' ;
if ( \trim ( $originalLocation , '/' ) === $originalPath ) {
2024-08-13 12:03:54 +05:45
$path = $entry [ 'href' ];
break ;
2022-12-21 17:22:22 +05:45
}
}
2024-08-13 12:03:54 +05:45
if ( $path === "" ) {
throw new Exception (
__METHOD__
2025-06-19 15:34:02 +05:45
. " could not find the trashbin entry for original path ' $originalPath ' of user ' $user '" ,
2024-08-13 12:03:54 +05:45
);
}
$password = $password ?? $this -> featureContext -> getPasswordForUser ( $user );
$fullUrl = $this -> featureContext -> getBaseUrl () . $path ;
return HttpRequestHelper :: sendRequest (
$fullUrl ,
"DELETE" ,
$user ,
2025-06-19 15:34:02 +05:45
$password ,
2024-08-13 12:03:54 +05:45
);
2022-12-21 17:22:22 +05:45
}
/**
* @When /^user "([^"]*)" deletes the (?:file|folder|entry) with original path "([^"]*)" from the trashbin using the trashbin API$/
*
* @param string $user
* @param string $originalPath
*
* @return void
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function deleteFileFromTrashbin ( string $user , string $originalPath ) : void {
2024-08-13 12:03:54 +05:45
$response = $this -> deleteItemFromTrashbin ( $user , $originalPath );
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
$this -> featureContext -> pushToLastStatusCodesArrays ();
}
2024-07-16 15:37:00 +05:45
/**
* @Given /^user "([^"]*)" has deleted the (?:file|folder|entry) with original path "([^"]*)" from the trashbin$/
*
* @param string $user
* @param string $originalPath
*
* @return void
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function userHasDeletedTheFolderWithOriginalPathFromTheTrashbin ( string $user , string $originalPath ) : void {
2024-08-13 12:03:54 +05:45
$response = $this -> deleteItemFromTrashbin ( $user , $originalPath );
$this -> featureContext -> theHTTPStatusCodeShouldBe ( 204 , '' , $response );
2024-07-16 15:37:00 +05:45
}
2022-12-21 17:22:22 +05:45
/**
* @When /^user "([^"]*)" deletes the following (?:files|folders|entries) with original path from the trashbin$/
*
* @param string $user
* @param TableNode $table
*
* @return void
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function deleteFollowingFilesFromTrashbin ( string $user , TableNode $table ) : void {
2022-12-21 17:22:22 +05:45
$this -> featureContext -> verifyTableNodeColumns ( $table , [ "path" ]);
$paths = $table -> getHash ();
foreach ( $paths as $path ) {
2024-08-13 12:03:54 +05:45
$response = $this -> deleteItemFromTrashbin ( $user , $path [ "path" ]);
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
$this -> featureContext -> pushToLastStatusCodesArrays ();
}
}
/**
* @Then /^as "([^"]*)" (?:file|folder|entry) "([^"]*)" should exist in the trashbin$/
*
* @param string|null $user
* @param string|null $path
*
* @return void
* @throws JsonException
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function asFileOrFolderExistsInTrash ( ? string $user , ? string $path ) : void {
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$path = \trim ( $path , '/' );
$sections = \explode ( '/' , $path , 2 );
$firstEntry = $this -> findFirstTrashedEntry ( $user , \trim ( $sections [ 0 ], '/' ));
Assert :: assertNotNull (
$firstEntry ,
2025-06-19 15:34:02 +05:45
"The first trash entry was not found while looking for trashbin entry ' $path ' of user ' $user '" ,
2022-12-21 17:22:22 +05:45
);
if ( \count ( $sections ) !== 1 ) {
$listing = $this -> listTrashbinFolderCollection ( $user , \basename ( \rtrim ( $firstEntry [ 'href' ], '/' )));
2023-05-02 17:25:56 +05:45
} else {
$listing = [];
2022-12-21 17:22:22 +05:45
}
2024-04-26 15:27:00 +05:45
// query was on the main element?
2022-12-21 17:22:22 +05:45
if ( \count ( $sections ) === 1 ) {
// already found, return
return ;
}
$checkedName = \basename ( $path );
$found = false ;
foreach ( $listing as $entry ) {
if ( $entry [ 'name' ] === $checkedName ) {
$found = true ;
break ;
}
}
Assert :: assertTrue (
$found ,
__METHOD__
2025-06-19 15:34:02 +05:45
. " Could not find expected resource ' $path ' in the trash" ,
2022-12-21 17:22:22 +05:45
);
}
/**
* Function to check if an element is in the trashbin
*
* @param string|null $user
* @param string|null $originalPath
*
* @return bool
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
private function isInTrash ( ? string $user , ? string $originalPath ) : bool {
2022-12-21 17:22:22 +05:45
$listing = $this -> listTrashbinFolder ( $user );
// we don't care if the test step writes a leading "/" or not
$originalPath = \ltrim ( $originalPath , '/' );
foreach ( $listing as $entry ) {
if ( $entry [ 'original-location' ] !== null && \ltrim ( $entry [ 'original-location' ], '/' ) === $originalPath ) {
return true ;
}
}
return false ;
}
/**
* @param string $user
* @param string $trashItemHRef
* @param string $destinationPath
* @param string|null $asUser - To send request as another user
* @param string|null $password
*
2023-09-15 15:31:25 +05:45
* @return ResponseInterface
2023-05-02 17:25:56 +05:45
* @throws JsonException
* @throws GuzzleException
2022-12-21 17:22:22 +05:45
*/
2024-12-24 09:30:42 +05:45
private function sendUndeleteRequest (
string $user ,
string $trashItemHRef ,
string $destinationPath ,
? string $asUser = null ,
2025-06-19 15:34:02 +05:45
? string $password = null ,
2024-12-24 09:30:42 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$asUser = $asUser ?? $user ;
2024-09-17 17:11:20 +05:45
$password = $password ?? $this -> featureContext -> getPasswordForUser ( $asUser );
2022-12-21 17:22:22 +05:45
$destinationPath = \trim ( $destinationPath , '/' );
2024-09-17 17:11:20 +05:45
$baseUrl = $this -> featureContext -> getBaseUrl ();
$davPathVersion = $this -> featureContext -> getDavPathVersion ();
2022-12-21 17:22:22 +05:45
2024-10-21 12:24:50 +05:45
$suffixPath = $asUser ;
2024-09-17 17:11:20 +05:45
if ( $davPathVersion === WebDavHelper :: DAV_VERSION_SPACES ) {
if ( \str_starts_with ( $destinationPath , "Shares/" )) {
2024-10-21 12:24:50 +05:45
$suffixPath = $this -> featureContext -> spacesContext -> getSpaceIdByName ( $user , "Shares" );
2024-09-17 17:11:20 +05:45
$destinationPath = \str_replace ( "Shares/" , "" , $destinationPath );
} else {
2024-10-21 12:24:50 +05:45
$suffixPath = WebDavHelper :: getPersonalSpaceIdForUser (
2024-09-17 17:11:20 +05:45
$baseUrl ,
$asUser ,
$password ,
);
}
}
2024-10-21 12:24:50 +05:45
$destinationDavPath = WebDavHelper :: getDavPath ( $davPathVersion , $suffixPath );
$destination = " $baseUrl / $destinationDavPath / $destinationPath " ;
2024-09-17 17:11:20 +05:45
$trashItemHRef = \ltrim ( $this -> convertTrashbinHref ( $trashItemHRef ), "/" );
$headers [ 'Destination' ] = $destination ;
2023-09-15 15:31:25 +05:45
return $this -> featureContext -> makeDavRequest (
2024-09-17 17:11:20 +05:45
$user ,
2022-12-21 17:22:22 +05:45
'MOVE' ,
$trashItemHRef ,
$headers ,
null ,
2024-09-18 11:26:20 +05:45
null ,
2022-12-21 17:22:22 +05:45
'trash-bin' ,
2024-09-17 17:11:20 +05:45
$davPathVersion ,
2022-12-21 17:22:22 +05:45
false ,
$password ,
[],
2025-06-19 15:34:02 +05:45
$asUser ,
2022-12-21 17:22:22 +05:45
);
}
/**
* @param string $user
* @param string $originalPath
* @param string|null $destinationPath
* @param string|null $asUser - To send request as another user
* @param string|null $password
*
2023-09-15 15:31:25 +05:45
* @return ResponseInterface
2023-05-02 17:25:56 +05:45
* @throws JsonException
* @throws GuzzleException
2022-12-21 17:22:22 +05:45
*/
2024-12-24 09:30:42 +05:45
private function restoreElement (
string $user ,
string $originalPath ,
? string $destinationPath = null ,
? string $asUser = null ,
2025-06-19 15:34:02 +05:45
? string $password = null ,
2024-12-24 09:30:42 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$asUser = $asUser ?? $user ;
$listing = $this -> listTrashbinFolder ( $user );
$originalPath = \trim ( $originalPath , '/' );
if ( $destinationPath === null ) {
$destinationPath = $originalPath ;
}
foreach ( $listing as $entry ) {
if ( $entry [ 'original-location' ] === $originalPath ) {
2023-09-15 15:31:25 +05:45
return $this -> sendUndeleteRequest (
2022-12-21 17:22:22 +05:45
$user ,
$entry [ 'href' ],
$destinationPath ,
$asUser ,
2025-06-19 15:34:02 +05:45
$password ,
2022-12-21 17:22:22 +05:45
);
}
}
// The requested element to restore was not even in the trashbin.
// Throw an exception, because there was not any API call, and so there
// is also no up-to-date response to examine in later test steps.
2024-06-26 12:16:22 +05:45
throw new \Exception (
__METHOD__
2025-06-19 15:34:02 +05:45
. " cannot restore from trashbin."
. " No element was found for user $user at original path $originalPath " ,
2024-06-26 12:16:22 +05:45
);
2022-12-21 17:22:22 +05:45
}
/**
2024-05-13 09:52:39 +05:45
* @When user :user restores the folder/file with original path :originalPath without specifying the destination using the trashbin API
2022-12-21 17:22:22 +05:45
*
* @param $user string
* @param $originalPath string
*
2024-11-29 13:19:54 +05:45
* @return void
2022-12-21 17:22:22 +05:45
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function userRestoresResourceWithOriginalPathWithoutSpecifyingDestinationUsingTrashbinApi (
string $user ,
2025-06-19 15:34:02 +05:45
string $originalPath ,
2024-12-24 09:30:42 +05:45
) : void {
2022-12-21 17:22:22 +05:45
$listing = $this -> listTrashbinFolder ( $user );
$originalPath = \trim ( $originalPath , '/' );
foreach ( $listing as $entry ) {
if ( $entry [ 'original-location' ] === $originalPath ) {
$trashItemHRef = $this -> convertTrashbinHref ( $entry [ 'href' ]);
2024-11-29 13:19:54 +05:45
$this -> featureContext -> setResponse (
$this -> featureContext -> makeDavRequest (
$user ,
'MOVE' ,
$trashItemHRef ,
[],
null ,
null ,
2025-06-19 15:34:02 +05:45
'trash-bin' ,
),
2022-12-21 17:22:22 +05:45
);
}
}
}
/**
* @Then /^the content of file "([^"]*)" for user "([^"]*)" if the file is also in the trashbin should be "([^"]*)" otherwise "([^"]*)"$/
*
* Note: this is a special step for an unusual bug combination.
* Delete it when the bug is fixed and the step is no longer needed.
*
* @param string|null $fileName
* @param string|null $user
* @param string|null $content
* @param string|null $alternativeContent
*
* @return void
* @throws JsonException
* @throws Exception
*/
public function contentOfFileForUserIfAlsoInTrashShouldBeOtherwise (
? string $fileName ,
? string $user ,
? string $content ,
2025-06-19 15:34:02 +05:45
? string $alternativeContent ,
2024-12-24 09:30:42 +05:45
) : void {
2022-12-21 17:22:22 +05:45
$isInTrash = $this -> isInTrash ( $user , $fileName );
$user = $this -> featureContext -> getActualUsername ( $user );
2023-10-11 16:15:31 +05:45
$response = $this -> featureContext -> downloadFileAsUserUsingPassword ( $user , $fileName );
2022-12-21 17:22:22 +05:45
if ( $isInTrash ) {
2023-10-11 16:15:31 +05:45
$this -> featureContext -> checkDownloadedContentMatches ( $content , '' , $response );
2022-12-21 17:22:22 +05:45
} else {
2023-10-11 16:15:31 +05:45
$this -> featureContext -> checkDownloadedContentMatches ( $alternativeContent , '' , $response );
2022-12-21 17:22:22 +05:45
}
}
/**
* @When /^user "([^"]*)" restores the (?:file|folder|entry) with original path "([^"]*)" using the trashbin API$/
*
* @param string|null $user
* @param string $originalPath
*
* @return void
* @throws JsonException
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function elementInTrashIsRestored ( ? string $user , string $originalPath ) : void {
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
2023-09-15 15:31:25 +05:45
$this -> featureContext -> setResponse ( $this -> restoreElement ( $user , $originalPath ));
2022-12-21 17:22:22 +05:45
}
/**
* @When /^user "([^"]*)" restores the following (?:files|folders|entries) with original path$/
*
* @param string $user
* @param TableNode $table
*
* @return void
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function userRestoresFollowingFiles ( string $user , TableNode $table ) : void {
2022-12-21 17:22:22 +05:45
$this -> featureContext -> verifyTableNodeColumns ( $table , [ "path" ]);
$paths = $table -> getHash ();
foreach ( $paths as $originalPath ) {
2024-04-11 09:56:27 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$this -> featureContext -> setResponse ( $this -> restoreElement ( $user , $originalPath [ "path" ]));
2022-12-21 17:22:22 +05:45
$this -> featureContext -> pushToLastStatusCodesArrays ();
}
}
/**
* @Given /^user "([^"]*)" has restored the (?:file|folder|entry) with original path "([^"]*)"$/
*
* @param string $user
* @param string $originalPath
*
* @return void
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
public function elementInTrashHasBeenRestored ( string $user , string $originalPath ) : void {
2023-09-15 15:31:25 +05:45
$response = $this -> restoreElement ( $user , $originalPath );
$this -> featureContext -> theHTTPStatusCodeShouldBe ( 201 , "" , $response );
2022-12-21 17:22:22 +05:45
if ( $this -> isInTrash ( $user , $originalPath )) {
throw new Exception ( "File previously located at $originalPath is still in the trashbin" );
}
}
/**
* @When /^user "([^"]*)" restores the (?:file|folder|entry) with original path "([^"]*)" to "([^"]*)" using the trashbin API$/
*
* @param string|null $user
* @param string|null $originalPath
* @param string|null $destinationPath
*
* @return void
* @throws JsonException
* @throws Exception
*/
public function userRestoresTheFileWithOriginalPathToUsingTheTrashbinApi (
? string $user ,
? string $originalPath ,
2025-06-19 15:34:02 +05:45
? string $destinationPath ,
2024-12-24 09:30:42 +05:45
) : void {
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
2023-09-15 15:31:25 +05:45
$this -> featureContext -> setResponse ( $this -> restoreElement ( $user , $originalPath , $destinationPath ));
2022-12-21 17:22:22 +05:45
}
/**
* @Then /^as "([^"]*)" the (?:file|folder|entry) with original path "([^"]*)" should exist in the trashbin$/
*
* @param string|null $user
* @param string|null $originalPath
*
* @return void
* @throws JsonException
* @throws Exception
*/
public function elementIsInTrashCheckingOriginalPath (
? string $user ,
2025-06-19 15:34:02 +05:45
? string $originalPath ,
2024-12-24 09:30:42 +05:45
) : void {
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
Assert :: assertTrue (
$this -> isInTrash ( $user , $originalPath ),
2025-06-19 15:34:02 +05:45
"File previously located at $originalPath wasn't found in the trashbin of user $user " ,
2022-12-21 17:22:22 +05:45
);
}
/**
* @Then /^as "([^"]*)" the (?:file|folder|entry) with original path "([^"]*)" should not exist in the trashbin/
*
* @param string|null $user
* @param string $originalPath
*
* @return void
* @throws Exception
*/
public function elementIsNotInTrashCheckingOriginalPath (
? string $user ,
2025-06-19 15:34:02 +05:45
string $originalPath ,
2024-12-24 09:30:42 +05:45
) : void {
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
Assert :: assertFalse (
$this -> isInTrash ( $user , $originalPath ),
2025-06-19 15:34:02 +05:45
"File previously located at $originalPath was found in the trashbin of user $user " ,
2022-12-21 17:22:22 +05:45
);
}
/**
* @Then /^as "([^"]*)" the (?:files|folders|entries) with following original paths should not exist in the trashbin$/
*
* @param string $user
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function followingElementsAreNotInTrashCheckingOriginalPath (
string $user ,
2025-06-19 15:34:02 +05:45
TableNode $table ,
2024-12-24 09:30:42 +05:45
) : void {
2022-12-21 17:22:22 +05:45
$this -> featureContext -> verifyTableNodeColumns ( $table , [ "path" ]);
2023-05-02 17:25:56 +05:45
$paths = $table -> getHash ();
2022-12-21 17:22:22 +05:45
foreach ( $paths as $originalPath ) {
2024-04-11 09:56:27 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
Assert :: assertFalse (
$this -> isInTrash ( $user , $originalPath [ "path" ]),
2025-06-19 15:34:02 +05:45
"File previously located at " . $originalPath [ "path" ] . " was found in the trashbin of user $user " ,
2024-04-11 09:56:27 +05:45
);
2022-12-21 17:22:22 +05:45
}
}
/**
* @Then /^as "([^"]*)" the (?:files|folders|entries) with following original paths should exist in the trashbin$/
*
* @param string $user
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function followingElementsAreInTrashCheckingOriginalPath (
string $user ,
2025-06-19 15:34:02 +05:45
TableNode $table ,
2024-12-24 09:30:42 +05:45
) : void {
2022-12-21 17:22:22 +05:45
$this -> featureContext -> verifyTableNodeColumns ( $table , [ "path" ]);
2023-05-02 17:25:56 +05:45
$paths = $table -> getHash ();
2022-12-21 17:22:22 +05:45
foreach ( $paths as $originalPath ) {
2024-04-11 09:56:27 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
Assert :: assertTrue (
$this -> isInTrash ( $user , $originalPath [ "path" ]),
2025-06-19 15:34:02 +05:45
"File previously located at " . $originalPath [ "path" ] . " wasn't found in the trashbin of user $user " ,
2024-04-11 09:56:27 +05:45
);
2022-12-21 17:22:22 +05:45
}
}
/**
* Finds the first trashed entry matching the given name
*
* @param string $user
* @param string $name
*
* @return array|null real entry name with timestamp suffix or null if not found
* @throws Exception
*/
2024-12-24 09:30:42 +05:45
private function findFirstTrashedEntry ( string $user , string $name ) : ? array {
2022-12-21 17:22:22 +05:45
$listing = $this -> listTrashbinFolder ( $user );
foreach ( $listing as $entry ) {
if ( $entry [ 'name' ] === $name ) {
return $entry ;
}
}
return null ;
}
/**
* This will run before EVERY scenario.
* It will set the properties for this object.
*
* @BeforeScenario
*
* @param BeforeScenarioScope $scope
*
* @return void
*/
2024-12-24 09:30:42 +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' );
2022-12-21 17:22:22 +05:45
}
/**
* @Then /^the deleted (?:file|folder) "([^"]*)" should have the correct deletion mtime in the response$/
*
* @param string $resource file or folder in trashbin
*
* @return void
*/
2024-12-24 09:30:42 +05:45
public function theDeletedFileFolderShouldHaveCorrectDeletionMtimeInTheResponse ( string $resource ) : void {
2022-12-21 17:22:22 +05:45
$files = $this -> getTrashbinContentFromResponseXml (
2025-06-19 15:34:02 +05:45
HttpRequestHelper :: getResponseXml ( $this -> featureContext -> getResponse ()),
2022-12-21 17:22:22 +05:45
);
$found = false ;
$expectedMtime = $this -> featureContext -> getLastUploadDeleteTime ();
$responseMtime = '' ;
foreach ( $files as $file ) {
2023-05-02 17:25:56 +05:45
if ( \ltrim ( $resource , "/" ) === \ltrim (( string ) $file [ 'original-location' ], "/" )) {
2022-12-21 17:22:22 +05:45
$responseMtime = $file [ 'mtime' ];
$mtime_difference = \abs (( int ) \trim (( string ) $expectedMtime ) - ( int ) \trim ( $responseMtime ));
if ( $mtime_difference <= 2 ) {
$found = true ;
break ;
}
}
}
Assert :: assertTrue (
$found ,
2025-06-19 15:34:02 +05:45
" $resource expected to be listed in response with mtime ' $expectedMtime ' but found ' $responseMtime '" ,
2022-12-21 17:22:22 +05:45
);
}
}