Skip to content

Commit ca82ac0

Browse files
committed
test: allow multiple xpaths checks
1 parent 732f2b2 commit ca82ac0

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

tests/acceptance/TestHelpers/HttpRequestHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ public static function sendScenarioLineReferencesInXRequestId(): bool {
674674
}
675675

676676
/**
677-
* @return bool
677+
* @return string
678678
*/
679679
public static function getXRequestIdRegex(): string {
680680
if (self::sendScenarioLineReferencesInXRequestId()) {

tests/acceptance/bootstrap/SpacesContext.php

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3772,6 +3772,7 @@ public function sendPropfindRequestToSpace(string $user, string $spaceName, ?str
37723772

37733773
$davPathVersion = $this->featureContext->getDavPathVersion();
37743774
if ($spaceName === 'Shares' && $davPathVersion !== WebDavHelper::DAV_VERSION_SPACES) {
3775+
// for old/new dav paths, append the Shares space path
37753776
if ($resource === '' || $resource === '/') {
37763777
$resource = $spaceName;
37773778
} else {
@@ -3795,10 +3796,9 @@ public function sendPropfindRequestToSpace(string $user, string $spaceName, ?str
37953796
}
37963797

37973798
/**
3798-
* @Then /^as user "([^"]*)" the (PROPFIND|REPORT) response should contain a (resource|space) "([^"]*)" with these key and value pairs:$/
3799+
* @Then /^as user "([^"]*)" the (?:PROPFIND|REPORT) response should contain a (resource|space) "([^"]*)" with these key and value pairs:$/
37993800
*
38003801
* @param string $user
3801-
* @param string $method # method should be either PROPFIND or REPORT
38023802
* @param string $type # type should be either resource or space
38033803
* @param string $resource
38043804
* @param TableNode $table
@@ -3807,26 +3807,29 @@ public function sendPropfindRequestToSpace(string $user, string $spaceName, ?str
38073807
* @throws GuzzleException
38083808
* @throws JsonException
38093809
*/
3810-
public function asUsertheXMLResponseShouldContainMountpointWithTheseKeyAndValuePair(string $user, string $method, string $type, string $resource, TableNode $table): void {
3810+
public function asUsertheXMLResponseShouldContainMountpointWithTheseKeyAndValuePair(string $user, string $type, string $resource, TableNode $table): void {
38113811
$this->featureContext->verifyTableNodeColumns($table, ['key', 'value']);
38123812
if ($this->featureContext->getDavPathVersion() === WebDavHelper::DAV_VERSION_SPACES && $type === 'space') {
38133813
$space = $this->getSpaceByName($user, $resource);
38143814
$resource = $space['id'];
3815+
} elseif (\preg_match(GraphHelper::jsonSchemaRegexToPureRegex(GraphHelper::getFileIdRegex()), $resource)) {
3816+
// When using file-id, some characters need to be encoded
3817+
$resource = \str_replace("!", "%21", $resource);
38153818
} else {
38163819
$resource = \rawurlencode($resource);
38173820
}
3818-
$this->theXMLResponseShouldContain($resource, $table);
3821+
$this->theXMLResponseShouldContain($resource, $table->getHash());
38193822
}
38203823

38213824
/**
3822-
* @param string $resource
3823-
* @param TableNode $table
3825+
* @param string $resource // can be resource name, space id or file id
3826+
* @param array $properties // ["key" => "value"]
38243827
*
38253828
* @return void
38263829
* @throws GuzzleException
38273830
* @throws JsonException
38283831
*/
3829-
public function theXMLResponseShouldContain(string $resource, TableNode $table): void {
3832+
public function theXMLResponseShouldContain(string $resource, array $properties): void {
38303833
$xmlResponse = $this->featureContext->getResponseXml();
38313834
$hrefs = array_map(fn ($href) => $href->__toString(), $xmlResponse->xpath("//d:response/d:href"));
38323835

@@ -3838,29 +3841,67 @@ public function theXMLResponseShouldContain(string $resource, TableNode $table):
38383841
}
38393842
}
38403843

3841-
foreach ($table->getHash() as $row) {
3842-
$itemToFind = $row['key'];
3843-
$foundXmlItem = $xmlResponse->xpath("//d:href[text()='$currentHref']/following-sibling::d:propstat//$itemToFind");
3844+
foreach ($properties as $property) {
3845+
$itemToFind = $property['key'];
3846+
3847+
// if href is not found, build xpath using oc:name
3848+
$xpaths = [];
3849+
if (!$currentHref) {
3850+
$decodedResource = \urldecode($resource);
3851+
if ($property['key'] === 'oc:name') {
3852+
$xpath = "//oc:name[text()='$decodedResource']";
3853+
} elseif (\array_key_exists('oc:shareroot', $properties)) {
3854+
$xpaths[] = "//oc:name[text()='$resource']/preceding-sibling::oc:shareroot[text()='" . $properties['oc:shareroot'] . "'/preceding-sibling::/";
3855+
$xpaths[] = "//oc:name[text()='$resource']/preceding-sibling::oc:shareroot[text()='" . $properties['oc:shareroot'] . "'/following-sibling::/";
3856+
$xpaths[] = "//oc:name[text()='$resource']/following-sibling::oc:shareroot[text()='" . $properties['oc:shareroot'] . "'/preceding-sibling::/";
3857+
$xpaths[] = "//oc:name[text()='$resource']/following-sibling::oc:shareroot[text()='" . $properties['oc:shareroot'] . "'/following-sibling::/";
3858+
} else {
3859+
$xpaths[] = "//oc:name[text()='$decodedResource']/preceding-sibling::";
3860+
$xpaths[] = "//oc:name[text()='$decodedResource']/following-sibling::";
3861+
}
3862+
} else {
3863+
$xpath = "//d:href[text()='$currentHref']/following-sibling::d:propstat//$itemToFind";
3864+
}
3865+
3866+
if (\count($xpaths)) {
3867+
// check every xpath
3868+
foreach ($xpaths as $key => $path) {
3869+
$xpath = "{$path}{$itemToFind}";
3870+
$foundXmlItem = $xmlResponse->xpath($xpath);
3871+
$xpaths[$key] = $xpath;
3872+
if (\count($foundXmlItem)) {
3873+
break;
3874+
}
3875+
}
3876+
} else {
3877+
$foundXmlItem = $xmlResponse->xpath($xpath);
3878+
$xpaths[] = $xpath;
3879+
}
3880+
38443881
Assert::assertNotEmpty(
38453882
$foundXmlItem,
3846-
'The xml response "' . $xmlResponse->asXML() . '" did not contain "<' . $itemToFind . '>" element'
3883+
// all these for the sake of a nice error message
3884+
"Using xpaths:\n\t- " . \join("\n\t- ", $xpaths)
3885+
. "\n"
3886+
. "Could not find '<$itemToFind>' element in the XML response\n\t"
3887+
. "'" . \trim($xmlResponse->asXML()) . "'"
38473888
);
38483889

38493890
$actualValue = $foundXmlItem[0]->__toString();
3850-
$expectedValue = $this->featureContext->substituteInLineCodes($row['value']);
3891+
$expectedValue = $this->featureContext->substituteInLineCodes($property['value']);
38513892

38523893
switch ($itemToFind) {
38533894
case "oc:fileid":
38543895
$expectedValue = GraphHelper::jsonSchemaRegexToPureRegex($expectedValue);
3855-
Assert::assertRegExp($expectedValue, $actualValue, 'wrong "fileid" in the response');
3896+
Assert::assertMatchesRegularExpression($expectedValue, $actualValue, 'wrong "fileid" in the response');
38563897
break;
38573898
case "oc:file-parent":
38583899
$expectedValue = GraphHelper::jsonSchemaRegexToPureRegex($expectedValue);
3859-
Assert::assertRegExp($expectedValue, $actualValue, 'wrong "file-parent" in the response');
3900+
Assert::assertMatchesRegularExpression($expectedValue, $actualValue, 'wrong "file-parent" in the response');
38603901
break;
38613902
case "oc:privatelink":
38623903
$expectedValue = GraphHelper::jsonSchemaRegexToPureRegex($expectedValue);
3863-
Assert::assertRegExp($expectedValue, $actualValue, 'wrong "privatelink" in the response');
3904+
Assert::assertMatchesRegularExpression($expectedValue, $actualValue, 'wrong "privatelink" in the response');
38643905
break;
38653906
case "oc:tags":
38663907
// The value should be a comma-separated string of tag names.
@@ -3888,7 +3929,7 @@ public function theXMLResponseShouldContain(string $resource, TableNode $table):
38883929
break;
38893930
case "oc:remote-item-id":
38903931
$expectedValue = GraphHelper::jsonSchemaRegexToPureRegex($expectedValue);
3891-
Assert::assertRegExp($expectedValue, $actualValue, 'wrong "remote-item-id" in the response');
3932+
Assert::assertMatchesRegularExpression($expectedValue, $actualValue, 'wrong "remote-item-id" in the response');
38923933
break;
38933934
default:
38943935
Assert::assertEquals($expectedValue, $actualValue, "wrong '$itemToFind' in the response");

0 commit comments

Comments
 (0)