From fe8c0c7de8889afbef7baced1c639cbea84185da Mon Sep 17 00:00:00 2001 From: Karun Atreya <33852651+KarunAtreya@users.noreply.github.com> Date: Thu, 21 Sep 2023 11:27:45 +0545 Subject: [PATCH] refactor given and when steps in trashbin context (#7302) applied same type of change in tagContext addressed the reviews added a space in step regex --- .../features/bootstrap/TagContext.php | 85 +++++++++++++------ .../features/bootstrap/TrashbinContext.php | 54 ++++++------ 2 files changed, 85 insertions(+), 54 deletions(-) diff --git a/tests/acceptance/features/bootstrap/TagContext.php b/tests/acceptance/features/bootstrap/TagContext.php index 1aeb2efefa..e608a3e801 100644 --- a/tests/acceptance/features/bootstrap/TagContext.php +++ b/tests/acceptance/features/bootstrap/TagContext.php @@ -24,6 +24,7 @@ use Behat\Behat\Hook\Scope\BeforeScenarioScope; use Behat\Gherkin\Node\TableNode; use PHPUnit\Framework\Assert; use TestHelpers\GraphHelper; +use \Psr\Http\Message\ResponseInterface; require_once 'bootstrap.php'; @@ -53,7 +54,38 @@ class TagContext implements Context { } /** - * @When /^user "([^"]*)" creates the following tags for (folder|file)\s?"([^"]*)" of space "([^"]*)":$/ + * @param string $user + * @param string $fileOrFolder (file|folder) + * @param string $resource + * @param string $space + * @param TableNode $table + * + * @return ResponseInterface + * @throws Exception + */ + public function createTags(string $user, string $fileOrFolder, string $resource, string $space, TableNode $table):ResponseInterface { + $tagNameArray = []; + foreach ($table->getRows() as $value) { + $tagNameArray[] = $value[0]; + } + if ($fileOrFolder === 'folder' || $fileOrFolder === 'folders') { + $resourceId = $this->spacesContext->getResourceId($user, $space, $resource); + } else { + $resourceId = $this->spacesContext->getFileId($user, $space, $resource); + } + + return GraphHelper::createTags( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $user, + $this->featureContext->getPasswordForUser($user), + $resourceId, + $tagNameArray + ); + } + + /** + * @When /^user "([^"]*)" creates the following tags for (folder|file) "([^"]*)" of space "([^"]*)":$/ * * @param string $user * @param string $fileOrFolder (file|folder) @@ -65,24 +97,7 @@ class TagContext implements Context { * @throws Exception */ public function theUserCreatesFollowingTags(string $user, string $fileOrFolder, string $resource, string $space, TableNode $table):void { - $tagNameArray = []; - foreach ($table->getRows() as $value) { - $tagNameArray[] = $value[0]; - } - if ($fileOrFolder === 'folder' || $fileOrFolder === 'folders') { - $resourceId = $this->spacesContext->getResourceId($user, $space, $resource); - } else { - $resourceId = $this->spacesContext->getFileId($user, $space, $resource); - } - - $response = GraphHelper::createTags( - $this->featureContext->getBaseUrl(), - $this->featureContext->getStepLineRef(), - $user, - $this->featureContext->getPasswordForUser($user), - $resourceId, - $tagNameArray - ); + $response = $this->createTags($user, $fileOrFolder, $resource, $space, $table); $this->featureContext->setResponse($response); } @@ -99,8 +114,8 @@ class TagContext implements Context { * @throws Exception */ public function theUserHasCreatedFollowingTags(string $user, string $fileOrFolder, string $resource, string $space, TableNode $table):void { - $this->theUserCreatesFollowingTags($user, $fileOrFolder, $resource, $space, $table); - $this->featureContext->theHttpStatusCodeShouldBe(200); + $response = $this->createTags($user, $fileOrFolder, $resource, $space, $table); + $this->featureContext->theHttpStatusCodeShouldBe(200, "", $response); } /** @@ -175,18 +190,16 @@ class TagContext implements Context { } /** - * @When /^user "([^"]*)" removes the following tags for (folder|file)\s?"([^"]*)" of space "([^"]*)":$/ - * * @param string $user * @param string $fileOrFolder (file|folder) * @param string $resource * @param string $space * @param TableNode $table * - * @return void + * @return ResponseInterface * @throws Exception */ - public function userRemovesTagsFromResourceOfTheSpace(string $user, string $fileOrFolder, string $resource, string $space, TableNode $table):void { + public function removeTagsFromResourceOfTheSpace(string $user, string $fileOrFolder, string $resource, string $space, TableNode $table):ResponseInterface { $tagNameArray = []; foreach ($table->getRows() as $value) { $tagNameArray[] = $value[0]; @@ -198,7 +211,7 @@ class TagContext implements Context { $resourceId = $this->spacesContext->getFileId($user, $space, $resource); } - $response = GraphHelper::deleteTags( + return GraphHelper::deleteTags( $this->featureContext->getBaseUrl(), $this->featureContext->getStepLineRef(), $user, @@ -206,6 +219,22 @@ class TagContext implements Context { $resourceId, $tagNameArray ); + } + + /** + * @When /^user "([^"]*)" removes the following tags for (folder|file) "([^"]*)" of space "([^"]*)":$/ + * + * @param string $user + * @param string $fileOrFolder (file|folder) + * @param string $resource + * @param string $space + * @param TableNode $table + * + * @return void + * @throws Exception + */ + public function userRemovesTagsFromResourceOfTheSpace(string $user, string $fileOrFolder, string $resource, string $space, TableNode $table):void { + $response = $this->removeTagsFromResourceOfTheSpace($user, $fileOrFolder, $resource, $space, $table); $this->featureContext->setResponse($response); } @@ -222,7 +251,7 @@ class TagContext implements Context { * @throws Exception */ public function userHAsRemovedTheFollowingTagsForFileOfSpace(string $user, string $fileOrFolder, string $resource, string $space, TableNode $table):void { - $this->userRemovesTagsFromResourceOfTheSpace($user, $fileOrFolder, $resource, $space, $table); - $this->featureContext->theHttpStatusCodeShouldBe(200); + $response = $this->removeTagsFromResourceOfTheSpace($user, $fileOrFolder, $resource, $space, $table); + $this->featureContext->theHttpStatusCodeShouldBe(200, "", $response); } } diff --git a/tests/acceptance/features/bootstrap/TrashbinContext.php b/tests/acceptance/features/bootstrap/TrashbinContext.php index ef098f3d12..c237a25be9 100644 --- a/tests/acceptance/features/bootstrap/TrashbinContext.php +++ b/tests/acceptance/features/bootstrap/TrashbinContext.php @@ -37,8 +37,6 @@ class TrashbinContext implements Context { private FeatureContext $featureContext; /** - * @When user :user empties the trashbin using the trashbin API - * * @param string|null $user user * * @return ResponseInterface @@ -46,7 +44,7 @@ class TrashbinContext implements Context { public function emptyTrashbin(?string $user):ResponseInterface { $user = $this->featureContext->getActualUsername($user); $davPathVersion = $this->featureContext->getDavPathVersion(); - $response = WebDavHelper::makeDavRequest( + return WebDavHelper::makeDavRequest( $this->featureContext->getBaseUrl(), $user, $this->featureContext->getPasswordForUser($user), @@ -58,11 +56,18 @@ class TrashbinContext implements Context { $davPathVersion, 'trash-bin' ); - - $this->featureContext->setResponse($response); - return $response; } + /** + * @When user :user empties the trashbin using the trashbin API + * + * @param string $user user + * + * @return void + */ + public function userEmptiesTrashbin(string $user): void { + $this->featureContext->setResponse($this->emptyTrashbin($user)); + } /** * @Given user :user has emptied the trashbin * @@ -72,12 +77,7 @@ class TrashbinContext implements Context { */ public function userHasEmptiedTrashbin(string $user):void { $response = $this->emptyTrashbin($user); - - Assert::assertEquals( - 204, - $response->getStatusCode(), - __METHOD__ . " Expected status code was '204' but got '" . $response->getStatusCode() . "'" - ); + $this->featureContext->theHTTPStatusCodeShouldBe(204, '', $response); } /** @@ -547,7 +547,8 @@ class TrashbinContext implements Context { public function userTriesToRestoreFromTrashbinOfUser(?string $asUser, ?string $path, ?string $user):void { $user = $this->featureContext->getActualUsername($user); $asUser = $this->featureContext->getActualUsername($asUser); - $this->restoreElement($user, $path, null, true, $asUser); + $response = $this->restoreElement($user, $path, null, true, $asUser); + $this->featureContext->setResponse($response); } /** @@ -565,7 +566,8 @@ class TrashbinContext implements Context { public function userTriesToRestoreFromTrashbinOfUserUsingPassword(?string $asUser, ?string $path, ?string $user, ?string $password):void { $asUser = $this->featureContext->getActualUsername($asUser); $user = $this->featureContext->getActualUsername($user); - $this->restoreElement($user, $path, null, true, $asUser, $password); + $response = $this->restoreElement($user, $path, null, true, $asUser, $password); + $this->featureContext->setResponse($response); } /** @@ -752,18 +754,18 @@ class TrashbinContext implements Context { * @param string|null $asUser - To send request as another user * @param string|null $password * - * @return void + * @return ResponseInterface * @throws JsonException * @throws GuzzleException */ - private function sendUndeleteRequest(string $user, string $trashItemHRef, string $destinationPath, ?string $asUser = null, ?string $password = null):void { + private function sendUndeleteRequest(string $user, string $trashItemHRef, string $destinationPath, ?string $asUser = null, ?string $password = null):ResponseInterface { $asUser = $asUser ?? $user; $destinationPath = \trim($destinationPath, '/'); $destinationValue = $this->featureContext->getBaseUrl() . "/remote.php/dav/files/$user/$destinationPath"; $trashItemHRef = $this->convertTrashbinHref($trashItemHRef); $headers['Destination'] = $destinationValue; - $response = $this->featureContext->makeDavRequest( + return $this->featureContext->makeDavRequest( $asUser, 'MOVE', $trashItemHRef, @@ -776,7 +778,6 @@ class TrashbinContext implements Context { [], $user ); - $this->featureContext->setResponse($response); } /** @@ -787,11 +788,11 @@ class TrashbinContext implements Context { * @param string|null $asUser - To send request as another user * @param string|null $password * - * @return void + * @return ResponseInterface * @throws JsonException * @throws GuzzleException */ - private function restoreElement(string $user, string $originalPath, ?string $destinationPath = null, bool $throwExceptionIfNotFound = true, ?string $asUser = null, ?string $password = null):void { + private function restoreElement(string $user, string $originalPath, ?string $destinationPath = null, bool $throwExceptionIfNotFound = true, ?string $asUser = null, ?string $password = null):ResponseInterface { $asUser = $asUser ?? $user; $listing = $this->listTrashbinFolder($user); $originalPath = \trim($originalPath, '/'); @@ -800,14 +801,13 @@ class TrashbinContext implements Context { } foreach ($listing as $entry) { if ($entry['original-location'] === $originalPath) { - $this->sendUndeleteRequest( + return $this->sendUndeleteRequest( $user, $entry['href'], $destinationPath, $asUser, $password ); - return; } } // The requested element to restore was not even in the trashbin. @@ -907,7 +907,8 @@ class TrashbinContext implements Context { * @throws Exception */ public function userTriesToRestoreElementInTrash(string $user, string $originalPath):void { - $this->restoreElement($user, $originalPath, null, false); + $response = $this->restoreElement($user, $originalPath, null, false); + $this->featureContext->setResponse($response); } /** @@ -922,7 +923,7 @@ class TrashbinContext implements Context { */ public function elementInTrashIsRestored(?string $user, string $originalPath):void { $user = $this->featureContext->getActualUsername($user); - $this->restoreElement($user, $originalPath); + $this->featureContext->setResponse($this->restoreElement($user, $originalPath)); } /** @@ -955,7 +956,8 @@ class TrashbinContext implements Context { * @throws Exception */ public function elementInTrashHasBeenRestored(string $user, string $originalPath):void { - $this->restoreElement($user, $originalPath); + $response = $this->restoreElement($user, $originalPath); + $this->featureContext->theHTTPStatusCodeShouldBe(201, "", $response); if ($this->isInTrash($user, $originalPath)) { throw new Exception("File previously located at $originalPath is still in the trashbin"); } @@ -978,7 +980,7 @@ class TrashbinContext implements Context { ?string $destinationPath ):void { $user = $this->featureContext->getActualUsername($user); - $this->restoreElement($user, $originalPath, $destinationPath); + $this->featureContext->setResponse($this->restoreElement($user, $originalPath, $destinationPath)); } /**