From 71daf3f6ba1db3dd1e07719f85d4e138153f4f21 Mon Sep 17 00:00:00 2001 From: Karun Atreya <33852651+KarunAtreya@users.noreply.github.com> Date: Wed, 13 Sep 2023 16:24:42 +0545 Subject: [PATCH] refactor given when then steps in fileversions context (#7269) migrating set response from helper function to when step in preview feature separate single when/then steps by converting to helper function use of httpstatuscode check function in given step and rebased change function name remove use of setresponse in then step set the returned response --- .../bootstrap/FilesVersionsContext.php | 67 ++++++++++++++----- .../features/bootstrap/SpacesContext.php | 2 +- .../acceptance/features/bootstrap/WebDav.php | 22 +++--- 3 files changed, 66 insertions(+), 25 deletions(-) diff --git a/tests/acceptance/features/bootstrap/FilesVersionsContext.php b/tests/acceptance/features/bootstrap/FilesVersionsContext.php index b18f2591ed..9959515c06 100644 --- a/tests/acceptance/features/bootstrap/FilesVersionsContext.php +++ b/tests/acceptance/features/bootstrap/FilesVersionsContext.php @@ -134,17 +134,14 @@ class FilesVersionsContext implements Context { } /** - * @When user :user restores version index :versionIndex of file :path using the WebDAV API - * @Given user :user has restored version index :versionIndex of file :path - * * @param string $user * @param int $versionIndex * @param string $path * - * @return void + * @return ResponseInterface * @throws Exception */ - public function userRestoresVersionIndexOfFile(string $user, int $versionIndex, string $path):void { + public function restoreVersionIndexOfFile(string $user, int $versionIndex, string $path):ResponseInterface { $user = $this->featureContext->getActualUsername($user); $fileId = $this->featureContext->getFileIdForPath($user, $path); Assert::assertNotNull($fileId, __METHOD__ . " fileid of file $path user $user not found (the file may not exist)"); @@ -155,7 +152,7 @@ class FilesVersionsContext implements Context { WebDavHelper::getDavPath($user, 2) . \trim($path, "/"); $fullUrl = $this->featureContext->getBaseUrlWithoutPath() . $xmlPart[$versionIndex]; - $response = HttpRequestHelper::sendRequest( + return HttpRequestHelper::sendRequest( $fullUrl, $this->featureContext->getStepLineRef(), 'COPY', @@ -163,6 +160,35 @@ class FilesVersionsContext implements Context { $this->featureContext->getPasswordForUser($user), ['Destination' => $destinationUrl] ); + } + + /** + * @Given user :user has restored version index :versionIndex of file :path + * + * @param string $user + * @param int $versionIndex + * @param string $path + * + * @return void + * @throws Exception + */ + public function userHasRestoredVersionIndexOfFile(string $user, int $versionIndex, string $path):void { + $response = $this->restoreVersionIndexOfFile($user, $versionIndex, $path); + $this->featureContext->theHTTPStatusCodeShouldBe(204, "", $response); + } + + /** + * @When user :user restores version index :versionIndex of file :path using the WebDAV API + * + * @param string $user + * @param int $versionIndex + * @param string $path + * + * @return void + * @throws Exception + */ + public function userRestoresVersionIndexOfFile(string $user, int $versionIndex, string $path):void { + $response = $this->restoreVersionIndexOfFile($user, $versionIndex, $path); $this->featureContext->setResponse($response, $user); } @@ -280,16 +306,14 @@ class FilesVersionsContext implements Context { } /** - * @When user :user downloads the version of file :path with the index :index - * * @param string $user * @param string $path * @param string $index * - * @return void + * @return ResponseInterface * @throws Exception */ - public function downloadVersion(string $user, string $path, string $index):void { + public function downloadVersion(string $user, string $path, string $index):ResponseInterface { $user = $this->featureContext->getActualUsername($user); $fileId = $this->featureContext->getFileIdForPath($user, $path); Assert::assertNotNull($fileId, __METHOD__ . " fileid of file $path user $user not found (the file may not exist)"); @@ -305,13 +329,26 @@ class FilesVersionsContext implements Context { $url = WebDavHelper::sanitizeUrl( $this->featureContext->getBaseUrlWithoutPath() . $xmlPart[$index] ); - $response = HttpRequestHelper::get( + return HttpRequestHelper::get( $url, $this->featureContext->getStepLineRef(), $user, $this->featureContext->getPasswordForUser($user) ); - $this->featureContext->setResponse($response, $user); + } + + /** + * @When user :user downloads the version of file :path with the index :index + * + * @param string $user + * @param string $path + * @param string $index + * + * @return void + * @throws Exception + */ + public function userDownloadsVersion(string $user, string $path, string $index):void { + $this->featureContext->setResponse($this->downloadVersion($user, $path, $index), $user); } /** @@ -331,9 +368,9 @@ class FilesVersionsContext implements Context { string $user, string $content ): void { - $this->downloadVersion($user, $path, $index); - $this->featureContext->theHTTPStatusCodeShouldBe("200"); - $this->featureContext->downloadedContentShouldBe($content); + $response = $this->downloadVersion($user, $path, $index); + $this->featureContext->theHTTPStatusCodeShouldBe("200", '', $response); + $this->featureContext->checkDownloadedContentMatches($content, '', $response); } /** diff --git a/tests/acceptance/features/bootstrap/SpacesContext.php b/tests/acceptance/features/bootstrap/SpacesContext.php index cc34c07e68..bed6ba6539 100644 --- a/tests/acceptance/features/bootstrap/SpacesContext.php +++ b/tests/acceptance/features/bootstrap/SpacesContext.php @@ -2673,7 +2673,7 @@ class SpacesContext implements Context { string $spaceName ): void { $this->setSpaceIDByName($user, $spaceName); - $this->filesVersionsContext->downloadVersion($user, $fileName, $index); + $this->featureContext->setResponse($this->filesVersionsContext->downloadVersion($user, $fileName, $index)); WebDavHelper::$SPACE_ID_FROM_OCIS = ''; } diff --git a/tests/acceptance/features/bootstrap/WebDav.php b/tests/acceptance/features/bootstrap/WebDav.php index c20d3f1c44..1c1e17ad2a 100644 --- a/tests/acceptance/features/bootstrap/WebDav.php +++ b/tests/acceptance/features/bootstrap/WebDav.php @@ -455,10 +455,9 @@ trait WebDav { * @param string|null $width * @param string|null $height * - * @return void - * @throws GuzzleException + * @return ResponseInterface */ - public function downloadPreviews(string $user, ?string $path, ?string $doDavRequestAsUser, ?string $width, ?string $height):void { + public function downloadPreviews(string $user, ?string $path, ?string $doDavRequestAsUser, ?string $width, ?string $height):ResponseInterface { $user = $this->getActualUsername($user); $doDavRequestAsUser = $this->getActualUsername($doDavRequestAsUser); $urlParameter = [ @@ -467,14 +466,14 @@ trait WebDav { 'forceIcon' => '0', 'preview' => '1' ]; - $this->response = $this->makeDavRequest( + return $this->makeDavRequest( $user, "GET", $path, [], null, "files", - null, + '2', false, null, $urlParameter, @@ -1054,14 +1053,17 @@ trait WebDav { /** * @param string $expectedContent * @param string $extraErrorText + * @param ResponseInterface|null $response * * @return void */ public function checkDownloadedContentMatches( string $expectedContent, - string $extraErrorText = "" + string $extraErrorText = "", + ?ResponseInterface $response = null ):void { - $actualContent = (string) $this->response->getBody(); + $response = $response ?? $this->response; + $actualContent = (string) $response->getBody(); // For this test we really care about the content. // A separate "Then" step can specifically check the HTTP status. // But if the content is wrong (e.g. empty) then it is useful to @@ -4639,13 +4641,14 @@ trait WebDav { * @return void */ public function downloadPreviewOfFiles(string $user, string $path, string $width, string $height):void { - $this->downloadPreviews( + $response = $this->downloadPreviews( $user, $path, null, $width, $height ); + $this->setResponse($response); } /** @@ -4660,13 +4663,14 @@ trait WebDav { * @return void */ public function downloadPreviewOfOtherUser(string $user1, string $path, string $doDavRequestAsUser, string $width, string $height):void { - $this->downloadPreviews( + $response = $this->downloadPreviews( $user1, $path, $doDavRequestAsUser, $width, $height ); + $this->setResponse($response); } /**