Skip to content

Commit c79c13b

Browse files
committed
test: allow multiple xpaths
1 parent a4e0c92 commit c79c13b

File tree

1 file changed

+56
-16
lines changed

1 file changed

+56
-16
lines changed

tests/acceptance/bootstrap/SpacesContext.php

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3795,10 +3795,9 @@ public function sendPropfindRequestToSpace(string $user, string $spaceName, ?str
37953795
}
37963796

37973797
/**
3798-
* @Then /^as user "([^"]*)" the (PROPFIND|REPORT) response should contain a (resource|space) "([^"]*)" with these key and value pairs:$/
3798+
* @Then /^as user "([^"]*)" the (?:PROPFIND|REPORT) response should contain a (resource|space) "([^"]*)" with these key and value pairs:$/
37993799
*
38003800
* @param string $user
3801-
* @param string $method # method should be either PROPFIND or REPORT
38023801
* @param string $type # type should be either resource or space
38033802
* @param string $resource
38043803
* @param TableNode $table
@@ -3807,26 +3806,29 @@ public function sendPropfindRequestToSpace(string $user, string $spaceName, ?str
38073806
* @throws GuzzleException
38083807
* @throws JsonException
38093808
*/
3810-
public function asUsertheXMLResponseShouldContainMountpointWithTheseKeyAndValuePair(string $user, string $method, string $type, string $resource, TableNode $table): void {
3809+
public function asUsertheXMLResponseShouldContainMountpointWithTheseKeyAndValuePair(string $user, string $type, string $resource, TableNode $table): void {
38113810
$this->featureContext->verifyTableNodeColumns($table, ['key', 'value']);
38123811
if ($this->featureContext->getDavPathVersion() === WebDavHelper::DAV_VERSION_SPACES && $type === 'space') {
38133812
$space = $this->getSpaceByName($user, $resource);
38143813
$resource = $space['id'];
3814+
} elseif (\preg_match(GraphHelper::jsonSchemaRegexToPureRegex(GraphHelper::getFileIdRegex()), $resource)) {
3815+
// When using file-id, some characters need to be encoded
3816+
$resource = \str_replace("!", "%21", $resource);
38153817
} else {
38163818
$resource = \rawurlencode($resource);
38173819
}
3818-
$this->theXMLResponseShouldContain($resource, $table);
3820+
$this->theXMLResponseShouldContain($resource, $table->getHash());
38193821
}
38203822

38213823
/**
3822-
* @param string $resource
3823-
* @param TableNode $table
3824+
* @param string $resource // can be resource name, space id or file id
3825+
* @param array $properties // ["key" => "value"]
38243826
*
38253827
* @return void
38263828
* @throws GuzzleException
38273829
* @throws JsonException
38283830
*/
3829-
public function theXMLResponseShouldContain(string $resource, TableNode $table): void {
3831+
public function theXMLResponseShouldContain(string $resource, array $properties): void {
38303832
$xmlResponse = $this->featureContext->getResponseXml();
38313833
$hrefs = array_map(fn ($href) => $href->__toString(), $xmlResponse->xpath("//d:response/d:href"));
38323834

@@ -3838,29 +3840,67 @@ public function theXMLResponseShouldContain(string $resource, TableNode $table):
38383840
}
38393841
}
38403842

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

38493889
$actualValue = $foundXmlItem[0]->__toString();
3850-
$expectedValue = $this->featureContext->substituteInLineCodes($row['value']);
3890+
$expectedValue = $this->featureContext->substituteInLineCodes($property['value']);
38513891

38523892
switch ($itemToFind) {
38533893
case "oc:fileid":
38543894
$expectedValue = GraphHelper::jsonSchemaRegexToPureRegex($expectedValue);
3855-
Assert::assertRegExp($expectedValue, $actualValue, 'wrong "fileid" in the response');
3895+
Assert::assertMatchesRegularExpression($expectedValue, $actualValue, 'wrong "fileid" in the response');
38563896
break;
38573897
case "oc:file-parent":
38583898
$expectedValue = GraphHelper::jsonSchemaRegexToPureRegex($expectedValue);
3859-
Assert::assertRegExp($expectedValue, $actualValue, 'wrong "file-parent" in the response');
3899+
Assert::assertMatchesRegularExpression($expectedValue, $actualValue, 'wrong "file-parent" in the response');
38603900
break;
38613901
case "oc:privatelink":
38623902
$expectedValue = GraphHelper::jsonSchemaRegexToPureRegex($expectedValue);
3863-
Assert::assertRegExp($expectedValue, $actualValue, 'wrong "privatelink" in the response');
3903+
Assert::assertMatchesRegularExpression($expectedValue, $actualValue, 'wrong "privatelink" in the response');
38643904
break;
38653905
case "oc:tags":
38663906
// The value should be a comma-separated string of tag names.
@@ -3888,7 +3928,7 @@ public function theXMLResponseShouldContain(string $resource, TableNode $table):
38883928
break;
38893929
case "oc:remote-item-id":
38903930
$expectedValue = GraphHelper::jsonSchemaRegexToPureRegex($expectedValue);
3891-
Assert::assertRegExp($expectedValue, $actualValue, 'wrong "remote-item-id" in the response');
3931+
Assert::assertMatchesRegularExpression($expectedValue, $actualValue, 'wrong "remote-item-id" in the response');
38923932
break;
38933933
default:
38943934
Assert::assertEquals($expectedValue, $actualValue, "wrong '$itemToFind' in the response");

0 commit comments

Comments
 (0)