From dd95d73e0dbc0b6c6aafb10a9fef556d4f697240 Mon Sep 17 00:00:00 2001 From: Saw-jan Date: Mon, 2 Oct 2023 18:35:49 +0545 Subject: [PATCH 1/3] remove AppConfigurationContext file --- tests/acceptance/config/behat-core.yml | 20 - .../bootstrap/AppConfigurationContext.php | 574 ------------------ .../bootstrap/CapabilitiesContext.php | 274 +++++++-- .../features/bootstrap/FeatureContext.php | 149 ----- 4 files changed, 237 insertions(+), 780 deletions(-) delete mode 100644 tests/acceptance/features/bootstrap/AppConfigurationContext.php diff --git a/tests/acceptance/config/behat-core.yml b/tests/acceptance/config/behat-core.yml index fdb1a7b4c..dd0e9a1ae 100644 --- a/tests/acceptance/config/behat-core.yml +++ b/tests/acceptance/config/behat-core.yml @@ -18,7 +18,6 @@ default: adminPassword: admin regularUserPassword: 123456 ocPath: apps/testing/api/v1/occ - - AppConfigurationContext: - ChecksumContext: - FilesVersionsContext: - TrashbinContext: @@ -57,7 +56,6 @@ default: contexts: - FeatureContext: *common_feature_context_params - CapabilitiesContext: - - AppConfigurationContext: coreApiFavorites: paths: @@ -67,7 +65,6 @@ default: - FeatureContext: *common_feature_context_params - FavoritesContext: - WebDavPropertiesContext: - - AppConfigurationContext: coreApiShareCreateSpecialToShares1: paths: @@ -77,7 +74,6 @@ default: - FeatureContext: *common_feature_context_params - TrashbinContext: - WebDavPropertiesContext: - - AppConfigurationContext: coreApiShareCreateSpecialToShares2: paths: @@ -87,7 +83,6 @@ default: - FeatureContext: *common_feature_context_params - TrashbinContext: - WebDavPropertiesContext: - - AppConfigurationContext: coreApiSharees: paths: @@ -96,7 +91,6 @@ default: contexts: - FeatureContext: *common_feature_context_params - ShareesContext: - - AppConfigurationContext: coreApiShareManagementToShares: paths: @@ -107,7 +101,6 @@ default: - PublicWebDavContext: - TrashbinContext: - WebDavPropertiesContext: - - AppConfigurationContext: - FilesVersionsContext: coreApiShareManagementBasicToShares: @@ -150,7 +143,6 @@ default: - PublicWebDavContext: - TrashbinContext: - WebDavPropertiesContext: - - AppConfigurationContext: coreApiSharePublicLink2: paths: @@ -161,7 +153,6 @@ default: - PublicWebDavContext: - TrashbinContext: - WebDavPropertiesContext: - - AppConfigurationContext: coreApiSharePublicLink3: paths: @@ -172,7 +163,6 @@ default: - PublicWebDavContext: - TrashbinContext: - WebDavPropertiesContext: - - AppConfigurationContext: coreApiShareReshareToShares1: paths: @@ -193,7 +183,6 @@ default: - PublicWebDavContext: - TrashbinContext: - WebDavPropertiesContext: - - AppConfigurationContext: coreApiShareReshareToShares3: paths: @@ -204,7 +193,6 @@ default: - PublicWebDavContext: - TrashbinContext: - WebDavPropertiesContext: - - AppConfigurationContext: coreApiShareUpdateToShares: paths: @@ -215,7 +203,6 @@ default: - PublicWebDavContext: - TrashbinContext: - WebDavPropertiesContext: - - AppConfigurationContext: coreApiTrashbin: paths: @@ -224,7 +211,6 @@ default: contexts: - FeatureContext: *common_feature_context_params - TrashbinContext: - - AppConfigurationContext: - WebDavPropertiesContext: coreApiTrashbinRestore: @@ -234,7 +220,6 @@ default: contexts: - FeatureContext: *common_feature_context_params - TrashbinContext: - - AppConfigurationContext: - WebDavPropertiesContext: coreApiVersions: @@ -246,7 +231,6 @@ default: - ChecksumContext: - FilesVersionsContext: - WebDavPropertiesContext: - - AppConfigurationContext: - TrashbinContext: coreApiWebdavDelete: @@ -332,7 +316,6 @@ default: contexts: - FeatureContext: *common_feature_context_params - WebDavPropertiesContext: - - AppConfigurationContext: coreApiWebdavProperties2: paths: @@ -341,7 +324,6 @@ default: contexts: - FeatureContext: *common_feature_context_params - WebDavPropertiesContext: - - AppConfigurationContext: coreApiWebdavUpload1: paths: @@ -381,7 +363,6 @@ default: - PublicWebDavContext: - FilesVersionsContext: - WebDavPropertiesContext: - - AppConfigurationContext: coreApiWebdavEtagPropagation2: paths: @@ -393,7 +374,6 @@ default: - PublicWebDavContext: - FilesVersionsContext: - WebDavPropertiesContext: - - AppConfigurationContext: extensions: rdx\behatvars\BehatVariablesExtension: ~ diff --git a/tests/acceptance/features/bootstrap/AppConfigurationContext.php b/tests/acceptance/features/bootstrap/AppConfigurationContext.php deleted file mode 100644 index 59f83b399..000000000 --- a/tests/acceptance/features/bootstrap/AppConfigurationContext.php +++ /dev/null @@ -1,574 +0,0 @@ - - * @author Sergio Bertolin - * @author Phillip Davis - * @copyright Copyright (c) 2018, ownCloud GmbH - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, - * as published by the Free Software Foundation; - * either version 3 of the License, or any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see - * - */ - -use Behat\Behat\Hook\Scope\BeforeScenarioScope; -use GuzzleHttp\Exception\GuzzleException; -use PHPUnit\Framework\Assert; -use TestHelpers\AppConfigHelper; -use TestHelpers\OcsApiHelper; -use Behat\Gherkin\Node\TableNode; -use Behat\Behat\Context\Context; -use Psr\Http\Message\ResponseInterface; - -/** - * AppConfiguration trait - */ -class AppConfigurationContext implements Context { - private FeatureContext $featureContext; - - /** - * @When /^the administrator sets parameter "([^"]*)" of app "([^"]*)" to ((?:'[^']*')|(?:"[^"]*"))$/ - * - * @param string $parameter - * @param string $app - * @param string $value - * - * @return void - * @throws Exception - */ - public function adminSetsServerParameterToUsingAPI( - string $parameter, - string $app, - string $value - ):void { - // The capturing group of the regex always includes the quotes at each - // end of the captured string, so trim them. - $value = \trim($value, $value[0]); - $this->modifyAppConfig($app, $parameter, $value); - } - - /** - * @Then the capabilities setting of :capabilitiesApp path :capabilitiesPath should be :expectedValue - * @Given the capabilities setting of :capabilitiesApp path :capabilitiesPath has been confirmed to be :expectedValue - * - * @param string $capabilitiesApp the "app" name in the capabilities response - * @param string $capabilitiesPath the path to the element - * @param string $expectedValue - * - * @return void - * @throws Exception - */ - public function theCapabilitiesSettingOfAppParameterShouldBe( - string $capabilitiesApp, - string $capabilitiesPath, - string $expectedValue - ):void { - $this->theAdministratorGetsCapabilitiesCheckResponse(); - $actualValue = $this->getAppParameter($capabilitiesApp, $capabilitiesPath); - - Assert::assertEquals( - $expectedValue, - $actualValue, - __METHOD__ - . " $capabilitiesApp path $capabilitiesPath should be $expectedValue but is $actualValue" - ); - } - - /** - * @param string $capabilitiesApp the "app" name in the capabilities response - * @param string $capabilitiesPath the path to the element - * - * @return string - * @throws Exception - */ - public function getAppParameter(string $capabilitiesApp, string $capabilitiesPath):string { - return $this->getParameterValueFromXml( - $this->getCapabilitiesXml(__METHOD__), - $capabilitiesApp, - $capabilitiesPath - ); - } - - /** - * @When user :username retrieves the capabilities using the capabilities API - * - * @param string $username - * - * @return void - * @throws GuzzleException - * @throws JsonException - */ - public function userRetrievesCapabilities(string $username): void { - $user = $this->featureContext->getActualUsername($username); - $this->featureContext->setResponse($this->userGetsCapabilities($user, true)); - } - - /** - * - * @param string $username - * @param boolean $formatJson // if true then formats the response in json - * - * @return ResponseInterface - * @throws GuzzleException - * @throws JsonException - */ - public function userGetsCapabilities(string $username, ?bool $formatJson = false):ResponseInterface { - $user = $this->featureContext->getActualUsername($username); - $password = $this->featureContext->getPasswordForUser($user); - return OcsApiHelper::sendRequest( - $this->featureContext->getBaseUrl(), - $user, - $password, - 'GET', - '/cloud/capabilities' . ($formatJson ? '?format=json' : ''), - $this->featureContext->getStepLineRef(), - [], - $this->featureContext->getOcsApiVersion() - ); - } - - /** - * @Given user :username has retrieved the capabilities - * - * @param string $username - * - * @return void - * @throws Exception - */ - public function userGetsCapabilitiesCheckResponse(string $username):void { - $this->featureContext->theHTTPStatusCodeShouldBe(200, '', $this->userGetsCapabilities($username)); - } - - /** - * @When the user retrieves the capabilities using the capabilities API - * - * @return void - */ - public function theUserGetsCapabilities():void { - $this->featureContext->setResponse($this->userGetsCapabilities($this->featureContext->getCurrentUser())); - } - - /** - * @Given the user has retrieved the capabilities - * - * @return void - * @throws Exception - */ - public function theUserGetsCapabilitiesCheckResponse():void { - $this->userGetsCapabilitiesCheckResponse($this->featureContext->getCurrentUser()); - } - - /** - * @return string - * @throws Exception|GuzzleException - */ - public function getAdminUsernameForCapabilitiesCheck():string { - if (\TestHelpers\OcisHelper::isTestingOnReva()) { - // When testing on reva we don't have a user called "admin" to use - // to access the capabilities. So create an ordinary user on-the-fly - // with a default password. That user should be able to get a - // capabilities response that the test can process. - $adminUsername = "PseudoAdminForRevaTest"; - $createdUsers = $this->featureContext->getCreatedUsers(); - if (!\array_key_exists($adminUsername, $createdUsers)) { - $this->featureContext->userHasBeenCreated(["userName" => $adminUsername]); - } - } else { - $adminUsername = $this->featureContext->getAdminUsername(); - } - return $adminUsername; - } - - /** - * @When the administrator retrieves the capabilities using the capabilities API - * - * @return void - */ - public function theAdministratorGetsCapabilities():void { - $user = $this->getAdminUsernameForCapabilitiesCheck(); - $this->featureContext->setResponse($this->userGetsCapabilities($user, true)); - } - - /** - * @Given the administrator has retrieved the capabilities - * - * @return void - * @throws Exception - */ - public function theAdministratorGetsCapabilitiesCheckResponse():void { - $this->userGetsCapabilitiesCheckResponse($this->getAdminUsernameForCapabilitiesCheck()); - } - - /** - * @param string $exceptionText text to put at the front of exception messages - * - * @return SimpleXMLElement latest retrieved capabilities in XML format - * @throws Exception - */ - public function getCapabilitiesXml(string $exceptionText = ''): SimpleXMLElement { - if ($exceptionText === '') { - $exceptionText = __METHOD__; - } - return $this->featureContext->getResponseXml(null, $exceptionText)->data->capabilities; - } - - /** - * @param string $exceptionText text to put at the front of exception messages - * - * @return SimpleXMLElement latest retrieved version data in XML format - * @throws Exception - */ - public function getVersionXml(string $exceptionText = ''): SimpleXMLElement { - if ($exceptionText === '') { - $exceptionText = __METHOD__; - } - return $this->featureContext->getResponseXml(null, $exceptionText)->data->version; - } - - /** - * @param SimpleXMLElement $xml of the capabilities - * @param string $capabilitiesApp the "app" name in the capabilities response - * @param string $capabilitiesPath the path to the element - * - * @return string - */ - public function getParameterValueFromXml( - SimpleXMLElement $xml, - string $capabilitiesApp, - string $capabilitiesPath - ):string { - $path_to_element = \explode('@@@', $capabilitiesPath); - $answeredValue = $xml->{$capabilitiesApp}; - foreach ($path_to_element as $element) { - $nameIndexParts = \explode('[', $element); - if (isset($nameIndexParts[1])) { - // This part of the path should be something like "some_element[1]" - // Separately extract the name and the index - $name = $nameIndexParts[0]; - $index = (int) \explode(']', $nameIndexParts[1])[0]; - // and use those to construct the reference into the next XML level - $answeredValue = $answeredValue->{$name}[$index]; - } else { - if ($element !== "") { - $answeredValue = $answeredValue->{$element}; - } - } - } - - return (string) $answeredValue; - } - - /** - * @param SimpleXMLElement $xml of the capabilities - * @param string $capabilitiesApp the "app" name in the capabilities response - * @param string $capabilitiesPath the path to the element - * - * @return boolean - */ - public function parameterValueExistsInXml( - SimpleXMLElement $xml, - string $capabilitiesApp, - string $capabilitiesPath - ):bool { - $path_to_element = \explode('@@@', $capabilitiesPath); - $answeredValue = $xml->{$capabilitiesApp}; - - foreach ($path_to_element as $element) { - $nameIndexParts = \explode('[', $element); - if (isset($nameIndexParts[1])) { - // This part of the path should be something like "some_element[1]" - // Separately extract the name and the index - $name = $nameIndexParts[0]; - $index = (int) \explode(']', $nameIndexParts[1])[0]; - // and use those to construct the reference into the next XML level - if (isset($answeredValue->{$name}[$index])) { - $answeredValue = $answeredValue->{$name}[$index]; - } else { - // The path ends at this level - return false; - } - } else { - if (isset($answeredValue->{$element})) { - $answeredValue = $answeredValue->{$element}; - } else { - // The path ends at this level - return false; - } - } - } - - return true; - } - - /** - * @param string $app - * @param string $parameter - * @param string $value - * - * @return void - * @throws Exception - */ - public function modifyAppConfig(string $app, string $parameter, string $value):void { - AppConfigHelper::modifyAppConfig( - $this->featureContext->getBaseUrl(), - $this->featureContext->getAdminUsername(), - $this->featureContext->getAdminPassword(), - $app, - $parameter, - $value, - $this->featureContext->getStepLineRef(), - $this->featureContext->getOcsApiVersion() - ); - } - - /** - * @param array $appParameterValues - * - * @return void - * @throws Exception - */ - public function modifyAppConfigs(array $appParameterValues):void { - AppConfigHelper::modifyAppConfigs( - $this->featureContext->getBaseUrl(), - $this->featureContext->getAdminUsername(), - $this->featureContext->getAdminPassword(), - $appParameterValues, - $this->featureContext->getStepLineRef(), - $this->featureContext->getOcsApiVersion() - ); - } - - /** - * @When the administrator adds url :url as trusted server using the testing API - * - * @param string $url - * - * @return void - * @throws GuzzleException - */ - public function theAdministratorAddsUrlAsTrustedServerUsingTheTestingApi(string $url):void { - $adminUser = $this->featureContext->getAdminUsername(); - $response = OcsApiHelper::sendRequest( - $this->featureContext->getBaseUrl(), - $adminUser, - $this->featureContext->getAdminPassword(), - 'POST', - "/apps/testing/api/v1/trustedservers", - $this->featureContext->getStepLineRef(), - ['url' => $this->featureContext->substituteInLineCodes($url)] - ); - $this->featureContext->setResponse($response); - $this->featureContext->pushToLastStatusCodesArrays(); - } - - /** - * Return text that contains the details of the URL, including any differences due to inline codes - * - * @param string $url - * - * @return string - */ - private function getUrlStringForMessage(string $url):string { - $text = $url; - $expectedUrl = $this->featureContext->substituteInLineCodes($url); - if ($expectedUrl !== $url) { - $text .= " ($expectedUrl)"; - } - return $text; - } - - /** - * @param string $url - * - * @return string - */ - private function getNotTrustedServerMessage(string $url):string { - return - "URL " - . $this->getUrlStringForMessage($url) - . " is not a trusted server but should be"; - } - - /** - * @Then url :url should be a trusted server - * - * @param string $url - * - * @return void - * @throws Exception - */ - public function urlShouldBeATrustedServer(string $url):void { - $trustedServers = $this->featureContext->getTrustedServers(); - foreach ($trustedServers as $server => $id) { - if ($server === $this->featureContext->substituteInLineCodes($url)) { - return; - } - } - Assert::fail($this->getNotTrustedServerMessage($url)); - } - - /** - * @Then the trusted server list should include these urls: - * - * @param TableNode $table - * - * @return void - * @throws Exception - */ - public function theTrustedServerListShouldIncludeTheseUrls(TableNode $table):void { - $trustedServers = $this->featureContext->getTrustedServers(); - $expected = $table->getColumnsHash(); - - foreach ($expected as $server) { - $found = false; - foreach ($trustedServers as $url => $id) { - if ($url === $this->featureContext->substituteInLineCodes($server['url'])) { - $found = true; - break; - } - } - if (!$found) { - Assert::fail($this->getNotTrustedServerMessage($server['url'])); - } - } - } - - /** - * @Given the administrator has added url :url as trusted server - * - * @param string $url - * - * @return void - * @throws Exception - * @throws GuzzleException - */ - public function theAdministratorHasAddedUrlAsTrustedServer(string $url):void { - $this->theAdministratorAddsUrlAsTrustedServerUsingTheTestingApi($url); - $status = $this->featureContext->getResponse()->getStatusCode(); - if ($status !== 201) { - throw new \Exception( - __METHOD__ . - " Could not add trusted server " . $this->getUrlStringForMessage($url) - . ". The request failed with status $status" - ); - } - } - - /** - * @When the administrator deletes url :url from trusted servers using the testing API - * - * @param string $url - * - * @return void - * @throws GuzzleException - */ - public function theAdministratorDeletesUrlFromTrustedServersUsingTheTestingApi(string $url):void { - $adminUser = $this->featureContext->getAdminUsername(); - $response = OcsApiHelper::sendRequest( - $this->featureContext->getBaseUrl(), - $adminUser, - $this->featureContext->getAdminPassword(), - 'DELETE', - "/apps/testing/api/v1/trustedservers", - $this->featureContext->getStepLineRef(), - ['url' => $this->featureContext->substituteInLineCodes($url)] - ); - $this->featureContext->setResponse($response); - } - - /** - * @Then url :url should not be a trusted server - * - * @param string $url - * - * @return void - * @throws Exception - */ - public function urlShouldNotBeATrustedServer(string $url):void { - $trustedServers = $this->featureContext->getTrustedServers(); - foreach ($trustedServers as $server => $id) { - if ($server === $this->featureContext->substituteInLineCodes($url)) { - Assert::fail( - "URL " . $this->getUrlStringForMessage($url) - . " is a trusted server but is not expected to be" - ); - } - } - } - - /** - * @When the administrator deletes all trusted servers using the testing API - * - * @return void - * @throws GuzzleException - */ - public function theAdministratorDeletesAllTrustedServersUsingTheTestingApi():void { - $adminUser = $this->featureContext->getAdminUsername(); - $response = OcsApiHelper::sendRequest( - $this->featureContext->getBaseUrl(), - $adminUser, - $this->featureContext->getAdminPassword(), - 'DELETE', - "/apps/testing/api/v1/trustedservers/all", - $this->featureContext->getStepLineRef() - ); - $this->featureContext->setResponse($response); - } - - /** - * @Given the trusted server list is cleared - * - * @return void - * @throws Exception - */ - public function theTrustedServerListIsCleared():void { - $this->theAdministratorDeletesAllTrustedServersUsingTheTestingApi(); - $statusCode = $this->featureContext->getResponse()->getStatusCode(); - if ($statusCode !== 204) { - $contents = $this->featureContext->getResponse()->getBody()->getContents(); - throw new \Exception( - __METHOD__ - . " Failed to clear all trusted servers" . $contents - ); - } - } - - /** - * @Then the trusted server list should be empty - * - * @return void - * @throws Exception - */ - public function theTrustedServerListShouldBeEmpty():void { - $trustedServers = $this->featureContext->getTrustedServers(); - Assert::assertEmpty( - $trustedServers, - __METHOD__ . " Trusted server list is not empty" - ); - } - - /** - * @BeforeScenario - * - * @param BeforeScenarioScope $scope - * - * @return void - */ - public function setUpScenario(BeforeScenarioScope $scope):void { - // Get the environment - $environment = $scope->getEnvironment(); - // Get all the contexts you need in this context - $this->featureContext = $environment->getContext('FeatureContext'); - } -} diff --git a/tests/acceptance/features/bootstrap/CapabilitiesContext.php b/tests/acceptance/features/bootstrap/CapabilitiesContext.php index 2695fca43..5bc3cd88a 100644 --- a/tests/acceptance/features/bootstrap/CapabilitiesContext.php +++ b/tests/acceptance/features/bootstrap/CapabilitiesContext.php @@ -34,6 +34,125 @@ require_once 'bootstrap.php'; class CapabilitiesContext implements Context { private FeatureContext $featureContext; + /** + * This will run before EVERY scenario. + * It will set the properties for this object. + * + * @BeforeScenario + * + * @param BeforeScenarioScope $scope + * + * @return void + */ + public function before(BeforeScenarioScope $scope):void { + // Get the environment + $environment = $scope->getEnvironment(); + // Get all the contexts you need in this context + $this->featureContext = $environment->getContext('FeatureContext'); + } + + /** + * + * @param string $username + * @param boolean $formatJson // if true then formats the response in json + * + * @return ResponseInterface + * @throws GuzzleException + * @throws JsonException + */ + public function userGetsCapabilities(string $username, ?bool $formatJson = false): ResponseInterface { + $user = $this->featureContext->getActualUsername($username); + $password = $this->featureContext->getPasswordForUser($user); + return OcsApiHelper::sendRequest( + $this->featureContext->getBaseUrl(), + $user, + $password, + 'GET', + '/cloud/capabilities' . ($formatJson ? '?format=json' : ''), + $this->featureContext->getStepLineRef(), + [], + $this->featureContext->getOcsApiVersion() + ); + } + + /** + * @return string + * @throws Exception|GuzzleException + */ + public function getAdminUsernameForCapabilitiesCheck(): string { + if (\TestHelpers\OcisHelper::isTestingOnReva()) { + // When testing on reva we don't have a user called "admin" to use + // to access the capabilities. So create an ordinary user on-the-fly + // with a default password. That user should be able to get a + // capabilities response that the test can process. + $adminUsername = "PseudoAdminForRevaTest"; + $createdUsers = $this->featureContext->getCreatedUsers(); + if (!\array_key_exists($adminUsername, $createdUsers)) { + $this->featureContext->userHasBeenCreated(["userName" => $adminUsername]); + } + } else { + $adminUsername = $this->featureContext->getAdminUsername(); + } + return $adminUsername; + } + + /** + * @param SimpleXMLElement $xml of the capabilities + * @param string $capabilitiesApp the "app" name in the capabilities response + * @param string $capabilitiesPath the path to the element + * + * @return string + */ + public function getParameterValueFromXml( + SimpleXMLElement $xml, + string $capabilitiesApp, + string $capabilitiesPath + ): string { + $path_to_element = \explode('@@@', $capabilitiesPath); + $answeredValue = $xml->{$capabilitiesApp}; + foreach ($path_to_element as $element) { + $nameIndexParts = \explode('[', $element); + if (isset($nameIndexParts[1])) { + // This part of the path should be something like "some_element[1]" + // Separately extract the name and the index + $name = $nameIndexParts[0]; + $index = (int) \explode(']', $nameIndexParts[1])[0]; + // and use those to construct the reference into the next XML level + $answeredValue = $answeredValue->{$name}[$index]; + } else { + if ($element !== "") { + $answeredValue = $answeredValue->{$element}; + } + } + } + + return (string) $answeredValue; + } + + /** + * @When user :username retrieves the capabilities using the capabilities API + * + * @param string $username + * + * @return void + * @throws GuzzleException + * @throws JsonException + */ + public function userRetrievesCapabilities(string $username): void { + $user = $this->featureContext->getActualUsername($username); + $this->featureContext->setResponse($this->userGetsCapabilities($user, true)); + } + + /** + * @When the administrator retrieves the capabilities using the capabilities API + * + * @return void + */ + public function theAdministratorGetsCapabilities(): void { + $user = $this->getAdminUsernameForCapabilitiesCheck(); + $this->featureContext->setResponse($this->userGetsCapabilities($user, true)); + } + /** * @Then the major-minor-micro version data in the response should match the version string * @@ -77,48 +196,129 @@ class CapabilitiesContext implements Context { } /** - * @Then the :pathToElement capability of files sharing app should be :value + * @Then the status.php response should include * - * @param string $pathToElement - * @param string $value + * @param PyStringNode $jsonExpected * * @return void * @throws Exception */ - public function theCapabilityOfFilesSharingAppShouldBe( - string $pathToElement, - string $value - ):void { - $this->featureContext->appConfigurationContext->userGetsCapabilitiesCheckResponse( - $this->featureContext->getCurrentUser() - ); - $capabilitiesXML = $this->featureContext->appConfigurationContext->getCapabilitiesXml(__METHOD__); - $actualValue = $this->featureContext->appConfigurationContext->getParameterValueFromXml( - $capabilitiesXML, - "files_sharing", - $pathToElement - ); - Assert::assertEquals( - $value === "EMPTY" ? '' : $value, - $actualValue, - "Expected $pathToElement capability of files sharing app to be $value, but got $actualValue" - ); - } + public function statusPhpRespondedShouldMatch(PyStringNode $jsonExpected): void { + $jsonExpectedDecoded = \json_decode($jsonExpected->getRaw(), true); + $jsonRespondedDecoded = $this->getJsonDecodedResponse(); - /** - * This will run before EVERY scenario. - * It will set the properties for this object. - * - * @BeforeScenario - * - * @param BeforeScenarioScope $scope - * - * @return void - */ - public function before(BeforeScenarioScope $scope):void { - // Get the environment - $environment = $scope->getEnvironment(); - // Get all the contexts you need in this context - $this->featureContext = $environment->getContext('FeatureContext'); + $response = $this->userGetsCapabilities($this->getAdminUsernameForCapabilitiesCheck()); + $this->theHTTPStatusCodeShouldBe(200, '', $response); + $responseXml = $this->getResponseXml($response)->data->capabilities; + $edition = $this->getParameterValueFromXml( + $responseXml, + 'core', + 'status@@@edition' + ); + + if (!\strlen($edition)) { + Assert::fail( + "Cannot get edition from core capabilities" + ); + } + + $product = $this->getParameterValueFromXml( + $responseXml, + 'core', + 'status@@@product' + ); + if (!\strlen($product)) { + Assert::fail( + "Cannot get product from core capabilities" + ); + } + + $productName = $this->getParameterValueFromXml( + $responseXml, + 'core', + 'status@@@productname' + ); + + if (!\strlen($productName)) { + Assert::fail( + "Cannot get productname from core capabilities" + ); + } + + $jsonExpectedDecoded['edition'] = $edition; + $jsonExpectedDecoded['product'] = $product; + $jsonExpectedDecoded['productname'] = $productName; + + // 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, + 'core', + 'status@@@version' + ); + + if (!\strlen($version)) { + Assert::fail( + "Cannot get version from core capabilities" + ); + } + + $versionString = $this->getParameterValueFromXml( + $responseXml, + 'core', + 'status@@@versionstring' + ); + + if (!\strlen($versionString)) { + Assert::fail( + "Cannot get versionstring from core capabilities" + ); + } + + $jsonExpectedDecoded['version'] = $version; + $jsonExpectedDecoded['versionstring'] = $versionString; + $errorMessage = ""; + $errorFound = false; + foreach ($jsonExpectedDecoded as $key => $expectedValue) { + if (\array_key_exists($key, $jsonRespondedDecoded)) { + $actualValue = $jsonRespondedDecoded[$key]; + if ($actualValue !== $expectedValue) { + $errorMessage .= "$key expected value was $expectedValue but actual value was $actualValue\n"; + $errorFound = true; + } + } else { + $errorMessage .= "$key was not found in the status response\n"; + $errorFound = true; + } + } + Assert::assertFalse($errorFound, $errorMessage); + // We have checked that the status.php response has data that matches up with + // data found in the capabilities response and/or the "occ status" command output. + // But the output might be reported wrongly in all of these in the same way. + // So check that the values also seem "reasonable". + $version = $jsonExpectedDecoded['version']; + $versionString = $jsonExpectedDecoded['versionstring']; + Assert::assertMatchesRegularExpression( + "/^\d+\.\d+\.\d+\.\d+$/", + $version, + "version should be in a form like 10.9.8.1 but is $version" + ); + if (\preg_match("/^(\d+\.\d+\.\d+)\.\d+(-[0-9A-Za-z-]+)?(\+[0-9A-Za-z-]+)?$/", $version, $matches)) { + // We should have matched something like 10.9.8 - the first 3 numbers in the version. + // Ignore pre-releases and meta information + Assert::assertArrayHasKey( + 1, + $matches, + "version $version could not match the pattern Major.Minor.Patch" + ); + $majorMinorPatchVersion = $matches[1]; + } else { + Assert::fail("version '$version' does not start in a form like 10.9.8"); + } + Assert::assertStringStartsWith( + $majorMinorPatchVersion, + $versionString, + "versionstring should start with $majorMinorPatchVersion but is $versionString" + ); } } diff --git a/tests/acceptance/features/bootstrap/FeatureContext.php b/tests/acceptance/features/bootstrap/FeatureContext.php index b437bda4c..45ff6e2f2 100644 --- a/tests/acceptance/features/bootstrap/FeatureContext.php +++ b/tests/acceptance/features/bootstrap/FeatureContext.php @@ -163,7 +163,6 @@ class FeatureContext extends BehatVariablesContext { public AuthContext $authContext; public GraphContext $graphContext; public SpacesContext $spacesContext; - public AppConfigurationContext $appConfigurationContext; private array $initialTrustedServer; /** @@ -2290,133 +2289,6 @@ class FeatureContext extends BehatVariablesContext { ); } - /** - * @Then the status.php response should include - * - * @param PyStringNode $jsonExpected - * - * @return void - * @throws Exception - */ - public function statusPhpRespondedShouldMatch(PyStringNode $jsonExpected): void { - $jsonExpectedDecoded = \json_decode($jsonExpected->getRaw(), true); - $jsonRespondedDecoded = $this->getJsonDecodedResponse(); - - $response = $this->appConfigurationContext->userGetsCapabilities($this->appConfigurationContext->getAdminUsernameForCapabilitiesCheck()); - $this->theHTTPStatusCodeShouldBe(200, '', $response); - $responseXml = $this->getResponseXml($response)->data->capabilities; - $edition = $this->appConfigurationContext->getParameterValueFromXml( - $responseXml, - 'core', - 'status@@@edition' - ); - - if (!\strlen($edition)) { - Assert::fail( - "Cannot get edition from core capabilities" - ); - } - - $product = $this->appConfigurationContext->getParameterValueFromXml( - $responseXml, - 'core', - 'status@@@product' - ); - if (!\strlen($product)) { - Assert::fail( - "Cannot get product from core capabilities" - ); - } - - $productName = $this->appConfigurationContext->getParameterValueFromXml( - $responseXml, - 'core', - 'status@@@productname' - ); - - if (!\strlen($productName)) { - Assert::fail( - "Cannot get productname from core capabilities" - ); - } - - $jsonExpectedDecoded['edition'] = $edition; - $jsonExpectedDecoded['product'] = $product; - $jsonExpectedDecoded['productname'] = $productName; - - // 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->appConfigurationContext->getParameterValueFromXml( - $responseXml, - 'core', - 'status@@@version' - ); - - if (!\strlen($version)) { - Assert::fail( - "Cannot get version from core capabilities" - ); - } - - $versionString = $this->appConfigurationContext->getParameterValueFromXml( - $responseXml, - 'core', - 'status@@@versionstring' - ); - - if (!\strlen($versionString)) { - Assert::fail( - "Cannot get versionstring from core capabilities" - ); - } - - $jsonExpectedDecoded['version'] = $version; - $jsonExpectedDecoded['versionstring'] = $versionString; - $errorMessage = ""; - $errorFound = false; - foreach ($jsonExpectedDecoded as $key => $expectedValue) { - if (\array_key_exists($key, $jsonRespondedDecoded)) { - $actualValue = $jsonRespondedDecoded[$key]; - if ($actualValue !== $expectedValue) { - $errorMessage .= "$key expected value was $expectedValue but actual value was $actualValue\n"; - $errorFound = true; - } - } else { - $errorMessage .= "$key was not found in the status response\n"; - $errorFound = true; - } - } - Assert::assertFalse($errorFound, $errorMessage); - // We have checked that the status.php response has data that matches up with - // data found in the capabilities response and/or the "occ status" command output. - // But the output might be reported wrongly in all of these in the same way. - // So check that the values also seem "reasonable". - $version = $jsonExpectedDecoded['version']; - $versionString = $jsonExpectedDecoded['versionstring']; - Assert::assertMatchesRegularExpression( - "/^\d+\.\d+\.\d+\.\d+$/", - $version, - "version should be in a form like 10.9.8.1 but is $version" - ); - if (\preg_match("/^(\d+\.\d+\.\d+)\.\d+(-[0-9A-Za-z-]+)?(\+[0-9A-Za-z-]+)?$/", $version, $matches)) { - // We should have matched something like 10.9.8 - the first 3 numbers in the version. - // Ignore pre-releases and meta information - Assert::assertArrayHasKey( - 1, - $matches, - "version $version could not match the pattern Major.Minor.Patch" - ); - $majorMinorPatchVersion = $matches[1]; - } else { - Assert::fail("version '$version' does not start in a form like 10.9.8"); - } - Assert::assertStringStartsWith( - $majorMinorPatchVersion, - $versionString, - "versionstring should start with $majorMinorPatchVersion but is $versionString" - ); - } - /** * send request to read a server file for core * @@ -3237,13 +3109,10 @@ class FeatureContext extends BehatVariablesContext { // that calls BasicStructure.php $this->ocsContext = new OCSContext(); $this->authContext = new AuthContext(); - $this->appConfigurationContext = new AppConfigurationContext(); $this->ocsContext->before($scope); $this->authContext->setUpScenario($scope); - $this->appConfigurationContext->setUpScenario($scope); $environment->registerContext($this->ocsContext); $environment->registerContext($this->authContext); - $environment->registerContext($this->appConfigurationContext); $scenarioLine = $scope->getScenario()->getLine(); $featureFile = $scope->getFeature()->getFile(); $suiteName = $scope->getSuite()->getName(); @@ -3590,24 +3459,6 @@ class FeatureContext extends BehatVariablesContext { return \substr($foundPath, \strlen($davPath) + 1); } - /** - * After Scenario. restore trusted servers - * - * @param string $server 'LOCAL'/'REMOTE' - * - * @return void - * @throws Exception - */ - public function restoreTrustedServers(string $server): void { - $currentTrustedServers = $this->getTrustedServers($server); - foreach (\array_diff($currentTrustedServers, $this->initialTrustedServer[$server]) as $url => $id) { - $this->appConfigurationContext->theAdministratorDeletesUrlFromTrustedServersUsingTheTestingApi($url); - } - foreach (\array_diff($this->initialTrustedServer[$server], $currentTrustedServers) as $url => $id) { - $this->appConfigurationContext->theAdministratorAddsUrlAsTrustedServerUsingTheTestingApi($url); - } - } - /** * Get the array of trusted servers in format ["url" => "id"] * From 6e43e1f0bae62df9968c19b7c3fd7d0f6ccb8121 Mon Sep 17 00:00:00 2001 From: Saw-jan Date: Tue, 3 Oct 2023 14:10:13 +0545 Subject: [PATCH 2/3] include capabilities context --- tests/acceptance/config/behat-core.yml | 1 + tests/acceptance/features/bootstrap/CapabilitiesContext.php | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/acceptance/config/behat-core.yml b/tests/acceptance/config/behat-core.yml index dd0e9a1ae..3d4ba8e27 100644 --- a/tests/acceptance/config/behat-core.yml +++ b/tests/acceptance/config/behat-core.yml @@ -21,6 +21,7 @@ default: - ChecksumContext: - FilesVersionsContext: - TrashbinContext: + - CapabilitiesContext: coreApiAuth: paths: diff --git a/tests/acceptance/features/bootstrap/CapabilitiesContext.php b/tests/acceptance/features/bootstrap/CapabilitiesContext.php index 5bc3cd88a..497d68b51 100644 --- a/tests/acceptance/features/bootstrap/CapabilitiesContext.php +++ b/tests/acceptance/features/bootstrap/CapabilitiesContext.php @@ -25,6 +25,7 @@ use Behat\Behat\Context\Context; use Behat\Behat\Hook\Scope\BeforeScenarioScope; use PHPUnit\Framework\Assert; +use TestHelpers\OcsApiHelper; require_once 'bootstrap.php'; From 6c3f68f8f9230d6a1bd782133f4ff43977c4ae36 Mon Sep 17 00:00:00 2001 From: Saw-jan Date: Tue, 3 Oct 2023 16:23:13 +0545 Subject: [PATCH 3/3] fix return type --- .../acceptance/features/bootstrap/CapabilitiesContext.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/acceptance/features/bootstrap/CapabilitiesContext.php b/tests/acceptance/features/bootstrap/CapabilitiesContext.php index 497d68b51..90e1beece 100644 --- a/tests/acceptance/features/bootstrap/CapabilitiesContext.php +++ b/tests/acceptance/features/bootstrap/CapabilitiesContext.php @@ -24,6 +24,8 @@ use Behat\Behat\Context\Context; use Behat\Behat\Hook\Scope\BeforeScenarioScope; +use Behat\Gherkin\Node\PyStringNode; +use Psr\Http\Message\ResponseInterface; use PHPUnit\Framework\Assert; use TestHelpers\OcsApiHelper; @@ -206,11 +208,11 @@ class CapabilitiesContext implements Context { */ public function statusPhpRespondedShouldMatch(PyStringNode $jsonExpected): void { $jsonExpectedDecoded = \json_decode($jsonExpected->getRaw(), true); - $jsonRespondedDecoded = $this->getJsonDecodedResponse(); + $jsonRespondedDecoded = $this->featureContext->getJsonDecodedResponse(); $response = $this->userGetsCapabilities($this->getAdminUsernameForCapabilitiesCheck()); - $this->theHTTPStatusCodeShouldBe(200, '', $response); - $responseXml = $this->getResponseXml($response)->data->capabilities; + $this->featureContext->theHTTPStatusCodeShouldBe(200, '', $response); + $responseXml = $this->featureContext->getResponseXml($response)->data->capabilities; $edition = $this->getParameterValueFromXml( $responseXml, 'core',