2022-12-21 17:22:22 +05:45
<? php declare ( strict_types = 1 );
/**
* ownCloud
*
* @author Artur Neumann <artur@jankaritech.com>
* @copyright Copyright (c) 2019, ownCloud GmbH
*
* 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 ;
use Behat\Gherkin\Node\TableNode ;
use PHPUnit\Framework\Assert ;
2024-10-08 12:26:40 +05:45
use Psr\Http\Message\ResponseInterface ;
2022-12-21 17:22:22 +05:45
use TestHelpers\Asserts\WebDav as WebDavTest ;
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' ;
/**
* Steps that relate to managing file/folder properties via WebDav
*/
class WebDavPropertiesContext implements Context {
2023-05-02 17:25:56 +05:45
private FeatureContext $featureContext ;
2022-12-21 17:22:22 +05:45
/**
* @var array map with user as key and another map as value,
* which has path as key and etag as value
*/
2023-05-02 17:25:56 +05:45
private array $storedETAG = [];
2022-12-21 17:22:22 +05:45
/**
* @When /^user "([^"]*)" gets the properties of (?:file|folder|entry) "([^"]*)" using the WebDAV API$/
*
* @param string $user
* @param string $path
*
* @return void
* @throws Exception
*/
public function userGetsThePropertiesOfFolder (
string $user ,
string $path
) : void {
2023-10-05 09:55:11 +05:45
$response = $this -> featureContext -> listFolder (
$user ,
$path ,
'0'
2022-12-21 17:22:22 +05:45
);
2023-10-05 09:55:11 +05:45
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @When /^user "([^"]*)" gets the properties of (?:file|folder|entry) "([^"]*)" with depth (\d+) using the WebDAV API$/
*
* @param string $user
* @param string $path
* @param string $depth
*
* @return void
* @throws Exception
*/
public function userGetsThePropertiesOfFolderWithDepth (
string $user ,
string $path ,
string $depth
) : void {
2023-10-05 09:55:11 +05:45
$response = $this -> featureContext -> listFolder (
$user ,
$path ,
$depth
2022-12-21 17:22:22 +05:45
);
2023-10-05 09:55:11 +05:45
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @param string $user
* @param string $path
2024-09-18 11:26:20 +05:45
* @param string|null $spaceId
2022-12-21 17:22:22 +05:45
* @param TableNode|null $propertiesTable
*
2023-10-05 09:55:11 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
* @throws Exception
*/
2023-10-05 09:55:11 +05:45
public function getPropertiesOfFolder (
2022-12-21 17:22:22 +05:45
string $user ,
string $path ,
2024-09-18 11:26:20 +05:45
? string $spaceId ,
2022-12-21 17:22:22 +05:45
TableNode $propertiesTable
2023-10-05 09:55:11 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$properties = null ;
$this -> featureContext -> verifyTableNodeColumns ( $propertiesTable , [ "propertyName" ]);
$this -> featureContext -> verifyTableNodeColumnsCount ( $propertiesTable , 1 );
2023-05-02 17:25:56 +05:45
foreach ( $propertiesTable -> getColumnsHash () as $row ) {
$properties [] = $row [ "propertyName" ];
2022-12-21 17:22:22 +05:45
}
2023-10-05 09:55:11 +05:45
return $this -> featureContext -> listFolder (
$user ,
$path ,
"1" ,
2024-09-18 11:26:20 +05:45
$properties ,
$spaceId ,
2022-12-21 17:22:22 +05:45
);
}
/**
2023-10-05 09:55:11 +05:45
* @When /^user "([^"]*)" gets the following properties of (?:file|folder|entry) "([^"]*)" using the WebDAV API$/
*
2022-12-21 17:22:22 +05:45
* @param string $user
* @param string $path
2023-10-05 09:55:11 +05:45
* @param TableNode|null $propertiesTable
2022-12-21 17:22:22 +05:45
*
* @return void
* @throws Exception
*/
2023-10-05 09:55:11 +05:45
public function userGetsFollowingPropertiesOfEntryUsingWebDavApi (
2022-12-21 17:22:22 +05:45
string $user ,
string $path ,
2023-10-05 09:55:11 +05:45
TableNode $propertiesTable
2022-12-21 17:22:22 +05:45
) : void {
2024-09-18 11:26:20 +05:45
$response = $this -> getPropertiesOfFolder ( $user , $path , null , $propertiesTable );
2023-10-05 09:55:11 +05:45
$this -> featureContext -> setResponse ( $response );
$this -> featureContext -> pushToLastStatusCodesArrays ();
2022-12-21 17:22:22 +05:45
}
/**
* @When /^the user gets the following properties of (?:file|folder|entry) "([^"]*)" using the WebDAV API$/
*
* @param string $path
* @param TableNode|null $propertiesTable
*
* @return void
* @throws Exception
*/
public function theUserGetsPropertiesOfFolder ( string $path , TableNode $propertiesTable ) {
2023-10-05 09:55:11 +05:45
$response = $this -> getPropertiesOfFolder (
2022-12-21 17:22:22 +05:45
$this -> featureContext -> getCurrentUser (),
$path ,
2024-09-18 11:26:20 +05:45
null ,
2022-12-21 17:22:22 +05:45
$propertiesTable
);
2023-10-05 09:55:11 +05:45
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
2024-08-05 12:25:55 +05:45
* @Given /^user "([^"]*)" has set the following properties to (?:file|folder|entry) "([^"]*)" using the WebDav API$/
2022-12-21 17:22:22 +05:45
*
* @param string $username
* @param string $path
* @param TableNode|null $propertiesTable with following columns with column header as:
* property: name of prop to be set
* value: value of prop to be set
*
* @return void
* @throws Exception
*/
public function userHasSetFollowingPropertiesUsingProppatch ( string $username , string $path , TableNode $propertiesTable ) {
$username = $this -> featureContext -> getActualUsername ( $username );
$this -> featureContext -> verifyTableNodeColumns ( $propertiesTable , [ 'propertyName' , 'propertyValue' ]);
$properties = $propertiesTable -> getColumnsHash ();
2023-10-05 09:55:11 +05:45
$response = WebDavHelper :: proppatchWithMultipleProps (
$this -> featureContext -> getBaseUrl (),
$username ,
$this -> featureContext -> getPasswordForUser ( $username ),
$path ,
$properties ,
$this -> featureContext -> getStepLineRef (),
$this -> featureContext -> getDavPathVersion ()
2022-12-21 17:22:22 +05:45
);
2023-10-05 09:55:11 +05:45
$this -> featureContext -> theHTTPStatusCodeShouldBe ( 207 , "" , $response );
2022-12-21 17:22:22 +05:45
}
2024-07-29 14:57:52 +05:45
/**
* @When user :user gets a custom property :propertyName of file :path
*
* @param string $user
* @param string $propertyName
* @param string $path
*
* @return void
* @throws Exception
*/
public function userGetsCustomPropertyOfFile (
string $user ,
string $propertyName ,
string $path
) : void {
$user = $this -> featureContext -> getActualUsername ( $user );
$properties = [ $propertyName ];
$response = WebDavHelper :: propfind (
2024-07-30 17:10:35 +05:45
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getActualUsername ( $user ),
$this -> featureContext -> getUserPassword ( $user ),
$path ,
$properties ,
$this -> featureContext -> getStepLineRef (),
"0" ,
2024-09-18 11:26:20 +05:45
null ,
2024-07-30 17:10:35 +05:45
"files" ,
$this -> featureContext -> getDavPathVersion ()
2024-07-29 14:57:52 +05:45
);
$this -> featureContext -> setResponse ( $response );
}
2022-12-21 17:22:22 +05:45
/**
* @When user :user gets a custom property :propertyName with namespace :namespace of file :path
*
* @param string $user
* @param string $propertyName
2024-04-26 15:27:00 +05:45
* @param string $namespace namespace in the form of "x1='http://whatever.org/ns'"
2022-12-21 17:22:22 +05:45
* @param string $path
*
* @return void
* @throws Exception
*/
2024-07-29 14:57:52 +05:45
public function userGetsPropertiesWithNamespaceOfFile (
2022-12-21 17:22:22 +05:45
string $user ,
string $propertyName ,
string $namespace ,
string $path
) : void {
$user = $this -> featureContext -> getActualUsername ( $user );
$properties = [
$namespace => $propertyName
];
2024-07-29 14:57:52 +05:45
$response = WebDavHelper :: propfind (
2024-07-30 17:10:35 +05:45
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getActualUsername ( $user ),
$this -> featureContext -> getUserPassword ( $user ),
$path ,
$properties ,
$this -> featureContext -> getStepLineRef (),
"0" ,
2024-10-08 14:46:33 +05:45
null ,
2024-07-30 17:10:35 +05:45
"files" ,
$this -> featureContext -> getDavPathVersion ()
2022-12-21 17:22:22 +05:45
);
2024-07-29 14:57:52 +05:45
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
/**
* @param string $path
* @param TableNode $propertiesTable
*
2023-10-05 09:55:11 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
* @throws Exception
*/
2023-10-05 09:55:11 +05:45
public function getPropertiesOfEntryFromLastLinkShare ( string $path , TableNode $propertiesTable ) : ResponseInterface {
2024-05-15 17:37:54 +05:45
$token = ( $this -> featureContext -> isUsingSharingNG ()) ? $this -> featureContext -> shareNgGetLastCreatedLinkShareToken () : $this -> featureContext -> getLastCreatedPublicShareToken ();
2022-12-21 17:22:22 +05:45
$properties = null ;
2023-05-02 17:25:56 +05:45
foreach ( $propertiesTable -> getRows () as $row ) {
$properties [] = $row [ 0 ];
2022-12-21 17:22:22 +05:45
}
2023-10-05 09:55:11 +05:45
return $this -> featureContext -> listFolder (
2024-05-15 17:37:54 +05:45
$token ,
2023-10-05 09:55:11 +05:45
$path ,
'0' ,
$properties ,
2024-10-08 14:46:33 +05:45
null ,
2024-09-17 17:11:20 +05:45
"public-files"
2022-12-21 17:22:22 +05:45
);
}
2023-10-05 09:55:11 +05:45
/**
* @When /^the public gets the following properties of (?:file|folder|entry) "([^"]*)" in the last created public link using the WebDAV API$/
*
* @param string $path
* @param TableNode $propertiesTable
*
* @return void
* @throws Exception
*/
public function thePublicGetsFollowingPropertiesOfEntryFromLastLinkShare ( string $path , TableNode $propertiesTable ) : void {
$response = $this -> getPropertiesOfEntryFromLastLinkShare ( $path , $propertiesTable );
$this -> featureContext -> setResponse ( $response );
}
2022-12-21 17:22:22 +05:45
/**
* @param string $user user id who sets the property
* @param string $propertyName name of property in Clark notation
* @param string $path path on which to set properties to
* @param string $propertyValue property value
2024-07-29 14:57:52 +05:45
* @param string|null $namespace namespace in the form of "x1='http://whatever.org/ns'"
2022-12-21 17:22:22 +05:45
*
2023-10-05 09:55:11 +05:45
* @return ResponseInterface
2022-12-21 17:22:22 +05:45
* @throws Exception
*/
2024-07-29 14:57:52 +05:45
public function setResourceProperty (
2022-12-21 17:22:22 +05:45
string $user ,
string $propertyName ,
string $path ,
2024-07-29 14:57:52 +05:45
string $propertyValue ,
string $namespace = null ,
2023-10-05 09:55:11 +05:45
) : ResponseInterface {
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
2023-10-05 09:55:11 +05:45
return WebDavHelper :: proppatch (
2022-12-21 17:22:22 +05:45
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getActualUsername ( $user ),
$this -> featureContext -> getUserPassword ( $user ),
$path ,
$propertyName ,
$propertyValue ,
$this -> featureContext -> getStepLineRef (),
$namespace ,
$this -> featureContext -> getDavPathVersion ()
);
}
/**
2024-07-29 14:57:52 +05:45
* @Given /^user "([^"]*)" sets property "([^"]*)" of (?:file|folder|entry) "([^"]*)" to "([^"]*)"$/
*
* @param string $user user id who sets the property
* @param string $propertyName name of property in Clark notation
* @param string $path path on which to set properties to
* @param string $propertyValue property value
*
* @return void
* @throws Exception
*/
public function userSetsPropertyOfEntryTo (
string $user ,
string $propertyName ,
string $path ,
string $propertyValue
) : void {
$response = $this -> setResourceProperty (
$user ,
$propertyName ,
$path ,
$propertyValue
);
$this -> featureContext -> setResponse ( $response );
}
/**
* @When /^user "([^"]*)" sets property "([^"]*)" with namespace "([^"]*)" of (?:file|folder|entry) "([^"]*)" to "([^"]*)" using the WebDAV API$/
2022-12-21 17:22:22 +05:45
*
* @param string $user user id who sets the property
* @param string $propertyName name of property in Clark notation
2024-04-26 15:27:00 +05:45
* @param string $namespace namespace in the form of "x1='http://whatever.org/ns'"
2022-12-21 17:22:22 +05:45
* @param string $path path on which to set properties to
* @param string $propertyValue property value
*
* @return void
* @throws Exception
*/
public function userSetsPropertyWithNamespaceOfEntryTo (
string $user ,
string $propertyName ,
string $namespace ,
string $path ,
string $propertyValue
) : void {
2024-07-29 14:57:52 +05:45
$response = $this -> setResourceProperty (
2022-12-21 17:22:22 +05:45
$user ,
$propertyName ,
$path ,
2024-07-29 14:57:52 +05:45
$propertyValue ,
$namespace
2022-12-21 17:22:22 +05:45
);
2023-10-05 09:55:11 +05:45
$this -> featureContext -> setResponse ( $response );
2022-12-21 17:22:22 +05:45
}
2024-07-29 14:57:52 +05:45
/**
* @Given /^user "([^"]*)" has set property "([^"]*)" of (?:file|folder|entry) "([^"]*)" to "([^"]*)"$/
*
* @param string $user user id who sets the property
* @param string $propertyName name of property in Clark notation
* @param string $path path on which to set properties to
* @param string $propertyValue property value
*
* @return void
* @throws Exception
*/
public function userHasSetPropertyOfEntryTo (
string $user ,
string $propertyName ,
string $path ,
string $propertyValue
) : void {
$response = $this -> setResourceProperty (
$user ,
$propertyName ,
$path ,
$propertyValue
);
$this -> featureContext -> theHTTPStatusCodeShouldBe ( 207 , "" , $response );
}
2022-12-21 17:22:22 +05:45
/**
* @Given /^user "([^"]*)" has set property "([^"]*)" with namespace "([^"]*)" of (?:file|folder|entry) "([^"]*)" to "([^"]*)"$/
*
* @param string $user user id who sets the property
* @param string $propertyName name of property in Clark notation
2024-04-26 15:27:00 +05:45
* @param string $namespace namespace in the form of "x1='http://whatever.org/ns'"
2022-12-21 17:22:22 +05:45
* @param string $path path on which to set properties to
* @param string $propertyValue property value
*
* @return void
* @throws Exception
*/
public function userHasSetPropertyWithNamespaceOfEntryTo (
string $user ,
string $propertyName ,
string $namespace ,
string $path ,
string $propertyValue
) : void {
2024-07-29 14:57:52 +05:45
$response = $this -> setResourceProperty (
2022-12-21 17:22:22 +05:45
$user ,
$propertyName ,
$path ,
2024-07-29 14:57:52 +05:45
$propertyValue ,
$namespace
2022-12-21 17:22:22 +05:45
);
2023-10-05 09:55:11 +05:45
$this -> featureContext -> theHTTPStatusCodeShouldBe ( 207 , "" , $response );
2022-12-21 17:22:22 +05:45
}
/**
2024-07-29 14:57:52 +05:45
* @Then /^the response should contain a custom "([^"]*)" property with value "(([^"\\]|\\.)*)"$/
2022-12-21 17:22:22 +05:45
*
* @param string $propertyName
* @param string $propertyValue
*
* @return void
* @throws Exception
*/
2024-07-29 14:57:52 +05:45
public function theResponseShouldContainCustomPropertyWithValue ( string $propertyName , string $propertyValue ) : void {
$propertyValue = \str_replace ( '\"' , '"' , $propertyValue );
2023-10-05 09:55:11 +05:45
$responseXmlObject = $this -> featureContext -> getResponseXml (
$this -> featureContext -> getResponse (),
__METHOD__
2022-12-21 17:22:22 +05:45
);
$xmlPart = $responseXmlObject -> xpath (
2024-07-29 14:57:52 +05:45
"//d:prop/" . " $propertyName "
2022-12-21 17:22:22 +05:45
);
Assert :: assertArrayHasKey (
0 ,
$xmlPart ,
"Cannot find property \" $propertyName \" "
);
Assert :: assertEquals (
$propertyValue ,
$xmlPart [ 0 ] -> __toString (),
" \" $propertyName \" has a value \" " .
$xmlPart [ 0 ] -> __toString () . " \" but \" $propertyValue \" expected"
);
}
/**
2024-07-29 14:57:52 +05:45
* @Then /^the response should contain a custom "([^"]*)" property with namespace "([^"]*)" and value "([^"]*)"$/
2022-12-21 17:22:22 +05:45
*
* @param string $propertyName
* @param string $namespaceString
* @param string $propertyValue
*
* @return void
* @throws Exception
*/
2024-07-29 14:57:52 +05:45
public function theResponseShouldContainACustomPropertyWithNamespaceAndValue (
2022-12-21 17:22:22 +05:45
string $propertyName ,
string $namespaceString ,
string $propertyValue
) : void {
// let's unescape quotes first
$propertyValue = \str_replace ( '\"' , '"' , $propertyValue );
2023-10-05 09:55:11 +05:45
$responseXmlObject = $this -> featureContext -> getResponseXml (
$this -> featureContext -> getResponse (),
__METHOD__
2022-12-21 17:22:22 +05:45
);
2024-08-05 12:25:55 +05:45
$ns = WebDavHelper :: parseNamespace ( $namespaceString );
2022-12-21 17:22:22 +05:45
$responseXmlObject -> registerXPathNamespace (
2024-07-29 14:57:52 +05:45
$ns -> prefix ,
$ns -> namespace
2022-12-21 17:22:22 +05:45
);
$xmlPart = $responseXmlObject -> xpath (
2024-07-29 14:57:52 +05:45
"//d:prop/ $propertyName "
2022-12-21 17:22:22 +05:45
);
Assert :: assertArrayHasKey (
0 ,
$xmlPart ,
"Cannot find property \" $propertyName \" "
);
Assert :: assertEquals (
$propertyValue ,
2024-07-29 14:57:52 +05:45
$xmlPart [ 0 ] -> __toString (),
2022-12-21 17:22:22 +05:45
" \" $propertyName \" has a value \" " .
2024-07-29 14:57:52 +05:45
$xmlPart [ 0 ] -> __toString () . " \" but \" $propertyValue \" expected"
2022-12-21 17:22:22 +05:45
);
}
/**
* @Then the single response should contain a property :property with a child property :childProperty
*
* @param string $property
* @param string $childProperty
*
* @return void
*
* @throws Exception
*/
public function theSingleResponseShouldContainAPropertyWithChildProperty (
string $property ,
string $childProperty
) : void {
2023-10-05 09:55:11 +05:45
$xmlPart = $this -> featureContext -> getResponseXml () -> xpath (
2022-12-21 17:22:22 +05:45
"//d:prop/ $property / $childProperty "
);
Assert :: assertTrue (
isset ( $xmlPart [ 0 ]),
"Cannot find property \" $property / $childProperty \" "
);
}
2024-07-29 14:57:52 +05:45
/**
* @Then the xml response should contain a property :key
*
* @param string $key
*
* @return void
* @throws Exception
*/
public function theResponseShouldContainProperty ( string $key ) : void {
$this -> checkResponseContainsProperty (
$this -> featureContext -> getResponse (),
$key
);
}
/**
* @Then the xml response should contain a property :key with namespace :namespace
*
* @param string $key
* @param string $namespace
*
* @return void
* @throws Exception
*/
public function theResponseShouldContainPropertyWithNamespace ( string $key , string $namespace ) : void {
$this -> checkResponseContainsProperty (
$this -> featureContext -> getResponse (),
$key ,
$namespace
);
}
2022-12-21 17:22:22 +05:45
/**
* @Then the single response should contain a property :key with value :value
*
* @param string $key
* @param string $expectedValue
*
* @return void
* @throws Exception
*/
public function theSingleResponseShouldContainAPropertyWithValue (
string $key ,
string $expectedValue
) : void {
2023-10-05 09:55:11 +05:45
$this -> checkResponseContainsAPropertyWithValue (
$this -> featureContext -> getResponse (),
2022-12-21 17:22:22 +05:45
$key ,
$expectedValue ,
$expectedValue
);
}
/**
* @Then the single response about the file owned by :user should contain a property :key with value :value
*
* @param string $user
* @param string $key
* @param string $expectedValue
*
* @return void
* @throws Exception
*/
public function theSingleResponseAboutTheFileOwnedByShouldContainAPropertyWithValue (
string $user ,
string $key ,
string $expectedValue
) : void {
2023-10-05 09:55:11 +05:45
$this -> checkResponseContainsAPropertyWithValue (
$this -> featureContext -> getResponse (),
2022-12-21 17:22:22 +05:45
$key ,
$expectedValue ,
$expectedValue ,
$user
);
}
/**
* @Then the single response should contain a property :key with value :value or with value :altValue
*
* @param string $key
* @param string $expectedValue
* @param string $altExpectedValue
*
* @return void
* @throws Exception
*/
public function theSingleResponseShouldContainAPropertyWithValueAndAlternative (
string $key ,
string $expectedValue ,
string $altExpectedValue
) : void {
2023-10-05 09:55:11 +05:45
$this -> checkResponseContainsAPropertyWithValue (
$this -> featureContext -> getResponse (),
2022-12-21 17:22:22 +05:45
$key ,
$expectedValue ,
$altExpectedValue
);
}
2024-07-29 14:57:52 +05:45
/**
* @param ResponseInterface $response
* @param string $key
* @param string|null $namespaceString
*
* @return SimpleXMLElement
* @throws Exception
*/
public function checkResponseContainsProperty ( ResponseInterface $response , string $key , string $namespaceString = null ) : SimpleXMLElement {
$xmlPart = $this -> featureContext -> getResponseXml ( $response );
if ( $namespaceString !== null ) {
2024-08-05 12:25:55 +05:45
$ns = WebDavHelper :: parseNamespace ( $namespaceString );
2024-07-29 14:57:52 +05:45
$xmlPart -> registerXPathNamespace (
$ns -> prefix ,
$ns -> namespace
);
}
$match = $xmlPart -> xpath ( "//d:prop/ $key " );
Assert :: assertTrue (
isset ( $match [ 0 ]),
"Cannot find property \" $key \" "
);
2024-07-30 17:10:35 +05:45
$property = \explode ( ":" , $key );
$propertyName = $property [ \count ( $property ) - 1 ];
2024-07-29 14:57:52 +05:45
Assert :: assertEquals (
2024-07-30 17:10:35 +05:45
$match [ 0 ] -> getName (),
$propertyName
2024-07-29 14:57:52 +05:45
);
return $match [ 0 ];
}
2022-12-21 17:22:22 +05:45
/**
2023-10-05 09:55:11 +05:45
* @param ResponseInterface $response
2022-12-21 17:22:22 +05:45
* @param string $key
* @param string $expectedValue
* @param string $altExpectedValue
* @param string|null $user
*
* @return void
* @throws Exception
*/
2023-10-05 09:55:11 +05:45
public function checkResponseContainsAPropertyWithValue (
ResponseInterface $response ,
2022-12-21 17:22:22 +05:45
string $key ,
string $expectedValue ,
string $altExpectedValue ,
? string $user = null
) : void {
2024-07-29 14:57:52 +05:45
$xmlPart = $this -> checkResponseContainsProperty ( $response , $key );
$value = $xmlPart -> __toString ();
2022-12-21 17:22:22 +05:45
$expectedValue = $this -> featureContext -> substituteInLineCodes (
$expectedValue ,
$user
);
$expectedValue = "#^ $expectedValue $#" ;
$altExpectedValue = "#^ $altExpectedValue $#" ;
if ( \preg_match ( $expectedValue , $value ) !== 1
&& \preg_match ( $altExpectedValue , $value ) !== 1
) {
Assert :: fail (
"Property \" $key \" found with value \" $value \" , " .
"expected \" $expectedValue \" or \" $altExpectedValue \" "
);
}
}
/**
* @Then the value of the item :xpath in the response should be :value
*
* @param string $xpath
* @param string $expectedValue
*
* @return void
* @throws Exception
*/
public function assertValueOfItemInResponseIs ( string $xpath , string $expectedValue ) : void {
$this -> assertValueOfItemInResponseAboutUserIs (
$xpath ,
null ,
$expectedValue
);
}
2023-01-05 17:54:33 +05:45
/**
* @Then as user :user the value of the item :xpath of path :path in the response should be :value
*
* @param string $user
* @param string $xpath
* @param string $path
* @param string $expectedValue
*
* @return void
* @throws Exception
*/
public function valueOfItemOfPathShouldBe ( string $user , string $xpath , string $path , string $expectedValue ) : void {
$path = $this -> featureContext -> substituteInLineCodes ( $path , $user );
2024-09-17 17:11:20 +05:45
$path = \ltrim ( $path , '/' );
2024-10-21 12:24:50 +05:45
$path = "/" . WebdavHelper :: prefixRemotePhp ( $path );
2023-01-05 17:54:33 +05:45
$fullXpath = "//d:response/d:href[.=' $path ']/following-sibling::d:propstat $xpath " ;
$this -> assertValueOfItemInResponseAboutUserIs (
$fullXpath ,
null ,
$expectedValue
);
}
2022-12-21 17:22:22 +05:45
/**
* @Then the value of the item :xpath in the response about user :user should be :value
*
* @param string $xpath
2024-04-17 16:50:33 +05:45
* @param string $user
2022-12-21 17:22:22 +05:45
* @param string $expectedValue
*
* @return void
* @throws Exception
*/
2024-04-17 16:50:33 +05:45
public function theValueOfTheItemInTheResponseAboutUserShouldBe ( string $xpath , string $user , string $expectedValue ) : void {
$this -> assertValueOfItemInResponseAboutUserIs ( $xpath , $user , $expectedValue );
}
/**
* @param string $xpath
* @param string|null $user
* @param string $expectedValue
*
* @return void
*/
2022-12-21 17:22:22 +05:45
public function assertValueOfItemInResponseAboutUserIs ( string $xpath , ? string $user , string $expectedValue ) : void {
2023-10-05 09:55:11 +05:45
$resXml = $this -> featureContext -> getResponseXml (
$this -> featureContext -> getResponse (),
__METHOD__
);
2023-06-28 10:56:08 +05:45
$value = $this -> getXmlItemByXpath ( $resXml , $xpath );
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$expectedValue = $this -> featureContext -> substituteInLineCodes (
$expectedValue ,
$user
);
// The expected value can contain /%base_path%/ which can be empty some time
2024-09-17 17:11:20 +05:45
// This will result in urls starting from '//', so replace that with single'/'
$expectedValue = preg_replace ( "/^\/\//i" , "/" , $expectedValue );
2022-12-21 17:22:22 +05:45
Assert :: assertEquals (
$expectedValue ,
$value ,
"item \" $xpath \" found with value \" $value \" , " .
"expected \" $expectedValue \" "
);
}
/**
* @Then the value of the item :xpath in the response about user :user should be :value1 or :value2
*
* @param string $xpath
* @param string|null $user
* @param string $expectedValue1
* @param string $expectedValue2
*
* @return void
* @throws Exception
*/
public function assertValueOfItemInResponseAboutUserIsEitherOr ( string $xpath , ? string $user , string $expectedValue1 , string $expectedValue2 ) : void {
if ( ! $expectedValue2 ) {
$expectedValue2 = $expectedValue1 ;
}
2023-10-05 09:55:11 +05:45
$resXml = $this -> featureContext -> getResponseXml (
$this -> featureContext -> getResponse (),
__METHOD__
);
2023-06-28 10:56:08 +05:45
$value = $this -> getXmlItemByXpath ( $resXml , $xpath );
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$expectedValue1 = $this -> featureContext -> substituteInLineCodes (
$expectedValue1 ,
$user
);
$expectedValue2 = $this -> featureContext -> substituteInLineCodes (
$expectedValue2 ,
$user
);
// The expected value can contain /%base_path%/ which can be empty some time
2024-09-17 17:11:20 +05:45
// This will result in urls starting from '//', so replace that with single'/'
$expectedValue1 = preg_replace ( "/^\/\//i" , "/" , $expectedValue1 );
$expectedValue2 = preg_replace ( "/^\/\//i" , "/" , $expectedValue2 );
2022-12-21 17:22:22 +05:45
$expectedValues = [ $expectedValue1 , $expectedValue2 ];
$isExpectedValueInMessage = \in_array ( $value , $expectedValues );
Assert :: assertTrue ( $isExpectedValueInMessage , "The actual value \" $value \" is not one of the expected values: \" $expectedValue1 \" or \" $expectedValue2 \" " );
}
2023-06-28 10:56:08 +05:45
/**
2024-04-26 15:27:00 +05:45
* @param SimpleXMLElement $xmlResponse
2023-06-28 10:56:08 +05:45
* @param string $xpath
*
* @return string
*/
public function getXmlItemByXpath (
SimpleXMLElement $xmlResponse ,
string $xpath
) : string {
$xmlPart = $xmlResponse -> xpath ( $xpath );
Assert :: assertTrue (
isset ( $xmlPart [ 0 ]),
"Cannot find item with xpath \" $xpath \" "
);
return $xmlPart [ 0 ] -> __toString ();
}
2022-12-21 17:22:22 +05:45
/**
* @Then the value of the item :xpath in the response should match :value
*
* @param string $xpath
* @param string $pattern
*
* @return void
* @throws Exception
*/
public function assertValueOfItemInResponseRegExp ( string $xpath , string $pattern ) : void {
2023-10-05 09:55:11 +05:45
$this -> assertXpathValueMatchesPattern (
$this -> featureContext -> getResponseXml (),
2022-12-21 17:22:22 +05:45
$xpath ,
$pattern
);
}
/**
* @Then /^as a public the lock discovery property "([^"]*)" of the (?:file|folder|entry) "([^"]*)" should match "([^"]*)"$/
*
* @param string $xpath
* @param string $path
* @param string $pattern
*
* @return void
* @throws Exception
*/
public function publicGetsThePropertiesOfFolderAndAssertValueOfItemInResponseRegExp ( string $xpath , string $path , string $pattern ) : void {
$propertiesTable = new TableNode ([[ 'propertyName' ],[ 'd:lockdiscovery' ]]);
2023-10-05 09:55:11 +05:45
$response = $this -> getPropertiesOfEntryFromLastLinkShare ( $path , $propertiesTable );
$this -> featureContext -> theHTTPStatusCodeShouldBe ( '207' , "" , $response );
$this -> assertXpathValueMatchesPattern (
$this -> featureContext -> getResponseXml ( $response ),
2022-12-21 17:22:22 +05:45
$xpath ,
$pattern
);
}
/**
* @Then there should be an entry with href containing :expectedHref in the response to user :user
*
* @param string $expectedHref
* @param string $user
*
* @return void
* @throws Exception
*/
public function assertEntryWithHrefMatchingRegExpInResponseToUser ( string $expectedHref , string $user ) : void {
2023-10-05 09:55:11 +05:45
$resXml = $this -> featureContext -> getResponseXml (
$this -> featureContext -> getResponse (),
__METHOD__
);
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$expectedHref = $this -> featureContext -> substituteInLineCodes (
$expectedHref ,
$user ,
[ 'preg_quote' => [ '/' ]]
);
2024-10-21 12:24:50 +05:45
$expectedHref = WebdavHelper :: prefixRemotePhp ( $expectedHref );
2022-12-21 17:22:22 +05:45
$index = 0 ;
while ( true ) {
$index ++ ;
$xpath = "//d:response[ $index ]/d:href" ;
$xmlPart = $resXml -> xpath ( $xpath );
// If we have run out of entries in the response, then fail the test
Assert :: assertTrue (
isset ( $xmlPart [ 0 ]),
"Cannot find any entry having href with value $expectedHref in response to $user "
);
$value = $xmlPart [ 0 ] -> __toString ();
$decodedValue = \rawurldecode ( $value );
$explodeDecoded = \explode ( '/' , $decodedValue );
// get the first item of the expected href.
2024-09-17 17:11:20 +05:45
// 'dav' from "dav/spaces/%spaceid%/C++ file.cpp"
$explodeExpected = \explode ( '/' , $expectedHref )[ 0 ];
$remotePhpIndex = \array_search ( $explodeExpected , $explodeDecoded );
2022-12-21 17:22:22 +05:45
if ( $remotePhpIndex ) {
$explodedHrefPartArray = \array_slice ( $explodeDecoded , $remotePhpIndex );
$actualHrefPart = \implode ( '/' , $explodedHrefPartArray );
if ( $this -> featureContext -> getDavPathVersion () === WebDavHelper :: DAV_VERSION_SPACES ) {
// for spaces webdav, space id is included in the href
// space id from our helper is returned as d8c029e0\-2bc9\-4b9a\-8613\-c727e5417f05
// so we've to remove "\" before every "-"
$expectedHref = str_replace ( '\-' , '-' , $expectedHref );
$expectedHref = str_replace ( '\$' , '$' , $expectedHref );
$expectedHref = str_replace ( '\!' , '!' , $expectedHref );
}
if ( $actualHrefPart === $expectedHref ) {
break ;
}
}
}
}
/**
2023-10-05 09:55:11 +05:45
* @param SimpleXMLElement $responseXml
2022-12-21 17:22:22 +05:45
* @param string $xpath
* @param string $pattern
2023-10-05 09:55:11 +05:45
* @param string|null $user
2022-12-21 17:22:22 +05:45
*
* @return void
* @throws Exception
*/
2023-10-05 09:55:11 +05:45
public function assertXpathValueMatchesPattern ( SimpleXMLElement $responseXml , string $xpath , string $pattern , ? string $user = null ) : void {
$xmlPart = $responseXml -> xpath ( $xpath );
2022-12-21 17:22:22 +05:45
Assert :: assertTrue (
isset ( $xmlPart [ 0 ]),
"Cannot find item with xpath \" $xpath \" "
);
$user = $this -> featureContext -> getActualUsername ( $user );
$value = $xmlPart [ 0 ] -> __toString ();
2024-05-15 17:37:54 +05:45
$callback = ( $this -> featureContext -> isUsingSharingNG ()) ? "shareNgGetLastCreatedLinkShareToken" : "getLastCreatedPublicShareToken" ;
2024-09-17 17:11:20 +05:45
if ( \str_ends_with ( $xpath , "d:href" )) {
$pattern = \preg_replace ( "/^\//" , "" , $pattern );
$pattern = \preg_replace ( "/^\^/" , "" , $pattern );
$pattern = \ltrim ( $pattern , "\/" );
2024-10-21 12:24:50 +05:45
$prefixRemotePhp = \rtrim ( WebdavHelper :: prefixRemotePhp ( "" ), "/" );
$pattern = "/^\/ { $prefixRemotePhp } \/ { $pattern } " ;
2024-09-17 17:11:20 +05:45
}
2022-12-21 17:22:22 +05:45
$pattern = $this -> featureContext -> substituteInLineCodes (
$pattern ,
$user ,
2023-01-05 14:39:18 +05:45
[ 'preg_quote' => [ '/' ]],
[
[
"code" => "%public_token%" ,
"function" =>
2024-05-15 17:37:54 +05:45
[ $this -> featureContext , $callback ],
2023-01-05 14:39:18 +05:45
"parameter" => []
],
]
2022-12-21 17:22:22 +05:45
);
Assert :: assertMatchesRegularExpression (
$pattern ,
$value ,
"item \" $xpath \" found with value \" $value \" , " .
"expected to match regex pattern: \" $pattern \" "
);
}
2023-10-05 09:55:11 +05:45
/**
* @Then the value of the item :xpath in the response to user :user should match :value
*
* @param string $xpath
* @param string|null $user
* @param string $pattern
* @param SimpleXMLElement|null $responseXml
*
* @return void
* @throws Exception
*/
public function theValueOfItemInResponseToUserShouldMatch ( string $xpath , ? string $user , string $pattern , ? SimpleXMLElement $responseXml = null ) : void {
$resXml = $this -> featureContext -> getResponseXml ();
$this -> assertXpathValueMatchesPattern ( $resXml , $xpath , $pattern , $user );
}
2022-12-21 17:22:22 +05:45
/**
* @Then /^as user "([^"]*)" the lock discovery property "([^"]*)" of the (?:file|folder|entry) "([^"]*)" should match "([^"]*)"$/
*
* @param string|null $user
* @param string $xpath
* @param string $path
* @param string $pattern
*
* @return void
* @throws Exception
*/
public function userGetsPropertiesOfFolderAndAssertValueOfItemInResponseToUserRegExp ( string $user , string $xpath , string $path , string $pattern ) : void {
$propertiesTable = new TableNode ([[ 'propertyName' ],[ 'd:lockdiscovery' ]]);
2023-10-05 09:55:11 +05:45
$response = $this -> getPropertiesOfFolder (
2022-12-21 17:22:22 +05:45
$user ,
$path ,
2024-09-18 11:26:20 +05:45
null ,
2022-12-21 17:22:22 +05:45
$propertiesTable
);
2023-10-05 09:55:11 +05:45
$this -> featureContext -> theHTTPStatusCodeShouldBe ( '207' , '' , $response );
$this -> assertXpathValueMatchesPattern (
$this -> featureContext -> getResponseXml ( $response ),
2022-12-21 17:22:22 +05:45
$xpath ,
2023-10-05 09:55:11 +05:45
$pattern ,
$user
2022-12-21 17:22:22 +05:45
);
}
/**
* @Then the item :xpath in the response should not exist
*
* @param string $xpath
*
* @return void
* @throws Exception
*/
public function assertItemInResponseDoesNotExist ( string $xpath ) : void {
2023-10-05 09:55:11 +05:45
$xmlPart = $this -> featureContext -> getResponseXml () -> xpath ( $xpath );
2022-12-21 17:22:22 +05:45
Assert :: assertFalse (
isset ( $xmlPart [ 0 ]),
"Found item with xpath \" $xpath \" but it should not exist"
);
}
/**
* @Then /^as user "([^"]*)" (?:file|folder|entry) "([^"]*)" should contain a property "([^"]*)" with value "([^"]*)" or with value "([^"]*)"$/
2024-04-17 16:50:33 +05:45
* @Then /^as user "([^"]*)" (?:file|folder|entry) "([^"]*)" should contain a property "([^"]*)" with value "([^"]*)"$/
2022-12-21 17:22:22 +05:45
*
* @param string $user
* @param string $path
* @param string $property
* @param string $expectedValue
2024-04-26 15:27:00 +05:45
* @param string|null $altExpectedValue
2022-12-21 17:22:22 +05:45
*
* @return void
*/
public function asUserFolderShouldContainAPropertyWithValueOrWithValue (
string $user ,
string $path ,
string $property ,
string $expectedValue ,
2024-04-17 16:50:33 +05:45
? string $altExpectedValue = null
2022-12-21 17:22:22 +05:45
) : void {
2024-04-17 16:50:33 +05:45
$this -> checkPropertyOfAFolder ( $user , $path , $property , $expectedValue , $altExpectedValue );
2022-12-21 17:22:22 +05:45
}
/**
* @param string $user
* @param string $path
* @param string $property
2024-04-17 16:50:33 +05:45
* @param string $expectedValue
* @param string|null $altExpectedValue
2024-09-18 11:26:20 +05:45
* @param string|null $spaceId
2022-12-21 17:22:22 +05:45
*
* @return void
*/
2024-04-17 16:50:33 +05:45
public function checkPropertyOfAFolder (
2022-12-21 17:22:22 +05:45
string $user ,
string $path ,
string $property ,
2024-04-17 16:50:33 +05:45
string $expectedValue ,
2024-09-18 11:26:20 +05:45
? string $altExpectedValue = null ,
? string $spaceId = null ,
2022-12-21 17:22:22 +05:45
) : void {
2024-04-17 16:50:33 +05:45
$response = $this -> featureContext -> listFolder (
2022-12-21 17:22:22 +05:45
$user ,
$path ,
2024-04-17 16:50:33 +05:45
'0' ,
2024-09-18 11:26:20 +05:45
[ $property ],
$spaceId ,
2024-04-17 16:50:33 +05:45
);
if ( $altExpectedValue === null ) {
$altExpectedValue = $expectedValue ;
}
$this -> checkResponseContainsAPropertyWithValue (
$response ,
2022-12-21 17:22:22 +05:45
$property ,
2024-04-17 16:50:33 +05:45
$expectedValue ,
$altExpectedValue
2022-12-21 17:22:22 +05:45
);
}
/**
* @Then the single response should contain a property :key with value like :regex
*
* @param string $key
* @param string $regex
*
* @return void
* @throws Exception
*/
public function theSingleResponseShouldContainAPropertyWithValueLike (
string $key ,
string $regex
) : void {
2023-10-05 09:55:11 +05:45
$xmlPart = $this -> featureContext -> getResponseXml () -> xpath (
2022-12-21 17:22:22 +05:45
"//d:prop/ $key "
);
Assert :: assertTrue (
isset ( $xmlPart [ 0 ]),
"Cannot find property \" $key \" "
);
$value = $xmlPart [ 0 ] -> __toString ();
Assert :: assertMatchesRegularExpression (
$regex ,
$value ,
"Property \" $key \" found with value \" $value \" , expected \" $regex \" "
);
}
/**
* @Then the response should contain a share-types property with
*
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function theResponseShouldContainAShareTypesPropertyWith ( TableNode $table ) : void {
$this -> featureContext -> verifyTableNodeColumnsCount ( $table , 1 );
WebdavTest :: assertResponseContainsShareTypes (
2023-10-05 09:55:11 +05:45
$this -> featureContext -> getResponseXml (),
2022-12-21 17:22:22 +05:45
$table -> getRows ()
);
}
/**
* @Then the response should contain an empty property :property
*
* @param string $property
*
* @return void
* @throws Exception
*/
public function theResponseShouldContainAnEmptyProperty ( string $property ) : void {
2023-10-05 09:55:11 +05:45
$xmlPart = $this -> featureContext -> getResponseXml () -> xpath (
2022-12-21 17:22:22 +05:45
"//d:prop/ $property "
);
Assert :: assertCount (
1 ,
$xmlPart ,
"Cannot find property \" $property \" "
);
Assert :: assertEmpty (
$xmlPart [ 0 ],
"Property \" $property \" is not empty"
);
}
/**
* @param string $user
* @param string $path
* @param string|null $storePath
2024-09-18 11:26:20 +05:45
* @param string|null $spaceId
2022-12-21 17:22:22 +05:45
*
2023-10-05 09:55:11 +05:45
* @return SimpleXMLElement
2022-12-21 17:22:22 +05:45
* @throws Exception
*/
2024-09-18 11:26:20 +05:45
public function storeEtagOfElement ( string $user , string $path , ? string $storePath = "" , ? string $spaceId = null ) : SimpleXMLElement {
2022-12-21 17:22:22 +05:45
if ( $storePath === "" ) {
$storePath = $path ;
}
$user = $this -> featureContext -> getActualUsername ( $user );
2024-07-30 17:10:35 +05:45
$propertiesTable = new TableNode ([[ 'propertyName' ],[ 'd:getetag' ]]);
2023-10-05 09:55:11 +05:45
$response = $this -> getPropertiesOfFolder (
2022-12-21 17:22:22 +05:45
$user ,
$path ,
2024-09-18 11:26:20 +05:45
$spaceId ,
2022-12-21 17:22:22 +05:45
$propertiesTable
);
2023-10-05 09:55:11 +05:45
$xmlObject = $this -> featureContext -> getResponseXml ( $response );
2022-12-21 17:22:22 +05:45
$this -> storedETAG [ $user ][ $storePath ]
2023-10-05 09:55:11 +05:45
= $this -> featureContext -> getEtagFromResponseXmlObject ( $xmlObject );
return $xmlObject ;
2022-12-21 17:22:22 +05:45
}
/**
* @When user :user stores etag of element :path using the WebDAV API
*
* @param string $user
* @param string $path
*
* @return void
* @throws Exception
*/
public function userStoresEtagOfElement ( string $user , string $path ) : void {
$this -> storeEtagOfElement (
$user ,
$path
);
}
/**
* @Given user :user has stored etag of element :path on path :storePath
*
* @param string $user
* @param string $path
* @param string $storePath
*
* @return void
* @throws Exception
*/
public function userStoresEtagOfElementOnPath ( string $user , string $path , string $storePath ) : void {
$user = $this -> featureContext -> getActualUsername ( $user );
$this -> storeEtagOfElement (
$user ,
$path ,
$storePath
);
if ( $storePath == "" ) {
$storePath = $path ;
}
if ( $this -> storedETAG [ $user ][ $storePath ] === null || $this -> storedETAG [ $user ][ $path ] === "" ) {
throw new Exception ( "Expected stored etag to be some string but found null!" );
}
}
/**
* @Given user :user has stored etag of element :path
*
* @param string $user
* @param string $path
*
* @return void
* @throws Exception
*/
public function userHasStoredEtagOfElement ( string $user , string $path ) : void {
$user = $this -> featureContext -> getActualUsername ( $user );
$this -> storeEtagOfElement (
$user ,
$path
);
if ( $this -> storedETAG [ $user ][ $path ] === "" || $this -> storedETAG [ $user ][ $path ] === null ) {
throw new Exception ( "Expected stored etag to be some string but found null!" );
}
}
/**
* @Then /^the properties response should contain an etag$/
*
* @return void
* @throws Exception
*/
public function thePropertiesResponseShouldContainAnEtag () : void {
Assert :: assertTrue (
2023-10-05 09:55:11 +05:45
$this -> featureContext -> isEtagValid ( $this -> featureContext -> getEtagFromResponseXmlObject ()),
2022-12-21 17:22:22 +05:45
__METHOD__
. " getetag not found in response"
);
}
2024-08-05 12:25:55 +05:45
/**
* @param string $href
*
* @return string
*/
public function parseBaseDavPathFromXMLHref ( string $href ) : string {
$hrefArr = \explode ( '/' , $href );
if ( \in_array ( "webdav" , $hrefArr )) {
$hrefArr = \array_slice ( $hrefArr , 0 , \array_search ( "webdav" , $hrefArr ) + 1 );
} elseif ( \in_array ( "files" , $hrefArr )) {
$hrefArr = \array_slice ( $hrefArr , 0 , \array_search ( "files" , $hrefArr ) + 2 );
} elseif ( \in_array ( "spaces" , $hrefArr )) {
$hrefArr = \array_slice ( $hrefArr , 0 , \array_search ( "spaces" , $hrefArr ) + 2 );
}
return \implode ( '/' , $hrefArr );
}
2022-12-21 17:22:22 +05:45
/**
* @Then as user :username the last response should have the following properties
*
* only supports new DAV version
*
* @param string $username
2024-04-26 15:27:00 +05:45
* @param TableNode $expectedPropTable with the following columns:
2022-12-21 17:22:22 +05:45
* resource: full path of resource(file/folder/entry) from root of your oc storage
2024-04-26 15:27:00 +05:45
* property: expected name of property to be asserted, e.g.: status, href, customPropName
2022-12-21 17:22:22 +05:45
* value: expected value of expected property
*
* @return void
* @throws Exception
*/
public function theResponseShouldHavePropertyWithValue ( string $username , TableNode $expectedPropTable ) : void {
$username = $this -> featureContext -> getActualUsername ( $username );
$this -> featureContext -> verifyTableNodeColumns ( $expectedPropTable , [ 'resource' , 'propertyName' , 'propertyValue' ]);
2023-10-05 09:55:11 +05:45
$responseXmlObject = $this -> featureContext -> getResponseXml ();
2022-12-21 17:22:22 +05:45
2024-08-05 12:25:55 +05:45
$href = ( string ) $responseXmlObject -> xpath ( "//d:href" )[ 0 ];
$hrefBase = $this -> parseBaseDavPathFromXMLHref ( $href );
2022-12-21 17:22:22 +05:45
foreach ( $expectedPropTable -> getColumnsHash () as $col ) {
2024-08-05 12:25:55 +05:45
$xpath = "//d:href[.=' $hrefBase " . $col [ "resource" ] . "']" .
"/following-sibling::d:propstat//" . $col [ "propertyName" ];
$xmlPart = $responseXmlObject -> xpath ( $xpath );
2022-12-21 17:22:22 +05:45
Assert :: assertEquals (
$col [ "propertyValue" ],
$xmlPart [ 0 ],
__METHOD__
. " Expected '" . $col [ "propertyValue" ] . "' but got '" . $xmlPart [ 0 ] . "'"
);
}
}
/**
* @param string $path
* @param string $user
*
* @return string
* @throws Exception
*/
public function getCurrentEtagOfElement ( string $path , string $user ) : string {
$user = $this -> featureContext -> getActualUsername ( $user );
2024-07-30 17:10:35 +05:45
$propertiesTable = new TableNode ([[ 'propertyName' ],[ 'd:getetag' ]]);
2023-10-05 09:55:11 +05:45
$response = $this -> getPropertiesOfFolder (
2022-12-21 17:22:22 +05:45
$user ,
$path ,
2024-09-18 11:26:20 +05:45
null ,
2022-12-21 17:22:22 +05:45
$propertiesTable
);
2023-10-05 09:55:11 +05:45
return $this -> featureContext -> getEtagFromResponseXmlObject ( $this -> featureContext -> getResponseXml ( $response ));
2022-12-21 17:22:22 +05:45
}
/**
* @param string $path
* @param string $user
* @param string $messageStart
*
* @return string
*/
public function getStoredEtagOfElement ( string $path , string $user , string $messageStart = '' ) : string {
if ( $messageStart === '' ) {
$messageStart = __METHOD__ ;
}
Assert :: assertArrayHasKey (
$user ,
$this -> storedETAG ,
$messageStart
. " Trying to check etag of element $path of user $user but the user does not have any stored etags"
);
Assert :: assertArrayHasKey (
$path ,
$this -> storedETAG [ $user ],
$messageStart
. " Trying to check etag of element $path of user $user but the user does not have a stored etag for the element"
);
return $this -> storedETAG [ $user ][ $path ];
}
/**
* @Then these etags should not have changed:
*
* @param TableNode $etagTable
*
* @return void
* @throws Exception
*/
public function theseEtagsShouldNotHaveChanged ( TableNode $etagTable ) : void {
$this -> featureContext -> verifyTableNodeColumns ( $etagTable , [ "user" , "path" ]);
$this -> featureContext -> verifyTableNodeColumnsCount ( $etagTable , 2 );
$changedEtagCount = 0 ;
$changedEtagMessage = __METHOD__ ;
foreach ( $etagTable -> getColumnsHash () as $row ) {
$user = $row [ "user" ];
$path = $row [ "path" ];
$user = $this -> featureContext -> getActualUsername ( $user );
$actualEtag = $this -> getCurrentEtagOfElement ( $path , $user );
$storedEtag = $this -> getStoredEtagOfElement ( $path , $user , __METHOD__ );
if ( $actualEtag !== $storedEtag ) {
$changedEtagCount = $changedEtagCount + 1 ;
$changedEtagMessage
.= " \n The etag ' $storedEtag ' of element ' $path ' of user ' $user ' changed to ' $actualEtag '." ;
}
}
Assert :: assertEquals ( 0 , $changedEtagCount , $changedEtagMessage );
}
/**
* @Then these etags should have changed:
*
* @param TableNode $etagTable
*
* @return void
* @throws Exception
*/
public function theseEtagsShouldHaveChanged ( TableNode $etagTable ) : void {
$this -> featureContext -> verifyTableNodeColumns ( $etagTable , [ "user" , "path" ]);
$this -> featureContext -> verifyTableNodeColumnsCount ( $etagTable , 2 );
$unchangedEtagCount = 0 ;
$unchangedEtagMessage = __METHOD__ ;
foreach ( $etagTable -> getColumnsHash () as $row ) {
$user = $row [ "user" ];
$path = $row [ "path" ];
$user = $this -> featureContext -> getActualUsername ( $user );
$actualEtag = $this -> getCurrentEtagOfElement ( $path , $user );
$storedEtag = $this -> getStoredEtagOfElement ( $path , $user , __METHOD__ );
if ( $actualEtag === $storedEtag ) {
$unchangedEtagCount = $unchangedEtagCount + 1 ;
$unchangedEtagMessage
.= " \n The etag ' $storedEtag ' of element ' $path ' of user ' $user ' did not change." ;
}
}
Assert :: assertEquals ( 0 , $unchangedEtagCount , $unchangedEtagMessage );
}
/**
2023-06-28 10:56:08 +05:45
* @Then /^the etag of element "([^"]*)" of user "([^"]*)" (should|should not) have changed$/
2022-12-21 17:22:22 +05:45
*
* @param string $path
* @param string $user
2023-06-28 10:56:08 +05:45
* @param string $shouldShouldNot
2022-12-21 17:22:22 +05:45
*
* @return void
* @throws Exception
*/
2023-06-28 10:56:08 +05:45
public function etagOfElementOfUserShouldOrShouldNotHaveChanged ( string $path , string $user , string $shouldShouldNot ) : void {
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$actualEtag = $this -> getCurrentEtagOfElement ( $path , $user );
$storedEtag = $this -> getStoredEtagOfElement ( $path , $user , __METHOD__ );
2023-06-28 10:56:08 +05:45
if ( $shouldShouldNot === 'should not' ) {
Assert :: assertEquals (
$storedEtag ,
$actualEtag ,
__METHOD__
. " The etag of element ' $path ' of user ' $user ' was not expected to change."
. " The stored etag was ' $storedEtag ' but got ' $actualEtag ' from the response"
);
} else {
Assert :: assertNotEquals (
$storedEtag ,
$actualEtag ,
__METHOD__
. " The etag of element ' $path ' of user ' $user ' was expected to change."
. " The stored etag was ' $storedEtag ' and also got ' $actualEtag ' from the response"
);
}
2022-12-21 17:22:22 +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
2024-10-08 12:26:40 +05:45
$this -> featureContext = BehatHelper :: getContext ( $scope , $environment , 'FeatureContext' );
2022-12-21 17:22:22 +05:45
}
}