2024-08-29 12:29:39 +05:45
<? php declare ( strict_types = 1 );
/**
* ownCloud
*
* @author Amrita Shrestha <amrita@jankaritech.com>
* @copyright Copyright (c) 2024 Amrita Shrestha <amrita@jankaritech.com>
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License,
* as published by the Free Software Foundation;
* either version 3 of the License, or any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
use Behat\Behat\Context\Context ;
use Behat\Behat\Hook\Scope\BeforeScenarioScope ;
2024-09-18 14:23:16 +05:45
use Behat\Gherkin\Node\TableNode ;
2024-08-29 12:29:39 +05:45
use GuzzleHttp\Exception\GuzzleException ;
2024-10-17 17:08:03 +05:45
use PHPUnit\Framework\Assert ;
2024-08-29 12:29:39 +05:45
use TestHelpers\HttpRequestHelper ;
2024-09-16 10:02:37 +05:45
use TestHelpers\WebDavHelper ;
use TestHelpers\CollaborationHelper ;
2024-10-08 12:26:40 +05:45
use TestHelpers\BehatHelper ;
2024-08-29 12:29:39 +05:45
/**
* steps needed to re-configure oCIS server
*/
class CollaborationContext implements Context {
private FeatureContext $featureContext ;
private SpacesContext $spacesContext ;
2024-09-23 11:19:06 +05:45
private string $lastAppOpenData ;
2024-08-29 12:29:39 +05:45
/**
* 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
2024-10-08 12:26:40 +05:45
$this -> featureContext = BehatHelper :: getContext ( $scope , $environment , 'FeatureContext' );
$this -> spacesContext = BehatHelper :: getContext ( $scope , $environment , 'SpacesContext' );
2024-08-29 12:29:39 +05:45
}
2024-09-18 14:23:16 +05:45
/**
2024-09-23 11:19:06 +05:45
* @param string $data
2024-09-18 14:23:16 +05:45
*
* @return void
*/
2024-09-23 11:19:06 +05:45
public function setLastAppOpenData ( string $data ) : void {
$this -> lastAppOpenData = $data ;
2024-09-18 14:23:16 +05:45
}
/**
* @return string
*/
2024-09-23 11:19:06 +05:45
public function getLastAppOpenData () : string {
return $this -> lastAppOpenData ;
2024-09-18 14:23:16 +05:45
}
2024-08-29 12:29:39 +05:45
/**
2024-08-30 09:46:02 +05:45
* @When user :user checks the information of file :file of space :space using office :app
2024-09-11 16:13:38 +05:45
* @When user :user checks the information of file :file of space :space using office :app with view mode :view
2024-08-29 12:29:39 +05:45
*
2024-08-30 09:46:02 +05:45
* @param string $user
* @param string $file
* @param string $space
* @param string $app
2024-09-11 16:13:38 +05:45
* @param string|null $viewMode
2024-08-29 12:29:39 +05:45
*
* @return void
*
* @throws GuzzleException
*/
2024-09-11 16:13:38 +05:45
public function userChecksTheInformationOfFileOfSpaceUsingOffice ( string $user , string $file , string $space , string $app , string $viewMode = null ) : void {
2024-08-29 12:29:39 +05:45
$fileId = $this -> spacesContext -> getFileId ( $user , $space , $file );
$response = \json_decode (
2024-09-16 10:02:37 +05:45
CollaborationHelper :: sendPOSTRequestToAppOpen (
$fileId ,
$app ,
2024-08-29 12:29:39 +05:45
$this -> featureContext -> getActualUsername ( $user ),
$this -> featureContext -> getPasswordForUser ( $user ),
2024-09-16 10:02:37 +05:45
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$viewMode
2024-08-29 12:29:39 +05:45
) -> getBody () -> getContents ()
);
$accessToken = $response -> form_parameters -> access_token ;
// Extract the WOPISrc from the app_url
$parsedUrl = parse_url ( $response -> app_url );
parse_str ( $parsedUrl [ 'query' ], $queryParams );
$wopiSrc = $queryParams [ 'WOPISrc' ];
$this -> featureContext -> setResponse (
HttpRequestHelper :: get (
$wopiSrc . "?access_token= $accessToken " ,
$this -> featureContext -> getStepLineRef ()
)
);
}
2024-09-02 14:52:36 +05:45
/**
* @When user :user creates a file :file inside folder :folder in space :space using wopi endpoint
* @When user :user tries to create a file :file inside folder :folder in space :space using wopi endpoint
*
* @param string $user
* @param string $file
* @param string $folder
* @param string $space
*
* @return void
2024-09-17 09:43:45 +05:45
* @throws GuzzleException
2024-09-02 14:52:36 +05:45
*/
public function userCreatesFileInsideFolderInSpaceUsingWopiEndpoint ( string $user , string $file , string $folder , string $space ) : void {
2024-09-17 09:43:45 +05:45
$parentContainerId = $this -> spacesContext -> getResourceId ( $user , $space , $folder );
2024-09-02 14:52:36 +05:45
$this -> featureContext -> setResponse (
2024-09-17 09:43:45 +05:45
CollaborationHelper :: createFile (
$this -> featureContext -> getBaseUrl (),
2024-09-02 14:52:36 +05:45
$this -> featureContext -> getStepLineRef (),
$user ,
2024-09-17 09:43:45 +05:45
$this -> featureContext -> getPasswordForUser ( $user ),
$parentContainerId ,
$file
)
);
}
/**
* @param string $file
* @param string $password
2024-09-17 17:05:53 +05:45
* @param string $folder
2024-09-17 09:43:45 +05:45
*
* @return void
* @throws GuzzleException
*/
2024-09-17 17:05:53 +05:45
public function createFile ( string $file , string $password , string $folder = "" ) : void {
2024-09-17 09:43:45 +05:45
$token = $this -> featureContext -> shareNgGetLastCreatedLinkShareToken ();
2024-09-17 17:11:20 +05:45
$baseUrl = $this -> featureContext -> getBaseUrl ();
$davPath = WebDavHelper :: getDavPath ( WebDavHelper :: DAV_VERSION_NEW , $token , "public-files" );
2024-09-17 09:43:45 +05:45
$response = HttpRequestHelper :: sendRequest (
2024-09-17 17:11:20 +05:45
" $baseUrl / $davPath / $folder " ,
2024-09-17 09:43:45 +05:45
$this -> featureContext -> getStepLineRef (),
"PROPFIND" ,
"public" ,
$this -> featureContext -> getActualPassword ( $password )
);
$responseXml = HttpRequestHelper :: getResponseXml (
$response ,
__METHOD__
);
$xmlPart = $responseXml -> xpath ( "//d:prop/oc:fileid" );
$parentContainerId = ( string ) $xmlPart [ 0 ];
$headers = [
"Public-Token" => $token
];
$this -> featureContext -> setResponse (
CollaborationHelper :: createFile (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
"public" ,
$this -> featureContext -> getActualPassword ( $password ),
$parentContainerId ,
$file ,
$headers
2024-09-02 14:52:36 +05:45
)
);
}
2024-09-16 10:02:37 +05:45
2024-09-17 17:05:53 +05:45
/**
* @When the public creates a file :file inside the last shared public link folder with password :password using wopi endpoint
* @When the public tries to create a file :file inside the last shared public link folder with password :password using wopi endpoint
*
* @param string $file
* @param string $password
*
* @return void
* @throws GuzzleException
*/
public function thePublicCreatesAFileInsideTheLastSharedPublicLinkFolderWithPasswordUsingWopiEndpoint ( string $file , string $password ) : void {
$this -> createFile ( $file , $password );
}
/**
* @When the public creates a file :file inside folder :folder in the last shared public link space with password :password using wopi endpoint
* @When the public tries to create a file :file inside folder :folder in the last shared public link space with password :password using wopi endpoint
*
* @param string $file
* @param string $folder
* @param string $password
*
* @return void
* @throws GuzzleException
*/
public function thePublicCreatesAFileInsideFolderInTheLastSharedPublicLinkSpaceWithPasswordUsingWopiEndpoint ( string $file , string $folder , string $password ) : void {
$this -> createFile ( $file , $password , $folder );
}
2024-09-16 10:02:37 +05:45
/**
* @When user :user tries to check the information of file :file of space :space using office :app with invalid file-id
*
* @param string $user
* @param string $file
* @param string $space
* @param string $app
*
* @return void
*
* @throws GuzzleException
* @throws JsonException
*/
public function userTriesToCheckTheInformationOfFileOfSpaceUsingOfficeWithInvalidFileId ( string $user , string $file , string $space , string $app ) : void {
$response = \json_decode (
CollaborationHelper :: sendPOSTRequestToAppOpen (
$this -> spacesContext -> getFileId ( $user , $space , $file ),
$app ,
$this -> featureContext -> getActualUsername ( $user ),
$this -> featureContext -> getPasswordForUser ( $user ),
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef ()
) -> getBody () -> getContents ()
);
$accessToken = $response -> form_parameters -> access_token ;
// Extract the WOPISrc from the app_url
$parsedUrl = parse_url ( $response -> app_url );
parse_str ( $parsedUrl [ 'query' ], $queryParams );
$wopiSrc = $queryParams [ 'WOPISrc' ];
$position = strpos ( $wopiSrc , '/files/' ) + \strlen ( '/files/' );
// Extract the base URL up to and including '/files/'
$fullUrl = substr ( $wopiSrc , 0 , $position ) . WebDavHelper :: generateUUIDv4 ();
$this -> featureContext -> setResponse (
HttpRequestHelper :: get (
$fullUrl . "?access_token= $accessToken " ,
$this -> featureContext -> getStepLineRef ()
)
);
}
2024-09-17 10:02:02 +05:45
/**
* @When user :user tries to create a file :file inside deleted folder using wopi endpoint
*
* @param string $user
* @param string $file
*
* @return void
* @throws GuzzleException
*/
public function userTriesToCreateAFileInsideDeletedFolderUsingWopiEndpoint ( string $user , string $file ) : void {
$parentContainerId = $this -> featureContext -> getStoredFileID ();
$this -> featureContext -> setResponse (
CollaborationHelper :: createFile (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$user ,
$this -> featureContext -> getPasswordForUser ( $user ),
$parentContainerId ,
$file
)
);
}
2024-09-18 14:23:16 +05:45
/**
2024-09-23 11:19:06 +05:45
* @Given user :user has sent the following app-open request:
2024-09-18 14:23:16 +05:45
*
* @param string $user
2024-09-23 11:19:06 +05:45
* @param TableNode $properties
2024-09-18 14:23:16 +05:45
*
* @return void
* @throws GuzzleException
*/
2024-09-23 11:19:06 +05:45
public function userHasSentTheFollowingAppOpenRequest ( string $user , TableNode $properties ) : void {
$rows = $properties -> getRowsHash ();
2024-09-18 14:23:16 +05:45
$appResponse = CollaborationHelper :: sendPOSTRequestToAppOpen (
$this -> spacesContext -> getFileId ( $user , $rows [ 'space' ], $rows [ 'resource' ]),
2024-09-23 11:19:06 +05:45
$rows [ 'app' ],
2024-09-18 14:23:16 +05:45
$this -> featureContext -> getActualUsername ( $user ),
$this -> featureContext -> getPasswordForUser ( $user ),
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef ()
);
$this -> featureContext -> theHTTPStatusCodeShouldBe ( 200 , '' , $appResponse );
2024-09-23 11:19:06 +05:45
$this -> setLastAppOpenData ( $appResponse -> getBody () -> getContents ());
2024-09-18 14:23:16 +05:45
}
/**
2024-09-23 11:19:06 +05:45
* @When user :user tries to get the information of the last opened file using wopi endpoint
* @When user :user gets the information of the last opened file using wopi endpoint
2024-09-18 14:23:16 +05:45
*
* @param string $user
*
* @return void
* @throws GuzzleException
*/
2024-09-23 11:19:06 +05:45
public function userGetsTheInformationOfTheLastOpenedFileUsingWopiEndpoint ( string $user ) : void {
$response = json_decode ( $this -> getLastAppOpenData ());
2024-09-18 14:23:16 +05:45
$accessToken = $response -> form_parameters -> access_token ;
// Extract the WOPISrc from the app_url
$parsedUrl = parse_url ( $response -> app_url );
parse_str ( $parsedUrl [ 'query' ], $queryParams );
$wopiSrc = $queryParams [ 'WOPISrc' ];
$this -> featureContext -> setResponse (
HttpRequestHelper :: get (
$wopiSrc . "?access_token= $accessToken " ,
$this -> featureContext -> getStepLineRef ()
)
);
}
2024-10-17 17:08:03 +05:45
/**
* @Then /^the response (should|should not) contain the following MIME types:$/
*
* @param string $shouldOrNot
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function theFollowingMimeTypesShouldExistForUser ( string $shouldOrNot , TableNode $table ) : void {
$rows = $table -> getRows ();
$responseArray = $this -> featureContext -> getJsonDecodedResponse ( $this -> featureContext -> getResponse ())[ 'mime-types' ];
$mimeTypes = \array_column ( $responseArray , 'mime_type' );
foreach ( $rows as $row ) {
if ( $shouldOrNot === "should not" ) {
Assert :: assertFalse (
\in_array ( $row [ 0 ], $mimeTypes ),
"the response should not contain the mimetype $row[0] . \n Mime Types found in response: \n "
. print_r ( $mimeTypes , true )
);
} else {
Assert :: assertTrue (
\in_array ( $row [ 0 ], $mimeTypes ),
"the response does not contain the mimetype $row[0] . \n Mime Types found in response: \n "
. print_r ( $mimeTypes , true )
);
}
}
}
2024-11-19 20:49:42 +05:45
/**
2024-11-21 16:18:36 +05:45
* @Then the app list response should contain the following template information for office :app:
2024-11-19 20:49:42 +05:45
*
2024-11-21 16:18:36 +05:45
* @param string $app
2024-11-19 20:49:42 +05:45
* @param TableNode $table
*
* @return void
*/
2024-11-21 16:18:36 +05:45
public function theAppListResponseShouldContainTheFollowingTemplateInformationForOffice ( string $app , TableNode $table ) : void {
2024-11-19 20:49:42 +05:45
$responseArray = $this -> featureContext -> getJsonDecodedResponse ( $this -> featureContext -> getResponse ());
2024-11-21 16:18:36 +05:45
Assert :: assertArrayHasKey ( "mime-types" , $responseArray , "Expected 'mime-types' in the response but not found. \n " . print_r ( $responseArray , true ));
2024-11-19 20:49:42 +05:45
$mimeTypes = $responseArray [ 'mime-types' ];
$mimeTypeMap = [];
foreach ( $mimeTypes as $mimeType ) {
$mimeTypeMap [ $mimeType [ 'mime_type' ]] = $mimeType ;
}
foreach ( $table -> getColumnsHash () as $row ) {
2024-11-21 16:18:36 +05:45
Assert :: assertArrayHasKey ( $row [ 'mime-type' ], $mimeTypeMap , "Expected mime-type ' { $row [ 'mime-type' ] } ' to exist in the response but it doesn't. \n " . print_r ( $mimeTypeMap , true ));
$mimeType = $mimeTypeMap [ $row [ 'mime-type' ]];
$found = false ;
2024-11-19 20:49:42 +05:45
foreach ( $mimeType [ 'app_providers' ] as $provider ) {
2024-11-21 16:18:36 +05:45
if ( $provider [ 'name' ] === $app && isset ( $row [ 'target-extension' ])) {
2024-11-19 20:49:42 +05:45
Assert :: assertSame (
2024-11-21 16:18:36 +05:45
$row [ 'target-extension' ],
2024-11-19 20:49:42 +05:45
$provider [ 'target_ext' ],
2024-11-21 16:18:36 +05:45
"Expected 'target_ext' for $app to be ' { $row [ 'target-extension' ] } ' but found ' { $provider [ 'target_ext' ] } '"
2024-11-19 20:49:42 +05:45
);
2024-11-21 16:18:36 +05:45
$found = true ;
break ;
2024-11-19 20:49:42 +05:45
}
}
2024-11-21 16:18:36 +05:45
if ( ! $found ) {
Assert :: fail (
"Expected response to contain app-provider ' $app ' with target-extension ' { $row [ 'target-extension' ] } ' for mime-type ' { $row [ 'mime-type' ] } ', but no matching provider was found. \n " .
"App Providers Found: " . print_r ( $mimeType [ 'app_providers' ], true )
);
}
2024-11-19 20:49:42 +05:45
}
}
/**
2024-11-21 16:18:36 +05:45
* @When user :user has created a file :file using wopi endpoint
2024-11-19 20:49:42 +05:45
*
* @param string $user
* @param string $file
*
* @return string
* @throws GuzzleException
*/
2024-11-21 16:18:36 +05:45
public function userHasCreatedAFileInSpaceUsingWopiEndpoint ( string $user , string $file ) : string {
$parentContainerId = $this -> featureContext -> getFileIdForPath ( $user , "/" );
2024-11-19 20:49:42 +05:45
$response = CollaborationHelper :: createFile (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$user ,
$this -> featureContext -> getPasswordForUser ( $user ),
$parentContainerId ,
$file
);
$this -> featureContext -> theHTTPStatusCodeShouldBe ( 200 , "" , $response );
2024-11-21 16:18:36 +05:45
$decodedResponse [] = $this -> featureContext -> getJsonDecodedResponseBodyContent ( $response );
return $decodedResponse [ 0 ] -> file_id ;
2024-11-19 20:49:42 +05:45
}
2024-08-29 12:29:39 +05:45
}