diff --git a/tests/acceptance/TestHelpers/Asserts/WebDav.php b/tests/acceptance/TestHelpers/Asserts/WebDav.php index 8b72da2a2..155d5ff1c 100644 --- a/tests/acceptance/TestHelpers/Asserts/WebDav.php +++ b/tests/acceptance/TestHelpers/Asserts/WebDav.php @@ -32,7 +32,7 @@ class WebDav extends Assert { * * @param string|null $element exception|message|reason * @param string|null $expectedValue - * @param array|null $responseXml + * @param array|null $responseXmlArray * @param string|null $extraErrorText * * @return void @@ -40,7 +40,7 @@ class WebDav extends Assert { public static function assertDavResponseElementIs( ?string $element, ?string $expectedValue, - ?array $responseXml, + ?array $responseXmlArray, ?string $extraErrorText = '' ):void { if ($extraErrorText !== '') { @@ -48,15 +48,15 @@ class WebDav extends Assert { } self::assertArrayHasKey( 'value', - $responseXml, + $responseXmlArray, $extraErrorText . "responseXml does not have key 'value'" ); if ($element === "exception") { - $result = $responseXml['value'][0]['value']; + $result = $responseXmlArray['value'][0]['value']; } elseif ($element === "message") { - $result = $responseXml['value'][1]['value']; + $result = $responseXmlArray['value'][1]['value']; } elseif ($element === "reason") { - $result = $responseXml['value'][3]['value']; + $result = $responseXmlArray['value'][3]['value']; } else { self::fail(__METHOD__ . " element must be one of exception, response or reason. But '$element' was passed in."); } diff --git a/tests/acceptance/bootstrap/CapabilitiesContext.php b/tests/acceptance/bootstrap/CapabilitiesContext.php index 446f74461..739c9dee3 100644 --- a/tests/acceptance/bootstrap/CapabilitiesContext.php +++ b/tests/acceptance/bootstrap/CapabilitiesContext.php @@ -29,6 +29,7 @@ use Psr\Http\Message\ResponseInterface; use PHPUnit\Framework\Assert; use TestHelpers\OcsApiHelper; use TestHelpers\BehatHelper; +use TestHelpers\HttpRequestHelper; require_once 'bootstrap.php'; @@ -213,9 +214,10 @@ class CapabilitiesContext implements Context { $response = $this->userGetsCapabilities($this->getAdminUsernameForCapabilitiesCheck()); $this->featureContext->theHTTPStatusCodeShouldBe(200, '', $response); - $responseXml = $this->featureContext->getResponseXml($response)->data->capabilities; + + $responseXmlObject = HttpRequestHelper::getResponseXml($response, __METHOD__)->data->capabilities; $edition = $this->getParameterValueFromXml( - $responseXml, + $responseXmlObject, 'core', 'status@@@edition' ); @@ -227,7 +229,7 @@ class CapabilitiesContext implements Context { } $product = $this->getParameterValueFromXml( - $responseXml, + $responseXmlObject, 'core', 'status@@@product' ); @@ -238,7 +240,7 @@ class CapabilitiesContext implements Context { } $productName = $this->getParameterValueFromXml( - $responseXml, + $responseXmlObject, 'core', 'status@@@productname' ); @@ -256,7 +258,7 @@ class CapabilitiesContext implements Context { // We are on oCIS or reva or some other implementation. We cannot do "occ status". // So get the expected version values by looking in the capabilities response. $version = $this->getParameterValueFromXml( - $responseXml, + $responseXmlObject, 'core', 'status@@@version' ); @@ -268,7 +270,7 @@ class CapabilitiesContext implements Context { } $versionString = $this->getParameterValueFromXml( - $responseXml, + $responseXmlObject, 'core', 'status@@@versionstring' ); @@ -322,7 +324,7 @@ class CapabilitiesContext implements Context { Assert::assertStringStartsWith( $majorMinorPatchVersion, $versionString, - "versionstring should start with $majorMinorPatchVersion but is $versionString" + "version string should start with $majorMinorPatchVersion but is $versionString" ); } } diff --git a/tests/acceptance/bootstrap/CollaborationContext.php b/tests/acceptance/bootstrap/CollaborationContext.php index bf44f9660..ce080fe22 100644 --- a/tests/acceptance/bootstrap/CollaborationContext.php +++ b/tests/acceptance/bootstrap/CollaborationContext.php @@ -148,23 +148,23 @@ class CollaborationContext implements Context { * * @return void * @throws GuzzleException + * @throws Exception */ public function createFile(string $file, string $password, string $folder = ""): void { $token = $this->featureContext->shareNgGetLastCreatedLinkShareToken(); $baseUrl = $this->featureContext->getBaseUrl(); $davPath = WebDavHelper::getDavPath(WebDavHelper::DAV_VERSION_NEW, $token, "public-files"); - $response = HttpRequestHelper::sendRequest( - "$baseUrl/$davPath/$folder", - $this->featureContext->getStepLineRef(), - "PROPFIND", - "public", - $this->featureContext->getActualPassword($password) + + $responseXmlObject = HttpRequestHelper::getResponseXml( + HttpRequestHelper::sendRequest( + "$baseUrl/$davPath/$folder", + $this->featureContext->getStepLineRef(), + "PROPFIND", + "public", + $this->featureContext->getActualPassword($password) + ) ); - $responseXml = HttpRequestHelper::getResponseXml( - $response, - __METHOD__ - ); - $xmlPart = $responseXml->xpath("//d:prop/oc:fileid"); + $xmlPart = $responseXmlObject->xpath("//d:prop/oc:fileid"); $parentContainerId = (string) $xmlPart[0]; $headers = [ diff --git a/tests/acceptance/bootstrap/FeatureContext.php b/tests/acceptance/bootstrap/FeatureContext.php index e1807e0d0..5e6a7912c 100644 --- a/tests/acceptance/bootstrap/FeatureContext.php +++ b/tests/acceptance/bootstrap/FeatureContext.php @@ -54,7 +54,6 @@ use Swaggest\JsonSchema\Exception\NumericException; use Swaggest\JsonSchema\Exception\ObjectException; use Swaggest\JsonSchema\Exception\StringException; use Swaggest\JsonSchema\Exception\TypeException; -use Swaggest\JsonSchema\Exception\Error as JsonSchemaError; require_once 'bootstrap.php'; @@ -998,11 +997,6 @@ class FeatureContext extends BehatVariablesContext { string $username = "" ): void { $this->response = $response; - //after a new response reset the response xml - $this->responseXml = []; - //after a new response reset the response xml object - $this->responseXmlObject = null; - // remember the user that received the response $this->responseUser = $username; } @@ -1032,26 +1026,6 @@ class FeatureContext extends BehatVariablesContext { return $previousServer; } - /** - * Parses the response as XML - * - * @param ResponseInterface|null $response - * @param string|null $exceptionText text to put at the front of exception messages - * - * @return SimpleXMLElement - * @throws Exception - */ - public function getResponseXml(?ResponseInterface $response = null, ?string $exceptionText = ''): SimpleXMLElement { - if ($response === null) { - $response = $this->response; - } - - if ($exceptionText === '') { - $exceptionText = __METHOD__; - } - return HttpRequestHelper::getResponseXml($response, $exceptionText); - } - /** * @param JsonSchema $schemaObj * @@ -2611,30 +2585,6 @@ class FeatureContext extends BehatVariablesContext { return $this->acceptanceTestsDirLocation() . $this->temporaryStorageSubfolderName() . "/"; } - /** - * Parse list of config keys from the given XML response - * - * @param SimpleXMLElement $responseXml - * - * @return array - */ - public function parseConfigListFromResponseXml(SimpleXMLElement $responseXml): array { - $configkeyData = \json_decode(\json_encode($responseXml->data), true); - if (isset($configkeyData['element'])) { - $configkeyData = $configkeyData['element']; - } else { - // There are no keys for the app - return []; - } - if (isset($configkeyData[0])) { - $configkeyValues = $configkeyData; - } else { - // There is just 1 key for the app - $configkeyValues[0] = $configkeyData; - } - return $configkeyValues; - } - /** * This will run before EVERY scenario. * It will set the properties for this object. diff --git a/tests/acceptance/bootstrap/FilesVersionsContext.php b/tests/acceptance/bootstrap/FilesVersionsContext.php index 7507ad78e..abe787215 100644 --- a/tests/acceptance/bootstrap/FilesVersionsContext.php +++ b/tests/acceptance/bootstrap/FilesVersionsContext.php @@ -219,11 +219,11 @@ class FilesVersionsContext implements Context { $fileId = $this->featureContext->getFileIdForPath($user, $path); Assert::assertNotNull($fileId, __METHOD__ . " fileid of file $path user $user not found (the file may not exist)"); $response = $this->listVersionFolder($user, $fileId, 1); - $responseXml = HttpRequestHelper::getResponseXml( + $responseXmlObject = HttpRequestHelper::getResponseXml( $response, __METHOD__ ); - $xmlPart = $responseXml->xpath("//d:response/d:href"); + $xmlPart = $responseXmlObject->xpath("//d:response/d:href"); //restoring the version only works with DAV path v2 $destinationUrl = $this->featureContext->getBaseUrl() . "/" . WebDavHelper::getDavPath(WebDavHelper::DAV_VERSION_NEW, $user) . \trim($path, "/"); @@ -281,18 +281,18 @@ class FilesVersionsContext implements Context { */ public function assertFileVersionsCount(string $user, string $fileId, int $expectedCount):void { $response = $this->listVersionFolder($user, $fileId, 1); - $responseXml = HttpRequestHelper::getResponseXml( + $responseXmlObject = HttpRequestHelper::getResponseXml( $response, __METHOD__ ); - $actualCount = \count($responseXml->xpath("//d:prop/d:getetag")) - 1; + $actualCount = \count($responseXmlObject->xpath("//d:prop/d:getetag")) - 1; if ($actualCount === -1) { $actualCount = 0; } Assert::assertEquals( $expectedCount, $actualCount, - "Expected $expectedCount versions but found $actualCount in \n" . $responseXml->asXML() + "Expected $expectedCount versions but found $actualCount in \n" . $responseXmlObject->asXML() ); } @@ -356,11 +356,11 @@ class FilesVersionsContext implements Context { $fileId = $this->featureContext->getFileIdForPath($user, $path); Assert::assertNotNull($fileId, __METHOD__ . " fileid of file $path user $user not found (the file may not exist)"); $response = $this->listVersionFolder($user, $fileId, 1, ['d:getcontentlength']); - $responseXml = HttpRequestHelper::getResponseXml( + $responseXmlObject = HttpRequestHelper::getResponseXml( $response, __METHOD__ ); - $xmlPart = $responseXml->xpath("//d:prop/d:getcontentlength"); + $xmlPart = $responseXmlObject->xpath("//d:prop/d:getcontentlength"); Assert::assertEquals( $length, (int) $xmlPart[$index], @@ -422,8 +422,8 @@ class FilesVersionsContext implements Context { if ($response->getStatusCode() === 403) { return $response; } - $responseXml = new SimpleXMLElement($response->getBody()->getContents()); - $xmlPart = $responseXml->xpath("//d:response/d:href"); + $responseXmlObject = new SimpleXMLElement($response->getBody()->getContents()); + $xmlPart = $responseXmlObject->xpath("//d:response/d:href"); if (!isset($xmlPart[$index])) { Assert::fail( 'could not find version of path "' . $path . '" with index "' . $index . '"' @@ -519,11 +519,6 @@ class FilesVersionsContext implements Context { null ); $this->featureContext->setResponse($response); - $responseXml = HttpRequestHelper::getResponseXml( - $response, - __METHOD__ - ); - $this->featureContext->setResponseXmlObject($responseXml); } /** diff --git a/tests/acceptance/bootstrap/OCSContext.php b/tests/acceptance/bootstrap/OCSContext.php index c485ca19d..2a0ee2f0b 100644 --- a/tests/acceptance/bootstrap/OCSContext.php +++ b/tests/acceptance/bootstrap/OCSContext.php @@ -26,6 +26,7 @@ use Behat\Gherkin\Node\PyStringNode; use Behat\Gherkin\Node\TableNode; use Psr\Http\Message\ResponseInterface; use PHPUnit\Framework\Assert; +use TestHelpers\HttpRequestHelper; use TestHelpers\OcsApiHelper; use TestHelpers\TranslationHelper; use TestHelpers\BehatHelper; @@ -564,13 +565,11 @@ class OCSContext implements Context { return (string) $jsonResponse->ocs->meta->statuscode; } // go to xml response when json response is null (it means not formatted and get status code) - $responseXml = $this->featureContext->getResponseXml($response, __METHOD__); - if (isset($responseXml->meta[0], $responseXml->meta[0]->statuscode)) { - return (string) $responseXml->meta[0]->statuscode; + $responseXmlObject = HttpRequestHelper::getResponseXml($response, __METHOD__); + if (isset($responseXmlObject->meta[0], $responseXmlObject->meta[0]->statuscode)) { + return (string) $responseXmlObject->meta[0]->statuscode; } - throw new Exception( - "No OCS status code found in response" - ); + Assert::fail("No OCS status code found in response"); } /** @@ -582,13 +581,11 @@ class OCSContext implements Context { * @throws Exception */ public function getOCSResponseData(ResponseInterface $response): SimpleXMLElement { - $responseXml = $this->featureContext->getResponseXml($response, __METHOD__); - if (isset($responseXml->data)) { - return $responseXml->data; + $responseXmlObject = HttpRequestHelper::getResponseXml($response, __METHOD__); + if (isset($responseXmlObject->data)) { + return $responseXmlObject->data; } - throw new Exception( - "No OCS data items found in responseXml" - ); + Assert::fail("No OCS data items found in response"); } /** @@ -600,7 +597,7 @@ class OCSContext implements Context { * @return string */ public function getOCSResponseStatusMessage(ResponseInterface $response):string { - return (string) $this->featureContext->getResponseXml($response, __METHOD__)->meta[0]->message; + return (string) HttpRequestHelper::getResponseXml($response, __METHOD__)->meta[0]->message; } /** diff --git a/tests/acceptance/bootstrap/Provisioning.php b/tests/acceptance/bootstrap/Provisioning.php index 8fd01b396..487b6e2aa 100644 --- a/tests/acceptance/bootstrap/Provisioning.php +++ b/tests/acceptance/bootstrap/Provisioning.php @@ -1905,8 +1905,7 @@ trait Provisioning { * @throws Exception */ public function getArrayOfUsersResponded(ResponseInterface $resp):array { - $listCheckedElements - = $this->getResponseXml($resp, __METHOD__)->data[0]->users[0]->element; + $listCheckedElements = HttpRequestHelper::getResponseXml($resp, __METHOD__)->data[0]->users[0]->element; return \json_decode(\json_encode($listCheckedElements), true); } @@ -1920,7 +1919,7 @@ trait Provisioning { */ public function getArrayOfGroupsResponded(ResponseInterface $resp):array { $listCheckedElements - = $this->getResponseXml($resp, __METHOD__)->data[0]->groups[0]->element; + = HttpRequestHelper::getResponseXml($resp, __METHOD__)->data[0]->groups[0]->element; return \json_decode(\json_encode($listCheckedElements), true); } @@ -1933,8 +1932,7 @@ trait Provisioning { * @throws Exception */ public function getArrayOfAppsResponded(ResponseInterface $resp):array { - $listCheckedElements - = $this->getResponseXml($resp, __METHOD__)->data[0]->apps[0]->element; + $listCheckedElements = HttpRequestHelper::getResponseXml($resp, __METHOD__)->data[0]->apps[0]->element; return \json_decode(\json_encode($listCheckedElements), true); } @@ -1945,7 +1943,7 @@ trait Provisioning { * @throws Exception */ public function theApiShouldNotReturnAnyData():void { - $responseData = $this->getResponseXml(null, __METHOD__)->data[0]; + $responseData = HttpRequestHelper::getResponseXml($this->response, __METHOD__)->data[0]; Assert::assertEmpty( $responseData, "Response data is not empty but it should be empty" diff --git a/tests/acceptance/bootstrap/ShareesContext.php b/tests/acceptance/bootstrap/ShareesContext.php index 0bd6b3227..1a2048377 100644 --- a/tests/acceptance/bootstrap/ShareesContext.php +++ b/tests/acceptance/bootstrap/ShareesContext.php @@ -28,6 +28,7 @@ use Behat\Gherkin\Node\TableNode; use Psr\Http\Message\ResponseInterface; use PHPUnit\Framework\Assert; use TestHelpers\BehatHelper; +use TestHelpers\HttpRequestHelper; require_once 'bootstrap.php'; @@ -155,7 +156,7 @@ class ShareesContext implements Context { ResponseInterface $response, string $shareeType ):array { - $elements = $this->featureContext->getResponseXml($response, __METHOD__)->data; + $elements = HttpRequestHelper::getResponseXml($response, __METHOD__)->data; $elements = \json_decode(\json_encode($elements), true); if (\strpos($shareeType, 'exact ') === 0) { $elements = $elements['exact']; diff --git a/tests/acceptance/bootstrap/Sharing.php b/tests/acceptance/bootstrap/Sharing.php index 0191bc836..6d3091b5e 100755 --- a/tests/acceptance/bootstrap/Sharing.php +++ b/tests/acceptance/bootstrap/Sharing.php @@ -271,7 +271,7 @@ trait Sharing { * @throws Exception */ public function getServerShareTimeFromLastResponse():int { - $stime = $this->getResponseXml(null, __METHOD__)->xpath("//stime"); + $stime = HttpRequestHelper::getResponseXml($this->response, __METHOD__)->xpath("//stime"); if ($stime) { return (int) $stime[0]; } @@ -921,13 +921,13 @@ trait Sharing { if (($response->getStatusCode() === 200) && \in_array($this->ocsContext->getOCSResponseStatusCode($response), ['100', '200']) ) { - $xmlResponse = $this->getResponseXml($response); - if (isset($xmlResponse->data)) { - $shareData = $xmlResponse->data; + $responseXmlObject = HttpRequestHelper::getResponseXml($response); + if (isset($responseXmlObject->data)) { + $shareData = $responseXmlObject->data; if ($shareType === 'public_link') { $this->addToCreatedPublicShares($shareData); } else { - $sharer = (string) $xmlResponse->data->uid_owner; + $sharer = (string) $responseXmlObject->data->uid_owner; $this->addToCreatedUserGroupshares($sharer, $shareData); } } @@ -981,7 +981,7 @@ trait Sharing { */ public function isFieldInResponse(string $field, ?string $contentExpected, bool $expectSuccess = true, ?SimpleXMLElement $data = null):bool { if ($data === null) { - $data = $this->getResponseXml(null, __METHOD__)->data[0]; + $data = HttpRequestHelper::getResponseXml($this->response, __METHOD__)->data[0]; } Assert::assertIsObject($data, __METHOD__ . " data not found in response XML"); @@ -1053,7 +1053,7 @@ trait Sharing { * @return void */ public function checkNoFilesFoldersInResponse():void { - $data = $this->getResponseXml(null, __METHOD__)->data[0]; + $data = HttpRequestHelper::getResponseXml($this->response, __METHOD__)->data[0]; Assert::assertIsObject($data, __METHOD__ . " data not found in response XML"); Assert::assertCount(0, $data); } @@ -1067,7 +1067,7 @@ trait Sharing { */ public function checkCountFilesFoldersInResponse(string $count):void { $count = (int) $count; - $data = $this->getResponseXml(null, __METHOD__)->data[0]; + $data = HttpRequestHelper::getResponseXml($this->response, __METHOD__)->data[0]; Assert::assertIsObject($data, __METHOD__ . " data not found in response XML"); Assert::assertCount($count, $data, __METHOD__ . " the response does not contain $count entries"); } @@ -2212,7 +2212,7 @@ trait Sharing { * @return void */ public function responseShouldNotContainAnyShareIds(ResponseInterface $response):void { - $data = $this->getResponseXml($response, __METHOD__)->data[0]; + $data = HttpRequestHelper::getResponseXml($response, __METHOD__)->data[0]; $fieldIsSet = false; $receivedShareCount = 0; @@ -2275,7 +2275,7 @@ trait Sharing { * @return void */ public function checkingTheResponseEntriesCount(int $count):void { - $actualCount = \count($this->getResponseXml(null, __METHOD__)->data[0]); + $actualCount = \count(HttpRequestHelper::getResponseXml($this->response, __METHOD__)->data[0]); Assert::assertEquals( $count, $actualCount, @@ -2305,7 +2305,7 @@ trait Sharing { */ public function checkTheFields(string $user, ?TableNode $body, ?ResponseInterface $response = null):void { $response = $response ?? $this->getResponse(); - $data = $this->getResponseXml($response, __METHOD__)->data[0]; + $data = HttpRequestHelper::getResponseXml($response, __METHOD__)->data[0]; $this->verifyTableNodeColumnsCount($body, 2); $bodyRows = $body->getRowsHash(); $userRelatedFieldNames = [ @@ -2359,7 +2359,7 @@ trait Sharing { "space_id" ]; - $response = $this->getResponseXml(null, __METHOD__); + $response = HttpRequestHelper::getResponseXml($this->response, __METHOD__); $this->addToCreatedPublicShares($response->data); foreach ($bodyRows as $field => $value) { if (\in_array($field, $userRelatedFieldNames)) { @@ -2440,7 +2440,7 @@ trait Sharing { * @return void */ public function theFieldsOfTheLastResponseShouldBeEmpty():void { - $data = $this->getResponseXml(null, __METHOD__)->data[0]; + $data = HttpRequestHelper::getResponseXml($this->response, __METHOD__)->data[0]; Assert::assertEquals( \count($data->element), 0, @@ -2455,8 +2455,8 @@ trait Sharing { * @throws Exception */ public function getSharingAttributesFromLastResponse():string { - $responseXml = $this->getResponseXml(null, __METHOD__)->data[0]; - $actualAttributesElement = $responseXml->xpath('//attributes'); + $responseXmlObject = HttpRequestHelper::getResponseXml($this->response, __METHOD__)->data[0]; + $actualAttributesElement = $responseXmlObject->xpath('//attributes'); if ($actualAttributesElement) { $actualAttributes = (array) $actualAttributesElement[0]; @@ -2543,7 +2543,7 @@ trait Sharing { public function userDownloadsFailWithMessage(string $fileName, string $user, PyStringNode $errorMessage):void { $user = $this->getActualUsername($user); $response = $this->downloadFileAsUserUsingPassword($user, $fileName); - $receivedErrorMessage = $this->getResponseXml($response, __METHOD__)->xpath('//s:message'); + $receivedErrorMessage = HttpRequestHelper::getResponseXml($response, __METHOD__)->xpath('//s:message'); Assert::assertEquals( $errorMessage, (string) $receivedErrorMessage[0], @@ -2600,7 +2600,7 @@ trait Sharing { [], $this->ocsApiVersion ); - return $this->getResponseXml($response, __METHOD__)->data->element; + return HttpRequestHelper::getResponseXml($response, __METHOD__)->data->element; } /** diff --git a/tests/acceptance/bootstrap/SpacesContext.php b/tests/acceptance/bootstrap/SpacesContext.php index 45183408a..f83643cdf 100644 --- a/tests/acceptance/bootstrap/SpacesContext.php +++ b/tests/acceptance/bootstrap/SpacesContext.php @@ -129,29 +129,6 @@ class SpacesContext implements Context { $this->availableSpaces = $availableSpaces; } - /** - * response content parsed from XML to an array - * - * @var array - */ - private array $responseXml = []; - - /** - * @return array - */ - public function getResponseXml(): array { - return $this->responseXml; - } - - /** - * @param array $responseXml - * - * @return void - */ - public function setResponseXml(array $responseXml): void { - $this->responseXml = $responseXml; - } - /** * Get SpaceId by Name * @@ -369,8 +346,8 @@ class SpacesContext implements Context { ); $this->featureContext->theHttpStatusCodeShouldBe(207, '', $response); - $xmlResponse = $this->featureContext->getResponseXml($response); - $fileId = $xmlResponse->xpath("//d:response/d:propstat/d:prop/oc:fileid")[0]; + $responseXmlObject = HttpRequestHelper::getResponseXml($response, __METHOD__); + $fileId = $responseXmlObject->xpath("//d:response/d:propstat/d:prop/oc:fileid")[0]; return $fileId->__toString(); } @@ -396,7 +373,7 @@ class SpacesContext implements Context { "files", WebDavHelper::DAV_VERSION_SPACES ); - $responseArray = json_decode(json_encode($this->featureContext->getResponseXml($response)->xpath("//d:response/d:propstat/d:prop/oc:privatelink")), true, 512, JSON_THROW_ON_ERROR); + $responseArray = json_decode(json_encode(HttpRequestHelper::getResponseXml($response, __METHOD__)->xpath("//d:response/d:propstat/d:prop/oc:privatelink")), true, 512, JSON_THROW_ON_ERROR); Assert::assertNotEmpty($responseArray, "the PROPFIND response for $spaceName is empty"); return $responseArray[0][0]; } @@ -2424,9 +2401,9 @@ class SpacesContext implements Context { $body, $this->featureContext->getStepLineRef() ); - $responseXml = $this->featureContext->getResponseXml($response, __METHOD__); - $sharer = (string) $responseXml->data->uid_owner; - $this->featureContext->addToCreatedUserGroupshares($sharer, $responseXml->data); + $responseXmlObject = HttpRequestHelper::getResponseXml($response, __METHOD__); + $sharer = (string) $responseXmlObject->data->uid_owner; + $this->featureContext->addToCreatedUserGroupshares($sharer, $responseXmlObject->data); return $response; } @@ -2587,8 +2564,8 @@ class SpacesContext implements Context { $this->featureContext->getStepLineRef() ); - $responseXml = $this->featureContext->getResponseXml($response, __METHOD__); - $this->featureContext->addToCreatedPublicShares($responseXml->data); + $responseXmlObject = HttpRequestHelper::getResponseXml($response, __METHOD__); + $this->featureContext->addToCreatedPublicShares($responseXmlObject->data); return $response; } @@ -3101,7 +3078,7 @@ class SpacesContext implements Context { $response ); return $this->trashbinContext->getTrashbinContentFromResponseXml( - $this->featureContext->getResponseXml($response) + HttpRequestHelper::getResponseXml($response, __METHOD__) ); } @@ -3572,8 +3549,7 @@ class SpacesContext implements Context { $this->featureContext->getStepLineRef() ); - $responseXml = $this->featureContext->getResponseXml($response, __METHOD__); - $this->featureContext->addToCreatedPublicShares($responseXml->data); + $this->featureContext->addToCreatedPublicShares(HttpRequestHelper::getResponseXml($response, __METHOD__)->data); return $response; } @@ -3663,7 +3639,7 @@ class SpacesContext implements Context { ); $should = ($shouldOrNot !== "not"); - $responseArray = json_decode(json_encode($this->featureContext->getResponseXml($response)->data), true, 512, JSON_THROW_ON_ERROR); + $responseArray = json_decode(json_encode(HttpRequestHelper::getResponseXml($response, __METHOD__)->data), true, 512, JSON_THROW_ON_ERROR); if ($should) { Assert::assertNotEmpty($responseArray, __METHOD__ . ' Response should contain a link, but it is empty'); @@ -3837,7 +3813,7 @@ class SpacesContext implements Context { */ public function searchResultShouldContainSpace(string $user, string $spaceName): void { // get a response after a Report request (called in the core) - $responseArray = json_decode(json_encode($this->featureContext->getResponseXml()->xpath("//d:response/d:href")), true, 512, JSON_THROW_ON_ERROR); + $responseArray = json_decode(json_encode(HttpRequestHelper::getResponseXml($this->featureContext->getResponse())->xpath("//d:response/d:href")), true, 512, JSON_THROW_ON_ERROR); Assert::assertNotEmpty($responseArray, "search result is empty"); // for mountpoint, id looks a little different than for project space @@ -4024,16 +4000,16 @@ class SpacesContext implements Context { } /** - * @param string $resource // can be resource name, space id or file id - * @param array $properties // ["key" => "value"] + * @param string $resource // can be resource name, space id or file id + * @param array $properties // ["key" => "value"] * * @return void * @throws GuzzleException * @throws JsonException */ public function theXMLResponseShouldContain(string $resource, array $properties): void { - $xmlResponse = $this->featureContext->getResponseXml(); - $hrefs = array_map(fn ($href) => $href->__toString(), $xmlResponse->xpath("//d:response/d:href")); + $responseXmlObject = HttpRequestHelper::getResponseXml($this->featureContext->getResponse(), __METHOD__); + $hrefs = array_map(fn ($href) => $href->__toString(), $responseXmlObject->xpath("//d:response/d:href")); $currentHref = ''; foreach ($hrefs as $href) { @@ -4069,14 +4045,14 @@ class SpacesContext implements Context { // check every xpath foreach ($xpaths as $key => $path) { $xpath = "{$path}{$itemToFind}"; - $foundXmlItem = $xmlResponse->xpath($xpath); + $foundXmlItem = $responseXmlObject->xpath($xpath); $xpaths[$key] = $xpath; if (\count($foundXmlItem)) { break; } } } else { - $foundXmlItem = $xmlResponse->xpath($xpath); + $foundXmlItem = $responseXmlObject->xpath($xpath); $xpaths[] = $xpath; } @@ -4086,7 +4062,7 @@ class SpacesContext implements Context { "Using xpaths:\n\t- " . \join("\n\t- ", $xpaths) . "\n" . "Could not find '<$itemToFind>' element in the XML response\n\t" - . "'" . \trim($xmlResponse->asXML()) . "'" + . "'" . \trim($responseXmlObject->asXML()) . "'" ); $actualValue = $foundXmlItem[0]->__toString(); @@ -4151,8 +4127,8 @@ class SpacesContext implements Context { * @throws GuzzleException */ public function asUserTheKeyFromPropfindResponseShouldMatchWithSharedwithmeDriveitemidOfShare(string $user, string $key, string $resource): void { - $xmlResponse = $this->featureContext->getResponseXml(); - $fileId = $xmlResponse->xpath("//oc:name[text()='$resource']/preceding-sibling::$key")[0]->__toString(); + $responseXmlObject = HttpRequestHelper::getResponseXml($this->featureContext->getResponse(), __METHOD__); + $fileId = $responseXmlObject->xpath("//oc:name[text()='$resource']/preceding-sibling::$key")[0]->__toString(); $jsonResponse = GraphHelper::getSharesSharedWithMe( $this->featureContext->getBaseUrl(), @@ -4179,25 +4155,30 @@ class SpacesContext implements Context { * @param string $resource * * @return void - * @throws GuzzleException|JsonException + * @throws GuzzleException + * @throws Exception */ public function publicDownloadsTheFolderFromTheLastCreatedPublicLink(string $resource) { $token = ($this->featureContext->isUsingSharingNG()) ? $this->featureContext->shareNgGetLastCreatedLinkShareToken() : $this->featureContext->getLastCreatedPublicShareToken(); - $response = $this->featureContext->listFolderAndReturnResponseXml( - $token, - $resource, - '0', - ['oc:fileid'], - "public-files" + + $responseXmlObject = HttpRequestHelper::getResponseXml( + $this->featureContext->listFolder( + $token, + $resource, + '0', + ['oc:fileid'], + null, + "public-files" + ) ); - $resourceId = json_decode(json_encode($response->xpath("//d:response/d:propstat/d:prop/oc:fileid")), true, 512, JSON_THROW_ON_ERROR); + $resourceId = $responseXmlObject->xpath("//d:response/d:propstat/d:prop/oc:fileid"); $queryString = 'public-token=' . $token . '&id=' . $resourceId[0][0]; $this->featureContext->setResponse( HttpRequestHelper::get( $this->archiverContext->getArchiverUrl($queryString), $this->featureContext->getStepLineRef(), '', - '', + '' ) ); } @@ -4218,11 +4199,9 @@ class SpacesContext implements Context { $quotaAmount, "Expected relative quota amount to be $quotaAmount but found to be $data->quota->relative" ); - } else { - throw new Exception( - "No relative quota amount found in responseXml" - ); + return; } + Assert::fail("No relative quota amount found in response"); } /** diff --git a/tests/acceptance/bootstrap/TrashbinContext.php b/tests/acceptance/bootstrap/TrashbinContext.php index 7dfdc0dbc..cb657ff64 100644 --- a/tests/acceptance/bootstrap/TrashbinContext.php +++ b/tests/acceptance/bootstrap/TrashbinContext.php @@ -85,12 +85,12 @@ class TrashbinContext implements Context { /** * Get files list from the response from trashbin api * - * @param SimpleXMLElement|null $responseXml + * @param SimpleXMLElement|null $responseXmlObject * * @return array */ - public function getTrashbinContentFromResponseXml(?SimpleXMLElement $responseXml): array { - $xmlElements = $responseXml->xpath('//d:response'); + public function getTrashbinContentFromResponseXml(?SimpleXMLElement $responseXmlObject): array { + $xmlElements = $responseXmlObject->xpath('//d:response'); $files = \array_map( static function (SimpleXMLElement $element) { $href = $element->xpath('./d:href')[0]; @@ -178,13 +178,13 @@ class TrashbinContext implements Context { $davPathVersion ); $this->featureContext->setResponse($response); - $responseXml = HttpRequestHelper::getResponseXml( - $response, - __METHOD__ - ); - $this->featureContext->setResponseXmlObject($responseXml); - $files = $this->getTrashbinContentFromResponseXml($responseXml); + $files = $this->getTrashbinContentFromResponseXml( + HttpRequestHelper::getResponseXml( + $response, + __METHOD__ + ) + ); // filter root element $files = \array_filter( $files, @@ -253,13 +253,13 @@ class TrashbinContext implements Context { $respBody = $response->getBody()->getContents(); Assert::assertEquals("207", $statusCode, "Expected status code to be '207' but got $statusCode \nResponse\n$respBody"); - $responseXml = HttpRequestHelper::getResponseXml( - $response, - __METHOD__ . " $collectionPath" + $files = $this->getTrashbinContentFromResponseXml( + HttpRequestHelper::getResponseXml( + $response, + __METHOD__ . " $collectionPath" + ) ); - $files = $this->getTrashbinContentFromResponseXml($responseXml); - $suffixPath = $user; if ($davPathVersion === WebDavHelper::DAV_VERSION_SPACES) { $suffixPath = WebDavHelper::getPersonalSpaceIdForUser( @@ -345,8 +345,7 @@ class TrashbinContext implements Context { */ public function theTrashbinDavResponseShouldNotContainTheseNodes(TableNode $table):void { $this->featureContext->verifyTableNodeColumns($table, ['name']); - $responseXml = $this->featureContext->getResponseXml(); - $files = $this->getTrashbinContentFromResponseXml($responseXml); + $files = $this->getTrashbinContentFromResponseXml(HttpRequestHelper::getResponseXml($this->featureContext->getResponse(), __METHOD__)); foreach ($table->getHash() as $row) { $path = trim((string)$row['name'], "/"); @@ -368,9 +367,8 @@ class TrashbinContext implements Context { */ public function theTrashbinDavResponseShouldContainTheseNodes(TableNode $table):void { $this->featureContext->verifyTableNodeColumns($table, ['name']); - $responseXml = $this->featureContext->getResponseXml(); - $files = $this->getTrashbinContentFromResponseXml($responseXml); + $files = $this->getTrashbinContentFromResponseXml(HttpRequestHelper::getResponseXml($this->featureContext->getResponse(), __METHOD__)); foreach ($table->getHash() as $row) { $path = trim($row['name'], "/"); @@ -460,7 +458,7 @@ class TrashbinContext implements Context { * @return void */ public function theLastWebdavResponseShouldContainFollowingElements(TableNode $elements):void { - $files = $this->getTrashbinContentFromResponseXml($this->featureContext->getResponseXml()); + $files = $this->getTrashbinContentFromResponseXml(HttpRequestHelper::getResponseXml($response, __METHOD__)); $elementRows = $elements->getHash(); foreach ($elementRows as $expectedElement) { $found = false; @@ -484,7 +482,7 @@ class TrashbinContext implements Context { * @throws Exception */ public function theLastWebdavResponseShouldNotContainFollowingElements(TableNode $elements):void { - $files = $this->getTrashbinContentFromResponseXml($this->featureContext->getResponseXml()); + $files = $this->getTrashbinContentFromResponseXml(HttpRequestHelper::getResponseXml($this->featureContext->getResponse())); // 'user' is also allowed in the table even though it is not used anywhere // This for better readability in feature files @@ -869,44 +867,29 @@ class TrashbinContext implements Context { * @param $user string * @param $originalPath string * - * @return ResponseInterface + * @return void * @throws Exception */ - public function userRestoresResourceWithOriginalPathWithoutSpecifyingDestinationUsingTrashbinApi(string $user, string $originalPath):ResponseInterface { - $asUser = $asUser ?? $user; + public function userRestoresResourceWithOriginalPathWithoutSpecifyingDestinationUsingTrashbinApi(string $user, string $originalPath):void { $listing = $this->listTrashbinFolder($user); $originalPath = \trim($originalPath, '/'); foreach ($listing as $entry) { if ($entry['original-location'] === $originalPath) { $trashItemHRef = $this->convertTrashbinHref($entry['href']); - $response = $this->featureContext->makeDavRequest( - $asUser, - 'MOVE', - $trashItemHRef, - [], - null, - null, - 'trash-bin' + $this->featureContext->setResponse( + $this->featureContext->makeDavRequest( + $user, + 'MOVE', + $trashItemHRef, + [], + null, + null, + 'trash-bin' + ) ); - $this->featureContext->setResponse($response); - // this gives empty response in ocis - try { - $responseXml = HttpRequestHelper::getResponseXml( - $response, - __METHOD__ - ); - $this->featureContext->setResponseXmlObject($responseXml); - } catch (Exception $e) { - } - - return $response; } } - throw new \Exception( - __METHOD__ - . " cannot restore from trashbin because no element was found for user $user at original path $originalPath" - ); } /** @@ -1150,7 +1133,7 @@ class TrashbinContext implements Context { */ public function theDeletedFileFolderShouldHaveCorrectDeletionMtimeInTheResponse(string $resource):void { $files = $this->getTrashbinContentFromResponseXml( - $this->featureContext->getResponseXml() + HttpRequestHelper::getResponseXml($this->featureContext->getResponse()) ); $found = false; diff --git a/tests/acceptance/bootstrap/WebDav.php b/tests/acceptance/bootstrap/WebDav.php index 62459d44d..26941fe10 100644 --- a/tests/acceptance/bootstrap/WebDav.php +++ b/tests/acceptance/bootstrap/WebDav.php @@ -53,11 +53,6 @@ trait WebDav { private ?int $lastUploadDeleteTime = null; - /** - * response content parsed from XML to an array - */ - private array $responseXml = []; - /** * add resource created by admin in an array * This array is used while cleaning up the resource created by admin during test run @@ -140,22 +135,6 @@ trait WebDav { return $this->lastUploadDeleteTime; } - /** - * @param SimpleXMLElement $responseXmlObject - * - * @return void - */ - public function setResponseXmlObject(SimpleXMLElement $responseXmlObject):void { - $this->responseXmlObject = $responseXmlObject; - } - - /** - * @return void - */ - public function clearResponseXmlObject():void { - $this->responseXmlObject = null; - } - /** * @param string $fileID * @@ -178,7 +157,7 @@ trait WebDav { * @return string the etag or an empty string if the getetag property does not exist */ public function getEtagFromResponseXmlObject(?SimpleXMLElement $xmlObject = null): string { - $xmlObject = $xmlObject ?? $this->getResponseXml(); + $xmlObject = $xmlObject ?? HttpRequestHelper::getResponseXml($this->getResponse()); $xmlPart = $xmlObject->xpath("//d:prop/d:getetag"); if (!\is_array($xmlPart) || (\count($xmlPart) === 0)) { return ''; @@ -201,15 +180,6 @@ trait WebDav { } } - /** - * @param array $responseXml - * - * @return void - */ - public function setResponseXml(array $responseXml):void { - $this->responseXml = $responseXml; - } - /** * @Given /^using (old|new|spaces) DAV path$/ * @@ -444,15 +414,8 @@ trait WebDav { * @throws Exception */ public function theNumberOfVersionsShouldBe(int $number):void { - $resXml = $this->getResponseXml(); - if ($resXml === null) { - $resXml = HttpRequestHelper::getResponseXml( - $this->getResponse(), - __METHOD__ - ); - $this->setResponseXmlObject($resXml); - } - $xmlPart = $resXml->xpath("//d:getlastmodified"); + $responseXmlObject = HttpRequestHelper::getResponseXml($this->getResponse(), __METHOD__); + $xmlPart = $responseXmlObject->xpath("//d:getlastmodified"); $actualNumber = \count($xmlPart); Assert::assertEquals( $number, @@ -470,14 +433,8 @@ trait WebDav { * @throws Exception */ public function theNumberOfEtagElementInTheResponseShouldBe(int $number):void { - $resXml = $this->getResponseXml(); - if ($resXml === null) { - $resXml = HttpRequestHelper::getResponseXml( - $this->getResponse(), - __METHOD__ - ); - } - $xmlPart = $resXml->xpath("//d:getetag"); + $responseXmlObject = HttpRequestHelper::getResponseXml($this->getResponse()); + $xmlPart = $responseXmlObject->xpath("//d:getetag"); $actualNumber = \count($xmlPart); Assert::assertEquals( $number, @@ -974,8 +931,7 @@ trait WebDav { * @return void */ public function contentOfFileForUserShouldBe(string $fileName, string $user, string $content):void { - $user = $this->getActualUsername($user); - $response = $this->downloadFileAsUserUsingPassword($user, $fileName); + $response = $this->downloadFileAsUserUsingPassword($this->getActualUsername($user), $fileName); $actualStatus = $response->getStatusCode(); if ($actualStatus !== 200) { throw new Exception( @@ -1159,11 +1115,11 @@ trait WebDav { * @throws Exception */ public function theSizeOfTheFileShouldBe(string $size):void { - $responseXml = HttpRequestHelper::getResponseXml( + $responseXmlObject = HttpRequestHelper::getResponseXml( $this->response, __METHOD__ ); - $xmlPart = $responseXml->xpath("//d:prop/d:getcontentlength"); + $xmlPart = $responseXmlObject->xpath("//d:prop/d:getcontentlength"); $actualSize = (string) $xmlPart[0]; Assert::assertEquals( $size, @@ -1277,7 +1233,7 @@ trait WebDav { $statusCode = $response->getStatusCode(); if ($statusCode < 400 || $statusCode > 499) { try { - $responseXml = HttpRequestHelper::getResponseXml( + $responseXmlObject = HttpRequestHelper::getResponseXml( $response, __METHOD__ ); @@ -1287,10 +1243,10 @@ trait WebDav { ); } Assert::assertTrue( - $this->isEtagValid($this->getEtagFromResponseXmlObject($responseXml)), + $this->isEtagValid($this->getEtagFromResponseXmlObject($responseXmlObject)), "$entry '$path' should not exist. But API returned $statusCode without an etag in the body" ); - $isCollection = $responseXml->xpath("//d:prop/d:resourcetype/d:collection"); + $isCollection = $responseXmlObject->xpath("//d:prop/d:resourcetype/d:collection"); if (\count($isCollection) === 0) { $actualResourceType = "file"; } else { @@ -1354,6 +1310,7 @@ trait WebDav { * @param string|null $type * * @return void + * @throws Exception */ public function checkFileOrFolderExistsForUser( string $user, @@ -1363,18 +1320,22 @@ trait WebDav { ):void { $user = $this->getActualUsername($user); $path = $this->substituteInLineCodes($path); - $responseXml = $this->listFolderAndReturnResponseXml( - $user, - $path, - '0', - null, - $type + $responseXmlObject = HttpRequestHelper::getResponseXml( + $this->listFolder( + $user, + $path, + '0', + null, + null, + $type + ) ); + Assert::assertTrue( - $this->isEtagValid($this->getEtagFromResponseXmlObject($responseXml)), + $this->isEtagValid($this->getEtagFromResponseXmlObject($responseXmlObject)), "$entry '$path' expected to exist for user $user but not found" ); - $isCollection = $responseXml->xpath("//d:prop/d:resourcetype/d:collection"); + $isCollection = $responseXmlObject->xpath("//d:prop/d:resourcetype/d:collection"); if ($entry === "folder") { Assert::assertEquals(\count($isCollection), 1, "Unexpectedly, `$path` is not a folder"); } elseif ($entry === "file") { @@ -1463,37 +1424,6 @@ trait WebDav { ); } - /** - * - * @param string $user - * @param string $path - * @param string $folderDepth requires 1 to see elements without children - * @param array|null $properties - * @param string $type - * - * @return SimpleXMLElement - * @throws Exception - */ - public function listFolderAndReturnResponseXml( - string $user, - string $path, - string $folderDepth, - ?array $properties = null, - string $type = "files" - ):SimpleXMLElement { - return HttpRequestHelper::getResponseXml( - $this->listFolder( - $user, - $path, - $folderDepth, - $properties, - null, - $type - ), - __METHOD__ - ); - } - /** * @Then /^user "([^"]*)" should (not|)\s?see the following elements$/ * @@ -1534,10 +1464,12 @@ trait WebDav { if ($this->davPropfindDepthInfinityIsEnabled()) { // get a full "infinite" list of the user's root folder in one request // and process that to check the elements (resources) - $responseXmlObject = $this->listFolderAndReturnResponseXml( - $user, - "/", - "infinity" + $responseXmlObject = HttpRequestHelper::getResponseXml( + $this->listFolder( + $user, + "/", + "infinity" + ) ); foreach ($elementsSimplified as $expectedElement) { // Allow the table of expected elements to have entries that do @@ -1575,10 +1507,12 @@ trait WebDav { // /some-folder%20with%20spaces/sub-folder // So we need both $elementToRequest and $expectedElement $expectedElement = $this->encodePath($elementToRequest); - $responseXmlObject = $this->listFolderAndReturnResponseXml( - $user, - $elementToRequest, - "1" + $responseXmlObject = HttpRequestHelper::getResponseXml( + $this->listFolder( + $user, + $elementToRequest, + '1' + ) ); $webdavPath = "/" . $this->getFullDavFilesPath($user) . $expectedElement; @@ -1659,11 +1593,8 @@ trait WebDav { ):void { $response = $this->uploadFile($user, $source, $destination); $this->setResponse($response); - $this->setResponseXml( - HttpRequestHelper::parseResponseAsXml($response) - ); $this->pushToLastHttpStatusCodesArray( - (string) $this->getResponse()->getStatusCode() + (string) $response->getStatusCode() ); } @@ -1710,7 +1641,6 @@ trait WebDav { $chunkingVersion = null; } try { - $this->responseXml = []; $this->pauseUploadDelete(); $this->response = UploadHelper::upload( $this->getBaseUrl(), @@ -3571,6 +3501,7 @@ trait WebDav { * @param boolean $checkEachDelete * * @return void + * @throws Exception */ public function deleteEverythingInFolder( string $user, @@ -3578,10 +3509,12 @@ trait WebDav { bool $checkEachDelete = false ):void { $user = $this->getActualUsername($user); - $responseXmlObject = $this->listFolderAndReturnResponseXml( - $user, - $folder, - '1' + $responseXmlObject = HttpRequestHelper::getResponseXml( + $this->listFolder( + $user, + $folder, + '1' + ) ); $elementList = $responseXmlObject->xpath("//d:response/d:href"); if (\is_array($elementList) && \count($elementList)) { @@ -3953,9 +3886,8 @@ trait WebDav { */ public function theDownloadedPreviewContentShouldMatchWithFixturesPreviewContentFor(string $filename):void { $expectedPreview = \file_get_contents(__DIR__ . "/../fixtures/" . $filename); - $response = $response ?? $this->getResponse(); - $response->getBody()->rewind(); - $responseBodyContent = $response->getBody()->getContents(); + $this->getResponse()->getBody()->rewind(); + $responseBodyContent = $this->getResponse()->getBody()->getContents(); Assert::assertEquals($expectedPreview, $responseBodyContent); } @@ -4081,11 +4013,11 @@ trait WebDav { * @throws Exception */ public function theDavElementShouldBe(string $element, string $message):void { - $resXmlArray = HttpRequestHelper::parseResponseAsXml($this->getResponse()); + $responseXmlArray = HttpRequestHelper::parseResponseAsXml($this->getResponse()); WebDavAssert::assertDavResponseElementIs( $element, $message, - $resXmlArray, + $responseXmlArray, __METHOD__ ); } @@ -4220,23 +4152,18 @@ trait WebDav { * @return void */ public function checkIFResponseContainsNumberEntries(int $numFiles):void { - //if we are using that step the second time in a scenario e.g. 'But ... should not' - //then don't parse the result again, because the result in a ResponseInterface - if (empty($this->responseXml)) { - $this->setResponseXml( - HttpRequestHelper::parseResponseAsXml($this->response) - ); - } + $responseXmlArray = HttpRequestHelper::parseResponseAsXml($this->response); + Assert::assertIsArray( - $this->responseXml, - __METHOD__ . " responseXml is not an array" + $responseXmlArray, + __METHOD__ . " is not an array" ); Assert::assertArrayHasKey( "value", - $this->responseXml, - __METHOD__ . " responseXml does not have key 'value'" + $responseXmlArray, + __METHOD__ . " does not have key 'value'" ); - $multistatusResults = $this->responseXml["value"]; + $multistatusResults = $responseXmlArray["value"]; if ($multistatusResults === null) { $multistatusResults = []; } @@ -4342,7 +4269,6 @@ trait WebDav { $depth ); $this->setResponse($response); - $this->setResponseXml(HttpRequestHelper::parseResponseAsXml($this->response)); } /** @@ -4429,16 +4355,16 @@ trait WebDav { */ public function thePublicListsTheResourcesInTheLastCreatedPublicLinkWithDepthUsingTheWebdavApi(string $depth):void { $token = ($this->isUsingSharingNG()) ? $this->shareNgGetLastCreatedLinkShareToken() : $this->getLastCreatedPublicShareToken(); - $response = $this->listFolder( - $token, - '/', - $depth, - null, - null, - "public-files" + $this->setResponse( + $this->listFolder( + $token, + '/', + $depth, + null, + null, + "public-files" + ) ); - $this->setResponse($response); - $this->setResponseXml(HttpRequestHelper::parseResponseAsXml($this->response)); } /** @@ -4447,7 +4373,7 @@ trait WebDav { * @return array */ public function findEntryFromReportResponse(?string $user):array { - $responseXmlObj = $this->getResponseXml(); + $responseXmlObj = HttpRequestHelper::getResponseXml($this->getResponse()); $responseResources = []; $hrefs = $responseXmlObj->xpath('//d:href'); foreach ($hrefs as $href) { @@ -4488,28 +4414,22 @@ trait WebDav { public function getMultiStatusResultFromPropfindResult( ?string $user = null ):array { - //if we are using that step the second time in a scenario e.g. 'But ... should not' - //then don't parse the result again, because the result in a ResponseInterface - if (empty($this->responseXml)) { - $this->setResponseXml( - HttpRequestHelper::parseResponseAsXml($this->response) - ); - } - Assert::assertNotEmpty($this->responseXml, __METHOD__ . ' Response is empty'); + $responseXmlArray = HttpRequestHelper::parseResponseAsXml($this->response); + + Assert::assertNotEmpty($responseXmlArray, __METHOD__ . ' Response is empty'); if ($user === null) { $user = $this->getCurrentUser(); } - Assert::assertIsArray( - $this->responseXml, - __METHOD__ . " responseXml for user $user is not an array" + $responseXmlArray, + __METHOD__ . " response for user $user is not an array" ); Assert::assertArrayHasKey( "value", - $this->responseXml, - __METHOD__ . " responseXml for user $user does not have key 'value'" + $responseXmlArray, + __METHOD__ . " response for user $user does not have key 'value'" ); - $multistatus = $this->responseXml["value"]; + $multistatus = $responseXmlArray["value"]; if ($multistatus == null) { $multistatus = []; } @@ -4573,7 +4493,9 @@ trait WebDav { default: throw new Exception("error"); } + $multistatusResults = $this->getMultiStatusResultFromPropfindResult($user); + $results = []; foreach ($multistatusResults as $multistatusResult) { $entryPath = $multistatusResult['value'][0]['value']; @@ -4627,7 +4549,8 @@ trait WebDav { } $hrefRegex = "/^" . $hrefRegex . "/"; - $searchResults = $this->getResponseXml()->xpath("//d:multistatus/d:response"); + $searchResults = HttpRequestHelper::getResponseXml($this->getResponse())->xpath("//d:multistatus/d:response"); + $results = []; foreach ($searchResults as $item) { $href = (string)$item->xpath("d:href")[0]; @@ -4707,17 +4630,13 @@ trait WebDav { */ public function checkAuthorOfAVersionOfFile(string $index, string $expectedUsername):void { $expectedUserDisplayName = $this->getUserDisplayName($expectedUsername); - $resXml = $this->getResponseXml(); - if ($resXml === null) { - $resXml = HttpRequestHelper::getResponseXml( - $this->getResponse(), - __METHOD__ - ); - $this->setResponseXmlObject($resXml); - } + $responseXmlObject = HttpRequestHelper::getResponseXml( + $this->getResponse(), + __METHOD__ + ); // the username should be in oc:meta-version-edited-by - $xmlPart = $resXml->xpath("//oc:meta-version-edited-by"); + $xmlPart = $responseXmlObject->xpath("//oc:meta-version-edited-by"); $authors = []; foreach ($xmlPart as $idx => $author) { // The first element is the root path element which is not a version @@ -4739,7 +4658,7 @@ trait WebDav { ); // the user's display name should be in oc:meta-version-edited-by-name - $xmlPart = $resXml->xpath("//oc:meta-version-edited-by-name"); + $xmlPart = $responseXmlObject->xpath("//oc:meta-version-edited-by-name"); $displaynames = []; foreach ($xmlPart as $idx => $displayname) { // The first element is the root path element which is not a version diff --git a/tests/acceptance/bootstrap/WebDavLockingContext.php b/tests/acceptance/bootstrap/WebDavLockingContext.php index 0ffeb4e01..01c20c740 100644 --- a/tests/acceptance/bootstrap/WebDavLockingContext.php +++ b/tests/acceptance/bootstrap/WebDavLockingContext.php @@ -123,8 +123,8 @@ class WebDavLockingContext implements Context { ); } - $responseXml = $this->featureContext->getResponseXml($response, __METHOD__); - $xmlPart = $responseXml->xpath("//d:locktoken/d:href"); + $responseXmlObject = HttpRequestHelper::getResponseXml($response, __METHOD__); + $xmlPart = $responseXmlObject->xpath("//d:locktoken/d:href"); if (isset($xmlPart[0])) { $this->tokenOfLastLock[$user][$file] = (string) $xmlPart[0]; } else { @@ -556,8 +556,8 @@ class WebDavLockingContext implements Context { $body, $this->featureContext->getDavPathVersion() ); - $responseXml = $this->featureContext->getResponseXml($response, __METHOD__); - $xmlPart = $responseXml->xpath("//d:response//d:lockdiscovery/d:activelock"); + $responseXmlObject = HttpRequestHelper::getResponseXml($response, __METHOD__); + $xmlPart = $responseXmlObject->xpath("//d:response//d:lockdiscovery/d:activelock"); if (\is_array($xmlPart)) { return \count($xmlPart); } else { @@ -904,8 +904,8 @@ class WebDavLockingContext implements Context { public function numberOfLockShouldBeReportedInProjectSpace(int $count, string $file, string $spaceName, string $user) { $response = $this->spacesContext->sendPropfindRequestToSpace($user, $spaceName, $file, null, '0'); $this->featureContext->theHTTPStatusCodeShouldBe(207, "", $response); - $responseXml = $this->featureContext->getResponseXml($response); - $xmlPart = $responseXml->xpath("//d:response//d:lockdiscovery/d:activelock"); + $responseXmlObject = HttpRequestHelper::getResponseXml($response, __METHOD__); + $xmlPart = $responseXmlObject->xpath("//d:response//d:lockdiscovery/d:activelock"); if (\is_array($xmlPart)) { $lockCount = \count($xmlPart); } else { diff --git a/tests/acceptance/bootstrap/WebDavPropertiesContext.php b/tests/acceptance/bootstrap/WebDavPropertiesContext.php index d075b4eea..bb2cd9d5a 100644 --- a/tests/acceptance/bootstrap/WebDavPropertiesContext.php +++ b/tests/acceptance/bootstrap/WebDavPropertiesContext.php @@ -28,6 +28,7 @@ use Psr\Http\Message\ResponseInterface; use TestHelpers\Asserts\WebDav as WebDavTest; use TestHelpers\WebDavHelper; use TestHelpers\BehatHelper; +use TestHelpers\HttpRequestHelper; require_once 'bootstrap.php'; @@ -442,7 +443,7 @@ class WebDavPropertiesContext implements Context { */ public function theResponseShouldContainCustomPropertyWithValue(string $propertyName, string $propertyValue): void { $propertyValue = \str_replace('\"', '"', $propertyValue); - $responseXmlObject = $this->featureContext->getResponseXml( + $responseXmlObject = HttpRequestHelper::getResponseXml( $this->featureContext->getResponse(), __METHOD__ ); @@ -479,7 +480,7 @@ class WebDavPropertiesContext implements Context { ):void { // let's unescape quotes first $propertyValue = \str_replace('\"', '"', $propertyValue); - $responseXmlObject = $this->featureContext->getResponseXml( + $responseXmlObject = HttpRequestHelper::getResponseXml( $this->featureContext->getResponse(), __METHOD__ ); @@ -518,7 +519,7 @@ class WebDavPropertiesContext implements Context { string $property, string $childProperty ):void { - $xmlPart = $this->featureContext->getResponseXml()->xpath( + $xmlPart = HttpRequestHelper::getResponseXml($this->featureContext->getResponse())->xpath( "//d:prop/$property/$childProperty" ); Assert::assertTrue( @@ -636,7 +637,8 @@ class WebDavPropertiesContext implements Context { * @throws Exception */ public function checkResponseContainsProperty(ResponseInterface $response, string $key, string $namespaceString = null): SimpleXMLElement { - $xmlPart = $this->featureContext->getResponseXml($response); + $xmlPart = HttpRequestHelper::getResponseXml($response, __METHOD__); + ; if ($namespaceString !== null) { $ns = WebDavHelper::parseNamespace($namespaceString); @@ -759,11 +761,11 @@ class WebDavPropertiesContext implements Context { * @return void */ public function assertValueOfItemInResponseAboutUserIs(string $xpath, ?string $user, string $expectedValue):void { - $resXml = $this->featureContext->getResponseXml( + $responseXmlObject = HttpRequestHelper::getResponseXml( $this->featureContext->getResponse(), __METHOD__ ); - $value = $this->getXmlItemByXpath($resXml, $xpath); + $value = $this->getXmlItemByXpath($responseXmlObject, $xpath); $user = $this->featureContext->getActualUsername($user); $expectedValue = $this->featureContext->substituteInLineCodes( $expectedValue, @@ -796,11 +798,11 @@ class WebDavPropertiesContext implements Context { if (!$expectedValue2) { $expectedValue2 = $expectedValue1; } - $resXml = $this->featureContext->getResponseXml( + $responseXmlObject = HttpRequestHelper::getResponseXml( $this->featureContext->getResponse(), __METHOD__ ); - $value = $this->getXmlItemByXpath($resXml, $xpath); + $value = $this->getXmlItemByXpath($responseXmlObject, $xpath); $user = $this->featureContext->getActualUsername($user); $expectedValue1 = $this->featureContext->substituteInLineCodes( $expectedValue1, @@ -822,16 +824,16 @@ class WebDavPropertiesContext implements Context { } /** - * @param SimpleXMLElement $xmlResponse + * @param SimpleXMLElement $responseXmlObject * @param string $xpath * * @return string */ public function getXmlItemByXpath( - SimpleXMLElement $xmlResponse, + SimpleXMLElement $responseXmlObject, string $xpath ): string { - $xmlPart = $xmlResponse->xpath($xpath); + $xmlPart = $responseXmlObject->xpath($xpath); Assert::assertTrue( isset($xmlPart[0]), "Cannot find item with xpath \"$xpath\"" @@ -850,7 +852,7 @@ class WebDavPropertiesContext implements Context { */ public function assertValueOfItemInResponseRegExp(string $xpath, string $pattern):void { $this->assertXpathValueMatchesPattern( - $this->featureContext->getResponseXml(), + HttpRequestHelper::getResponseXml($this->featureContext->getResponse()), $xpath, $pattern ); @@ -871,7 +873,7 @@ class WebDavPropertiesContext implements Context { $response = $this->getPropertiesOfEntryFromLastLinkShare($path, $propertiesTable); $this->featureContext->theHTTPStatusCodeShouldBe('207', "", $response); $this->assertXpathValueMatchesPattern( - $this->featureContext->getResponseXml($response), + HttpRequestHelper::getResponseXml($response, __METHOD__), $xpath, $pattern ); @@ -887,7 +889,7 @@ class WebDavPropertiesContext implements Context { * @throws Exception */ public function assertEntryWithHrefMatchingRegExpInResponseToUser(string $expectedHref, string $user):void { - $resXml = $this->featureContext->getResponseXml( + $responseXmlObject = HttpRequestHelper::getResponseXml( $this->featureContext->getResponse(), __METHOD__ ); @@ -904,7 +906,7 @@ class WebDavPropertiesContext implements Context { while (true) { $index++; $xpath = "//d:response[$index]/d:href"; - $xmlPart = $resXml->xpath($xpath); + $xmlPart = $responseXmlObject->xpath($xpath); // If we have run out of entries in the response, then fail the test Assert::assertTrue( isset($xmlPart[0]), @@ -937,16 +939,16 @@ class WebDavPropertiesContext implements Context { } /** - * @param SimpleXMLElement $responseXml + * @param SimpleXMLElement $responseXmlObject * @param string $xpath * @param string $pattern * @param string|null $user * * @return void - * @throws Exception + * @throws JsonException */ - public function assertXpathValueMatchesPattern(SimpleXMLElement $responseXml, string $xpath, string $pattern, ?string $user = null): void { - $xmlPart = $responseXml->xpath($xpath); + public function assertXpathValueMatchesPattern(SimpleXMLElement $responseXmlObject, string $xpath, string $pattern, ?string $user = null): void { + $xmlPart = $responseXmlObject->xpath($xpath); Assert::assertTrue( isset($xmlPart[0]), "Cannot find item with xpath \"$xpath\"" @@ -983,22 +985,6 @@ class WebDavPropertiesContext implements Context { ); } - /** - * @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); - } - /** * @Then /^as user "([^"]*)" the lock discovery property "([^"]*)" of the (?:file|folder|entry) "([^"]*)" should match "([^"]*)"$/ * @@ -1020,7 +1006,7 @@ class WebDavPropertiesContext implements Context { ); $this->featureContext->theHTTPStatusCodeShouldBe('207', '', $response); $this->assertXpathValueMatchesPattern( - $this->featureContext->getResponseXml($response), + HttpRequestHelper::getResponseXml($response, __METHOD__), $xpath, $pattern, $user @@ -1036,7 +1022,7 @@ class WebDavPropertiesContext implements Context { * @throws Exception */ public function assertItemInResponseDoesNotExist(string $xpath):void { - $xmlPart = $this->featureContext->getResponseXml()->xpath($xpath); + $xmlPart = HttpRequestHelper::getResponseXml($this->featureContext->getResponse())->xpath($xpath); Assert::assertFalse( isset($xmlPart[0]), "Found item with xpath \"$xpath\" but it should not exist" @@ -1114,7 +1100,7 @@ class WebDavPropertiesContext implements Context { string $key, string $regex ):void { - $xmlPart = $this->featureContext->getResponseXml()->xpath( + $xmlPart = HttpRequestHelper::getResponseXml($this->featureContext->getResponse())->xpath( "//d:prop/$key" ); Assert::assertTrue( @@ -1140,7 +1126,7 @@ class WebDavPropertiesContext implements Context { public function theResponseShouldContainAShareTypesPropertyWith(TableNode $table):void { $this->featureContext->verifyTableNodeColumnsCount($table, 1); WebdavTest::assertResponseContainsShareTypes( - $this->featureContext->getResponseXml(), + HttpRequestHelper::getResponseXml($this->featureContext->getResponse()), $table->getRows() ); } @@ -1154,7 +1140,7 @@ class WebDavPropertiesContext implements Context { * @throws Exception */ public function theResponseShouldContainAnEmptyProperty(string $property):void { - $xmlPart = $this->featureContext->getResponseXml()->xpath( + $xmlPart = HttpRequestHelper::getResponseXml($this->featureContext->getResponse())->xpath( "//d:prop/$property" ); Assert::assertCount( @@ -1189,7 +1175,7 @@ class WebDavPropertiesContext implements Context { $spaceId, $propertiesTable ); - $xmlObject = $this->featureContext->getResponseXml($response); + $xmlObject = HttpRequestHelper::getResponseXml($response, __METHOD__); $this->storedETAG[$user][$storePath] = $this->featureContext->getEtagFromResponseXmlObject($xmlObject); return $xmlObject; @@ -1302,9 +1288,8 @@ class WebDavPropertiesContext implements Context { * @throws Exception */ public function theResponseShouldHavePropertyWithValue(string $username, TableNode $expectedPropTable):void { - $username = $this->featureContext->getActualUsername($username); $this->featureContext->verifyTableNodeColumns($expectedPropTable, ['resource', 'propertyName', 'propertyValue']); - $responseXmlObject = $this->featureContext->getResponseXml(); + $responseXmlObject = HttpRequestHelper::getResponseXml($this->featureContext->getResponse()); $href = (string)$responseXmlObject->xpath("//d:href")[0]; $hrefBase = $this->parseBaseDavPathFromXMLHref($href); @@ -1339,7 +1324,7 @@ class WebDavPropertiesContext implements Context { null, $propertiesTable ); - return $this->featureContext->getEtagFromResponseXmlObject($this->featureContext->getResponseXml($response)); + return $this->featureContext->getEtagFromResponseXmlObject(HttpRequestHelper::getResponseXml($response, __METHOD__)); } /**